0

I am trying to calculate the width of the unicode character in directwrite without using GDI api's .My issue is some characters are full width and some are half width characters hence below formula doesn't work as leftside bearings and right side bearings vary for diff characters. How do I resolve this issue?

const size_t cSize = strlen((const char *)&globals->def_font.name[1]) + 1;
        wchar_t* fontFamily = new wchar_t[cSize];
        mbstowcs(fontFamily, (const char *)&globals->def_font.name[1], cSize);

        const size_t size = strlen((const char *)data) + 1;
        wchar_t* textData = new wchar_t[size];
        mbstowcs(textData, (const char *)data, size);

        IDWriteFontFace* fontName = GetFontFaceName(fontFamily);
        IDWriteFontFace1* fontName1 = static_cast <IDWriteFontFace1*> (fontName);
        //UINT32 textLength = fontName1->GetGlyphCount();
        UINT32 textLength = (UINT32)wcslen(textData);



        UINT32* pCodePoints = new UINT32[textLength];
        ZeroMemory(pCodePoints, sizeof(UINT32)* textLength);

        UINT16* pGlyphIndices = new UINT16[textLength];
        ZeroMemory(pGlyphIndices, sizeof(UINT16)* textLength);
        for (unsigned int i = 0; i < textLength; ++i)
        {
            pCodePoints[i] = textData[i];
        }

        // Get text length

        HRESULT hr = fontName1->GetGlyphIndices(
            pCodePoints,
            textLength,
            pGlyphIndices
            );
        if (FAILED(hr))
        {
            MessageBox(NULL, L"Get glyph indices failed!", L"Error", 0);
            return 1;
        }

        // Create path geometry
        hr = direct2DFactory->CreatePathGeometry(&pathGeometry);
        if (FAILED(hr))
        {
            MessageBox(NULL, L"Create path geometry failed!", L"Error", 0);
            return 1;
        }

        // Open sink
        hr = pathGeometry->Open(&pGeometrySink);
        if (FAILED(hr))
        {
            MessageBox(NULL, L"Open geometry sink failed!", L"Error", 0);
            return 1;
        }

        float fontSize_ = textFormat->GetFontSize();
        // Get glyph run outline
        hr = fontName->GetGlyphRunOutline(
            fontSize_,              // font size
            pGlyphIndices,
            NULL,
            NULL,
            textLength,
            FALSE,
            FALSE,
            pGeometrySink
            );
        if (FAILED(hr))
        {
            MessageBox(NULL, L"Get glyph run outline failed!", L"Error", 0);
            return 1;
        }
        D2D1_MATRIX_3X2_F   matrix_;
        hdc->GetTransform(&matrix_);


        direct2DFactory->CreateTransformedGeometry(
            pathGeometry,
            matrix_,
            &pTransformedGeometry
            );

        // Close sink
        D2D1_RECT_F rect;

        pTransformedGeometry->GetBounds(matrix_, &rect);
        pGeometrySink->Close();

        DWRITE_GLYPH_METRICS* glyphmetrics = new DWRITE_GLYPH_METRICS[textLength];
        fontName1->GetDesignGlyphMetrics(pGlyphIndices, textLength, glyphmetrics);
        fontName1->GetMetrics(&fontMetrics);
        float boundingBox = fontMetrics.glyphBoxRight - fontMetrics.glyphBoxLeft;
        //Actual point calculation
        // fontSize = textFormat->GetFontSize();
        float fontHeight = ((fontMetrics.ascent + fontMetrics.descent )* fontSize_) / (fontMetrics.designUnitsPerEm); 

        index = *data;
        n_widths[index] = (((glyphmetrics->advanceWidth + glyphmetrics->leftSideBearing + glyphmetrics->rightSideBearing) * fontSize_) / (fontMetrics.designUnitsPerEm)) ;
        n_widths[index] = n_widths[index]* 1.33;**

        //Release the glyphmetrics
        delete[]glyphmetrics;
        //delete[]pCodePoints;
        delete[]pGlyphIndices;
        //delete[]fontFamily;
        delete[]textData;
        glyphmetrics = NULL;

        textLayout->GetMetrics(&textMetrics);
MrLore
  • 3,759
  • 2
  • 28
  • 36
Girija Ram
  • 61
  • 8
  • Why do you want the width of an isolated and lonely character? Better measure the width of the whole string you actually care about directly. – Deduplicator Aug 02 '14 at 14:17
  • As of now i need a solution for knowing the width of each character – Girija Ram Aug 02 '14 at 15:00
  • @GirijaRam Do you want the black box ink width of the character, or just the advance width? It's important to understand how you are planning to use this metric. – Dwayne Robinson Nov 03 '14 at 21:19

0 Answers0