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/SkBitmap.h"
10 #include "include/core/SkCanvas.h"
11 #include "include/core/SkColor.h"
12 #include "include/core/SkColorSpace.h"
13 #include "include/core/SkFont.h"
14 #include "include/core/SkFontTypes.h"
15 #include "include/core/SkImage.h"
16 #include "include/core/SkImageGenerator.h"
17 #include "include/core/SkImageInfo.h"
18 #include "include/core/SkMatrix.h"
19 #include "include/core/SkPaint.h"
20 #include "include/core/SkPath.h"
21 #include "include/core/SkPicture.h"
22 #include "include/core/SkPictureRecorder.h"
23 #include "include/core/SkPoint.h"
24 #include "include/core/SkRect.h"
25 #include "include/core/SkRefCnt.h"
26 #include "include/core/SkScalar.h"
27 #include "include/core/SkShader.h"
28 #include "include/core/SkSize.h"
29 #include "include/core/SkString.h"
30 #include "include/core/SkTileMode.h"
31 #include "include/core/SkTypeface.h"
32 #include "include/core/SkTypes.h"
33 #include "include/effects/SkGradientShader.h"
34 #include "include/pathops/SkPathOps.h"
35 #include "include/utils/SkTextUtils.h"
36 #include "tools/ToolUtils.h"
37 
38 #include <string.h>
39 #include <memory>
40 
draw_vector_logo(SkCanvas * canvas,const SkRect & viewBox)41 static void draw_vector_logo(SkCanvas* canvas, const SkRect& viewBox) {
42     constexpr char kSkiaStr[] = "SKIA";
43     constexpr SkScalar kGradientPad = .1f;
44     constexpr SkScalar kVerticalSpacing = 0.25f;
45     constexpr SkScalar kAccentScale = 1.20f;
46 
47     SkPaint paint;
48     paint.setAntiAlias(true);
49 
50     SkFont font(ToolUtils::create_portable_typeface());
51     font.setSubpixel(true);
52     font.setEmbolden(true);
53 
54     SkPath path;
55     SkRect iBox, skiBox, skiaBox;
56     SkTextUtils::GetPath("SKI", 3, SkTextEncoding::kUTF8, 0, 0, font, &path);
57     TightBounds(path, &skiBox);
58     SkTextUtils::GetPath("I", 1, SkTextEncoding::kUTF8, 0, 0, font, &path);
59     TightBounds(path, &iBox);
60     iBox.offsetTo(skiBox.fRight - iBox.width(), iBox.fTop);
61 
62     const size_t textLen = strlen(kSkiaStr);
63     SkTextUtils::GetPath(kSkiaStr, textLen, SkTextEncoding::kUTF8, 0, 0, font, &path);
64     TightBounds(path, &skiaBox);
65     skiaBox.outset(0, 2 * iBox.width() * (kVerticalSpacing + 1));
66 
67     const SkScalar accentSize = iBox.width() * kAccentScale;
68     const SkScalar underlineY = iBox.bottom() +
69         (kVerticalSpacing + SkScalarSqrt(3) / 2) * accentSize;
70     SkMatrix m;
71     m.setRectToRect(skiaBox, viewBox, SkMatrix::kFill_ScaleToFit);
72     SkAutoCanvasRestore acr(canvas, true);
73     canvas->concat(m);
74 
75     canvas->drawCircle(iBox.centerX(),
76                        iBox.y() - (0.5f + kVerticalSpacing) * accentSize,
77                        accentSize / 2,
78                        paint);
79 
80     path.reset();
81     path.moveTo(iBox.centerX() - accentSize / 2, iBox.bottom() + kVerticalSpacing * accentSize);
82     path.rLineTo(accentSize, 0);
83     path.lineTo(iBox.centerX(), underlineY);
84     canvas->drawPath(path, paint);
85 
86     SkRect underlineRect = SkRect::MakeLTRB(iBox.centerX() - iBox.width() * accentSize * 3,
87                                             underlineY,
88                                             iBox.centerX(),
89                                             underlineY + accentSize / 10);
90     const SkPoint pts1[] = { SkPoint::Make(underlineRect.x(), 0),
91                              SkPoint::Make(iBox.centerX(), 0) };
92     const SkScalar pos1[] = { 0, 0.75f };
93     const SkColor colors1[] = { SK_ColorTRANSPARENT, SK_ColorBLACK };
94     SkASSERT(SK_ARRAY_COUNT(pos1) == SK_ARRAY_COUNT(colors1));
95     paint.setShader(SkGradientShader::MakeLinear(pts1, colors1, pos1, SK_ARRAY_COUNT(pos1),
96                                                  SkTileMode::kClamp));
97     canvas->drawRect(underlineRect, paint);
98 
99     const SkPoint pts2[] = { SkPoint::Make(iBox.x() - iBox.width() * kGradientPad, 0),
100                              SkPoint::Make(iBox.right() + iBox.width() * kGradientPad, 0) };
101     const SkScalar pos2[] = { 0, .01f, 1.0f/3, 1.0f/3, 2.0f/3, 2.0f/3, .99f, 1 };
102     const SkColor colors2[] = {
103         SK_ColorBLACK,
104         0xffca5139,
105         0xffca5139,
106         0xff8dbd53,
107         0xff8dbd53,
108         0xff5460a5,
109         0xff5460a5,
110         SK_ColorBLACK
111     };
112     SkASSERT(SK_ARRAY_COUNT(pos2) == SK_ARRAY_COUNT(colors2));
113     paint.setShader(SkGradientShader::MakeLinear(pts2, colors2, pos2, SK_ARRAY_COUNT(pos2),
114                                                  SkTileMode::kClamp));
115     canvas->drawSimpleText(kSkiaStr, textLen, SkTextEncoding::kUTF8, 0, 0, font, paint);
116 }
117 
118 // This GM exercises SkPictureImageGenerator features
119 // (in particular its matrix vs. bounds semantics).
120 class PictureGeneratorGM : public skiagm::GM {
121 protected:
onShortName()122     SkString onShortName() override {
123         return SkString("pictureimagegenerator");
124     }
125 
onISize()126     SkISize onISize() override {
127         return SkISize::Make(1160, 860);
128     }
129 
onOnceBeforeDraw()130     void onOnceBeforeDraw() override {
131         const SkRect rect = SkRect::MakeWH(kPictureWidth, kPictureHeight);
132         SkPictureRecorder recorder;
133         SkCanvas* canvas = recorder.beginRecording(rect);
134         draw_vector_logo(canvas, rect);
135         fPicture = recorder.finishRecordingAsPicture();
136     }
137 
onDraw(SkCanvas * canvas)138     void onDraw(SkCanvas* canvas) override {
139         const struct {
140             SkISize  size;
141             SkScalar scaleX, scaleY;
142             SkScalar opacity;
143         } configs[] = {
144             { SkISize::Make(200, 100), 1, 1, 1 },
145             { SkISize::Make(200, 200), 1, 1, 1 },
146             { SkISize::Make(200, 200), 1, 2, 1 },
147             { SkISize::Make(400, 200), 2, 2, 1 },
148 
149             { SkISize::Make(200, 100), 1, 1, 0.9f  },
150             { SkISize::Make(200, 200), 1, 1, 0.75f },
151             { SkISize::Make(200, 200), 1, 2, 0.5f  },
152             { SkISize::Make(400, 200), 2, 2, 0.25f },
153 
154             { SkISize::Make(200, 200), 0.5f, 1,    1 },
155             { SkISize::Make(200, 200), 1,    0.5f, 1 },
156             { SkISize::Make(200, 200), 0.5f, 0.5f, 1 },
157             { SkISize::Make(200, 200), 2,    2,    1 },
158 
159             { SkISize::Make(200, 100), -1,  1, 1    },
160             { SkISize::Make(200, 100),  1, -1, 1    },
161             { SkISize::Make(200, 100), -1, -1, 1    },
162             { SkISize::Make(200, 100), -1, -1, 0.5f },
163         };
164 
165         auto srgbColorSpace = SkColorSpace::MakeSRGB();
166         const unsigned kDrawsPerRow = 4;
167         const SkScalar kDrawSize = 250;
168 
169         for (size_t i = 0; i < SK_ARRAY_COUNT(configs); ++i) {
170             SkPaint p;
171             p.setAlphaf(configs[i].opacity);
172 
173             SkMatrix m = SkMatrix::Scale(configs[i].scaleX, configs[i].scaleY);
174             if (configs[i].scaleX < 0) {
175                 m.postTranslate(SkIntToScalar(configs[i].size.width()), 0);
176             }
177             if (configs[i].scaleY < 0) {
178                 m.postTranslate(0, SkIntToScalar(configs[i].size.height()));
179             }
180             std::unique_ptr<SkImageGenerator> gen =
181                 SkImageGenerator::MakeFromPicture(configs[i].size, fPicture, &m,
182                                                  p.getAlpha() != 255 ? &p : nullptr,
183                                                  SkImage::BitDepth::kU8, srgbColorSpace);
184 
185             SkImageInfo bmInfo = gen->getInfo().makeColorSpace(canvas->imageInfo().refColorSpace());
186 
187             SkBitmap bm;
188             bm.allocPixels(bmInfo);
189             SkAssertResult(gen->getPixels(bm.info(), bm.getPixels(), bm.rowBytes()));
190 
191             const SkScalar x = kDrawSize * (i % kDrawsPerRow);
192             const SkScalar y = kDrawSize * (i / kDrawsPerRow);
193 
194             p.setColor(0xfff0f0f0);
195             p.setAlphaf(1.0f);
196             canvas->drawRect(SkRect::MakeXYWH(x, y,
197                                               SkIntToScalar(bm.width()),
198                                               SkIntToScalar(bm.height())), p);
199             canvas->drawBitmap(bm, x, y);
200         }
201     }
202 
203 private:
204     sk_sp<SkPicture> fPicture;
205 
206     const SkScalar kPictureWidth = 200;
207     const SkScalar kPictureHeight = 100;
208 
209     using INHERITED = skiagm::GM;
210 };
211 
212 DEF_GM(return new PictureGeneratorGM;)
213