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/gm.h" 9 #include "include/core/SkCanvas.h" 10 #include "include/core/SkColor.h" 11 #include "include/core/SkColorSpace.h" 12 #include "include/core/SkFont.h" 13 #include "include/core/SkFontStyle.h" 14 #include "include/core/SkFontTypes.h" 15 #include "include/core/SkImageInfo.h" 16 #include "include/core/SkPaint.h" 17 #include "include/core/SkRect.h" 18 #include "include/core/SkRefCnt.h" 19 #include "include/core/SkScalar.h" 20 #include "include/core/SkSize.h" 21 #include "include/core/SkString.h" 22 #include "include/core/SkSurface.h" 23 #include "include/core/SkSurfaceProps.h" 24 #include "include/core/SkTextBlob.h" 25 #include "include/core/SkTypeface.h" 26 #include "include/gpu/ganesh/GrDirectContext.h" 27 #include "include/gpu/ganesh/GrRecordingContext.h" 28 #include "tools/ToolUtils.h" 29 #include "tools/fonts/FontToolUtils.h" 30 #include "tools/fonts/RandomScalerContext.h" 31 32 #include <string.h> 33 #include <utility> 34 35 namespace skiagm { 36 class TextBlobRandomFont : public GM { 37 public: 38 // This gm tests that textblobs can be translated and scaled with a font that returns random 39 // but deterministic masks TextBlobRandomFont()40 TextBlobRandomFont() { } 41 42 protected: onOnceBeforeDraw()43 void onOnceBeforeDraw() override { 44 SkTextBlobBuilder builder; 45 46 const char* text = "The quick brown fox jumps over the lazy dog."; 47 48 SkPaint paint; 49 paint.setAntiAlias(true); 50 paint.setColor(SK_ColorMAGENTA); 51 52 // make textbloben 53 SkFont font; 54 font.setSize(32); 55 font.setEdging(SkFont::Edging::kSubpixelAntiAlias); 56 57 // Setup our random scaler context 58 auto typeface = ToolUtils::CreatePortableTypeface("sans-serif", SkFontStyle::Bold()); 59 SkASSERT(typeface); 60 font.setTypeface(sk_make_sp<SkRandomTypeface>(std::move(typeface), paint, false)); 61 62 SkScalar y = 0; 63 SkRect bounds; 64 font.measureText(text, strlen(text), SkTextEncoding::kUTF8, &bounds); 65 y -= bounds.fTop; 66 ToolUtils::add_to_text_blob(&builder, text, font, 0, y); 67 y += bounds.fBottom; 68 69 // A8 70 const char* bigtext1 = "The quick brown fox"; 71 const char* bigtext2 = "jumps over the lazy dog."; 72 font.setSize(160); 73 font.setSubpixel(false); 74 font.setEdging(SkFont::Edging::kAntiAlias); 75 font.measureText(bigtext1, strlen(bigtext1), SkTextEncoding::kUTF8, &bounds); 76 y -= bounds.fTop; 77 ToolUtils::add_to_text_blob(&builder, bigtext1, font, 0, y); 78 y += bounds.fBottom; 79 80 font.measureText(bigtext2, strlen(bigtext2), SkTextEncoding::kUTF8, &bounds); 81 y -= bounds.fTop; 82 ToolUtils::add_to_text_blob(&builder, bigtext2, font, 0, y); 83 y += bounds.fBottom; 84 85 // color emoji 86 ToolUtils::EmojiTestSample sample = ToolUtils::EmojiSample(); 87 if (sample.typeface) { 88 font.setTypeface(sk_make_sp<SkRandomTypeface>(sample.typeface, paint, false)); 89 font.measureText(sample.sampleText, strlen(sample.sampleText), SkTextEncoding::kUTF8, &bounds); 90 y -= bounds.fTop; 91 ToolUtils::add_to_text_blob(&builder, sample.sampleText, font, 0, y); 92 y += bounds.fBottom; 93 } 94 95 // build 96 fBlob = builder.make(); 97 } 98 getName() const99 SkString getName() const override { return SkString("textblobrandomfont"); } 100 getISize()101 SkISize getISize() override { return SkISize::Make(kWidth, kHeight); } 102 onDraw(SkCanvas * canvas,SkString * errorMsg)103 DrawResult onDraw(SkCanvas* canvas, SkString* errorMsg) override { 104 GrDirectContext* dContext = GrAsDirectContext(canvas->recordingContext()); 105 bool isGPU = SkToBool(dContext); 106 107 #if defined(SK_GRAPHITE) 108 skgpu::graphite::Recorder* recorder = canvas->recorder(); 109 isGPU = isGPU || SkToBool(recorder); 110 #endif 111 112 if (!isGPU) { 113 *errorMsg = skiagm::GM::kErrorMsg_DrawSkippedGpuOnly; 114 return skiagm::DrawResult::kSkip; 115 } 116 117 // This GM uses ToolUtils::makeSurface which doesn't work well with vias. 118 // This GM uses SkRandomTypeface which doesn't work well with serialization. 119 canvas->drawColor(SK_ColorWHITE); 120 121 SkImageInfo info = SkImageInfo::Make(kWidth, kHeight, canvas->imageInfo().colorType(), 122 kPremul_SkAlphaType, 123 canvas->imageInfo().refColorSpace()); 124 SkSurfaceProps props(0, kUnknown_SkPixelGeometry); 125 auto surface(ToolUtils::makeSurface(canvas, info, &props)); 126 if (!surface) { 127 *errorMsg = "This test requires a surface"; 128 return DrawResult::kFail; 129 } 130 131 SkPaint paint; 132 paint.setAntiAlias(true); 133 134 SkCanvas* surfaceCanvas = surface->getCanvas(); 135 136 SkScalar stride = SkScalarCeilToScalar(fBlob->bounds().height()); 137 SkScalar yOffset = 5; 138 139 canvas->save(); 140 // Originally we would alternate between rotating and not to force blob regeneration, 141 // but that code seems to have rotted. Keeping the rotate to match the old GM as 142 // much as possible, and it seems like a reasonable stress test for transformed 143 // color emoji. 144 canvas->rotate(-0.05f); 145 canvas->drawTextBlob(fBlob, 10, yOffset, paint); 146 yOffset += stride; 147 canvas->restore(); 148 149 // Rotate in the surface canvas, not the final canvas, to avoid aliasing 150 surfaceCanvas->rotate(-0.05f); 151 surfaceCanvas->drawTextBlob(fBlob, 10, yOffset, paint); 152 surface->draw(canvas, 0, 0); 153 yOffset += stride; 154 155 if (dContext) { 156 // free gpu resources and verify 157 dContext->freeGpuResources(); 158 } 159 160 canvas->rotate(-0.05f); 161 canvas->drawTextBlob(fBlob, 10, yOffset, paint); 162 yOffset += stride; 163 return DrawResult::kOk; 164 } 165 166 private: 167 sk_sp<SkTextBlob> fBlob; 168 169 inline static constexpr int kWidth = 2000; 170 inline static constexpr int kHeight = 1600; 171 172 using INHERITED = GM; 173 }; 174 175 ////////////////////////////////////////////////////////////////////////////// 176 177 DEF_GM(return new TextBlobRandomFont;) 178 } // namespace skiagm 179