Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
SkScalerContext.h
Go to the documentation of this file.
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
14#include "include/core/SkRect.h"
25#include "src/core/SkGlyph.h"
26#include "src/core/SkMask.h"
28
29#include <cstddef>
30#include <cstdint>
31#include <memory>
32#include <utility>
33
34class SkArenaAlloc;
36class SkDescriptor;
37class SkDrawable;
38class SkFont;
39class SkMaskFilter;
40class SkPath;
41class SkPathEffect;
42enum class SkFontHinting;
43struct SkFontMetrics;
44
45enum class SkScalerContextFlags : uint32_t {
46 kNone = 0,
47 kFakeGamma = 1 << 0,
48 kBoostContrast = 1 << 1,
50};
52
53/*
54 * To allow this to be forward-declared, it must be its own typename, rather
55 * than a nested struct inside SkScalerContext (where it started).
56 *
57 * SkScalerContextRec must be dense, and all bytes must be set to a know quantity because this
58 * structure is used to calculate a checksum.
59 */
66
67 // This will be set if to the paint's foreground color if
68 // kNeedsForegroundColor is set, which will usually be the case for COLRv0 and
69 // COLRv1 fonts.
71
72private:
73 //These describe the parameters to create (uniquely identify) the pre-blend.
74 uint32_t fLumBits;
75 uint8_t fDeviceGamma; //2.6, (0.0, 4.0) gamma, 0.0 for sRGB
76 uint8_t fPaintGamma; //2.6, (0.0, 4.0) gamma, 0.0 for sRGB
77 uint8_t fContrast; //0.8+1, [0.0, 1.0] artificial contrast
78 const uint8_t fReservedAlign{0};
79
80public:
81
83 return SkIntToScalar(fDeviceGamma) / (1 << 6);
84 }
90
92 return SkIntToScalar(fPaintGamma) / (1 << 6);
93 }
99
101 sk_ignore_unused_variable(fReservedAlign);
102 return SkIntToScalar(fContrast) / ((1 << 8) - 1);
103 }
109
110 /**
111 * Causes the luminance color to be ignored, and the paint and device
112 * gamma to be effectively 1.0
113 */
119
120 /**
121 * Causes the luminance color and contrast to be ignored, and the
122 * paint and device gamma to be effectively 1.0.
123 */
125 ignoreGamma();
126 setContrast(0);
127 }
128
130
131private:
132 uint8_t fStrokeJoin : 4;
133 uint8_t fStrokeCap : 4;
134
135public:
136 uint16_t fFlags;
137
138 // Warning: when adding members note that the size of this structure
139 // must be a multiple of 4. SkDescriptor requires that its arguments be
140 // multiples of four and this structure is put in an SkDescriptor in
141 // SkPaint::MakeRecAndEffects.
142
143 SkString dump() const {
144 SkString msg;
145 msg.appendf(" Rec\n");
146 msg.appendf(" textsize %a prescale %a preskew %a post [%a %a %a %a]\n",
148 fPost2x2[0][1], fPost2x2[1][0], fPost2x2[1][1]);
149 msg.appendf(" frame %g miter %g format %d join %d cap %d flags %#hx\n",
150 fFrameWidth, fMiterLimit, fMaskFormat, fStrokeJoin, fStrokeCap, fFlags);
151 msg.appendf(" lum bits %x, device gamma %d, paint gamma %d contrast %d\n", fLumBits,
152 fDeviceGamma, fPaintGamma, fContrast);
153 msg.appendf(" foreground color %x\n", fForegroundColor);
154 return msg;
155 }
156
157 void getMatrixFrom2x2(SkMatrix*) const;
158 void getLocalMatrix(SkMatrix*) const;
159 void getSingleMatrix(SkMatrix*) const;
160
161 /** The kind of scale which will be applied by the underlying port (pre-matrix). */
162 enum class PreMatrixScale {
163 kFull, // The underlying port can apply both x and y scale.
164 kVertical, // The underlying port can only apply a y scale.
165 kVerticalInteger // The underlying port can only apply an integer y scale.
166 };
167 /**
168 * Compute useful matrices for use with sizing in underlying libraries.
169 *
170 * There are two kinds of text size, a 'requested/logical size' which is like asking for size
171 * '12' and a 'real' size which is the size after the matrix is applied. The matrices produced
172 * by this method are based on the 'real' size. This method effectively finds the total device
173 * matrix and decomposes it in various ways.
174 *
175 * The most useful decomposition is into 'scale' and 'remaining'. The 'scale' is applied first
176 * and then the 'remaining' to fully apply the total matrix. This decomposition is useful when
177 * the text size ('scale') may have meaning apart from the total matrix. This is true when
178 * hinting, and sometimes true for other properties as well.
179 *
180 * The second (optional) decomposition is of 'remaining' into a non-rotational part
181 * 'remainingWithoutRotation' and a rotational part 'remainingRotation'. The 'scale' is applied
182 * first, then 'remainingWithoutRotation', then 'remainingRotation' to fully apply the total
183 * matrix. This decomposition is helpful when only horizontal metrics can be trusted, so the
184 * 'scale' and 'remainingWithoutRotation' will be handled by the underlying library, but
185 * the final rotation 'remainingRotation' will be handled manually.
186 *
187 * The 'total' matrix is also (optionally) available. This is useful in cases where the
188 * underlying library will not be used, often when working directly with font data.
189 *
190 * The parameters 'scale' and 'remaining' are required, the other pointers may be nullptr.
191 *
192 * @param preMatrixScale the kind of scale to extract from the total matrix.
193 * @param scale the scale extracted from the total matrix (both values positive).
194 * @param remaining apply after scale to apply the total matrix.
195 * @param remainingWithoutRotation apply after scale to apply the total matrix sans rotation.
196 * @param remainingRotation apply after remainingWithoutRotation to apply the total matrix.
197 * @param total the total matrix.
198 * @return false if the matrix was singular. The output will be valid but not invertible.
199 */
200 bool computeMatrices(PreMatrixScale preMatrixScale,
201 SkVector* scale, SkMatrix* remaining,
202 SkMatrix* remainingWithoutRotation = nullptr,
203 SkMatrix* remainingRotation = nullptr,
204 SkMatrix* total = nullptr);
205
207
208 inline SkFontHinting getHinting() const;
209 inline void setHinting(SkFontHinting);
210
212 return fMaskFormat;
213 }
214
216 return fLumBits;
217 }
218
219 // setLuminanceColor forces the alpha to be 0xFF because the blitter that draws the glyph
220 // will apply the alpha from the paint. Don't apply the alpha twice.
222
223private:
224 // TODO: remove
225 friend class SkScalerContext;
226};
228
229// TODO: rename SkScalerContextEffects -> SkStrikeEffects
241
242//The following typedef hides from the rest of the implementation the number of
243//most significant bits to consider when creating mask gamma tables. Two bits
244//per channel was chosen as a balance between fidelity (more bits) and cache
245//sizes (fewer bits). Three bits per channel was chosen when #303942; (used by
246//the Chrome UI) turned out too green.
248
250public:
251 enum Flags {
253 kUnused = 0x0002,
257 kForceAutohinting_Flag = 0x0020, // Use auto instead of bytcode hinting if hinting.
258
259 // together, these two flags resulting in a two bit value which matches
260 // up with the SkPaint::Hinting enum.
261 kHinting_Shift = 7, // to shift into the other flags above
264
265 // Pixel geometry information.
266 // only meaningful if fMaskFormat is kLCD16
267 kLCD_Vertical_Flag = 0x0200, // else Horizontal
268 kLCD_BGROrder_Flag = 0x0400, // else RGB order
269
270 // Generate A8 from LCD source (for GDI and CoreGraphics).
271 // only meaningful if fMaskFormat is kA8
272 kGenA8FromLCD_Flag = 0x0800, // could be 0x200 (bit meaning dependent on fMaskFormat)
275
277 };
278
279 // computed values
280 enum {
282 };
283
285 virtual ~SkScalerContext();
286
287 SkTypeface* getTypeface() const { return fTypeface.get(); }
288
290 return fRec.fMaskFormat;
291 }
292
293 bool isSubpixel() const {
295 }
296
297 bool isLinearMetrics() const {
299 }
300
301 // DEPRECATED
302 bool isVertical() const { return false; }
303
305 void getImage(const SkGlyph&);
309
310 /** Return the size in bytes of the associated gamma lookup table
311 */
312 static size_t GetGammaLUTSize(SkScalar contrast, SkScalar paintGamma, SkScalar deviceGamma,
313 int* width, int* height);
314
315 /** Get the associated gamma lookup table. The 'data' pointer must point to pre-allocated
316 * memory, with size in bytes greater than or equal to the return value of getGammaLUTSize().
317 *
318 * If the lookup table hasn't been initialized (e.g., it's linear), this will return false.
319 */
320 static bool GetGammaLUTData(SkScalar contrast, SkScalar paintGamma, SkScalar deviceGamma,
321 uint8_t* data);
322
323 static void MakeRecAndEffects(const SkFont& font, const SkPaint& paint,
324 const SkSurfaceProps& surfaceProps,
325 SkScalerContextFlags scalerContextFlags,
326 const SkMatrix& deviceMatrix,
328 SkScalerContextEffects* effects);
329
330 // If we are creating rec and effects from a font only, then there is no device around either.
331 static void MakeRecAndEffectsFromFont(const SkFont& font,
333 SkScalerContextEffects* effects) {
335 return MakeRecAndEffects(
336 font, paint, SkSurfaceProps(),
338 }
339
340 static std::unique_ptr<SkScalerContext> MakeEmpty(
341 sk_sp<SkTypeface> typeface, const SkScalerContextEffects& effects,
342 const SkDescriptor* desc);
343
345 const SkScalerContextRec& rec,
346 const SkScalerContextEffects& effects,
347 SkAutoDescriptor* ad);
348
349 static std::unique_ptr<SkDescriptor> DescriptorGivenRecAndEffects(
350 const SkScalerContextRec& rec,
351 const SkScalerContextEffects& effects);
352
353 static void DescriptorBufferGiveRec(const SkScalerContextRec& rec, void* buffer);
354 static bool CheckBufferSizeForRec(const SkScalerContextRec& rec,
355 const SkScalerContextEffects& effects,
356 size_t size);
357
359
360 const SkScalerContextRec& getRec() const { return fRec; }
361
363 return { fPathEffect.get(), fMaskFilter.get() };
364 }
365
366 /**
367 * Return the axis (if any) that the baseline for horizontal text should land on.
368 * As an example, the identity matrix will return SkAxisAlignment::kX.
369 */
371
373 const SkFont&, const SkPaint&, const SkSurfaceProps&,
374 SkScalerContextFlags scalerContextFlags,
375 const SkMatrix& deviceMatrix, SkAutoDescriptor* ad,
376 SkScalerContextEffects* effects);
377
378protected:
380
398
400
401 static void GenerateMetricsFromPath(
402 SkGlyph* glyph, const SkPath& path, SkMask::Format format,
403 bool verticalLCD, bool a8FromLCD, bool hairline);
404
405 static void SaturateGlyphBounds(SkGlyph* glyph, SkRect&&);
406 static void SaturateGlyphBounds(SkGlyph* glyph, SkIRect const &);
407
408 /** Generates the contents of glyph.fImage.
409 * When called, glyph.fImage will be pointing to a pre-allocated,
410 * uninitialized region of memory of size glyph.imageSize().
411 * This method may not change glyph.fMaskFormat.
412 *
413 * Because glyph.imageSize() will determine the size of fImage,
414 * generateMetrics will be called before generateImage.
415 */
416 virtual void generateImage(const SkGlyph& glyph, void* imageBuffer) = 0;
417 static void GenerateImageFromPath(
418 SkMaskBuilder& dst, const SkPath& path, const SkMaskGamma::PreBlend& maskPreBlend,
419 bool doBGR, bool verticalLCD, bool a8FromLCD, bool hairline);
420
421 /** Sets the passed path to the glyph outline.
422 * If this cannot be done the path is set to empty;
423 * Does not apply subpixel positioning to the path.
424 * @return false if this glyph does not have any path.
425 */
426 [[nodiscard]] virtual bool generatePath(const SkGlyph&, SkPath*) = 0;
427
428 /** Returns the drawable for the glyph (if any).
429 *
430 * The generated drawable will be lifetime scoped to the lifetime of this scaler context.
431 * This means the drawable may refer to the scaler context and associated font data.
432 *
433 * The drawable does not need to be flattenable (e.g. implement getFactory and getTypeName).
434 * Any necessary serialization will be done with makePictureSnapshot.
435 */
436 virtual sk_sp<SkDrawable> generateDrawable(const SkGlyph&); // TODO: = 0
437
438 /** Retrieves font metrics. */
440
441 void forceGenerateImageFromPath() { fGenerateImageFromPath = true; }
442 void forceOffGenerateImageFromPath() { fGenerateImageFromPath = false; }
443
444private:
445 friend class PathText; // For debug purposes
446 friend class PathTextBench; // For debug purposes
447 friend class RandomScalerContext; // For debug purposes
448
449 static SkScalerContextRec PreprocessRec(const SkTypeface&,
451 const SkDescriptor&);
452
453 // never null
454 sk_sp<SkTypeface> fTypeface;
455
456 // optional objects, which may be null
457 sk_sp<SkPathEffect> fPathEffect;
458 sk_sp<SkMaskFilter> fMaskFilter;
459
460 // if this is set, we draw the image from a path, rather than
461 // calling generateImage.
462 bool fGenerateImageFromPath;
463
464 void internalGetPath(SkGlyph&, SkArenaAlloc*);
466
467protected:
468 // SkMaskGamma::PreBlend converts linear masks to gamma correcting masks.
469 // Visible to subclasses so that generateImage can apply the pre-blend directly.
471};
472
473#define kRec_SkDescriptorTag SkSetFourByteTag('s', 'r', 'e', 'c')
474#define kEffects_SkDescriptorTag SkSetFourByteTag('e', 'f', 'c', 't')
475
476///////////////////////////////////////////////////////////////////////////////
477
483
485 fFlags = (fFlags & ~SkScalerContext::kHinting_Mask) |
486 (static_cast<unsigned>(hinting) << SkScalerContext::kHinting_Shift);
487}
488
489
490#endif
#define SkASSERT(cond)
Definition SkAssert.h:116
uint32_t SkColor
Definition SkColor.h:37
constexpr SkColor SK_ColorTRANSPARENT
Definition SkColor.h:99
constexpr SkColor SK_ColorBLACK
Definition SkColor.h:103
SkFontHinting
Definition SkFontTypes.h:18
SkAxisAlignment
Definition SkGlyph.h:218
#define SK_MAKE_BITFIELD_OPS(X)
Definition SkMacros.h:66
#define SK_BEGIN_REQUIRE_DENSE
Definition SkMacros.h:37
#define SK_END_REQUIRE_DENSE
Definition SkMacros.h:38
#define SK_Scalar1
Definition SkScalar.h:18
#define SkScalarRoundToInt(x)
Definition SkScalar.h:37
#define SkIntToScalar(x)
Definition SkScalar.h:57
#define SkScalarFloorToInt(x)
Definition SkScalar.h:35
SkTMaskGamma< 3, 3, 3 > SkMaskGamma
SkScalerContextFlags
void sk_ignore_unused_variable(const T &)
Definition SkTemplates.h:37
static constexpr bool SkToBool(const T &x)
Definition SkTo.h:35
uint32_t SkTypefaceID
Definition SkTypeface.h:38
static const SkMatrix & I()
void forceGenerateImageFromPath()
virtual void generateFontMetrics(SkFontMetrics *)=0
SkScalerContextRec fRec
static size_t GetGammaLUTSize(SkScalar contrast, SkScalar paintGamma, SkScalar deviceGamma, int *width, int *height)
virtual void generateImage(const SkGlyph &glyph, void *imageBuffer)=0
static void GenerateImageFromPath(SkMaskBuilder &dst, const SkPath &path, const SkMaskGamma::PreBlend &maskPreBlend, bool doBGR, bool verticalLCD, bool a8FromLCD, bool hairline)
static void MakeRecAndEffects(const SkFont &font, const SkPaint &paint, const SkSurfaceProps &surfaceProps, SkScalerContextFlags scalerContextFlags, const SkMatrix &deviceMatrix, SkScalerContextRec *rec, SkScalerContextEffects *effects)
SkMask::Format getMaskFormat() const
static std::unique_ptr< SkDescriptor > DescriptorGivenRecAndEffects(const SkScalerContextRec &rec, const SkScalerContextEffects &effects)
virtual GlyphMetrics generateMetrics(const SkGlyph &, SkArenaAlloc *)=0
virtual bool generatePath(const SkGlyph &, SkPath *)=0
void getPath(SkGlyph &, SkArenaAlloc *)
const SkMaskGamma::PreBlend fPreBlend
static SkMaskGamma::PreBlend GetMaskPreBlend(const SkScalerContextRec &rec)
SkTypeface * getTypeface() const
static SkDescriptor * CreateDescriptorAndEffectsUsingPaint(const SkFont &, const SkPaint &, const SkSurfaceProps &, SkScalerContextFlags scalerContextFlags, const SkMatrix &deviceMatrix, SkAutoDescriptor *ad, SkScalerContextEffects *effects)
virtual sk_sp< SkDrawable > generateDrawable(const SkGlyph &)
static SkDescriptor * AutoDescriptorGivenRecAndEffects(const SkScalerContextRec &rec, const SkScalerContextEffects &effects, SkAutoDescriptor *ad)
static void MakeRecAndEffectsFromFont(const SkFont &font, SkScalerContextRec *rec, SkScalerContextEffects *effects)
const SkScalerContextRec & getRec() const
static void GenerateMetricsFromPath(SkGlyph *glyph, const SkPath &path, SkMask::Format format, bool verticalLCD, bool a8FromLCD, bool hairline)
static bool CheckBufferSizeForRec(const SkScalerContextRec &rec, const SkScalerContextEffects &effects, size_t size)
bool isSubpixel() const
SkScalerContextEffects getEffects() const
friend class PathText
SkAxisAlignment computeAxisAlignmentForHText() const
sk_sp< SkDrawable > getDrawable(SkGlyph &)
static bool GetGammaLUTData(SkScalar contrast, SkScalar paintGamma, SkScalar deviceGamma, uint8_t *data)
static std::unique_ptr< SkScalerContext > MakeEmpty(sk_sp< SkTypeface > typeface, const SkScalerContextEffects &effects, const SkDescriptor *desc)
bool isVertical() const
SkGlyph makeGlyph(SkPackedGlyphID, SkArenaAlloc *)
static void DescriptorBufferGiveRec(const SkScalerContextRec &rec, void *buffer)
bool isLinearMetrics() const
void forceOffGenerateImageFromPath()
void getFontMetrics(SkFontMetrics *)
void getImage(const SkGlyph &)
static void SaturateGlyphBounds(SkGlyph *glyph, SkRect &&)
void void void appendf(const char format[],...) SK_PRINTF_LIKE(2
Definition SkString.cpp:550
static constexpr SkScalar kMaxContrastInclusive
static constexpr SkScalar kMaxGammaExclusive
static constexpr SkScalar kMinGammaInclusive
static constexpr SkScalar kMinContrastInclusive
T * get() const
Definition SkRefCnt.h:303
const Paint & paint
float SkScalar
Definition extension.cpp:12
static const uint8_t buffer[]
uint32_t uint32_t * format
int32_t height
int32_t width
const Scalar scale
Format
Definition SkMask.h:26
SkScalerContextEffects(const SkPaint &paint)
SkScalerContextEffects(SkPathEffect *pe, SkMaskFilter *mf)
void setPaintGamma(SkScalar pg)
void getSingleMatrix(SkMatrix *) const
void getLocalMatrix(SkMatrix *) const
SkAxisAlignment computeAxisAlignmentForHText() const
void setDeviceGamma(SkScalar dg)
SkMask::Format fMaskFormat
void setContrast(SkScalar c)
SkMask::Format getFormat() const
void getMatrixFrom2x2(SkMatrix *) const
bool computeMatrices(PreMatrixScale preMatrixScale, SkVector *scale, SkMatrix *remaining, SkMatrix *remainingWithoutRotation=nullptr, SkMatrix *remainingRotation=nullptr, SkMatrix *total=nullptr)
SkScalar getPaintGamma() const
SkTypefaceID fTypefaceID
SkScalar getContrast() const
SkScalar getDeviceGamma() const
SkScalar fPost2x2[2][2]
SkColor getLuminanceColor() const
SkString dump() const
SkFontHinting getHinting() const
void setHinting(SkFontHinting)
void setLuminanceColor(SkColor c)
GlyphMetrics(SkMask::Format format)