michael@0: /* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 4 -*- michael@0: * This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #include "gfxFT2FontBase.h" michael@0: #include "gfxFT2Utils.h" michael@0: #include "mozilla/Likely.h" michael@0: #include FT_TRUETYPE_TAGS_H michael@0: #include FT_TRUETYPE_TABLES_H michael@0: #include michael@0: michael@0: #ifdef HAVE_FONTCONFIG_FCFREETYPE_H michael@0: #include michael@0: #endif michael@0: michael@0: #include "prlink.h" michael@0: michael@0: // aScale is intended for a 16.16 x/y_scale of an FT_Size_Metrics michael@0: static inline FT_Long michael@0: ScaleRoundDesignUnits(FT_Short aDesignMetric, FT_Fixed aScale) michael@0: { michael@0: FT_Long fixed26dot6 = FT_MulFix(aDesignMetric, aScale); michael@0: return ROUND_26_6_TO_INT(fixed26dot6); michael@0: } michael@0: michael@0: // Snap a line to pixels while keeping the center and size of the line as michael@0: // close to the original position as possible. michael@0: // michael@0: // Pango does similar snapping for underline and strikethrough when fonts are michael@0: // hinted, but nsCSSRendering::GetTextDecorationRectInternal always snaps the michael@0: // top and size of lines. Optimizing the distance between the line and michael@0: // baseline is probably good for the gap between text and underline, but michael@0: // optimizing the center of the line is better for positioning strikethough. michael@0: static void michael@0: SnapLineToPixels(gfxFloat& aOffset, gfxFloat& aSize) michael@0: { michael@0: gfxFloat snappedSize = std::max(floor(aSize + 0.5), 1.0); michael@0: // Correct offset for change in size michael@0: gfxFloat offset = aOffset - 0.5 * (aSize - snappedSize); michael@0: // Snap offset michael@0: aOffset = floor(offset + 0.5); michael@0: aSize = snappedSize; michael@0: } michael@0: michael@0: void michael@0: gfxFT2LockedFace::GetMetrics(gfxFont::Metrics* aMetrics, michael@0: uint32_t* aSpaceGlyph) michael@0: { michael@0: NS_PRECONDITION(aMetrics != nullptr, "aMetrics must not be NULL"); michael@0: NS_PRECONDITION(aSpaceGlyph != nullptr, "aSpaceGlyph must not be NULL"); michael@0: michael@0: if (MOZ_UNLIKELY(!mFace)) { michael@0: // No face. This unfortunate situation might happen if the font michael@0: // file is (re)moved at the wrong time. michael@0: const gfxFloat emHeight = mGfxFont->GetStyle()->size; michael@0: aMetrics->emHeight = emHeight; michael@0: aMetrics->maxAscent = aMetrics->emAscent = 0.8 * emHeight; michael@0: aMetrics->maxDescent = aMetrics->emDescent = 0.2 * emHeight; michael@0: aMetrics->maxHeight = emHeight; michael@0: aMetrics->internalLeading = 0.0; michael@0: aMetrics->externalLeading = 0.2 * emHeight; michael@0: const gfxFloat spaceWidth = 0.5 * emHeight; michael@0: aMetrics->spaceWidth = spaceWidth; michael@0: aMetrics->maxAdvance = spaceWidth; michael@0: aMetrics->aveCharWidth = spaceWidth; michael@0: aMetrics->zeroOrAveCharWidth = spaceWidth; michael@0: const gfxFloat xHeight = 0.5 * emHeight; michael@0: aMetrics->xHeight = xHeight; michael@0: aMetrics->superscriptOffset = xHeight; michael@0: aMetrics->subscriptOffset = xHeight; michael@0: const gfxFloat underlineSize = emHeight / 14.0; michael@0: aMetrics->underlineSize = underlineSize; michael@0: aMetrics->underlineOffset = -underlineSize; michael@0: aMetrics->strikeoutOffset = 0.25 * emHeight; michael@0: aMetrics->strikeoutSize = underlineSize; michael@0: michael@0: *aSpaceGlyph = 0; michael@0: return; michael@0: } michael@0: michael@0: const FT_Size_Metrics& ftMetrics = mFace->size->metrics; michael@0: michael@0: gfxFloat emHeight; michael@0: // Scale for vertical design metric conversion: pixels per design unit. michael@0: // If this remains at 0.0, we can't use metrics from OS/2 etc. michael@0: gfxFloat yScale = 0.0; michael@0: if (FT_IS_SCALABLE(mFace)) { michael@0: // Prefer FT_Size_Metrics::x_scale to x_ppem as x_ppem does not michael@0: // have subpixel accuracy. michael@0: // michael@0: // FT_Size_Metrics::y_scale is in 16.16 fixed point format. Its michael@0: // (fractional) value is a factor that converts vertical metrics from michael@0: // design units to units of 1/64 pixels, so that the result may be michael@0: // interpreted as pixels in 26.6 fixed point format. michael@0: yScale = FLOAT_FROM_26_6(FLOAT_FROM_16_16(ftMetrics.y_scale)); michael@0: emHeight = mFace->units_per_EM * yScale; michael@0: } else { // Not scalable. michael@0: emHeight = ftMetrics.y_ppem; michael@0: // FT_Face doc says units_per_EM and a bunch of following fields michael@0: // are "only relevant to scalable outlines". If it's an sfnt, michael@0: // we can get units_per_EM from the 'head' table instead; otherwise, michael@0: // we don't have a unitsPerEm value so we can't compute/use yScale. michael@0: const TT_Header* head = michael@0: static_cast(FT_Get_Sfnt_Table(mFace, ft_sfnt_head)); michael@0: if (head) { michael@0: gfxFloat emUnit = head->Units_Per_EM; michael@0: yScale = emHeight / emUnit; michael@0: } michael@0: } michael@0: michael@0: TT_OS2 *os2 = michael@0: static_cast(FT_Get_Sfnt_Table(mFace, ft_sfnt_os2)); michael@0: michael@0: aMetrics->maxAscent = FLOAT_FROM_26_6(ftMetrics.ascender); michael@0: aMetrics->maxDescent = -FLOAT_FROM_26_6(ftMetrics.descender); michael@0: aMetrics->maxAdvance = FLOAT_FROM_26_6(ftMetrics.max_advance); michael@0: michael@0: gfxFloat lineHeight; michael@0: if (os2 && os2->sTypoAscender && yScale > 0.0) { michael@0: aMetrics->emAscent = os2->sTypoAscender * yScale; michael@0: aMetrics->emDescent = -os2->sTypoDescender * yScale; michael@0: FT_Short typoHeight = michael@0: os2->sTypoAscender - os2->sTypoDescender + os2->sTypoLineGap; michael@0: lineHeight = typoHeight * yScale; michael@0: michael@0: // If the OS/2 fsSelection USE_TYPO_METRICS bit is set, michael@0: // or if this is an OpenType Math font, michael@0: // set maxAscent/Descent from the sTypo* fields instead of hhea. michael@0: const uint16_t kUseTypoMetricsMask = 1 << 7; michael@0: FT_ULong length = 0; michael@0: if ((os2->fsSelection & kUseTypoMetricsMask) || michael@0: 0 == FT_Load_Sfnt_Table(mFace, FT_MAKE_TAG('M','A','T','H'), michael@0: 0, nullptr, &length)) { michael@0: aMetrics->maxAscent = NS_round(aMetrics->emAscent); michael@0: aMetrics->maxDescent = NS_round(aMetrics->emDescent); michael@0: } else { michael@0: // maxAscent/maxDescent get used for frame heights, and some fonts michael@0: // don't have the HHEA table ascent/descent set (bug 279032). michael@0: // We use NS_round here to parallel the pixel-rounded values that michael@0: // freetype gives us for ftMetrics.ascender/descender. michael@0: aMetrics->maxAscent = michael@0: std::max(aMetrics->maxAscent, NS_round(aMetrics->emAscent)); michael@0: aMetrics->maxDescent = michael@0: std::max(aMetrics->maxDescent, NS_round(aMetrics->emDescent)); michael@0: } michael@0: } else { michael@0: aMetrics->emAscent = aMetrics->maxAscent; michael@0: aMetrics->emDescent = aMetrics->maxDescent; michael@0: lineHeight = FLOAT_FROM_26_6(ftMetrics.height); michael@0: } michael@0: michael@0: cairo_text_extents_t extents; michael@0: *aSpaceGlyph = GetCharExtents(' ', &extents); michael@0: if (*aSpaceGlyph) { michael@0: aMetrics->spaceWidth = extents.x_advance; michael@0: } else { michael@0: aMetrics->spaceWidth = aMetrics->maxAdvance; // guess michael@0: } michael@0: michael@0: aMetrics->zeroOrAveCharWidth = 0.0; michael@0: if (GetCharExtents('0', &extents)) { michael@0: aMetrics->zeroOrAveCharWidth = extents.x_advance; michael@0: } michael@0: michael@0: // Prefering a measured x over sxHeight because sxHeight doesn't consider michael@0: // hinting, but maybe the x extents are not quite right in some fancy michael@0: // script fonts. CSS 2.1 suggests possibly using the height of an "o", michael@0: // which would have a more consistent glyph across fonts. michael@0: if (GetCharExtents('x', &extents) && extents.y_bearing < 0.0) { michael@0: aMetrics->xHeight = -extents.y_bearing; michael@0: aMetrics->aveCharWidth = extents.x_advance; michael@0: } else { michael@0: if (os2 && os2->sxHeight && yScale > 0.0) { michael@0: aMetrics->xHeight = os2->sxHeight * yScale; michael@0: } else { michael@0: // CSS 2.1, section 4.3.2 Lengths: "In the cases where it is michael@0: // impossible or impractical to determine the x-height, a value of michael@0: // 0.5em should be used." michael@0: aMetrics->xHeight = 0.5 * emHeight; michael@0: } michael@0: aMetrics->aveCharWidth = 0.0; // updated below michael@0: } michael@0: // aveCharWidth is used for the width of text input elements so be michael@0: // liberal rather than conservative in the estimate. michael@0: if (os2 && os2->xAvgCharWidth) { michael@0: // Round to pixels as this is compared with maxAdvance to guess michael@0: // whether this is a fixed width font. michael@0: gfxFloat avgCharWidth = michael@0: ScaleRoundDesignUnits(os2->xAvgCharWidth, ftMetrics.x_scale); michael@0: aMetrics->aveCharWidth = michael@0: std::max(aMetrics->aveCharWidth, avgCharWidth); michael@0: } michael@0: aMetrics->aveCharWidth = michael@0: std::max(aMetrics->aveCharWidth, aMetrics->zeroOrAveCharWidth); michael@0: if (aMetrics->aveCharWidth == 0.0) { michael@0: aMetrics->aveCharWidth = aMetrics->spaceWidth; michael@0: } michael@0: if (aMetrics->zeroOrAveCharWidth == 0.0) { michael@0: aMetrics->zeroOrAveCharWidth = aMetrics->aveCharWidth; michael@0: } michael@0: // Apparently hinting can mean that max_advance is not always accurate. michael@0: aMetrics->maxAdvance = michael@0: std::max(aMetrics->maxAdvance, aMetrics->aveCharWidth); michael@0: michael@0: // gfxFont::Metrics::underlineOffset is the position of the top of the michael@0: // underline. michael@0: // michael@0: // FT_FaceRec documentation describes underline_position as "the michael@0: // center of the underlining stem". This was the original definition michael@0: // of the PostScript metric, but in the PostScript table of OpenType michael@0: // fonts the metric is "the top of the underline" michael@0: // (http://www.microsoft.com/typography/otspec/post.htm), and FreeType michael@0: // (up to version 2.3.7) doesn't make any adjustment. michael@0: // michael@0: // Therefore get the underline position directly from the table michael@0: // ourselves when this table exists. Use FreeType's metrics for michael@0: // other (including older PostScript) fonts. michael@0: if (mFace->underline_position && mFace->underline_thickness && yScale > 0.0) { michael@0: aMetrics->underlineSize = mFace->underline_thickness * yScale; michael@0: TT_Postscript *post = static_cast michael@0: (FT_Get_Sfnt_Table(mFace, ft_sfnt_post)); michael@0: if (post && post->underlinePosition) { michael@0: aMetrics->underlineOffset = post->underlinePosition * yScale; michael@0: } else { michael@0: aMetrics->underlineOffset = mFace->underline_position * yScale michael@0: + 0.5 * aMetrics->underlineSize; michael@0: } michael@0: } else { // No underline info. michael@0: // Imitate Pango. michael@0: aMetrics->underlineSize = emHeight / 14.0; michael@0: aMetrics->underlineOffset = -aMetrics->underlineSize; michael@0: } michael@0: michael@0: if (os2 && os2->yStrikeoutSize && os2->yStrikeoutPosition && yScale > 0.0) { michael@0: aMetrics->strikeoutSize = os2->yStrikeoutSize * yScale; michael@0: aMetrics->strikeoutOffset = os2->yStrikeoutPosition * yScale; michael@0: } else { // No strikeout info. michael@0: aMetrics->strikeoutSize = aMetrics->underlineSize; michael@0: // Use OpenType spec's suggested position for Roman font. michael@0: aMetrics->strikeoutOffset = emHeight * 409.0 / 2048.0 michael@0: + 0.5 * aMetrics->strikeoutSize; michael@0: } michael@0: SnapLineToPixels(aMetrics->strikeoutOffset, aMetrics->strikeoutSize); michael@0: michael@0: if (os2 && os2->ySuperscriptYOffset) { michael@0: gfxFloat val = ScaleRoundDesignUnits(os2->ySuperscriptYOffset, michael@0: ftMetrics.y_scale); michael@0: aMetrics->superscriptOffset = std::max(1.0, val); michael@0: } else { michael@0: aMetrics->superscriptOffset = aMetrics->xHeight; michael@0: } michael@0: michael@0: if (os2 && os2->ySubscriptYOffset) { michael@0: gfxFloat val = ScaleRoundDesignUnits(os2->ySubscriptYOffset, michael@0: ftMetrics.y_scale); michael@0: // some fonts have the incorrect sign. michael@0: val = fabs(val); michael@0: aMetrics->subscriptOffset = std::max(1.0, val); michael@0: } else { michael@0: aMetrics->subscriptOffset = aMetrics->xHeight; michael@0: } michael@0: michael@0: aMetrics->maxHeight = aMetrics->maxAscent + aMetrics->maxDescent; michael@0: michael@0: // Make the line height an integer number of pixels so that lines will be michael@0: // equally spaced (rather than just being snapped to pixels, some up and michael@0: // some down). Layout calculates line height from the emHeight + michael@0: // internalLeading + externalLeading, but first each of these is rounded michael@0: // to layout units. To ensure that the result is an integer number of michael@0: // pixels, round each of the components to pixels. michael@0: aMetrics->emHeight = floor(emHeight + 0.5); michael@0: michael@0: // maxHeight will normally be an integer, but round anyway in case michael@0: // FreeType is configured differently. michael@0: aMetrics->internalLeading = michael@0: floor(aMetrics->maxHeight - aMetrics->emHeight + 0.5); michael@0: michael@0: // Text input boxes currently don't work well with lineHeight michael@0: // significantly less than maxHeight (with Verdana, for example). michael@0: lineHeight = floor(std::max(lineHeight, aMetrics->maxHeight) + 0.5); michael@0: aMetrics->externalLeading = michael@0: lineHeight - aMetrics->internalLeading - aMetrics->emHeight; michael@0: michael@0: // Ensure emAscent + emDescent == emHeight michael@0: gfxFloat sum = aMetrics->emAscent + aMetrics->emDescent; michael@0: aMetrics->emAscent = sum > 0.0 ? michael@0: aMetrics->emAscent * aMetrics->emHeight / sum : 0.0; michael@0: aMetrics->emDescent = aMetrics->emHeight - aMetrics->emAscent; michael@0: } michael@0: michael@0: uint32_t michael@0: gfxFT2LockedFace::GetGlyph(uint32_t aCharCode) michael@0: { michael@0: if (MOZ_UNLIKELY(!mFace)) michael@0: return 0; michael@0: michael@0: #ifdef HAVE_FONTCONFIG_FCFREETYPE_H michael@0: // FcFreeTypeCharIndex will search starting from the most recently michael@0: // selected charmap. This can cause non-determistic behavior when more michael@0: // than one charmap supports a character but with different glyphs, as michael@0: // with older versions of MS Gothic, for example. Always prefer a Unicode michael@0: // charmap, if there is one. (FcFreeTypeCharIndex usually does the michael@0: // appropriate Unicode conversion, but some fonts have non-Roman glyphs michael@0: // for FT_ENCODING_APPLE_ROMAN characters.) michael@0: if (!mFace->charmap || mFace->charmap->encoding != FT_ENCODING_UNICODE) { michael@0: FT_Select_Charmap(mFace, FT_ENCODING_UNICODE); michael@0: } michael@0: michael@0: return FcFreeTypeCharIndex(mFace, aCharCode); michael@0: #else michael@0: return FT_Get_Char_Index(mFace, aCharCode); michael@0: #endif michael@0: } michael@0: michael@0: typedef FT_UInt (*GetCharVariantFunction)(FT_Face face, michael@0: FT_ULong charcode, michael@0: FT_ULong variantSelector); michael@0: michael@0: uint32_t michael@0: gfxFT2LockedFace::GetUVSGlyph(uint32_t aCharCode, uint32_t aVariantSelector) michael@0: { michael@0: NS_PRECONDITION(aVariantSelector, "aVariantSelector should not be NULL"); michael@0: michael@0: if (MOZ_UNLIKELY(!mFace)) michael@0: return 0; michael@0: michael@0: // This function is available from FreeType 2.3.6 (June 2008). michael@0: static CharVariantFunction sGetCharVariantPtr = FindCharVariantFunction(); michael@0: if (!sGetCharVariantPtr) michael@0: return 0; michael@0: michael@0: #ifdef HAVE_FONTCONFIG_FCFREETYPE_H michael@0: // FcFreeTypeCharIndex may have changed the selected charmap. michael@0: // FT_Face_GetCharVariantIndex needs a unicode charmap. michael@0: if (!mFace->charmap || mFace->charmap->encoding != FT_ENCODING_UNICODE) { michael@0: FT_Select_Charmap(mFace, FT_ENCODING_UNICODE); michael@0: } michael@0: #endif michael@0: michael@0: return (*sGetCharVariantPtr)(mFace, aCharCode, aVariantSelector); michael@0: } michael@0: michael@0: uint32_t michael@0: gfxFT2LockedFace::GetCharExtents(char aChar, cairo_text_extents_t* aExtents) michael@0: { michael@0: NS_PRECONDITION(aExtents != nullptr, "aExtents must not be NULL"); michael@0: michael@0: if (!mFace) michael@0: return 0; michael@0: michael@0: FT_UInt gid = mGfxFont->GetGlyph(aChar); michael@0: if (gid) { michael@0: mGfxFont->GetGlyphExtents(gid, aExtents); michael@0: } michael@0: michael@0: return gid; michael@0: } michael@0: michael@0: gfxFT2LockedFace::CharVariantFunction michael@0: gfxFT2LockedFace::FindCharVariantFunction() michael@0: { michael@0: // This function is available from FreeType 2.3.6 (June 2008). michael@0: PRLibrary *lib = nullptr; michael@0: CharVariantFunction function = michael@0: reinterpret_cast michael@0: (PR_FindFunctionSymbolAndLibrary("FT_Face_GetCharVariantIndex", &lib)); michael@0: if (!lib) { michael@0: return nullptr; michael@0: } michael@0: michael@0: FT_Int major; michael@0: FT_Int minor; michael@0: FT_Int patch; michael@0: FT_Library_Version(mFace->glyph->library, &major, &minor, &patch); michael@0: michael@0: // Versions 2.4.0 to 2.4.3 crash if configured with michael@0: // FT_CONFIG_OPTION_OLD_INTERNALS. Presence of the symbol FT_Alloc michael@0: // indicates FT_CONFIG_OPTION_OLD_INTERNALS. michael@0: if (major == 2 && minor == 4 && patch < 4 && michael@0: PR_FindFunctionSymbol(lib, "FT_Alloc")) { michael@0: function = nullptr; michael@0: } michael@0: michael@0: // Decrement the reference count incremented in michael@0: // PR_FindFunctionSymbolAndLibrary. michael@0: PR_UnloadLibrary(lib); michael@0: michael@0: return function; michael@0: }