Update rive-cpp to 2.0 version
[platform/core/uifw/rive-tizen.git] / submodule / skia / src / text / gpu / SDFTControl.cpp
1 /*
2  * Copyright 2020 Google Inc.
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 #include "src/text/gpu/SDFTControl.h"
9
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"
18
19 #include <tuple>
20
21 namespace sktext::gpu {
22
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;
31 #endif
32
33 SkScalar SDFTControl::MinSDFTRange(bool useSDFTForSmallText, SkScalar min) {
34     if (!useSDFTForSmallText) {
35         return kLargeDFFontLimit;
36     }
37     return min;
38 }
39
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);
46 }
47
48 bool SDFTControl::isDirect(SkScalar approximateDeviceTextSize, const SkPaint& paint) const {
49     return !isSDFT(approximateDeviceTextSize, paint) &&
50             approximateDeviceTextSize < SkStrikeCommon::kSkSideTooBigForAtlas;
51 }
52
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;
59 }
60
61 SkScalar scaled_text_size(const SkScalar textSize, const SkMatrix& viewMatrix) {
62     SkScalar scaledTextSize = textSize;
63
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;
68     } else {
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;
75         }
76     }
77
78     return scaledTextSize;
79 }
80
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);
85
86     SkFont dfFont{font};
87
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;
100     } else {
101         dfMaskScaleFloor = kLargeDFFontLimit;
102         dfMaskScaleCeil = kExtraLargeDFFontLimit;
103     }
104 #else
105     } else {
106         dfMaskScaleFloor = kMediumDFFontLimit;
107         dfMaskScaleCeil = kLargeDFFontLimit;
108     }
109 #endif
110
111     dfFont.setSize(SkIntToScalar(dfMaskScaleCeil));
112     dfFont.setEdging(SkFont::Edging::kAntiAlias);
113     dfFont.setForceAutoHinting(false);
114     dfFont.setHinting(SkFontHinting::kNormal);
115
116     // The sub-pixel position will always happen when transforming to the screen.
117     dfFont.setSubpixel(false);
118
119     SkScalar minMatrixScale = dfMaskScaleFloor / textSize,
120              maxMatrixScale = dfMaskScaleCeil  / textSize;
121     return {dfFont, textSize / dfMaskScaleCeil, {minMatrixScale, maxMatrixScale}};
122 }
123
124 bool SDFTMatrixRange::matrixInRange(const SkMatrix& matrix) const {
125     SkScalar maxScale = matrix.getMaxScale();
126     return fMatrixMin < maxScale && maxScale <= fMatrixMax;
127 }
128
129 void SDFTMatrixRange::flatten(SkWriteBuffer& buffer) const {
130     buffer.writeScalar(fMatrixMin);
131     buffer.writeScalar(fMatrixMax);
132 }
133
134 SDFTMatrixRange SDFTMatrixRange::MakeFromBuffer(SkReadBuffer& buffer) {
135     SkScalar min = buffer.readScalar();
136     SkScalar max = buffer.readScalar();
137     return SDFTMatrixRange{min, max};
138 }
139
140 }  // namespace sktext::gpu