SkScalerContext.h revision bfe1572f2484cb928a17b4c45ed2d094e3e57e17
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). Three bits per channel was chosen when #303942; (used by 112//the Chrome UI) turned out too green. 113typedef SkTMaskGamma<3, 3, 3> SkMaskGamma; 114 115class SkScalerContext { 116public: 117 typedef SkScalerContextRec Rec; 118 119 enum Flags { 120 kFrameAndFill_Flag = 0x0001, 121 kDevKernText_Flag = 0x0002, 122 kEmbeddedBitmapText_Flag = 0x0004, 123 kEmbolden_Flag = 0x0008, 124 kSubpixelPositioning_Flag = 0x0010, 125 kAutohinting_Flag = 0x0020, 126 kVertical_Flag = 0x0040, 127 128 // together, these two flags resulting in a two bit value which matches 129 // up with the SkPaint::Hinting enum. 130 kHinting_Shift = 7, // to shift into the other flags above 131 kHintingBit1_Flag = 0x0080, 132 kHintingBit2_Flag = 0x0100, 133 134 // these should only ever be set if fMaskFormat is LCD16 or LCD32 135 kLCD_Vertical_Flag = 0x0200, // else Horizontal 136 kLCD_BGROrder_Flag = 0x0400, // else RGB order 137 138 // Generate A8 from LCD source (for GDI), only meaningful if fMaskFormat is kA8 139 // Perhaps we can store this (instead) in fMaskFormat, in hight bit? 140 kGenA8FromLCD_Flag = 0x0800, 141 }; 142 143 // computed values 144 enum { 145 kHinting_Mask = kHintingBit1_Flag | kHintingBit2_Flag, 146 }; 147 148 149 SkScalerContext(const SkDescriptor* desc); 150 virtual ~SkScalerContext(); 151 152 SkMask::Format getMaskFormat() const { 153 return (SkMask::Format)fRec.fMaskFormat; 154 } 155 156 bool isSubpixel() const { 157 return SkToBool(fRec.fFlags & kSubpixelPositioning_Flag); 158 } 159 160 // remember our glyph offset/base 161 void setBaseGlyphCount(unsigned baseGlyphCount) { 162 fBaseGlyphCount = baseGlyphCount; 163 } 164 165 /** Return the corresponding glyph for the specified unichar. Since contexts 166 may be chained (under the hood), the glyphID that is returned may in 167 fact correspond to a different font/context. In that case, we use the 168 base-glyph-count to know how to translate back into local glyph space. 169 */ 170 uint16_t charToGlyphID(SkUnichar uni); 171 172 /** Map the glyphID to its glyph index, and then to its char code. Unmapped 173 glyphs return zero. 174 */ 175 SkUnichar glyphIDToChar(uint16_t glyphID); 176 177 unsigned getGlyphCount() { return this->generateGlyphCount(); } 178 void getAdvance(SkGlyph*); 179 void getMetrics(SkGlyph*); 180 void getImage(const SkGlyph&); 181 void getPath(const SkGlyph&, SkPath*); 182 void getFontMetrics(SkPaint::FontMetrics* mX, 183 SkPaint::FontMetrics* mY); 184 185#ifdef SK_BUILD_FOR_ANDROID 186 unsigned getBaseGlyphCount(SkUnichar charCode); 187 188 // This function must be public for SkTypeface_android.h, but should not be 189 // called by other callers 190 SkFontID findTypefaceIdForChar(SkUnichar uni) { 191 SkScalerContext* ctx = this; 192 while (NULL != ctx) { 193 if (ctx->generateCharToGlyph(uni)) { 194 return ctx->fRec.fFontID; 195 } 196 ctx = ctx->getNextContext(); 197 } 198 return 0; 199 } 200#endif 201 202 static inline void MakeRec(const SkPaint&, const SkMatrix*, Rec* rec); 203 static inline void PostMakeRec(const SkPaint&, Rec*); 204 205 static SkScalerContext* Create(const SkDescriptor*); 206 static SkMaskGamma::PreBlend GetMaskPreBlend(const Rec& rec); 207 208protected: 209 Rec fRec; 210 unsigned fBaseGlyphCount; 211 212 virtual unsigned generateGlyphCount() = 0; 213 virtual uint16_t generateCharToGlyph(SkUnichar) = 0; 214 virtual void generateAdvance(SkGlyph*) = 0; 215 virtual void generateMetrics(SkGlyph*) = 0; 216 virtual void generateImage(const SkGlyph&, SkMaskGamma::PreBlend* maskPreBlend) = 0; 217 virtual void generatePath(const SkGlyph&, SkPath*) = 0; 218 virtual void generateFontMetrics(SkPaint::FontMetrics* mX, 219 SkPaint::FontMetrics* mY) = 0; 220 // default impl returns 0, indicating failure. 221 virtual SkUnichar generateGlyphToChar(uint16_t); 222 223 void forceGenerateImageFromPath() { fGenerateImageFromPath = true; } 224 225private: 226 SkPathEffect* fPathEffect; 227 SkMaskFilter* fMaskFilter; 228 SkRasterizer* fRasterizer; 229 230 // if this is set, we draw the image from a path, rather than 231 // calling generateImage. 232 bool fGenerateImageFromPath; 233 234 void internalGetPath(const SkGlyph& glyph, SkPath* fillPath, 235 SkPath* devPath, SkMatrix* fillToDevMatrix); 236 237 // return the next context, treating fNextContext as a cache of the answer 238 SkScalerContext* getNextContext(); 239 240 // returns the right context from our link-list for this glyph. If no match 241 // is found, just returns the original context (this) 242 SkScalerContext* getGlyphContext(const SkGlyph& glyph); 243 244 // link-list of context, to handle missing chars. null-terminated. 245 SkScalerContext* fNextContext; 246 247 // converts linear masks to gamma correcting masks. 248 SkMaskGamma::PreBlend fMaskPreBlend; 249}; 250 251#define kRec_SkDescriptorTag SkSetFourByteTag('s', 'r', 'e', 'c') 252#define kPathEffect_SkDescriptorTag SkSetFourByteTag('p', 't', 'h', 'e') 253#define kMaskFilter_SkDescriptorTag SkSetFourByteTag('m', 's', 'k', 'f') 254#define kRasterizer_SkDescriptorTag SkSetFourByteTag('r', 'a', 's', 't') 255 256/////////////////////////////////////////////////////////////////////////////// 257 258enum SkAxisAlignment { 259 kNone_SkAxisAlignment, 260 kX_SkAxisAlignment, 261 kY_SkAxisAlignment 262}; 263 264/** 265 * Return the axis (if any) that the baseline for horizontal text will land on 266 * after running through the specified matrix. 267 * 268 * As an example, the identity matrix will return kX_SkAxisAlignment 269 */ 270SkAxisAlignment SkComputeAxisAlignmentForHText(const SkMatrix& matrix); 271 272/////////////////////////////////////////////////////////////////////////////// 273 274SkPaint::Hinting SkScalerContextRec::getHinting() const { 275 unsigned hint = (fFlags & SkScalerContext::kHinting_Mask) >> 276 SkScalerContext::kHinting_Shift; 277 return static_cast<SkPaint::Hinting>(hint); 278} 279 280void SkScalerContextRec::setHinting(SkPaint::Hinting hinting) { 281 fFlags = (fFlags & ~SkScalerContext::kHinting_Mask) | 282 (hinting << SkScalerContext::kHinting_Shift); 283} 284 285 286#endif 287 288