Canvas.cpp revision afc221499d943386256feb9db46c119ff834bf79
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 "RecordingCanvas.h" 20#include "MinikinUtils.h" 21#include "Paint.h" 22#include "Typeface.h" 23 24#include <SkDrawFilter.h> 25 26namespace android { 27 28Canvas* Canvas::create_recording_canvas(int width, int height) { 29 return new uirenderer::RecordingCanvas(width, height); 30} 31 32void Canvas::drawTextDecorations(float x, float y, float length, const SkPaint& paint) { 33 uint32_t flags; 34 SkDrawFilter* drawFilter = getDrawFilter(); 35 if (drawFilter) { 36 SkPaint paintCopy(paint); 37 drawFilter->filter(&paintCopy, SkDrawFilter::kText_Type); 38 flags = paintCopy.getFlags(); 39 } else { 40 flags = paint.getFlags(); 41 } 42 if (flags & (SkPaint::kUnderlineText_Flag | SkPaint::kStrikeThruText_Flag)) { 43 // Same values used by Skia 44 const float kStdStrikeThru_Offset = (-6.0f / 21.0f); 45 const float kStdUnderline_Offset = (1.0f / 9.0f); 46 const float kStdUnderline_Thickness = (1.0f / 18.0f); 47 48 SkScalar left = x; 49 SkScalar right = x + length; 50 float textSize = paint.getTextSize(); 51 float strokeWidth = fmax(textSize * kStdUnderline_Thickness, 1.0f); 52 if (flags & SkPaint::kUnderlineText_Flag) { 53 SkScalar top = y + textSize * kStdUnderline_Offset - 0.5f * strokeWidth; 54 SkScalar bottom = y + textSize * kStdUnderline_Offset + 0.5f * strokeWidth; 55 drawRect(left, top, right, bottom, paint); 56 } 57 if (flags & SkPaint::kStrikeThruText_Flag) { 58 SkScalar top = y + textSize * kStdStrikeThru_Offset - 0.5f * strokeWidth; 59 SkScalar bottom = y + textSize * kStdStrikeThru_Offset + 0.5f * strokeWidth; 60 drawRect(left, top, right, bottom, paint); 61 } 62 } 63} 64 65static void simplifyPaint(int color, SkPaint* paint) { 66 paint->setColor(color); 67 paint->setShader(nullptr); 68 paint->setColorFilter(nullptr); 69 paint->setLooper(nullptr); 70 paint->setStrokeWidth(4 + 0.04 * paint->getTextSize()); 71 paint->setStrokeJoin(SkPaint::kRound_Join); 72 paint->setLooper(nullptr); 73} 74 75class DrawTextFunctor { 76public: 77 DrawTextFunctor(const minikin::Layout& layout, Canvas* canvas, uint16_t* glyphs, float* pos, 78 const SkPaint& paint, float x, float y, minikin::MinikinRect& bounds, 79 float totalAdvance) 80 : layout(layout) 81 , canvas(canvas) 82 , glyphs(glyphs) 83 , pos(pos) 84 , paint(paint) 85 , x(x) 86 , y(y) 87 , bounds(bounds) 88 , totalAdvance(totalAdvance) { 89 } 90 91 void operator()(size_t start, size_t end) { 92 if (canvas->drawTextAbsolutePos()) { 93 for (size_t i = start; i < end; i++) { 94 glyphs[i] = layout.getGlyphId(i); 95 pos[2 * i] = x + layout.getX(i); 96 pos[2 * i + 1] = y + layout.getY(i); 97 } 98 } else { 99 for (size_t i = start; i < end; i++) { 100 glyphs[i] = layout.getGlyphId(i); 101 pos[2 * i] = layout.getX(i); 102 pos[2 * i + 1] = layout.getY(i); 103 } 104 } 105 106 size_t glyphCount = end - start; 107 108 if (CC_UNLIKELY(canvas->isHighContrastText() && paint.getAlpha() != 0)) { 109 // high contrast draw path 110 int color = paint.getColor(); 111 int channelSum = SkColorGetR(color) + SkColorGetG(color) + SkColorGetB(color); 112 bool darken = channelSum < (128 * 3); 113 114 // outline 115 SkPaint outlinePaint(paint); 116 simplifyPaint(darken ? SK_ColorWHITE : SK_ColorBLACK, &outlinePaint); 117 outlinePaint.setStyle(SkPaint::kStrokeAndFill_Style); 118 canvas->drawGlyphs(glyphs + start, pos + (2 * start), glyphCount, outlinePaint, x, y, 119 bounds.mLeft, bounds.mTop, bounds.mRight, bounds.mBottom, totalAdvance); 120 121 // inner 122 SkPaint innerPaint(paint); 123 simplifyPaint(darken ? SK_ColorBLACK : SK_ColorWHITE, &innerPaint); 124 innerPaint.setStyle(SkPaint::kFill_Style); 125 canvas->drawGlyphs(glyphs + start, pos + (2 * start), glyphCount, innerPaint, x, y, 126 bounds.mLeft, bounds.mTop, bounds.mRight, bounds.mBottom, totalAdvance); 127 } else { 128 // standard draw path 129 canvas->drawGlyphs(glyphs + start, pos + (2 * start), glyphCount, paint, x, y, 130 bounds.mLeft, bounds.mTop, bounds.mRight, bounds.mBottom, totalAdvance); 131 } 132 } 133private: 134 const minikin::Layout& layout; 135 Canvas* canvas; 136 uint16_t* glyphs; 137 float* pos; 138 const SkPaint& paint; 139 float x; 140 float y; 141 minikin::MinikinRect& bounds; 142 float totalAdvance; 143}; 144 145void Canvas::drawText(const uint16_t* text, int start, int count, int contextCount, 146 float x, float y, int bidiFlags, const Paint& origPaint, Typeface* typeface) { 147 // minikin may modify the original paint 148 Paint paint(origPaint); 149 150 minikin::Layout layout; 151 MinikinUtils::doLayout(&layout, &paint, bidiFlags, typeface, text, start, count, contextCount); 152 153 size_t nGlyphs = layout.nGlyphs(); 154 std::unique_ptr<uint16_t[]> glyphs(new uint16_t[nGlyphs]); 155 std::unique_ptr<float[]> pos(new float[nGlyphs * 2]); 156 157 x += MinikinUtils::xOffsetForTextAlign(&paint, layout); 158 159 minikin::MinikinRect bounds; 160 layout.getBounds(&bounds); 161 if (!drawTextAbsolutePos()) { 162 bounds.offset(x, y); 163 } 164 165 // Set align to left for drawing, as we don't want individual 166 // glyphs centered or right-aligned; the offset above takes 167 // care of all alignment. 168 paint.setTextAlign(Paint::kLeft_Align); 169 170 DrawTextFunctor f(layout, this, glyphs.get(), pos.get(), 171 paint, x, y, bounds, layout.getAdvance()); 172 MinikinUtils::forFontRun(layout, &paint, f); 173} 174 175class DrawTextOnPathFunctor { 176public: 177 DrawTextOnPathFunctor(const minikin::Layout& layout, Canvas* canvas, float hOffset, 178 float vOffset, const Paint& paint, const SkPath& path) 179 : layout(layout) 180 , canvas(canvas) 181 , hOffset(hOffset) 182 , vOffset(vOffset) 183 , paint(paint) 184 , path(path) { 185 } 186 187 void operator()(size_t start, size_t end) { 188 canvas->drawLayoutOnPath(layout, hOffset, vOffset, paint, path, start, end); 189 } 190private: 191 const minikin::Layout& layout; 192 Canvas* canvas; 193 float hOffset; 194 float vOffset; 195 const Paint& paint; 196 const SkPath& path; 197}; 198 199void Canvas::drawTextOnPath(const uint16_t* text, int count, int bidiFlags, const SkPath& path, 200 float hOffset, float vOffset, const Paint& paint, Typeface* typeface) { 201 Paint paintCopy(paint); 202 minikin::Layout layout; 203 MinikinUtils::doLayout(&layout, &paintCopy, bidiFlags, typeface, text, 0, count, count); 204 hOffset += MinikinUtils::hOffsetForTextAlign(&paintCopy, layout, path); 205 206 // Set align to left for drawing, as we don't want individual 207 // glyphs centered or right-aligned; the offset above takes 208 // care of all alignment. 209 paintCopy.setTextAlign(Paint::kLeft_Align); 210 211 DrawTextOnPathFunctor f(layout, this, hOffset, vOffset, paintCopy, path); 212 MinikinUtils::forFontRun(layout, &paintCopy, f); 213} 214 215} // namespace android 216