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     if (text_feature::fix_double_underline()) {
114         Paint copied(paint);
115         PaintFilter* filter = getPaintFilter();
116         if (filter != nullptr) {
117             filter->filterFullPaint(&copied);
118         }
119         const bool isUnderline = copied.isUnderline();
120         const bool isStrikeThru = copied.isStrikeThru();
121         if (isUnderline || isStrikeThru) {
122             const SkScalar left = x;
123             const SkScalar right = x + layout.getAdvance();
124             if (isUnderline) {
125                 const SkScalar top = y + f.getUnderlinePosition();
126                 drawStroke(left, right, top, f.getUnderlineThickness(), copied, this);
127             }
128             if (isStrikeThru) {
129                 float textSize = paint.getSkFont().getSize();
130                 const float position = textSize * Paint::kStdStrikeThru_Top;
131                 const SkScalar thickness = textSize * Paint::kStdStrikeThru_Thickness;
132                 const SkScalar top = y + position;
133                 drawStroke(left, right, top, thickness, copied, this);
134             }
135         }
136     }
137 }
138 
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)139 void Canvas::drawDoubleRoundRectXY(float outerLeft, float outerTop, float outerRight,
140                             float outerBottom, float outerRx, float outerRy, float innerLeft,
141                             float innerTop, float innerRight, float innerBottom, float innerRx,
142                             float innerRy, const Paint& paint) {
143     if (CC_UNLIKELY(paint.nothingToDraw())) return;
144     SkRect outer = SkRect::MakeLTRB(outerLeft, outerTop, outerRight, outerBottom);
145     SkRect inner = SkRect::MakeLTRB(innerLeft, innerTop, innerRight, innerBottom);
146 
147     SkRRect outerRRect;
148     outerRRect.setRectXY(outer, outerRx, outerRy);
149 
150     SkRRect innerRRect;
151     innerRRect.setRectXY(inner, innerRx, innerRy);
152     drawDoubleRoundRect(outerRRect, innerRRect, paint);
153 }
154 
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)155 void Canvas::drawDoubleRoundRectRadii(float outerLeft, float outerTop, float outerRight,
156                             float outerBottom, const float* outerRadii, float innerLeft,
157                             float innerTop, float innerRight, float innerBottom,
158                             const float* innerRadii, const Paint& paint) {
159     static_assert(sizeof(SkVector) == sizeof(float) * 2);
160     if (CC_UNLIKELY(paint.nothingToDraw())) return;
161     SkRect outer = SkRect::MakeLTRB(outerLeft, outerTop, outerRight, outerBottom);
162     SkRect inner = SkRect::MakeLTRB(innerLeft, innerTop, innerRight, innerBottom);
163 
164     SkRRect outerRRect;
165     const SkVector* outerSkVector = reinterpret_cast<const SkVector*>(outerRadii);
166     outerRRect.setRectRadii(outer, outerSkVector);
167 
168     SkRRect innerRRect;
169     const SkVector* innerSkVector = reinterpret_cast<const SkVector*>(innerRadii);
170     innerRRect.setRectRadii(inner, innerSkVector);
171     drawDoubleRoundRect(outerRRect, innerRRect, paint);
172 }
173 
174 class DrawTextOnPathFunctor {
175 public:
DrawTextOnPathFunctor(const minikin::Layout & layout,Canvas * canvas,float hOffset,float vOffset,const Paint & paint,const SkPath & path)176     DrawTextOnPathFunctor(const minikin::Layout& layout, Canvas* canvas, float hOffset,
177                           float vOffset, const Paint& paint, const SkPath& path)
178             : layout(layout)
179             , canvas(canvas)
180             , hOffset(hOffset)
181             , vOffset(vOffset)
182             , paint(paint)
183             , path(path) {}
184 
operator ()(size_t start,size_t end)185     void operator()(size_t start, size_t end) {
186         canvas->drawLayoutOnPath(layout, hOffset, vOffset, paint, path, start, end);
187     }
188 
189 private:
190     const minikin::Layout& layout;
191     Canvas* canvas;
192     float hOffset;
193     float vOffset;
194     const Paint& paint;
195     const SkPath& path;
196 };
197 
drawTextOnPath(const uint16_t * text,int count,minikin::Bidi bidiFlags,const SkPath & path,float hOffset,float vOffset,const Paint & origPaint,const Typeface * typeface)198 void Canvas::drawTextOnPath(const uint16_t* text, int count, minikin::Bidi bidiFlags,
199                             const SkPath& path, float hOffset, float vOffset,
200                             const Paint& origPaint, const Typeface* typeface) {
201     // minikin may modify the original paint
202     Paint paint(origPaint);
203 
204     // interpret 'linear metrics' flag as 'linear', forcing no-hinting when drawing
205     if (paint.getSkFont().isLinearMetrics()) {
206         paint.getSkFont().setHinting(SkFontHinting::kNone);
207     }
208 
209     minikin::Layout layout =
210             MinikinUtils::doLayout(&paint, bidiFlags, typeface, text, count,  // text buffer
211                                    0, count,                                  // draw range
212                                    0, count,                                  // context range
213                                    nullptr);
214     hOffset += MinikinUtils::hOffsetForTextAlign(&paint, layout, path);
215 
216     // Set align to left for drawing, as we don't want individual
217     // glyphs centered or right-aligned; the offset above takes
218     // care of all alignment.
219     paint.setTextAlign(Paint::kLeft_Align);
220 
221     DrawTextOnPathFunctor f(layout, this, hOffset, vOffset, paint, path);
222     MinikinUtils::forFontRun(layout, &paint, f);
223 }
224 
225 int Canvas::sApiLevel = 1;
226 
setCompatibilityVersion(int apiLevel)227 void Canvas::setCompatibilityVersion(int apiLevel) {
228     sApiLevel = apiLevel;
229 }
230 
231 }  // namespace android
232