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