15#ifdef MYGUI_USE_FREETYPE
18 #include FT_FREETYPE_H
20 #include FT_TRUETYPE_TABLES_H
22 #include FT_WINFONTS_H
24 #ifdef MYGUI_MSDF_FONTS
26 #include "msdfgen-ext.h"
36#ifndef MYGUI_USE_FREETYPE
37 ResourceTrueTypeFont::~ResourceTrueTypeFont() =
default;
39 void ResourceTrueTypeFont::deserialization(xml::ElementPtr _node, Version _version)
41 Base::deserialization(_node, _version);
44 "ResourceTrueTypeFont: TrueType font '"
45 << getResourceName() <<
"' disabled. Define MYGUI_USE_FREETYPE if you need TrueType fonts.");
48 const GlyphInfo* ResourceTrueTypeFont::getGlyphInfo(
Char _id)
const
53 ITexture* ResourceTrueTypeFont::getTextureFont()
const
58 int ResourceTrueTypeFont::getDefaultHeight()
const
63 void ResourceTrueTypeFont::textureInvalidate(ITexture* _texture)
67 std::vector<std::pair<Char, Char>> ResourceTrueTypeFont::getCodePointRanges()
const
69 return std::vector<std::pair<Char, Char>>();
72 Char ResourceTrueTypeFont::getSubstituteCodePoint()
const
77 void ResourceTrueTypeFont::initialise()
81 void ResourceTrueTypeFont::setSource(std::string_view _value)
85 void ResourceTrueTypeFont::setShader(std::string_view _value)
89 void ResourceTrueTypeFont::setSize(
float _value)
93 void ResourceTrueTypeFont::setResolution(
unsigned int _value)
97 void ResourceTrueTypeFont::setHinting(std::string_view _value)
101 void ResourceTrueTypeFont::setAntialias(
bool _value)
105 void ResourceTrueTypeFont::setTabWidth(
float _value)
109 void ResourceTrueTypeFont::setOffsetHeight(
int _value)
113 void ResourceTrueTypeFont::setSubstituteCode(
int _value)
117 void ResourceTrueTypeFont::setDistance(
int _value)
121 void ResourceTrueTypeFont::setMsdfMode(
bool _value)
125 void ResourceTrueTypeFont::setMsdfRange(
int _value)
129 void ResourceTrueTypeFont::addCodePointRange(
Char _first,
Char _second)
133 void ResourceTrueTypeFont::removeCodePointRange(
Char _first,
Char _second)
137 void ResourceTrueTypeFont::setKerningEnabled(
bool _value)
141 void ResourceTrueTypeFont::setDpiScale(
float _value)
145 void ResourceTrueTypeFont::setAutoDpi(
bool _value)
149 float ResourceTrueTypeFont::getKerning(
Char _left,
Char _right)
const
159 void setMax(T& _var,
const T& _newValue)
161 if (_var < _newValue)
165 const std::map<const Char, const uint8> charMask{
170 const uint8 charMaskBlack = 0x00;
171 const uint8 charMaskWhite = 0xFF;
173 std::pair<int, int> integerGlyphSize(
const GlyphInfo& _info,
float _scale)
175 return {(int)std::ceil(_info.width * _scale), (int)std::ceil(_info.height * _scale)};
178 template<
bool LAMode>
181 static PixelFormat::Enum getFormat()
183 if constexpr (LAMode)
184 return PixelFormat::L8A8;
186 return PixelFormat::R8G8B8A8;
189 static size_t getNumBytes()
191 if constexpr (LAMode)
200 static void set(uint8*& _dest, uint8 _luminance, uint8 _alpha)
202 if constexpr (LAMode)
204 *_dest++ = _luminance;
209 *_dest++ = _luminance;
210 *_dest++ = _luminance;
211 *_dest++ = _luminance;
217 template<
bool LAMode,
bool FromSource = false,
bool Antialias = false>
218 struct Pixel : PixelBase<LAMode>
225 static void set(uint8*& _dest, uint8 _luminance, uint8 _alpha, uint8*& _source)
227 if constexpr (FromSource)
230 if constexpr (Antialias)
234 PixelBase<LAMode>::set(_dest, *_source, *_source);
239 PixelBase<LAMode>::set(_dest, _luminance, *_source);
247 PixelBase<LAMode>::set(_dest, _luminance, _alpha);
254 const int ResourceTrueTypeFont::mDefaultGlyphSpacing = 1;
255 const float ResourceTrueTypeFont::mDefaultTabWidth = 8.0f;
256 const float ResourceTrueTypeFont::mSelectedWidth = 1.0f;
257 const float ResourceTrueTypeFont::mCursorWidth = 2.0f;
259 ResourceTrueTypeFont::~ResourceTrueTypeFont()
261 if (mTexture !=
nullptr)
263 RenderManager::getInstance().destroyTexture(mTexture);
268 void ResourceTrueTypeFont::deserialization(xml::ElementPtr _node, Version _version)
270 Base::deserialization(_node, _version);
272 for (
auto node : _node.node())
274 if (node.name() == std::string_view(
"Property"))
276 std::string_view key = node.attribute(
"key").value();
277 std::string_view value = node.attribute(
"value").value();
280 else if (key ==
"Shader")
282 else if (key ==
"Size")
284 else if (key ==
"Resolution")
286 else if (key ==
"Antialias")
288 else if (key ==
"TabWidth")
290 else if (key ==
"OffsetHeight")
292 else if (key ==
"SubstituteCode")
294 else if (key ==
"Distance")
296 else if (key ==
"Hinting")
298 else if (key ==
"SpaceWidth")
303 _node.node().attribute(
"type").value()
304 <<
": Property '" << key <<
"' in font '" << _node.node().attribute(
"name").value()
305 <<
"' is deprecated; remove it to use automatic calculation.");
307 else if (key ==
"CursorWidth")
311 _node.node().attribute(
"type").value()
312 <<
": Property '" << key <<
"' in font '" << _node.node().attribute(
"name").value()
313 <<
"' is deprecated; value ignored.");
315 else if (key ==
"MsdfMode")
317 else if (key ==
"MsdfRange")
319 else if (key ==
"Kerning")
321 else if (key ==
"DpiScale")
323 else if (key ==
"AutoDpi")
326 else if (node.name() == std::string_view(
"Codes"))
329 for (
auto range : node.children(
"Code"))
331 std::string_view range_value{range.attribute(
"range").value()};
332 if (!range_value.empty())
334 std::vector<std::string> parse_range =
utility::split(range_value);
335 if (!parse_range.empty())
339 addCodePointRange(first, last);
346 if (mCharMap.empty())
347 addCodePointRange(0, 0xFFFF);
350 for (
auto range : node.children(
"Code"))
352 std::string_view range_value{range.attribute(
"hide").value()};
353 if (!range_value.empty())
355 std::vector<std::string> parse_range =
utility::split(range_value);
356 if (!parse_range.empty())
360 removeCodePointRange(first, last);
370 const GlyphInfo* ResourceTrueTypeFont::getGlyphInfo(
Char _id)
const
372 auto glyphIter = mGlyphMap.find(_id);
374 if (glyphIter != mGlyphMap.end())
376 return &glyphIter->second;
379 return mSubstituteGlyphInfo;
382 ITexture* ResourceTrueTypeFont::getTextureFont()
const
387 int ResourceTrueTypeFont::getDefaultHeight()
const
389 return mDefaultHeight;
392 void ResourceTrueTypeFont::textureInvalidate(ITexture* _texture)
398 std::vector<std::pair<Char, Char>> ResourceTrueTypeFont::getCodePointRanges()
const
400 std::vector<std::pair<Char, Char>> result;
402 if (!mCharMap.empty())
404 auto iter = mCharMap.begin();
405 auto endIter = mCharMap.end();
408 Char rangeBegin = iter->first;
409 Char rangeEnd = rangeBegin;
412 for (++iter; iter != endIter; ++iter)
414 if (iter->first == rangeEnd + 1)
422 result.emplace_back(rangeBegin, rangeEnd);
425 rangeBegin = rangeEnd = iter->first;
430 result.emplace_back(rangeBegin, rangeEnd);
436 Char ResourceTrueTypeFont::getSubstituteCodePoint()
const
438 return mSubstituteCodePoint;
441 void ResourceTrueTypeFont::addCodePoint(
Char _codePoint)
443 mCharMap.try_emplace(_codePoint, 0);
446 void ResourceTrueTypeFont::addCodePointRange(
Char _first,
Char _second)
448 CharMap::iterator positionHint = mCharMap.lower_bound(_first);
450 if (positionHint != mCharMap.begin())
453 for (
Char i = _first; i <= _second; ++i)
454 positionHint = mCharMap.insert(positionHint, CharMap::value_type(i, 0));
457 void ResourceTrueTypeFont::removeCodePointRange(
Char _first,
Char _second)
459 mCharMap.erase(mCharMap.lower_bound(_first), mCharMap.upper_bound(_second));
462 void ResourceTrueTypeFont::initialise()
465 mDpiScale = Gui::getInstance().getDpiScale();
470 if (mGlyphSpacing == -1)
471 mGlyphSpacing = mDefaultGlyphSpacing;
474 bool laMode = MyGUI::RenderManager::getInstance().isFormatSupported(
475 Pixel<true>::getFormat(),
476 TextureUsage::Static | TextureUsage::Write);
486 ResourceTrueTypeFont::initialiseFreeType<true, true>();
488 ResourceTrueTypeFont::initialiseFreeType<true, false>();
493 ResourceTrueTypeFont::initialiseFreeType<false, true>();
495 ResourceTrueTypeFont::initialiseFreeType<false, false>();
499 template<
bool LAMode,
bool Antialias>
500 void ResourceTrueTypeFont::initialiseFreeType()
506 FT_Library ftLibrary;
508 if (FT_Init_FreeType(&ftLibrary) != 0)
509 MYGUI_EXCEPT(
"ResourceTrueTypeFont: Could not init the FreeType library!");
511 std::vector<std::byte> fontBuffer;
513 FT_Face ftFace = loadFace(ftLibrary, fontBuffer);
515 if (ftFace ==
nullptr)
517 MYGUI_LOG(Error,
"ResourceTrueTypeFont: Could not load the font '" << getResourceName() <<
"'!");
518 FT_Done_FreeType(ftLibrary);
522 #ifdef MYGUI_MSDF_FONTS
523 msdfgen::FontHandle* msdfFont =
nullptr;
527 msdfFont = msdfgen::adoptFreetypeFont(ftFace);
528 mMsdfScale = (double)ftFace->size->metrics.x_ppem;
540 int fontAscent = ftFace->size->metrics.ascender >> 6;
541 int fontDescent = -ftFace->size->metrics.descender >> 6;
543 TT_OS2* os2 = (TT_OS2*)FT_Get_Sfnt_Table(ftFace, ft_sfnt_os2);
547 setMax(fontAscent, os2->usWinAscent * ftFace->size->metrics.y_ppem / ftFace->units_per_EM);
548 setMax(fontDescent, os2->usWinDescent * ftFace->size->metrics.y_ppem / ftFace->units_per_EM);
550 setMax(fontAscent, os2->sTypoAscender * ftFace->size->metrics.y_ppem / ftFace->units_per_EM);
551 setMax(fontDescent, -os2->sTypoDescender * ftFace->size->metrics.y_ppem / ftFace->units_per_EM);
558 mDefaultHeight = fontAscent + fontDescent;
560 if (mDpiScale != 1.0f)
561 mDefaultHeight =
static_cast<int>(std::ceil(mDefaultHeight / mDpiScale));
564 FT_Int32 ftLoadFlags = FT_LOAD_DEFAULT;
568 case HintingUseNative: ftLoadFlags = FT_LOAD_DEFAULT;
break;
569 case HintingForceAuto: ftLoadFlags = FT_LOAD_FORCE_AUTOHINT;
break;
570 case HintingDisableAuto: ftLoadFlags = FT_LOAD_NO_AUTOHINT;
break;
571 case HintingMonochrome: ftLoadFlags = FT_LOAD_TARGET_MONO;
break;
572 case HintingDisableAll:
575 ftLoadFlags = FT_LOAD_NO_HINTING | FT_LOAD_RENDER;
583 GlyphHeightMap glyphHeightMap;
591 for (
auto iter = mCharMap.begin(); iter != mCharMap.end();)
593 const Char& codePoint = iter->first;
594 FT_UInt glyphIndex = FT_Get_Char_Index(ftFace, codePoint);
599 iter = mCharMap.erase(iter);
604 texWidth += createFaceGlyph(glyphIndex, codePoint, fontAscent, ftFace, ftLoadFlags, glyphHeightMap);
605 #ifdef MYGUI_MSDF_FONTS
607 texWidth += createMsdfFaceGlyph(glyphIndex, codePoint, fontAscent, msdfFont, glyphHeightMap);
611 if (iter->second != 0)
614 mCharMap.erase(iter++);
620 if (spaceGlyphIter != mGlyphMap.end())
623 if (mSpaceWidth != 0.0f)
625 texWidth += (int)std::ceil(mSpaceWidth) - (int)std::ceil(spaceGlyphIter->second.width);
626 spaceGlyphIter->second.width = mSpaceWidth;
627 spaceGlyphIter->second.advance = mSpaceWidth;
631 if (mTabWidth == 0.0f)
632 mTabWidth = mDefaultTabWidth * spaceGlyphIter->second.advance;
638 FT_UInt nextGlyphIndex = (FT_UInt)ftFace->num_glyphs;
640 float height = (float)mDefaultHeight;
642 texWidth += createGlyph(
646 texWidth += createGlyph(
650 texWidth += createGlyph(
654 texWidth += createGlyph(
669 texWidth += createFaceGlyph(
676 #ifdef MYGUI_MSDF_FONTS
678 texWidth += createMsdfFaceGlyph(
688 mSubstituteGlyphInfo = &mGlyphMap.find(mSubstituteCodePoint)->second;
695 double averageGlyphHeight = 0.0;
697 for (
const auto& heightItem : glyphHeightMap)
698 averageGlyphHeight += heightItem.first * heightItem.second.size();
700 averageGlyphHeight /= mGlyphMap.size();
707 texWidth = Bitwise::firstPO2From(texWidth);
708 int texHeight = Bitwise::firstPO2From((
int)ceil(averageGlyphHeight) + mGlyphSpacing);
713 while (texWidth > texHeight)
726 if (texHeight > texWidth * 2)
729 int texX = mGlyphSpacing;
730 int texY = mGlyphSpacing;
732 for (
const auto& heightItem : glyphHeightMap)
734 for (
const auto& [key, info] : heightItem.second)
736 auto [glyphWidth, glyphHeight] = integerGlyphSize(*info, mDpiScale);
738 autoWrapGlyphPos(glyphWidth, texWidth, glyphHeight, texX, texY);
741 texX += mGlyphSpacing + glyphWidth;
745 texHeight = Bitwise::firstPO2From(texY + glyphHeightMap.rbegin()->first);
746 }
while (texHeight > texWidth * 2);
754 RenderManager::getInstance().destroyTexture(mTexture);
761 ->createManual(texWidth, texHeight, TextureUsage::Static | TextureUsage::Write, Pixel<LAMode>::getFormat());
762 mTexture->setInvalidateListener(
this);
764 if (!mShader.empty())
765 mTexture->setShader(mShader);
767 uint8* texBuffer =
static_cast<uint8*
>(mTexture->lock(TextureUsage::Write));
769 if (texBuffer !=
nullptr)
772 for (
uint8 *dest = texBuffer, *endDest = dest + texWidth * texHeight * Pixel<LAMode>::getNumBytes();
774 Pixel<LAMode, false, false>::set(dest, mMsdfMode ? charMaskBlack : charMaskWhite, charMaskBlack, dest);
777 renderGlyphs<LAMode, Antialias>(
785 #ifdef MYGUI_MSDF_FONTS
787 renderMsdfGlyphs(glyphHeightMap, msdfFont, texBuffer, texWidth, texHeight);
794 "ResourceTrueTypeFont: Font '"
795 << getResourceName() <<
"' using texture size " << texWidth <<
" x " << texHeight <<
".");
798 "ResourceTrueTypeFont: Font '"
799 << getResourceName() <<
"' using real height " << mDefaultHeight <<
" pixels.");
803 MYGUI_LOG(Error,
"ResourceTrueTypeFont: Error locking texture; pointer is nullptr.");
806 #ifdef MYGUI_MSDF_FONTS
809 msdfgen::destroyFont(msdfFont);
813 FT_Done_Face(ftFace);
814 FT_Done_FreeType(ftLibrary);
817 FT_Face ResourceTrueTypeFont::loadFace(
const FT_Library& _ftLibrary, std::vector<std::byte>& _fontBuffer)
819 FT_Face result =
nullptr;
823 auto datastream = DataManager::getInstance().getDataHolder(mSource);
826 _fontBuffer = datastream->readAll();
828 const auto* fontData =
reinterpret_cast<const FT_Byte*
>(_fontBuffer.data());
829 const auto fontBufferSize =
static_cast<FT_Long
>(_fontBuffer.size());
832 if (FT_New_Memory_Face(_ftLibrary, fontData, fontBufferSize, -1, &result) != 0)
833 MYGUI_EXCEPT(
"ResourceTrueTypeFont: Could not load the font '" << getResourceName() <<
"'!");
835 FT_Long numFaces = result->num_faces;
836 FT_Long faceIndex = 0;
838 FT_Done_Face(result);
841 if (FT_New_Memory_Face(_ftLibrary, fontData, fontBufferSize, faceIndex, &result) != 0)
842 MYGUI_EXCEPT(
"ResourceTrueTypeFont: Could not load the font '" << getResourceName() <<
"'!");
844 if (result->face_flags & FT_FACE_FLAG_SCALABLE)
848 FT_F26Dot6 ftSize = (FT_F26Dot6)(mSize * mDpiScale * (1 << 6));
850 if (FT_Set_Char_Size(result, ftSize, 0, mResolution, mResolution) != 0)
851 MYGUI_EXCEPT(
"ResourceTrueTypeFont: Could not set the font size for '" << getResourceName() <<
"'!");
854 if (mCharMap.empty())
855 addCodePointRange(0, 0xFFFF);
860 FT_WinFNT_HeaderRec fnt;
864 std::map<float, FT_Long> faceSizes;
868 if (FT_Get_WinFNT_Header(result, &fnt) != 0)
869 MYGUI_EXCEPT(
"ResourceTrueTypeFont: Could not load the font '" << getResourceName() <<
"'!");
872 std::make_pair((
float)fnt.nominal_point_size * fnt.vertical_resolution / mResolution, faceIndex));
874 FT_Done_Face(result);
876 if (++faceIndex < numFaces)
877 if (FT_New_Memory_Face(_ftLibrary, fontData, fontBufferSize, faceIndex, &result) != 0)
878 MYGUI_EXCEPT(
"ResourceTrueTypeFont: Could not load the font '" << getResourceName() <<
"'!");
879 }
while (faceIndex < numFaces);
881 auto iter = faceSizes.lower_bound(mSize);
883 faceIndex = (iter != faceSizes.end()) ? iter->second : faceSizes.rbegin()->second;
885 if (FT_New_Memory_Face(_ftLibrary, fontData, fontBufferSize, faceIndex, &result) != 0)
886 MYGUI_EXCEPT(
"ResourceTrueTypeFont: Could not load the font '" << getResourceName() <<
"'!");
890 if (FT_Select_Size(result, 0) != 0)
891 MYGUI_EXCEPT(
"ResourceTrueTypeFont: Could not set the font size for '" << getResourceName() <<
"'!");
895 if (mCharMap.empty())
898 addCodePointRange(0x20, 0x7E);
902 if (fnt.charset == FT_WinFNT_ID_CP1252)
903 addCodePointRange(0xA0, 0xFF);
909 removeCodePointRange(0, 0x1F);
910 removeCodePointRange(0x100, std::numeric_limits<Char>::max());
914 if (fnt.charset == FT_WinFNT_ID_CP1252)
915 removeCodePointRange(0x7F, 0x9F);
917 removeCodePointRange(0x7F, 0xFF);
924 float ResourceTrueTypeFont::getKerning(
Char _left,
Char _right)
const
926 auto iter = mKerningMap.find(std::make_pair(_left, _right));
927 if (iter != mKerningMap.end())
932 void ResourceTrueTypeFont::loadKerning(
const FT_Face& _ftFace)
936 if (!mKerningEnabled)
939 if (!FT_HAS_KERNING(_ftFace))
942 for (
const auto& [leftChar, leftIndex] : mCharMap)
944 for (
const auto& [rightChar, rightIndex] : mCharMap)
947 if (FT_Get_Kerning(_ftFace, leftIndex, rightIndex, FT_KERNING_DEFAULT, &kerning) == 0)
951 mKerningMap[std::make_pair(leftChar, rightChar)] = kerning.x / 64.0f / mDpiScale;
958 void ResourceTrueTypeFont::autoWrapGlyphPos(
int _glyphWidth,
int _texWidth,
int _lineHeight,
int& _texX,
int& _texY)
961 if (_glyphWidth > 0 && _texX + mGlyphSpacing + _glyphWidth > _texWidth)
963 _texX = mGlyphSpacing;
964 _texY += mGlyphSpacing + _lineHeight;
968 GlyphInfo ResourceTrueTypeFont::createFaceGlyphInfo(
Char _codePoint,
int _fontAscent, FT_GlyphSlot _glyph)
const
970 float scale = 1.0f / mDpiScale;
973 std::max((
float)_glyph->bitmap.width, _glyph->metrics.width / 64.0f) * scale,
974 std::max((
float)_glyph->bitmap.rows, _glyph->metrics.height / 64.0f) * scale,
975 _glyph->advance.x / 64.0f * scale,
976 _glyph->metrics.horiBearingX / 64.0f * scale,
977 (_fontAscent - (_glyph->metrics.horiBearingY / 64.0f)) * scale - mOffsetHeight};
980 int ResourceTrueTypeFont::createGlyph(
983 GlyphHeightMap& _glyphHeightMap,
986 auto [width, height] = integerGlyphSize(_glyphInfo, _scale < 0 ? mDpiScale : _scale);
988 mCharMap[_glyphInfo.codePoint] = _glyphIndex;
989 GlyphInfo& info = mGlyphMap.insert(GlyphMap::value_type(_glyphInfo.codePoint, _glyphInfo)).first->second;
990 _glyphHeightMap[height].insert(std::make_pair(_glyphIndex, &info));
992 return (width > 0) ? mGlyphSpacing + width : 0;
995 int ResourceTrueTypeFont::createFaceGlyph(
999 const FT_Face& _ftFace,
1000 FT_Int32 _ftLoadFlags,
1001 GlyphHeightMap& _glyphHeightMap)
1003 if (mGlyphMap.find(_codePoint) == mGlyphMap.end())
1005 if (FT_Load_Glyph(_ftFace, _glyphIndex, _ftLoadFlags) == 0)
1008 createFaceGlyphInfo(_codePoint, _fontAscent, _ftFace->glyph),
1012 "ResourceTrueTypeFont: Cannot load glyph "
1013 << _glyphIndex <<
" for character " << _codePoint <<
" in font '" << getResourceName() <<
"'.");
1017 mCharMap[_codePoint] = _glyphIndex;
1023 template<
bool LAMode,
bool Antialias>
1024 void ResourceTrueTypeFont::renderGlyphs(
1025 const GlyphHeightMap& _glyphHeightMap,
1026 const FT_Library& _ftLibrary,
1027 const FT_Face& _ftFace,
1028 FT_Int32 _ftLoadFlags,
1034 FT_Bitmap_New(&ftBitmap);
1036 int texX = mGlyphSpacing;
1037 int texY = mGlyphSpacing;
1039 for (
const auto& sameHeightGlyphs : _glyphHeightMap)
1041 int glyphHeight = sameHeightGlyphs.first;
1042 for (
const auto& glyph : sameHeightGlyphs.second)
1045 auto [intWidth, intHeight] = integerGlyphSize(info, mDpiScale);
1047 switch (info.codePoint)
1052 renderGlyph<LAMode, false, false>(
1056 charMask.find(info.codePoint)->second,
1068 auto glyphIter = mGlyphMap.find(info.codePoint);
1069 if (glyphIter != mGlyphMap.end())
1071 glyphIter->second.width = 0.0f;
1072 glyphIter->second.uvRect.right = glyphIter->second.uvRect.left;
1079 renderGlyph<LAMode, false, false>(
1083 charMask.find(info.codePoint)->second,
1095 if (FT_Load_Glyph(_ftFace, glyph.first, _ftLoadFlags) != 0)
1099 "ResourceTrueTypeFont: Cannot load glyph "
1100 << glyph.first <<
" for character " << info.codePoint <<
" in font '"
1101 << getResourceName() <<
"'.");
1104 if (FT_Render_Glyph(_ftFace->glyph, FT_RENDER_MODE_NORMAL) != 0)
1108 "ResourceTrueTypeFont: Cannot render glyph "
1109 << glyph.first <<
" for character " << info.codePoint <<
" in font '"
1110 << getResourceName() <<
"'.");
1114 if (_ftFace->glyph->bitmap.buffer !=
nullptr)
1116 uint8* glyphBuffer =
nullptr;
1118 switch (_ftFace->glyph->bitmap.pixel_mode)
1120 case FT_PIXEL_MODE_GRAY: glyphBuffer = _ftFace->glyph->bitmap.buffer;
break;
1122 case FT_PIXEL_MODE_MONO:
1124 if (FT_Bitmap_Convert(_ftLibrary, &_ftFace->glyph->bitmap, &ftBitmap, 1) == 0)
1127 for (
uint8 *p = ftBitmap.buffer, *endP = p + ftBitmap.width * ftBitmap.rows;
1132 glyphBuffer = ftBitmap.buffer;
1137 if (glyphBuffer !=
nullptr)
1138 renderGlyph<LAMode, true, Antialias>(
1149 _ftFace->glyph->bitmap.width,
1150 _ftFace->glyph->bitmap.rows,
1159 FT_Bitmap_Done(_ftLibrary, &ftBitmap);
1162 template<
bool LAMode,
bool UseBuffer,
bool Antialias>
1163 void ResourceTrueTypeFont::renderGlyph(
1176 uint8* _glyphBuffer)
1178 autoWrapGlyphPos(_bitmapWidth, _texWidth, _lineHeight, _texX, _texY);
1180 uint8* dest = _texBuffer + (_texY * _texWidth + _texX) * Pixel<LAMode>::getNumBytes();
1183 ptrdiff_t destNextRow = (_texWidth - _bitmapWidth) * Pixel<LAMode>::getNumBytes();
1185 if (!mMsdfMode || !UseBuffer)
1187 for (
int j = _bitmapHeight; j > 0; --j)
1190 for (i = _bitmapWidth; i > 1; i -= 2)
1192 Pixel<LAMode, UseBuffer, Antialias>::set(dest, _luminance0, _alpha, _glyphBuffer);
1193 Pixel<LAMode, UseBuffer, Antialias>::set(dest, _luminance1, _alpha, _glyphBuffer);
1197 Pixel<LAMode, UseBuffer, Antialias>::set(dest, _luminance0, _alpha, _glyphBuffer);
1199 dest += destNextRow;
1204 for (
int y = 0; y < _bitmapHeight; ++y)
1206 for (
int x = 0; x < _bitmapWidth; ++x)
1208 for (
int i = 0; i < 3; ++i)
1210 *dest++ = *_glyphBuffer++;
1215 dest += destNextRow;
1220 _info.uvRect.left = (float)_texX / _texWidth;
1221 _info.uvRect.top = (float)_texY / _texHeight;
1222 _info.uvRect.right = (float)(_texX + _info.width * mDpiScale) / _texWidth;
1223 _info.uvRect.bottom = (float)(_texY + _info.height * mDpiScale) / _texHeight;
1225 if (_bitmapWidth > 0)
1226 _texX += mGlyphSpacing + _bitmapWidth;
1229 #ifdef MYGUI_MSDF_FONTS
1230 GlyphInfo ResourceTrueTypeFont::createMsdfFaceGlyphInfo(
1232 const msdfgen::Shape& _shape,
1236 msdfgen::Shape::Bounds bounds = _shape.getBounds();
1237 double range = mMsdfRange / 2.0;
1238 if (_shape.contours.empty())
1240 bounds = {0, 0, 0, 0};
1246 (bounds.r - bounds.l) * mMsdfScale + 2 * range,
1247 (bounds.t - bounds.b) * mMsdfScale + 2 * range,
1248 _advance * mMsdfScale,
1249 bounds.l * mMsdfScale - range,
1250 _fontAscent - bounds.t * mMsdfScale - mOffsetHeight - range);
1253 int ResourceTrueTypeFont::createMsdfFaceGlyph(
1254 FT_UInt _glyphIndex,
1257 msdfgen::FontHandle* _fontHandle,
1258 GlyphHeightMap& _glyphHeightMap)
1260 if (mGlyphMap.find(_codePoint) == mGlyphMap.end())
1262 msdfgen::Shape shape;
1264 if (msdfgen::loadGlyph(shape, _fontHandle, _codePoint, msdfgen::FONT_SCALING_EM_NORMALIZED, &advance))
1267 createMsdfFaceGlyphInfo(_codePoint, shape, advance, _fontAscent),
1272 "ResourceTrueTypeFont: Cannot load msdf glyph for character "
1273 << _codePoint <<
" in font '" << getResourceName() <<
"'.");
1277 mCharMap[_codePoint] = _glyphIndex;
1283 void ResourceTrueTypeFont::renderMsdfGlyphs(
1284 const GlyphHeightMap& _glyphHeightMap,
1285 msdfgen::FontHandle* _fontHandle,
1290 int texX = mGlyphSpacing, texY = mGlyphSpacing;
1292 for (
const auto& sameHeightGlyphs : _glyphHeightMap)
1294 int glyphHeight = sameHeightGlyphs.first;
1295 for (
const auto& glyph : sameHeightGlyphs.second)
1299 switch (info.codePoint)
1304 renderGlyph<false, false, false>(
1308 charMask.find(info.codePoint)->second,
1315 (
int)std::ceil(info.width),
1316 (
int)std::ceil(info.height));
1320 auto glyphIter = mGlyphMap.find(info.codePoint);
1321 if (glyphIter != mGlyphMap.end())
1323 glyphIter->second.width = 0.0f;
1324 glyphIter->second.uvRect.right = glyphIter->second.uvRect.left;
1326 glyphIter->second.height = glyphIter->second.height / 2;
1327 float uvHeight = glyphIter->second.uvRect.bottom - glyphIter->second.uvRect.top;
1328 glyphIter->second.uvRect.top += uvHeight / 4;
1329 glyphIter->second.uvRect.bottom -= uvHeight / 4;
1336 renderGlyph<false, false, false>(
1340 charMask.find(info.codePoint)->second,
1347 (
int)std::ceil(info.width),
1348 (
int)std::ceil(info.height));
1352 msdfgen::Shape shape;
1353 if (msdfgen::loadGlyph(shape, _fontHandle, info.codePoint, msdfgen::FONT_SCALING_EM_NORMALIZED))
1355 msdfgen::Shape::Bounds bounds = shape.getBounds();
1356 double range = mMsdfRange / 2.0;
1357 if (shape.contours.empty())
1359 bounds = {0, 0, 0, 0};
1364 msdfgen::edgeColoringSimple(shape, 3.0);
1366 msdfgen::Bitmap<float, 3> msdf(
1367 (
int)std::ceil((bounds.r - bounds.l) * mMsdfScale + 2 * range),
1368 (
int)std::ceil((bounds.t - bounds.b) * mMsdfScale + 2 * range));
1369 msdfgen::generateMSDF(
1372 mMsdfRange / mMsdfScale,
1373 msdfgen::Vector2(mMsdfScale, mMsdfScale),
1374 msdfgen::Vector2(-bounds.l + range / mMsdfScale, -bounds.b + range / mMsdfScale));
1384 uint8* glyphBuffer =
new uint8[msdf.width() * msdf.height() * 3];
1385 uint8* glyphBufferPointer = glyphBuffer;
1386 for (
int y = 0; y < msdf.height(); ++y)
1388 for (
int x = 0; x < msdf.width(); ++x)
1390 for (
int i = 0; i < 3; ++i)
1393 *glyphBufferPointer++ =
1394 msdfgen::pixelFloatToByte(msdf(x, msdf.height() - y - 1)[2 - i]);
1399 renderGlyph<false, true, false>(
1413 delete[] glyphBuffer;
1419 "ResourceTrueTypeFont: Cannot render glyph for character "
1420 << info.codePoint <<
" in font '" << getResourceName() <<
"'.");
1429 void ResourceTrueTypeFont::setSource(std::string_view _value)
1434 void ResourceTrueTypeFont::setShader(std::string_view _value)
1439 void ResourceTrueTypeFont::setSize(
float _value)
1444 void ResourceTrueTypeFont::setResolution(
unsigned int _value)
1446 mResolution = _value;
1449 void ResourceTrueTypeFont::setHinting(std::string_view _value)
1451 if (_value ==
"use_native")
1452 mHinting = HintingUseNative;
1453 else if (_value ==
"force_auto")
1454 mHinting = HintingForceAuto;
1455 else if (_value ==
"disable_auto")
1456 mHinting = HintingDisableAuto;
1457 else if (_value ==
"disable_all")
1458 mHinting = HintingDisableAll;
1459 else if (_value ==
"monochrome")
1460 mHinting = HintingMonochrome;
1462 mHinting = HintingUseNative;
1465 void ResourceTrueTypeFont::setAntialias(
bool _value)
1467 mAntialias = _value;
1470 void ResourceTrueTypeFont::setTabWidth(
float _value)
1475 void ResourceTrueTypeFont::setOffsetHeight(
int _value)
1477 mOffsetHeight = _value;
1480 void ResourceTrueTypeFont::setSubstituteCode(
int _value)
1482 mSubstituteCodePoint = _value;
1485 void ResourceTrueTypeFont::setDistance(
int _value)
1487 mGlyphSpacing = _value;
1490 void ResourceTrueTypeFont::setMsdfMode(
bool _value)
1492 #ifndef MYGUI_MSDF_FONTS
1496 "MsdfMode flag ignored Define MYGUI_MSDF_FONTS if you need msdf fonts, msdf mode ignored.");
1502 void ResourceTrueTypeFont::setMsdfRange(
int _value)
1504 mMsdfRange = _value;
1507 void ResourceTrueTypeFont::setKerningEnabled(
bool _value)
1509 mKerningEnabled = _value;
1512 void ResourceTrueTypeFont::setDpiScale(
float _value)
1517 void ResourceTrueTypeFont::setAutoDpi(
bool _value)
#define MYGUI_EXCEPT(dest)
#define MYGUI_LOG(level, text)
unsigned int parseUInt(std::string_view _value)
bool parseBool(std::string_view _value)
float parseFloat(std::string_view _value)
__attribute__((visibility("default"))) void trim(std std::string toString(T _value)
std::vector< std::string > split(std::string_view _source, std::string_view _delims)
int parseInt(std::string_view _value)
GlyphInfo(Char _codePoint=0U, float _width=0.0f, float _height=0.0f, float _advance=0.0f, float _bearingX=0.0f, float _bearingY=0.0f, const FloatRect &_uvRect=FloatRect())