SkScalerContext.h revision 1f8787a99f15e37c40a1260c9c456ed91f11ba15
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(0x00000000); 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 SkScalerContext* ctx = this; 195 while (NULL != ctx) { 196 if (ctx->generateCharToGlyph(uni)) { 197 return ctx->fRec.fFontID; 198 } 199 ctx = ctx->getNextContext(); 200 } 201 return 0; 202 } 203#endif 204 205 static inline void MakeRec(const SkPaint&, const SkMatrix*, Rec* rec); 206 static inline void PostMakeRec(const SkPaint&, Rec*); 207 208 static SkScalerContext* Create(const SkDescriptor*); 209 static SkMaskGamma::PreBlend GetMaskPreBlend(const Rec& rec); 210 211protected: 212 Rec fRec; 213 unsigned fBaseGlyphCount; 214 215 virtual unsigned generateGlyphCount() = 0; 216 virtual uint16_t generateCharToGlyph(SkUnichar) = 0; 217 virtual void generateAdvance(SkGlyph*) = 0; 218 virtual void generateMetrics(SkGlyph*) = 0; 219 virtual void generateImage(const SkGlyph&, SkMaskGamma::PreBlend* maskPreBlend) = 0; 220 virtual void generatePath(const SkGlyph&, SkPath*) = 0; 221 virtual void generateFontMetrics(SkPaint::FontMetrics* mX, 222 SkPaint::FontMetrics* mY) = 0; 223 // default impl returns 0, indicating failure. 224 virtual SkUnichar generateGlyphToChar(uint16_t); 225 226 void forceGenerateImageFromPath() { fGenerateImageFromPath = true; } 227 228private: 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 next context, treating fNextContext as a cache of the answer 241 SkScalerContext* getNextContext(); 242 243 // returns the right context from our link-list for this glyph. If no match 244 // is found, just returns the original context (this) 245 SkScalerContext* getGlyphContext(const SkGlyph& glyph); 246 247 // link-list of context, to handle missing chars. null-terminated. 248 SkScalerContext* fNextContext; 249 250 // converts linear masks to gamma correcting masks. 251 SkMaskGamma::PreBlend fMaskPreBlend; 252}; 253 254#define kRec_SkDescriptorTag SkSetFourByteTag('s', 'r', 'e', 'c') 255#define kPathEffect_SkDescriptorTag SkSetFourByteTag('p', 't', 'h', 'e') 256#define kMaskFilter_SkDescriptorTag SkSetFourByteTag('m', 's', 'k', 'f') 257#define kRasterizer_SkDescriptorTag SkSetFourByteTag('r', 'a', 's', 't') 258 259/////////////////////////////////////////////////////////////////////////////// 260 261enum SkAxisAlignment { 262 kNone_SkAxisAlignment, 263 kX_SkAxisAlignment, 264 kY_SkAxisAlignment 265}; 266 267/** 268 * Return the axis (if any) that the baseline for horizontal text will land on 269 * after running through the specified matrix. 270 * 271 * As an example, the identity matrix will return kX_SkAxisAlignment 272 */ 273SkAxisAlignment SkComputeAxisAlignmentForHText(const SkMatrix& matrix); 274 275/////////////////////////////////////////////////////////////////////////////// 276 277SkPaint::Hinting SkScalerContextRec::getHinting() const { 278 unsigned hint = (fFlags & SkScalerContext::kHinting_Mask) >> 279 SkScalerContext::kHinting_Shift; 280 return static_cast<SkPaint::Hinting>(hint); 281} 282 283void SkScalerContextRec::setHinting(SkPaint::Hinting hinting) { 284 fFlags = (fFlags & ~SkScalerContext::kHinting_Mask) | 285 (hinting << SkScalerContext::kHinting_Shift); 286} 287 288 289#endif 290 291