coloremoji_blendmodes.cpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. /*
  2. * Copyright 2014 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/SkBitmap.h"
  9. #include "include/core/SkBlendMode.h"
  10. #include "include/core/SkCanvas.h"
  11. #include "include/core/SkColor.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/SkMatrix.h"
  17. #include "include/core/SkPaint.h"
  18. #include "include/core/SkRect.h"
  19. #include "include/core/SkRefCnt.h"
  20. #include "include/core/SkScalar.h"
  21. #include "include/core/SkShader.h"
  22. #include "include/core/SkSize.h"
  23. #include "include/core/SkString.h"
  24. #include "include/core/SkTileMode.h"
  25. #include "include/core/SkTypeface.h"
  26. #include "include/core/SkTypes.h"
  27. #include "include/effects/SkGradientShader.h"
  28. #include "include/utils/SkTextUtils.h"
  29. #include "src/utils/SkUTF.h"
  30. #include "tools/ToolUtils.h"
  31. #include <string.h>
  32. namespace {
  33. static uint16_t gData[] = { 0xFFFF, 0xCCCF, 0xCCCF, 0xFFFF };
  34. class ColorEmojiBlendModesGM : public skiagm::GM {
  35. public:
  36. const static int W = 64;
  37. const static int H = 64;
  38. ColorEmojiBlendModesGM() {}
  39. protected:
  40. void onOnceBeforeDraw() override {
  41. const SkColor colors[] = {
  42. SK_ColorRED, SK_ColorGREEN, SK_ColorBLUE,
  43. SK_ColorMAGENTA, SK_ColorCYAN, SK_ColorYELLOW
  44. };
  45. SkMatrix local;
  46. local.setRotate(180);
  47. SkPaint paint;
  48. paint.setAntiAlias(true);
  49. paint.setShader(SkGradientShader::MakeSweep(0, 0, colors, nullptr, SK_ARRAY_COUNT(colors),
  50. 0, &local));
  51. sk_sp<SkTypeface> orig(ToolUtils::create_portable_typeface("serif", SkFontStyle::Bold()));
  52. if (nullptr == orig) {
  53. orig = SkTypeface::MakeDefault();
  54. }
  55. fColorType = ToolUtils::emoji_typeface();
  56. fBG.installPixels(SkImageInfo::Make(2, 2, kARGB_4444_SkColorType,
  57. kOpaque_SkAlphaType), gData, 4);
  58. }
  59. SkString onShortName() override {
  60. return SkString("coloremoji_blendmodes");
  61. }
  62. SkISize onISize() override {
  63. return {400, 640};
  64. }
  65. void onDraw(SkCanvas* canvas) override {
  66. canvas->translate(SkIntToScalar(10), SkIntToScalar(20));
  67. const SkBlendMode gModes[] = {
  68. SkBlendMode::kClear,
  69. SkBlendMode::kSrc,
  70. SkBlendMode::kDst,
  71. SkBlendMode::kSrcOver,
  72. SkBlendMode::kDstOver,
  73. SkBlendMode::kSrcIn,
  74. SkBlendMode::kDstIn,
  75. SkBlendMode::kSrcOut,
  76. SkBlendMode::kDstOut,
  77. SkBlendMode::kSrcATop,
  78. SkBlendMode::kDstATop,
  79. SkBlendMode::kXor,
  80. SkBlendMode::kPlus,
  81. SkBlendMode::kModulate,
  82. SkBlendMode::kScreen,
  83. SkBlendMode::kOverlay,
  84. SkBlendMode::kDarken,
  85. SkBlendMode::kLighten,
  86. SkBlendMode::kColorDodge,
  87. SkBlendMode::kColorBurn,
  88. SkBlendMode::kHardLight,
  89. SkBlendMode::kSoftLight,
  90. SkBlendMode::kDifference,
  91. SkBlendMode::kExclusion,
  92. SkBlendMode::kMultiply,
  93. SkBlendMode::kHue,
  94. SkBlendMode::kSaturation,
  95. SkBlendMode::kColor,
  96. SkBlendMode::kLuminosity,
  97. };
  98. const SkScalar w = SkIntToScalar(W);
  99. const SkScalar h = SkIntToScalar(H);
  100. SkMatrix m;
  101. m.setScale(SkIntToScalar(6), SkIntToScalar(6));
  102. auto s = fBG.makeShader(SkTileMode::kRepeat, SkTileMode::kRepeat, &m);
  103. SkFont labelFont(ToolUtils::create_portable_typeface());
  104. SkPaint textP;
  105. textP.setAntiAlias(true);
  106. SkFont textFont(fColorType, 70);
  107. const int W = 5;
  108. SkScalar x0 = 0;
  109. SkScalar y0 = 0;
  110. SkScalar x = x0, y = y0;
  111. for (size_t i = 0; i < SK_ARRAY_COUNT(gModes); i++) {
  112. SkRect r;
  113. r.set(x, y, x+w, y+h);
  114. SkPaint p;
  115. p.setStyle(SkPaint::kFill_Style);
  116. p.setShader(s);
  117. canvas->drawRect(r, p);
  118. r.inset(-SK_ScalarHalf, -SK_ScalarHalf);
  119. p.setStyle(SkPaint::kStroke_Style);
  120. p.setShader(nullptr);
  121. canvas->drawRect(r, p);
  122. {
  123. SkAutoCanvasRestore arc(canvas, true);
  124. canvas->clipRect(r);
  125. textP.setBlendMode(gModes[i]);
  126. const char* text = ToolUtils::emoji_sample_text();
  127. SkUnichar unichar = SkUTF::NextUTF8(&text, text + strlen(text));
  128. SkASSERT(unichar >= 0);
  129. canvas->drawSimpleText(&unichar, 4, SkTextEncoding::kUTF32,
  130. x+ w/10.f, y + 7.f*h/8.f, textFont, textP);
  131. }
  132. #if 1
  133. const char* label = SkBlendMode_Name(gModes[i]);
  134. SkTextUtils::DrawString(canvas, label, x + w/2, y - labelFont.getSize()/2,
  135. labelFont, SkPaint(), SkTextUtils::kCenter_Align);
  136. #endif
  137. x += w + SkIntToScalar(10);
  138. if ((i % W) == W - 1) {
  139. x = x0;
  140. y += h + SkIntToScalar(30);
  141. }
  142. }
  143. }
  144. private:
  145. SkBitmap fBG;
  146. sk_sp<SkTypeface> fColorType;
  147. typedef GM INHERITED;
  148. };
  149. } // namespace
  150. DEF_GM( return new ColorEmojiBlendModesGM; )