SVGnumber WindowsSVGFont::GetStringWidth(const uni_char* text, INT32 len) { UINT32 realLen = 0; SVGnumber stringVisualLen = 0; if(!text) { return 0; } if(len < 0) { realLen = uni_strlen(text); } else { realLen = len; } if(realLen == 0) return 0; for(UINT32 i = 0; i < realLen; i++) { stringVisualLen += GetGlyphAdvance(text[i]); } return stringVisualLen; }
BOOL KG3DFontTexture::GetTextPosExtent(LPCWSTR wszText, int cchText, FLOAT fCursorPos, LPINT lpCharPos, FLOAT cchSpace, BOOL bBorder) { // ASSERT(((m_dwEffect & KG3DUI_TEXT_STYLE_BORDER) != 0) == (bBorder != 0)); if (NULL == lpCharPos || NULL == wszText || -1 > cchText || 0 == cchText) return FALSE; FLOAT penX = 0.0f; FLOAT penEndX = fCursorPos; FLOAT scalingX = m_bVertical ? m_fScalingY : m_fScalingX; FLOAT spaceX = cchSpace * scalingX; LPCWSTR wszToken = wszText; for (; *wszToken; ++wszToken) { WCHAR const charCode = *wszToken; WCHAR const charNextCode = *(wszToken + 1); // hinting and draw FLOAT glyphAdvX = 0.0f; FLOAT glyphAdvY = 0.0f; if (charGlyphHit(charCode)) GetGlyphAdvance(charCode, charNextCode, glyphAdvX, glyphAdvY); penX += (m_bVertical ? glyphAdvY : glyphAdvX) * scalingX + spaceX; if (penX > penEndX + 0.0001f) break; } *lpCharPos = (INT)(wszToken - wszText); return TRUE; }
bool KG3DFontTexture::GetGlyphAdvance(UINT chText, UINT chNext, FLOAT& fX, FLOAT& fY) { FLOAT glyphAdvX = 0.0f; FLOAT glyphAdvY = 0.0f; if (GetGlyphAdvance(chText, glyphAdvX, glyphAdvY)) { FLOAT glyphKernX = 0.0f; FLOAT glyphKernY = 0.0f; if (charGlyphKernAdvance(chText, chNext, glyphKernX, glyphKernY)) { glyphAdvX += glyphKernX; glyphAdvY += glyphKernY; } } fX = glyphAdvX; fY = glyphAdvY; return true; }
void KG3DFontTexture::DrawString(FLOAT x, FLOAT y, FLOAT xSpace, FLOAT ySpace, LPCWSTR wszText) { ASSERT(wszText); while (*wszText != L'\0') { WCHAR wcCurrent = *wszText++; WCHAR wcNext = *wszText; FLOAT xAdvance = 0.0f; FLOAT yAdvance = 0.0f; if (charGlyphHit(wcCurrent) && GetGlyphAdvance(wcCurrent, wcNext, xAdvance, yAdvance)) { if (m_uVertex + 12 >= _countof(m_aVertex)) { ASSERT(m_nTextureID != -1); m_pRenderer->PrepareVertex(m_nTextureID, m_aVertex, m_uVertex); m_uVertex = 0; m_nTextureID = -1; } if (m_bVertical && IsVerticalChar(wcCurrent)) DrawCharVertical(x, y, wcCurrent); else DrawChar(x, y, wcCurrent); x += (xAdvance + xSpace) * m_fScalingX; y += (yAdvance + ySpace) * m_fScalingY; } } if (m_uVertex) { ASSERT(m_nTextureID != -1); m_pRenderer->PrepareVertex(m_nTextureID, m_aVertex, m_uVertex); m_uVertex = 0; m_nTextureID = -1; } }
BOOL KG3DFontTexture::GetTextExtent(LPCWSTR wszText, int cchText, FLOAT &fWidth, FLOAT &fHeight, FLOAT cchSpace, BOOL bBorder) { // ASSERT(((m_dwEffect & KG3DUI_TEXT_STYLE_BORDER) != 0) == (bBorder != 0)); if (wszText == NULL || -1 > cchText || 0 == cchText) return FALSE; FLOAT penX = 0.0f; FLOAT penY = 0.0f; FLOAT spaceX = cchSpace * m_fScalingX; FLOAT spaceY = cchSpace * m_fScalingY; if (cchText == -1) cchText = (int)wcslen(wszText); if (cchText > 0) { for (int nIndex = 0; nIndex < cchText; ++nIndex) { WCHAR wcCurrent = wszText[nIndex]; WCHAR wcNext = wszText[nIndex + 1]; FLOAT glyphAdvX = 0.0f; FLOAT glyphAdvY = 0.0f; if (charGlyphHit(wcCurrent)) GetGlyphAdvance(wcCurrent, wcNext, glyphAdvX, glyphAdvY); penX += glyphAdvX * m_fScalingX + spaceX; penY += glyphAdvY * m_fScalingY + spaceY; } } fWidth = m_bVertical ? GetCharWidth() * m_fScalingX: penX; fHeight = !m_bVertical ? GetCharHeight() * m_fScalingY: penY; return TRUE; }