SkScalerContext.h revision 0da48618a758ef46c2174bdc1eaeb6dd8a693a2e
1/*
2 * Copyright 2006 The Android Open Source Project
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8#ifndef SkScalerContext_DEFINED
9#define SkScalerContext_DEFINED
10
11#include "SkMask.h"
12#include "SkMaskGamma.h"
13#include "SkMatrix.h"
14#include "SkPaint.h"
15#include "SkTypeface.h"
16
17struct SkGlyph;
18class SkDescriptor;
19class SkMaskFilter;
20class SkPathEffect;
21class SkRasterizer;
22
23/*
24 *  To allow this to be forward-declared, it must be its own typename, rather
25 *  than a nested struct inside SkScalerContext (where it started).
26 */
27struct SkScalerContextRec {
28    uint32_t    fOrigFontID;
29    uint32_t    fFontID;
30    SkScalar    fTextSize, fPreScaleX, fPreSkewX;
31    SkScalar    fPost2x2[2][2];
32    SkScalar    fFrameWidth, fMiterLimit;
33#ifdef SK_SUPPORT_HINTING_SCALE_FACTOR
34    SkScalar    fHintingScaleFactor;
35#endif
36
37    //These describe the parameters to create (uniquely identify) the pre-blend.
38    uint32_t    fLumBits;
39    uint8_t     fDeviceGamma; //2.6, (0.0, 4.0) gamma, 0.0 for sRGB
40    uint8_t     fPaintGamma;  //2.6, (0.0, 4.0) gamma, 0.0 for sRGB
41    uint8_t     fContrast;    //0.8+1, [0.0, 1.0] artificial contrast
42    uint8_t     fReservedAlign;
43
44    SkScalar getDeviceGamma() const {
45        return SkIntToScalar(fDeviceGamma) / (1 << 6);
46    }
47    void setDeviceGamma(SkScalar dg) {
48        SkASSERT(0 <= dg && dg < SkIntToScalar(4));
49        fDeviceGamma = SkScalarFloorToInt(dg * (1 << 6));
50    }
51
52    SkScalar getPaintGamma() const {
53        return SkIntToScalar(fPaintGamma) / (1 << 6);
54    }
55    void setPaintGamma(SkScalar pg) {
56        SkASSERT(0 <= pg && pg < SkIntToScalar(4));
57        fPaintGamma = SkScalarFloorToInt(pg * (1 << 6));
58    }
59
60    SkScalar getContrast() const {
61        return SkIntToScalar(fContrast) / ((1 << 8) - 1);
62    }
63    void setContrast(SkScalar c) {
64        SkASSERT(0 <= c && c <= SK_Scalar1);
65        fContrast = SkScalarRoundToInt(c * ((1 << 8) - 1));
66    }
67
68    /**
69     *  Causes the luminance color and contrast to be ignored, and the
70     *  paint and device gamma to be effectively 1.0.
71     */
72    void ignorePreBlend() {
73        setLuminanceColor(SK_ColorTRANSPARENT);
74        setPaintGamma(SK_Scalar1);
75        setDeviceGamma(SK_Scalar1);
76        setContrast(0);
77    }
78
79    uint8_t     fMaskFormat;
80    uint8_t     fStrokeJoin;
81    uint16_t    fFlags;
82    // Warning: when adding members note that the size of this structure
83    // must be a multiple of 4. SkDescriptor requires that its arguments be
84    // multiples of four and this structure is put in an SkDescriptor in
85    // SkPaint::MakeRec.
86
87    void    getMatrixFrom2x2(SkMatrix*) const;
88    void    getLocalMatrix(SkMatrix*) const;
89    void    getSingleMatrix(SkMatrix*) const;
90
91    inline SkPaint::Hinting getHinting() const;
92    inline void setHinting(SkPaint::Hinting);
93
94    SkMask::Format getFormat() const {
95        return static_cast<SkMask::Format>(fMaskFormat);
96    }
97
98    SkColor getLuminanceColor() const {
99        return fLumBits;
100    }
101
102    void setLuminanceColor(SkColor c) {
103        fLumBits = c;
104    }
105};
106
107//The following typedef hides from the rest of the implementation the number of
108//most significant bits to consider when creating mask gamma tables. Two bits
109//per channel was chosen as a balance between fidelity (more bits) and cache
110//sizes (fewer bits). Three bits per channel was chosen when #303942; (used by
111//the Chrome UI) turned out too green.
112typedef SkTMaskGamma<3, 3, 3> SkMaskGamma;
113
114class SkScalerContext {
115public:
116    typedef SkScalerContextRec Rec;
117
118    enum Flags {
119        kFrameAndFill_Flag        = 0x0001,
120        kDevKernText_Flag         = 0x0002,
121        kEmbeddedBitmapText_Flag  = 0x0004,
122        kEmbolden_Flag            = 0x0008,
123        kSubpixelPositioning_Flag = 0x0010,
124        kAutohinting_Flag         = 0x0020,
125        kVertical_Flag            = 0x0040,
126
127        // together, these two flags resulting in a two bit value which matches
128        // up with the SkPaint::Hinting enum.
129        kHinting_Shift            = 7, // to shift into the other flags above
130        kHintingBit1_Flag         = 0x0080,
131        kHintingBit2_Flag         = 0x0100,
132
133        // these should only ever be set if fMaskFormat is LCD16 or LCD32
134        kLCD_Vertical_Flag        = 0x0200,    // else Horizontal
135        kLCD_BGROrder_Flag        = 0x0400,    // else RGB order
136
137        // Generate A8 from LCD source (for GDI), only meaningful if fMaskFormat is kA8
138        // Perhaps we can store this (instead) in fMaskFormat, in hight bit?
139        kGenA8FromLCD_Flag        = 0x0800,
140    };
141
142    // computed values
143    enum {
144        kHinting_Mask   = kHintingBit1_Flag | kHintingBit2_Flag,
145    };
146
147
148    SkScalerContext(SkTypeface*, const SkDescriptor*);
149    virtual ~SkScalerContext();
150
151    SkTypeface* getTypeface() const { return fTypeface.get(); }
152
153    SkMask::Format getMaskFormat() const {
154        return (SkMask::Format)fRec.fMaskFormat;
155    }
156
157    bool isSubpixel() const {
158        return SkToBool(fRec.fFlags & kSubpixelPositioning_Flag);
159    }
160
161    // remember our glyph offset/base
162    void setBaseGlyphCount(unsigned baseGlyphCount) {
163        fBaseGlyphCount = baseGlyphCount;
164    }
165
166    /** Return the corresponding glyph for the specified unichar. Since contexts
167        may be chained (under the hood), the glyphID that is returned may in
168        fact correspond to a different font/context. In that case, we use the
169        base-glyph-count to know how to translate back into local glyph space.
170     */
171    uint16_t charToGlyphID(SkUnichar uni);
172
173    /** Map the glyphID to its glyph index, and then to its char code. Unmapped
174        glyphs return zero.
175    */
176    SkUnichar glyphIDToChar(uint16_t glyphID);
177
178    unsigned    getGlyphCount() { return this->generateGlyphCount(); }
179    void        getAdvance(SkGlyph*);
180    void        getMetrics(SkGlyph*);
181    void        getImage(const SkGlyph&);
182    void        getPath(const SkGlyph&, SkPath*);
183    void        getFontMetrics(SkPaint::FontMetrics* mX,
184                               SkPaint::FontMetrics* mY);
185
186#ifdef SK_BUILD_FOR_ANDROID
187    unsigned getBaseGlyphCount(SkUnichar charCode);
188
189    // This function must be public for SkTypeface_android.h, but should not be
190    // called by other callers
191    SkFontID findTypefaceIdForChar(SkUnichar uni);
192#endif
193
194    static inline void MakeRec(const SkPaint&, const SkDeviceProperties* deviceProperties,
195                               const SkMatrix*, Rec* rec);
196    static inline void PostMakeRec(const SkPaint&, Rec*);
197
198    static SkMaskGamma::PreBlend GetMaskPreBlend(const Rec& rec);
199
200protected:
201    Rec         fRec;
202    unsigned    fBaseGlyphCount;
203
204    virtual unsigned generateGlyphCount() = 0;
205    virtual uint16_t generateCharToGlyph(SkUnichar) = 0;
206    virtual void generateAdvance(SkGlyph*) = 0;
207    virtual void generateMetrics(SkGlyph*) = 0;
208    virtual void generateImage(const SkGlyph&) = 0;
209    virtual void generatePath(const SkGlyph&, SkPath*) = 0;
210    virtual void generateFontMetrics(SkPaint::FontMetrics* mX,
211                                     SkPaint::FontMetrics* mY) = 0;
212    // default impl returns 0, indicating failure.
213    virtual SkUnichar generateGlyphToChar(uint16_t);
214
215    void forceGenerateImageFromPath() { fGenerateImageFromPath = true; }
216
217private:
218    // never null
219    SkAutoTUnref<SkTypeface> fTypeface;
220
221    // optional object, which may be null
222    SkPathEffect*   fPathEffect;
223    SkMaskFilter*   fMaskFilter;
224    SkRasterizer*   fRasterizer;
225
226    // if this is set, we draw the image from a path, rather than
227    // calling generateImage.
228    bool fGenerateImageFromPath;
229
230    void internalGetPath(const SkGlyph& glyph, SkPath* fillPath,
231                         SkPath* devPath, SkMatrix* fillToDevMatrix);
232
233    // return the next context, treating fNextContext as a cache of the answer
234    SkScalerContext* getNextContext();
235
236    // returns the right context from our link-list for this glyph. If no match
237    // is found, just returns the original context (this)
238    SkScalerContext* getGlyphContext(const SkGlyph& glyph);
239
240    // returns the right context from our link-list for this char. If no match
241    // is found it returns NULL. If a match is found then the glyphID param is
242    // set to the glyphID that maps to the provided char.
243    SkScalerContext* getContextFromChar(SkUnichar uni, uint16_t* glyphID);
244
245    // link-list of context, to handle missing chars. null-terminated.
246    SkScalerContext* fNextContext;
247
248    // SkMaskGamma::PreBlend converts linear masks to gamma correcting masks.
249protected:
250    // Visible to subclasses so that generateImage can apply the pre-blend directly.
251    const SkMaskGamma::PreBlend fPreBlend;
252private:
253    // When there is a filter, previous steps must create a linear mask
254    // and the pre-blend applied as a final step.
255    const SkMaskGamma::PreBlend fPreBlendForFilter;
256};
257
258#define kRec_SkDescriptorTag            SkSetFourByteTag('s', 'r', 'e', 'c')
259#define kPathEffect_SkDescriptorTag     SkSetFourByteTag('p', 't', 'h', 'e')
260#define kMaskFilter_SkDescriptorTag     SkSetFourByteTag('m', 's', 'k', 'f')
261#define kRasterizer_SkDescriptorTag     SkSetFourByteTag('r', 'a', 's', 't')
262
263///////////////////////////////////////////////////////////////////////////////
264
265enum SkAxisAlignment {
266    kNone_SkAxisAlignment,
267    kX_SkAxisAlignment,
268    kY_SkAxisAlignment
269};
270
271/**
272 *  Return the axis (if any) that the baseline for horizontal text will land on
273 *  after running through the specified matrix.
274 *
275 *  As an example, the identity matrix will return kX_SkAxisAlignment
276 */
277SkAxisAlignment SkComputeAxisAlignmentForHText(const SkMatrix& matrix);
278
279///////////////////////////////////////////////////////////////////////////////
280
281SkPaint::Hinting SkScalerContextRec::getHinting() const {
282    unsigned hint = (fFlags & SkScalerContext::kHinting_Mask) >>
283                                            SkScalerContext::kHinting_Shift;
284    return static_cast<SkPaint::Hinting>(hint);
285}
286
287void SkScalerContextRec::setHinting(SkPaint::Hinting hinting) {
288    fFlags = (fFlags & ~SkScalerContext::kHinting_Mask) |
289                                (hinting << SkScalerContext::kHinting_Shift);
290}
291
292
293#endif
294