void FreeTypeFont::doFillGlyphCache(GlyphCache& cache, const std::vector<char32_t>& characters) #endif { /** @bug Crash when atlas is too small */ /* Get glyph codes from characters */ std::vector<FT_UInt> charIndices; charIndices.resize(characters.size()+1); charIndices[0] = 0; std::transform(characters.begin(), characters.end(), charIndices.begin()+1, [this](const char32_t c) { return FT_Get_Char_Index(ftFont, c); }); /* Remove duplicates (e.g. uppercase and lowercase mapped to same glyph) */ std::sort(charIndices.begin(), charIndices.end()); charIndices.erase(std::unique(charIndices.begin(), charIndices.end()), charIndices.end()); /* Sizes of all characters */ std::vector<Vector2i> charSizes; charSizes.reserve(charIndices.size()); for(FT_UInt c: charIndices) { CORRADE_INTERNAL_ASSERT_OUTPUT(FT_Load_Glyph(ftFont, c, FT_LOAD_DEFAULT) == 0); charSizes.push_back(Vector2i(ftFont->glyph->metrics.width, ftFont->glyph->metrics.height)/64); } /* Create texture atlas */ const std::vector<Rectanglei> charPositions = cache.reserve(charSizes); /* Render all characters to the atlas and create character map */ unsigned char* pixmap = new unsigned char[cache.textureSize().product()](); /** @todo Some better way for this */ #ifndef MAGNUM_TARGET_GLES2 Image2D image(ImageFormat::Red, ImageType::UnsignedByte, cache.textureSize(), pixmap); #else Image2D image(Context::current() && Context::current()->isExtensionSupported<Extensions::GL::EXT::texture_rg>() ? ImageFormat::Red : ImageFormat::Luminance, ImageType::UnsignedByte, cache.textureSize(), pixmap); #endif for(std::size_t i = 0; i != charPositions.size(); ++i) { /* Load and render glyph */ /** @todo B&W only if radius != 0 */ FT_GlyphSlot glyph = ftFont->glyph; CORRADE_INTERNAL_ASSERT_OUTPUT(FT_Load_Glyph(ftFont, charIndices[i], FT_LOAD_DEFAULT) == 0); CORRADE_INTERNAL_ASSERT_OUTPUT(FT_Render_Glyph(glyph, FT_RENDER_MODE_NORMAL) == 0); /* Copy rendered bitmap to texture image */ const FT_Bitmap& bitmap = glyph->bitmap; CORRADE_INTERNAL_ASSERT(std::abs(bitmap.width-charPositions[i].width()) <= 2); CORRADE_INTERNAL_ASSERT(std::abs(bitmap.rows-charPositions[i].height()) <= 2); for(Int yin = 0, yout = charPositions[i].bottom(), ymax = bitmap.rows; yin != ymax; ++yin, ++yout) for(Int xin = 0, xout = charPositions[i].left(), xmax = bitmap.width; xin != xmax; ++xin, ++xout) pixmap[yout*cache.textureSize().x() + xout] = bitmap.buffer[(bitmap.rows-yin-1)*bitmap.width + xin]; /* Insert glyph parameters into cache */ cache.insert(charIndices[i], Vector2i(glyph->bitmap_left, glyph->bitmap_top-charPositions[i].height()), charPositions[i]); } /* Set cache image */ cache.setImage({}, image); }
std::vector<std::pair<std::string, Containers::Array<char>>> MagnumFontConverter::doExportFontToData(AbstractFont& font, GlyphCache& cache, const std::string& filename, const std::u32string& characters) const { Utility::Configuration configuration; configuration.setValue("version", 1); configuration.setValue("image", Utility::Directory::filename(filename) + ".tga"); configuration.setValue("originalImageSize", cache.textureSize()); configuration.setValue("padding", cache.padding()); configuration.setValue("fontSize", font.size()); configuration.setValue("ascent", font.ascent()); configuration.setValue("descent", font.descent()); configuration.setValue("lineHeight", font.lineHeight()); /* Compress glyph IDs so the glyphs are in consecutive array, glyph 0 should stay at position 0 */ std::unordered_map<UnsignedInt, UnsignedInt> glyphIdMap; glyphIdMap.reserve(cache.glyphCount()); glyphIdMap.emplace(0, 0); for(const std::pair<UnsignedInt, std::pair<Vector2i, Range2Di>>& glyph: cache) glyphIdMap.emplace(glyph.first, glyphIdMap.size()); /** @todo Save only glyphs contained in @p characters */ /* Inverse map from new glyph IDs to old ones */ std::vector<UnsignedInt> inverseGlyphIdMap(glyphIdMap.size()); for(const std::pair<UnsignedInt, UnsignedInt>& map: glyphIdMap) inverseGlyphIdMap[map.second] = map.first; /* Character->glyph map, map glyph IDs to new ones */ for(const char32_t c: characters) { Utility::ConfigurationGroup* group = configuration.addGroup("char"); const UnsignedInt glyphId = font.glyphId(c); group->setValue("unicode", c); /* Map old glyph ID to new, if not found, map to glyph 0 */ auto found = glyphIdMap.find(glyphId); group->setValue("glyph", found == glyphIdMap.end() ? 0 : glyphIdMap.at(glyphId)); } /* Save glyph properties in order which preserves their IDs, remove padding from the values so they aren't added twice when using the font later */ /** @todo Some better way to handle this padding stuff */ for(UnsignedInt oldGlyphId: inverseGlyphIdMap) { std::pair<Vector2i, Range2Di> glyph = cache[oldGlyphId]; Utility::ConfigurationGroup* group = configuration.addGroup("glyph"); group->setValue("advance", font.glyphAdvance(oldGlyphId)); group->setValue("position", glyph.first+cache.padding()); group->setValue("rectangle", glyph.second.padded(-cache.padding())); } std::ostringstream confOut; configuration.save(confOut); std::string confStr = confOut.str(); Containers::Array<char> confData{confStr.size()}; std::copy(confStr.begin(), confStr.end(), confData.begin()); /* Save cache image */ Image2D image(PixelFormat::Red, PixelType::UnsignedByte); cache.texture().image(0, image); auto tgaData = Trade::TgaImageConverter().exportToData(image); std::vector<std::pair<std::string, Containers::Array<char>>> out; out.emplace_back(filename + ".conf", std::move(confData)); out.emplace_back(filename + ".tga", std::move(tgaData)); return out; }