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