coloremoji.cpp 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. /*
  2. * Copyright 2013 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/SkColor.h"
  10. #include "include/core/SkColorFilter.h"
  11. #include "include/core/SkFont.h"
  12. #include "include/core/SkFontMetrics.h"
  13. #include "include/core/SkFontTypes.h"
  14. #include "include/core/SkImageFilter.h"
  15. #include "include/core/SkPaint.h"
  16. #include "include/core/SkPoint.h"
  17. #include "include/core/SkRect.h"
  18. #include "include/core/SkRefCnt.h"
  19. #include "include/core/SkScalar.h"
  20. #include "include/core/SkShader.h"
  21. #include "include/core/SkSize.h"
  22. #include "include/core/SkString.h"
  23. #include "include/core/SkTileMode.h"
  24. #include "include/core/SkTypeface.h"
  25. #include "include/core/SkTypes.h"
  26. #include "include/effects/SkBlurImageFilter.h"
  27. #include "include/effects/SkColorFilterImageFilter.h"
  28. #include "include/effects/SkColorMatrixFilter.h"
  29. #include "include/effects/SkGradientShader.h"
  30. #include "tools/ToolUtils.h"
  31. #include <string.h>
  32. #include <initializer_list>
  33. #include <utility>
  34. /*
  35. * Spits out a dummy gradient to test blur with shader on paint
  36. */
  37. static sk_sp<SkShader> MakeLinear() {
  38. constexpr SkPoint kPts[] = { { 0, 0 }, { 32, 32 } };
  39. constexpr SkScalar kPos[] = { 0, SK_Scalar1/2, SK_Scalar1 };
  40. constexpr SkColor kColors[] = {0x80F00080, 0xF0F08000, 0x800080F0 };
  41. return SkGradientShader::MakeLinear(kPts, kColors, kPos, SK_ARRAY_COUNT(kColors),
  42. SkTileMode::kClamp);
  43. }
  44. static sk_sp<SkImageFilter> make_grayscale(sk_sp<SkImageFilter> input) {
  45. float matrix[20];
  46. memset(matrix, 0, 20 * sizeof(float));
  47. matrix[0] = matrix[5] = matrix[10] = 0.2126f;
  48. matrix[1] = matrix[6] = matrix[11] = 0.7152f;
  49. matrix[2] = matrix[7] = matrix[12] = 0.0722f;
  50. matrix[18] = 1.0f;
  51. sk_sp<SkColorFilter> filter(SkColorFilters::Matrix(matrix));
  52. return SkColorFilterImageFilter::Make(std::move(filter), std::move(input));
  53. }
  54. static sk_sp<SkImageFilter> make_blur(float amount, sk_sp<SkImageFilter> input) {
  55. return SkBlurImageFilter::Make(amount, amount, std::move(input));
  56. }
  57. static sk_sp<SkColorFilter> make_color_filter() {
  58. return SkColorMatrixFilter::MakeLightingFilter(SkColorSetRGB(0x00, 0x80, 0xFF),
  59. SkColorSetRGB(0xFF, 0x20, 0x00));
  60. }
  61. namespace skiagm {
  62. class ColorEmojiGM : public GM {
  63. public:
  64. ColorEmojiGM() { }
  65. protected:
  66. struct EmojiFont {
  67. sk_sp<SkTypeface> typeface;
  68. const char* text;
  69. } emojiFont;
  70. virtual void onOnceBeforeDraw() override {
  71. emojiFont.typeface = ToolUtils::emoji_typeface();
  72. emojiFont.text = ToolUtils::emoji_sample_text();
  73. }
  74. SkString onShortName() override {
  75. return SkString("coloremoji");
  76. }
  77. SkISize onISize() override { return SkISize::Make(650, 1200); }
  78. void onDraw(SkCanvas* canvas) override {
  79. canvas->drawColor(SK_ColorGRAY);
  80. SkFont font(emojiFont.typeface);
  81. char const * const text = emojiFont.text;
  82. size_t textLen = strlen(text);
  83. // draw text at different point sizes
  84. constexpr SkScalar textSizes[] = { 10, 30, 50, };
  85. SkFontMetrics metrics;
  86. SkScalar y = 0;
  87. for (const bool& fakeBold : { false, true }) {
  88. font.setEmbolden(fakeBold);
  89. for (const SkScalar& textSize : textSizes) {
  90. font.setSize(textSize);
  91. font.getMetrics(&metrics);
  92. y += -metrics.fAscent;
  93. canvas->drawSimpleText(text, textLen, SkTextEncoding::kUTF8,
  94. 10, y, font, SkPaint());
  95. y += metrics.fDescent + metrics.fLeading;
  96. }
  97. }
  98. y += 20;
  99. SkScalar savedY = y;
  100. // draw with shaders and image filters
  101. for (int makeLinear = 0; makeLinear < 2; makeLinear++) {
  102. for (int makeBlur = 0; makeBlur < 2; makeBlur++) {
  103. for (int makeGray = 0; makeGray < 2; makeGray++) {
  104. for (int makeMode = 0; makeMode < 2; ++makeMode) {
  105. for (int alpha = 0; alpha < 2; ++alpha) {
  106. SkFont shaderFont(font.refTypefaceOrDefault());
  107. SkPaint shaderPaint;
  108. if (SkToBool(makeLinear)) {
  109. shaderPaint.setShader(MakeLinear());
  110. }
  111. if (SkToBool(makeBlur) && SkToBool(makeGray)) {
  112. sk_sp<SkImageFilter> grayScale(make_grayscale(nullptr));
  113. sk_sp<SkImageFilter> blur(make_blur(3.0f, std::move(grayScale)));
  114. shaderPaint.setImageFilter(std::move(blur));
  115. } else if (SkToBool(makeBlur)) {
  116. shaderPaint.setImageFilter(make_blur(3.0f, nullptr));
  117. } else if (SkToBool(makeGray)) {
  118. shaderPaint.setImageFilter(make_grayscale(nullptr));
  119. }
  120. if (makeMode) {
  121. shaderPaint.setColorFilter(make_color_filter());
  122. }
  123. if (alpha) {
  124. shaderPaint.setAlphaf(0.5f);
  125. }
  126. shaderFont.setSize(30);
  127. shaderFont.getMetrics(&metrics);
  128. y += -metrics.fAscent;
  129. canvas->drawSimpleText(text, textLen, SkTextEncoding::kUTF8, 380, y,
  130. shaderFont, shaderPaint);
  131. y += metrics.fDescent + metrics.fLeading;
  132. }
  133. }
  134. }
  135. }
  136. }
  137. // setup work needed to draw text with different clips
  138. canvas->translate(10, savedY);
  139. font.setSize(40);
  140. // compute the bounds of the text
  141. SkRect bounds;
  142. font.measureText(text, textLen, SkTextEncoding::kUTF8, &bounds);
  143. const SkScalar boundsHalfWidth = bounds.width() * SK_ScalarHalf;
  144. const SkScalar boundsHalfHeight = bounds.height() * SK_ScalarHalf;
  145. const SkScalar boundsQuarterWidth = boundsHalfWidth * SK_ScalarHalf;
  146. const SkScalar boundsQuarterHeight = boundsHalfHeight * SK_ScalarHalf;
  147. SkRect upperLeftClip = SkRect::MakeXYWH(bounds.left(), bounds.top(),
  148. boundsHalfWidth, boundsHalfHeight);
  149. SkRect lowerRightClip = SkRect::MakeXYWH(bounds.centerX(), bounds.centerY(),
  150. boundsHalfWidth, boundsHalfHeight);
  151. SkRect interiorClip = bounds;
  152. interiorClip.inset(boundsQuarterWidth, boundsQuarterHeight);
  153. const SkRect clipRects[] = { bounds, upperLeftClip, lowerRightClip, interiorClip };
  154. SkPaint clipHairline;
  155. clipHairline.setColor(SK_ColorWHITE);
  156. clipHairline.setStyle(SkPaint::kStroke_Style);
  157. SkPaint paint;
  158. for (const SkRect& clipRect : clipRects) {
  159. canvas->translate(0, bounds.height());
  160. canvas->save();
  161. canvas->drawRect(clipRect, clipHairline);
  162. paint.setAlpha(0x20);
  163. canvas->drawSimpleText(text, textLen, SkTextEncoding::kUTF8, 0, 0, font, paint);
  164. canvas->clipRect(clipRect);
  165. paint.setAlphaf(1.0f);
  166. canvas->drawSimpleText(text, textLen, SkTextEncoding::kUTF8, 0, 0, font, paint);
  167. canvas->restore();
  168. canvas->translate(0, SkIntToScalar(25));
  169. }
  170. }
  171. typedef GM INHERITED;
  172. };
  173. //////////////////////////////////////////////////////////////////////////////
  174. DEF_GM(return new ColorEmojiGM;)
  175. }