reed@google.com | 14debba | 2011-11-10 20:06:01 +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 | */ |
| 7 | |
| 8 | |
| 9 | #include "gm.h" |
| 10 | #include "SkCanvas.h" |
bungeman@google.com | 8ff8a19 | 2012-09-25 20:38:28 +0000 | [diff] [blame] | 11 | #include "SkTypeface.h" |
reed@google.com | 14debba | 2011-11-10 20:06:01 +0000 | [diff] [blame] | 12 | |
| 13 | namespace skiagm { |
| 14 | |
| 15 | #define TEXT_SIZE 48 |
mtklein | dbfd7ab | 2016-09-01 11:24:54 -0700 | [diff] [blame] | 16 | constexpr char gText[] = "Hello"; |
bungeman@google.com | 8ff8a19 | 2012-09-25 20:38:28 +0000 | [diff] [blame] | 17 | |
| 18 | //Before shaping |
mtklein | dbfd7ab | 2016-09-01 11:24:54 -0700 | [diff] [blame] | 19 | //constexpr char gText[] = "「テスト。」"; |
| 20 | //constexpr char gText[] = {0xE3,0x80,0x8C, 0xE3,0x83,0x86, 0xE3,0x82,0xB9, 0xE3,0x83,0x88, 0xE3,0x80,0x82, 0xE3,0x80,0x8D, 0x0}; |
bungeman@google.com | 8ff8a19 | 2012-09-25 20:38:28 +0000 | [diff] [blame] | 21 | |
| 22 | //After shaping |
mtklein | dbfd7ab | 2016-09-01 11:24:54 -0700 | [diff] [blame] | 23 | //constexpr char gText[] = "﹁テスト︒﹂"; |
| 24 | //constexpr char gText[] = {0xEF,0xB9,0x81, 0xE3,0x83,0x86, 0xE3,0x82,0xB9, 0xE3,0x83,0x88, 0xEF,0xB8,0x92, 0xEF,0xB9,0x82, 0x0}; |
bungeman@google.com | 8ff8a19 | 2012-09-25 20:38:28 +0000 | [diff] [blame] | 25 | |
mtklein | dbfd7ab | 2016-09-01 11:24:54 -0700 | [diff] [blame] | 26 | constexpr size_t gLen = sizeof(gText) - sizeof(gText[0]); |
reed@google.com | 14debba | 2011-11-10 20:06:01 +0000 | [diff] [blame] | 27 | |
| 28 | class VertTextGM : public GM { |
| 29 | public: |
bungeman@google.com | 8ff8a19 | 2012-09-25 20:38:28 +0000 | [diff] [blame] | 30 | VertTextGM() |
Ben Wagner | 7131950 | 2017-07-27 10:45:29 -0400 | [diff] [blame] | 31 | // : fFace(SkTypeface::MakeFromName("unifont", SkFontStyle::Normal())) |
| 32 | // : fFace(SkTypeface::MakeFromFile("MotoyaL04Mincho_3.ttf")) |
bungeman@google.com | cefd981 | 2013-05-15 15:07:32 +0000 | [diff] [blame] | 33 | |
| 34 | // Bitmap fonts on OS X. |
Ben Wagner | 7131950 | 2017-07-27 10:45:29 -0400 | [diff] [blame] | 35 | // : fFace(SkTypeface::MakeFromName("GB18030 Bitmap", SkFontStyle::Normal())) |
| 36 | // : fFace(SkTypeface::MakeFromName("Apple Color Emoji", SkFontStyle::Normal())) |
bungeman@google.com | cefd981 | 2013-05-15 15:07:32 +0000 | [diff] [blame] | 37 | |
| 38 | // OTF CFF fonts on OS X. |
Ben Wagner | 7131950 | 2017-07-27 10:45:29 -0400 | [diff] [blame] | 39 | // : fFace(SkTypeface::MakeFromName("Hiragino Mincho ProN", SkFontStyle::Normal())) |
| 40 | // : fFace(SkTypeface::MakeFromName("Hiragino Kaku Gothic Pro", SkFontStyle::Normal())) |
| 41 | // : fFace(SkTypeface::MakeFromName("Hiragino Sans GB", SkFontStyle::Normal())) |
| 42 | // : fFace(SkTypeface::MakeFromName("STIXGeneral", SkFontStyle::Normal())) |
| 43 | // : fFace(SkTypeface::MakeFromName("Yuppy SC", SkFontStyle::Normal())) |
| 44 | // : fFace(SkTypeface::MakeFromName("Yuppy TC", SkFontStyle::Normal())) |
bungeman@google.com | cefd981 | 2013-05-15 15:07:32 +0000 | [diff] [blame] | 45 | |
bungeman@google.com | 8ff8a19 | 2012-09-25 20:38:28 +0000 | [diff] [blame] | 46 | { |
| 47 | } |
| 48 | |
Hal Canary | cefc431 | 2016-11-04 16:26:16 -0400 | [diff] [blame] | 49 | //sk_sp<SkTypeface> fFace; |
reed@google.com | 14debba | 2011-11-10 20:06:01 +0000 | [diff] [blame] | 50 | |
| 51 | protected: |
| 52 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 53 | SkString onShortName() override { |
reed@google.com | 2f6c957 | 2011-11-30 14:58:16 +0000 | [diff] [blame] | 54 | return SkString("verttext"); |
reed@google.com | 14debba | 2011-11-10 20:06:01 +0000 | [diff] [blame] | 55 | } |
| 56 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 57 | SkISize onISize() override { return SkISize::Make(640, 480); } |
reed@google.com | 14debba | 2011-11-10 20:06:01 +0000 | [diff] [blame] | 58 | |
| 59 | static void drawBaseline(SkCanvas* canvas, const SkPaint& paint, |
| 60 | SkScalar x, SkScalar y) { |
| 61 | SkScalar total = paint.measureText(gText, gLen); |
| 62 | |
| 63 | SkPaint p; |
| 64 | p.setAntiAlias(true); |
| 65 | p.setColor(0x80FF0000); |
| 66 | canvas->drawLine(x, y, |
| 67 | paint.isVerticalText() ? x : x + total, |
| 68 | paint.isVerticalText() ? y + total : y, |
| 69 | p); |
| 70 | |
| 71 | p.setColor(0xFF0000FF); |
| 72 | SkScalar adv[gLen]; |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 73 | int numChars = paint.getTextWidths(gText, gLen, adv, nullptr); |
bungeman@google.com | 3334648 | 2013-08-27 19:05:32 +0000 | [diff] [blame] | 74 | for (int i = 0; i < numChars; ++i) { |
reed@google.com | 14debba | 2011-11-10 20:06:01 +0000 | [diff] [blame] | 75 | canvas->drawCircle(x, y, SK_Scalar1 * 3 / 2, p); |
| 76 | if (paint.isVerticalText()) { |
| 77 | y += adv[i]; |
| 78 | } else { |
| 79 | x += adv[i]; |
| 80 | } |
| 81 | } |
| 82 | canvas->drawCircle(x, y, SK_Scalar1 * 3 / 2, p); |
| 83 | } |
| 84 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 85 | void onDraw(SkCanvas* canvas) override { |
reed@google.com | 14debba | 2011-11-10 20:06:01 +0000 | [diff] [blame] | 86 | SkScalar x = SkIntToScalar(100); |
| 87 | SkScalar y = SkIntToScalar(50); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 88 | |
reed@google.com | 14debba | 2011-11-10 20:06:01 +0000 | [diff] [blame] | 89 | for (int i = 0; i < 4; ++i) { |
bungeman@google.com | 8ff8a19 | 2012-09-25 20:38:28 +0000 | [diff] [blame] | 90 | SkPaint paint; |
reed@google.com | 14debba | 2011-11-10 20:06:01 +0000 | [diff] [blame] | 91 | paint.setAntiAlias(true); |
| 92 | paint.setTextSize(SkIntToScalar(TEXT_SIZE)); |
bungeman@google.com | 8ff8a19 | 2012-09-25 20:38:28 +0000 | [diff] [blame] | 93 | //paint.setTypeface(fFace); |
| 94 | //paint.setFakeBoldText(true); |
reed@google.com | 14debba | 2011-11-10 20:06:01 +0000 | [diff] [blame] | 95 | |
| 96 | paint.setVerticalText(false); |
| 97 | drawBaseline(canvas, paint, x, y); |
| 98 | canvas->drawText(gText, gLen, x, y, paint); |
| 99 | |
| 100 | paint.setVerticalText(true); |
| 101 | drawBaseline(canvas, paint, x, y); |
| 102 | canvas->drawText(gText, gLen, x, y, paint); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 103 | |
reed@google.com | 14debba | 2011-11-10 20:06:01 +0000 | [diff] [blame] | 104 | x += SkIntToScalar(40); |
| 105 | y += SkIntToScalar(120); |
| 106 | |
| 107 | canvas->rotate(SkIntToScalar(-15)); |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | private: |
| 112 | typedef GM INHERITED; |
| 113 | }; |
| 114 | |
| 115 | /////////////////////////////////////////////////////////////////////////////// |
| 116 | |
| 117 | static GM* MyFactory(void*) { return new VertTextGM; } |
| 118 | static GMRegistry reg(MyFactory); |
| 119 | |
| 120 | } |