SkScalerContext.h revision c0f1dfb4ed6133dec29b30ef4c5da68008f2d34f
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
38    //These describe the parameters to create (uniquely identify) the pre-blend.
39    uint32_t    fLumBits;
40    uint8_t     fDeviceGamma; //2.6, (0.0, 4.0) gamma, 0.0 for sRGB
41    uint8_t     fPaintGamma;  //2.6, (0.0, 4.0) gamma, 0.0 for sRGB
42    uint8_t     fContrast;    //0.8+1, [0.0, 1.0] artificial contrast
43    uint8_t     fReservedAlign;
44
45    SkScalar getDeviceGamma() const {
46        return SkIntToScalar(fDeviceGamma) / (1 << 6);
47    }
48    void setDeviceGamma(SkScalar dg) {
49        SkASSERT(0 <= dg && dg < SkIntToScalar(4));
50        fDeviceGamma = SkScalarFloorToInt(dg * (1 << 6));
51    }
52
53    SkScalar getPaintGamma() const {
54        return SkIntToScalar(fPaintGamma) / (1 << 6);
55    }
56    void setPaintGamma(SkScalar pg) {
57        SkASSERT(0 <= pg && pg < SkIntToScalar(4));
58        fPaintGamma = SkScalarFloorToInt(pg * (1 << 6));
59    }
60
61    SkScalar getContrast() const {
62        return SkIntToScalar(fContrast) / ((1 << 8) - 1);
63    }
64    void setContrast(SkScalar c) {
65        SkASSERT(0 <= c && c <= SK_Scalar1);
66        fContrast = SkScalarRoundToInt(c * ((1 << 8) - 1));
67    }
68
69    /**
70     *  Causes the luminance color and contrast to be ignored, and the
71     *  paint and device gamma to be effectively 1.0.
72     */
73    void ignorePreBlend() {
74        setLuminanceColor(0x00000000);
75        setPaintGamma(SK_Scalar1);
76        setDeviceGamma(SK_Scalar1);
77        setContrast(0);
78    }
79
80    uint8_t     fMaskFormat;
81    uint8_t     fStrokeJoin;
82    uint16_t    fFlags;
83    // Warning: when adding members note that the size of this structure
84    // must be a multiple of 4. SkDescriptor requires that its arguments be
85    // multiples of four and this structure is put in an SkDescriptor in
86    // SkPaint::MakeRec.
87
88    void    getMatrixFrom2x2(SkMatrix*) const;
89    void    getLocalMatrix(SkMatrix*) const;
90    void    getSingleMatrix(SkMatrix*) const;
91
92    inline SkPaint::Hinting getHinting() const;
93    inline void setHinting(SkPaint::Hinting);
94
95    SkMask::Format getFormat() const {
96        return static_cast<SkMask::Format>(fMaskFormat);
97    }
98
99    SkColor getLuminanceColor() const {
100        return fLumBits;
101    }
102
103    void setLuminanceColor(SkColor c) {
104        fLumBits = c;
105    }
106};
107
108//The following typedef hides from the rest of the implementation the number of
109//most significant bits to consider when creating mask gamma tables. Two bits
110//per channel was chosen as a balance between fidelity (more bits) and cache
111//sizes (fewer bits).
112typedef SkTMaskGamma<2, 2, 2> 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(const SkDescriptor* desc);
149    virtual ~SkScalerContext();
150
151    SkMask::Format getMaskFormat() const {
152        return (SkMask::Format)fRec.fMaskFormat;
153    }
154
155    bool isSubpixel() const {
156        return SkToBool(fRec.fFlags & kSubpixelPositioning_Flag);
157    }
158
159    // remember our glyph offset/base
160    void setBaseGlyphCount(unsigned baseGlyphCount) {
161        fBaseGlyphCount = baseGlyphCount;
162    }
163
164    /** Return the corresponding glyph for the specified unichar. Since contexts
165        may be chained (under the hood), the glyphID that is returned may in
166        fact correspond to a different font/context. In that case, we use the
167        base-glyph-count to know how to translate back into local glyph space.
168     */
169    uint16_t charToGlyphID(SkUnichar uni);
170
171    /** Map the glyphID to its glyph index, and then to its char code. Unmapped
172        glyphs return zero.
173    */
174    SkUnichar glyphIDToChar(uint16_t glyphID);
175
176    unsigned    getGlyphCount() { return this->generateGlyphCount(); }
177    void        getAdvance(SkGlyph*);
178    void        getMetrics(SkGlyph*);
179    void        getImage(const SkGlyph&);
180    void        getPath(const SkGlyph&, SkPath*);
181    void        getFontMetrics(SkPaint::FontMetrics* mX,
182                               SkPaint::FontMetrics* mY);
183
184#ifdef SK_BUILD_FOR_ANDROID
185    unsigned getBaseGlyphCount(SkUnichar charCode);
186
187    // This function must be public for SkTypeface_android.h, but should not be
188    // called by other callers
189    SkFontID findTypefaceIdForChar(SkUnichar uni) {
190        SkScalerContext* ctx = this;
191        while (NULL != ctx) {
192            if (ctx->generateCharToGlyph(uni)) {
193                return ctx->fRec.fFontID;
194            }
195            ctx = ctx->getNextContext();
196        }
197        return 0;
198    }
199#endif
200
201    static inline void MakeRec(const SkPaint&, const SkMatrix*, Rec* rec);
202    static inline void PostMakeRec(const SkPaint&, Rec*);
203
204    static SkScalerContext* Create(const SkDescriptor*);
205    static SkMaskGamma::PreBlend GetMaskPreBlend(const Rec& rec);
206
207protected:
208    Rec         fRec;
209    unsigned    fBaseGlyphCount;
210
211    virtual unsigned generateGlyphCount() = 0;
212    virtual uint16_t generateCharToGlyph(SkUnichar) = 0;
213    virtual void generateAdvance(SkGlyph*) = 0;
214    virtual void generateMetrics(SkGlyph*) = 0;
215    virtual void generateImage(const SkGlyph&, SkMaskGamma::PreBlend* maskPreBlend) = 0;
216    virtual void generatePath(const SkGlyph&, SkPath*) = 0;
217    virtual void generateFontMetrics(SkPaint::FontMetrics* mX,
218                                     SkPaint::FontMetrics* mY) = 0;
219    // default impl returns 0, indicating failure.
220    virtual SkUnichar generateGlyphToChar(uint16_t);
221
222    void forceGenerateImageFromPath() { fGenerateImageFromPath = true; }
223
224private:
225    SkPathEffect*   fPathEffect;
226    SkMaskFilter*   fMaskFilter;
227    SkRasterizer*   fRasterizer;
228
229    // if this is set, we draw the image from a path, rather than
230    // calling generateImage.
231    bool fGenerateImageFromPath;
232
233    void internalGetPath(const SkGlyph& glyph, SkPath* fillPath,
234                         SkPath* devPath, SkMatrix* fillToDevMatrix);
235
236    // return the next context, treating fNextContext as a cache of the answer
237    SkScalerContext* getNextContext();
238
239    // returns the right context from our link-list for this glyph. If no match
240    // is found, just returns the original context (this)
241    SkScalerContext* getGlyphContext(const SkGlyph& glyph);
242
243    // link-list of context, to handle missing chars. null-terminated.
244    SkScalerContext* fNextContext;
245
246    // converts linear masks to gamma correcting masks.
247    SkMaskGamma::PreBlend fMaskPreBlend;
248};
249
250#define kRec_SkDescriptorTag            SkSetFourByteTag('s', 'r', 'e', 'c')
251#define kPathEffect_SkDescriptorTag     SkSetFourByteTag('p', 't', 'h', 'e')
252#define kMaskFilter_SkDescriptorTag     SkSetFourByteTag('m', 's', 'k', 'f')
253#define kRasterizer_SkDescriptorTag     SkSetFourByteTag('r', 'a', 's', 't')
254
255///////////////////////////////////////////////////////////////////////////////
256
257enum SkAxisAlignment {
258    kNone_SkAxisAlignment,
259    kX_SkAxisAlignment,
260    kY_SkAxisAlignment
261};
262
263/**
264 *  Return the axis (if any) that the baseline for horizontal text will land on
265 *  after running through the specified matrix.
266 *
267 *  As an example, the identity matrix will return kX_SkAxisAlignment
268 */
269SkAxisAlignment SkComputeAxisAlignmentForHText(const SkMatrix& matrix);
270
271///////////////////////////////////////////////////////////////////////////////
272
273SkPaint::Hinting SkScalerContextRec::getHinting() const {
274    unsigned hint = (fFlags & SkScalerContext::kHinting_Mask) >>
275                                            SkScalerContext::kHinting_Shift;
276    return static_cast<SkPaint::Hinting>(hint);
277}
278
279void SkScalerContextRec::setHinting(SkPaint::Hinting hinting) {
280    fFlags = (fFlags & ~SkScalerContext::kHinting_Mask) |
281                                (hinting << SkScalerContext::kHinting_Shift);
282}
283
284
285#endif
286
287