gfx/skia/trunk/src/core/SkScalerContext.h

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/gfx/skia/trunk/src/core/SkScalerContext.h	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,346 @@
     1.4 +/*
     1.5 + * Copyright 2006 The Android Open Source Project
     1.6 + *
     1.7 + * Use of this source code is governed by a BSD-style license that can be
     1.8 + * found in the LICENSE file.
     1.9 + */
    1.10 +
    1.11 +#ifndef SkScalerContext_DEFINED
    1.12 +#define SkScalerContext_DEFINED
    1.13 +
    1.14 +#include "SkMask.h"
    1.15 +#include "SkMaskGamma.h"
    1.16 +#include "SkMatrix.h"
    1.17 +#include "SkPaint.h"
    1.18 +#include "SkTypeface.h"
    1.19 +
    1.20 +#ifdef SK_BUILD_FOR_ANDROID
    1.21 +    #include "SkPaintOptionsAndroid.h"
    1.22 +#endif
    1.23 +
    1.24 +struct SkGlyph;
    1.25 +class SkDescriptor;
    1.26 +class SkMaskFilter;
    1.27 +class SkPathEffect;
    1.28 +class SkRasterizer;
    1.29 +
    1.30 +/*
    1.31 + *  To allow this to be forward-declared, it must be its own typename, rather
    1.32 + *  than a nested struct inside SkScalerContext (where it started).
    1.33 + */
    1.34 +struct SkScalerContextRec {
    1.35 +    uint32_t    fOrigFontID;
    1.36 +    uint32_t    fFontID;
    1.37 +    SkScalar    fTextSize, fPreScaleX, fPreSkewX;
    1.38 +    SkScalar    fPost2x2[2][2];
    1.39 +    SkScalar    fFrameWidth, fMiterLimit;
    1.40 +
    1.41 +    //These describe the parameters to create (uniquely identify) the pre-blend.
    1.42 +    uint32_t    fLumBits;
    1.43 +    uint8_t     fDeviceGamma; //2.6, (0.0, 4.0) gamma, 0.0 for sRGB
    1.44 +    uint8_t     fPaintGamma;  //2.6, (0.0, 4.0) gamma, 0.0 for sRGB
    1.45 +    uint8_t     fContrast;    //0.8+1, [0.0, 1.0] artificial contrast
    1.46 +    uint8_t     fReservedAlign;
    1.47 +
    1.48 +    SkScalar getDeviceGamma() const {
    1.49 +        return SkIntToScalar(fDeviceGamma) / (1 << 6);
    1.50 +    }
    1.51 +    void setDeviceGamma(SkScalar dg) {
    1.52 +        SkASSERT(0 <= dg && dg < SkIntToScalar(4));
    1.53 +        fDeviceGamma = SkScalarFloorToInt(dg * (1 << 6));
    1.54 +    }
    1.55 +
    1.56 +    SkScalar getPaintGamma() const {
    1.57 +        return SkIntToScalar(fPaintGamma) / (1 << 6);
    1.58 +    }
    1.59 +    void setPaintGamma(SkScalar pg) {
    1.60 +        SkASSERT(0 <= pg && pg < SkIntToScalar(4));
    1.61 +        fPaintGamma = SkScalarFloorToInt(pg * (1 << 6));
    1.62 +    }
    1.63 +
    1.64 +    SkScalar getContrast() const {
    1.65 +        return SkIntToScalar(fContrast) / ((1 << 8) - 1);
    1.66 +    }
    1.67 +    void setContrast(SkScalar c) {
    1.68 +        SkASSERT(0 <= c && c <= SK_Scalar1);
    1.69 +        fContrast = SkScalarRoundToInt(c * ((1 << 8) - 1));
    1.70 +    }
    1.71 +
    1.72 +    /**
    1.73 +     *  Causes the luminance color and contrast to be ignored, and the
    1.74 +     *  paint and device gamma to be effectively 1.0.
    1.75 +     */
    1.76 +    void ignorePreBlend() {
    1.77 +        setLuminanceColor(SK_ColorTRANSPARENT);
    1.78 +        setPaintGamma(SK_Scalar1);
    1.79 +        setDeviceGamma(SK_Scalar1);
    1.80 +        setContrast(0);
    1.81 +    }
    1.82 +
    1.83 +    uint8_t     fMaskFormat;
    1.84 +    uint8_t     fStrokeJoin;
    1.85 +    uint16_t    fFlags;
    1.86 +    // Warning: when adding members note that the size of this structure
    1.87 +    // must be a multiple of 4. SkDescriptor requires that its arguments be
    1.88 +    // multiples of four and this structure is put in an SkDescriptor in
    1.89 +    // SkPaint::MakeRec.
    1.90 +
    1.91 +    void    getMatrixFrom2x2(SkMatrix*) const;
    1.92 +    void    getLocalMatrix(SkMatrix*) const;
    1.93 +    void    getSingleMatrix(SkMatrix*) const;
    1.94 +
    1.95 +    inline SkPaint::Hinting getHinting() const;
    1.96 +    inline void setHinting(SkPaint::Hinting);
    1.97 +
    1.98 +    SkMask::Format getFormat() const {
    1.99 +        return static_cast<SkMask::Format>(fMaskFormat);
   1.100 +    }
   1.101 +
   1.102 +    SkColor getLuminanceColor() const {
   1.103 +        return fLumBits;
   1.104 +    }
   1.105 +
   1.106 +    void setLuminanceColor(SkColor c) {
   1.107 +        fLumBits = c;
   1.108 +    }
   1.109 +};
   1.110 +
   1.111 +//The following typedef hides from the rest of the implementation the number of
   1.112 +//most significant bits to consider when creating mask gamma tables. Two bits
   1.113 +//per channel was chosen as a balance between fidelity (more bits) and cache
   1.114 +//sizes (fewer bits). Three bits per channel was chosen when #303942; (used by
   1.115 +//the Chrome UI) turned out too green.
   1.116 +typedef SkTMaskGamma<3, 3, 3> SkMaskGamma;
   1.117 +
   1.118 +class SkScalerContext {
   1.119 +public:
   1.120 +    typedef SkScalerContextRec Rec;
   1.121 +
   1.122 +    enum Flags {
   1.123 +        kFrameAndFill_Flag        = 0x0001,
   1.124 +        kDevKernText_Flag         = 0x0002,
   1.125 +        kEmbeddedBitmapText_Flag  = 0x0004,
   1.126 +        kEmbolden_Flag            = 0x0008,
   1.127 +        kSubpixelPositioning_Flag = 0x0010,
   1.128 +        kForceAutohinting_Flag    = 0x0020,  // Use auto instead of bytcode hinting if hinting.
   1.129 +        kVertical_Flag            = 0x0040,
   1.130 +
   1.131 +        // together, these two flags resulting in a two bit value which matches
   1.132 +        // up with the SkPaint::Hinting enum.
   1.133 +        kHinting_Shift            = 7, // to shift into the other flags above
   1.134 +        kHintingBit1_Flag         = 0x0080,
   1.135 +        kHintingBit2_Flag         = 0x0100,
   1.136 +
   1.137 +        // Pixel geometry information.
   1.138 +        // only meaningful if fMaskFormat is LCD16 or LCD32
   1.139 +        kLCD_Vertical_Flag        = 0x0200,    // else Horizontal
   1.140 +        kLCD_BGROrder_Flag        = 0x0400,    // else RGB order
   1.141 +
   1.142 +        // Generate A8 from LCD source (for GDI and CoreGraphics).
   1.143 +        // only meaningful if fMaskFormat is kA8
   1.144 +        kGenA8FromLCD_Flag        = 0x0800, // could be 0x200 (bit meaning dependent on fMaskFormat)
   1.145 +    };
   1.146 +
   1.147 +    // computed values
   1.148 +    enum {
   1.149 +        kHinting_Mask   = kHintingBit1_Flag | kHintingBit2_Flag,
   1.150 +    };
   1.151 +
   1.152 +
   1.153 +    SkScalerContext(SkTypeface*, const SkDescriptor*);
   1.154 +    virtual ~SkScalerContext();
   1.155 +
   1.156 +    SkTypeface* getTypeface() const { return fTypeface.get(); }
   1.157 +
   1.158 +    SkMask::Format getMaskFormat() const {
   1.159 +        return (SkMask::Format)fRec.fMaskFormat;
   1.160 +    }
   1.161 +
   1.162 +    bool isSubpixel() const {
   1.163 +        return SkToBool(fRec.fFlags & kSubpixelPositioning_Flag);
   1.164 +    }
   1.165 +
   1.166 +    // remember our glyph offset/base
   1.167 +    void setBaseGlyphCount(unsigned baseGlyphCount) {
   1.168 +        fBaseGlyphCount = baseGlyphCount;
   1.169 +    }
   1.170 +
   1.171 +    /** Return the corresponding glyph for the specified unichar. Since contexts
   1.172 +        may be chained (under the hood), the glyphID that is returned may in
   1.173 +        fact correspond to a different font/context. In that case, we use the
   1.174 +        base-glyph-count to know how to translate back into local glyph space.
   1.175 +     */
   1.176 +    uint16_t charToGlyphID(SkUnichar uni);
   1.177 +
   1.178 +    /** Map the glyphID to its glyph index, and then to its char code. Unmapped
   1.179 +        glyphs return zero.
   1.180 +    */
   1.181 +    SkUnichar glyphIDToChar(uint16_t glyphID);
   1.182 +
   1.183 +    unsigned    getGlyphCount() { return this->generateGlyphCount(); }
   1.184 +    void        getAdvance(SkGlyph*);
   1.185 +    void        getMetrics(SkGlyph*);
   1.186 +    void        getImage(const SkGlyph&);
   1.187 +    void        getPath(const SkGlyph&, SkPath*);
   1.188 +    void        getFontMetrics(SkPaint::FontMetrics*);
   1.189 +
   1.190 +#ifdef SK_BUILD_FOR_ANDROID
   1.191 +    unsigned getBaseGlyphCount(SkUnichar charCode);
   1.192 +
   1.193 +    // This function must be public for SkTypeface_android.h, but should not be
   1.194 +    // called by other callers
   1.195 +    SkFontID findTypefaceIdForChar(SkUnichar uni);
   1.196 +#endif
   1.197 +
   1.198 +    static inline void MakeRec(const SkPaint&, const SkDeviceProperties* deviceProperties,
   1.199 +                               const SkMatrix*, Rec* rec);
   1.200 +    static inline void PostMakeRec(const SkPaint&, Rec*);
   1.201 +
   1.202 +    static SkMaskGamma::PreBlend GetMaskPreBlend(const Rec& rec);
   1.203 +
   1.204 +protected:
   1.205 +    Rec         fRec;
   1.206 +    unsigned    fBaseGlyphCount;
   1.207 +
   1.208 +    /** Generates the contents of glyph.fAdvanceX and glyph.fAdvanceY.
   1.209 +     *  May call getMetrics if that would be just as fast.
   1.210 +     */
   1.211 +    virtual void generateAdvance(SkGlyph* glyph) = 0;
   1.212 +
   1.213 +    /** Generates the contents of glyph.fWidth, fHeight, fTop, fLeft,
   1.214 +     *  as well as fAdvanceX and fAdvanceY if not already set.
   1.215 +     *
   1.216 +     *  TODO: fMaskFormat is set by getMetrics later; cannot be set here.
   1.217 +     */
   1.218 +    virtual void generateMetrics(SkGlyph* glyph) = 0;
   1.219 +
   1.220 +    /** Generates the contents of glyph.fImage.
   1.221 +     *  When called, glyph.fImage will be pointing to a pre-allocated,
   1.222 +     *  uninitialized region of memory of size glyph.computeImageSize().
   1.223 +     *  This method may change glyph.fMaskFormat if the new image size is
   1.224 +     *  less than or equal to the old image size.
   1.225 +     *
   1.226 +     *  Because glyph.computeImageSize() will determine the size of fImage,
   1.227 +     *  generateMetrics will be called before generateImage.
   1.228 +     */
   1.229 +    virtual void generateImage(const SkGlyph& glyph) = 0;
   1.230 +
   1.231 +    /** Sets the passed path to the glyph outline.
   1.232 +     *  If this cannot be done the path is set to empty;
   1.233 +     *  this is indistinguishable from a glyph with an empty path.
   1.234 +     *  This does not set glyph.fPath.
   1.235 +     *
   1.236 +     *  TODO: path is always glyph.fPath, no reason to pass separately.
   1.237 +     */
   1.238 +    virtual void generatePath(const SkGlyph& glyph, SkPath* path) = 0;
   1.239 +
   1.240 +    /** Retrieves font metrics.
   1.241 +     *  TODO: there is now a vertical bit, no need for two parameters.
   1.242 +     */
   1.243 +    virtual void generateFontMetrics(SkPaint::FontMetrics* mX,
   1.244 +                                     SkPaint::FontMetrics* mY) = 0;
   1.245 +
   1.246 +    /** Returns the number of glyphs in the font. */
   1.247 +    virtual unsigned generateGlyphCount() = 0;
   1.248 +
   1.249 +    /** Returns the glyph id for the given unichar.
   1.250 +     *  If there is no 1:1 mapping from the unichar to a glyph id, returns 0.
   1.251 +     */
   1.252 +    virtual uint16_t generateCharToGlyph(SkUnichar unichar) = 0;
   1.253 +
   1.254 +    /** Returns the unichar for the given glyph id.
   1.255 +     *  If there is no 1:1 mapping from the glyph id to a unichar, returns 0.
   1.256 +     *  The default implementation always returns 0, indicating failure.
   1.257 +     */
   1.258 +    virtual SkUnichar generateGlyphToChar(uint16_t glyphId);
   1.259 +
   1.260 +    void forceGenerateImageFromPath() { fGenerateImageFromPath = true; }
   1.261 +
   1.262 +private:
   1.263 +    // never null
   1.264 +    SkAutoTUnref<SkTypeface> fTypeface;
   1.265 +
   1.266 +#ifdef SK_BUILD_FOR_ANDROID
   1.267 +    SkPaintOptionsAndroid fPaintOptionsAndroid;
   1.268 +#endif
   1.269 +
   1.270 +    // optional object, which may be null
   1.271 +    SkPathEffect*   fPathEffect;
   1.272 +    SkMaskFilter*   fMaskFilter;
   1.273 +    SkRasterizer*   fRasterizer;
   1.274 +
   1.275 +    // if this is set, we draw the image from a path, rather than
   1.276 +    // calling generateImage.
   1.277 +    bool fGenerateImageFromPath;
   1.278 +
   1.279 +    void internalGetPath(const SkGlyph& glyph, SkPath* fillPath,
   1.280 +                         SkPath* devPath, SkMatrix* fillToDevMatrix);
   1.281 +
   1.282 +    // Return the context associated with the next logical typeface, or NULL if
   1.283 +    // there are no more entries in the fallback chain.
   1.284 +    SkScalerContext* allocNextContext() const;
   1.285 +
   1.286 +    // return the next context, treating fNextContext as a cache of the answer
   1.287 +    SkScalerContext* getNextContext();
   1.288 +
   1.289 +    // returns the right context from our link-list for this glyph. If no match
   1.290 +    // is found, just returns the original context (this)
   1.291 +    SkScalerContext* getGlyphContext(const SkGlyph& glyph);
   1.292 +
   1.293 +    // returns the right context from our link-list for this char. If no match
   1.294 +    // is found it returns NULL. If a match is found then the glyphID param is
   1.295 +    // set to the glyphID that maps to the provided char.
   1.296 +    SkScalerContext* getContextFromChar(SkUnichar uni, uint16_t* glyphID);
   1.297 +
   1.298 +    // link-list of context, to handle missing chars. null-terminated.
   1.299 +    SkScalerContext* fNextContext;
   1.300 +
   1.301 +    // SkMaskGamma::PreBlend converts linear masks to gamma correcting masks.
   1.302 +protected:
   1.303 +    // Visible to subclasses so that generateImage can apply the pre-blend directly.
   1.304 +    const SkMaskGamma::PreBlend fPreBlend;
   1.305 +private:
   1.306 +    // When there is a filter, previous steps must create a linear mask
   1.307 +    // and the pre-blend applied as a final step.
   1.308 +    const SkMaskGamma::PreBlend fPreBlendForFilter;
   1.309 +};
   1.310 +
   1.311 +#define kRec_SkDescriptorTag            SkSetFourByteTag('s', 'r', 'e', 'c')
   1.312 +#define kPathEffect_SkDescriptorTag     SkSetFourByteTag('p', 't', 'h', 'e')
   1.313 +#define kMaskFilter_SkDescriptorTag     SkSetFourByteTag('m', 's', 'k', 'f')
   1.314 +#define kRasterizer_SkDescriptorTag     SkSetFourByteTag('r', 'a', 's', 't')
   1.315 +#ifdef SK_BUILD_FOR_ANDROID
   1.316 +#define kAndroidOpts_SkDescriptorTag    SkSetFourByteTag('a', 'n', 'd', 'r')
   1.317 +#endif
   1.318 +
   1.319 +///////////////////////////////////////////////////////////////////////////////
   1.320 +
   1.321 +enum SkAxisAlignment {
   1.322 +    kNone_SkAxisAlignment,
   1.323 +    kX_SkAxisAlignment,
   1.324 +    kY_SkAxisAlignment
   1.325 +};
   1.326 +
   1.327 +/**
   1.328 + *  Return the axis (if any) that the baseline for horizontal text will land on
   1.329 + *  after running through the specified matrix.
   1.330 + *
   1.331 + *  As an example, the identity matrix will return kX_SkAxisAlignment
   1.332 + */
   1.333 +SkAxisAlignment SkComputeAxisAlignmentForHText(const SkMatrix& matrix);
   1.334 +
   1.335 +///////////////////////////////////////////////////////////////////////////////
   1.336 +
   1.337 +SkPaint::Hinting SkScalerContextRec::getHinting() const {
   1.338 +    unsigned hint = (fFlags & SkScalerContext::kHinting_Mask) >>
   1.339 +                                            SkScalerContext::kHinting_Shift;
   1.340 +    return static_cast<SkPaint::Hinting>(hint);
   1.341 +}
   1.342 +
   1.343 +void SkScalerContextRec::setHinting(SkPaint::Hinting hinting) {
   1.344 +    fFlags = (fFlags & ~SkScalerContext::kHinting_Mask) |
   1.345 +                                (hinting << SkScalerContext::kHinting_Shift);
   1.346 +}
   1.347 +
   1.348 +
   1.349 +#endif

mercurial