blob: 61f94ad0064344f41ff0d521ffb6c3f7b4523d7e [file] [log] [blame]
reed@android.com8a1c16f2008-12-17 15:59:43 +00001/*
epoger@google.comec3ed6a2011-07-28 14:26:00 +00002 * Copyright 2006 The Android Open Source Project
reed@android.com8a1c16f2008-12-17 15:59:43 +00003 *
epoger@google.comec3ed6a2011-07-28 14:26:00 +00004 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
reed@android.com8a1c16f2008-12-17 15:59:43 +00006 */
7
8#ifndef SkScalerContext_DEFINED
9#define SkScalerContext_DEFINED
10
11#include "SkMask.h"
bungeman@google.com97efada2012-07-30 20:40:50 +000012#include "SkMaskGamma.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000013#include "SkMatrix.h"
14#include "SkPaint.h"
reed@google.com0da48612013-03-19 16:06:52 +000015#include "SkTypeface.h"
bungeman@google.com6e502fe2012-07-24 21:18:54 +000016
herbb69d0e02015-02-25 06:47:06 -080017class SkGlyph;
reed@android.com8a1c16f2008-12-17 15:59:43 +000018class SkDescriptor;
19class SkMaskFilter;
20class SkPathEffect;
21class SkRasterizer;
22
reeda9322c22016-04-12 06:47:05 -070023struct SkScalerContextEffects {
24 SkScalerContextEffects() : fPathEffect(nullptr), fMaskFilter(nullptr), fRasterizer(nullptr) {}
25 SkScalerContextEffects(SkPathEffect* pe, SkMaskFilter* mf, SkRasterizer* ra)
26 : fPathEffect(pe), fMaskFilter(mf), fRasterizer(ra) {}
27
28 SkPathEffect* fPathEffect;
29 SkMaskFilter* fMaskFilter;
30 SkRasterizer* fRasterizer;
31};
32
bungeman27876bc2016-02-29 11:22:55 -080033enum SkAxisAlignment {
34 kNone_SkAxisAlignment,
35 kX_SkAxisAlignment,
36 kY_SkAxisAlignment
37};
38
reed@google.coma9d4e842012-08-14 19:13:55 +000039/*
40 * To allow this to be forward-declared, it must be its own typename, rather
41 * than a nested struct inside SkScalerContext (where it started).
42 */
43struct SkScalerContextRec {
reed@google.coma9d4e842012-08-14 19:13:55 +000044 uint32_t fFontID;
45 SkScalar fTextSize, fPreScaleX, fPreSkewX;
46 SkScalar fPost2x2[2][2];
47 SkScalar fFrameWidth, fMiterLimit;
rmistry@google.comfbfcd562012-08-23 18:09:54 +000048
reed@google.coma9d4e842012-08-14 19:13:55 +000049 //These describe the parameters to create (uniquely identify) the pre-blend.
50 uint32_t fLumBits;
51 uint8_t fDeviceGamma; //2.6, (0.0, 4.0) gamma, 0.0 for sRGB
52 uint8_t fPaintGamma; //2.6, (0.0, 4.0) gamma, 0.0 for sRGB
53 uint8_t fContrast; //0.8+1, [0.0, 1.0] artificial contrast
54 uint8_t fReservedAlign;
rmistry@google.comfbfcd562012-08-23 18:09:54 +000055
reed@google.coma9d4e842012-08-14 19:13:55 +000056 SkScalar getDeviceGamma() const {
57 return SkIntToScalar(fDeviceGamma) / (1 << 6);
58 }
59 void setDeviceGamma(SkScalar dg) {
60 SkASSERT(0 <= dg && dg < SkIntToScalar(4));
61 fDeviceGamma = SkScalarFloorToInt(dg * (1 << 6));
62 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +000063
reed@google.coma9d4e842012-08-14 19:13:55 +000064 SkScalar getPaintGamma() const {
65 return SkIntToScalar(fPaintGamma) / (1 << 6);
66 }
67 void setPaintGamma(SkScalar pg) {
68 SkASSERT(0 <= pg && pg < SkIntToScalar(4));
69 fPaintGamma = SkScalarFloorToInt(pg * (1 << 6));
70 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +000071
reed@google.coma9d4e842012-08-14 19:13:55 +000072 SkScalar getContrast() const {
73 return SkIntToScalar(fContrast) / ((1 << 8) - 1);
74 }
75 void setContrast(SkScalar c) {
76 SkASSERT(0 <= c && c <= SK_Scalar1);
77 fContrast = SkScalarRoundToInt(c * ((1 << 8) - 1));
78 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +000079
reed@google.coma9d4e842012-08-14 19:13:55 +000080 /**
brianosmana1e8f8d2016-04-08 06:47:54 -070081 * Causes the luminance color to be ignored, and the paint and device
82 * gamma to be effectively 1.0
83 */
84 void ignoreGamma() {
85 setLuminanceColor(SK_ColorTRANSPARENT);
86 setPaintGamma(SK_Scalar1);
87 setDeviceGamma(SK_Scalar1);
88 }
89
90 /**
reed@google.coma9d4e842012-08-14 19:13:55 +000091 * Causes the luminance color and contrast to be ignored, and the
92 * paint and device gamma to be effectively 1.0.
93 */
94 void ignorePreBlend() {
brianosmana1e8f8d2016-04-08 06:47:54 -070095 ignoreGamma();
reed@google.coma9d4e842012-08-14 19:13:55 +000096 setContrast(0);
97 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +000098
reed@google.coma9d4e842012-08-14 19:13:55 +000099 uint8_t fMaskFormat;
caryclarkd7ea92f2016-03-16 07:34:02 -0700100 uint8_t fStrokeJoin : 4;
101 uint8_t fStrokeCap : 4;
reed@google.coma9d4e842012-08-14 19:13:55 +0000102 uint16_t fFlags;
103 // Warning: when adding members note that the size of this structure
104 // must be a multiple of 4. SkDescriptor requires that its arguments be
105 // multiples of four and this structure is put in an SkDescriptor in
106 // SkPaint::MakeRec.
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000107
reed@google.coma9d4e842012-08-14 19:13:55 +0000108 void getMatrixFrom2x2(SkMatrix*) const;
109 void getLocalMatrix(SkMatrix*) const;
110 void getSingleMatrix(SkMatrix*) const;
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000111
bungeman5f14c5e2014-12-05 12:26:44 -0800112 /** The kind of scale which will be applied by the underlying port (pre-matrix). */
113 enum PreMatrixScale {
114 kFull_PreMatrixScale, // The underlying port can apply both x and y scale.
115 kVertical_PreMatrixScale, // The underlying port can only apply a y scale.
116 kVerticalInteger_PreMatrixScale // The underlying port can only apply an integer y scale.
117 };
118 /**
119 * Compute useful matrices for use with sizing in underlying libraries.
120 *
121 * There are two kinds of text size, a 'requested/logical size' which is like asking for size
122 * '12' and a 'real' size which is the size after the matrix is applied. The matrices produced
123 * by this method are based on the 'real' size. This method effectively finds the total device
124 * matrix and decomposes it in various ways.
125 *
126 * The most useful decomposition is into 'scale' and 'remaining'. The 'scale' is applied first
127 * and then the 'remaining' to fully apply the total matrix. This decomposition is useful when
128 * the text size ('scale') may have meaning apart from the total matrix. This is true when
129 * hinting, and sometimes true for other properties as well.
130 *
131 * The second (optional) decomposition is of 'remaining' into a non-rotational part
132 * 'remainingWithoutRotation' and a rotational part 'remainingRotation'. The 'scale' is applied
133 * first, then 'remainingWithoutRotation', then 'remainingRotation' to fully apply the total
134 * matrix. This decomposition is helpful when only horizontal metrics can be trusted, so the
135 * 'scale' and 'remainingWithoutRotation' will be handled by the underlying library, but
136 * the final rotation 'remainingRotation' will be handled manually.
137 *
138 * The 'total' matrix is also (optionally) available. This is useful in cases where the
139 * underlying library will not be used, often when working directly with font data.
140 *
halcanary96fcdcc2015-08-27 07:41:13 -0700141 * The parameters 'scale' and 'remaining' are required, the other pointers may be nullptr.
bungeman5f14c5e2014-12-05 12:26:44 -0800142 *
143 * @param preMatrixScale the kind of scale to extract from the total matrix.
144 * @param scale the scale extracted from the total matrix (both values positive).
145 * @param remaining apply after scale to apply the total matrix.
146 * @param remainingWithoutRotation apply after scale to apply the total matrix sans rotation.
147 * @param remainingRotation apply after remainingWithoutRotation to apply the total matrix.
148 * @param total the total matrix.
bungemane55131c2016-08-24 12:01:31 -0700149 * @return false if the matrix was singular. The output will be valid but not invertible.
bungeman5f14c5e2014-12-05 12:26:44 -0800150 */
bungeman1f0e78d2016-08-23 13:19:01 -0700151 bool computeMatrices(PreMatrixScale preMatrixScale,
bungeman5f14c5e2014-12-05 12:26:44 -0800152 SkVector* scale, SkMatrix* remaining,
halcanary96fcdcc2015-08-27 07:41:13 -0700153 SkMatrix* remainingWithoutRotation = nullptr,
154 SkMatrix* remainingRotation = nullptr,
155 SkMatrix* total = nullptr);
bungeman5f14c5e2014-12-05 12:26:44 -0800156
reed@google.coma9d4e842012-08-14 19:13:55 +0000157 inline SkPaint::Hinting getHinting() const;
158 inline void setHinting(SkPaint::Hinting);
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000159
reed@google.coma9d4e842012-08-14 19:13:55 +0000160 SkMask::Format getFormat() const {
161 return static_cast<SkMask::Format>(fMaskFormat);
162 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000163
reed@google.coma9d4e842012-08-14 19:13:55 +0000164 SkColor getLuminanceColor() const {
165 return fLumBits;
166 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000167
reed@google.coma9d4e842012-08-14 19:13:55 +0000168 void setLuminanceColor(SkColor c) {
169 fLumBits = c;
170 }
171};
172
bungeman@google.com97efada2012-07-30 20:40:50 +0000173//The following typedef hides from the rest of the implementation the number of
174//most significant bits to consider when creating mask gamma tables. Two bits
175//per channel was chosen as a balance between fidelity (more bits) and cache
bungeman@google.comfd668cf2012-08-24 17:46:11 +0000176//sizes (fewer bits). Three bits per channel was chosen when #303942; (used by
177//the Chrome UI) turned out too green.
178typedef SkTMaskGamma<3, 3, 3> SkMaskGamma;
bungeman@google.com97efada2012-07-30 20:40:50 +0000179
reed@android.com8a1c16f2008-12-17 15:59:43 +0000180class SkScalerContext {
181public:
reed@google.coma9d4e842012-08-14 19:13:55 +0000182 typedef SkScalerContextRec Rec;
183
reed@android.com8a1c16f2008-12-17 15:59:43 +0000184 enum Flags {
reed@google.comb6bb5cb2011-11-21 19:32:29 +0000185 kFrameAndFill_Flag = 0x0001,
186 kDevKernText_Flag = 0x0002,
187 kEmbeddedBitmapText_Flag = 0x0004,
188 kEmbolden_Flag = 0x0008,
189 kSubpixelPositioning_Flag = 0x0010,
bungeman@google.comf6f56872014-01-23 19:01:36 +0000190 kForceAutohinting_Flag = 0x0020, // Use auto instead of bytcode hinting if hinting.
reed@google.comb6bb5cb2011-11-21 19:32:29 +0000191 kVertical_Flag = 0x0040,
reed@google.comffe49f52011-11-22 19:42:41 +0000192
agl@chromium.org309485b2009-07-21 17:41:32 +0000193 // together, these two flags resulting in a two bit value which matches
194 // up with the SkPaint::Hinting enum.
reed@google.comffe49f52011-11-22 19:42:41 +0000195 kHinting_Shift = 7, // to shift into the other flags above
reed@google.comb6bb5cb2011-11-21 19:32:29 +0000196 kHintingBit1_Flag = 0x0080,
197 kHintingBit2_Flag = 0x0100,
reed@google.comffe49f52011-11-22 19:42:41 +0000198
bungeman@google.com0abbff92013-07-27 20:37:56 +0000199 // Pixel geometry information.
reedd54d3fc2014-11-13 14:39:58 -0800200 // only meaningful if fMaskFormat is kLCD16
reed@google.comb6bb5cb2011-11-21 19:32:29 +0000201 kLCD_Vertical_Flag = 0x0200, // else Horizontal
202 kLCD_BGROrder_Flag = 0x0400, // else RGB order
reed@google.comffe49f52011-11-22 19:42:41 +0000203
bungeman@google.com0abbff92013-07-27 20:37:56 +0000204 // Generate A8 from LCD source (for GDI and CoreGraphics).
205 // only meaningful if fMaskFormat is kA8
206 kGenA8FromLCD_Flag = 0x0800, // could be 0x200 (bit meaning dependent on fMaskFormat)
reed@android.com8a1c16f2008-12-17 15:59:43 +0000207 };
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000208
reed@google.comffe49f52011-11-22 19:42:41 +0000209 // computed values
reed@android.come2ca2072009-07-27 16:39:38 +0000210 enum {
reed@google.comffe49f52011-11-22 19:42:41 +0000211 kHinting_Mask = kHintingBit1_Flag | kHintingBit2_Flag,
reed@android.come2ca2072009-07-27 16:39:38 +0000212 };
reed@google.comffe49f52011-11-22 19:42:41 +0000213
bungeman7cfd46a2016-10-20 16:06:52 -0400214 SkScalerContext(sk_sp<SkTypeface>, const SkScalerContextEffects&, const SkDescriptor*);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000215 virtual ~SkScalerContext();
216
reed@google.com0da48612013-03-19 16:06:52 +0000217 SkTypeface* getTypeface() const { return fTypeface.get(); }
218
reed@google.com98539c62011-03-15 15:40:16 +0000219 SkMask::Format getMaskFormat() const {
220 return (SkMask::Format)fRec.fMaskFormat;
221 }
222
reed@google.comabf00aa2012-01-03 19:43:20 +0000223 bool isSubpixel() const {
224 return SkToBool(fRec.fFlags & kSubpixelPositioning_Flag);
225 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000226
bungeman41078062014-07-07 08:16:37 -0700227 bool isVertical() const {
228 return SkToBool(fRec.fFlags & kVertical_Flag);
229 }
230
reed@android.coma14ea0e2009-03-17 17:59:53 +0000231 /** Return the corresponding glyph for the specified unichar. Since contexts
232 may be chained (under the hood), the glyphID that is returned may in
233 fact correspond to a different font/context. In that case, we use the
234 base-glyph-count to know how to translate back into local glyph space.
235 */
djsollen1b277042014-08-06 06:58:06 -0700236 uint16_t charToGlyphID(SkUnichar uni) {
237 return generateCharToGlyph(uni);
238 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000239
reed@android.com9d3a9852010-01-08 14:07:42 +0000240 /** Map the glyphID to its glyph index, and then to its char code. Unmapped
241 glyphs return zero.
242 */
djsollen1b277042014-08-06 06:58:06 -0700243 SkUnichar glyphIDToChar(uint16_t glyphID) {
244 return (glyphID < getGlyphCount()) ? generateGlyphToChar(glyphID) : 0;
245 }
reed@android.com9d3a9852010-01-08 14:07:42 +0000246
ctguil@chromium.org0bc7bf52011-03-04 19:04:57 +0000247 unsigned getGlyphCount() { return this->generateGlyphCount(); }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000248 void getAdvance(SkGlyph*);
249 void getMetrics(SkGlyph*);
250 void getImage(const SkGlyph&);
251 void getPath(const SkGlyph&, SkPath*);
reed@google.com0a01f5a2013-05-08 14:19:08 +0000252 void getFontMetrics(SkPaint::FontMetrics*);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000253
jvanverth2d2a68c2014-06-10 06:42:56 -0700254 /** Return the size in bytes of the associated gamma lookup table
255 */
256 static size_t GetGammaLUTSize(SkScalar contrast, SkScalar paintGamma, SkScalar deviceGamma,
257 int* width, int* height);
258
259 /** Get the associated gamma lookup table. The 'data' pointer must point to pre-allocated
260 memory, with size in bytes greater than or equal to the return value of getGammaLUTSize().
261 */
262 static void GetGammaLUTData(SkScalar contrast, SkScalar paintGamma, SkScalar deviceGamma,
263 void* data);
264
robertphillipsfcf78292015-06-19 11:49:52 -0700265 static void MakeRec(const SkPaint&, const SkSurfaceProps* surfaceProps,
commit-bot@chromium.orgeefd8a02014-04-08 20:14:32 +0000266 const SkMatrix*, Rec* rec);
bungeman@google.com97efada2012-07-30 20:40:50 +0000267 static inline void PostMakeRec(const SkPaint&, Rec*);
reed@google.com10d2d4d2012-03-01 22:32:51 +0000268
bungeman@google.com97efada2012-07-30 20:40:50 +0000269 static SkMaskGamma::PreBlend GetMaskPreBlend(const Rec& rec);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000270
reed40dab982015-01-28 13:28:53 -0800271 const Rec& getRec() const { return fRec; }
272
reeda9322c22016-04-12 06:47:05 -0700273 SkScalerContextEffects getEffects() const {
274 return { fPathEffect.get(), fMaskFilter.get(), fRasterizer.get() };
275 }
276
bungeman27876bc2016-02-29 11:22:55 -0800277 /**
278 * Return the axis (if any) that the baseline for horizontal text should land on.
279 * As an example, the identity matrix will return kX_SkAxisAlignment
280 */
281 SkAxisAlignment computeAxisAlignmentForHText();
282
reed@android.com8a1c16f2008-12-17 15:59:43 +0000283protected:
284 Rec fRec;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000285
rmistry@google.comd6bab022013-12-02 13:50:38 +0000286 /** Generates the contents of glyph.fAdvanceX and glyph.fAdvanceY.
287 * May call getMetrics if that would be just as fast.
288 */
289 virtual void generateAdvance(SkGlyph* glyph) = 0;
290
291 /** Generates the contents of glyph.fWidth, fHeight, fTop, fLeft,
292 * as well as fAdvanceX and fAdvanceY if not already set.
293 *
294 * TODO: fMaskFormat is set by getMetrics later; cannot be set here.
295 */
296 virtual void generateMetrics(SkGlyph* glyph) = 0;
297
298 /** Generates the contents of glyph.fImage.
299 * When called, glyph.fImage will be pointing to a pre-allocated,
300 * uninitialized region of memory of size glyph.computeImageSize().
301 * This method may change glyph.fMaskFormat if the new image size is
302 * less than or equal to the old image size.
303 *
304 * Because glyph.computeImageSize() will determine the size of fImage,
305 * generateMetrics will be called before generateImage.
306 */
307 virtual void generateImage(const SkGlyph& glyph) = 0;
308
309 /** Sets the passed path to the glyph outline.
310 * If this cannot be done the path is set to empty;
311 * this is indistinguishable from a glyph with an empty path.
312 * This does not set glyph.fPath.
313 *
314 * TODO: path is always glyph.fPath, no reason to pass separately.
315 */
316 virtual void generatePath(const SkGlyph& glyph, SkPath* path) = 0;
317
bungeman41078062014-07-07 08:16:37 -0700318 /** Retrieves font metrics. */
319 virtual void generateFontMetrics(SkPaint::FontMetrics*) = 0;
rmistry@google.comd6bab022013-12-02 13:50:38 +0000320
321 /** Returns the number of glyphs in the font. */
322 virtual unsigned generateGlyphCount() = 0;
323
324 /** Returns the glyph id for the given unichar.
325 * If there is no 1:1 mapping from the unichar to a glyph id, returns 0.
326 */
327 virtual uint16_t generateCharToGlyph(SkUnichar unichar) = 0;
328
329 /** Returns the unichar for the given glyph id.
330 * If there is no 1:1 mapping from the glyph id to a unichar, returns 0.
331 * The default implementation always returns 0, indicating failure.
332 */
333 virtual SkUnichar generateGlyphToChar(uint16_t glyphId);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000334
reed@google.coma767fa02011-08-05 21:40:26 +0000335 void forceGenerateImageFromPath() { fGenerateImageFromPath = true; }
joshualittaa2f6582015-07-29 10:14:58 -0700336 void forceOffGenerateImageFromPath() { fGenerateImageFromPath = false; }
reed@google.coma767fa02011-08-05 21:40:26 +0000337
reed@android.com8a1c16f2008-12-17 15:59:43 +0000338private:
joshualitt65e96b42015-07-31 11:45:22 -0700339 friend class SkRandomScalerContext; // For debug purposes
340
reed@google.com0da48612013-03-19 16:06:52 +0000341 // never null
reeda9322c22016-04-12 06:47:05 -0700342 sk_sp<SkTypeface> fTypeface;
skia.committer@gmail.com01c34ee2013-03-20 07:01:02 +0000343
reeda9322c22016-04-12 06:47:05 -0700344 // optional objects, which may be null
345 sk_sp<SkPathEffect> fPathEffect;
346 sk_sp<SkMaskFilter> fMaskFilter;
347 sk_sp<SkRasterizer> fRasterizer;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000348
reed@google.coma767fa02011-08-05 21:40:26 +0000349 // if this is set, we draw the image from a path, rather than
350 // calling generateImage.
351 bool fGenerateImageFromPath;
352
reed@android.com8a1c16f2008-12-17 15:59:43 +0000353 void internalGetPath(const SkGlyph& glyph, SkPath* fillPath,
354 SkPath* devPath, SkMatrix* fillToDevMatrix);
355
bungeman@google.coma76de722012-10-26 19:35:54 +0000356 // SkMaskGamma::PreBlend converts linear masks to gamma correcting masks.
357protected:
358 // Visible to subclasses so that generateImage can apply the pre-blend directly.
359 const SkMaskGamma::PreBlend fPreBlend;
360private:
361 // When there is a filter, previous steps must create a linear mask
362 // and the pre-blend applied as a final step.
363 const SkMaskGamma::PreBlend fPreBlendForFilter;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000364};
365
366#define kRec_SkDescriptorTag SkSetFourByteTag('s', 'r', 'e', 'c')
367#define kPathEffect_SkDescriptorTag SkSetFourByteTag('p', 't', 'h', 'e')
368#define kMaskFilter_SkDescriptorTag SkSetFourByteTag('m', 's', 'k', 'f')
369#define kRasterizer_SkDescriptorTag SkSetFourByteTag('r', 'a', 's', 't')
370
reed@google.comcb6ccdd2011-08-23 21:30:47 +0000371///////////////////////////////////////////////////////////////////////////////
372
reed@google.coma9d4e842012-08-14 19:13:55 +0000373SkPaint::Hinting SkScalerContextRec::getHinting() const {
374 unsigned hint = (fFlags & SkScalerContext::kHinting_Mask) >>
375 SkScalerContext::kHinting_Shift;
376 return static_cast<SkPaint::Hinting>(hint);
377}
378
379void SkScalerContextRec::setHinting(SkPaint::Hinting hinting) {
380 fFlags = (fFlags & ~SkScalerContext::kHinting_Mask) |
381 (hinting << SkScalerContext::kHinting_Shift);
382}
383
384
reed@android.com8a1c16f2008-12-17 15:59:43 +0000385#endif