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 "Resources.h" | |
| 11 #include "SkBlurMask.h" | |
| 12 #include "SkBlurMaskFilter.h" | |
| 13 #include "SkCanvas.h" | |
| 14 #include "SkGradientShader.h" | |
| 15 #include "SkImage.h" | |
| 16 #include "SkRandom.h" | |
| 17 #include "SkStream.h" | |
| 18 #include "SkSurface.h" | |
| 19 #include "SkTextBlob.h" | |
| 20 #include "SkTypeface.h" | |
| 21 | |
| 22 namespace skiagm { | |
| 23 class TextBlobMixedSizes : public GM { | |
| 24 public: | |
| 25 // This gm tests that textblobs of mixed sizes with a large glyph will rende r properly | |
| 26 TextBlobMixedSizes(bool useDFT) : fUseDFT(useDFT) {} | |
| 27 | |
| 28 protected: | |
| 29 void onOnceBeforeDraw() override { | |
| 30 SkAutoTUnref<SkTypeface> typeface(GetResourceAsTypeface("/fonts/HangingS .ttf")); | |
| 31 SkTextBlobBuilder builder; | |
| 32 | |
| 33 // make textblob. To stress distance fields, we choose sizes appropriat ely | |
| 34 SkPaint paint; | |
| 35 paint.setAntiAlias(true); | |
| 36 paint.setSubpixelText(true); | |
| 37 paint.setLCDRenderText(true); | |
| 38 paint.setTypeface(typeface); | |
| 39 | |
| 40 const char* text = "Sk"; | |
| 41 | |
| 42 // extra large | |
| 43 paint.setTextSize(262); | |
| 44 sk_tool_utils::add_to_text_blob(&builder, text, paint, 0, 0); | |
| 45 | |
| 46 // large | |
| 47 SkRect bounds; | |
| 48 paint.measureText(text, strlen(text), &bounds); | |
| 49 SkScalar yOffset = bounds.height(); | |
| 50 paint.setTextSize(162); | |
| 51 | |
| 52 sk_tool_utils::add_to_text_blob(&builder, text, paint, 0, yOffset); | |
| 53 | |
| 54 // Medium | |
| 55 paint.measureText(text, strlen(text), &bounds); | |
| 56 yOffset += bounds.height(); | |
| 57 paint.setTextSize(72); | |
|
robertphillips
2015/08/26 17:33:58
add line ?
| |
| 58 sk_tool_utils::add_to_text_blob(&builder, text, paint, 0, yOffset); | |
| 59 | |
| 60 // Small | |
| 61 paint.measureText(text, strlen(text), &bounds); | |
| 62 yOffset += bounds.height(); | |
| 63 | |
|
robertphillips
2015/08/26 17:33:58
rm line ?
| |
| 64 paint.setTextSize(32); | |
|
robertphillips
2015/08/26 17:33:58
add line ?
| |
| 65 sk_tool_utils::add_to_text_blob(&builder, text, paint, 0, yOffset); | |
| 66 | |
| 67 // micro (will fall out of distance field text even if distance field te xt is enabled) | |
| 68 paint.measureText(text, strlen(text), &bounds); | |
| 69 yOffset += bounds.height(); | |
| 70 | |
| 71 paint.setTextSize(14); | |
| 72 sk_tool_utils::add_to_text_blob(&builder, text, paint, 0, yOffset); | |
| 73 | |
| 74 // build | |
| 75 fBlob.reset(builder.build()); | |
| 76 } | |
| 77 | |
| 78 SkString onShortName() override { | |
| 79 SkString name("textblobmixedsizes"); | |
| 80 if (fUseDFT) { | |
| 81 name.appendf("_df"); | |
| 82 } | |
| 83 return name; | |
| 84 } | |
| 85 | |
| 86 SkISize onISize() override { | |
| 87 return SkISize::Make(kWidth, kHeight); | |
| 88 } | |
| 89 | |
| 90 void onDraw(SkCanvas* inputCanvas) override { | |
| 91 SkCanvas* canvas = inputCanvas; | |
| 92 SkAutoTUnref<SkSurface> surface; | |
| 93 if (fUseDFT) { | |
| 94 #if SK_SUPPORT_GPU | |
|
robertphillips
2015/08/26 17:33:58
// Create a new Canvas to enable DFT
?
| |
| 95 GrContext* ctx = inputCanvas->getGrContext(); | |
| 96 SkImageInfo info = SkImageInfo::MakeN32Premul(onISize()); | |
| 97 SkSurfaceProps props(SkSurfaceProps::kUseDistanceFieldFonts_Flag, | |
| 98 SkSurfaceProps::kLegacyFontHost_InitType); | |
| 99 surface.reset(SkSurface::NewRenderTarget(ctx, SkSurface::kNo_Budgete d, info, 0, | |
| 100 &props)); | |
| 101 canvas = surface.get() ? surface->getCanvas() : inputCanvas; | |
| 102 // init our new canvas with the old canvas's matrix | |
| 103 canvas->setMatrix(inputCanvas->getTotalMatrix()); | |
| 104 canvas->drawColor(sk_tool_utils::color_to_565(SK_ColorWHITE)); | |
| 105 #endif | |
| 106 } else { | |
| 107 canvas->drawColor(sk_tool_utils::color_to_565(SK_ColorBLACK)); | |
| 108 } | |
| 109 | |
| 110 SkRect bounds = fBlob->bounds(); | |
| 111 | |
| 112 static const int kPadX = bounds.width() / 3; | |
| 113 static const int kPadY = bounds.height() / 3; | |
| 114 | |
| 115 int rowCount = 0; | |
| 116 canvas->translate(SkIntToScalar(kPadX), SkIntToScalar(kPadY)); | |
| 117 canvas->save(); | |
| 118 SkRandom random; | |
| 119 | |
| 120 SkPaint paint; | |
| 121 paint.setAntiAlias(true); | |
| 122 | |
| 123 static const SkScalar kSigma = SkBlurMask::ConvertRadiusToSigma(SkIntToS calar(8)); | |
| 124 | |
| 125 // setup blur paint | |
| 126 SkPaint blurPaint(paint); | |
| 127 blurPaint.setColor(sk_tool_utils::color_to_565(SK_ColorWHITE)); | |
| 128 SkAutoTUnref<SkMaskFilter> mf(SkBlurMaskFilter::Create(kNormal_SkBlurSty le, kSigma)); | |
| 129 blurPaint.setMaskFilter(mf); | |
| 130 | |
| 131 for (int i = 0; i < 4; i++) { | |
| 132 canvas->save(); | |
|
robertphillips
2015/08/26 17:33:58
Can this just be "i % 2" ?
| |
| 133 switch (random.nextU() % 2) { | |
| 134 case 0: | |
| 135 canvas->rotate(random.nextF() * 45.f); | |
| 136 break; | |
| 137 case 1: | |
| 138 canvas->rotate(-random.nextF() * 45.f); | |
| 139 break; | |
| 140 } | |
| 141 if (!fUseDFT) { | |
| 142 canvas->drawTextBlob(fBlob, 0, 0, blurPaint); | |
| 143 } | |
| 144 canvas->drawTextBlob(fBlob, 0, 0, paint); | |
| 145 canvas->restore(); | |
| 146 canvas->translate(bounds.width() + SK_Scalar1 * kPadX, 0); | |
| 147 ++rowCount; | |
| 148 if ((bounds.width() + 2 * kPadX) * rowCount > kWidth) { | |
| 149 canvas->restore(); | |
| 150 canvas->translate(0, bounds.height() + SK_Scalar1 * kPadY); | |
| 151 canvas->save(); | |
| 152 rowCount = 0; | |
| 153 } | |
| 154 } | |
| 155 canvas->restore(); | |
| 156 | |
| 157 #if SK_SUPPORT_GPU | |
| 158 // render offscreen buffer | |
| 159 if (surface) { | |
| 160 SkAutoCanvasRestore acr(inputCanvas, true); | |
| 161 // since we prepended this matrix already, we blit using identity | |
| 162 inputCanvas->resetMatrix(); | |
| 163 SkImage* image = surface->newImageSnapshot(); | |
| 164 inputCanvas->drawImage(image, 0, 0, NULL); | |
| 165 image->unref(); | |
| 166 } | |
| 167 #endif | |
| 168 } | |
| 169 | |
| 170 private: | |
| 171 SkAutoTUnref<const SkTextBlob> fBlob; | |
| 172 | |
| 173 static const int kWidth = 2000; | |
| 174 static const int kHeight = 2000; | |
| 175 | |
| 176 bool fUseDFT; | |
| 177 | |
| 178 typedef GM INHERITED; | |
| 179 }; | |
| 180 | |
| 181 ////////////////////////////////////////////////////////////////////////////// | |
| 182 | |
| 183 DEF_GM( return SkNEW_ARGS(TextBlobMixedSizes, (false)); ) | |
| 184 #if SK_SUPPORT_GPU | |
| 185 DEF_GM( return SkNEW_ARGS(TextBlobMixedSizes, (true)); ) | |
| 186 #endif | |
| 187 } | |
| OLD | NEW |