SkScalerContext.h revision c7a20e40938ae031368e3a9e4df33ce90a765aba
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 17#ifdef SK_BUILD_FOR_ANDROID 18 #include "SkPaintOptionsAndroid.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(SkTypeface*, const SkDescriptor*); 153 virtual ~SkScalerContext(); 154 155 SkTypeface* getTypeface() const { return fTypeface.get(); } 156 157 SkMask::Format getMaskFormat() const { 158 return (SkMask::Format)fRec.fMaskFormat; 159 } 160 161 bool isSubpixel() const { 162 return SkToBool(fRec.fFlags & kSubpixelPositioning_Flag); 163 } 164 165 // remember our glyph offset/base 166 void setBaseGlyphCount(unsigned baseGlyphCount) { 167 fBaseGlyphCount = baseGlyphCount; 168 } 169 170 /** Return the corresponding glyph for the specified unichar. Since contexts 171 may be chained (under the hood), the glyphID that is returned may in 172 fact correspond to a different font/context. In that case, we use the 173 base-glyph-count to know how to translate back into local glyph space. 174 */ 175 uint16_t charToGlyphID(SkUnichar uni); 176 177 /** Map the glyphID to its glyph index, and then to its char code. Unmapped 178 glyphs return zero. 179 */ 180 SkUnichar glyphIDToChar(uint16_t glyphID); 181 182 unsigned getGlyphCount() { return this->generateGlyphCount(); } 183 void getAdvance(SkGlyph*); 184 void getMetrics(SkGlyph*); 185 void getImage(const SkGlyph&); 186 void getPath(const SkGlyph&, SkPath*); 187 void getFontMetrics(SkPaint::FontMetrics*); 188 189#ifdef SK_BUILD_FOR_ANDROID 190 unsigned getBaseGlyphCount(SkUnichar charCode); 191 192 // This function must be public for SkTypeface_android.h, but should not be 193 // called by other callers 194 SkFontID findTypefaceIdForChar(SkUnichar uni); 195#endif 196 197 static inline void MakeRec(const SkPaint&, const SkDeviceProperties* deviceProperties, 198 const SkMatrix*, Rec* rec); 199 static inline void PostMakeRec(const SkPaint&, Rec*); 200 201 static SkMaskGamma::PreBlend GetMaskPreBlend(const Rec& rec); 202 203protected: 204 Rec fRec; 205 unsigned fBaseGlyphCount; 206 207 virtual unsigned generateGlyphCount() = 0; 208 virtual uint16_t generateCharToGlyph(SkUnichar) = 0; 209 virtual void generateAdvance(SkGlyph*) = 0; 210 virtual void generateMetrics(SkGlyph*) = 0; 211 virtual void generateImage(const SkGlyph&) = 0; 212 virtual void generatePath(const SkGlyph&, SkPath*) = 0; 213 virtual void generateFontMetrics(SkPaint::FontMetrics* mX, 214 SkPaint::FontMetrics* mY) = 0; 215 // default impl returns 0, indicating failure. 216 virtual SkUnichar generateGlyphToChar(uint16_t); 217 218 void forceGenerateImageFromPath() { fGenerateImageFromPath = true; } 219 220private: 221 // never null 222 SkAutoTUnref<SkTypeface> fTypeface; 223 224#ifdef SK_BUILD_FOR_ANDROID 225 SkPaintOptionsAndroid fPaintOptionsAndroid; 226#endif 227 228 // optional object, which may be null 229 SkPathEffect* fPathEffect; 230 SkMaskFilter* fMaskFilter; 231 SkRasterizer* fRasterizer; 232 233 // if this is set, we draw the image from a path, rather than 234 // calling generateImage. 235 bool fGenerateImageFromPath; 236 237 void internalGetPath(const SkGlyph& glyph, SkPath* fillPath, 238 SkPath* devPath, SkMatrix* fillToDevMatrix); 239 240 // Return the context associated with the next logical typeface, or NULL if 241 // there are no more entries in the fallback chain. 242 SkScalerContext* allocNextContext() const; 243 244 // return the next context, treating fNextContext as a cache of the answer 245 SkScalerContext* getNextContext(); 246 247 // returns the right context from our link-list for this glyph. If no match 248 // is found, just returns the original context (this) 249 SkScalerContext* getGlyphContext(const SkGlyph& glyph); 250 251 // returns the right context from our link-list for this char. If no match 252 // is found it returns NULL. If a match is found then the glyphID param is 253 // set to the glyphID that maps to the provided char. 254 SkScalerContext* getContextFromChar(SkUnichar uni, uint16_t* glyphID); 255 256 // link-list of context, to handle missing chars. null-terminated. 257 SkScalerContext* fNextContext; 258 259 // SkMaskGamma::PreBlend converts linear masks to gamma correcting masks. 260protected: 261 // Visible to subclasses so that generateImage can apply the pre-blend directly. 262 const SkMaskGamma::PreBlend fPreBlend; 263private: 264 // When there is a filter, previous steps must create a linear mask 265 // and the pre-blend applied as a final step. 266 const SkMaskGamma::PreBlend fPreBlendForFilter; 267}; 268 269#define kRec_SkDescriptorTag SkSetFourByteTag('s', 'r', 'e', 'c') 270#define kPathEffect_SkDescriptorTag SkSetFourByteTag('p', 't', 'h', 'e') 271#define kMaskFilter_SkDescriptorTag SkSetFourByteTag('m', 's', 'k', 'f') 272#define kRasterizer_SkDescriptorTag SkSetFourByteTag('r', 'a', 's', 't') 273#ifdef SK_BUILD_FOR_ANDROID 274#define kAndroidOpts_SkDescriptorTag SkSetFourByteTag('a', 'n', 'd', 'r') 275#endif 276 277/////////////////////////////////////////////////////////////////////////////// 278 279enum SkAxisAlignment { 280 kNone_SkAxisAlignment, 281 kX_SkAxisAlignment, 282 kY_SkAxisAlignment 283}; 284 285/** 286 * Return the axis (if any) that the baseline for horizontal text will land on 287 * after running through the specified matrix. 288 * 289 * As an example, the identity matrix will return kX_SkAxisAlignment 290 */ 291SkAxisAlignment SkComputeAxisAlignmentForHText(const SkMatrix& matrix); 292 293/////////////////////////////////////////////////////////////////////////////// 294 295SkPaint::Hinting SkScalerContextRec::getHinting() const { 296 unsigned hint = (fFlags & SkScalerContext::kHinting_Mask) >> 297 SkScalerContext::kHinting_Shift; 298 return static_cast<SkPaint::Hinting>(hint); 299} 300 301void SkScalerContextRec::setHinting(SkPaint::Hinting hinting) { 302 fFlags = (fFlags & ~SkScalerContext::kHinting_Mask) | 303 (hinting << SkScalerContext::kHinting_Shift); 304} 305 306 307#endif 308