joshualitt | 44c4851 | 2015-08-01 07:33:41 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2015 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.h" |
| 9 | |
| 10 | #include "Resources.h" |
| 11 | #include "SkCanvas.h" |
| 12 | #include "SkGradientShader.h" |
| 13 | #include "SkStream.h" |
| 14 | #include "SkSurface.h" |
| 15 | #include "SkTextBlob.h" |
| 16 | #include "SkTypeface.h" |
| 17 | #include "../src/fonts/SkRandomScalerContext.h" |
| 18 | |
| 19 | #if SK_SUPPORT_GPU |
| 20 | |
| 21 | #include "GrContext.h" |
| 22 | |
| 23 | namespace skiagm { |
| 24 | class TextBlobRandomFont : public GM { |
| 25 | public: |
| 26 | // This gm tests that textblobs can be translated and scaled with a font that returns random |
| 27 | // but deterministic masks |
| 28 | TextBlobRandomFont() { } |
| 29 | |
| 30 | protected: |
| 31 | void onOnceBeforeDraw() override { |
| 32 | SkTextBlobBuilder builder; |
| 33 | |
| 34 | const char* text = "The quick brown fox jumps over the lazy dog."; |
| 35 | |
| 36 | // make textbloben |
| 37 | SkPaint paint; |
| 38 | paint.setTextSize(32); |
| 39 | paint.setLCDRenderText(true); |
| 40 | |
| 41 | // Setup our random scaler context |
mboc | ee6a991 | 2016-05-31 11:42:36 -0700 | [diff] [blame] | 42 | sk_sp<SkTypeface> orig(sk_tool_utils::create_portable_typeface( |
| 43 | "sans-serif", SkFontStyle::FromOldStyle(SkTypeface::kBold))); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 44 | if (nullptr == orig) { |
bungeman | 13b9c95 | 2016-05-12 10:09:30 -0700 | [diff] [blame] | 45 | orig = SkTypeface::MakeDefault(); |
joshualitt | 44c4851 | 2015-08-01 07:33:41 -0700 | [diff] [blame] | 46 | } |
bungeman | 13b9c95 | 2016-05-12 10:09:30 -0700 | [diff] [blame] | 47 | paint.setTypeface(sk_make_sp<SkRandomTypeface>(orig, paint, false)); |
joshualitt | 44c4851 | 2015-08-01 07:33:41 -0700 | [diff] [blame] | 48 | |
| 49 | SkRect bounds; |
| 50 | paint.measureText(text, strlen(text), &bounds); |
joshualitt | 44c4851 | 2015-08-01 07:33:41 -0700 | [diff] [blame] | 51 | sk_tool_utils::add_to_text_blob(&builder, text, paint, 0, 0); |
| 52 | |
| 53 | // A8 |
joshualitt | d45fb5a | 2015-08-01 10:33:40 -0700 | [diff] [blame] | 54 | const char* bigtext1 = "The quick brown fox"; |
| 55 | const char* bigtext2 = "jumps over the lazy dog."; |
| 56 | paint.setTextSize(160); |
joshualitt | 44c4851 | 2015-08-01 07:33:41 -0700 | [diff] [blame] | 57 | paint.setSubpixelText(false); |
| 58 | paint.setLCDRenderText(false); |
joshualitt | d45fb5a | 2015-08-01 10:33:40 -0700 | [diff] [blame] | 59 | paint.measureText(bigtext1, strlen(bigtext1), &bounds); |
| 60 | SkScalar offset = bounds.height(); |
| 61 | sk_tool_utils::add_to_text_blob(&builder, bigtext1, paint, 0, offset); |
| 62 | |
| 63 | paint.measureText(bigtext2, strlen(bigtext2), &bounds); |
| 64 | offset += bounds.height(); |
| 65 | sk_tool_utils::add_to_text_blob(&builder, bigtext2, paint, 0, offset); |
| 66 | |
| 67 | // color emoji |
bungeman | 13b9c95 | 2016-05-12 10:09:30 -0700 | [diff] [blame] | 68 | sk_sp<SkTypeface> origEmoji = sk_tool_utils::emoji_typeface(); |
joshualitt | d45fb5a | 2015-08-01 10:33:40 -0700 | [diff] [blame] | 69 | const char* osName = sk_tool_utils::platform_os_name(); |
| 70 | // The mac emoji string will break us |
| 71 | if (origEmoji && (!strcmp(osName, "Android") || !strcmp(osName, "Ubuntu"))) { |
| 72 | const char* emojiText = sk_tool_utils::emoji_sample_text(); |
| 73 | paint.measureText(emojiText, strlen(emojiText), &bounds); |
| 74 | offset += bounds.height(); |
bungeman | 13b9c95 | 2016-05-12 10:09:30 -0700 | [diff] [blame] | 75 | paint.setTypeface(sk_make_sp<SkRandomTypeface>(orig, paint, false)); |
joshualitt | d45fb5a | 2015-08-01 10:33:40 -0700 | [diff] [blame] | 76 | sk_tool_utils::add_to_text_blob(&builder, emojiText, paint, 0, offset); |
| 77 | } |
joshualitt | 44c4851 | 2015-08-01 07:33:41 -0700 | [diff] [blame] | 78 | |
| 79 | // build |
fmalita | 37283c2 | 2016-09-13 10:00:23 -0700 | [diff] [blame] | 80 | fBlob = builder.make(); |
joshualitt | 44c4851 | 2015-08-01 07:33:41 -0700 | [diff] [blame] | 81 | } |
| 82 | |
| 83 | SkString onShortName() override { |
| 84 | return SkString("textblobrandomfont"); |
| 85 | } |
| 86 | |
| 87 | SkISize onISize() override { |
| 88 | return SkISize::Make(kWidth, kHeight); |
| 89 | } |
| 90 | |
| 91 | void onDraw(SkCanvas* canvas) override { |
| 92 | // This GM exists to test a specific feature of the GPU backend. |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 93 | if (nullptr == canvas->getGrContext()) { |
halcanary | 2a24338 | 2015-09-09 08:16:41 -0700 | [diff] [blame] | 94 | skiagm::GM::DrawGpuOnlyMessage(canvas); |
joshualitt | 44c4851 | 2015-08-01 07:33:41 -0700 | [diff] [blame] | 95 | return; |
| 96 | } |
| 97 | |
| 98 | canvas->drawColor(sk_tool_utils::color_to_565(SK_ColorWHITE)); |
| 99 | |
brianosman | 52ede1d | 2016-06-20 08:25:02 -0700 | [diff] [blame] | 100 | SkImageInfo info = SkImageInfo::MakeN32(kWidth, kHeight, kPremul_SkAlphaType, |
| 101 | sk_ref_sp(canvas->imageInfo().colorSpace())); |
brianosman | 3a0dbde | 2016-07-26 11:36:05 -0700 | [diff] [blame] | 102 | SkSurfaceProps props(0, kUnknown_SkPixelGeometry); |
reed | e8f3062 | 2016-03-23 18:59:25 -0700 | [diff] [blame] | 103 | auto surface(canvas->makeSurface(info, &props)); |
joshualitt | 44c4851 | 2015-08-01 07:33:41 -0700 | [diff] [blame] | 104 | if (surface) { |
| 105 | SkPaint paint; |
| 106 | paint.setAntiAlias(true); |
| 107 | |
| 108 | SkCanvas* c = surface->getCanvas(); |
| 109 | |
joshualitt | d45fb5a | 2015-08-01 10:33:40 -0700 | [diff] [blame] | 110 | int stride = SkScalarCeilToInt(fBlob->bounds().height()); |
| 111 | int yOffset = stride / 8; |
| 112 | for (int i = 0; i < 1; i++) { |
joshualitt | 44c4851 | 2015-08-01 07:33:41 -0700 | [diff] [blame] | 113 | // fiddle the canvas to force regen of textblobs |
| 114 | canvas->rotate(i % 2 ? 0.0f : -0.05f); |
| 115 | canvas->drawTextBlob(fBlob, 10.0f, SkIntToScalar(yOffset), paint); |
| 116 | yOffset += stride; |
| 117 | |
| 118 | // This will draw as black boxes |
| 119 | c->drawTextBlob(fBlob, 10, SkIntToScalar(yOffset), paint); |
| 120 | surface->draw(canvas, 0, 0, nullptr); |
| 121 | |
| 122 | // free gpu resources and verify |
| 123 | yOffset += stride; |
| 124 | canvas->getGrContext()->freeGpuResources(); |
| 125 | canvas->drawTextBlob(fBlob, 10, SkIntToScalar(yOffset), paint); |
| 126 | |
| 127 | yOffset += stride; |
| 128 | } |
| 129 | |
| 130 | } else { |
| 131 | const char* text = "This test requires a surface"; |
| 132 | size_t len = strlen(text); |
| 133 | SkPaint paint; |
| 134 | canvas->drawText(text, len, 10, 100, paint); |
| 135 | } |
| 136 | } |
| 137 | |
| 138 | private: |
fmalita | 37283c2 | 2016-09-13 10:00:23 -0700 | [diff] [blame] | 139 | sk_sp<SkTextBlob> fBlob; |
joshualitt | 44c4851 | 2015-08-01 07:33:41 -0700 | [diff] [blame] | 140 | |
mtklein | dbfd7ab | 2016-09-01 11:24:54 -0700 | [diff] [blame] | 141 | static constexpr int kWidth = 2000; |
| 142 | static constexpr int kHeight = 1600; |
joshualitt | 44c4851 | 2015-08-01 07:33:41 -0700 | [diff] [blame] | 143 | |
| 144 | typedef GM INHERITED; |
| 145 | }; |
| 146 | |
| 147 | ////////////////////////////////////////////////////////////////////////////// |
| 148 | |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 149 | DEF_GM(return new TextBlobRandomFont;) |
joshualitt | 44c4851 | 2015-08-01 07:33:41 -0700 | [diff] [blame] | 150 | } |
| 151 | #endif |