Texture2D * FontDefinitionTTF::getTexture(int index) { TextFontPagesDef *pPages = _textImages->getPages(); if (!pPages) return nullptr; return pPages->getPageAt(index)->getPageTexture(); }
int FontDefinitionTTF::getNumTextures() { TextFontPagesDef *pPages = _textImages->getPages(); if (pPages) { return pPages->getNumPages(); } return 0; }
FontAtlas * FontDefinitionTTF::createFontAtlas() { int numTextures = 0; TextFontPagesDef *pages = m_pTextImages->getPages(); if (pages) numTextures = pages->getNumPages(); else return nullptr; if (!numTextures) return nullptr; FontAtlas *retAtlas = new FontAtlas(*m_pTextImages->getFont()); if (!retAtlas) return 0; for (int c = 0; c < numTextures; ++c) { TextFontPagesDef *pPages = m_pTextImages->getPages(); retAtlas->addTexture(*(pPages->getPageAt(c)->getPageTexture()), c); } // set the common line height retAtlas->setCommonLineHeight(m_fCommonLineHeight * 0.8); for( auto &item: m_aFontLettersDefinitionUTF16 ) { if ( item.second.validDefinition ) { FontLetterDefinition tempDefinition = item.second; tempDefinition.offsetX = 0; tempDefinition.anchorX = 0.0f; tempDefinition.anchorY = 1.0f; retAtlas->addLetterDefinition(tempDefinition); } } // done here return retAtlas; }
bool FontDefinitionTTF::prepareLetterDefinitions(TextFontPagesDef *pageDefs) { // get all the pages TextFontPagesDef *pages = pageDefs; if (!pages) return (false); float maxLineHeight = -1; // loops all the pages for (int cPages = 0; cPages < pages->getNumPages(); ++cPages) { // loops all the lines in this page for (int cLines = 0; cLines<pages->getPageAt(cPages)->getNumLines(); ++cLines) { float posXUV = 0.0; float posYUV = pages->getPageAt(cPages)->getLineAt(cLines)->getY(); int charsCounter = 0; for (int c = 0; c < pages->getPageAt(cPages)->getLineAt(cLines)->getNumGlyph(); ++c) { // the current glyph GlyphDef currentGlyph = pages->getPageAt(cPages)->getLineAt(cLines)->getGlyphAt(c); // letter width float letterWidth = currentGlyph.getRect().size.width; // letter height float letterHeight = pages->getPageAt(cPages)->getLineAt(cLines)->getHeight(); // add this letter definition FontLetterDefinition tempDef; // carloX little hack (this should be done outside the loop) if (posXUV == 0.0) posXUV = currentGlyph.getPadding(); tempDef.validDefinition = currentGlyph.isValid(); if (tempDef.validDefinition) { tempDef.letteCharUTF16 = currentGlyph.getUTF8Letter(); tempDef.width = letterWidth + currentGlyph.getPadding(); tempDef.height = (letterHeight - 1); tempDef.U = (posXUV - 1); tempDef.V = posYUV; tempDef.offsetX = currentGlyph.getRect().origin.x; tempDef.offsetY = currentGlyph.getRect().origin.y; tempDef.textureID = cPages; tempDef.commonLineHeight = currentGlyph.getCommonHeight(); // take from pixels to points tempDef.width = tempDef.width / CC_CONTENT_SCALE_FACTOR(); tempDef.height = tempDef.height / CC_CONTENT_SCALE_FACTOR(); tempDef.U = tempDef.U / CC_CONTENT_SCALE_FACTOR(); tempDef.V = tempDef.V / CC_CONTENT_SCALE_FACTOR(); if (tempDef.commonLineHeight>maxLineHeight) maxLineHeight = tempDef.commonLineHeight; } else { tempDef.letteCharUTF16 = currentGlyph.getUTF8Letter(); tempDef.commonLineHeight = 0; tempDef.width = 0; tempDef.height = 0; tempDef.U = 0; tempDef.V = 0; tempDef.offsetX = 0; tempDef.offsetY = 0; tempDef.textureID = 0; } // add this definition addLetterDefinition(tempDef); // move bounding box to the next letter posXUV += letterWidth + currentGlyph.getPadding(); // next char in the string ++charsCounter; } } } // store the common line height m_fCommonLineHeight = maxLineHeight; // return true; }