epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 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 | */ |
reed@google.com | 8ea6983 | 2013-06-21 15:11:40 +0000 | [diff] [blame] | 7 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 8 | #include "gm/gm.h" |
Ben Wagner | 7fde8e1 | 2019-05-01 17:28:53 -0400 | [diff] [blame] | 9 | #include "include/core/SkCanvas.h" |
| 10 | #include "include/core/SkColor.h" |
| 11 | #include "include/core/SkFont.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 12 | #include "include/core/SkFontMetrics.h" |
Ben Wagner | 7fde8e1 | 2019-05-01 17:28:53 -0400 | [diff] [blame] | 13 | #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 Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 22 | #include "include/core/SkTypeface.h" |
| 23 | #include "include/utils/SkTextUtils.h" |
| 24 | #include "tools/Resources.h" |
Ben Wagner | 7fde8e1 | 2019-05-01 17:28:53 -0400 | [diff] [blame] | 25 | |
| 26 | #include <stdint.h> |
reed@google.com | 67981c5 | 2011-02-17 14:21:01 +0000 | [diff] [blame] | 27 | |
reed@google.com | 8ea6983 | 2013-06-21 15:11:40 +0000 | [diff] [blame] | 28 | class Poly2PolyGM : public skiagm::GM { |
reed@google.com | 67981c5 | 2011-02-17 14:21:01 +0000 | [diff] [blame] | 29 | public: |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 30 | Poly2PolyGM() {} |
| 31 | |
reed@google.com | 67981c5 | 2011-02-17 14:21:01 +0000 | [diff] [blame] | 32 | protected: |
commit-bot@chromium.org | a90c680 | 2014-04-30 13:20:45 +0000 | [diff] [blame] | 33 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 34 | SkString onShortName() override { |
reed@google.com | 67981c5 | 2011-02-17 14:21:01 +0000 | [diff] [blame] | 35 | return SkString("poly2poly"); |
| 36 | } |
| 37 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 38 | SkISize onISize() override { |
reed@google.com | 8ea6983 | 2013-06-21 15:11:40 +0000 | [diff] [blame] | 39 | return SkISize::Make(835, 840); |
reed@google.com | 67981c5 | 2011-02-17 14:21:01 +0000 | [diff] [blame] | 40 | } |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 41 | |
Mike Reed | dc5863c | 2018-12-23 23:19:14 -0500 | [diff] [blame] | 42 | static void doDraw(SkCanvas* canvas, const SkFont& font, SkPaint* paint, const int isrc[], |
reed@google.com | 67981c5 | 2011-02-17 14:21:01 +0000 | [diff] [blame] | 43 | const int idst[], int count) { |
| 44 | SkMatrix matrix; |
| 45 | SkPoint src[4], dst[4]; |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 46 | |
reed@google.com | 67981c5 | 2011-02-17 14:21:01 +0000 | [diff] [blame] | 47 | 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.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 51 | |
reed@google.com | 67981c5 | 2011-02-17 14:21:01 +0000 | [diff] [blame] | 52 | canvas->save(); |
| 53 | matrix.setPolyToPoly(src, dst, count); |
| 54 | canvas->concat(matrix); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 55 | |
Mike Klein | d46dce3 | 2018-08-16 10:17:03 -0400 | [diff] [blame] | 56 | paint->setColor(SK_ColorGRAY); |
reed@google.com | 67981c5 | 2011-02-17 14:21:01 +0000 | [diff] [blame] | 57 | paint->setStyle(SkPaint::kStroke_Style); |
Mike Reed | 3661bc9 | 2017-02-22 13:21:42 -0500 | [diff] [blame] | 58 | const SkScalar D = 64; |
| 59 | canvas->drawRect(SkRect::MakeWH(D, D), *paint); |
reed@google.com | 67981c5 | 2011-02-17 14:21:01 +0000 | [diff] [blame] | 60 | canvas->drawLine(0, 0, D, D, *paint); |
| 61 | canvas->drawLine(0, D, D, 0, *paint); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 62 | |
Mike Reed | b5784ac | 2018-11-12 09:35:15 -0500 | [diff] [blame] | 63 | SkFontMetrics fm; |
Mike Reed | dc5863c | 2018-12-23 23:19:14 -0500 | [diff] [blame] | 64 | font.getMetrics(&fm); |
reed@google.com | 67981c5 | 2011-02-17 14:21:01 +0000 | [diff] [blame] | 65 | paint->setColor(SK_ColorRED); |
| 66 | paint->setStyle(SkPaint::kFill_Style); |
| 67 | SkScalar x = D/2; |
bsalomon@google.com | 72e49b8 | 2011-10-27 21:47:03 +0000 | [diff] [blame] | 68 | SkScalar y = D/2 - (fm.fAscent + fm.fDescent)/2; |
caryclark | f597c42 | 2015-07-28 10:37:53 -0700 | [diff] [blame] | 69 | uint16_t glyphID = 3; // X |
Ben Wagner | 51e15a6 | 2019-05-07 15:38:46 -0400 | [diff] [blame] | 70 | SkTextUtils::Draw(canvas, &glyphID, sizeof(glyphID), SkTextEncoding::kGlyphID, x, y, |
Mike Reed | dc5863c | 2018-12-23 23:19:14 -0500 | [diff] [blame] | 71 | font, *paint, SkTextUtils::kCenter_Align); |
reed@google.com | 67981c5 | 2011-02-17 14:21:01 +0000 | [diff] [blame] | 72 | canvas->restore(); |
| 73 | } |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 74 | |
caryclark | f597c42 | 2015-07-28 10:37:53 -0700 | [diff] [blame] | 75 | void onOnceBeforeDraw() override { |
Hal Canary | 53e5e7d | 2017-12-08 14:25:14 -0500 | [diff] [blame] | 76 | fEmFace = MakeResourceAsTypeface("fonts/Em.ttf"); |
caryclark | f597c42 | 2015-07-28 10:37:53 -0700 | [diff] [blame] | 77 | } |
| 78 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 79 | void onDraw(SkCanvas* canvas) override { |
reed@google.com | 67981c5 | 2011-02-17 14:21:01 +0000 | [diff] [blame] | 80 | SkPaint paint; |
| 81 | paint.setAntiAlias(true); |
| 82 | paint.setStrokeWidth(SkIntToScalar(4)); |
Mike Reed | dc5863c | 2018-12-23 23:19:14 -0500 | [diff] [blame] | 83 | SkFont font(fEmFace, 40); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 84 | |
reed@google.com | 67981c5 | 2011-02-17 14:21:01 +0000 | [diff] [blame] | 85 | 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 Reed | dc5863c | 2018-12-23 23:19:14 -0500 | [diff] [blame] | 90 | doDraw(canvas, font, &paint, src1, dst1, 1); |
reed@google.com | 67981c5 | 2011-02-17 14:21:01 +0000 | [diff] [blame] | 91 | canvas->restore(); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 92 | |
reed@google.com | 67981c5 | 2011-02-17 14:21:01 +0000 | [diff] [blame] | 93 | 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 Reed | dc5863c | 2018-12-23 23:19:14 -0500 | [diff] [blame] | 98 | doDraw(canvas, font, &paint, src2, dst2, 2); |
reed@google.com | 67981c5 | 2011-02-17 14:21:01 +0000 | [diff] [blame] | 99 | canvas->restore(); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 100 | |
reed@google.com | 67981c5 | 2011-02-17 14:21:01 +0000 | [diff] [blame] | 101 | 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 Reed | dc5863c | 2018-12-23 23:19:14 -0500 | [diff] [blame] | 106 | doDraw(canvas, font, &paint, src3, dst3, 3); |
reed@google.com | 67981c5 | 2011-02-17 14:21:01 +0000 | [diff] [blame] | 107 | canvas->restore(); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 108 | |
reed@google.com | 67981c5 | 2011-02-17 14:21:01 +0000 | [diff] [blame] | 109 | 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 Reed | dc5863c | 2018-12-23 23:19:14 -0500 | [diff] [blame] | 114 | doDraw(canvas, font, &paint, src4, dst4, 4); |
reed@google.com | 67981c5 | 2011-02-17 14:21:01 +0000 | [diff] [blame] | 115 | canvas->restore(); |
| 116 | } |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 117 | |
reed@google.com | 67981c5 | 2011-02-17 14:21:01 +0000 | [diff] [blame] | 118 | private: |
reed@google.com | 8ea6983 | 2013-06-21 15:11:40 +0000 | [diff] [blame] | 119 | typedef skiagm::GM INHERITED; |
bungeman | 13b9c95 | 2016-05-12 10:09:30 -0700 | [diff] [blame] | 120 | sk_sp<SkTypeface> fEmFace; |
reed@google.com | 67981c5 | 2011-02-17 14:21:01 +0000 | [diff] [blame] | 121 | }; |
| 122 | |
| 123 | ////////////////////////////////////////////////////////////////////////////// |
| 124 | |
reed@google.com | 8ea6983 | 2013-06-21 15:11:40 +0000 | [diff] [blame] | 125 | DEF_GM( return new Poly2PolyGM; ) |