Chromium Code Reviews| 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 "GrContext.h" | |
| 11 #include "Resources.h" | |
| 12 #include "SkCanvas.h" | |
| 13 #include "SkGradientShader.h" | |
| 14 #include "SkStream.h" | |
| 15 #include "SkSurface.h" | |
| 16 #include "SkTextBlob.h" | |
| 17 #include "SkTypeface.h" | |
| 18 #include "../src/fonts/SkRandomScalerContext.h" | |
| 19 | |
| 20 namespace skiagm { | |
| 21 class TextBlobRandomFont : public GM { | |
| 22 public: | |
| 23 // This gm tests that textblobs can be translated and scaled with a font tha t returns random | |
| 24 // but deterministic masks | |
| 25 TextBlobRandomFont() { } | |
| 26 | |
| 27 protected: | |
| 28 void onOnceBeforeDraw() override { | |
| 29 SkTextBlobBuilder builder; | |
| 30 | |
| 31 const char* text = "The quick brown fox jumps over the lazy yellow dog." ; | |
|
bsalomon
2015/07/31 21:06:23
yellow?
| |
| 32 | |
| 33 // make textbloben | |
| 34 SkPaint paint; | |
| 35 paint.setTextSize(32); | |
| 36 paint.setLCDRenderText(true); | |
| 37 | |
| 38 // Setup our random scaler context | |
| 39 SkAutoTUnref<SkTypeface> orig(sk_tool_utils::create_portable_typeface("s ans-serif", | |
| 40 Sk Typeface::kBold)); | |
| 41 if (NULL == orig) { | |
| 42 orig.reset(SkTypeface::RefDefault()); | |
| 43 } | |
| 44 SkAutoTUnref<SkTypeface> random(SkNEW_ARGS(SkRandomTypeface, (orig, pain t, false))); | |
| 45 paint.setTypeface(random); | |
| 46 | |
| 47 SkRect bounds; | |
| 48 paint.measureText(text, strlen(text), &bounds); | |
| 49 | |
| 50 SkScalar yOffset = bounds.height(); | |
| 51 sk_tool_utils::add_to_text_blob(&builder, text, paint, 0, 0); | |
| 52 | |
| 53 // A8 | |
| 54 paint.setSubpixelText(false); | |
| 55 paint.setLCDRenderText(false); | |
| 56 sk_tool_utils::add_to_text_blob(&builder, text, paint, 0, yOffset - 32); | |
| 57 | |
| 58 // build | |
| 59 fBlob.reset(builder.build()); | |
| 60 } | |
| 61 | |
| 62 SkString onShortName() override { | |
| 63 return SkString("textblobrandomfont"); | |
| 64 } | |
| 65 | |
| 66 SkISize onISize() override { | |
| 67 return SkISize::Make(kWidth, kHeight); | |
| 68 } | |
| 69 | |
| 70 void onDraw(SkCanvas* canvas) override { | |
| 71 // This GM exists to test a specific feature of the GPU backend. | |
| 72 if (NULL == canvas->getGrContext()) { | |
| 73 this->drawGpuOnlyMessage(canvas); | |
| 74 return; | |
| 75 } | |
| 76 | |
| 77 canvas->drawColor(sk_tool_utils::color_to_565(SK_ColorWHITE)); | |
| 78 | |
| 79 SkImageInfo info = SkImageInfo::MakeN32Premul(kWidth, kHeight); | |
| 80 SkSurfaceProps props(0, kUnknown_SkPixelGeometry); | |
| 81 SkAutoTUnref<SkSurface> surface(canvas->newSurface(info, &props)); | |
| 82 if (surface) { | |
| 83 SkPaint paint; | |
| 84 paint.setAntiAlias(true); | |
| 85 | |
| 86 SkCanvas* c = surface->getCanvas(); | |
| 87 | |
| 88 int stride = SkScalarCeilToInt(fBlob->bounds().height() / 2) + 10; | |
| 89 int yOffset = stride; | |
| 90 for (int i = 0; i < 10; i++) { | |
| 91 // fiddle the canvas to force regen of textblobs | |
| 92 canvas->rotate(i % 2 ? 0 : -0.05); | |
| 93 canvas->drawTextBlob(fBlob, 10, yOffset, paint); | |
| 94 yOffset += stride; | |
| 95 | |
| 96 // This will draw as black boxes | |
| 97 c->drawTextBlob(fBlob, 10, yOffset, paint); | |
| 98 surface->draw(canvas, 0, 0, nullptr); | |
| 99 | |
| 100 // free gpu resources and verify | |
| 101 yOffset += stride; | |
| 102 canvas->getGrContext()->freeGpuResources(); | |
| 103 canvas->drawTextBlob(fBlob, 10, yOffset, paint); | |
| 104 | |
| 105 yOffset += stride; | |
| 106 } | |
| 107 | |
| 108 } else { | |
| 109 const char* text = "This test requires a surface"; | |
| 110 size_t len = strlen(text); | |
| 111 SkPaint paint; | |
| 112 canvas->drawText(text, len, 10, 100, paint); | |
| 113 } | |
| 114 } | |
| 115 | |
| 116 private: | |
| 117 SkAutoTUnref<const SkTextBlob> fBlob; | |
| 118 | |
| 119 static const int kWidth = 1000; | |
| 120 static const int kHeight = 1000; | |
| 121 | |
| 122 typedef GM INHERITED; | |
| 123 }; | |
| 124 | |
| 125 ////////////////////////////////////////////////////////////////////////////// | |
| 126 | |
| 127 DEF_GM( return SkNEW(TextBlobRandomFont); ) | |
| 128 } | |
| OLD | NEW |