1 /* 2 * Copyright 2011 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 #include "samplecode/Sample.h" 8 9 #include "include/core/SkCanvas.h" 10 #include "include/core/SkColorFilter.h" 11 #include "include/core/SkColorPriv.h" 12 #include "include/core/SkGraphics.h" 13 #include "include/core/SkPath.h" 14 #include "include/core/SkRegion.h" 15 #include "include/core/SkShader.h" 16 #include "include/core/SkStream.h" 17 #include "include/core/SkTextBlob.h" 18 #include "include/core/SkTime.h" 19 #include "include/core/SkTypeface.h" 20 #include "include/effects/SkBlurMaskFilter.h" 21 #include "include/effects/SkGradientShader.h" 22 #include "include/utils/SkRandom.h" 23 #include "modules/skshaper/include/SkShaper.h" 24 #include "src/core/SkOSFile.h" 25 #include "src/shaders/SkColorShader.h" 26 #include "src/utils/SkUTF.h" 27 28 static const char gText[] = 29 "When in the Course of human events it becomes necessary for one people " 30 "to dissolve the political bands which have connected them with another " 31 "and to assume among the powers of the earth, the separate and equal " 32 "station to which the Laws of Nature and of Nature's God entitle them, " 33 "a decent respect to the opinions of mankind requires that they should " 34 "declare the causes which impel them to the separation."; 35 36 class TextBoxView : public Sample { 37 public: TextBoxView()38 TextBoxView() : fShaper(SkShaper::Make()) {} 39 40 protected: name()41 SkString name() override { return SkString("TextBox"); } 42 drawTest(SkCanvas * canvas,SkScalar w,SkScalar h,SkColor fg,SkColor bg)43 void drawTest(SkCanvas* canvas, SkScalar w, SkScalar h, SkColor fg, SkColor bg) { 44 SkAutoCanvasRestore acr(canvas, true); 45 46 canvas->clipRect(SkRect::MakeWH(w, h)); 47 canvas->drawColor(bg); 48 49 SkScalar margin = 20; 50 51 SkPaint paint; 52 paint.setColor(fg); 53 54 for (int i = 9; i < 24; i += 2) { 55 SkTextBlobBuilderRunHandler builder(gText, { margin, margin }); 56 SkFont font(nullptr, SkIntToScalar(i)); 57 font.setEdging(SkFont::Edging::kSubpixelAntiAlias); 58 59 fShaper->shape(gText, strlen(gText), font, true, w - margin, &builder); 60 canvas->drawTextBlob(builder.makeBlob(), 0, 0, paint); 61 62 canvas->translate(0, builder.endPoint().y()); 63 } 64 } 65 onDrawContent(SkCanvas * canvas)66 void onDrawContent(SkCanvas* canvas) override { 67 SkScalar width = this->width() / 3; 68 drawTest(canvas, width, this->height(), SK_ColorBLACK, SK_ColorWHITE); 69 canvas->translate(width, 0); 70 drawTest(canvas, width, this->height(), SK_ColorWHITE, SK_ColorBLACK); 71 canvas->translate(width, 0); 72 drawTest(canvas, width, this->height()/2, SK_ColorGRAY, SK_ColorWHITE); 73 canvas->translate(0, this->height()/2); 74 drawTest(canvas, width, this->height()/2, SK_ColorGRAY, SK_ColorBLACK); 75 } 76 77 private: 78 std::unique_ptr<SkShaper> fShaper; 79 typedef Sample INHERITED; 80 }; 81 82 ////////////////////////////////////////////////////////////////////////////// 83 84 DEF_SAMPLE( return new TextBoxView(); ) 85