FontAtlas * FontAtlasCache::getFontAtlasCharMap(const std::string& plistFile) { std::string atlasName = generateFontName(plistFile, 0, GlyphCollection::CUSTOM,false); FontAtlas *tempAtlas = _atlasMap[atlasName]; if ( !tempAtlas ) { Font *font = FontCharMap::create(plistFile); if(font) { tempAtlas = font->createFontAtlas(); if (tempAtlas) _atlasMap[atlasName] = tempAtlas; } else { return nullptr; } } else { tempAtlas->retain(); } return tempAtlas; }
FontAtlas * FontAtlasCache::getFontAtlasTTF(const std::string& fontFileName, int size, GlyphCollection glyphs, const char *customGlyphs, bool useDistanceField) { std::string atlasName = generateFontName(fontFileName, size, glyphs, useDistanceField); FontAtlas *tempAtlas = _atlasMap[atlasName]; if ( !tempAtlas ) { FontFreeType *font = FontFreeType::create(fontFileName, size, glyphs, customGlyphs); if (font) { font->setDistanceFieldEnabled(useDistanceField); tempAtlas = font->createFontAtlas(); if (tempAtlas) _atlasMap[atlasName] = tempAtlas; } else { return nullptr; } } else { tempAtlas->retain(); } return tempAtlas; }
FontAtlas * FontAtlasCache::getFontAtlasCharMap(Texture2D* texture, int itemWidth, int itemHeight, int startCharMap) { char tmp[30]; sprintf(tmp,"name:%u_%d_%d_%d",texture->getName(),itemWidth,itemHeight,startCharMap); std::string atlasName = generateFontName(tmp, 0, GlyphCollection::CUSTOM,false); FontAtlas *tempAtlas = _atlasMap[atlasName]; if ( !tempAtlas ) { Font *font = FontCharMap::create(texture,itemWidth,itemHeight,startCharMap); if(font) { tempAtlas = font->createFontAtlas(); if (tempAtlas) _atlasMap[atlasName] = tempAtlas; } else { return nullptr; } } else { tempAtlas->retain(); } return tempAtlas; }
FontAtlas * FontDefinitionTTF::createFontAtlas() { FontAtlas *retAtlas = new FontAtlas( *_textImages->getFont() ); if (!retAtlas) return 0; // add all the textures int numTextures = getNumTextures(); if (!numTextures) return 0; for (int c = 0; c<numTextures; ++c) retAtlas->addTexture(*getTexture(c), c); // set the common line height retAtlas->setCommonLineHeight(getCommonLineHeight() * 0.8); for( auto &item: _fontLettersDefinitionUTF16 ) { 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; }
TEST_F (FontAtlasIntegration, AllowsForDifferentSizes) { FontAtlas *bigger = FontManager::get_instance().create_font_atlas(FONT, FONT_SIZE * 2); for (unsigned char i = 'a'; i < 'z'; i++) { EXPECT_LT(atlas->get_character_info(i).width, bigger->get_character_info(i).width); EXPECT_LT(atlas->get_character_info(i).height, bigger->get_character_info(i).height); } delete bigger; }
gui_Slider::gui_Slider(std::string name, FontAtlas &font, float x, float y, float w, float h, float depth, float maxValue, float defaultValue, float minValue) : m_background(x, y, w, h, depth, 1.0f, 1.0f, 1.0f, 1.0f), m_filler(x + 1.0f, y + 1.0f, w - (w*(defaultValue / maxValue)) - 2.0f, h - 2.0f, depth, 0.0f, 200.0f / 255.0f, 245.0f / 255.0f, 1.0f), m_maxValue(maxValue), m_minValue(minValue), m_value(defaultValue), m_text(name, font, glm::vec4((int)(x + (w / 2) - font.getATextWidth(name) / 2), (int)(y + (h / 2) - font.getATextHeight(name) / 2), font.getATextWidth(name), font.getATextHeight(name)), depth, 0.0f, 0.0f, 0.0f), m_x(x), m_y(y), m_w(w), m_h(h), m_isEntered(false), m_isExited(true), m_isPressed(false), m_beenReleased(false), m_format(name) { for (int i = 0; i < 24;i+=4) { m_colors[i] = 1.0; m_colors[i+1] = 1.0; m_colors[i+2] = 1.0; m_colors[i+3] = 1.0; m_ocolors[i] = 0.85; m_ocolors[i + 1] = 0.85; m_ocolors[i + 2] = 0.85; m_ocolors[i + 3] = 1.0; m_pcolors[i] = 0.75; m_pcolors[i + 1] = 0.75; m_pcolors[i + 2] = 0.75; m_pcolors[i + 3] = 1.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; }
FontAtlas * FontCharMap::createFontAtlas() { FontAtlas *tempAtlas = new FontAtlas(*this); if (!tempAtlas) return nullptr; Size s = _texture->getContentSize(); int itemsPerColumn = (int)(s.height / _itemHeight); int itemsPerRow = (int)(s.width / _itemWidth); tempAtlas->setCommonLineHeight(_itemHeight); FontLetterDefinition tempDefinition; tempDefinition.textureID = 0; tempDefinition.offsetX = 0.0f; tempDefinition.offsetY = 0.0f; tempDefinition.validDefinition = true; tempDefinition.width = _itemWidth; tempDefinition.height = _itemHeight; tempDefinition.xAdvance = _itemWidth * CC_CONTENT_SCALE_FACTOR(); int charId = _mapStartChar; for (int row = 0; row < itemsPerColumn; ++row) { for (int col = 0; col < itemsPerRow; ++col) { tempDefinition.letteCharUTF16 = charId; tempDefinition.U = _itemWidth * col; tempDefinition.V = _itemHeight * row; tempAtlas->addLetterDefinition(tempDefinition); charId++; } } tempAtlas->addTexture(_texture,0); return tempAtlas; }
gui_TextField::gui_TextField(FontAtlas &font, float x, float y, float w, float h, float depth, std::string defaultText, float r, float g, float b, float a, float tx, float ty) : m_x(x), m_y(y), m_w(w), m_h(h), m_text(defaultText, font, glm::vec4(x + tx, y + ty + w / 2 - font.getFontHeight() / 2, w, h), depth, r, g, b), m_background(x, y, w, h, depth, r, g, b, a), m_isEntered(false), m_isExited(true), m_isPressed(false), m_cursorPosition(0) { }
bool LabelTextFormatter::createStringSprites(Label *theLabel) { // check for string unsigned int stringLen = theLabel->getStringLenght(); // no string if (stringLen == 0) return false; int nextFontPositionX = 0; int nextFontPositionY = 0; unsigned short prev = -1; Size tmpSize = Size::ZERO; int longestLine = 0; unsigned int totalHeight = 0; int quantityOfLines = theLabel->getStringNumLines(); int commonLineHeight = theLabel->getCommonLineHeight(); totalHeight = commonLineHeight * quantityOfLines; nextFontPositionY = totalHeight; Rect charRect; int charXOffset = 0; int charYOffset = 0; int charAdvance = 0; auto strWhole = theLabel->getUTF16String(); FontAtlas* fontAtlas = theLabel->getFontAtlas(); FontLetterDefinition tempDefinition; auto kernings = theLabel->getKernings(); for (unsigned int i = 0; i < stringLen; i++) { unsigned short c = strWhole[i]; if (fontAtlas->getLetterDefinitionForChar(c, tempDefinition)) { charXOffset = tempDefinition.offsetX; charYOffset = tempDefinition.offsetY; charAdvance = tempDefinition.xAdvance; } else { charXOffset = -1; charYOffset = -1; charAdvance = -1; } if (c == '\n') { nextFontPositionX = 0; nextFontPositionY -= commonLineHeight; theLabel->recordPlaceholderInfo(i); continue; } Point fontPos = Point((float)nextFontPositionX + charXOffset + kernings[i], (float)nextFontPositionY - charYOffset); if( theLabel->recordLetterInfo(CC_POINT_PIXELS_TO_POINTS(fontPos),c,i) == false) { log("WARNING: can't find letter definition in font file for letter: %c", c); continue; } // update kerning nextFontPositionX += charAdvance + kernings[i]; prev = c; if (longestLine < nextFontPositionX) { longestLine = nextFontPositionX; } } float lastCharWidth = tempDefinition.width * CC_CONTENT_SCALE_FACTOR(); // If the last character processed has an xAdvance which is less that the width of the characters image, then we need // to adjust the width of the string to take this into account, or the character will overlap the end of the bounding // box if(charAdvance < lastCharWidth) { tmpSize.width = longestLine - charAdvance + lastCharWidth; } else { tmpSize.width = longestLine; } tmpSize.height = totalHeight; theLabel->setContentSize(CC_SIZE_PIXELS_TO_POINTS(tmpSize)); return true; }