jvanverth | 9564ce6 | 2014-09-16 05:45:19 -0700 | [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 | #include "gm.h" |
Mike Klein | 33d2055 | 2017-03-22 13:47:51 -0400 | [diff] [blame] | 8 | #include "sk_tool_utils.h" |
jvanverth | fca302c | 2014-10-20 13:12:54 -0700 | [diff] [blame] | 9 | #include "Resources.h" |
| 10 | #include "SkCanvas.h" |
| 11 | #include "SkStream.h" |
jvanverth | 9564ce6 | 2014-09-16 05:45:19 -0700 | [diff] [blame] | 12 | #include "SkSurface.h" |
| 13 | #include "SkTypeface.h" |
| 14 | |
jvanverth | fca302c | 2014-10-20 13:12:54 -0700 | [diff] [blame] | 15 | class DFTextGM : public skiagm::GM { |
jvanverth | 9564ce6 | 2014-09-16 05:45:19 -0700 | [diff] [blame] | 16 | public: |
| 17 | DFTextGM() { |
| 18 | this->setBGColor(0xFFFFFFFF); |
jvanverth | 9564ce6 | 2014-09-16 05:45:19 -0700 | [diff] [blame] | 19 | } |
| 20 | |
| 21 | protected: |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 22 | void onOnceBeforeDraw() override { |
bungeman | 13b9c95 | 2016-05-12 10:09:30 -0700 | [diff] [blame] | 23 | fEmojiTypeface = sk_tool_utils::emoji_typeface(); |
caryclark | c3dcb67 | 2015-07-21 12:27:36 -0700 | [diff] [blame] | 24 | fEmojiText = sk_tool_utils::emoji_sample_text(); |
jvanverth | fca302c | 2014-10-20 13:12:54 -0700 | [diff] [blame] | 25 | } |
| 26 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 27 | SkString onShortName() override { |
caryclark | c3dcb67 | 2015-07-21 12:27:36 -0700 | [diff] [blame] | 28 | SkString name("dftext"); |
Mike Klein | f436fbc | 2017-11-15 10:42:46 -0500 | [diff] [blame] | 29 | name.append(sk_tool_utils::platform_font_manager()); |
caryclark | c3dcb67 | 2015-07-21 12:27:36 -0700 | [diff] [blame] | 30 | return name; |
jvanverth | 9564ce6 | 2014-09-16 05:45:19 -0700 | [diff] [blame] | 31 | } |
| 32 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 33 | SkISize onISize() override { |
jvanverth | 9564ce6 | 2014-09-16 05:45:19 -0700 | [diff] [blame] | 34 | return SkISize::Make(1024, 768); |
| 35 | } |
| 36 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 37 | virtual void onDraw(SkCanvas* inputCanvas) override { |
jvanverth | ed14f1d | 2014-10-06 12:50:54 -0700 | [diff] [blame] | 38 | SkScalar textSizes[] = { 9.0f, 9.0f*2.0f, 9.0f*5.0f, 9.0f*2.0f*5.0f }; |
jvanverth | 9564ce6 | 2014-09-16 05:45:19 -0700 | [diff] [blame] | 39 | SkScalar scales[] = { 2.0f*5.0f, 5.0f, 2.0f, 1.0f }; |
| 40 | |
| 41 | // set up offscreen rendering with distance field text |
| 42 | #if SK_SUPPORT_GPU |
| 43 | GrContext* ctx = inputCanvas->getGrContext(); |
brianosman | 52ede1d | 2016-06-20 08:25:02 -0700 | [diff] [blame] | 44 | SkISize size = onISize(); |
| 45 | SkImageInfo info = SkImageInfo::MakeN32(size.width(), size.height(), kPremul_SkAlphaType, |
Mike Reed | 693fdbd | 2017-01-12 10:13:40 -0500 | [diff] [blame] | 46 | inputCanvas->imageInfo().refColorSpace()); |
brianosman | 3a0dbde | 2016-07-26 11:36:05 -0700 | [diff] [blame] | 47 | SkSurfaceProps props(SkSurfaceProps::kUseDeviceIndependentFonts_Flag, |
reed | 4a8126e | 2014-09-22 07:29:03 -0700 | [diff] [blame] | 48 | SkSurfaceProps::kLegacyFontHost_InitType); |
reed | e8f3062 | 2016-03-23 18:59:25 -0700 | [diff] [blame] | 49 | auto surface(SkSurface::MakeRenderTarget(ctx, SkBudgeted::kNo, info, 0, &props)); |
| 50 | SkCanvas* canvas = surface ? surface->getCanvas() : inputCanvas; |
jvanverth | f9669f4 | 2014-10-07 07:59:45 -0700 | [diff] [blame] | 51 | // init our new canvas with the old canvas's matrix |
| 52 | canvas->setMatrix(inputCanvas->getTotalMatrix()); |
jvanverth | 9564ce6 | 2014-09-16 05:45:19 -0700 | [diff] [blame] | 53 | #else |
| 54 | SkCanvas* canvas = inputCanvas; |
| 55 | #endif |
jvanverth | 76ce81e | 2014-09-22 14:26:53 -0700 | [diff] [blame] | 56 | // apply global scale to test glyph positioning |
| 57 | canvas->scale(1.05f, 1.05f); |
jvanverth | 9564ce6 | 2014-09-16 05:45:19 -0700 | [diff] [blame] | 58 | canvas->clear(0xffffffff); |
| 59 | |
| 60 | SkPaint paint; |
| 61 | paint.setAntiAlias(true); |
| 62 | paint.setSubpixelText(true); |
jvanverth | 4854d13 | 2015-06-22 06:46:56 -0700 | [diff] [blame] | 63 | |
mboc | ee6a991 | 2016-05-31 11:42:36 -0700 | [diff] [blame] | 64 | sk_tool_utils::set_portable_typeface(&paint, "serif"); |
jvanverth | 9564ce6 | 2014-09-16 05:45:19 -0700 | [diff] [blame] | 65 | |
| 66 | const char* text = "Hamburgefons"; |
| 67 | const size_t textLen = strlen(text); |
| 68 | |
| 69 | // check scaling up |
| 70 | SkScalar x = SkIntToScalar(0); |
| 71 | SkScalar y = SkIntToScalar(78); |
| 72 | for (size_t i = 0; i < SK_ARRAY_COUNT(textSizes); ++i) { |
| 73 | SkAutoCanvasRestore acr(canvas, true); |
| 74 | canvas->translate(x, y); |
| 75 | canvas->scale(scales[i], scales[i]); |
| 76 | paint.setTextSize(textSizes[i]); |
| 77 | canvas->drawText(text, textLen, 0, 0, paint); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 78 | y += paint.getFontMetrics(nullptr)*scales[i]; |
jvanverth | 9564ce6 | 2014-09-16 05:45:19 -0700 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | // check rotation |
| 82 | for (size_t i = 0; i < 5; ++i) { |
| 83 | SkScalar rotX = SkIntToScalar(10); |
| 84 | SkScalar rotY = y; |
| 85 | |
| 86 | SkAutoCanvasRestore acr(canvas, true); |
| 87 | canvas->translate(SkIntToScalar(10 + i * 200), -80); |
bungeman | 7438bfc | 2016-07-12 15:01:19 -0700 | [diff] [blame] | 88 | canvas->rotate(SkIntToScalar(i * 5), rotX, rotY); |
jvanverth | 9564ce6 | 2014-09-16 05:45:19 -0700 | [diff] [blame] | 89 | for (int ps = 6; ps <= 32; ps += 3) { |
| 90 | paint.setTextSize(SkIntToScalar(ps)); |
| 91 | canvas->drawText(text, textLen, rotX, rotY, paint); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 92 | rotY += paint.getFontMetrics(nullptr); |
jvanverth | 9564ce6 | 2014-09-16 05:45:19 -0700 | [diff] [blame] | 93 | } |
| 94 | } |
| 95 | |
| 96 | // check scaling down |
| 97 | paint.setLCDRenderText(true); |
jvanverth | 76ce81e | 2014-09-22 14:26:53 -0700 | [diff] [blame] | 98 | x = SkIntToScalar(680); |
jvanverth | 9564ce6 | 2014-09-16 05:45:19 -0700 | [diff] [blame] | 99 | y = SkIntToScalar(20); |
| 100 | size_t arraySize = SK_ARRAY_COUNT(textSizes); |
| 101 | for (size_t i = 0; i < arraySize; ++i) { |
| 102 | SkAutoCanvasRestore acr(canvas, true); |
| 103 | canvas->translate(x, y); |
| 104 | SkScalar scaleFactor = SkScalarInvert(scales[arraySize - i - 1]); |
| 105 | canvas->scale(scaleFactor, scaleFactor); |
| 106 | paint.setTextSize(textSizes[i]); |
| 107 | canvas->drawText(text, textLen, 0, 0, paint); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 108 | y += paint.getFontMetrics(nullptr)*scaleFactor; |
jvanverth | 9564ce6 | 2014-09-16 05:45:19 -0700 | [diff] [blame] | 109 | } |
| 110 | |
jvanverth | 76ce81e | 2014-09-22 14:26:53 -0700 | [diff] [blame] | 111 | // check pos text |
| 112 | { |
| 113 | SkAutoCanvasRestore acr(canvas, true); |
| 114 | |
| 115 | canvas->scale(2.0f, 2.0f); |
| 116 | |
bsalomon | fbaace0 | 2014-12-12 16:41:46 -0800 | [diff] [blame] | 117 | SkAutoTArray<SkPoint> pos(SkToInt(textLen)); |
| 118 | SkAutoTArray<SkScalar> widths(SkToInt(textLen)); |
jvanverth | 76ce81e | 2014-09-22 14:26:53 -0700 | [diff] [blame] | 119 | paint.setTextSize(textSizes[0]); |
| 120 | |
| 121 | paint.getTextWidths(text, textLen, &widths[0]); |
| 122 | |
| 123 | SkScalar x = SkIntToScalar(340); |
| 124 | SkScalar y = SkIntToScalar(75); |
| 125 | for (unsigned int i = 0; i < textLen; ++i) { |
| 126 | pos[i].set(x, y); |
| 127 | x += widths[i]; |
| 128 | } |
| 129 | |
| 130 | canvas->drawPosText(text, textLen, &pos[0], paint); |
| 131 | } |
| 132 | |
| 133 | |
jvanverth | 9564ce6 | 2014-09-16 05:45:19 -0700 | [diff] [blame] | 134 | // check gamma-corrected blending |
| 135 | const SkColor fg[] = { |
| 136 | 0xFFFFFFFF, |
| 137 | 0xFFFFFF00, 0xFFFF00FF, 0xFF00FFFF, |
| 138 | 0xFFFF0000, 0xFF00FF00, 0xFF0000FF, |
| 139 | 0xFF000000, |
| 140 | }; |
| 141 | |
caryclark | c3dcb67 | 2015-07-21 12:27:36 -0700 | [diff] [blame] | 142 | paint.setColor(0xFFF7F3F7); |
caryclark | ba9757a | 2015-07-23 14:07:58 -0700 | [diff] [blame] | 143 | SkRect r = SkRect::MakeLTRB(670, 215, 820, 397); |
jvanverth | 9564ce6 | 2014-09-16 05:45:19 -0700 | [diff] [blame] | 144 | canvas->drawRect(r, paint); |
| 145 | |
jvanverth | 76ce81e | 2014-09-22 14:26:53 -0700 | [diff] [blame] | 146 | x = SkIntToScalar(680); |
caryclark | ba9757a | 2015-07-23 14:07:58 -0700 | [diff] [blame] | 147 | y = SkIntToScalar(235); |
jvanverth | ed14f1d | 2014-10-06 12:50:54 -0700 | [diff] [blame] | 148 | paint.setTextSize(SkIntToScalar(19)); |
jvanverth | 9564ce6 | 2014-09-16 05:45:19 -0700 | [diff] [blame] | 149 | for (size_t i = 0; i < SK_ARRAY_COUNT(fg); ++i) { |
| 150 | paint.setColor(fg[i]); |
| 151 | |
| 152 | canvas->drawText(text, textLen, x, y, paint); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 153 | y += paint.getFontMetrics(nullptr); |
jvanverth | 9564ce6 | 2014-09-16 05:45:19 -0700 | [diff] [blame] | 154 | } |
| 155 | |
caryclark | c3dcb67 | 2015-07-21 12:27:36 -0700 | [diff] [blame] | 156 | paint.setColor(0xFF181C18); |
caryclark | ba9757a | 2015-07-23 14:07:58 -0700 | [diff] [blame] | 157 | r = SkRect::MakeLTRB(820, 215, 970, 397); |
jvanverth | 9564ce6 | 2014-09-16 05:45:19 -0700 | [diff] [blame] | 158 | canvas->drawRect(r, paint); |
| 159 | |
jvanverth | 76ce81e | 2014-09-22 14:26:53 -0700 | [diff] [blame] | 160 | x = SkIntToScalar(830); |
caryclark | ba9757a | 2015-07-23 14:07:58 -0700 | [diff] [blame] | 161 | y = SkIntToScalar(235); |
jvanverth | ed14f1d | 2014-10-06 12:50:54 -0700 | [diff] [blame] | 162 | paint.setTextSize(SkIntToScalar(19)); |
jvanverth | 9564ce6 | 2014-09-16 05:45:19 -0700 | [diff] [blame] | 163 | for (size_t i = 0; i < SK_ARRAY_COUNT(fg); ++i) { |
| 164 | paint.setColor(fg[i]); |
| 165 | |
| 166 | canvas->drawText(text, textLen, x, y, paint); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 167 | y += paint.getFontMetrics(nullptr); |
jvanverth | 9564ce6 | 2014-09-16 05:45:19 -0700 | [diff] [blame] | 168 | } |
| 169 | |
jvanverth | 354eba5 | 2015-03-16 11:32:49 -0700 | [diff] [blame] | 170 | // check skew |
| 171 | { |
| 172 | paint.setLCDRenderText(false); |
| 173 | SkAutoCanvasRestore acr(canvas, true); |
| 174 | canvas->skew(0.0f, 0.151515f); |
jvanverth | d68a550 | 2015-03-16 12:58:43 -0700 | [diff] [blame] | 175 | paint.setTextSize(SkIntToScalar(32)); |
jvanverth | 354eba5 | 2015-03-16 11:32:49 -0700 | [diff] [blame] | 176 | canvas->drawText(text, textLen, 745, 70, paint); |
| 177 | } |
| 178 | { |
| 179 | paint.setLCDRenderText(true); |
| 180 | SkAutoCanvasRestore acr(canvas, true); |
| 181 | canvas->skew(0.5f, 0.0f); |
jvanverth | d68a550 | 2015-03-16 12:58:43 -0700 | [diff] [blame] | 182 | paint.setTextSize(SkIntToScalar(32)); |
caryclark | ba9757a | 2015-07-23 14:07:58 -0700 | [diff] [blame] | 183 | canvas->drawText(text, textLen, 580, 125, paint); |
jvanverth | 354eba5 | 2015-03-16 11:32:49 -0700 | [diff] [blame] | 184 | } |
| 185 | |
jvanverth | fca302c | 2014-10-20 13:12:54 -0700 | [diff] [blame] | 186 | // check color emoji |
caryclark | c3dcb67 | 2015-07-21 12:27:36 -0700 | [diff] [blame] | 187 | if (fEmojiTypeface) { |
| 188 | paint.setTypeface(fEmojiTypeface); |
| 189 | paint.setTextSize(SkIntToScalar(19)); |
Cary Clark | 2a475ea | 2017-04-28 15:35:12 -0400 | [diff] [blame] | 190 | canvas->drawString(fEmojiText, 670, 90, paint); |
caryclark | c3dcb67 | 2015-07-21 12:27:36 -0700 | [diff] [blame] | 191 | } |
jvanverth | 9564ce6 | 2014-09-16 05:45:19 -0700 | [diff] [blame] | 192 | #if SK_SUPPORT_GPU |
| 193 | // render offscreen buffer |
jvanverth | 76ce81e | 2014-09-22 14:26:53 -0700 | [diff] [blame] | 194 | if (surface) { |
jvanverth | f9669f4 | 2014-10-07 07:59:45 -0700 | [diff] [blame] | 195 | SkAutoCanvasRestore acr(inputCanvas, true); |
| 196 | // since we prepended this matrix already, we blit using identity |
| 197 | inputCanvas->resetMatrix(); |
reed | 9ce9d67 | 2016-03-17 10:51:11 -0700 | [diff] [blame] | 198 | inputCanvas->drawImage(surface->makeImageSnapshot().get(), 0, 0, nullptr); |
jvanverth | 76ce81e | 2014-09-22 14:26:53 -0700 | [diff] [blame] | 199 | } |
jvanverth | 9564ce6 | 2014-09-16 05:45:19 -0700 | [diff] [blame] | 200 | #endif |
| 201 | } |
| 202 | |
| 203 | private: |
bungeman | 13b9c95 | 2016-05-12 10:09:30 -0700 | [diff] [blame] | 204 | sk_sp<SkTypeface> fEmojiTypeface; |
caryclark | c3dcb67 | 2015-07-21 12:27:36 -0700 | [diff] [blame] | 205 | const char* fEmojiText; |
jvanverth | fca302c | 2014-10-20 13:12:54 -0700 | [diff] [blame] | 206 | |
| 207 | typedef skiagm::GM INHERITED; |
jvanverth | 9564ce6 | 2014-09-16 05:45:19 -0700 | [diff] [blame] | 208 | }; |
| 209 | |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 210 | DEF_GM(return new DFTextGM;) |