2 * Copyright 2020 Google Inc.
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
8 #include "src/text/gpu/SDFTControl.h"
10 #include "include/core/SkFont.h"
11 #include "include/core/SkGraphics.h"
12 #include "include/core/SkMatrix.h"
13 #include "include/core/SkPaint.h"
14 #include "include/core/SkScalar.h"
15 #include "include/core/SkSurfaceProps.h"
16 #include "src/core/SkGlyphRunPainter.h"
17 #include "src/core/SkReadBuffer.h"
21 namespace sktext::gpu {
23 // DF sizes and thresholds for usage of the small and medium sizes. For example, above
24 // kSmallDFFontLimit we will use the medium size. The large size is used up until the size at
25 // which we switch over to drawing as paths as controlled by Control.
26 static const int kSmallDFFontLimit = 32;
27 static const int kMediumDFFontLimit = 72;
28 static const int kLargeDFFontLimit = 162;
29 #ifdef SK_BUILD_FOR_MAC
30 static const int kExtraLargeDFFontLimit = 256;
33 SkScalar SDFTControl::MinSDFTRange(bool useSDFTForSmallText, SkScalar min) {
34 if (!useSDFTForSmallText) {
35 return kLargeDFFontLimit;
40 SDFTControl::SDFTControl(
41 bool ableToUseSDFT, bool useSDFTForSmallText, SkScalar min, SkScalar max)
42 : fMinDistanceFieldFontSize{MinSDFTRange(useSDFTForSmallText, min)}
43 , fMaxDistanceFieldFontSize{max}
44 , fAbleToUseSDFT{ableToUseSDFT} {
45 SkASSERT_RELEASE(0 < min && min <= max);
48 bool SDFTControl::isDirect(SkScalar approximateDeviceTextSize, const SkPaint& paint) const {
49 return !isSDFT(approximateDeviceTextSize, paint) &&
50 approximateDeviceTextSize < SkStrikeCommon::kSkSideTooBigForAtlas;
53 bool SDFTControl::isSDFT(SkScalar approximateDeviceTextSize, const SkPaint& paint) const {
54 return fAbleToUseSDFT &&
55 paint.getMaskFilter() == nullptr &&
56 paint.getStyle() == SkPaint::kFill_Style &&
57 fMinDistanceFieldFontSize <= approximateDeviceTextSize &&
58 approximateDeviceTextSize <= fMaxDistanceFieldFontSize;
61 SkScalar scaled_text_size(const SkScalar textSize, const SkMatrix& viewMatrix) {
62 SkScalar scaledTextSize = textSize;
64 if (viewMatrix.hasPerspective()) {
65 // for perspective, we simply force to the medium size
66 // TODO: compute a size based on approximate screen area
67 scaledTextSize = kMediumDFFontLimit;
69 SkScalar maxScale = viewMatrix.getMaxScale();
70 // if we have non-unity scale, we need to choose our base text size
71 // based on the SkPaint's text size multiplied by the max scale factor
72 // TODO: do we need to do this if we're scaling down (i.e. maxScale < 1)?
73 if (maxScale > 0 && !SkScalarNearlyEqual(maxScale, SK_Scalar1)) {
74 scaledTextSize *= maxScale;
78 return scaledTextSize;
81 std::tuple<SkFont, SkScalar, SDFTMatrixRange>
82 SDFTControl::getSDFFont(const SkFont& font, const SkMatrix& viewMatrix) const {
83 SkScalar textSize = font.getSize();
84 SkScalar scaledTextSize = scaled_text_size(textSize, viewMatrix);
88 SkScalar dfMaskScaleFloor;
89 SkScalar dfMaskScaleCeil;
90 if (scaledTextSize <= kSmallDFFontLimit) {
91 dfMaskScaleFloor = fMinDistanceFieldFontSize;
92 dfMaskScaleCeil = kSmallDFFontLimit;
93 } else if (scaledTextSize <= kMediumDFFontLimit) {
94 dfMaskScaleFloor = kSmallDFFontLimit;
95 dfMaskScaleCeil = kMediumDFFontLimit;
96 #ifdef SK_BUILD_FOR_MAC
97 } else if (scaledTextSize <= kLargeDFFontLimit) {
98 dfMaskScaleFloor = kMediumDFFontLimit;
99 dfMaskScaleCeil = kLargeDFFontLimit;
101 dfMaskScaleFloor = kLargeDFFontLimit;
102 dfMaskScaleCeil = kExtraLargeDFFontLimit;
106 dfMaskScaleFloor = kMediumDFFontLimit;
107 dfMaskScaleCeil = kLargeDFFontLimit;
111 dfFont.setSize(SkIntToScalar(dfMaskScaleCeil));
112 dfFont.setEdging(SkFont::Edging::kAntiAlias);
113 dfFont.setForceAutoHinting(false);
114 dfFont.setHinting(SkFontHinting::kNormal);
116 // The sub-pixel position will always happen when transforming to the screen.
117 dfFont.setSubpixel(false);
119 SkScalar minMatrixScale = dfMaskScaleFloor / textSize,
120 maxMatrixScale = dfMaskScaleCeil / textSize;
121 return {dfFont, textSize / dfMaskScaleCeil, {minMatrixScale, maxMatrixScale}};
124 bool SDFTMatrixRange::matrixInRange(const SkMatrix& matrix) const {
125 SkScalar maxScale = matrix.getMaxScale();
126 return fMatrixMin < maxScale && maxScale <= fMatrixMax;
129 void SDFTMatrixRange::flatten(SkWriteBuffer& buffer) const {
130 buffer.writeScalar(fMatrixMin);
131 buffer.writeScalar(fMatrixMax);
134 SDFTMatrixRange SDFTMatrixRange::MakeFromBuffer(SkReadBuffer& buffer) {
135 SkScalar min = buffer.readScalar();
136 SkScalar max = buffer.readScalar();
137 return SDFTMatrixRange{min, max};
140 } // namespace sktext::gpu