SkScalerContext.h revision da7a944e293d27ec5c7be06b224921ae0058d35a
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 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(SK_ColorTRANSPARENT); 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 kForceAutohinting_Flag = 0x0020, // Use auto instead of bytcode hinting if hinting. 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 // Pixel geometry information. 135 // only meaningful if fMaskFormat is LCD16 or LCD32 136 kLCD_Vertical_Flag = 0x0200, // else Horizontal 137 kLCD_BGROrder_Flag = 0x0400, // else RGB order 138 139 // Generate A8 from LCD source (for GDI and CoreGraphics). 140 // only meaningful if fMaskFormat is kA8 141 kGenA8FromLCD_Flag = 0x0800, // could be 0x200 (bit meaning dependent on fMaskFormat) 142 }; 143 144 // computed values 145 enum { 146 kHinting_Mask = kHintingBit1_Flag | kHintingBit2_Flag, 147 }; 148 149 150 SkScalerContext(SkTypeface*, const SkDescriptor*); 151 virtual ~SkScalerContext(); 152 153 SkTypeface* getTypeface() const { return fTypeface.get(); } 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 bool isVertical() const { 164 return SkToBool(fRec.fFlags & kVertical_Flag); 165 } 166 167 /** Return the corresponding glyph for the specified unichar. Since contexts 168 may be chained (under the hood), the glyphID that is returned may in 169 fact correspond to a different font/context. In that case, we use the 170 base-glyph-count to know how to translate back into local glyph space. 171 */ 172 uint16_t charToGlyphID(SkUnichar uni) { 173 return generateCharToGlyph(uni); 174 } 175 176 /** Map the glyphID to its glyph index, and then to its char code. Unmapped 177 glyphs return zero. 178 */ 179 SkUnichar glyphIDToChar(uint16_t glyphID) { 180 return (glyphID < getGlyphCount()) ? generateGlyphToChar(glyphID) : 0; 181 } 182 183 unsigned getGlyphCount() { return this->generateGlyphCount(); } 184 void getAdvance(SkGlyph*); 185 void getMetrics(SkGlyph*); 186 void getImage(const SkGlyph&); 187 void getPath(const SkGlyph&, SkPath*); 188 void getFontMetrics(SkPaint::FontMetrics*); 189 190 /** Return the size in bytes of the associated gamma lookup table 191 */ 192 static size_t GetGammaLUTSize(SkScalar contrast, SkScalar paintGamma, SkScalar deviceGamma, 193 int* width, int* height); 194 195 /** Get the associated gamma lookup table. The 'data' pointer must point to pre-allocated 196 memory, with size in bytes greater than or equal to the return value of getGammaLUTSize(). 197 */ 198 static void GetGammaLUTData(SkScalar contrast, SkScalar paintGamma, SkScalar deviceGamma, 199 void* data); 200 201 static void MakeRec(const SkPaint&, const SkDeviceProperties* deviceProperties, 202 const SkMatrix*, Rec* rec); 203 static inline void PostMakeRec(const SkPaint&, Rec*); 204 205 static SkMaskGamma::PreBlend GetMaskPreBlend(const Rec& rec); 206 207protected: 208 Rec fRec; 209 210 /** Generates the contents of glyph.fAdvanceX and glyph.fAdvanceY. 211 * May call getMetrics if that would be just as fast. 212 */ 213 virtual void generateAdvance(SkGlyph* glyph) = 0; 214 215 /** Generates the contents of glyph.fWidth, fHeight, fTop, fLeft, 216 * as well as fAdvanceX and fAdvanceY if not already set. 217 * 218 * TODO: fMaskFormat is set by getMetrics later; cannot be set here. 219 */ 220 virtual void generateMetrics(SkGlyph* glyph) = 0; 221 222 /** Generates the contents of glyph.fImage. 223 * When called, glyph.fImage will be pointing to a pre-allocated, 224 * uninitialized region of memory of size glyph.computeImageSize(). 225 * This method may change glyph.fMaskFormat if the new image size is 226 * less than or equal to the old image size. 227 * 228 * Because glyph.computeImageSize() will determine the size of fImage, 229 * generateMetrics will be called before generateImage. 230 */ 231 virtual void generateImage(const SkGlyph& glyph) = 0; 232 233 /** Sets the passed path to the glyph outline. 234 * If this cannot be done the path is set to empty; 235 * this is indistinguishable from a glyph with an empty path. 236 * This does not set glyph.fPath. 237 * 238 * TODO: path is always glyph.fPath, no reason to pass separately. 239 */ 240 virtual void generatePath(const SkGlyph& glyph, SkPath* path) = 0; 241 242 /** Retrieves font metrics. */ 243 virtual void generateFontMetrics(SkPaint::FontMetrics*) = 0; 244 245 /** Returns the number of glyphs in the font. */ 246 virtual unsigned generateGlyphCount() = 0; 247 248 /** Returns the glyph id for the given unichar. 249 * If there is no 1:1 mapping from the unichar to a glyph id, returns 0. 250 */ 251 virtual uint16_t generateCharToGlyph(SkUnichar unichar) = 0; 252 253 /** Returns the unichar for the given glyph id. 254 * If there is no 1:1 mapping from the glyph id to a unichar, returns 0. 255 * The default implementation always returns 0, indicating failure. 256 */ 257 virtual SkUnichar generateGlyphToChar(uint16_t glyphId); 258 259 void forceGenerateImageFromPath() { fGenerateImageFromPath = true; } 260 261private: 262 // never null 263 SkAutoTUnref<SkTypeface> fTypeface; 264 265#ifdef SK_BUILD_FOR_ANDROID 266 SkPaintOptionsAndroid fPaintOptionsAndroid; 267#endif 268 269 // optional object, which may be null 270 SkPathEffect* fPathEffect; 271 SkMaskFilter* fMaskFilter; 272 SkRasterizer* fRasterizer; 273 274 // if this is set, we draw the image from a path, rather than 275 // calling generateImage. 276 bool fGenerateImageFromPath; 277 278 void internalGetPath(const SkGlyph& glyph, SkPath* fillPath, 279 SkPath* devPath, SkMatrix* fillToDevMatrix); 280 281 // returns the right context from our link-list for this char. If no match 282 // is found it returns NULL. If a match is found then the glyphID param is 283 // set to the glyphID that maps to the provided char. 284 SkScalerContext* getContextFromChar(SkUnichar uni, uint16_t* glyphID); 285 286 // SkMaskGamma::PreBlend converts linear masks to gamma correcting masks. 287protected: 288 // Visible to subclasses so that generateImage can apply the pre-blend directly. 289 const SkMaskGamma::PreBlend fPreBlend; 290private: 291 // When there is a filter, previous steps must create a linear mask 292 // and the pre-blend applied as a final step. 293 const SkMaskGamma::PreBlend fPreBlendForFilter; 294}; 295 296#define kRec_SkDescriptorTag SkSetFourByteTag('s', 'r', 'e', 'c') 297#define kPathEffect_SkDescriptorTag SkSetFourByteTag('p', 't', 'h', 'e') 298#define kMaskFilter_SkDescriptorTag SkSetFourByteTag('m', 's', 'k', 'f') 299#define kRasterizer_SkDescriptorTag SkSetFourByteTag('r', 'a', 's', 't') 300#ifdef SK_BUILD_FOR_ANDROID 301#define kAndroidOpts_SkDescriptorTag SkSetFourByteTag('a', 'n', 'd', 'r') 302#endif 303 304/////////////////////////////////////////////////////////////////////////////// 305 306enum SkAxisAlignment { 307 kNone_SkAxisAlignment, 308 kX_SkAxisAlignment, 309 kY_SkAxisAlignment 310}; 311 312/** 313 * Return the axis (if any) that the baseline for horizontal text will land on 314 * after running through the specified matrix. 315 * 316 * As an example, the identity matrix will return kX_SkAxisAlignment 317 */ 318SkAxisAlignment SkComputeAxisAlignmentForHText(const SkMatrix& matrix); 319 320/////////////////////////////////////////////////////////////////////////////// 321 322SkPaint::Hinting SkScalerContextRec::getHinting() const { 323 unsigned hint = (fFlags & SkScalerContext::kHinting_Mask) >> 324 SkScalerContext::kHinting_Shift; 325 return static_cast<SkPaint::Hinting>(hint); 326} 327 328void SkScalerContextRec::setHinting(SkPaint::Hinting hinting) { 329 fFlags = (fFlags & ~SkScalerContext::kHinting_Mask) | 330 (hinting << SkScalerContext::kHinting_Shift); 331} 332 333 334#endif 335