SampleTextBox.cpp 2.9 KB

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