gfx/thebes/gfxFT2Utils.cpp

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/gfx/thebes/gfxFT2Utils.cpp	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,390 @@
     1.4 +/* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 4 -*-
     1.5 + * This Source Code Form is subject to the terms of the Mozilla Public
     1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.8 +
     1.9 +#include "gfxFT2FontBase.h"
    1.10 +#include "gfxFT2Utils.h"
    1.11 +#include "mozilla/Likely.h"
    1.12 +#include FT_TRUETYPE_TAGS_H
    1.13 +#include FT_TRUETYPE_TABLES_H
    1.14 +#include <algorithm>
    1.15 +
    1.16 +#ifdef HAVE_FONTCONFIG_FCFREETYPE_H
    1.17 +#include <fontconfig/fcfreetype.h>
    1.18 +#endif
    1.19 +
    1.20 +#include "prlink.h"
    1.21 +
    1.22 +// aScale is intended for a 16.16 x/y_scale of an FT_Size_Metrics
    1.23 +static inline FT_Long
    1.24 +ScaleRoundDesignUnits(FT_Short aDesignMetric, FT_Fixed aScale)
    1.25 +{
    1.26 +    FT_Long fixed26dot6 = FT_MulFix(aDesignMetric, aScale);
    1.27 +    return ROUND_26_6_TO_INT(fixed26dot6);
    1.28 +}
    1.29 +
    1.30 +// Snap a line to pixels while keeping the center and size of the line as
    1.31 +// close to the original position as possible.
    1.32 +//
    1.33 +// Pango does similar snapping for underline and strikethrough when fonts are
    1.34 +// hinted, but nsCSSRendering::GetTextDecorationRectInternal always snaps the
    1.35 +// top and size of lines.  Optimizing the distance between the line and
    1.36 +// baseline is probably good for the gap between text and underline, but
    1.37 +// optimizing the center of the line is better for positioning strikethough.
    1.38 +static void
    1.39 +SnapLineToPixels(gfxFloat& aOffset, gfxFloat& aSize)
    1.40 +{
    1.41 +    gfxFloat snappedSize = std::max(floor(aSize + 0.5), 1.0);
    1.42 +    // Correct offset for change in size
    1.43 +    gfxFloat offset = aOffset - 0.5 * (aSize - snappedSize);
    1.44 +    // Snap offset
    1.45 +    aOffset = floor(offset + 0.5);
    1.46 +    aSize = snappedSize;
    1.47 +}
    1.48 +
    1.49 +void
    1.50 +gfxFT2LockedFace::GetMetrics(gfxFont::Metrics* aMetrics,
    1.51 +                             uint32_t* aSpaceGlyph)
    1.52 +{
    1.53 +    NS_PRECONDITION(aMetrics != nullptr, "aMetrics must not be NULL");
    1.54 +    NS_PRECONDITION(aSpaceGlyph != nullptr, "aSpaceGlyph must not be NULL");
    1.55 +
    1.56 +    if (MOZ_UNLIKELY(!mFace)) {
    1.57 +        // No face.  This unfortunate situation might happen if the font
    1.58 +        // file is (re)moved at the wrong time.
    1.59 +        const gfxFloat emHeight = mGfxFont->GetStyle()->size;
    1.60 +        aMetrics->emHeight = emHeight;
    1.61 +        aMetrics->maxAscent = aMetrics->emAscent = 0.8 * emHeight;
    1.62 +        aMetrics->maxDescent = aMetrics->emDescent = 0.2 * emHeight;
    1.63 +        aMetrics->maxHeight = emHeight;
    1.64 +        aMetrics->internalLeading = 0.0;
    1.65 +        aMetrics->externalLeading = 0.2 * emHeight;
    1.66 +        const gfxFloat spaceWidth = 0.5 * emHeight;
    1.67 +        aMetrics->spaceWidth = spaceWidth;
    1.68 +        aMetrics->maxAdvance = spaceWidth;
    1.69 +        aMetrics->aveCharWidth = spaceWidth;
    1.70 +        aMetrics->zeroOrAveCharWidth = spaceWidth;
    1.71 +        const gfxFloat xHeight = 0.5 * emHeight;
    1.72 +        aMetrics->xHeight = xHeight;
    1.73 +        aMetrics->superscriptOffset = xHeight;
    1.74 +        aMetrics->subscriptOffset = xHeight;
    1.75 +        const gfxFloat underlineSize = emHeight / 14.0;
    1.76 +        aMetrics->underlineSize = underlineSize;
    1.77 +        aMetrics->underlineOffset = -underlineSize;
    1.78 +        aMetrics->strikeoutOffset = 0.25 * emHeight;
    1.79 +        aMetrics->strikeoutSize = underlineSize;
    1.80 +
    1.81 +        *aSpaceGlyph = 0;
    1.82 +        return;
    1.83 +    }
    1.84 +
    1.85 +    const FT_Size_Metrics& ftMetrics = mFace->size->metrics;
    1.86 +
    1.87 +    gfxFloat emHeight;
    1.88 +    // Scale for vertical design metric conversion: pixels per design unit.
    1.89 +    // If this remains at 0.0, we can't use metrics from OS/2 etc.
    1.90 +    gfxFloat yScale = 0.0;
    1.91 +    if (FT_IS_SCALABLE(mFace)) {
    1.92 +        // Prefer FT_Size_Metrics::x_scale to x_ppem as x_ppem does not
    1.93 +        // have subpixel accuracy.
    1.94 +        //
    1.95 +        // FT_Size_Metrics::y_scale is in 16.16 fixed point format.  Its
    1.96 +        // (fractional) value is a factor that converts vertical metrics from
    1.97 +        // design units to units of 1/64 pixels, so that the result may be
    1.98 +        // interpreted as pixels in 26.6 fixed point format.
    1.99 +        yScale = FLOAT_FROM_26_6(FLOAT_FROM_16_16(ftMetrics.y_scale));
   1.100 +        emHeight = mFace->units_per_EM * yScale;
   1.101 +    } else { // Not scalable.
   1.102 +        emHeight = ftMetrics.y_ppem;
   1.103 +        // FT_Face doc says units_per_EM and a bunch of following fields
   1.104 +        // are "only relevant to scalable outlines". If it's an sfnt,
   1.105 +        // we can get units_per_EM from the 'head' table instead; otherwise,
   1.106 +        // we don't have a unitsPerEm value so we can't compute/use yScale.
   1.107 +        const TT_Header* head =
   1.108 +            static_cast<TT_Header*>(FT_Get_Sfnt_Table(mFace, ft_sfnt_head));
   1.109 +        if (head) {
   1.110 +            gfxFloat emUnit = head->Units_Per_EM;
   1.111 +            yScale = emHeight / emUnit;
   1.112 +        }
   1.113 +    }
   1.114 +
   1.115 +    TT_OS2 *os2 =
   1.116 +        static_cast<TT_OS2*>(FT_Get_Sfnt_Table(mFace, ft_sfnt_os2));
   1.117 +
   1.118 +    aMetrics->maxAscent = FLOAT_FROM_26_6(ftMetrics.ascender);
   1.119 +    aMetrics->maxDescent = -FLOAT_FROM_26_6(ftMetrics.descender);
   1.120 +    aMetrics->maxAdvance = FLOAT_FROM_26_6(ftMetrics.max_advance);
   1.121 +
   1.122 +    gfxFloat lineHeight;
   1.123 +    if (os2 && os2->sTypoAscender && yScale > 0.0) {
   1.124 +        aMetrics->emAscent = os2->sTypoAscender * yScale;
   1.125 +        aMetrics->emDescent = -os2->sTypoDescender * yScale;
   1.126 +        FT_Short typoHeight =
   1.127 +            os2->sTypoAscender - os2->sTypoDescender + os2->sTypoLineGap;
   1.128 +        lineHeight = typoHeight * yScale;
   1.129 +
   1.130 +        // If the OS/2 fsSelection USE_TYPO_METRICS bit is set,
   1.131 +        // or if this is an OpenType Math font,
   1.132 +        // set maxAscent/Descent from the sTypo* fields instead of hhea.
   1.133 +        const uint16_t kUseTypoMetricsMask = 1 << 7;
   1.134 +        FT_ULong length = 0;
   1.135 +        if ((os2->fsSelection & kUseTypoMetricsMask) ||
   1.136 +            0 == FT_Load_Sfnt_Table(mFace, FT_MAKE_TAG('M','A','T','H'),
   1.137 +                                    0, nullptr, &length)) {
   1.138 +            aMetrics->maxAscent = NS_round(aMetrics->emAscent);
   1.139 +            aMetrics->maxDescent = NS_round(aMetrics->emDescent);
   1.140 +        } else {
   1.141 +            // maxAscent/maxDescent get used for frame heights, and some fonts
   1.142 +            // don't have the HHEA table ascent/descent set (bug 279032).
   1.143 +            // We use NS_round here to parallel the pixel-rounded values that
   1.144 +            // freetype gives us for ftMetrics.ascender/descender.
   1.145 +            aMetrics->maxAscent =
   1.146 +                std::max(aMetrics->maxAscent, NS_round(aMetrics->emAscent));
   1.147 +            aMetrics->maxDescent =
   1.148 +                std::max(aMetrics->maxDescent, NS_round(aMetrics->emDescent));
   1.149 +        }
   1.150 +    } else {
   1.151 +        aMetrics->emAscent = aMetrics->maxAscent;
   1.152 +        aMetrics->emDescent = aMetrics->maxDescent;
   1.153 +        lineHeight = FLOAT_FROM_26_6(ftMetrics.height);
   1.154 +    }
   1.155 +
   1.156 +    cairo_text_extents_t extents;
   1.157 +    *aSpaceGlyph = GetCharExtents(' ', &extents);
   1.158 +    if (*aSpaceGlyph) {
   1.159 +        aMetrics->spaceWidth = extents.x_advance;
   1.160 +    } else {
   1.161 +        aMetrics->spaceWidth = aMetrics->maxAdvance; // guess
   1.162 +    }
   1.163 +
   1.164 +    aMetrics->zeroOrAveCharWidth = 0.0;
   1.165 +    if (GetCharExtents('0', &extents)) {
   1.166 +        aMetrics->zeroOrAveCharWidth = extents.x_advance;
   1.167 +    }
   1.168 +
   1.169 +    // Prefering a measured x over sxHeight because sxHeight doesn't consider
   1.170 +    // hinting, but maybe the x extents are not quite right in some fancy
   1.171 +    // script fonts.  CSS 2.1 suggests possibly using the height of an "o",
   1.172 +    // which would have a more consistent glyph across fonts.
   1.173 +    if (GetCharExtents('x', &extents) && extents.y_bearing < 0.0) {
   1.174 +        aMetrics->xHeight = -extents.y_bearing;
   1.175 +        aMetrics->aveCharWidth = extents.x_advance;
   1.176 +    } else {
   1.177 +        if (os2 && os2->sxHeight && yScale > 0.0) {
   1.178 +            aMetrics->xHeight = os2->sxHeight * yScale;
   1.179 +        } else {
   1.180 +            // CSS 2.1, section 4.3.2 Lengths: "In the cases where it is
   1.181 +            // impossible or impractical to determine the x-height, a value of
   1.182 +            // 0.5em should be used."
   1.183 +            aMetrics->xHeight = 0.5 * emHeight;
   1.184 +        }
   1.185 +        aMetrics->aveCharWidth = 0.0; // updated below
   1.186 +    }
   1.187 +    // aveCharWidth is used for the width of text input elements so be
   1.188 +    // liberal rather than conservative in the estimate.
   1.189 +    if (os2 && os2->xAvgCharWidth) {
   1.190 +        // Round to pixels as this is compared with maxAdvance to guess
   1.191 +        // whether this is a fixed width font.
   1.192 +        gfxFloat avgCharWidth =
   1.193 +            ScaleRoundDesignUnits(os2->xAvgCharWidth, ftMetrics.x_scale);
   1.194 +        aMetrics->aveCharWidth =
   1.195 +            std::max(aMetrics->aveCharWidth, avgCharWidth);
   1.196 +    }
   1.197 +    aMetrics->aveCharWidth =
   1.198 +        std::max(aMetrics->aveCharWidth, aMetrics->zeroOrAveCharWidth);
   1.199 +    if (aMetrics->aveCharWidth == 0.0) {
   1.200 +        aMetrics->aveCharWidth = aMetrics->spaceWidth;
   1.201 +    }
   1.202 +    if (aMetrics->zeroOrAveCharWidth == 0.0) {
   1.203 +        aMetrics->zeroOrAveCharWidth = aMetrics->aveCharWidth;
   1.204 +    }
   1.205 +    // Apparently hinting can mean that max_advance is not always accurate.
   1.206 +    aMetrics->maxAdvance =
   1.207 +        std::max(aMetrics->maxAdvance, aMetrics->aveCharWidth);
   1.208 +
   1.209 +    // gfxFont::Metrics::underlineOffset is the position of the top of the
   1.210 +    // underline.
   1.211 +    //
   1.212 +    // FT_FaceRec documentation describes underline_position as "the
   1.213 +    // center of the underlining stem".  This was the original definition
   1.214 +    // of the PostScript metric, but in the PostScript table of OpenType
   1.215 +    // fonts the metric is "the top of the underline"
   1.216 +    // (http://www.microsoft.com/typography/otspec/post.htm), and FreeType
   1.217 +    // (up to version 2.3.7) doesn't make any adjustment.
   1.218 +    //
   1.219 +    // Therefore get the underline position directly from the table
   1.220 +    // ourselves when this table exists.  Use FreeType's metrics for
   1.221 +    // other (including older PostScript) fonts.
   1.222 +    if (mFace->underline_position && mFace->underline_thickness && yScale > 0.0) {
   1.223 +        aMetrics->underlineSize = mFace->underline_thickness * yScale;
   1.224 +        TT_Postscript *post = static_cast<TT_Postscript*>
   1.225 +            (FT_Get_Sfnt_Table(mFace, ft_sfnt_post));
   1.226 +        if (post && post->underlinePosition) {
   1.227 +            aMetrics->underlineOffset = post->underlinePosition * yScale;
   1.228 +        } else {
   1.229 +            aMetrics->underlineOffset = mFace->underline_position * yScale
   1.230 +                + 0.5 * aMetrics->underlineSize;
   1.231 +        }
   1.232 +    } else { // No underline info.
   1.233 +        // Imitate Pango.
   1.234 +        aMetrics->underlineSize = emHeight / 14.0;
   1.235 +        aMetrics->underlineOffset = -aMetrics->underlineSize;
   1.236 +    }
   1.237 +
   1.238 +    if (os2 && os2->yStrikeoutSize && os2->yStrikeoutPosition && yScale > 0.0) {
   1.239 +        aMetrics->strikeoutSize = os2->yStrikeoutSize * yScale;
   1.240 +        aMetrics->strikeoutOffset = os2->yStrikeoutPosition * yScale;
   1.241 +    } else { // No strikeout info.
   1.242 +        aMetrics->strikeoutSize = aMetrics->underlineSize;
   1.243 +        // Use OpenType spec's suggested position for Roman font.
   1.244 +        aMetrics->strikeoutOffset = emHeight * 409.0 / 2048.0
   1.245 +            + 0.5 * aMetrics->strikeoutSize;
   1.246 +    }
   1.247 +    SnapLineToPixels(aMetrics->strikeoutOffset, aMetrics->strikeoutSize);
   1.248 +
   1.249 +    if (os2 && os2->ySuperscriptYOffset) {
   1.250 +        gfxFloat val = ScaleRoundDesignUnits(os2->ySuperscriptYOffset,
   1.251 +                                             ftMetrics.y_scale);
   1.252 +        aMetrics->superscriptOffset = std::max(1.0, val);
   1.253 +    } else {
   1.254 +        aMetrics->superscriptOffset = aMetrics->xHeight;
   1.255 +    }
   1.256 +    
   1.257 +    if (os2 && os2->ySubscriptYOffset) {
   1.258 +        gfxFloat val = ScaleRoundDesignUnits(os2->ySubscriptYOffset,
   1.259 +                                             ftMetrics.y_scale);
   1.260 +        // some fonts have the incorrect sign. 
   1.261 +        val = fabs(val);
   1.262 +        aMetrics->subscriptOffset = std::max(1.0, val);
   1.263 +    } else {
   1.264 +        aMetrics->subscriptOffset = aMetrics->xHeight;
   1.265 +    }
   1.266 +
   1.267 +    aMetrics->maxHeight = aMetrics->maxAscent + aMetrics->maxDescent;
   1.268 +
   1.269 +    // Make the line height an integer number of pixels so that lines will be
   1.270 +    // equally spaced (rather than just being snapped to pixels, some up and
   1.271 +    // some down).  Layout calculates line height from the emHeight +
   1.272 +    // internalLeading + externalLeading, but first each of these is rounded
   1.273 +    // to layout units.  To ensure that the result is an integer number of
   1.274 +    // pixels, round each of the components to pixels.
   1.275 +    aMetrics->emHeight = floor(emHeight + 0.5);
   1.276 +
   1.277 +    // maxHeight will normally be an integer, but round anyway in case
   1.278 +    // FreeType is configured differently.
   1.279 +    aMetrics->internalLeading =
   1.280 +        floor(aMetrics->maxHeight - aMetrics->emHeight + 0.5);
   1.281 +
   1.282 +    // Text input boxes currently don't work well with lineHeight
   1.283 +    // significantly less than maxHeight (with Verdana, for example).
   1.284 +    lineHeight = floor(std::max(lineHeight, aMetrics->maxHeight) + 0.5);
   1.285 +    aMetrics->externalLeading =
   1.286 +        lineHeight - aMetrics->internalLeading - aMetrics->emHeight;
   1.287 +
   1.288 +    // Ensure emAscent + emDescent == emHeight
   1.289 +    gfxFloat sum = aMetrics->emAscent + aMetrics->emDescent;
   1.290 +    aMetrics->emAscent = sum > 0.0 ?
   1.291 +        aMetrics->emAscent * aMetrics->emHeight / sum : 0.0;
   1.292 +    aMetrics->emDescent = aMetrics->emHeight - aMetrics->emAscent;
   1.293 +}
   1.294 +
   1.295 +uint32_t
   1.296 +gfxFT2LockedFace::GetGlyph(uint32_t aCharCode)
   1.297 +{
   1.298 +    if (MOZ_UNLIKELY(!mFace))
   1.299 +        return 0;
   1.300 +
   1.301 +#ifdef HAVE_FONTCONFIG_FCFREETYPE_H
   1.302 +    // FcFreeTypeCharIndex will search starting from the most recently
   1.303 +    // selected charmap.  This can cause non-determistic behavior when more
   1.304 +    // than one charmap supports a character but with different glyphs, as
   1.305 +    // with older versions of MS Gothic, for example.  Always prefer a Unicode
   1.306 +    // charmap, if there is one.  (FcFreeTypeCharIndex usually does the
   1.307 +    // appropriate Unicode conversion, but some fonts have non-Roman glyphs
   1.308 +    // for FT_ENCODING_APPLE_ROMAN characters.)
   1.309 +    if (!mFace->charmap || mFace->charmap->encoding != FT_ENCODING_UNICODE) {
   1.310 +        FT_Select_Charmap(mFace, FT_ENCODING_UNICODE);
   1.311 +    }
   1.312 +
   1.313 +    return FcFreeTypeCharIndex(mFace, aCharCode);
   1.314 +#else
   1.315 +    return FT_Get_Char_Index(mFace, aCharCode);
   1.316 +#endif
   1.317 +}
   1.318 +
   1.319 +typedef FT_UInt (*GetCharVariantFunction)(FT_Face  face,
   1.320 +                                          FT_ULong charcode,
   1.321 +                                          FT_ULong variantSelector);
   1.322 +
   1.323 +uint32_t
   1.324 +gfxFT2LockedFace::GetUVSGlyph(uint32_t aCharCode, uint32_t aVariantSelector)
   1.325 +{
   1.326 +    NS_PRECONDITION(aVariantSelector, "aVariantSelector should not be NULL");
   1.327 +
   1.328 +    if (MOZ_UNLIKELY(!mFace))
   1.329 +        return 0;
   1.330 +
   1.331 +    // This function is available from FreeType 2.3.6 (June 2008).
   1.332 +    static CharVariantFunction sGetCharVariantPtr = FindCharVariantFunction();
   1.333 +    if (!sGetCharVariantPtr)
   1.334 +        return 0;
   1.335 +
   1.336 +#ifdef HAVE_FONTCONFIG_FCFREETYPE_H
   1.337 +    // FcFreeTypeCharIndex may have changed the selected charmap.
   1.338 +    // FT_Face_GetCharVariantIndex needs a unicode charmap.
   1.339 +    if (!mFace->charmap || mFace->charmap->encoding != FT_ENCODING_UNICODE) {
   1.340 +        FT_Select_Charmap(mFace, FT_ENCODING_UNICODE);
   1.341 +    }
   1.342 +#endif
   1.343 +
   1.344 +    return (*sGetCharVariantPtr)(mFace, aCharCode, aVariantSelector);
   1.345 +}
   1.346 +
   1.347 +uint32_t
   1.348 +gfxFT2LockedFace::GetCharExtents(char aChar, cairo_text_extents_t* aExtents)
   1.349 +{
   1.350 +    NS_PRECONDITION(aExtents != nullptr, "aExtents must not be NULL");
   1.351 +
   1.352 +    if (!mFace)
   1.353 +        return 0;
   1.354 +
   1.355 +    FT_UInt gid = mGfxFont->GetGlyph(aChar);
   1.356 +    if (gid) {
   1.357 +        mGfxFont->GetGlyphExtents(gid, aExtents);
   1.358 +    }
   1.359 +
   1.360 +    return gid;
   1.361 +}
   1.362 +
   1.363 +gfxFT2LockedFace::CharVariantFunction
   1.364 +gfxFT2LockedFace::FindCharVariantFunction()
   1.365 +{
   1.366 +    // This function is available from FreeType 2.3.6 (June 2008).
   1.367 +    PRLibrary *lib = nullptr;
   1.368 +    CharVariantFunction function =
   1.369 +        reinterpret_cast<CharVariantFunction>
   1.370 +        (PR_FindFunctionSymbolAndLibrary("FT_Face_GetCharVariantIndex", &lib));
   1.371 +    if (!lib) {
   1.372 +        return nullptr;
   1.373 +    }
   1.374 +
   1.375 +    FT_Int major;
   1.376 +    FT_Int minor;
   1.377 +    FT_Int patch;
   1.378 +    FT_Library_Version(mFace->glyph->library, &major, &minor, &patch);
   1.379 +
   1.380 +    // Versions 2.4.0 to 2.4.3 crash if configured with
   1.381 +    // FT_CONFIG_OPTION_OLD_INTERNALS.  Presence of the symbol FT_Alloc
   1.382 +    // indicates FT_CONFIG_OPTION_OLD_INTERNALS.
   1.383 +    if (major == 2 && minor == 4 && patch < 4 &&
   1.384 +        PR_FindFunctionSymbol(lib, "FT_Alloc")) {
   1.385 +        function = nullptr;
   1.386 +    }
   1.387 +
   1.388 +    // Decrement the reference count incremented in
   1.389 +    // PR_FindFunctionSymbolAndLibrary.
   1.390 +    PR_UnloadLibrary(lib);
   1.391 +
   1.392 +    return function;
   1.393 +}

mercurial