Jim Van Verth | 3912948 | 2023-01-19 17:20:15 -0500 | [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 | */ |
| 7 | |
| 8 | #include "gm/gm.h" |
| 9 | #include "include/core/SkCanvas.h" |
| 10 | #include "include/core/SkColor.h" |
| 11 | #include "include/core/SkFont.h" |
| 12 | #include "include/core/SkFontStyle.h" |
| 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/SkScalar.h" |
| 19 | #include "include/core/SkString.h" |
| 20 | #include "include/core/SkTypeface.h" |
| 21 | #include "include/core/SkTypes.h" |
Kevin Lubick | 0d4d114 | 2023-02-13 09:13:10 -0500 | [diff] [blame] | 22 | #include "include/private/base/SkTemplates.h" |
Jim Van Verth | 3912948 | 2023-01-19 17:20:15 -0500 | [diff] [blame] | 23 | #include "include/private/base/SkTo.h" |
| 24 | #include "src/core/SkMatrixPriv.h" |
| 25 | #include "tools/ToolUtils.h" |
Kevin Lubick | e836c3a | 2023-10-20 06:55:35 -0400 | [diff] [blame] | 26 | #include "tools/fonts/FontToolUtils.h" |
Jim Van Verth | 3912948 | 2023-01-19 17:20:15 -0500 | [diff] [blame] | 27 | |
| 28 | #include <string.h> |
| 29 | |
| 30 | class PerspTextGM : public skiagm::GM { |
| 31 | public: |
Jim Van Verth | 3e6cf40 | 2023-01-25 14:09:54 -0500 | [diff] [blame] | 32 | PerspTextGM(bool minimal) : fMinimal(minimal) { |
Jim Van Verth | 3912948 | 2023-01-19 17:20:15 -0500 | [diff] [blame] | 33 | this->setBGColor(0xFFFFFFFF); |
| 34 | } |
| 35 | |
| 36 | protected: |
Leandro Lovisolo | 24fa211 | 2023-08-15 19:05:17 +0000 | [diff] [blame] | 37 | SkString getName() const override { |
Jim Van Verth | 3e6cf40 | 2023-01-25 14:09:54 -0500 | [diff] [blame] | 38 | return SkString(fMinimal ? "persptext_minimal" : "persptext"); |
Jim Van Verth | 3912948 | 2023-01-19 17:20:15 -0500 | [diff] [blame] | 39 | } |
| 40 | |
Leandro Lovisolo | 8f02388 | 2023-08-15 21:13:52 +0000 | [diff] [blame] | 41 | SkISize getISize() override { return SkISize::Make(1024, 768); } |
Jim Van Verth | 3912948 | 2023-01-19 17:20:15 -0500 | [diff] [blame] | 42 | |
Leandro Lovisolo | 8f02388 | 2023-08-15 21:13:52 +0000 | [diff] [blame] | 43 | // #define TEST_PERSP_CHECK |
Jim Van Verth | 3e6cf40 | 2023-01-25 14:09:54 -0500 | [diff] [blame] | 44 | |
Jim Van Verth | 3912948 | 2023-01-19 17:20:15 -0500 | [diff] [blame] | 45 | void onDraw(SkCanvas* canvas) override { |
| 46 | |
| 47 | canvas->clear(0xffffffff); |
| 48 | |
| 49 | SkPaint paint; |
| 50 | paint.setAntiAlias(true); |
| 51 | |
Kevin Lubick | e836c3a | 2023-10-20 06:55:35 -0400 | [diff] [blame] | 52 | SkFont font(ToolUtils::CreatePortableTypeface("serif", SkFontStyle())); |
Jim Van Verth | 3912948 | 2023-01-19 17:20:15 -0500 | [diff] [blame] | 53 | font.setSubpixel(true); |
| 54 | font.setSize(32); |
Jim Van Verth | 3e6cf40 | 2023-01-25 14:09:54 -0500 | [diff] [blame] | 55 | font.setBaselineSnap(false); |
Jim Van Verth | 3912948 | 2023-01-19 17:20:15 -0500 | [diff] [blame] | 56 | |
| 57 | const char* text = "Hamburgefons"; |
| 58 | const size_t textLen = strlen(text); |
| 59 | |
| 60 | SkScalar textWidth = font.measureText(text, textLen, SkTextEncoding::kUTF8, |
| 61 | nullptr, nullptr); |
| 62 | SkScalar textHeight = font.getMetrics(nullptr); |
| 63 | |
| 64 | SkScalar x = 10, y = textHeight + 5.f; |
| 65 | const int kSteps = 8; |
Jim Van Verth | 3e6cf40 | 2023-01-25 14:09:54 -0500 | [diff] [blame] | 66 | float kMinimalFactor = fMinimal ? 32.f : 1.f; |
Jim Van Verth | 3912948 | 2023-01-19 17:20:15 -0500 | [diff] [blame] | 67 | for (auto pm : {PerspMode::kX, PerspMode::kY, PerspMode::kXY}) { |
| 68 | for (int i = 0; i < kSteps; ++i) { |
| 69 | canvas->save(); |
Jim Van Verth | 3e6cf40 | 2023-01-25 14:09:54 -0500 | [diff] [blame] | 70 | #ifdef TEST_PERSP_CHECK |
| 71 | // draw non-perspective text in the background for comparison |
| 72 | paint.setColor(SK_ColorRED); |
| 73 | canvas->drawSimpleText(text, textLen, SkTextEncoding::kUTF8, x, y, font, paint); |
| 74 | #endif |
| 75 | |
Jim Van Verth | 3912948 | 2023-01-19 17:20:15 -0500 | [diff] [blame] | 76 | SkMatrix persp = SkMatrix::I(); |
| 77 | switch (pm) { |
| 78 | case PerspMode::kX: |
Jim Van Verth | 3e6cf40 | 2023-01-25 14:09:54 -0500 | [diff] [blame] | 79 | if (fMinimal) { |
| 80 | persp.setPerspX(i*0.0005f/kSteps/kMinimalFactor); |
| 81 | } else { |
| 82 | persp.setPerspX(i*0.00025f/kSteps); |
| 83 | } |
Jim Van Verth | 3912948 | 2023-01-19 17:20:15 -0500 | [diff] [blame] | 84 | break; |
| 85 | case PerspMode::kY: |
Jim Van Verth | 3e6cf40 | 2023-01-25 14:09:54 -0500 | [diff] [blame] | 86 | persp.setPerspY(i*0.0025f/kSteps/kMinimalFactor); |
Jim Van Verth | 3912948 | 2023-01-19 17:20:15 -0500 | [diff] [blame] | 87 | break; |
| 88 | case PerspMode::kXY: |
Jim Van Verth | 3e6cf40 | 2023-01-25 14:09:54 -0500 | [diff] [blame] | 89 | persp.setPerspX(i*-0.00025f/kSteps/kMinimalFactor); |
| 90 | persp.setPerspY(i*-0.00125f/kSteps/kMinimalFactor); |
Jim Van Verth | 3912948 | 2023-01-19 17:20:15 -0500 | [diff] [blame] | 91 | break; |
| 92 | } |
| 93 | persp = SkMatrix::Concat(persp, SkMatrix::Translate(-x, -y)); |
| 94 | persp = SkMatrix::Concat(SkMatrix::Translate(x, y), persp); |
| 95 | canvas->concat(persp); |
| 96 | |
| 97 | paint.setColor(SK_ColorBLACK); |
Jim Van Verth | 3e6cf40 | 2023-01-25 14:09:54 -0500 | [diff] [blame] | 98 | #ifdef TEST_PERSP_CHECK |
| 99 | // Draw text as red if it is nearly affine |
| 100 | SkRect bounds = SkRect::MakeXYWH(0, -textHeight, textWidth, textHeight); |
| 101 | bounds.offset(x, y); |
| 102 | if (SkMatrixPriv::NearlyAffine(persp, bounds, SK_Scalar1/(1 << 4))) { |
| 103 | paint.setColor(SK_ColorRED); |
| 104 | } |
| 105 | #endif |
Jim Van Verth | 3912948 | 2023-01-19 17:20:15 -0500 | [diff] [blame] | 106 | canvas->drawSimpleText(text, textLen, SkTextEncoding::kUTF8, x, y, font, paint); |
| 107 | |
| 108 | y += textHeight + 5.f; |
| 109 | canvas->restore(); |
| 110 | } |
| 111 | |
| 112 | x += textWidth + 10.f; |
| 113 | y = textHeight + 5.f; |
| 114 | } |
| 115 | |
| 116 | } |
| 117 | |
| 118 | private: |
| 119 | enum class PerspMode { kX, kY, kXY }; |
Jim Van Verth | 3e6cf40 | 2023-01-25 14:09:54 -0500 | [diff] [blame] | 120 | bool fMinimal; |
Jim Van Verth | 3912948 | 2023-01-19 17:20:15 -0500 | [diff] [blame] | 121 | }; |
| 122 | |
Jim Van Verth | 3e6cf40 | 2023-01-25 14:09:54 -0500 | [diff] [blame] | 123 | DEF_GM(return new PerspTextGM(true);) |
| 124 | DEF_GM(return new PerspTextGM(false);) |