Skip to content

Commit

Permalink
Fix spaces occupancy in TTF::PrepareString (#15971)
Browse files Browse the repository at this point in the history
* Fix spaces occupancy in TTF::PrepareString

* The code responsible for computing the Trailing Blanks Width was duplicated in two locations. This commit refactors it to improve code reusability and maintainability.

* stressGraphics reference files need a minor adjustement

* Move fgTBlankW initialisation

* add a comment
  • Loading branch information
couet committed Jul 3, 2024
1 parent d9b6704 commit a9d4a0e
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 31 deletions.
2 changes: 2 additions & 0 deletions graf2d/graf/inc/TTF.h
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ TTF helper class containing glyphs description.

static void Init();
static void Cleanup();
static void ComputeTrailingBlanksWidth(Int_t n);
static Int_t GetAscent();
static const FT_BBox &GetBox();
static TTGlyph *GetGlyphs();
Expand All @@ -112,6 +113,7 @@ TTF helper class containing glyphs description.
static Int_t GetNumGlyphs();
static FT_Matrix *GetRotMatrix();
static Bool_t GetSmoothing();
static Int_t GetTrailingBlanksWidth();
static Int_t GetWidth();
static void SetHinting(Bool_t state);
static void SetKerning(Bool_t state);
Expand Down
71 changes: 42 additions & 29 deletions graf2d/graf/src/TTF.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,28 @@ Short_t TTF::CharToUnicode(UInt_t code)
return FT_Get_Char_Index(fgFace[fgCurFontIdx], (FT_ULong)code);
}

////////////////////////////////////////////////////////////////////////////////
/// Compute the trailing blanks width. It is use to compute the text width in GetTextExtent
/// `n` is the number of trailing blanks in a string.

void TTF::ComputeTrailingBlanksWidth(Int_t n)
{
fgTBlankW = 0;
if (n) {
FT_Face face = fgFace[fgCurFontIdx];
char space = ' ';
FT_UInt load_flags = FT_LOAD_DEFAULT;
if (!fgHinting) load_flags |= FT_LOAD_NO_HINTING;
FT_Load_Char(face, space, load_flags);

FT_GlyphSlot slot = face->glyph;
FT_Pos advance_x = slot->advance.x;
Int_t advance_x_pixels = advance_x >> 6;

fgTBlankW = advance_x_pixels * n;
}
}

////////////////////////////////////////////////////////////////////////////////
/// Get width (w) and height (h) when text is horizontal.

Expand All @@ -138,7 +160,7 @@ void TTF::GetTextExtent(UInt_t &w, UInt_t &h, char *text)
LayoutGlyphs();
Int_t Xoff = 0; if (fgCBox.xMin < 0) Xoff = -fgCBox.xMin;
Int_t Yoff = 0; if (fgCBox.yMin < 0) Yoff = -fgCBox.yMin;
w = fgCBox.xMax + Xoff + fgTBlankW;
w = fgCBox.xMax + Xoff + GetTrailingBlanksWidth();
h = fgCBox.yMax + Yoff;
}

Expand Down Expand Up @@ -167,7 +189,7 @@ void TTF::GetTextExtent(UInt_t &w, UInt_t &h, wchar_t *text)
LayoutGlyphs();
Int_t Xoff = 0; if (fgCBox.xMin < 0) Xoff = -fgCBox.xMin;
Int_t Yoff = 0; if (fgCBox.yMin < 0) Yoff = -fgCBox.yMin;
w = fgCBox.xMax + Xoff + fgTBlankW;
w = fgCBox.xMax + Xoff + GetTrailingBlanksWidth();
h = fgCBox.yMax + Yoff;
}

Expand Down Expand Up @@ -254,7 +276,6 @@ void TTF::PrepareString(const char *string)
UInt_t index; // Unicode value
Int_t NbTBlank = 0; // number of trailing blanks

fgTBlankW = 0;
fgNumGlyphs = 0;
while (*p) {
index = CharToUnicode((FT_ULong)*p);
Expand All @@ -272,14 +293,7 @@ void TTF::PrepareString(const char *string)
p++;
}

// compute the trailing blanks width. It is use to compute the text
// width in GetTextExtent
if (NbTBlank) {
FT_UInt load_flags = FT_LOAD_DEFAULT;
if (!fgHinting) load_flags |= FT_LOAD_NO_HINTING;
if (FT_Load_Glyph(fgFace[fgCurFontIdx], 3, load_flags)) return;
fgTBlankW = (Int_t)((fgFace[fgCurFontIdx]->glyph->advance.x)>>6)*NbTBlank;
}
ComputeTrailingBlanksWidth(NbTBlank);
}

////////////////////////////////////////////////////////////////////////////////
Expand All @@ -292,7 +306,6 @@ void TTF::PrepareString(const wchar_t *string)
UInt_t index; // Unicode value
Int_t NbTBlank = 0; // number of trailing blanks

fgTBlankW = 0;
fgNumGlyphs = 0;
while (*p) {
index = FT_Get_Char_Index(fgFace[fgCurFontIdx], (FT_ULong)*p);
Expand All @@ -310,14 +323,7 @@ void TTF::PrepareString(const wchar_t *string)
p++;
}

// compute the trailing blanks width. It is use to compute the text
// width in GetTextExtent
if (NbTBlank) {
FT_UInt load_flags = FT_LOAD_DEFAULT;
if (!fgHinting) load_flags |= FT_LOAD_NO_HINTING;
if (FT_Load_Glyph(fgFace[fgCurFontIdx], 3, load_flags)) return;
fgTBlankW = (Int_t)((fgFace[fgCurFontIdx]->glyph->advance.x)>>6)*NbTBlank;
}
ComputeTrailingBlanksWidth(NbTBlank);
}

////////////////////////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -587,63 +593,70 @@ void TTF::Version(Int_t &major, Int_t &minor, Int_t &patch)

Bool_t TTF::GetHinting()
{
return fgHinting;
return fgHinting;
}

////////////////////////////////////////////////////////////////////////////////

Bool_t TTF::GetKerning()
{
return fgKerning;
return fgKerning;
}

////////////////////////////////////////////////////////////////////////////////

Bool_t TTF::GetSmoothing()
{
return fgSmoothing;
return fgSmoothing;
}

////////////////////////////////////////////////////////////////////////////////

Bool_t TTF::IsInitialized()
{
return fgInit;
return fgInit;
}

////////////////////////////////////////////////////////////////////////////////

Int_t TTF::GetWidth()
{
return fgWidth;
return fgWidth;
}

////////////////////////////////////////////////////////////////////////////////

Int_t TTF::GetAscent()
{
return fgAscent;
return fgAscent;
}

////////////////////////////////////////////////////////////////////////////////

Int_t TTF::GetNumGlyphs()
{
return fgNumGlyphs;
return fgNumGlyphs;
}

////////////////////////////////////////////////////////////////////////////////

FT_Matrix *TTF::GetRotMatrix()
{
return fgRotMatrix;
return fgRotMatrix;
}

////////////////////////////////////////////////////////////////////////////////

Int_t TTF::GetTrailingBlanksWidth()
{
return fgTBlankW;
}

////////////////////////////////////////////////////////////////////////////////

const FT_BBox &TTF::GetBox()
{
return fgCBox;
return fgCBox;
}

////////////////////////////////////////////////////////////////////////////////
Expand Down
2 changes: 1 addition & 1 deletion test/stressGraphics.ref
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ Test# PS1Ref# PS1Err# PDFRef# PDFErr# JPGRef# JPGErr# PNGRef# PN
17 20444 400 21520 150 27511 11000 16867 600 20721 400
18 15894 100 19305 200 27187 10300 15925 350 15925 100
19 22521 300 42926 200 43868 17000 19232 11000 22729 300
20 3664 600 14437 100 19824 6300 10396 900 4220 600
20 3664 600 14437 150 19824 6300 10396 900 4220 600
21 17608 600 13900 150 49440 6300 37586 9000 5938 600
22 4866 600 14162 100 30889 10050 21311 1800 4845 600
23 4131 40 15272 100 34038 2500 12258 1400 4162 40
Expand Down
2 changes: 1 addition & 1 deletion test/stressGraphics_builtinzlib.ref
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ Test# PS1Ref# PS1Err# PDFRef# PDFErr# JPGRef# JPGErr# PNGRef# PN
17 20424 400 21328 100 27719 11000 16806 600 20704 400
18 15874 100 19080 100 27682 10300 16076 350 15905 100
19 22678 300 42680 150 44177 17000 20788 11000 22866 300
20 3817 600 14345 100 19907 6300 10674 900 4523 600
20 3817 600 14345 150 19907 6300 10674 900 4523 600
21 17464 600 13900 150 49201 6300 37054 9000 5938 600
22 4875 600 14112 100 31268 10050 22053 1800 4849 600
23 4130 40 15193 50 34790 2500 12156 1400 4161 40
Expand Down

0 comments on commit a9d4a0e

Please sign in to comment.