OLD | NEW |
(Empty) | |
| 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 tha
t 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 |
| 42 SkAutoTUnref<SkTypeface> orig(sk_tool_utils::create_portable_typeface("s
ans-serif", |
| 43 Sk
Typeface::kBold)); |
| 44 if (NULL == orig) { |
| 45 orig.reset(SkTypeface::RefDefault()); |
| 46 } |
| 47 SkAutoTUnref<SkTypeface> random(SkNEW_ARGS(SkRandomTypeface, (orig, pain
t, false))); |
| 48 paint.setTypeface(random); |
| 49 |
| 50 SkRect bounds; |
| 51 paint.measureText(text, strlen(text), &bounds); |
| 52 sk_tool_utils::add_to_text_blob(&builder, text, paint, 0, 0); |
| 53 |
| 54 // A8 |
| 55 const char* bigtext1 = "The quick brown fox"; |
| 56 const char* bigtext2 = "jumps over the lazy dog."; |
| 57 paint.setTextSize(160); |
| 58 paint.setSubpixelText(false); |
| 59 paint.setLCDRenderText(false); |
| 60 paint.measureText(bigtext1, strlen(bigtext1), &bounds); |
| 61 SkScalar offset = bounds.height(); |
| 62 sk_tool_utils::add_to_text_blob(&builder, bigtext1, paint, 0, offset); |
| 63 |
| 64 paint.measureText(bigtext2, strlen(bigtext2), &bounds); |
| 65 offset += bounds.height(); |
| 66 sk_tool_utils::add_to_text_blob(&builder, bigtext2, paint, 0, offset); |
| 67 |
| 68 // build |
| 69 fBlob.reset(builder.build()); |
| 70 } |
| 71 |
| 72 SkString onShortName() override { |
| 73 return SkString("textblobrandomfont"); |
| 74 } |
| 75 |
| 76 SkISize onISize() override { |
| 77 return SkISize::Make(kWidth, kHeight); |
| 78 } |
| 79 |
| 80 void onDraw(SkCanvas* canvas) override { |
| 81 // This GM exists to test a specific feature of the GPU backend. |
| 82 if (NULL == canvas->getGrContext()) { |
| 83 this->drawGpuOnlyMessage(canvas); |
| 84 return; |
| 85 } |
| 86 |
| 87 canvas->drawColor(sk_tool_utils::color_to_565(SK_ColorWHITE)); |
| 88 |
| 89 SkImageInfo info = SkImageInfo::MakeN32Premul(kWidth, kHeight); |
| 90 SkSurfaceProps props(0, kUnknown_SkPixelGeometry); |
| 91 SkAutoTUnref<SkSurface> surface(canvas->newSurface(info, &props)); |
| 92 if (surface) { |
| 93 SkPaint paint; |
| 94 paint.setAntiAlias(true); |
| 95 |
| 96 SkCanvas* c = surface->getCanvas(); |
| 97 |
| 98 int stride = SkScalarCeilToInt(fBlob->bounds().height()); |
| 99 int yOffset = stride / 8; |
| 100 for (int i = 0; i < 1; i++) { |
| 101 // fiddle the canvas to force regen of textblobs |
| 102 canvas->rotate(i % 2 ? 0.0f : -0.05f); |
| 103 canvas->drawTextBlob(fBlob, 10.0f, SkIntToScalar(yOffset), paint
); |
| 104 yOffset += stride; |
| 105 |
| 106 // This will draw as black boxes |
| 107 c->drawTextBlob(fBlob, 10, SkIntToScalar(yOffset), paint); |
| 108 surface->draw(canvas, 0, 0, nullptr); |
| 109 |
| 110 // free gpu resources and verify |
| 111 yOffset += stride; |
| 112 canvas->getGrContext()->freeGpuResources(); |
| 113 canvas->drawTextBlob(fBlob, 10, SkIntToScalar(yOffset), paint); |
| 114 |
| 115 yOffset += stride; |
| 116 } |
| 117 |
| 118 } else { |
| 119 const char* text = "This test requires a surface"; |
| 120 size_t len = strlen(text); |
| 121 SkPaint paint; |
| 122 canvas->drawText(text, len, 10, 100, paint); |
| 123 } |
| 124 } |
| 125 |
| 126 private: |
| 127 SkAutoTUnref<const SkTextBlob> fBlob; |
| 128 |
| 129 static const int kWidth = 2000; |
| 130 static const int kHeight = 1600; |
| 131 |
| 132 typedef GM INHERITED; |
| 133 }; |
| 134 |
| 135 ////////////////////////////////////////////////////////////////////////////// |
| 136 |
| 137 DEF_GM( return SkNEW(TextBlobRandomFont); ) |
| 138 } |
| 139 #endif |
OLD | NEW |