thinrects.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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/SkPaint.h"
  11. #include "include/core/SkRect.h"
  12. #include "include/core/SkSize.h"
  13. #include "include/core/SkString.h"
  14. #include "include/core/SkTypes.h"
  15. namespace skiagm {
  16. // Draw various width thin rects at 1/8 horizontal pixel increments
  17. class ThinRectsGM : public GM {
  18. public:
  19. ThinRectsGM() {
  20. this->setBGColor(0xFF000000);
  21. }
  22. protected:
  23. SkString onShortName() override {
  24. return SkString("thinrects");
  25. }
  26. SkISize onISize() override {
  27. return SkISize::Make(240, 320);
  28. }
  29. void onDraw(SkCanvas* canvas) override {
  30. SkPaint white;
  31. white.setColor(SK_ColorWHITE);
  32. white.setAntiAlias(true);
  33. SkPaint green;
  34. green.setColor(SK_ColorGREEN);
  35. green.setAntiAlias(true);
  36. for (int i = 0; i < 8; ++i) {
  37. canvas->save();
  38. canvas->translate(i*0.125f, i*40.0f);
  39. DrawVertRects(canvas, white);
  40. canvas->translate(40.0f, 0.0f);
  41. DrawVertRects(canvas, green);
  42. canvas->restore();
  43. canvas->save();
  44. canvas->translate(80.0f, i*40.0f + i*0.125f);
  45. DrawHorizRects(canvas, white);
  46. canvas->translate(40.0f, 0.0f);
  47. DrawHorizRects(canvas, green);
  48. canvas->restore();
  49. canvas->save();
  50. canvas->translate(160.0f + i*0.125f,
  51. i*40.0f + i*0.125f);
  52. DrawSquares(canvas, white);
  53. canvas->translate(40.0f, 0.0f);
  54. DrawSquares(canvas, green);
  55. canvas->restore();
  56. }
  57. }
  58. private:
  59. static void DrawVertRects(SkCanvas* canvas, const SkPaint& p) {
  60. constexpr SkRect vertRects[] = {
  61. { 1, 1, 5.0f, 21 }, // 4 pix wide
  62. { 8, 1, 10.0f, 21 }, // 2 pix wide
  63. { 13, 1, 14.0f, 21 }, // 1 pix wide
  64. { 17, 1, 17.5f, 21 }, // 1/2 pix wide
  65. { 21, 1, 21.25f, 21 }, // 1/4 pix wide
  66. { 25, 1, 25.125f, 21 }, // 1/8 pix wide
  67. { 29, 1, 29.0f, 21 } // 0 pix wide
  68. };
  69. for (size_t j = 0; j < SK_ARRAY_COUNT(vertRects); ++j) {
  70. canvas->drawRect(vertRects[j], p);
  71. }
  72. }
  73. static void DrawHorizRects(SkCanvas* canvas, const SkPaint& p) {
  74. constexpr SkRect horizRects[] = {
  75. { 1, 1, 21, 5.0f }, // 4 pix high
  76. { 1, 8, 21, 10.0f }, // 2 pix high
  77. { 1, 13, 21, 14.0f }, // 1 pix high
  78. { 1, 17, 21, 17.5f }, // 1/2 pix high
  79. { 1, 21, 21, 21.25f }, // 1/4 pix high
  80. { 1, 25, 21, 25.125f }, // 1/8 pix high
  81. { 1, 29, 21, 29.0f } // 0 pix high
  82. };
  83. for (size_t j = 0; j < SK_ARRAY_COUNT(horizRects); ++j) {
  84. canvas->drawRect(horizRects[j], p);
  85. }
  86. }
  87. static void DrawSquares(SkCanvas* canvas, const SkPaint& p) {
  88. constexpr SkRect squares[] = {
  89. { 1, 1, 5.0f, 5.0f }, // 4 pix
  90. { 8, 8, 10.0f, 10.0f }, // 2 pix
  91. { 13, 13, 14.0f, 14.0f }, // 1 pix
  92. { 17, 17, 17.5f, 17.5f }, // 1/2 pix
  93. { 21, 21, 21.25f, 21.25f }, // 1/4 pix
  94. { 25, 25, 25.125f, 25.125f }, // 1/8 pix
  95. { 29, 29, 29.0f, 29.0f } // 0 pix
  96. };
  97. for (size_t j = 0; j < SK_ARRAY_COUNT(squares); ++j) {
  98. canvas->drawRect(squares[j], p);
  99. }
  100. }
  101. typedef GM INHERITED;
  102. };
  103. //////////////////////////////////////////////////////////////////////////////
  104. DEF_GM( return new ThinRectsGM; )
  105. }