1 /* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2  * This Source Code Form is subject to the terms of the Mozilla Public
3  * License, v. 2.0. If a copy of the MPL was not distributed with this
4  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
5 
6 #include "gfxFT2FontBase.h"
7 #include "gfxFT2Utils.h"
8 #include "harfbuzz/hb.h"
9 #include "mozilla/Likely.h"
10 #include "mozilla/StaticPrefs_gfx.h"
11 #include "gfxFontConstants.h"
12 #include "gfxFontUtils.h"
13 #include <algorithm>
14 #include <dlfcn.h>
15 
16 #include FT_TRUETYPE_TAGS_H
17 #include FT_TRUETYPE_TABLES_H
18 #include FT_ADVANCES_H
19 #include FT_MULTIPLE_MASTERS_H
20 
21 #ifndef FT_LOAD_COLOR
22 #  define FT_LOAD_COLOR (1L << 20)
23 #endif
24 #ifndef FT_FACE_FLAG_COLOR
25 #  define FT_FACE_FLAG_COLOR (1L << 14)
26 #endif
27 
28 using namespace mozilla;
29 using namespace mozilla::gfx;
30 
gfxFT2FontBase(const RefPtr<UnscaledFontFreeType> & aUnscaledFont,RefPtr<mozilla::gfx::SharedFTFace> && aFTFace,gfxFontEntry * aFontEntry,const gfxFontStyle * aFontStyle,int aLoadFlags,bool aEmbolden)31 gfxFT2FontBase::gfxFT2FontBase(
32     const RefPtr<UnscaledFontFreeType>& aUnscaledFont,
33     RefPtr<mozilla::gfx::SharedFTFace>&& aFTFace, gfxFontEntry* aFontEntry,
34     const gfxFontStyle* aFontStyle, int aLoadFlags, bool aEmbolden)
35     : gfxFont(aUnscaledFont, aFontEntry, aFontStyle, kAntialiasDefault),
36       mFTFace(std::move(aFTFace)),
37       mFTLoadFlags(aLoadFlags | FT_LOAD_IGNORE_GLOBAL_ADVANCE_WIDTH |
38                    FT_LOAD_COLOR),
39       mEmbolden(aEmbolden),
40       mFTSize(0.0) {}
41 
~gfxFT2FontBase()42 gfxFT2FontBase::~gfxFT2FontBase() { mFTFace->ForgetLockOwner(this); }
43 
LockFTFace()44 FT_Face gfxFT2FontBase::LockFTFace() {
45   if (!mFTFace->Lock(this)) {
46     FT_Set_Transform(mFTFace->GetFace(), nullptr, nullptr);
47 
48     FT_F26Dot6 charSize = NS_lround(mFTSize * 64.0);
49     FT_Set_Char_Size(mFTFace->GetFace(), charSize, charSize, 0, 0);
50   }
51   return mFTFace->GetFace();
52 }
53 
UnlockFTFace()54 void gfxFT2FontBase::UnlockFTFace() { mFTFace->Unlock(); }
55 
GetCmapCacheSlot(uint32_t aCharCode)56 gfxFT2FontEntryBase::CmapCacheSlot* gfxFT2FontEntryBase::GetCmapCacheSlot(
57     uint32_t aCharCode) {
58   // This cache algorithm and size is based on what is done in
59   // cairo_scaled_font_text_to_glyphs and pango_fc_font_real_get_glyph.  I
60   // think the concept is that adjacent characters probably come mostly from
61   // one Unicode block.  This assumption is probably not so valid with
62   // scripts with large character sets as used for East Asian languages.
63   if (!mCmapCache) {
64     mCmapCache = mozilla::MakeUnique<CmapCacheSlot[]>(kNumCmapCacheSlots);
65 
66     // Invalidate slot 0 by setting its char code to something that would
67     // never end up in slot 0.  All other slots are already invalid
68     // because they have mCharCode = 0 and a glyph for char code 0 will
69     // always be in the slot 0.
70     mCmapCache[0].mCharCode = 1;
71   }
72   return &mCmapCache[aCharCode % kNumCmapCacheSlots];
73 }
74 
GetTableSizeFromFTFace(SharedFTFace * aFace,uint32_t aTableTag)75 static FT_ULong GetTableSizeFromFTFace(SharedFTFace* aFace,
76                                        uint32_t aTableTag) {
77   if (!aFace) {
78     return 0;
79   }
80   FT_ULong len = 0;
81   if (FT_Load_Sfnt_Table(aFace->GetFace(), aTableTag, 0, nullptr, &len) != 0) {
82     return 0;
83   }
84   return len;
85 }
86 
FaceHasTable(SharedFTFace * aFace,uint32_t aTableTag)87 bool gfxFT2FontEntryBase::FaceHasTable(SharedFTFace* aFace,
88                                        uint32_t aTableTag) {
89   return GetTableSizeFromFTFace(aFace, aTableTag) > 0;
90 }
91 
CopyFaceTable(SharedFTFace * aFace,uint32_t aTableTag,nsTArray<uint8_t> & aBuffer)92 nsresult gfxFT2FontEntryBase::CopyFaceTable(SharedFTFace* aFace,
93                                             uint32_t aTableTag,
94                                             nsTArray<uint8_t>& aBuffer) {
95   FT_ULong length = GetTableSizeFromFTFace(aFace, aTableTag);
96   if (!length) {
97     return NS_ERROR_NOT_AVAILABLE;
98   }
99   if (!aBuffer.SetLength(length, fallible)) {
100     return NS_ERROR_OUT_OF_MEMORY;
101   }
102   if (FT_Load_Sfnt_Table(aFace->GetFace(), aTableTag, 0, aBuffer.Elements(),
103                          &length) != 0) {
104     aBuffer.Clear();
105     return NS_ERROR_FAILURE;
106   }
107   return NS_OK;
108 }
109 
GetGlyph(uint32_t aCharCode)110 uint32_t gfxFT2FontBase::GetGlyph(uint32_t aCharCode) {
111   // FcFreeTypeCharIndex needs to lock the FT_Face and can end up searching
112   // through all the postscript glyph names in the font.  Therefore use a
113   // lightweight cache, which is stored on the font entry.
114   auto* slot = static_cast<gfxFT2FontEntryBase*>(mFontEntry.get())
115                    ->GetCmapCacheSlot(aCharCode);
116   if (slot->mCharCode != aCharCode) {
117     slot->mCharCode = aCharCode;
118     slot->mGlyphIndex = gfxFT2LockedFace(this).GetGlyph(aCharCode);
119   }
120   return slot->mGlyphIndex;
121 }
122 
123 // aScale is intended for a 16.16 x/y_scale of an FT_Size_Metrics
ScaleRoundDesignUnits(FT_Short aDesignMetric,FT_Fixed aScale)124 static inline FT_Long ScaleRoundDesignUnits(FT_Short aDesignMetric,
125                                             FT_Fixed aScale) {
126   FT_Long fixed26dot6 = FT_MulFix(aDesignMetric, aScale);
127   return ROUND_26_6_TO_INT(fixed26dot6);
128 }
129 
130 // Snap a line to pixels while keeping the center and size of the line as
131 // close to the original position as possible.
132 //
133 // Pango does similar snapping for underline and strikethrough when fonts are
134 // hinted, but nsCSSRendering::GetTextDecorationRectInternal always snaps the
135 // top and size of lines.  Optimizing the distance between the line and
136 // baseline is probably good for the gap between text and underline, but
137 // optimizing the center of the line is better for positioning strikethough.
SnapLineToPixels(gfxFloat & aOffset,gfxFloat & aSize)138 static void SnapLineToPixels(gfxFloat& aOffset, gfxFloat& aSize) {
139   gfxFloat snappedSize = std::max(floor(aSize + 0.5), 1.0);
140   // Correct offset for change in size
141   gfxFloat offset = aOffset - 0.5 * (aSize - snappedSize);
142   // Snap offset
143   aOffset = floor(offset + 0.5);
144   aSize = snappedSize;
145 }
146 
ScaleGlyphBounds(const IntRect & aBounds,gfxFloat aScale)147 static inline gfxRect ScaleGlyphBounds(const IntRect& aBounds,
148                                        gfxFloat aScale) {
149   return gfxRect(FLOAT_FROM_26_6(aBounds.x) * aScale,
150                  FLOAT_FROM_26_6(aBounds.y) * aScale,
151                  FLOAT_FROM_26_6(aBounds.width) * aScale,
152                  FLOAT_FROM_26_6(aBounds.height) * aScale);
153 }
154 
155 /**
156  * Get extents for a simple character representable by a single glyph.
157  * The return value is the glyph id of that glyph or zero if no such glyph
158  * exists.  aWidth/aBounds is only set when this returns a non-zero glyph id.
159  * This is just for use during initialization, and doesn't use the width cache.
160  */
GetCharExtents(uint32_t aChar,gfxFloat * aWidth,gfxRect * aBounds)161 uint32_t gfxFT2FontBase::GetCharExtents(uint32_t aChar, gfxFloat* aWidth,
162                                         gfxRect* aBounds) {
163   FT_UInt gid = GetGlyph(aChar);
164   int32_t width;
165   IntRect bounds;
166   if (gid && GetFTGlyphExtents(gid, aWidth ? &width : nullptr,
167                                aBounds ? &bounds : nullptr)) {
168     if (aWidth) {
169       *aWidth = FLOAT_FROM_16_16(width);
170     }
171     if (aBounds) {
172       *aBounds = ScaleGlyphBounds(bounds, GetAdjustedSize() / mFTSize);
173     }
174     return gid;
175   } else {
176     return 0;
177   }
178 }
179 
180 /**
181  * Find the closest available fixed strike size, if applicable, to the
182  * desired font size.
183  */
FindClosestSize(FT_Face aFace,double aSize)184 static double FindClosestSize(FT_Face aFace, double aSize) {
185   // FT size selection does not actually support sizes smaller than 1 and will
186   // clamp this internally, regardless of what is requested. Do the clamp here
187   // instead so that glyph extents/font matrix scaling will compensate it, as
188   // Cairo normally would.
189   if (aSize < 1.0) {
190     aSize = 1.0;
191   }
192   if (FT_IS_SCALABLE(aFace)) {
193     return aSize;
194   }
195   double bestDist = DBL_MAX;
196   FT_Int bestSize = -1;
197   for (FT_Int i = 0; i < aFace->num_fixed_sizes; i++) {
198     double dist = aFace->available_sizes[i].y_ppem / 64.0 - aSize;
199     // If the previous best is smaller than the desired size, prefer
200     // a bigger size. Otherwise, just choose whatever size is closest.
201     if (bestDist < 0 ? dist >= bestDist : fabs(dist) <= bestDist) {
202       bestDist = dist;
203       bestSize = i;
204     }
205   }
206   if (bestSize < 0) {
207     return aSize;
208   }
209   return aFace->available_sizes[bestSize].y_ppem / 64.0;
210 }
211 
InitMetrics()212 void gfxFT2FontBase::InitMetrics() {
213   mFUnitsConvFactor = 0.0;
214 
215   if (MOZ_UNLIKELY(mStyle.AdjustedSizeMustBeZero())) {
216     memset(&mMetrics, 0, sizeof(mMetrics));  // zero initialize
217     mSpaceGlyph = GetGlyph(' ');
218     return;
219   }
220 
221   if (FontSizeAdjust::Tag(mStyle.sizeAdjustBasis) !=
222           FontSizeAdjust::Tag::None &&
223       mStyle.sizeAdjust >= 0.0 && GetAdjustedSize() > 0.0 && mFTSize == 0.0) {
224     // If font-size-adjust is in effect, we need to get metrics in order to
225     // determine the aspect ratio, then compute the final adjusted size and
226     // re-initialize metrics.
227     // Setting mFTSize nonzero here ensures we will not recurse again; the
228     // actual value will be overridden by FindClosestSize below.
229     mFTSize = 1.0;
230     InitMetrics();
231     // Now do the font-size-adjust calculation and set the final size.
232     gfxFloat aspect;
233     switch (FontSizeAdjust::Tag(mStyle.sizeAdjustBasis)) {
234       default:
235         MOZ_ASSERT_UNREACHABLE("unhandled sizeAdjustBasis?");
236         aspect = 0.0;
237         break;
238       case FontSizeAdjust::Tag::ExHeight:
239         aspect = mMetrics.xHeight / mAdjustedSize;
240         break;
241       case FontSizeAdjust::Tag::CapHeight:
242         aspect = mMetrics.capHeight / mAdjustedSize;
243         break;
244       case FontSizeAdjust::Tag::ChWidth:
245         aspect =
246             mMetrics.zeroWidth > 0.0 ? mMetrics.zeroWidth / mAdjustedSize : 0.5;
247         break;
248       case FontSizeAdjust::Tag::IcWidth:
249       case FontSizeAdjust::Tag::IcHeight: {
250         bool vertical = FontSizeAdjust::Tag(mStyle.sizeAdjustBasis) ==
251                         FontSizeAdjust::Tag::IcHeight;
252         gfxFloat advance = GetCharAdvance(kWaterIdeograph, vertical);
253         aspect = advance > 0.0 ? advance / mAdjustedSize : 1.0;
254         break;
255       }
256     }
257     if (aspect > 0.0) {
258       // If we created a shaper above (to measure glyphs), discard it so we
259       // get a new one for the adjusted scaling.
260       mHarfBuzzShaper = nullptr;
261       mAdjustedSize = mStyle.GetAdjustedSize(aspect);
262       // Ensure the FT_Face will be reconfigured for the new size next time we
263       // need to use it.
264       mFTFace->ForgetLockOwner(this);
265     }
266   }
267 
268   // Set mAdjustedSize if it hasn't already been set by a font-size-adjust
269   // computation.
270   mAdjustedSize = GetAdjustedSize();
271 
272   // Cairo metrics are normalized to em-space, so that whatever fixed size
273   // might actually be chosen is factored out. They are then later scaled by
274   // the font matrix to the target adjusted size. Stash the chosen closest
275   // size here for later scaling of the metrics.
276   mFTSize = FindClosestSize(mFTFace->GetFace(), GetAdjustedSize());
277 
278   // Explicitly lock the face so we can release it early before calling
279   // back into Cairo below.
280   FT_Face face = LockFTFace();
281 
282   if (MOZ_UNLIKELY(!face)) {
283     // No face.  This unfortunate situation might happen if the font
284     // file is (re)moved at the wrong time.
285     const gfxFloat emHeight = GetAdjustedSize();
286     mMetrics.emHeight = emHeight;
287     mMetrics.maxAscent = mMetrics.emAscent = 0.8 * emHeight;
288     mMetrics.maxDescent = mMetrics.emDescent = 0.2 * emHeight;
289     mMetrics.maxHeight = emHeight;
290     mMetrics.internalLeading = 0.0;
291     mMetrics.externalLeading = 0.2 * emHeight;
292     const gfxFloat spaceWidth = 0.5 * emHeight;
293     mMetrics.spaceWidth = spaceWidth;
294     mMetrics.maxAdvance = spaceWidth;
295     mMetrics.aveCharWidth = spaceWidth;
296     mMetrics.zeroWidth = spaceWidth;
297     mMetrics.ideographicWidth = emHeight;
298     const gfxFloat xHeight = 0.5 * emHeight;
299     mMetrics.xHeight = xHeight;
300     mMetrics.capHeight = mMetrics.maxAscent;
301     const gfxFloat underlineSize = emHeight / 14.0;
302     mMetrics.underlineSize = underlineSize;
303     mMetrics.underlineOffset = -underlineSize;
304     mMetrics.strikeoutOffset = 0.25 * emHeight;
305     mMetrics.strikeoutSize = underlineSize;
306 
307     SanitizeMetrics(&mMetrics, false);
308     UnlockFTFace();
309     return;
310   }
311 
312   const FT_Size_Metrics& ftMetrics = face->size->metrics;
313 
314   mMetrics.maxAscent = FLOAT_FROM_26_6(ftMetrics.ascender);
315   mMetrics.maxDescent = -FLOAT_FROM_26_6(ftMetrics.descender);
316   mMetrics.maxAdvance = FLOAT_FROM_26_6(ftMetrics.max_advance);
317   gfxFloat lineHeight = FLOAT_FROM_26_6(ftMetrics.height);
318 
319   gfxFloat emHeight;
320   // Scale for vertical design metric conversion: pixels per design unit.
321   // If this remains at 0.0, we can't use metrics from OS/2 etc.
322   gfxFloat yScale = 0.0;
323   if (FT_IS_SCALABLE(face)) {
324     // Prefer FT_Size_Metrics::x_scale to x_ppem as x_ppem does not
325     // have subpixel accuracy.
326     //
327     // FT_Size_Metrics::y_scale is in 16.16 fixed point format.  Its
328     // (fractional) value is a factor that converts vertical metrics from
329     // design units to units of 1/64 pixels, so that the result may be
330     // interpreted as pixels in 26.6 fixed point format.
331     mFUnitsConvFactor = FLOAT_FROM_26_6(FLOAT_FROM_16_16(ftMetrics.x_scale));
332     yScale = FLOAT_FROM_26_6(FLOAT_FROM_16_16(ftMetrics.y_scale));
333     emHeight = face->units_per_EM * yScale;
334   } else {  // Not scalable.
335     emHeight = ftMetrics.y_ppem;
336     // FT_Face doc says units_per_EM and a bunch of following fields
337     // are "only relevant to scalable outlines". If it's an sfnt,
338     // we can get units_per_EM from the 'head' table instead; otherwise,
339     // we don't have a unitsPerEm value so we can't compute/use yScale or
340     // mFUnitsConvFactor (x scale).
341     const TT_Header* head =
342         static_cast<TT_Header*>(FT_Get_Sfnt_Table(face, ft_sfnt_head));
343     if (head) {
344       // Bug 1267909 - Even if the font is not explicitly scalable,
345       // if the face has color bitmaps, it should be treated as scalable
346       // and scaled to the desired size. Metrics based on y_ppem need
347       // to be rescaled for the adjusted size. This makes metrics agree
348       // with the scales we pass to Cairo for Fontconfig fonts.
349       if (face->face_flags & FT_FACE_FLAG_COLOR) {
350         emHeight = GetAdjustedSize();
351         gfxFloat adjustScale = emHeight / ftMetrics.y_ppem;
352         mMetrics.maxAscent *= adjustScale;
353         mMetrics.maxDescent *= adjustScale;
354         mMetrics.maxAdvance *= adjustScale;
355         lineHeight *= adjustScale;
356       }
357       gfxFloat emUnit = head->Units_Per_EM;
358       mFUnitsConvFactor = ftMetrics.x_ppem / emUnit;
359       yScale = emHeight / emUnit;
360     }
361   }
362 
363   TT_OS2* os2 = static_cast<TT_OS2*>(FT_Get_Sfnt_Table(face, ft_sfnt_os2));
364 
365   if (os2 && os2->sTypoAscender && yScale > 0.0) {
366     mMetrics.emAscent = os2->sTypoAscender * yScale;
367     mMetrics.emDescent = -os2->sTypoDescender * yScale;
368     FT_Short typoHeight =
369         os2->sTypoAscender - os2->sTypoDescender + os2->sTypoLineGap;
370     lineHeight = typoHeight * yScale;
371 
372     // If the OS/2 fsSelection USE_TYPO_METRICS bit is set,
373     // set maxAscent/Descent from the sTypo* fields instead of hhea.
374     const uint16_t kUseTypoMetricsMask = 1 << 7;
375     if ((os2->fsSelection & kUseTypoMetricsMask) ||
376         // maxAscent/maxDescent get used for frame heights, and some fonts
377         // don't have the HHEA table ascent/descent set (bug 279032).
378         (mMetrics.maxAscent == 0.0 && mMetrics.maxDescent == 0.0)) {
379       // We use NS_round here to parallel the pixel-rounded values that
380       // freetype gives us for ftMetrics.ascender/descender.
381       mMetrics.maxAscent = NS_round(mMetrics.emAscent);
382       mMetrics.maxDescent = NS_round(mMetrics.emDescent);
383     }
384   } else {
385     mMetrics.emAscent = mMetrics.maxAscent;
386     mMetrics.emDescent = mMetrics.maxDescent;
387   }
388 
389   // gfxFont::Metrics::underlineOffset is the position of the top of the
390   // underline.
391   //
392   // FT_FaceRec documentation describes underline_position as "the
393   // center of the underlining stem".  This was the original definition
394   // of the PostScript metric, but in the PostScript table of OpenType
395   // fonts the metric is "the top of the underline"
396   // (http://www.microsoft.com/typography/otspec/post.htm), and FreeType
397   // (up to version 2.3.7) doesn't make any adjustment.
398   //
399   // Therefore get the underline position directly from the table
400   // ourselves when this table exists.  Use FreeType's metrics for
401   // other (including older PostScript) fonts.
402   if (face->underline_position && face->underline_thickness && yScale > 0.0) {
403     mMetrics.underlineSize = face->underline_thickness * yScale;
404     TT_Postscript* post =
405         static_cast<TT_Postscript*>(FT_Get_Sfnt_Table(face, ft_sfnt_post));
406     if (post && post->underlinePosition) {
407       mMetrics.underlineOffset = post->underlinePosition * yScale;
408     } else {
409       mMetrics.underlineOffset =
410           face->underline_position * yScale + 0.5 * mMetrics.underlineSize;
411     }
412   } else {  // No underline info.
413     // Imitate Pango.
414     mMetrics.underlineSize = emHeight / 14.0;
415     mMetrics.underlineOffset = -mMetrics.underlineSize;
416   }
417 
418   if (os2 && os2->yStrikeoutSize && os2->yStrikeoutPosition && yScale > 0.0) {
419     mMetrics.strikeoutSize = os2->yStrikeoutSize * yScale;
420     mMetrics.strikeoutOffset = os2->yStrikeoutPosition * yScale;
421   } else {  // No strikeout info.
422     mMetrics.strikeoutSize = mMetrics.underlineSize;
423     // Use OpenType spec's suggested position for Roman font.
424     mMetrics.strikeoutOffset =
425         emHeight * 409.0 / 2048.0 + 0.5 * mMetrics.strikeoutSize;
426   }
427   SnapLineToPixels(mMetrics.strikeoutOffset, mMetrics.strikeoutSize);
428 
429   if (os2 && os2->sxHeight && yScale > 0.0) {
430     mMetrics.xHeight = os2->sxHeight * yScale;
431   } else {
432     // CSS 2.1, section 4.3.2 Lengths: "In the cases where it is
433     // impossible or impractical to determine the x-height, a value of
434     // 0.5em should be used."
435     mMetrics.xHeight = 0.5 * emHeight;
436   }
437 
438   // aveCharWidth is used for the width of text input elements so be
439   // liberal rather than conservative in the estimate.
440   if (os2 && os2->xAvgCharWidth) {
441     // Round to pixels as this is compared with maxAdvance to guess
442     // whether this is a fixed width font.
443     mMetrics.aveCharWidth =
444         ScaleRoundDesignUnits(os2->xAvgCharWidth, ftMetrics.x_scale);
445   } else {
446     mMetrics.aveCharWidth = 0.0;  // updated below
447   }
448 
449   if (os2 && os2->sCapHeight && yScale > 0.0) {
450     mMetrics.capHeight = os2->sCapHeight * yScale;
451   } else {
452     mMetrics.capHeight = mMetrics.maxAscent;
453   }
454 
455   // Release the face lock to safely load glyphs with GetCharExtents if
456   // necessary without recursively locking.
457   UnlockFTFace();
458 
459   gfxFloat width;
460   mSpaceGlyph = GetCharExtents(' ', &width);
461   if (mSpaceGlyph) {
462     mMetrics.spaceWidth = width;
463   } else {
464     mMetrics.spaceWidth = mMetrics.maxAdvance;  // guess
465   }
466 
467   if (GetCharExtents('0', &width)) {
468     mMetrics.zeroWidth = width;
469   } else {
470     mMetrics.zeroWidth = -1.0;  // indicates not found
471   }
472 
473   if (GetCharExtents(kWaterIdeograph, &width)) {
474     mMetrics.ideographicWidth = width;
475   } else {
476     mMetrics.ideographicWidth = -1.0;
477   }
478 
479   // If we didn't get a usable x-height or cap-height above, try measuring
480   // specific glyphs. This can be affected by hinting, leading to erratic
481   // behavior across font sizes and system configuration, so we prefer to
482   // use the metrics directly from the font if possible.
483   // Using glyph bounds for x-height or cap-height may not really be right,
484   // if fonts have fancy swashes etc. For x-height, CSS 2.1 suggests possibly
485   // using the height of an "o", which may be more consistent across fonts,
486   // but then curve-overshoot should also be accounted for.
487   gfxFloat xWidth;
488   gfxRect xBounds;
489   if (mMetrics.xHeight == 0.0) {
490     if (GetCharExtents('x', &xWidth, &xBounds) && xBounds.y < 0.0) {
491       mMetrics.xHeight = -xBounds.y;
492       mMetrics.aveCharWidth = std::max(mMetrics.aveCharWidth, xWidth);
493     }
494   }
495 
496   if (mMetrics.capHeight == 0.0) {
497     if (GetCharExtents('H', nullptr, &xBounds) && xBounds.y < 0.0) {
498       mMetrics.capHeight = -xBounds.y;
499     }
500   }
501 
502   mMetrics.aveCharWidth = std::max(mMetrics.aveCharWidth, mMetrics.zeroWidth);
503   if (mMetrics.aveCharWidth == 0.0) {
504     mMetrics.aveCharWidth = mMetrics.spaceWidth;
505   }
506   // Apparently hinting can mean that max_advance is not always accurate.
507   mMetrics.maxAdvance = std::max(mMetrics.maxAdvance, mMetrics.aveCharWidth);
508 
509   mMetrics.maxHeight = mMetrics.maxAscent + mMetrics.maxDescent;
510 
511   // Make the line height an integer number of pixels so that lines will be
512   // equally spaced (rather than just being snapped to pixels, some up and
513   // some down).  Layout calculates line height from the emHeight +
514   // internalLeading + externalLeading, but first each of these is rounded
515   // to layout units.  To ensure that the result is an integer number of
516   // pixels, round each of the components to pixels.
517   mMetrics.emHeight = floor(emHeight + 0.5);
518 
519   // maxHeight will normally be an integer, but round anyway in case
520   // FreeType is configured differently.
521   mMetrics.internalLeading =
522       floor(mMetrics.maxHeight - mMetrics.emHeight + 0.5);
523 
524   // Text input boxes currently don't work well with lineHeight
525   // significantly less than maxHeight (with Verdana, for example).
526   lineHeight = floor(std::max(lineHeight, mMetrics.maxHeight) + 0.5);
527   mMetrics.externalLeading =
528       lineHeight - mMetrics.internalLeading - mMetrics.emHeight;
529 
530   // Ensure emAscent + emDescent == emHeight
531   gfxFloat sum = mMetrics.emAscent + mMetrics.emDescent;
532   mMetrics.emAscent =
533       sum > 0.0 ? mMetrics.emAscent * mMetrics.emHeight / sum : 0.0;
534   mMetrics.emDescent = mMetrics.emHeight - mMetrics.emAscent;
535 
536   SanitizeMetrics(&mMetrics, false);
537 
538 #if 0
539     //    printf("font name: %s %f\n", NS_ConvertUTF16toUTF8(GetName()).get(), GetStyle()->size);
540     //    printf ("pango font %s\n", pango_font_description_to_string (pango_font_describe (font)));
541 
542     fprintf (stderr, "Font: %s\n", GetName().get());
543     fprintf (stderr, "    emHeight: %f emAscent: %f emDescent: %f\n", mMetrics.emHeight, mMetrics.emAscent, mMetrics.emDescent);
544     fprintf (stderr, "    maxAscent: %f maxDescent: %f\n", mMetrics.maxAscent, mMetrics.maxDescent);
545     fprintf (stderr, "    internalLeading: %f externalLeading: %f\n", mMetrics.externalLeading, mMetrics.internalLeading);
546     fprintf (stderr, "    spaceWidth: %f aveCharWidth: %f xHeight: %f\n", mMetrics.spaceWidth, mMetrics.aveCharWidth, mMetrics.xHeight);
547     fprintf (stderr, "    ideographicWidth: %f\n", mMetrics.ideographicWidth);
548     fprintf (stderr, "    uOff: %f uSize: %f stOff: %f stSize: %f\n", mMetrics.underlineOffset, mMetrics.underlineSize, mMetrics.strikeoutOffset, mMetrics.strikeoutSize);
549 #endif
550 }
551 
GetHorizontalMetrics()552 const gfxFont::Metrics& gfxFT2FontBase::GetHorizontalMetrics() {
553   return mMetrics;
554 }
555 
GetGlyph(uint32_t unicode,uint32_t variation_selector)556 uint32_t gfxFT2FontBase::GetGlyph(uint32_t unicode,
557                                   uint32_t variation_selector) {
558   if (variation_selector) {
559     uint32_t id =
560         gfxFT2LockedFace(this).GetUVSGlyph(unicode, variation_selector);
561     if (id) {
562       return id;
563     }
564     unicode = gfxFontUtils::GetUVSFallback(unicode, variation_selector);
565     if (unicode) {
566       return GetGlyph(unicode);
567     }
568     return 0;
569   }
570 
571   return GetGlyph(unicode);
572 }
573 
ShouldRoundXOffset(cairo_t * aCairo) const574 bool gfxFT2FontBase::ShouldRoundXOffset(cairo_t* aCairo) const {
575   // Force rounding if outputting to a Cairo context or if requested by pref to
576   // disable subpixel positioning. Otherwise, allow subpixel positioning (no
577   // rounding) if rendering a scalable outline font with anti-aliasing.
578   // Monochrome rendering or some bitmap fonts can become too distorted with
579   // subpixel positioning, so force rounding in those cases. Also be careful not
580   // to use subpixel positioning if the user requests full hinting via
581   // Fontconfig, which we detect by checking that neither hinting was disabled
582   // nor light hinting was requested. Allow pref to force subpixel positioning
583   // on even if full hinting was requested.
584   return MOZ_UNLIKELY(
585              StaticPrefs::
586                  gfx_text_subpixel_position_force_disabled_AtStartup()) ||
587          aCairo != nullptr || !mFTFace || !FT_IS_SCALABLE(mFTFace->GetFace()) ||
588          (mFTLoadFlags & FT_LOAD_MONOCHROME) ||
589          !((mFTLoadFlags & FT_LOAD_NO_HINTING) ||
590            FT_LOAD_TARGET_MODE(mFTLoadFlags) == FT_RENDER_MODE_LIGHT ||
591            MOZ_UNLIKELY(
592                StaticPrefs::
593                    gfx_text_subpixel_position_force_enabled_AtStartup()));
594 }
595 
GetEmboldenStrength(FT_Face aFace)596 FT_Vector gfxFT2FontBase::GetEmboldenStrength(FT_Face aFace) {
597   FT_Vector strength = {0, 0};
598   if (!mEmbolden) {
599     return strength;
600   }
601 
602   // If it's an outline glyph, we'll be using mozilla_glyphslot_embolden_less
603   // (see gfx/wr/webrender/src/platform/unix/font.rs), so we need to match its
604   // emboldening strength here.
605   if (aFace->glyph->format == FT_GLYPH_FORMAT_OUTLINE) {
606     strength.x =
607         FT_MulFix(aFace->units_per_EM, aFace->size->metrics.y_scale) / 48;
608     strength.y = strength.x;
609     return strength;
610   }
611 
612   // This is the embolden "strength" used by FT_GlyphSlot_Embolden.
613   strength.x =
614       FT_MulFix(aFace->units_per_EM, aFace->size->metrics.y_scale) / 24;
615   strength.y = strength.x;
616   if (aFace->glyph->format == FT_GLYPH_FORMAT_BITMAP) {
617     strength.x &= -64;
618     if (!strength.x) {
619       strength.x = 64;
620     }
621     strength.y &= -64;
622   }
623   return strength;
624 }
625 
GetFTGlyphExtents(uint16_t aGID,int32_t * aAdvance,IntRect * aBounds)626 bool gfxFT2FontBase::GetFTGlyphExtents(uint16_t aGID, int32_t* aAdvance,
627                                        IntRect* aBounds) {
628   gfxFT2LockedFace face(this);
629   MOZ_ASSERT(face.get());
630   if (!face.get()) {
631     // Failed to get the FT_Face? Give up already.
632     NS_WARNING("failed to get FT_Face!");
633     return false;
634   }
635 
636   FT_Int32 flags = mFTLoadFlags;
637   if (!aBounds) {
638     flags |= FT_LOAD_ADVANCE_ONLY;
639   }
640   if (Factory::LoadFTGlyph(face.get(), aGID, flags) != FT_Err_Ok) {
641     // FT_Face was somehow broken/invalid? Don't try to access glyph slot.
642     // This probably shouldn't happen, but does: see bug 1440938.
643     NS_WARNING("failed to load glyph!");
644     return false;
645   }
646 
647   // Whether to interpret hinting settings (i.e. not printing)
648   bool hintMetrics = ShouldHintMetrics();
649   // Whether to disable subpixel positioning
650   bool roundX = ShouldRoundXOffset(nullptr);
651   // No hinting disables X and Y hinting. Light disables only X hinting.
652   bool unhintedY = (mFTLoadFlags & FT_LOAD_NO_HINTING) != 0;
653   bool unhintedX =
654       unhintedY || FT_LOAD_TARGET_MODE(mFTLoadFlags) == FT_RENDER_MODE_LIGHT;
655 
656   // Normalize out the loaded FT glyph size and then scale to the actually
657   // desired size, in case these two sizes differ.
658   gfxFloat extentsScale = GetAdjustedSize() / mFTSize;
659 
660   FT_Vector bold = GetEmboldenStrength(face.get());
661 
662   // Due to freetype bug 52683 we MUST use the linearHoriAdvance field when
663   // dealing with a variation font; also use it for scalable fonts when not
664   // applying hinting. Otherwise, prefer hinted width from glyph->advance.x.
665   if (aAdvance) {
666     FT_Fixed advance;
667     if (!roundX || FT_HAS_MULTIPLE_MASTERS(face.get())) {
668       advance = face.get()->glyph->linearHoriAdvance;
669     } else {
670       advance = face.get()->glyph->advance.x << 10;  // convert 26.6 to 16.16
671     }
672     if (advance) {
673       advance += bold.x << 10;  // convert 26.6 to 16.16
674     }
675     // Hinting was requested, but FT did not apply any hinting to the metrics.
676     // Round the advance here to approximate hinting as Cairo does. This must
677     // happen BEFORE we apply the glyph extents scale, just like FT hinting
678     // would.
679     if (hintMetrics && roundX && unhintedX) {
680       advance = (advance + 0x8000) & 0xffff0000u;
681     }
682     *aAdvance = NS_lround(advance * extentsScale);
683   }
684 
685   if (aBounds) {
686     const FT_Glyph_Metrics& metrics = face.get()->glyph->metrics;
687     FT_F26Dot6 x = metrics.horiBearingX;
688     FT_F26Dot6 y = -metrics.horiBearingY;
689     FT_F26Dot6 x2 = x + metrics.width;
690     FT_F26Dot6 y2 = y + metrics.height;
691     // Synthetic bold moves the glyph top and right boundaries.
692     y -= bold.y;
693     x2 += bold.x;
694     if (hintMetrics) {
695       if (roundX && unhintedX) {
696         x &= -64;
697         x2 = (x2 + 63) & -64;
698       }
699       if (unhintedY) {
700         y &= -64;
701         y2 = (y2 + 63) & -64;
702       }
703     }
704     *aBounds = IntRect(x, y, x2 - x, y2 - y);
705   }
706   return true;
707 }
708 
709 /**
710  * Get the cached glyph metrics for the glyph id if available. Otherwise, query
711  * FreeType for the glyph extents and initialize the glyph metrics.
712  */
GetCachedGlyphMetrics(uint16_t aGID,IntRect * aBounds)713 const gfxFT2FontBase::GlyphMetrics& gfxFT2FontBase::GetCachedGlyphMetrics(
714     uint16_t aGID, IntRect* aBounds) {
715   if (!mGlyphMetrics) {
716     mGlyphMetrics =
717         mozilla::MakeUnique<nsTHashMap<nsUint32HashKey, GlyphMetrics>>(128);
718   }
719 
720   return mGlyphMetrics->LookupOrInsertWith(aGID, [&] {
721     GlyphMetrics metrics;
722     IntRect bounds;
723     if (GetFTGlyphExtents(aGID, &metrics.mAdvance, &bounds)) {
724       metrics.SetBounds(bounds);
725       if (aBounds) {
726         *aBounds = bounds;
727       }
728     }
729     return metrics;
730   });
731 }
732 
GetGlyphWidth(uint16_t aGID)733 int32_t gfxFT2FontBase::GetGlyphWidth(uint16_t aGID) {
734   return GetCachedGlyphMetrics(aGID).mAdvance;
735 }
736 
GetGlyphBounds(uint16_t aGID,gfxRect * aBounds,bool aTight)737 bool gfxFT2FontBase::GetGlyphBounds(uint16_t aGID, gfxRect* aBounds,
738                                     bool aTight) {
739   IntRect bounds;
740   const GlyphMetrics& metrics = GetCachedGlyphMetrics(aGID, &bounds);
741   if (!metrics.HasValidBounds()) {
742     return false;
743   }
744   // Check if there are cached bounds and use those if available. Otherwise,
745   // fall back to directly querying the glyph extents.
746   if (metrics.HasCachedBounds()) {
747     bounds = metrics.GetBounds();
748   } else if (bounds.IsEmpty() && !GetFTGlyphExtents(aGID, nullptr, &bounds)) {
749     return false;
750   }
751   // The bounds are stored unscaled, so must be scaled to the adjusted size.
752   *aBounds = ScaleGlyphBounds(bounds, GetAdjustedSize() / mFTSize);
753   return true;
754 }
755 
756 // For variation fonts, figure out the variation coordinates to be applied
757 // for each axis, in freetype's order (which may not match the order of
758 // axes in mStyle.variationSettings, so we need to search by axis tag).
759 /*static*/
SetupVarCoords(FT_MM_Var * aMMVar,const nsTArray<gfxFontVariation> & aVariations,FT_Face aFTFace)760 void gfxFT2FontBase::SetupVarCoords(
761     FT_MM_Var* aMMVar, const nsTArray<gfxFontVariation>& aVariations,
762     FT_Face aFTFace) {
763   if (!aMMVar) {
764     return;
765   }
766 
767   nsTArray<FT_Fixed> coords;
768   for (unsigned i = 0; i < aMMVar->num_axis; ++i) {
769     coords.AppendElement(aMMVar->axis[i].def);
770     for (const auto& v : aVariations) {
771       if (aMMVar->axis[i].tag == v.mTag) {
772         FT_Fixed val = v.mValue * 0x10000;
773         val = std::min(val, aMMVar->axis[i].maximum);
774         val = std::max(val, aMMVar->axis[i].minimum);
775         coords[i] = val;
776         break;
777       }
778     }
779   }
780 
781   if (!coords.IsEmpty()) {
782 #if MOZ_TREE_FREETYPE
783     FT_Set_Var_Design_Coordinates(aFTFace, coords.Length(), coords.Elements());
784 #else
785     typedef FT_Error (*SetCoordsFunc)(FT_Face, FT_UInt, FT_Fixed*);
786     static SetCoordsFunc setCoords;
787     static bool firstTime = true;
788     if (firstTime) {
789       firstTime = false;
790       setCoords =
791           (SetCoordsFunc)dlsym(RTLD_DEFAULT, "FT_Set_Var_Design_Coordinates");
792     }
793     if (setCoords) {
794       (*setCoords)(aFTFace, coords.Length(), coords.Elements());
795     }
796 #endif
797   }
798 }
799 
CloneFace(int aFaceIndex)800 already_AddRefed<SharedFTFace> FTUserFontData::CloneFace(int aFaceIndex) {
801   RefPtr<SharedFTFace> face = Factory::NewSharedFTFaceFromData(
802       nullptr, mFontData, mLength, aFaceIndex, this);
803   if (!face ||
804       (FT_Select_Charmap(face->GetFace(), FT_ENCODING_UNICODE) != FT_Err_Ok &&
805        FT_Select_Charmap(face->GetFace(), FT_ENCODING_MS_SYMBOL) !=
806            FT_Err_Ok)) {
807     return nullptr;
808   }
809   return face.forget();
810 }
811