blob: c8695b9b6ebc92f4742d227cae630abb57ab96ab [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001/*
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 */
reed@google.com8ea69832013-06-21 15:11:40 +00007
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "gm/gm.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -04009#include "include/core/SkCanvas.h"
10#include "include/core/SkColor.h"
11#include "include/core/SkFont.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050012#include "include/core/SkFontMetrics.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040013#include "include/core/SkFontTypes.h"
14#include "include/core/SkMatrix.h"
15#include "include/core/SkPaint.h"
16#include "include/core/SkPoint.h"
17#include "include/core/SkRect.h"
18#include "include/core/SkRefCnt.h"
19#include "include/core/SkScalar.h"
20#include "include/core/SkSize.h"
21#include "include/core/SkString.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050022#include "include/core/SkTypeface.h"
23#include "include/utils/SkTextUtils.h"
24#include "tools/Resources.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040025
26#include <stdint.h>
reed@google.com67981c52011-02-17 14:21:01 +000027
reed@google.com8ea69832013-06-21 15:11:40 +000028class Poly2PolyGM : public skiagm::GM {
reed@google.com67981c52011-02-17 14:21:01 +000029public:
rmistry@google.comd6176b02012-08-23 18:14:13 +000030 Poly2PolyGM() {}
31
reed@google.com67981c52011-02-17 14:21:01 +000032protected:
commit-bot@chromium.orga90c6802014-04-30 13:20:45 +000033
mtklein36352bf2015-03-25 18:17:31 -070034 SkString onShortName() override {
reed@google.com67981c52011-02-17 14:21:01 +000035 return SkString("poly2poly");
36 }
37
mtklein36352bf2015-03-25 18:17:31 -070038 SkISize onISize() override {
reed@google.com8ea69832013-06-21 15:11:40 +000039 return SkISize::Make(835, 840);
reed@google.com67981c52011-02-17 14:21:01 +000040 }
rmistry@google.comd6176b02012-08-23 18:14:13 +000041
Mike Reeddc5863c2018-12-23 23:19:14 -050042 static void doDraw(SkCanvas* canvas, const SkFont& font, SkPaint* paint, const int isrc[],
reed@google.com67981c52011-02-17 14:21:01 +000043 const int idst[], int count) {
44 SkMatrix matrix;
45 SkPoint src[4], dst[4];
rmistry@google.comd6176b02012-08-23 18:14:13 +000046
reed@google.com67981c52011-02-17 14:21:01 +000047 for (int i = 0; i < count; i++) {
48 src[i].set(SkIntToScalar(isrc[2*i+0]), SkIntToScalar(isrc[2*i+1]));
49 dst[i].set(SkIntToScalar(idst[2*i+0]), SkIntToScalar(idst[2*i+1]));
50 }
rmistry@google.comd6176b02012-08-23 18:14:13 +000051
reed@google.com67981c52011-02-17 14:21:01 +000052 canvas->save();
53 matrix.setPolyToPoly(src, dst, count);
54 canvas->concat(matrix);
rmistry@google.comd6176b02012-08-23 18:14:13 +000055
Mike Kleind46dce32018-08-16 10:17:03 -040056 paint->setColor(SK_ColorGRAY);
reed@google.com67981c52011-02-17 14:21:01 +000057 paint->setStyle(SkPaint::kStroke_Style);
Mike Reed3661bc92017-02-22 13:21:42 -050058 const SkScalar D = 64;
59 canvas->drawRect(SkRect::MakeWH(D, D), *paint);
reed@google.com67981c52011-02-17 14:21:01 +000060 canvas->drawLine(0, 0, D, D, *paint);
61 canvas->drawLine(0, D, D, 0, *paint);
rmistry@google.comd6176b02012-08-23 18:14:13 +000062
Mike Reedb5784ac2018-11-12 09:35:15 -050063 SkFontMetrics fm;
Mike Reeddc5863c2018-12-23 23:19:14 -050064 font.getMetrics(&fm);
reed@google.com67981c52011-02-17 14:21:01 +000065 paint->setColor(SK_ColorRED);
66 paint->setStyle(SkPaint::kFill_Style);
67 SkScalar x = D/2;
bsalomon@google.com72e49b82011-10-27 21:47:03 +000068 SkScalar y = D/2 - (fm.fAscent + fm.fDescent)/2;
caryclarkf597c422015-07-28 10:37:53 -070069 uint16_t glyphID = 3; // X
Ben Wagner51e15a62019-05-07 15:38:46 -040070 SkTextUtils::Draw(canvas, &glyphID, sizeof(glyphID), SkTextEncoding::kGlyphID, x, y,
Mike Reeddc5863c2018-12-23 23:19:14 -050071 font, *paint, SkTextUtils::kCenter_Align);
reed@google.com67981c52011-02-17 14:21:01 +000072 canvas->restore();
73 }
rmistry@google.comd6176b02012-08-23 18:14:13 +000074
caryclarkf597c422015-07-28 10:37:53 -070075 void onOnceBeforeDraw() override {
Hal Canary53e5e7d2017-12-08 14:25:14 -050076 fEmFace = MakeResourceAsTypeface("fonts/Em.ttf");
caryclarkf597c422015-07-28 10:37:53 -070077 }
78
mtklein36352bf2015-03-25 18:17:31 -070079 void onDraw(SkCanvas* canvas) override {
reed@google.com67981c52011-02-17 14:21:01 +000080 SkPaint paint;
81 paint.setAntiAlias(true);
82 paint.setStrokeWidth(SkIntToScalar(4));
Mike Reeddc5863c2018-12-23 23:19:14 -050083 SkFont font(fEmFace, 40);
rmistry@google.comd6176b02012-08-23 18:14:13 +000084
reed@google.com67981c52011-02-17 14:21:01 +000085 canvas->save();
86 canvas->translate(SkIntToScalar(10), SkIntToScalar(10));
87 // translate (1 point)
88 const int src1[] = { 0, 0 };
89 const int dst1[] = { 5, 5 };
Mike Reeddc5863c2018-12-23 23:19:14 -050090 doDraw(canvas, font, &paint, src1, dst1, 1);
reed@google.com67981c52011-02-17 14:21:01 +000091 canvas->restore();
rmistry@google.comd6176b02012-08-23 18:14:13 +000092
reed@google.com67981c52011-02-17 14:21:01 +000093 canvas->save();
94 canvas->translate(SkIntToScalar(160), SkIntToScalar(10));
95 // rotate/uniform-scale (2 points)
96 const int src2[] = { 32, 32, 64, 32 };
97 const int dst2[] = { 32, 32, 64, 48 };
Mike Reeddc5863c2018-12-23 23:19:14 -050098 doDraw(canvas, font, &paint, src2, dst2, 2);
reed@google.com67981c52011-02-17 14:21:01 +000099 canvas->restore();
rmistry@google.comd6176b02012-08-23 18:14:13 +0000100
reed@google.com67981c52011-02-17 14:21:01 +0000101 canvas->save();
102 canvas->translate(SkIntToScalar(10), SkIntToScalar(110));
103 // rotate/skew (3 points)
104 const int src3[] = { 0, 0, 64, 0, 0, 64 };
105 const int dst3[] = { 0, 0, 96, 0, 24, 64 };
Mike Reeddc5863c2018-12-23 23:19:14 -0500106 doDraw(canvas, font, &paint, src3, dst3, 3);
reed@google.com67981c52011-02-17 14:21:01 +0000107 canvas->restore();
rmistry@google.comd6176b02012-08-23 18:14:13 +0000108
reed@google.com67981c52011-02-17 14:21:01 +0000109 canvas->save();
110 canvas->translate(SkIntToScalar(160), SkIntToScalar(110));
111 // perspective (4 points)
112 const int src4[] = { 0, 0, 64, 0, 64, 64, 0, 64 };
113 const int dst4[] = { 0, 0, 96, 0, 64, 96, 0, 64 };
Mike Reeddc5863c2018-12-23 23:19:14 -0500114 doDraw(canvas, font, &paint, src4, dst4, 4);
reed@google.com67981c52011-02-17 14:21:01 +0000115 canvas->restore();
116 }
rmistry@google.comd6176b02012-08-23 18:14:13 +0000117
reed@google.com67981c52011-02-17 14:21:01 +0000118private:
John Stiles7571f9e2020-09-02 22:42:33 -0400119 using INHERITED = skiagm::GM;
bungeman13b9c952016-05-12 10:09:30 -0700120 sk_sp<SkTypeface> fEmFace;
reed@google.com67981c52011-02-17 14:21:01 +0000121};
122
123//////////////////////////////////////////////////////////////////////////////
124
reed@google.com8ea69832013-06-21 15:11:40 +0000125DEF_GM( return new Poly2PolyGM; )