45 return (
name ==
"wingding.ttf") || (
name.find(
"symbol.ttf") == 0);
71 thread_local FT_Library _library =
nullptr;
72 if ((direction > 0) || !_library) {
73 if (FT_Init_FreeType(&_library)) {
74 Error(
"TTFhandle::InitClose",
"error initializing FreeType");
77 }
else if ((direction < 0) && _library) {
79 FT_Done_FreeType(_library);
95 if (!
fFont->charmap) {
96 Int_t n = face->num_charmaps;
97 for (
Int_t i = 0; i <
n; i++) {
98 FT_CharMap charmap = face->charmaps[i];
99 auto platform = charmap->platform_id;
100 auto encoding = charmap->encoding_id;
101 if ((platform == 3 && encoding == 1) ||
102 (platform == 0 && encoding == 0) ||
103 (platform == 1 && encoding == 0 &&
fFont->is_symbol()))
105 fFont->charmap = charmap;
106 if (FT_Set_Charmap(face, charmap))
107 Error(
"TTF::CharToUnicode",
"error in FT_Set_CharMap");
112 return FT_Get_Char_Index(face, (FT_ULong)code);
123 FT_Face face =
fFont->face;
125 FT_UInt load_flags = FT_LOAD_DEFAULT;
126 if (!
fgHinting) load_flags |= FT_LOAD_NO_HINTING;
127 FT_Load_Char(face, space, load_flags);
129 FT_GlyphSlot slot = face->glyph;
130 FT_Pos advance_x = slot->advance.x;
131 Int_t advance_x_pixels = advance_x >> 6;
190 FT_UInt prev_index = 0;
195 load_flags = FT_LOAD_DEFAULT;
197 load_flags |= FT_LOAD_NO_HINTING;
212 FT_Get_Kerning(face, prev_index, glyph.fIndex,
213 fgHinting ? ft_kerning_default : ft_kerning_unfitted,
217 prev_index = glyph.fIndex;
225 FT_Done_Glyph(glyph.fImage);
226 glyph.fImage =
nullptr;
230 if (FT_Load_Glyph(face, glyph.fIndex, load_flags))
234 if (FT_Get_Glyph(face->glyph, &glyph.fImage))
238 fWidth += face->glyph->advance.x;
242 FT_Vector_Transform(&glyph.fPos,
fRotMatrix.get());
243 if (FT_Glyph_Transform(glyph.fImage,
fRotMatrix.get(), &glyph.fPos))
248 FT_Glyph_Get_CBox(glyph.fImage, ft_glyph_bbox_pixels, &bbox);
249 if (bbox.xMin <
fCBox.xMin)
fCBox.xMin = bbox.xMin;
250 if (bbox.yMin <
fCBox.yMin)
fCBox.yMin = bbox.yMin;
251 if (bbox.xMax >
fCBox.xMax)
fCBox.xMax = bbox.xMax;
252 if (bbox.yMax >
fCBox.yMax)
fCBox.yMax = bbox.yMax;
264 if (FT_Glyph_To_Bitmap(&
fGlyphs[
n].fImage, smooth ||
GetSmoothing() ? ft_render_mode_normal : ft_render_mode_mono,
nullptr, 1))
267 return (FT_BitmapGlyph)
fGlyphs[
n].fImage;
279 if (glyph.fImage && is_lib) {
280 FT_Done_Glyph(glyph.fImage);
281 glyph.fImage =
nullptr;
294 const unsigned char *p = (
const unsigned char*)
string;
323 const wchar_t *p =
string;
328 UInt_t index = FT_Get_Char_Index(face, (FT_ULong) *p);
355#if defined(FREETYPE_PATCH) && \
356 (FREETYPE_MAJOR == 2) && (FREETYPE_MINOR == 1) && (FREETYPE_PATCH == 2)
378 thread_local std::map<std::string, TTFontHandle> _fonts;
385 fFont = &(_fonts.begin()->second);
387 return fFont ? 0 : 1;
391 auto iter = _fonts.find(
name);
392 if (iter != _fonts.end()) {
393 fFont = &iter->second;
398 _fonts[
name] = {
name,
nullptr,
nullptr };
403 for (
auto &font : _fonts) {
404 if (font.second.face) {
405 FT_Done_Face(font.second.face);
406 font.second.face =
nullptr;
426 if (!fontname || !*fontname)
429 const char *basename =
gSystem->BaseName(fontname);
432 Fatal(
"SetTextFont",
"Fail to create font handle for font %s", basename);
442 Error(
"SetTextFont",
"no free type library initialized");
447 const char *ttpath =
gEnv->GetValue(
"Root.TTFontPath", TROOT::GetTTFFontDir());
455 if (FT_New_Face(lib, ttfont, 0, &
fFont->face))
460 slantMat.xx = (1 << 16);
461 slantMat.xy = ((1 << 16) >> 2);
463 slantMat.yy = (1 << 16);
464 FT_Set_Transform(
fFont->face, &slantMat,
nullptr);
496 static const char *fonttable[][2] = {
497 {
"Root.TTFont.0",
"FreeSansBold.otf" },
498 {
"Root.TTFont.1",
"FreeSerifItalic.otf" },
499 {
"Root.TTFont.2",
"FreeSerifBold.otf" },
500 {
"Root.TTFont.3",
"FreeSerifBoldItalic.otf" },
501 {
"Root.TTFont.4",
"texgyreheros-regular.otf" },
502 {
"Root.TTFont.5",
"texgyreheros-italic.otf" },
503 {
"Root.TTFont.6",
"texgyreheros-bold.otf" },
504 {
"Root.TTFont.7",
"texgyreheros-bolditalic.otf" },
505 {
"Root.TTFont.8",
"FreeMono.otf" },
506 {
"Root.TTFont.9",
"FreeMonoOblique.otf" },
507 {
"Root.TTFont.10",
"FreeMonoBold.otf" },
508 {
"Root.TTFont.11",
"FreeMonoBoldOblique.otf" },
509 {
"Root.TTFont.12",
"symbol.ttf" },
510 {
"Root.TTFont.13",
"FreeSerif.otf" },
511 {
"Root.TTFont.14",
"wingding.ttf" },
512 {
"Root.TTFont.15",
"symbol.ttf" },
513 {
"Root.TTFont.STIXGen",
"STIXGeneral.otf" },
514 {
"Root.TTFont.STIXGenIt",
"STIXGeneralItalic.otf" },
515 {
"Root.TTFont.STIXGenBd",
"STIXGeneralBol.otf" },
516 {
"Root.TTFont.STIXGenBdIt",
"STIXGeneralBolIta.otf" },
517 {
"Root.TTFont.STIXSiz1Sym",
"STIXSiz1Sym.otf" },
518 {
"Root.TTFont.STIXSiz1SymBd",
"STIXSiz1SymBol.otf" },
519 {
"Root.TTFont.STIXSiz2Sym",
"STIXSiz2Sym.otf" },
520 {
"Root.TTFont.STIXSiz2SymBd",
"STIXSiz2SymBol.otf" },
521 {
"Root.TTFont.STIXSiz3Sym",
"STIXSiz3Sym.otf" },
522 {
"Root.TTFont.STIXSiz3SymBd",
"STIXSiz3SymBol.otf" },
523 {
"Root.TTFont.STIXSiz4Sym",
"STIXSiz4Sym.otf" },
524 {
"Root.TTFont.STIXSiz4SymBd",
"STIXSiz4SymBol.otf" },
525 {
"Root.TTFont.STIXSiz5Sym",
"STIXSiz5Sym.otf" },
526 {
"Root.TTFont.ME",
"DroidSansFallback.ttf" },
527 {
"Root.TTFont.CJKMing",
"DroidSansFallback.ttf" },
528 {
"Root.TTFont.CJKGothic",
"DroidSansFallback.ttf" }
531 static int fontset = -1;
532 int thisset = fontset;
534 int fontid = fontnumber / 10;
535 if (fontid < 0 || fontid > 31) fontid = 0;
540 const char *ttpath =
gEnv->GetValue(
"Root.TTFontPath",
541 TROOT::GetTTFFontDir());
542 TString fname =
gEnv->GetValue(fonttable[fontid][0], fonttable[fontid][1]);
544 thisset = ttfont ? 0 : 1;
546 Int_t italic = fontid == 15 ? 1 : 0;
547 auto ret =
SetTextFont(
gEnv->GetValue(fonttable[fontid][thisset], fonttable[fontid][1]), italic);
551 if (
ret == 0 && fontid != 12)
564 Error(
"TTFhandle::SetTextSize",
"current font not selected");
569 FT_Error
err = FT_Set_Char_Size(
fFont->face, tsize, tsize, 72, 72);
572 Error(
"TTFhandle::SetTextSize",
"error in FT_Set_Char_Size: 0x%x (input size %f, calc. size 0x%x)",
err, textsize, tsize);
581 FT_Library_Version(
InitClose(), &major, &minor, &patch);
648 fgHandle = std::make_unique<TTFhandle>();
720 static FT_BBox dummy;
737 return fgHandle->CharToUnicode(code);
782 return fgHandle->SetTextFont(fontname, italic);
880 fgHandle->Version(major, minor, patch);
int Int_t
Signed integer 4 bytes (int).
unsigned int UInt_t
Unsigned integer 4 bytes (unsigned int).
bool Bool_t
Boolean (0=false, 1=true) (bool).
short Font_t
Font number (short).
short Short_t
Signed Short integer 2 bytes (short).
float Float_t
Float 4 bytes (float).
Error("WriteTObject","The current directory (%s) is not associated with a file. The object (%s) has not been written.", GetName(), objname)
void Warning(const char *location, const char *msgfmt,...)
Use this function in warning situations.
void Fatal(const char *location, const char *msgfmt,...)
Use this function in case of a fatal error. It will abort the program.
std::unique_ptr< TTFhandle > fgHandle
static TString Format(const char *fmt,...)
Static method which formats a string using a printf style format descriptor and return a TString.
TTF helper class containing glyphs description.
Interface to the freetype 2 library.
static void SetKerning(Bool_t state)
Set kerning flag.
static Bool_t IsInitialized()
static void PrepareString(const char *string)
Put the characters in "string" in the "glyphs" array.
static void GetTextExtent(UInt_t &w, UInt_t &h, const char *text)
Get width (w) and height (h) when text is horizontal.
static Bool_t GetKerning()
static void Version(Int_t &major, Int_t &minor, Int_t &patch)
static Int_t GetTrailingBlanksWidth()
static void SetHinting(Bool_t state)
Set hinting flag.
static void Init()
Init TTF environment.
static void LayoutGlyphs()
Compute the glyphs positions, fgAscent and fgWidth (needed for alignment).
static void SetSmoothing(Bool_t state)
Set smoothing (anti-aliasing) flag.
static void SetRotationMatrix(Float_t angle)
Set the rotation matrix used to rotate the font outlines.
static void CleanupGlyphs()
Remove temporary data created by LayoutGlyphs.
static void SetTextFont(Font_t fontnumber)
Set specified font.
static Short_t CharToUnicode(UInt_t code)
Map char to unicode. Returns 0 in case no mapping exists.
static TTGlyph * GetGlyphs()
static Int_t GetNumGlyphs()
static void ComputeTrailingBlanksWidth(Int_t n)
Compute the trailing blanks width.
virtual ~TTF()
Cleanup TTF environment.
static const FT_BBox & GetBox()
static void GetTextAdvance(UInt_t &a, const char *text)
Get advance (a) when text is horizontal.
static Bool_t GetHinting()
static Bool_t GetSmoothing()
static void SetTextSize(Float_t textsize)
static FT_Matrix * GetRotMatrix()
void GetTextExtent(UInt_t &w, UInt_t &h, const char *text)
Get width (w) and height (h) when text is horizontal.
Bool_t fKerning
use kerning (true by default)
std::unique_ptr< FT_Matrix > fRotMatrix
rotation matrix
std::vector< TTF::TTGlyph > fGlyphs
glyphs
Int_t fAscent
string ascent, used to compute Y alignment
Int_t SelectFontHandle(Int_t arg, const char *name=nullptr)
Return thread_local instance of TTFontHandle for speified font.
Bool_t SetTextSize(Float_t textsize)
Set current text size.
void SetRotationMatrix(Float_t angle)
Set the rotation matrix used to rotate the font outlines.
void PrepareString(const char *string)
Put the characters in "string" in the "glyphs" array.
UInt_t CharToUnicode(UInt_t code)
Map char to unicode. Returns 0 in case no mapping exists.
FT_BitmapGlyph GetGlyphBitmap(UInt_t n, Bool_t smooth=kFALSE)
Return bitmap for specified glyph.
static void SetHinting(Bool_t state)
FT_BBox fCBox
string control box
void SetTextFont(Font_t fontnumber)
Set specified font.
Int_t fTBlankW
trailing blanks width
Int_t GetTrailingBlanksWidth() const
TTFontHandle * fFont
selected font
static Bool_t fgHinting
use hinting (false by default)
void Version(Int_t &major, Int_t &minor, Int_t &patch)
FT_Face GetFontFace() const
Return current font index.
void CleanupGlyphs()
Remove temporary data created by LayoutGlyphs.
void LayoutGlyphs()
Compute the glyphs positions, fgAscent and fgWidth (needed for alignment).
static Bool_t GetSmoothing()
void ComputeTrailingBlanksWidth(Int_t n)
Compute the trailing blanks width.
void GetTextAdvance(UInt_t &a, const char *text)
Get advance (a) when text is horizontal.
static Bool_t fgSmoothing
use anti-aliasing (true when >8 planes, false otherwise)
static FT_Library InitClose(Int_t direction=0)
Initialize or close FreeType library If argument is 0 - just return current handle Library initialize...
Int_t fWidth
string width, used to compute X alignment
static Bool_t GetHinting()
static void SetSmoothing(Bool_t state)
Short_t Max(Short_t a, Short_t b)
Returns the largest of a and b.
Double_t Cos(Double_t)
Returns the cosine of an angle of x radians.
Double_t Sin(Double_t)
Returns the sine of an angle of x radians.