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