| 1 |
/* |
| 2 |
* Copyright 2011 Google Inc. |
| 3 |
* |
| 4 |
* Use of this source code is governed by a BSD-style license that can be |
| 5 |
* found in the LICENSE file. |
| 6 |
*/ |
| 7 |
|
| 8 |
#include "gm/gm.h" |
| 9 |
#include "include/core/SkCanvas.h" |
| 10 |
#include "include/core/SkColor.h" |
| 11 |
#include "include/core/SkColorSpace.h" |
| 12 |
#include "include/core/SkFont.h" |
| 13 |
#include "include/core/SkFontStyle.h" |
| 14 |
#include "include/core/SkFontTypes.h" |
| 15 |
#include "include/core/SkImageInfo.h" |
| 16 |
#include "include/core/SkMatrix.h" |
| 17 |
#include "include/core/SkPaint.h" |
| 18 |
#include "include/core/SkPoint.h" |
| 19 |
#include "include/core/SkRect.h" |
| 20 |
#include "include/core/SkRefCnt.h" |
| 21 |
#include "include/core/SkScalar.h" |
| 22 |
#include "include/core/SkSize.h" |
| 23 |
#include "include/core/SkString.h" |
| 24 |
#include "include/core/SkSurface.h" |
| 25 |
#include "include/core/SkSurfaceProps.h" |
| 26 |
#include "include/core/SkTextBlob.h" |
| 27 |
#include "include/core/SkTypeface.h" |
| 28 |
#include "include/core/SkTypes.h" |
| 29 |
#include "include/gpu/GpuTypes.h" |
| 30 |
#include "include/private/base/SkTemplates.h" |
| 31 |
#include "include/private/base/SkTo.h" |
| 32 |
#include "tools/ToolUtils.h" |
| 33 |
#include "tools/fonts/FontToolUtils.h" |
| 34 |
|
| 35 |
#if defined(SK_GANESH) |
| 36 |
#include "include/gpu/ganesh/SkSurfaceGanesh.h" |
| 37 |
#endif |
| 38 |
|
| 39 |
#if defined(SK_GRAPHITE) |
| 40 |
#include "include/gpu/graphite/Surface.h" |
| 41 |
#endif |
| 42 |
|
| 43 |
#include <string.h> |
| 44 |
|
| 45 |
using namespace skia_private; |
| 46 |
|
| 47 |
class DFTextGM : public skiagm::GM { |
| 48 |
public: |
| 49 |
DFTextGM() { |
| 50 |
this->setBGColor(0xFFFFFFFF); |
| 51 |
} |
| 52 |
|
| 53 |
protected: |
| 54 |
void onOnceBeforeDraw() override { fEmojiSample = ToolUtils::EmojiSample(); } |
| 55 |
|
| 56 |
SkString getName() const override { return SkString("dftext"); } |
| 57 |
|
| 58 |
SkISize getISize() override { return SkISize::Make(1024, 768); } |
| 59 |
|
| 60 |
void onDraw(SkCanvas* inputCanvas) override { |
| 61 |
SkScalar textSizes[] = { 9.0f, 9.0f*2.0f, 9.0f*5.0f, 9.0f*2.0f*5.0f }; |
| 62 |
SkScalar scales[] = { 2.0f*5.0f, 5.0f, 2.0f, 1.0f }; |
| 63 |
|
| 64 |
// set up offscreen rendering with distance field text |
| 65 |
SkISize size = this->getISize(); |
| 66 |
if (!inputCanvas->getBaseLayerSize().isEmpty()) { |
| 67 |
size = inputCanvas->getBaseLayerSize(); |
| 68 |
} |
| 69 |
SkImageInfo info = SkImageInfo::MakeN32(size.width(), size.height(), kPremul_SkAlphaType, |
| 70 |
inputCanvas->imageInfo().refColorSpace()); |
| 71 |
SkSurfaceProps inputProps; |
| 72 |
inputCanvas->getProps(&inputProps); |
| 73 |
SkSurfaceProps props(SkSurfaceProps::kUseDeviceIndependentFonts_Flag | inputProps.flags(), |
| 74 |
inputProps.pixelGeometry()); |
| 75 |
sk_sp<SkSurface> surface; |
| 76 |
#if defined(SK_GRAPHITE) |
| 77 |
if (auto recorder = inputCanvas->recorder()) { |
| 78 |
surface = SkSurfaces::RenderTarget(recorder, info, skgpu::Mipmapped::kNo, &props); |
| 79 |
} |
| 80 |
#endif |
| 81 |
#if defined(SK_GANESH) |
| 82 |
if (auto ctx = inputCanvas->recordingContext()) { |
| 83 |
surface = SkSurfaces::RenderTarget(ctx, skgpu::Budgeted::kNo, info, 0, &props); |
| 84 |
} |
| 85 |
#endif |
| 86 |
SkCanvas* canvas = surface ? surface->getCanvas() : inputCanvas; |
| 87 |
// init our new canvas with the old canvas's matrix |
| 88 |
canvas->setMatrix(inputCanvas->getLocalToDeviceAs3x3()); |
| 89 |
// apply global scale to test glyph positioning |
| 90 |
canvas->scale(1.05f, 1.05f); |
| 91 |
canvas->clear(0xffffffff); |
| 92 |
|
| 93 |
SkPaint paint; |
| 94 |
paint.setAntiAlias(true); |
| 95 |
|
| 96 |
SkFont font(ToolUtils::CreatePortableTypeface("serif", SkFontStyle())); |
| 97 |
font.setSubpixel(true); |
| 98 |
|
| 99 |
const char* text = "Hamburgefons"; |
| 100 |
const size_t textLen = strlen(text); |
| 101 |
|
| 102 |
// check scaling up |
| 103 |
SkScalar x = SkIntToScalar(0); |
| 104 |
SkScalar y = SkIntToScalar(78); |
| 105 |
for (size_t i = 0; i < std::size(textSizes); ++i) { |
| 106 |
SkAutoCanvasRestore acr(canvas, true); |
| 107 |
canvas->translate(x, y); |
| 108 |
canvas->scale(scales[i], scales[i]); |
| 109 |
font.setSize(textSizes[i]); |
| 110 |
canvas->drawSimpleText(text, textLen, SkTextEncoding::kUTF8, 0, 0, font, paint); |
| 111 |
y += font.getMetrics(nullptr)*scales[i]; |
| 112 |
} |
| 113 |
|
| 114 |
// check rotation |
| 115 |
for (size_t i = 0; i < 5; ++i) { |
| 116 |
SkScalar rotX = SkIntToScalar(10); |
| 117 |
SkScalar rotY = y; |
| 118 |
|
| 119 |
SkAutoCanvasRestore acr(canvas, true); |
| 120 |
canvas->translate(SkIntToScalar(10 + i * 200), -80); |
| 121 |
canvas->rotate(SkIntToScalar(i * 5), rotX, rotY); |
| 122 |
for (int ps = 6; ps <= 32; ps += 3) { |
| 123 |
font.setSize(SkIntToScalar(ps)); |
| 124 |
canvas->drawSimpleText(text, textLen, SkTextEncoding::kUTF8, rotX, rotY, font, paint); |
| 125 |
rotY += font.getMetrics(nullptr); |
| 126 |
} |
| 127 |
} |
| 128 |
|
| 129 |
// check scaling down |
| 130 |
font.setEdging(SkFont::Edging::kSubpixelAntiAlias); |
| 131 |
x = SkIntToScalar(680); |
| 132 |
y = SkIntToScalar(20); |
| 133 |
size_t arraySize = std::size(textSizes); |
| 134 |
for (size_t i = 0; i < arraySize; ++i) { |
| 135 |
SkAutoCanvasRestore acr(canvas, true); |
| 136 |
canvas->translate(x, y); |
| 137 |
SkScalar scaleFactor = SkScalarInvert(scales[arraySize - i - 1]); |
| 138 |
canvas->scale(scaleFactor, scaleFactor); |
| 139 |
font.setSize(textSizes[i]); |
| 140 |
canvas->drawSimpleText(text, textLen, SkTextEncoding::kUTF8, 0, 0, font, paint); |
| 141 |
y += font.getMetrics(nullptr)*scaleFactor; |
| 142 |
} |
| 143 |
|
| 144 |
// check pos text |
| 145 |
{ |
| 146 |
SkAutoCanvasRestore acr(canvas, true); |
| 147 |
|
| 148 |
canvas->scale(2.0f, 2.0f); |
| 149 |
|
| 150 |
AutoTArray<SkGlyphID> glyphs(SkToInt(textLen)); |
| 151 |
size_t count = font.textToGlyphs(text, textLen, SkTextEncoding::kUTF8, glyphs); |
| 152 |
AutoTArray<SkPoint> pos(count); |
| 153 |
font.setSize(textSizes[0]); |
| 154 |
font.getPos(glyphs, pos, {340, 75}); |
| 155 |
|
| 156 |
auto blob = SkTextBlob::MakeFromPosGlyphs({glyphs.data(), count}, pos, font); |
| 157 |
canvas->drawTextBlob(blob, 0, 0, paint); |
| 158 |
} |
| 159 |
|
| 160 |
|
| 161 |
// check gamma-corrected blending |
| 162 |
const SkColor fg[] = { |
| 163 |
0xFFFFFFFF, |
| 164 |
0xFFFFFF00, 0xFFFF00FF, 0xFF00FFFF, |
| 165 |
0xFFFF0000, 0xFF00FF00, 0xFF0000FF, |
| 166 |
0xFF000000, |
| 167 |
}; |
| 168 |
|
| 169 |
paint.setColor(0xFFF7F3F7); |
| 170 |
SkRect r = SkRect::MakeLTRB(670, 215, 820, 397); |
| 171 |
canvas->drawRect(r, paint); |
| 172 |
|
| 173 |
x = SkIntToScalar(680); |
| 174 |
y = SkIntToScalar(235); |
| 175 |
font.setSize(SkIntToScalar(19)); |
| 176 |
for (size_t i = 0; i < std::size(fg); ++i) { |
| 177 |
paint.setColor(fg[i]); |
| 178 |
|
| 179 |
canvas->drawSimpleText(text, textLen, SkTextEncoding::kUTF8, x, y, font, paint); |
| 180 |
y += font.getMetrics(nullptr); |
| 181 |
} |
| 182 |
|
| 183 |
paint.setColor(0xFF181C18); |
| 184 |
r = SkRect::MakeLTRB(820, 215, 970, 397); |
| 185 |
canvas->drawRect(r, paint); |
| 186 |
|
| 187 |
x = SkIntToScalar(830); |
| 188 |
y = SkIntToScalar(235); |
| 189 |
font.setSize(SkIntToScalar(19)); |
| 190 |
for (size_t i = 0; i < std::size(fg); ++i) { |
| 191 |
paint.setColor(fg[i]); |
| 192 |
|
| 193 |
canvas->drawSimpleText(text, textLen, SkTextEncoding::kUTF8, x, y, font, paint); |
| 194 |
y += font.getMetrics(nullptr); |
| 195 |
} |
| 196 |
|
| 197 |
// check skew |
| 198 |
{ |
| 199 |
font.setEdging(SkFont::Edging::kAntiAlias); |
| 200 |
SkAutoCanvasRestore acr(canvas, true); |
| 201 |
canvas->skew(0.0f, 0.151515f); |
| 202 |
font.setSize(SkIntToScalar(32)); |
| 203 |
canvas->drawSimpleText(text, textLen, SkTextEncoding::kUTF8, 745, 70, font, paint); |
| 204 |
} |
| 205 |
{ |
| 206 |
font.setEdging(SkFont::Edging::kSubpixelAntiAlias); |
| 207 |
SkAutoCanvasRestore acr(canvas, true); |
| 208 |
canvas->skew(0.5f, 0.0f); |
| 209 |
font.setSize(SkIntToScalar(32)); |
| 210 |
canvas->drawSimpleText(text, textLen, SkTextEncoding::kUTF8, 580, 125, font, paint); |
| 211 |
} |
| 212 |
|
| 213 |
// check perspective |
| 214 |
{ |
| 215 |
font.setEdging(SkFont::Edging::kAntiAlias); |
| 216 |
SkAutoCanvasRestore acr(canvas, true); |
| 217 |
SkMatrix persp; |
| 218 |
persp.setAll(0.9839f, 0, 0, |
| 219 |
0.2246f, 0.6829f, 0, |
| 220 |
0.0002352f, -0.0003844f, 1); |
| 221 |
canvas->concat(persp); |
| 222 |
canvas->translate(1100, -295); |
| 223 |
font.setSize(37.5f); |
| 224 |
canvas->drawSimpleText(text, textLen, SkTextEncoding::kUTF8, 0, 0, font, paint); |
| 225 |
} |
| 226 |
{ |
| 227 |
font.setSubpixel(false); |
| 228 |
font.setEdging(SkFont::Edging::kAlias); |
| 229 |
SkAutoCanvasRestore acr(canvas, true); |
| 230 |
SkMatrix persp; |
| 231 |
persp.setAll(0.9839f, 0, 0, |
| 232 |
0.2246f, 0.6829f, 0, |
| 233 |
0.0002352f, -0.0003844f, 1); |
| 234 |
canvas->concat(persp); |
| 235 |
canvas->translate(1075, -245); |
| 236 |
canvas->scale(375, 375); |
| 237 |
font.setSize(0.1f); |
| 238 |
canvas->drawSimpleText(text, textLen, SkTextEncoding::kUTF8, 0, 0, font, paint); |
| 239 |
} |
| 240 |
|
| 241 |
// check color emoji |
| 242 |
if (fEmojiSample.typeface) { |
| 243 |
SkFont emojiFont; |
| 244 |
emojiFont.setSubpixel(true); |
| 245 |
emojiFont.setTypeface(fEmojiSample.typeface); |
| 246 |
emojiFont.setSize(SkIntToScalar(19)); |
| 247 |
canvas->drawSimpleText(fEmojiSample.sampleText, |
| 248 |
strlen(fEmojiSample.sampleText), |
| 249 |
SkTextEncoding::kUTF8, |
| 250 |
670, |
| 251 |
90, |
| 252 |
emojiFont, |
| 253 |
paint); |
| 254 |
} |
| 255 |
|
| 256 |
// render offscreen buffer |
| 257 |
if (surface) { |
| 258 |
SkAutoCanvasRestore acr(inputCanvas, true); |
| 259 |
// since we prepended this matrix already, we blit using identity |
| 260 |
inputCanvas->resetMatrix(); |
| 261 |
inputCanvas->drawImage(surface->makeImageSnapshot().get(), 0, 0); |
| 262 |
} |
| 263 |
} |
| 264 |
|
| 265 |
private: |
| 266 |
ToolUtils::EmojiTestSample fEmojiSample; |
| 267 |
|
| 268 |
using INHERITED = skiagm::GM; |
| 269 |
}; |
| 270 |
|
| 271 |
DEF_GM(return new DFTextGM;) |