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(char aChar,gfxFloat * aWidth,gfxRect * aBounds)161 uint32_t gfxFT2FontBase::GetCharExtents(char 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 && 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(0x6C34, 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 const gfxFloat xHeight = 0.5 * emHeight;
298 mMetrics.xHeight = xHeight;
299 mMetrics.capHeight = mMetrics.maxAscent;
300 const gfxFloat underlineSize = emHeight / 14.0;
301 mMetrics.underlineSize = underlineSize;
302 mMetrics.underlineOffset = -underlineSize;
303 mMetrics.strikeoutOffset = 0.25 * emHeight;
304 mMetrics.strikeoutSize = underlineSize;
305
306 SanitizeMetrics(&mMetrics, false);
307 return;
308 }
309
310 const FT_Size_Metrics& ftMetrics = face->size->metrics;
311
312 mMetrics.maxAscent = FLOAT_FROM_26_6(ftMetrics.ascender);
313 mMetrics.maxDescent = -FLOAT_FROM_26_6(ftMetrics.descender);
314 mMetrics.maxAdvance = FLOAT_FROM_26_6(ftMetrics.max_advance);
315 gfxFloat lineHeight = FLOAT_FROM_26_6(ftMetrics.height);
316
317 gfxFloat emHeight;
318 // Scale for vertical design metric conversion: pixels per design unit.
319 // If this remains at 0.0, we can't use metrics from OS/2 etc.
320 gfxFloat yScale = 0.0;
321 if (FT_IS_SCALABLE(face)) {
322 // Prefer FT_Size_Metrics::x_scale to x_ppem as x_ppem does not
323 // have subpixel accuracy.
324 //
325 // FT_Size_Metrics::y_scale is in 16.16 fixed point format. Its
326 // (fractional) value is a factor that converts vertical metrics from
327 // design units to units of 1/64 pixels, so that the result may be
328 // interpreted as pixels in 26.6 fixed point format.
329 mFUnitsConvFactor = FLOAT_FROM_26_6(FLOAT_FROM_16_16(ftMetrics.x_scale));
330 yScale = FLOAT_FROM_26_6(FLOAT_FROM_16_16(ftMetrics.y_scale));
331 emHeight = face->units_per_EM * yScale;
332 } else { // Not scalable.
333 emHeight = ftMetrics.y_ppem;
334 // FT_Face doc says units_per_EM and a bunch of following fields
335 // are "only relevant to scalable outlines". If it's an sfnt,
336 // we can get units_per_EM from the 'head' table instead; otherwise,
337 // we don't have a unitsPerEm value so we can't compute/use yScale or
338 // mFUnitsConvFactor (x scale).
339 const TT_Header* head =
340 static_cast<TT_Header*>(FT_Get_Sfnt_Table(face, ft_sfnt_head));
341 if (head) {
342 // Bug 1267909 - Even if the font is not explicitly scalable,
343 // if the face has color bitmaps, it should be treated as scalable
344 // and scaled to the desired size. Metrics based on y_ppem need
345 // to be rescaled for the adjusted size. This makes metrics agree
346 // with the scales we pass to Cairo for Fontconfig fonts.
347 if (face->face_flags & FT_FACE_FLAG_COLOR) {
348 emHeight = GetAdjustedSize();
349 gfxFloat adjustScale = emHeight / ftMetrics.y_ppem;
350 mMetrics.maxAscent *= adjustScale;
351 mMetrics.maxDescent *= adjustScale;
352 mMetrics.maxAdvance *= adjustScale;
353 lineHeight *= adjustScale;
354 }
355 gfxFloat emUnit = head->Units_Per_EM;
356 mFUnitsConvFactor = ftMetrics.x_ppem / emUnit;
357 yScale = emHeight / emUnit;
358 }
359 }
360
361 TT_OS2* os2 = static_cast<TT_OS2*>(FT_Get_Sfnt_Table(face, ft_sfnt_os2));
362
363 if (os2 && os2->sTypoAscender && yScale > 0.0) {
364 mMetrics.emAscent = os2->sTypoAscender * yScale;
365 mMetrics.emDescent = -os2->sTypoDescender * yScale;
366 FT_Short typoHeight =
367 os2->sTypoAscender - os2->sTypoDescender + os2->sTypoLineGap;
368 lineHeight = typoHeight * yScale;
369
370 // If the OS/2 fsSelection USE_TYPO_METRICS bit is set,
371 // set maxAscent/Descent from the sTypo* fields instead of hhea.
372 const uint16_t kUseTypoMetricsMask = 1 << 7;
373 if ((os2->fsSelection & kUseTypoMetricsMask) ||
374 // maxAscent/maxDescent get used for frame heights, and some fonts
375 // don't have the HHEA table ascent/descent set (bug 279032).
376 (mMetrics.maxAscent == 0.0 && mMetrics.maxDescent == 0.0)) {
377 // We use NS_round here to parallel the pixel-rounded values that
378 // freetype gives us for ftMetrics.ascender/descender.
379 mMetrics.maxAscent = NS_round(mMetrics.emAscent);
380 mMetrics.maxDescent = NS_round(mMetrics.emDescent);
381 }
382 } else {
383 mMetrics.emAscent = mMetrics.maxAscent;
384 mMetrics.emDescent = mMetrics.maxDescent;
385 }
386
387 // gfxFont::Metrics::underlineOffset is the position of the top of the
388 // underline.
389 //
390 // FT_FaceRec documentation describes underline_position as "the
391 // center of the underlining stem". This was the original definition
392 // of the PostScript metric, but in the PostScript table of OpenType
393 // fonts the metric is "the top of the underline"
394 // (http://www.microsoft.com/typography/otspec/post.htm), and FreeType
395 // (up to version 2.3.7) doesn't make any adjustment.
396 //
397 // Therefore get the underline position directly from the table
398 // ourselves when this table exists. Use FreeType's metrics for
399 // other (including older PostScript) fonts.
400 if (face->underline_position && face->underline_thickness && yScale > 0.0) {
401 mMetrics.underlineSize = face->underline_thickness * yScale;
402 TT_Postscript* post =
403 static_cast<TT_Postscript*>(FT_Get_Sfnt_Table(face, ft_sfnt_post));
404 if (post && post->underlinePosition) {
405 mMetrics.underlineOffset = post->underlinePosition * yScale;
406 } else {
407 mMetrics.underlineOffset =
408 face->underline_position * yScale + 0.5 * mMetrics.underlineSize;
409 }
410 } else { // No underline info.
411 // Imitate Pango.
412 mMetrics.underlineSize = emHeight / 14.0;
413 mMetrics.underlineOffset = -mMetrics.underlineSize;
414 }
415
416 if (os2 && os2->yStrikeoutSize && os2->yStrikeoutPosition && yScale > 0.0) {
417 mMetrics.strikeoutSize = os2->yStrikeoutSize * yScale;
418 mMetrics.strikeoutOffset = os2->yStrikeoutPosition * yScale;
419 } else { // No strikeout info.
420 mMetrics.strikeoutSize = mMetrics.underlineSize;
421 // Use OpenType spec's suggested position for Roman font.
422 mMetrics.strikeoutOffset =
423 emHeight * 409.0 / 2048.0 + 0.5 * mMetrics.strikeoutSize;
424 }
425 SnapLineToPixels(mMetrics.strikeoutOffset, mMetrics.strikeoutSize);
426
427 if (os2 && os2->sxHeight && yScale > 0.0) {
428 mMetrics.xHeight = os2->sxHeight * yScale;
429 } else {
430 // CSS 2.1, section 4.3.2 Lengths: "In the cases where it is
431 // impossible or impractical to determine the x-height, a value of
432 // 0.5em should be used."
433 mMetrics.xHeight = 0.5 * emHeight;
434 }
435
436 // aveCharWidth is used for the width of text input elements so be
437 // liberal rather than conservative in the estimate.
438 if (os2 && os2->xAvgCharWidth) {
439 // Round to pixels as this is compared with maxAdvance to guess
440 // whether this is a fixed width font.
441 mMetrics.aveCharWidth =
442 ScaleRoundDesignUnits(os2->xAvgCharWidth, ftMetrics.x_scale);
443 } else {
444 mMetrics.aveCharWidth = 0.0; // updated below
445 }
446
447 if (os2 && os2->sCapHeight && yScale > 0.0) {
448 mMetrics.capHeight = os2->sCapHeight * yScale;
449 } else {
450 mMetrics.capHeight = mMetrics.maxAscent;
451 }
452
453 // Release the face lock to safely load glyphs with GetCharExtents if
454 // necessary without recursively locking.
455 UnlockFTFace();
456
457 gfxFloat width;
458 mSpaceGlyph = GetCharExtents(' ', &width);
459 if (mSpaceGlyph) {
460 mMetrics.spaceWidth = width;
461 } else {
462 mMetrics.spaceWidth = mMetrics.maxAdvance; // guess
463 }
464
465 if (GetCharExtents('0', &width)) {
466 mMetrics.zeroWidth = width;
467 } else {
468 mMetrics.zeroWidth = -1.0; // indicates not found
469 }
470
471 // If we didn't get a usable x-height or cap-height above, try measuring
472 // specific glyphs. This can be affected by hinting, leading to erratic
473 // behavior across font sizes and system configuration, so we prefer to
474 // use the metrics directly from the font if possible.
475 // Using glyph bounds for x-height or cap-height may not really be right,
476 // if fonts have fancy swashes etc. For x-height, CSS 2.1 suggests possibly
477 // using the height of an "o", which may be more consistent across fonts,
478 // but then curve-overshoot should also be accounted for.
479 gfxFloat xWidth;
480 gfxRect xBounds;
481 if (mMetrics.xHeight == 0.0) {
482 if (GetCharExtents('x', &xWidth, &xBounds) && xBounds.y < 0.0) {
483 mMetrics.xHeight = -xBounds.y;
484 mMetrics.aveCharWidth = std::max(mMetrics.aveCharWidth, xWidth);
485 }
486 }
487
488 if (mMetrics.capHeight == 0.0) {
489 if (GetCharExtents('H', nullptr, &xBounds) && xBounds.y < 0.0) {
490 mMetrics.capHeight = -xBounds.y;
491 }
492 }
493
494 mMetrics.aveCharWidth = std::max(mMetrics.aveCharWidth, mMetrics.zeroWidth);
495 if (mMetrics.aveCharWidth == 0.0) {
496 mMetrics.aveCharWidth = mMetrics.spaceWidth;
497 }
498 // Apparently hinting can mean that max_advance is not always accurate.
499 mMetrics.maxAdvance = std::max(mMetrics.maxAdvance, mMetrics.aveCharWidth);
500
501 mMetrics.maxHeight = mMetrics.maxAscent + mMetrics.maxDescent;
502
503 // Make the line height an integer number of pixels so that lines will be
504 // equally spaced (rather than just being snapped to pixels, some up and
505 // some down). Layout calculates line height from the emHeight +
506 // internalLeading + externalLeading, but first each of these is rounded
507 // to layout units. To ensure that the result is an integer number of
508 // pixels, round each of the components to pixels.
509 mMetrics.emHeight = floor(emHeight + 0.5);
510
511 // maxHeight will normally be an integer, but round anyway in case
512 // FreeType is configured differently.
513 mMetrics.internalLeading =
514 floor(mMetrics.maxHeight - mMetrics.emHeight + 0.5);
515
516 // Text input boxes currently don't work well with lineHeight
517 // significantly less than maxHeight (with Verdana, for example).
518 lineHeight = floor(std::max(lineHeight, mMetrics.maxHeight) + 0.5);
519 mMetrics.externalLeading =
520 lineHeight - mMetrics.internalLeading - mMetrics.emHeight;
521
522 // Ensure emAscent + emDescent == emHeight
523 gfxFloat sum = mMetrics.emAscent + mMetrics.emDescent;
524 mMetrics.emAscent =
525 sum > 0.0 ? mMetrics.emAscent * mMetrics.emHeight / sum : 0.0;
526 mMetrics.emDescent = mMetrics.emHeight - mMetrics.emAscent;
527
528 SanitizeMetrics(&mMetrics, false);
529
530 #if 0
531 // printf("font name: %s %f\n", NS_ConvertUTF16toUTF8(GetName()).get(), GetStyle()->size);
532 // printf ("pango font %s\n", pango_font_description_to_string (pango_font_describe (font)));
533
534 fprintf (stderr, "Font: %s\n", NS_ConvertUTF16toUTF8(GetName()).get());
535 fprintf (stderr, " emHeight: %f emAscent: %f emDescent: %f\n", mMetrics.emHeight, mMetrics.emAscent, mMetrics.emDescent);
536 fprintf (stderr, " maxAscent: %f maxDescent: %f\n", mMetrics.maxAscent, mMetrics.maxDescent);
537 fprintf (stderr, " internalLeading: %f externalLeading: %f\n", mMetrics.externalLeading, mMetrics.internalLeading);
538 fprintf (stderr, " spaceWidth: %f aveCharWidth: %f xHeight: %f\n", mMetrics.spaceWidth, mMetrics.aveCharWidth, mMetrics.xHeight);
539 fprintf (stderr, " uOff: %f uSize: %f stOff: %f stSize: %f\n", mMetrics.underlineOffset, mMetrics.underlineSize, mMetrics.strikeoutOffset, mMetrics.strikeoutSize);
540 #endif
541 }
542
GetHorizontalMetrics()543 const gfxFont::Metrics& gfxFT2FontBase::GetHorizontalMetrics() {
544 return mMetrics;
545 }
546
GetGlyph(uint32_t unicode,uint32_t variation_selector)547 uint32_t gfxFT2FontBase::GetGlyph(uint32_t unicode,
548 uint32_t variation_selector) {
549 if (variation_selector) {
550 uint32_t id =
551 gfxFT2LockedFace(this).GetUVSGlyph(unicode, variation_selector);
552 if (id) {
553 return id;
554 }
555 unicode = gfxFontUtils::GetUVSFallback(unicode, variation_selector);
556 if (unicode) {
557 return GetGlyph(unicode);
558 }
559 return 0;
560 }
561
562 return GetGlyph(unicode);
563 }
564
ShouldRoundXOffset(cairo_t * aCairo) const565 bool gfxFT2FontBase::ShouldRoundXOffset(cairo_t* aCairo) const {
566 // Force rounding if outputting to a Cairo context or if requested by pref to
567 // disable subpixel positioning. Otherwise, allow subpixel positioning (no
568 // rounding) if rendering a scalable outline font with anti-aliasing.
569 // Monochrome rendering or some bitmap fonts can become too distorted with
570 // subpixel positioning, so force rounding in those cases. Also be careful not
571 // to use subpixel positioning if the user requests full hinting via
572 // Fontconfig, which we detect by checking that neither hinting was disabled
573 // nor light hinting was requested. Allow pref to force subpixel positioning
574 // on even if full hinting was requested.
575 return MOZ_UNLIKELY(
576 StaticPrefs::
577 gfx_text_subpixel_position_force_disabled_AtStartup()) ||
578 aCairo != nullptr || !mFTFace || !FT_IS_SCALABLE(mFTFace->GetFace()) ||
579 (mFTLoadFlags & FT_LOAD_MONOCHROME) ||
580 !((mFTLoadFlags & FT_LOAD_NO_HINTING) ||
581 FT_LOAD_TARGET_MODE(mFTLoadFlags) == FT_RENDER_MODE_LIGHT ||
582 MOZ_UNLIKELY(
583 StaticPrefs::
584 gfx_text_subpixel_position_force_enabled_AtStartup()));
585 }
586
GetEmboldenStrength(FT_Face aFace)587 FT_Vector gfxFT2FontBase::GetEmboldenStrength(FT_Face aFace) {
588 FT_Vector strength = {0, 0};
589 if (!mEmbolden) {
590 return strength;
591 }
592
593 // If it's an outline glyph, we'll be using mozilla_glyphslot_embolden_less
594 // (see gfx/wr/webrender/src/platform/unix/font.rs), so we need to match its
595 // emboldening strength here.
596 if (aFace->glyph->format == FT_GLYPH_FORMAT_OUTLINE) {
597 strength.x =
598 FT_MulFix(aFace->units_per_EM, aFace->size->metrics.y_scale) / 48;
599 strength.y = strength.x;
600 return strength;
601 }
602
603 // This is the embolden "strength" used by FT_GlyphSlot_Embolden.
604 strength.x =
605 FT_MulFix(aFace->units_per_EM, aFace->size->metrics.y_scale) / 24;
606 strength.y = strength.x;
607 if (aFace->glyph->format == FT_GLYPH_FORMAT_BITMAP) {
608 strength.x &= -64;
609 if (!strength.x) {
610 strength.x = 64;
611 }
612 strength.y &= -64;
613 }
614 return strength;
615 }
616
GetFTGlyphExtents(uint16_t aGID,int32_t * aAdvance,IntRect * aBounds)617 bool gfxFT2FontBase::GetFTGlyphExtents(uint16_t aGID, int32_t* aAdvance,
618 IntRect* aBounds) {
619 gfxFT2LockedFace face(this);
620 MOZ_ASSERT(face.get());
621 if (!face.get()) {
622 // Failed to get the FT_Face? Give up already.
623 NS_WARNING("failed to get FT_Face!");
624 return false;
625 }
626
627 FT_Int32 flags = mFTLoadFlags;
628 if (!aBounds) {
629 flags |= FT_LOAD_ADVANCE_ONLY;
630 }
631 if (Factory::LoadFTGlyph(face.get(), aGID, flags) != FT_Err_Ok) {
632 // FT_Face was somehow broken/invalid? Don't try to access glyph slot.
633 // This probably shouldn't happen, but does: see bug 1440938.
634 NS_WARNING("failed to load glyph!");
635 return false;
636 }
637
638 // Whether to interpret hinting settings (i.e. not printing)
639 bool hintMetrics = ShouldHintMetrics();
640 // Whether to disable subpixel positioning
641 bool roundX = ShouldRoundXOffset(nullptr);
642 // No hinting disables X and Y hinting. Light disables only X hinting.
643 bool unhintedY = (mFTLoadFlags & FT_LOAD_NO_HINTING) != 0;
644 bool unhintedX =
645 unhintedY || FT_LOAD_TARGET_MODE(mFTLoadFlags) == FT_RENDER_MODE_LIGHT;
646
647 // Normalize out the loaded FT glyph size and then scale to the actually
648 // desired size, in case these two sizes differ.
649 gfxFloat extentsScale = GetAdjustedSize() / mFTSize;
650
651 FT_Vector bold = GetEmboldenStrength(face.get());
652
653 // Due to freetype bug 52683 we MUST use the linearHoriAdvance field when
654 // dealing with a variation font; also use it for scalable fonts when not
655 // applying hinting. Otherwise, prefer hinted width from glyph->advance.x.
656 if (aAdvance) {
657 FT_Fixed advance;
658 if (!roundX || FT_HAS_MULTIPLE_MASTERS(face.get())) {
659 advance = face.get()->glyph->linearHoriAdvance;
660 } else {
661 advance = face.get()->glyph->advance.x << 10; // convert 26.6 to 16.16
662 }
663 if (advance) {
664 advance += bold.x << 10; // convert 26.6 to 16.16
665 }
666 // Hinting was requested, but FT did not apply any hinting to the metrics.
667 // Round the advance here to approximate hinting as Cairo does. This must
668 // happen BEFORE we apply the glyph extents scale, just like FT hinting
669 // would.
670 if (hintMetrics && roundX && unhintedX) {
671 advance = (advance + 0x8000) & 0xffff0000u;
672 }
673 *aAdvance = NS_lround(advance * extentsScale);
674 }
675
676 if (aBounds) {
677 const FT_Glyph_Metrics& metrics = face.get()->glyph->metrics;
678 FT_F26Dot6 x = metrics.horiBearingX;
679 FT_F26Dot6 y = -metrics.horiBearingY;
680 FT_F26Dot6 x2 = x + metrics.width;
681 FT_F26Dot6 y2 = y + metrics.height;
682 // Synthetic bold moves the glyph top and right boundaries.
683 y -= bold.y;
684 x2 += bold.x;
685 if (hintMetrics) {
686 if (roundX && unhintedX) {
687 x &= -64;
688 x2 = (x2 + 63) & -64;
689 }
690 if (unhintedY) {
691 y &= -64;
692 y2 = (y2 + 63) & -64;
693 }
694 }
695 *aBounds = IntRect(x, y, x2 - x, y2 - y);
696 }
697 return true;
698 }
699
700 /**
701 * Get the cached glyph metrics for the glyph id if available. Otherwise, query
702 * FreeType for the glyph extents and initialize the glyph metrics.
703 */
GetCachedGlyphMetrics(uint16_t aGID,IntRect * aBounds)704 const gfxFT2FontBase::GlyphMetrics& gfxFT2FontBase::GetCachedGlyphMetrics(
705 uint16_t aGID, IntRect* aBounds) {
706 if (!mGlyphMetrics) {
707 mGlyphMetrics =
708 mozilla::MakeUnique<nsTHashMap<nsUint32HashKey, GlyphMetrics>>(128);
709 }
710
711 return mGlyphMetrics->LookupOrInsertWith(aGID, [&] {
712 GlyphMetrics metrics;
713 IntRect bounds;
714 if (GetFTGlyphExtents(aGID, &metrics.mAdvance, &bounds)) {
715 metrics.SetBounds(bounds);
716 if (aBounds) {
717 *aBounds = bounds;
718 }
719 }
720 return metrics;
721 });
722 }
723
GetGlyphWidth(uint16_t aGID)724 int32_t gfxFT2FontBase::GetGlyphWidth(uint16_t aGID) {
725 return GetCachedGlyphMetrics(aGID).mAdvance;
726 }
727
GetGlyphBounds(uint16_t aGID,gfxRect * aBounds,bool aTight)728 bool gfxFT2FontBase::GetGlyphBounds(uint16_t aGID, gfxRect* aBounds,
729 bool aTight) {
730 IntRect bounds;
731 const GlyphMetrics& metrics = GetCachedGlyphMetrics(aGID, &bounds);
732 if (!metrics.HasValidBounds()) {
733 return false;
734 }
735 // Check if there are cached bounds and use those if available. Otherwise,
736 // fall back to directly querying the glyph extents.
737 if (metrics.HasCachedBounds()) {
738 bounds = metrics.GetBounds();
739 } else if (bounds.IsEmpty() && !GetFTGlyphExtents(aGID, nullptr, &bounds)) {
740 return false;
741 }
742 // The bounds are stored unscaled, so must be scaled to the adjusted size.
743 *aBounds = ScaleGlyphBounds(bounds, GetAdjustedSize() / mFTSize);
744 return true;
745 }
746
747 // For variation fonts, figure out the variation coordinates to be applied
748 // for each axis, in freetype's order (which may not match the order of
749 // axes in mStyle.variationSettings, so we need to search by axis tag).
750 /*static*/
SetupVarCoords(FT_MM_Var * aMMVar,const nsTArray<gfxFontVariation> & aVariations,FT_Face aFTFace)751 void gfxFT2FontBase::SetupVarCoords(
752 FT_MM_Var* aMMVar, const nsTArray<gfxFontVariation>& aVariations,
753 FT_Face aFTFace) {
754 if (!aMMVar) {
755 return;
756 }
757
758 nsTArray<FT_Fixed> coords;
759 for (unsigned i = 0; i < aMMVar->num_axis; ++i) {
760 coords.AppendElement(aMMVar->axis[i].def);
761 for (const auto& v : aVariations) {
762 if (aMMVar->axis[i].tag == v.mTag) {
763 FT_Fixed val = v.mValue * 0x10000;
764 val = std::min(val, aMMVar->axis[i].maximum);
765 val = std::max(val, aMMVar->axis[i].minimum);
766 coords[i] = val;
767 break;
768 }
769 }
770 }
771
772 if (!coords.IsEmpty()) {
773 #if MOZ_TREE_FREETYPE
774 FT_Set_Var_Design_Coordinates(aFTFace, coords.Length(), coords.Elements());
775 #else
776 typedef FT_Error (*SetCoordsFunc)(FT_Face, FT_UInt, FT_Fixed*);
777 static SetCoordsFunc setCoords;
778 static bool firstTime = true;
779 if (firstTime) {
780 firstTime = false;
781 setCoords =
782 (SetCoordsFunc)dlsym(RTLD_DEFAULT, "FT_Set_Var_Design_Coordinates");
783 }
784 if (setCoords) {
785 (*setCoords)(aFTFace, coords.Length(), coords.Elements());
786 }
787 #endif
788 }
789 }
790
CloneFace(int aFaceIndex)791 already_AddRefed<SharedFTFace> FTUserFontData::CloneFace(int aFaceIndex) {
792 RefPtr<SharedFTFace> face = Factory::NewSharedFTFaceFromData(
793 nullptr, mFontData, mLength, aFaceIndex, this);
794 if (!face ||
795 (FT_Select_Charmap(face->GetFace(), FT_ENCODING_UNICODE) != FT_Err_Ok &&
796 FT_Select_Charmap(face->GetFace(), FT_ENCODING_MS_SYMBOL) !=
797 FT_Err_Ok)) {
798 return nullptr;
799 }
800 return face.forget();
801 }
802