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,minikin::MinikinRect & bounds,float totalAdvance)86 DrawTextFunctor(const minikin::Layout& layout, Canvas* canvas, const Paint& paint, float x,
87 float y, minikin::MinikinRect& bounds, float totalAdvance)
88 : layout(layout)
89 , canvas(canvas)
90 , paint(paint)
91 , x(x)
92 , y(y)
93 , bounds(bounds)
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 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, bounds.mLeft, bounds.mTop,
118 bounds.mRight, bounds.mBottom, totalAdvance);
119
120 // inner
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, bounds.mLeft, bounds.mTop,
125 bounds.mRight, bounds.mBottom, totalAdvance);
126 } else {
127 // standard draw path
128 canvas->drawGlyphs(glyphFunc, glyphCount, paint, x, y, bounds.mLeft, bounds.mTop,
129 bounds.mRight, bounds.mBottom, 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 minikin::MinikinRect& bounds;
140 float totalAdvance;
141 };
142
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)143 void Canvas::drawText(const uint16_t* text, int textSize, int start, int count, int contextStart,
144 int contextCount, float x, float y, minikin::Bidi bidiFlags,
145 const Paint& origPaint, const Typeface* typeface, minikin::MeasuredText* mt) {
146 // minikin may modify the original paint
147 Paint paint(origPaint);
148
149 // interpret 'linear metrics' flag as 'linear', forcing no-hinting when drawing
150 if (paint.getSkFont().isLinearMetrics()) {
151 paint.getSkFont().setHinting(SkFontHinting::kNone);
152 }
153
154 minikin::Layout layout = MinikinUtils::doLayout(&paint, bidiFlags, typeface, text, textSize,
155 start, count, contextStart, contextCount, mt);
156
157 x += MinikinUtils::xOffsetForTextAlign(&paint, layout);
158
159 minikin::MinikinRect bounds;
160 layout.getBounds(&bounds);
161
162 // Set align to left for drawing, as we don't want individual
163 // glyphs centered or right-aligned; the offset above takes
164 // care of all alignment.
165 paint.setTextAlign(Paint::kLeft_Align);
166
167 DrawTextFunctor f(layout, this, paint, x, y, bounds, layout.getAdvance());
168 MinikinUtils::forFontRun(layout, &paint, f);
169 }
170
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)171 void Canvas::drawDoubleRoundRectXY(float outerLeft, float outerTop, float outerRight,
172 float outerBottom, float outerRx, float outerRy, float innerLeft,
173 float innerTop, float innerRight, float innerBottom, float innerRx,
174 float innerRy, const Paint& paint) {
175 if (CC_UNLIKELY(paint.nothingToDraw())) return;
176 SkRect outer = SkRect::MakeLTRB(outerLeft, outerTop, outerRight, outerBottom);
177 SkRect inner = SkRect::MakeLTRB(innerLeft, innerTop, innerRight, innerBottom);
178
179 SkRRect outerRRect;
180 outerRRect.setRectXY(outer, outerRx, outerRy);
181
182 SkRRect innerRRect;
183 innerRRect.setRectXY(inner, innerRx, innerRy);
184 drawDoubleRoundRect(outerRRect, innerRRect, paint);
185 }
186
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)187 void Canvas::drawDoubleRoundRectRadii(float outerLeft, float outerTop, float outerRight,
188 float outerBottom, const float* outerRadii, float innerLeft,
189 float innerTop, float innerRight, float innerBottom,
190 const float* innerRadii, const Paint& paint) {
191 static_assert(sizeof(SkVector) == sizeof(float) * 2);
192 if (CC_UNLIKELY(paint.nothingToDraw())) return;
193 SkRect outer = SkRect::MakeLTRB(outerLeft, outerTop, outerRight, outerBottom);
194 SkRect inner = SkRect::MakeLTRB(innerLeft, innerTop, innerRight, innerBottom);
195
196 SkRRect outerRRect;
197 const SkVector* outerSkVector = reinterpret_cast<const SkVector*>(outerRadii);
198 outerRRect.setRectRadii(outer, outerSkVector);
199
200 SkRRect innerRRect;
201 const SkVector* innerSkVector = reinterpret_cast<const SkVector*>(innerRadii);
202 innerRRect.setRectRadii(inner, innerSkVector);
203 drawDoubleRoundRect(outerRRect, innerRRect, paint);
204 }
205
206 class DrawTextOnPathFunctor {
207 public:
DrawTextOnPathFunctor(const minikin::Layout & layout,Canvas * canvas,float hOffset,float vOffset,const Paint & paint,const SkPath & path)208 DrawTextOnPathFunctor(const minikin::Layout& layout, Canvas* canvas, float hOffset,
209 float vOffset, const Paint& paint, const SkPath& path)
210 : layout(layout)
211 , canvas(canvas)
212 , hOffset(hOffset)
213 , vOffset(vOffset)
214 , paint(paint)
215 , path(path) {}
216
operator ()(size_t start,size_t end)217 void operator()(size_t start, size_t end) {
218 canvas->drawLayoutOnPath(layout, hOffset, vOffset, paint, path, start, end);
219 }
220
221 private:
222 const minikin::Layout& layout;
223 Canvas* canvas;
224 float hOffset;
225 float vOffset;
226 const Paint& paint;
227 const SkPath& path;
228 };
229
drawTextOnPath(const uint16_t * text,int count,minikin::Bidi bidiFlags,const SkPath & path,float hOffset,float vOffset,const Paint & origPaint,const Typeface * typeface)230 void Canvas::drawTextOnPath(const uint16_t* text, int count, minikin::Bidi bidiFlags,
231 const SkPath& path, float hOffset, float vOffset,
232 const Paint& origPaint, const Typeface* typeface) {
233 // minikin may modify the original paint
234 Paint paint(origPaint);
235
236 // interpret 'linear metrics' flag as 'linear', forcing no-hinting when drawing
237 if (paint.getSkFont().isLinearMetrics()) {
238 paint.getSkFont().setHinting(SkFontHinting::kNone);
239 }
240
241 minikin::Layout layout =
242 MinikinUtils::doLayout(&paint, bidiFlags, typeface, text, count, // text buffer
243 0, count, // draw range
244 0, count, // context range
245 nullptr);
246 hOffset += MinikinUtils::hOffsetForTextAlign(&paint, layout, path);
247
248 // Set align to left for drawing, as we don't want individual
249 // glyphs centered or right-aligned; the offset above takes
250 // care of all alignment.
251 paint.setTextAlign(Paint::kLeft_Align);
252
253 DrawTextOnPathFunctor f(layout, this, hOffset, vOffset, paint, path);
254 MinikinUtils::forFontRun(layout, &paint, f);
255 }
256
257 int Canvas::sApiLevel = 1;
258
setCompatibilityVersion(int apiLevel)259 void Canvas::setCompatibilityVersion(int apiLevel) {
260 sApiLevel = apiLevel;
261 }
262
263 } // namespace android
264