1 /*
2  * Copyright (C) 2015 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 "Canvas.h"
18 
19 #include "MinikinUtils.h"
20 #include "Paint.h"
21 #include "Properties.h"
22 #include "RenderNode.h"
23 #include "Typeface.h"
24 #include "pipeline/skia/SkiaRecordingCanvas.h"
25 
26 #include "hwui/PaintFilter.h"
27 
28 #include <SkFontMetrics.h>
29 #include <SkRRect.h>
30 
31 namespace android {
32 
create_recording_canvas(int width,int height,uirenderer::RenderNode * renderNode)33 Canvas* Canvas::create_recording_canvas(int width, int height, uirenderer::RenderNode* renderNode) {
34     return new uirenderer::skiapipeline::SkiaRecordingCanvas(renderNode, width, height);
35 }
36 
drawStroke(SkScalar left,SkScalar right,SkScalar top,SkScalar thickness,const Paint & paint,Canvas * canvas)37 static inline void drawStroke(SkScalar left, SkScalar right, SkScalar top, SkScalar thickness,
38                               const Paint& paint, Canvas* canvas) {
39     const SkScalar strokeWidth = fmax(thickness, 1.0f);
40     const SkScalar bottom = top + strokeWidth;
41     canvas->drawRect(left, top, right, bottom, paint);
42 }
43 
drawTextDecorations(float x,float y,float length,const Paint & paint)44 void Canvas::drawTextDecorations(float x, float y, float length, const Paint& paint) {
45     // paint has already been filtered by our caller, so we can ignore any filter
46     const bool strikeThru = paint.isStrikeThru();
47     const bool underline = paint.isUnderline();
48     if (strikeThru || underline) {
49         const SkScalar left = x;
50         const SkScalar right = x + length;
51         const float textSize = paint.getSkFont().getSize();
52         if (underline) {
53             SkFontMetrics metrics;
54             paint.getSkFont().getMetrics(&metrics);
55             SkScalar position;
56             if (!metrics.hasUnderlinePosition(&position)) {
57                 position = textSize * Paint::kStdUnderline_Top;
58             }
59             SkScalar thickness;
60             if (!metrics.hasUnderlineThickness(&thickness)) {
61                 thickness = textSize * Paint::kStdUnderline_Thickness;
62             }
63             const SkScalar top = y + position;
64             drawStroke(left, right, top, thickness, paint, this);
65         }
66         if (strikeThru) {
67             const float position = textSize * Paint::kStdStrikeThru_Top;
68             const SkScalar thickness = textSize * Paint::kStdStrikeThru_Thickness;
69             const SkScalar top = y + position;
70             drawStroke(left, right, top, thickness, paint, this);
71         }
72     }
73 }
74 
simplifyPaint(int color,Paint * paint)75 static void simplifyPaint(int color, Paint* paint) {
76     paint->setColor(color);
77     paint->setShader(nullptr);
78     paint->setColorFilter(nullptr);
79     paint->setLooper(nullptr);
80     paint->setStrokeWidth(4 + 0.04 * paint->getSkFont().getSize());
81     paint->setStrokeJoin(SkPaint::kRound_Join);
82     paint->setLooper(nullptr);
83 }
84 
85 class DrawTextFunctor {
86 public:
DrawTextFunctor(const minikin::Layout & layout,Canvas * canvas,const Paint & paint,float x,float y,float totalAdvance)87     DrawTextFunctor(const minikin::Layout& layout, Canvas* canvas, const Paint& paint, float x,
88                     float y, float totalAdvance)
89             : layout(layout)
90             , canvas(canvas)
91             , paint(paint)
92             , x(x)
93             , y(y)
94             , totalAdvance(totalAdvance) {}
95 
operator ()(size_t start,size_t end)96     void operator()(size_t start, size_t end) {
97         auto glyphFunc = [&](uint16_t* text, float* positions) {
98             for (size_t i = start, textIndex = 0, posIndex = 0; i < end; i++) {
99                 text[textIndex++] = layout.getGlyphId(i);
100                 positions[posIndex++] = x + layout.getX(i);
101                 positions[posIndex++] = y + layout.getY(i);
102             }
103         };
104 
105         size_t glyphCount = end - start;
106 
107         if (CC_UNLIKELY(canvas->isHighContrastText() && paint.getAlpha() != 0)) {
108             // high contrast draw path
109             int color = paint.getColor();
110             int channelSum = SkColorGetR(color) + SkColorGetG(color) + SkColorGetB(color);
111             bool darken = channelSum < (128 * 3);
112 
113             // outline
114             gDrawTextBlobMode = DrawTextBlobMode::HctOutline;
115             Paint outlinePaint(paint);
116             simplifyPaint(darken ? SK_ColorWHITE : SK_ColorBLACK, &outlinePaint);
117             outlinePaint.setStyle(SkPaint::kStrokeAndFill_Style);
118             canvas->drawGlyphs(glyphFunc, glyphCount, outlinePaint, x, y, totalAdvance);
119 
120             // inner
121             gDrawTextBlobMode = DrawTextBlobMode::HctInner;
122             Paint innerPaint(paint);
123             simplifyPaint(darken ? SK_ColorBLACK : SK_ColorWHITE, &innerPaint);
124             innerPaint.setStyle(SkPaint::kFill_Style);
125             canvas->drawGlyphs(glyphFunc, glyphCount, innerPaint, x, y, totalAdvance);
126             gDrawTextBlobMode = DrawTextBlobMode::Normal;
127         } else {
128             // standard draw path
129             canvas->drawGlyphs(glyphFunc, glyphCount, paint, x, y, totalAdvance);
130         }
131     }
132 
133 private:
134     const minikin::Layout& layout;
135     Canvas* canvas;
136     const Paint& paint;
137     float x;
138     float y;
139     float totalAdvance;
140 };
141 
drawGlyphs(const minikin::Font & font,const int * glyphIds,const float * positions,int glyphCount,const Paint & paint)142 void Canvas::drawGlyphs(const minikin::Font& font, const int* glyphIds, const float* positions,
143                         int glyphCount, const Paint& paint) {
144     // Minikin modify skFont for auto-fakebold/auto-fakeitalic.
145     Paint copied(paint);
146 
147     auto glyphFunc = [&](uint16_t* outGlyphIds, float* outPositions) {
148         for (uint32_t i = 0; i < glyphCount; ++i) {
149             outGlyphIds[i] = static_cast<uint16_t>(glyphIds[i]);
150         }
151         memcpy(outPositions, positions, sizeof(float) * 2 * glyphCount);
152     };
153 
154     const minikin::MinikinFont* minikinFont = font.typeface().get();
155     SkFont* skfont = &copied.getSkFont();
156     MinikinFontSkia::populateSkFont(skfont, minikinFont, minikin::FontFakery());
157 
158     // total advance is used for drawing underline. We do not support underlyine by glyph drawing.
159     drawGlyphs(glyphFunc, glyphCount, copied, 0 /* x */, 0 /* y */, 0 /* total Advance */);
160 }
161 
drawText(const uint16_t * text,int textSize,int start,int count,int contextStart,int contextCount,float x,float y,minikin::Bidi bidiFlags,const Paint & origPaint,const Typeface * typeface,minikin::MeasuredText * mt)162 void Canvas::drawText(const uint16_t* text, int textSize, int start, int count, int contextStart,
163                       int contextCount, float x, float y, minikin::Bidi bidiFlags,
164                       const Paint& origPaint, const Typeface* typeface, minikin::MeasuredText* mt) {
165     // minikin may modify the original paint
166     Paint paint(origPaint);
167 
168     // interpret 'linear metrics' flag as 'linear', forcing no-hinting when drawing
169     if (paint.getSkFont().isLinearMetrics()) {
170         paint.getSkFont().setHinting(SkFontHinting::kNone);
171     }
172 
173     minikin::Layout layout = MinikinUtils::doLayout(&paint, bidiFlags, typeface, text, textSize,
174                                                     start, count, contextStart, contextCount, mt);
175 
176     x += MinikinUtils::xOffsetForTextAlign(&paint, layout);
177 
178     // Set align to left for drawing, as we don't want individual
179     // glyphs centered or right-aligned; the offset above takes
180     // care of all alignment.
181     paint.setTextAlign(Paint::kLeft_Align);
182 
183     DrawTextFunctor f(layout, this, paint, x, y, layout.getAdvance());
184     MinikinUtils::forFontRun(layout, &paint, f);
185 }
186 
drawDoubleRoundRectXY(float outerLeft,float outerTop,float outerRight,float outerBottom,float outerRx,float outerRy,float innerLeft,float innerTop,float innerRight,float innerBottom,float innerRx,float innerRy,const Paint & paint)187 void Canvas::drawDoubleRoundRectXY(float outerLeft, float outerTop, float outerRight,
188                             float outerBottom, float outerRx, float outerRy, float innerLeft,
189                             float innerTop, float innerRight, float innerBottom, float innerRx,
190                             float innerRy, const Paint& paint) {
191     if (CC_UNLIKELY(paint.nothingToDraw())) return;
192     SkRect outer = SkRect::MakeLTRB(outerLeft, outerTop, outerRight, outerBottom);
193     SkRect inner = SkRect::MakeLTRB(innerLeft, innerTop, innerRight, innerBottom);
194 
195     SkRRect outerRRect;
196     outerRRect.setRectXY(outer, outerRx, outerRy);
197 
198     SkRRect innerRRect;
199     innerRRect.setRectXY(inner, innerRx, innerRy);
200     drawDoubleRoundRect(outerRRect, innerRRect, paint);
201 }
202 
drawDoubleRoundRectRadii(float outerLeft,float outerTop,float outerRight,float outerBottom,const float * outerRadii,float innerLeft,float innerTop,float innerRight,float innerBottom,const float * innerRadii,const Paint & paint)203 void Canvas::drawDoubleRoundRectRadii(float outerLeft, float outerTop, float outerRight,
204                             float outerBottom, const float* outerRadii, float innerLeft,
205                             float innerTop, float innerRight, float innerBottom,
206                             const float* innerRadii, const Paint& paint) {
207     static_assert(sizeof(SkVector) == sizeof(float) * 2);
208     if (CC_UNLIKELY(paint.nothingToDraw())) return;
209     SkRect outer = SkRect::MakeLTRB(outerLeft, outerTop, outerRight, outerBottom);
210     SkRect inner = SkRect::MakeLTRB(innerLeft, innerTop, innerRight, innerBottom);
211 
212     SkRRect outerRRect;
213     const SkVector* outerSkVector = reinterpret_cast<const SkVector*>(outerRadii);
214     outerRRect.setRectRadii(outer, outerSkVector);
215 
216     SkRRect innerRRect;
217     const SkVector* innerSkVector = reinterpret_cast<const SkVector*>(innerRadii);
218     innerRRect.setRectRadii(inner, innerSkVector);
219     drawDoubleRoundRect(outerRRect, innerRRect, paint);
220 }
221 
222 class DrawTextOnPathFunctor {
223 public:
DrawTextOnPathFunctor(const minikin::Layout & layout,Canvas * canvas,float hOffset,float vOffset,const Paint & paint,const SkPath & path)224     DrawTextOnPathFunctor(const minikin::Layout& layout, Canvas* canvas, float hOffset,
225                           float vOffset, const Paint& paint, const SkPath& path)
226             : layout(layout)
227             , canvas(canvas)
228             , hOffset(hOffset)
229             , vOffset(vOffset)
230             , paint(paint)
231             , path(path) {}
232 
operator ()(size_t start,size_t end)233     void operator()(size_t start, size_t end) {
234         canvas->drawLayoutOnPath(layout, hOffset, vOffset, paint, path, start, end);
235     }
236 
237 private:
238     const minikin::Layout& layout;
239     Canvas* canvas;
240     float hOffset;
241     float vOffset;
242     const Paint& paint;
243     const SkPath& path;
244 };
245 
drawTextOnPath(const uint16_t * text,int count,minikin::Bidi bidiFlags,const SkPath & path,float hOffset,float vOffset,const Paint & origPaint,const Typeface * typeface)246 void Canvas::drawTextOnPath(const uint16_t* text, int count, minikin::Bidi bidiFlags,
247                             const SkPath& path, float hOffset, float vOffset,
248                             const Paint& origPaint, const Typeface* typeface) {
249     // minikin may modify the original paint
250     Paint paint(origPaint);
251 
252     // interpret 'linear metrics' flag as 'linear', forcing no-hinting when drawing
253     if (paint.getSkFont().isLinearMetrics()) {
254         paint.getSkFont().setHinting(SkFontHinting::kNone);
255     }
256 
257     minikin::Layout layout =
258             MinikinUtils::doLayout(&paint, bidiFlags, typeface, text, count,  // text buffer
259                                    0, count,                                  // draw range
260                                    0, count,                                  // context range
261                                    nullptr);
262     hOffset += MinikinUtils::hOffsetForTextAlign(&paint, layout, path);
263 
264     // Set align to left for drawing, as we don't want individual
265     // glyphs centered or right-aligned; the offset above takes
266     // care of all alignment.
267     paint.setTextAlign(Paint::kLeft_Align);
268 
269     DrawTextOnPathFunctor f(layout, this, hOffset, vOffset, paint, path);
270     MinikinUtils::forFontRun(layout, &paint, f);
271 }
272 
273 int Canvas::sApiLevel = 1;
274 
setCompatibilityVersion(int apiLevel)275 void Canvas::setCompatibilityVersion(int apiLevel) {
276     sApiLevel = apiLevel;
277 }
278 
279 }  // namespace android
280