xref: /aosp_15_r20/frameworks/base/libs/hwui/hwui/DrawTextFunctor.h (revision d57664e9bc4670b3ecf6748a746a57c557b6bc9e)
1 /*
2  * Copyright (C) 2023 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include <SkFontMetrics.h>
18 #include <SkRRect.h>
19 #include <SkTextBlob.h>
20 
21 #include "../utils/Color.h"
22 #include "Canvas.h"
23 #include "FeatureFlags.h"
24 #include "MinikinUtils.h"
25 #include "Paint.h"
26 #include "Properties.h"
27 #include "RenderNode.h"
28 #include "Typeface.h"
29 #include "hwui/PaintFilter.h"
30 #include "pipeline/skia/SkiaRecordingCanvas.h"
31 
32 #ifdef __ANDROID__
33 #include <com_android_graphics_hwui_flags.h>
34 namespace flags = com::android::graphics::hwui::flags;
35 #else
36 namespace flags {
high_contrast_text_luminance()37 constexpr bool high_contrast_text_luminance() {
38     return false;
39 }
high_contrast_text_small_text_rect()40 constexpr bool high_contrast_text_small_text_rect() {
41     return false;
42 }
43 }  // namespace flags
44 #endif
45 
46 namespace android {
47 
48 // These should match the constants in framework/base/core/java/android/text/Layout.java
49 inline constexpr float kHighContrastTextBorderWidth = 4.0f;
50 inline constexpr float kHighContrastTextBorderWidthFactor = 0.2f;
51 
drawStroke(SkScalar left,SkScalar right,SkScalar top,SkScalar thickness,const Paint & paint,Canvas * canvas)52 static inline void drawStroke(SkScalar left, SkScalar right, SkScalar top, SkScalar thickness,
53                               const Paint& paint, Canvas* canvas) {
54     const SkScalar strokeWidth = fmax(thickness, 1.0f);
55     const SkScalar bottom = top + strokeWidth;
56     canvas->drawRect(left, top, right, bottom, paint);
57 }
58 
simplifyPaint(int color,Paint * paint)59 static void simplifyPaint(int color, Paint* paint) {
60     paint->setColor(color);
61     paint->setShader(nullptr);
62     paint->setColorFilter(nullptr);
63     paint->setLooper(nullptr);
64 
65     if (flags::high_contrast_text_small_text_rect()) {
66         paint->setStrokeWidth(
67                 std::max(kHighContrastTextBorderWidth,
68                          kHighContrastTextBorderWidthFactor * paint->getSkFont().getSize()));
69     } else {
70         auto borderWidthFactor = 0.04f;
71         paint->setStrokeWidth(kHighContrastTextBorderWidth +
72                               borderWidthFactor * paint->getSkFont().getSize());
73     }
74     paint->setStrokeJoin(SkPaint::kRound_Join);
75     paint->setLooper(nullptr);
76     paint->setBlendMode(SkBlendMode::kSrcOver);
77 }
78 
79 class DrawTextFunctor {
80 public:
81     /**
82      * Creates a Functor to draw the given text layout.
83      *
84      * @param layout
85      * @param canvas
86      * @param paint
87      * @param x
88      * @param y
89      * @param totalAdvance
90      * @param bounds bounds of the text. Only required if high contrast text mode is enabled.
91      */
DrawTextFunctor(const minikin::Layout & layout,Canvas * canvas,const Paint & paint,float x,float y,float totalAdvance)92     DrawTextFunctor(const minikin::Layout& layout, Canvas* canvas, const Paint& paint, float x,
93                     float y, float totalAdvance)
94             : layout(layout)
95             , canvas(canvas)
96             , paint(paint)
97             , x(x)
98             , y(y)
99             , totalAdvance(totalAdvance)
100             , underlinePosition(0)
101             , underlineThickness(0) {}
102 
operator()103     void operator()(size_t start, size_t end) {
104         auto glyphFunc = [&](uint16_t* text, float* positions) {
105             for (size_t i = start, textIndex = 0, posIndex = 0; i < end; i++) {
106                 text[textIndex++] = layout.getGlyphId(i);
107                 positions[posIndex++] = x + layout.getX(i);
108                 positions[posIndex++] = y + layout.getY(i);
109             }
110         };
111 
112         size_t glyphCount = end - start;
113 
114         if (CC_UNLIKELY(canvas->isHighContrastText() && paint.getAlpha() != 0)) {
115             // high contrast draw path
116             int color = paint.getColor();
117             bool darken;
118             // This equation should match the one in core/java/android/text/Layout.java
119             if (flags::high_contrast_text_luminance()) {
120                 uirenderer::Lab lab = uirenderer::sRGBToLab(color);
121                 darken = lab.L <= 50;
122             } else {
123                 int channelSum = SkColorGetR(color) + SkColorGetG(color) + SkColorGetB(color);
124                 darken = channelSum < (128 * 3);
125             }
126 
127             // outline
128             gDrawTextBlobMode = DrawTextBlobMode::HctOutline;
129             Paint outlinePaint(paint);
130             simplifyPaint(darken ? SK_ColorWHITE : SK_ColorBLACK, &outlinePaint);
131             outlinePaint.setStyle(SkPaint::kStrokeAndFill_Style);
132             canvas->drawGlyphs(glyphFunc, glyphCount, outlinePaint, x, y, totalAdvance);
133 
134             // inner
135             gDrawTextBlobMode = DrawTextBlobMode::HctInner;
136             Paint innerPaint(paint);
137             simplifyPaint(darken ? SK_ColorBLACK : SK_ColorWHITE, &innerPaint);
138             innerPaint.setStyle(SkPaint::kFill_Style);
139             canvas->drawGlyphs(glyphFunc, glyphCount, innerPaint, x, y, totalAdvance);
140             gDrawTextBlobMode = DrawTextBlobMode::Normal;
141         } else {
142             // standard draw path
143             canvas->drawGlyphs(glyphFunc, glyphCount, paint, x, y, totalAdvance);
144         }
145 
146         // Extract underline position and thickness.
147         if (paint.isUnderline()) {
148             SkFontMetrics metrics;
149             paint.getSkFont().getMetrics(&metrics);
150             const float textSize = paint.getSkFont().getSize();
151             SkScalar position;
152             if (!metrics.hasUnderlinePosition(&position)) {
153                 position = textSize * Paint::kStdUnderline_Top;
154             }
155             SkScalar thickness;
156             if (!metrics.hasUnderlineThickness(&thickness)) {
157                 thickness = textSize * Paint::kStdUnderline_Thickness;
158             }
159 
160             // If multiple fonts are used, use the most bottom position and most thick stroke
161             // width as the underline position. This follows the CSS standard:
162             // https://www.w3.org/TR/css-text-decor-3/#text-underline-position-property
163             // <quote>
164             // The exact position and thickness of line decorations is UA-defined in this level.
165             // However, for underlines and overlines the UA must use a single thickness and
166             // position on each line for the decorations deriving from a single decorating box.
167             // </quote>
168             underlinePosition = std::max(underlinePosition, position);
169             underlineThickness = std::max(underlineThickness, thickness);
170         }
171     }
172 
getUnderlinePosition()173     float getUnderlinePosition() const { return underlinePosition; }
getUnderlineThickness()174     float getUnderlineThickness() const { return underlineThickness; }
175 
176 private:
177     const minikin::Layout& layout;
178     Canvas* canvas;
179     const Paint& paint;
180     float x;
181     float y;
182     float totalAdvance;
183     float underlinePosition;
184     float underlineThickness;
185 };
186 
187 }  // namespace android
188