14 #include <fontconfig/fontconfig.h>
19 #include FT_FREETYPE_H
30 #define KERNING_UNKNOWN (-10000)
69 advanceX = GlyphData->advance.x >> 6;
70 advanceY = GlyphData->advance.y >> 6;
71 left = GlyphData->bitmap_left;
72 top = GlyphData->bitmap_top;
73 width = GlyphData->bitmap.width;
74 rows = GlyphData->bitmap.rows;
75 pitch = GlyphData->bitmap.pitch;
78 memcpy(
bitmap, GlyphData->bitmap.buffer, bytes);
113 cGlyph*
Glyph(uint CharCode,
bool AntiAliased =
false)
const;
115 cFreetypeFont(
const char *Name,
int CharHeight,
int CharWidth = 0);
118 virtual int Size(
void)
const override {
return size; }
120 virtual int Width(uint c)
const override;
121 virtual int Width(
const char *s)
const override;
134 int error = FT_Init_FreeType(&
library);
138 if (
face->num_fixed_sizes &&
face->available_sizes) {
141 for (uint sym =
'A'; sym <
'z'; sym++) {
142 FT_UInt glyph_index = FT_Get_Char_Index(
face, sym);
143 error = FT_Load_Glyph(
face, glyph_index, FT_LOAD_DEFAULT);
145 error = FT_Render_Glyph(
face->glyph, FT_RENDER_MODE_NORMAL);
147 if (
int(
face->glyph->bitmap.rows-
face->glyph->bitmap_top) >
bottom)
151 esyslog(
"ERROR: FreeType: error %d in FT_Render_Glyph", error);
154 esyslog(
"ERROR: FreeType: error %d in FT_Load_Glyph", error);
158 error = FT_Set_Char_Size(
face,
164 height = (
face->size->metrics.ascender -
face->size->metrics.descender + 63) / 64;
165 bottom = abs((
face->size->metrics.descender - 63) / 64);
168 esyslog(
"ERROR: FreeType: error %d during FT_Set_Char_Size (font = %s)\n", error, Name);
172 esyslog(
"ERROR: FreeType: load error %d (font = %s)", error, Name);
175 esyslog(
"ERROR: FreeType: initialization error %d (font = %s)", error, Name);
187 if (
Glyph && PrevSym) {
192 FT_UInt glyph_index_prev = FT_Get_Char_Index(
face, PrevSym);
193 FT_Get_Kerning(
face, glyph_index_prev, glyph_index, FT_KERNING_DEFAULT, &delta);
194 kerning = delta.x / 64;
204 if (CharCode == 0xA0)
210 if (g->CharCode() == CharCode)
214 FT_UInt glyph_index = FT_Get_Char_Index(
face, CharCode);
217 int error = FT_Load_Glyph(
face, glyph_index, FT_LOAD_DEFAULT);
219 esyslog(
"ERROR: FreeType: error during FT_Load_Glyph");
221 #if ((FREETYPE_MAJOR == 2 && FREETYPE_MINOR == 1 && FREETYPE_PATCH >= 7) || (FREETYPE_MAJOR == 2 && FREETYPE_MINOR == 2 && FREETYPE_PATCH <= 1))
222 if (AntiAliased || CharCode == 32)
226 error = FT_Render_Glyph(
face->glyph, FT_RENDER_MODE_NORMAL);
228 error = FT_Render_Glyph(
face->glyph, FT_RENDER_MODE_MONO);
230 esyslog(
"ERROR: FreeType: error during FT_Render_Glyph %d, %d\n", CharCode, glyph_index);
237 #define UNKNOWN_GLYPH_INDICATOR '?'
271 #define MAX_BLEND_LEVELS 256
283 if (AntiAliased && !TransparentBackground)
284 memset(BlendLevelIndex, 0xFF,
sizeof(BlendLevelIndex));
294 int kerning =
Kerning(g, prevSym);
297 int symWidth = g->
Width();
300 if (x + symWidth + g->
Left() + kerning > 0) {
301 for (
int row = 0; row < g->
Rows(); row++) {
302 for (
int pitch = 0; pitch < g->
Pitch(); pitch++) {
303 uchar bt = *(buffer + (row * g->
Pitch() + pitch));
306 int px = x + pitch + g->
Left() + kerning;
311 else if (TransparentBackground)
313 else if (BlendLevelIndex[bt] >= 0)
314 bg = BlendLevelIndex[bt];
316 bg = BlendLevelIndex[bt] = Bitmap->
Index(Bitmap->
Blend(ColorFg, ColorBg, bt));
321 for (
int col = 0; col < 8 && col + pitch * 8 <= symWidth; col++) {
331 if (x > Bitmap->
Width() - 1)
353 int kerning =
Kerning(g, prevSym);
356 int symWidth = g->
Width();
359 if (x + symWidth + g->
Left() + kerning > 0) {
360 for (
int row = 0; row < g->
Rows(); row++) {
361 for (
int pitch = 0; pitch < g->
Pitch(); pitch++) {
362 uchar bt = *(buffer + (row * g->
Pitch() + pitch));
368 for (
int col = 0; col < 8 && col + pitch * 8 <= symWidth; col++) {
423 default:
esyslog(
"ERROR: unknown Font %d (%s %d)",
Font, __FUNCTION__, __LINE__);
442 if (!FontNames->
Size()) {
444 FcObjectSet *os = FcObjectSetBuild(FC_FAMILY, FC_STYLE, NULL);
445 FcPattern *pat = FcPatternCreate();
446 FcPatternAddBool(pat, FC_SCALABLE, FcTrue);
448 FcPatternAddInteger(pat, FC_SPACING, FC_MONO);
449 FcFontSet* fontset = FcFontList(NULL, pat, os);
450 for (
int i = 0; i < fontset->nfont; i++) {
451 char *s = (
char *)FcNameUnparse(fontset->fonts[i]);
454 char *c = strchr(s,
':');
456 char *p = strchr(c + 1,
',');
460 char *p = strchr(s,
',');
463 memmove(p, c, strlen(c) + 1);
473 FcFontSetDestroy(fontset);
474 FcPatternDestroy(pat);
475 FcObjectSetDestroy(os);
479 return FontNames->
Size() > 0;
490 FcPattern *pat = FcNameParse((FcChar8 *)fn);
491 FcPatternAddBool(pat, FC_SCALABLE, FcTrue);
492 FcConfigSubstitute(NULL, pat, FcMatchPattern);
493 FcDefaultSubstitute(pat);
495 FcFontSet *fontset = FcFontSort(NULL, pat, FcFalse, NULL, &fresult);
497 for (
int i = 0; i < fontset->nfont; i++) {
499 FcPatternGetBool(fontset->fonts[i], FC_SCALABLE, 0, &scalable);
502 FcPatternGetString(fontset->fonts[i], FC_FILE, 0, &s);
503 FontFileName = (
char *)s;
507 FcFontSetDestroy(fontset);
511 FcPatternDestroy(pat);
519 cString cFont::Bidi(
const char *Ltr)
522 fribidi_set_mirroring(
true);
523 fribidi_set_reorder_nsm(
false);
524 FriBidiCharSet fribidiCharset = FRIBIDI_CHAR_SET_UTF8;
525 int LtrLen = strlen(Ltr);
526 FriBidiCharType Base = FRIBIDI_TYPE_L;
527 FriBidiChar *Logical =
MALLOC(FriBidiChar, LtrLen + 1) ;
528 int RtlLen = fribidi_charset_to_unicode(fribidiCharset,
const_cast<char *
>(Ltr), LtrLen, Logical);
529 FriBidiChar *Visual =
MALLOC(FriBidiChar, LtrLen + 1) ;
531 bool ok = fribidi_log2vis(Logical, RtlLen, &Base, Visual, NULL, NULL, NULL);
533 fribidi_remove_bidi_marks(Visual, RtlLen, NULL, NULL, NULL);
534 Rtl =
MALLOC(
char, RtlLen * 4 + 1);
535 fribidi_unicode_to_charset(fribidiCharset, Visual, RtlLen, Rtl);
585 for (
char *p =
text; *p; ) {
591 Blank = Delim = NULL;
595 else if (sl == 1 && isspace(sym))
598 if (w + cw > Width) {
613 strcpy(s + l + 1, p);
621 if (strchr(
"-.,:;!?_~", *p)) {
650 for (
int i = 0; i < Line; i++) {
659 if ((
eol = strchr(s,
'\n')) != NULL)
tColor GetColor(int x, int y) const
Returns the color at the given coordinates.
void SetIndex(int x, int y, tIndex Index)
Sets the index at the given coordinates to Index.
static const char * SystemCharacterTable(void)
virtual int Width(void) const override
Returns the original character width as requested when the font was created, or 0 if the default widt...
virtual int Width(const char *s) const override
Returns the width of the given string in pixel.
virtual void DrawText(cBitmap *Bitmap, int x, int y, const char *s, tColor ColorFg, tColor ColorBg, int Width) const override
Draws the given text into the Bitmap at position (x, y) with the given colors.
virtual int Width(void) const override
Returns the original character width as requested when the font was created, or 0 if the default widt...
cDummyFont(int CharHeight, int CharWidth)
virtual void DrawText(cPixmap *Pixmap, int x, int y, const char *s, tColor ColorFg, tColor ColorBg, int Width) const override
virtual int Width(uint c) const override
Returns the width of the given character in pixel.
virtual int Height(void) const override
Returns the height of this font in pixel (all characters have the same height).
virtual const char * FontName(void) const
Returns the font name.
static cFont * CreateFont(const char *Name, int CharHeight, int CharWidth=0)
Creates a new font object with the given Name and makes its characters CharHeight pixels high.
virtual int Height(void) const =0
Returns the height of this font in pixel (all characters have the same height).
static void SetFont(eDvbFont Font, const char *Name, int CharHeight)
< Draws the given text into the Pixmap at position (x, y) with the given colors.
static bool GetAvailableFontNames(cStringList *FontNames, bool Monospaced=false)
Queries the font configuration for a list of available font names, which is returned in FontNames.
static const cFont * GetFont(eDvbFont Font)
Gets the given Font, which was previously set by a call to SetFont().
static cString GetFontFileName(const char *FontName)
Returns the actual font file name for the given FontName.
virtual void DrawText(cBitmap *Bitmap, int x, int y, const char *s, tColor ColorFg, tColor ColorBg, int Width) const override
Draws the given text into the Bitmap at position (x, y) with the given colors.
virtual const char * FontName(void) const override
Returns the font name.
virtual int Size(void) const override
Returns the original size as requested when the font was created.
cGlyph * Glyph(uint CharCode, bool AntiAliased=false) const
FT_Library library
Handle to library.
FT_Face face
Handle to face object.
virtual int Width(void) const override
Returns the original character width as requested when the font was created, or 0 if the default widt...
int Kerning(cGlyph *Glyph, uint PrevSym) const
cFreetypeFont(const char *Name, int CharHeight, int CharWidth=0)
virtual int Height(void) const override
Returns the height of this font in pixel (all characters have the same height).
virtual ~cFreetypeFont() override
cList< cGlyph > glyphCacheAntiAliased
cList< cGlyph > glyphCacheMonochrome
uchar * Bitmap(void) const
cGlyph(uint CharCode, FT_GlyphSlotRec_ *GlyphData)
cVector< tKerning > kerningCache
void SetKerningCache(uint PrevSym, int Kerning)
int left
The bitmap's left bearing expressed in integer pixels.
uint CharCode(void) const
int rows
The number of bitmap rows.
virtual ~cGlyph() override
int width
The number of pixels per bitmap row.
int pitch
The pitch's absolute value is the number of bytes taken by one bitmap row, including padding.
int top
The bitmap's top bearing expressed in integer pixels.
int GetKerningCache(uint PrevSym) const
void Add(cListObject *Object, cListObject *After=NULL)
const T * Next(const T *Object) const
< Returns the element immediately before Object in this list, or NULL if Object is the first element ...
const T * First(void) const
Returns the first element in this list, or NULL if the list is empty.
tColor Blend(tColor ColorFg, tColor ColorBg, uint8_t Level) const
Determines a color that consists of a linear blend between ColorFg and ColorBg.
int Index(tColor Color)
Returns the index of the given Color (the first color has index 0).
const cRect & DrawPort(void) const
Returns the pixmap's draw port, which is relative to the view port.
virtual void DrawPixel(const cPoint &Point, tColor Color)=0
Draws the image referenced by the given ImageHandle into this pixmap at the given Point and scales it...
char FontOsd[MAXFONTNAME]
char FontSml[MAXFONTNAME]
char FontFix[MAXFONTNAME]
void Sort(bool IgnoreCase=false)
const char * GetLine(int Line)
Returns the given Line. The first line is numbered 0.
void Set(const char *Text, const cFont *Font, int Width)
Wraps the Text to make it fit into the area defined by the given Width when displayed with the given ...
const char * Text(void)
Returns the full wrapped text.
virtual void Append(T Data)
const char * DefaultFontOsd
const char * DefaultFontSml
const char * DefaultFontFix
#define UNKNOWN_GLYPH_INDICATOR
static int Utf8CharLen(const char *s)
tColor AlphaBlend(tColor ColorFg, tColor ColorBg, uint8_t AlphaLayer)
static const cCursesFont Font
tKerning(uint PrevSym, int Kerning=0)