fontscalerdistortable.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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. #include "gm/gm.h"
  8. #include "include/core/SkCanvas.h"
  9. #include "include/core/SkFont.h"
  10. #include "include/core/SkFontArguments.h"
  11. #include "include/core/SkFontMgr.h"
  12. #include "include/core/SkFontTypes.h"
  13. #include "include/core/SkPaint.h"
  14. #include "include/core/SkRect.h"
  15. #include "include/core/SkRefCnt.h"
  16. #include "include/core/SkScalar.h"
  17. #include "include/core/SkSize.h"
  18. #include "include/core/SkStream.h"
  19. #include "include/core/SkString.h"
  20. #include "include/core/SkTypeface.h"
  21. #include "include/core/SkTypes.h"
  22. #include "tools/Resources.h"
  23. #include <string.h>
  24. #include <memory>
  25. #include <utility>
  26. namespace skiagm {
  27. class FontScalerDistortableGM : public GM {
  28. public:
  29. FontScalerDistortableGM() {
  30. this->setBGColor(0xFFFFFFFF);
  31. }
  32. protected:
  33. SkString onShortName() override {
  34. return SkString("fontscalerdistortable");
  35. }
  36. SkISize onISize() override {
  37. return SkISize::Make(550, 700);
  38. }
  39. DrawResult onDraw(SkCanvas* canvas, SkString* errorMsg) override {
  40. SkPaint paint;
  41. paint.setAntiAlias(true);
  42. SkFont font;
  43. font.setEdging(SkFont::Edging::kSubpixelAntiAlias);
  44. sk_sp<SkFontMgr> fontMgr(SkFontMgr::RefDefault());
  45. std::unique_ptr<SkStreamAsset> distortableStream(GetResourceAsStream("fonts/Distortable.ttf"));
  46. sk_sp<SkTypeface> distortable(MakeResourceAsTypeface("fonts/Distortable.ttf"));
  47. if (!distortableStream) {
  48. *errorMsg = "No distortableStream";
  49. return DrawResult::kFail;
  50. }
  51. const char* text = "abc";
  52. const size_t textLen = strlen(text);
  53. for (int j = 0; j < 2; ++j) {
  54. for (int i = 0; i < 5; ++i) {
  55. SkScalar x = SkIntToScalar(10);
  56. SkScalar y = SkIntToScalar(20);
  57. SkFourByteTag tag = SkSetFourByteTag('w','g','h','t');
  58. SkScalar styleValue = SkDoubleToScalar(0.5 + (5 * j + i) * ((2.0 - 0.5) / (2 * 5)));
  59. SkFontArguments::VariationPosition::Coordinate coordinates[] = {{tag, styleValue}};
  60. SkFontArguments::VariationPosition position =
  61. { coordinates, SK_ARRAY_COUNT(coordinates) };
  62. if (j == 0 && distortable) {
  63. sk_sp<SkTypeface> clone = distortable->makeClone(
  64. SkFontArguments().setVariationDesignPosition(position));
  65. font.setTypeface(clone ? std::move(clone) : distortable);
  66. } else {
  67. font.setTypeface(fontMgr->makeFromStream(
  68. distortableStream->duplicate(),
  69. SkFontArguments().setVariationDesignPosition(position)));
  70. }
  71. SkAutoCanvasRestore acr(canvas, true);
  72. canvas->translate(SkIntToScalar(30 + i * 100), SkIntToScalar(20));
  73. canvas->rotate(SkIntToScalar(i * 5), x, y * 10);
  74. {
  75. SkPaint p;
  76. p.setAntiAlias(true);
  77. SkRect r;
  78. r.set(x - SkIntToScalar(3), SkIntToScalar(15),
  79. x - SkIntToScalar(1), SkIntToScalar(280));
  80. canvas->drawRect(r, p);
  81. }
  82. for (int ps = 6; ps <= 22; ps++) {
  83. font.setSize(SkIntToScalar(ps));
  84. canvas->drawSimpleText(text, textLen, SkTextEncoding::kUTF8, x, y, font, paint);
  85. y += font.getMetrics(nullptr);
  86. }
  87. }
  88. canvas->translate(0, SkIntToScalar(360));
  89. font.setSubpixel(true);
  90. font.setLinearMetrics(true);
  91. }
  92. return DrawResult::kOk;
  93. }
  94. private:
  95. typedef GM INHERITED;
  96. };
  97. //////////////////////////////////////////////////////////////////////////////
  98. DEF_GM( return new FontScalerDistortableGM; )
  99. }