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