// this is a mimic of Navigator code in CNetscapeFont::Initialize() // return -1 if failed. int CyaFont::CalculateMeanWidth( HDC hDC, BOOL isUnicode ) { // calculate the mean width m_iMeanWidth = 0; if( isUnicode ) { m_iMeanWidth = CASTINT(MeasureTextWidth( hDC, "\x0W\x0w", 4, NULL, 0 )); } else { m_iMeanWidth = CASTINT(MeasureTextWidth( hDC, "Ww", 2, NULL, 0 )); } m_iMeanWidth = m_iMeanWidth / 2 ; return( m_iMeanWidth ); }
void FormatText::MeasureText(FTTextureFont* font, const char* text, float maxwidth, Vector* sizeOut) { if(maxwidth < 1) { float width = MeasureTextWidth(font, text); float height = FormatText::GetFaceMaxHeight(font);// * 0.75; // Do I really want this scaled? sizeOut->SetXyzw(width, height, 0, 0); return; } else { float outLongestLine = -1; float lines = (float) CountLines(font, text, maxwidth, &outLongestLine); float width = outLongestLine; if(lines == 1) { width = MeasureTextWidth(font, text); } float height = lines * FormatText::GetFaceMaxHeight(font); sizeOut->SetXyzw(width, height, 0, 0); return; } }
void FormatText::PushText(FTTextureFont* font, float x, float y, const char* text, const Vector& colour, AlignX::Enum alignX, AlignY::Enum alignY) { // Apply X alignment local x = self:ApplyAlignX(x, text) { if(alignX == AlignX::Right) // if self.mAlignX == "right" then { x -= MeasureTextWidth(font, text);//local width = self:MeasureText(text) } else if(alignX == AlignX::Center) // elseif self.mAlignX == "center" then { x -= MeasureTextWidth(font, text) / 2; } } // local y = self:ApplyAlignY(y) // The numbers are "ratio-ed" down because the height is from the bottom // of the glyph to the top, regardless of where the baseline. So this is // simple heurisitc that guesses the height of the face above the base line // only. { if(alignY == AlignY::Top) { float maxHeight = FormatText::GetFaceMaxHeight(font) * 0.75; y -= maxHeight; } else if(alignY == AlignY::Center) { float halfMaxHeight = FormatText::GetFaceMaxHeight(font) * 0.25; y -= halfMaxHeight; } } FTTextureFontImpl* impl = (FTTextureFontImpl*) font->GetImpl(); FTGlyphContainer* glyphList = impl->GetGlyphList(); FTCharmap* charMap = glyphList->GetCharmap(); FTVector<FTGlyph*>* glyphVector = glyphList->GetGlyphVector(); glEnable(GL_BLEND); glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); glEnable(GL_TEXTURE_2D); #ifndef ANDROID glPushAttrib(GL_ENABLE_BIT | GL_COLOR_BUFFER_BIT); FTTextureGlyphImpl::ResetActiveTexture(); #endif #ifdef ANDROID impl->PreRender(); #endif int prevC = -1; // local prevC = nil for (int i = 0; text[i] != '\0'; i++) { int c = text[i]; if(prevC != -1) { x += FormatText::GetKern(font, prevC, c); } { // Actually do the render unsigned int charCode = (unsigned int) c; // This isn't just a check! It's lazy loader if(!impl->CheckGlyph(charCode)) { return; } unsigned int index = charMap->GlyphListIndex(charCode); (*glyphVector)[index]->Render(FTPoint(x, y), FTGL::RENDER_ALL); } prevC = c; } #ifndef ANDROID glPopAttrib(); #endif #ifdef ANDROID impl->PostRender(); #endif }
BOOL CyaFont::MeasureTextSize( HDC hDC, jbyte *str, jsize strLen, jint *charLocs, jsize charLoc_len, LPSIZE lpSize) { lpSize->cx = CASTINT(MeasureTextWidth( hDC, str, strLen, charLocs, charLoc_len )); lpSize->cy = GetHeight(); return( lpSize->cx >= 0? TRUE : FALSE ); }