• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2015 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/SkFont.h"
11 #include "include/core/SkFontArguments.h"
12 #include "include/core/SkFontMgr.h"
13 #include "include/core/SkFontTypes.h"
14 #include "include/core/SkPaint.h"
15 #include "include/core/SkRect.h"
16 #include "include/core/SkRefCnt.h"
17 #include "include/core/SkScalar.h"
18 #include "include/core/SkSize.h"
19 #include "include/core/SkStream.h"
20 #include "include/core/SkString.h"
21 #include "include/core/SkTypeface.h"
22 #include "include/core/SkTypes.h"
23 #include "tools/Resources.h"
24 
25 #include <string.h>
26 #include <memory>
27 #include <utility>
28 
29 namespace skiagm {
30 
31 class FontScalerDistortableGM : public GM {
32 public:
FontScalerDistortableGM()33     FontScalerDistortableGM() {
34         this->setBGColor(0xFFFFFFFF);
35     }
36 
37 private:
38 
onShortName()39     SkString onShortName() override {
40         return SkString("fontscalerdistortable");
41     }
42 
onISize()43     SkISize onISize() override {
44         return SkISize::Make(550, 700);
45     }
46 
onDraw(SkCanvas * canvas,SkString * errorMsg)47     DrawResult onDraw(SkCanvas* canvas, SkString* errorMsg) override {
48         SkPaint paint;
49         paint.setAntiAlias(true);
50         SkFont font;
51         font.setEdging(SkFont::Edging::kSubpixelAntiAlias);
52         sk_sp<SkFontMgr> fontMgr(SkFontMgr::RefDefault());
53 
54         std::unique_ptr<SkStreamAsset> distortableStream(GetResourceAsStream("fonts/Distortable.ttf"));
55         sk_sp<SkTypeface> distortable(MakeResourceAsTypeface("fonts/Distortable.ttf"));
56 
57         if (!distortableStream) {
58             *errorMsg = "No distortableStream";
59             return DrawResult::kFail;
60         }
61         const char* text = "abc";
62         const size_t textLen = strlen(text);
63 
64         SkFourByteTag tag = SkSetFourByteTag('w','g','h','t');
65         constexpr SkScalar tagMin = 0.5f;
66         constexpr SkScalar tagMax = 2.0f;
67         constexpr int rows = 2;
68         constexpr int cols = 5;
69         for (int row = 0; row < rows; ++row) {
70             for (int col = 0; col < cols; ++col) {
71                 SkScalar x = SkIntToScalar(10);
72                 SkScalar y = SkIntToScalar(20);
73 
74                 SkScalar styleValue = SkScalarInterp(tagMin, tagMax,
75                                                      SkScalar(row * cols + col) / (rows * cols));
76                 SkFontArguments::VariationPosition::Coordinate coordinates[] = {{tag, styleValue}};
77                 SkFontArguments::VariationPosition position =
78                         { coordinates, SK_ARRAY_COUNT(coordinates) };
79                 if (row == 0 && distortable) {
80                     sk_sp<SkTypeface> clone = distortable->makeClone(
81                             SkFontArguments().setVariationDesignPosition(position));
82                     font.setTypeface(clone ? std::move(clone) : distortable);
83                 } else {
84                     font.setTypeface(fontMgr->makeFromStream(
85                         distortableStream->duplicate(),
86                         SkFontArguments().setVariationDesignPosition(position)));
87                 }
88 
89                 SkAutoCanvasRestore acr(canvas, true);
90                 canvas->translate(SkIntToScalar(30 + col * 100), SkIntToScalar(20));
91                 canvas->rotate(SkIntToScalar(col * 5), x, y * 10);
92 
93                 {
94                     SkPaint p;
95                     p.setAntiAlias(true);
96                     SkRect r;
97                     r.set(x - SkIntToScalar(3), SkIntToScalar(15),
98                           x - SkIntToScalar(1), SkIntToScalar(280));
99                     canvas->drawRect(r, p);
100                 }
101 
102                 for (int ps = 6; ps <= 22; ps++) {
103                     font.setSize(SkIntToScalar(ps));
104                     canvas->drawSimpleText(text, textLen, SkTextEncoding::kUTF8, x, y, font, paint);
105                     y += font.getMetrics(nullptr);
106                 }
107             }
108             canvas->translate(0, SkIntToScalar(360));
109             font.setSubpixel(true);
110             font.setLinearMetrics(true);
111         }
112         return DrawResult::kOk;
113     }
114 };
115 
116 //////////////////////////////////////////////////////////////////////////////
117 
118 DEF_GM( return new FontScalerDistortableGM; )
119 
120 }
121