halcanary | 05b48e2 | 2015-11-16 10:51:21 -0800 | [diff] [blame] | 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 | |
| 8 | #include "gm.h" |
| 9 | #include "SkImage.h" |
| 10 | #include "SkRRect.h" |
| 11 | |
| 12 | static void rotated_checkerboard_shader(SkPaint* paint, |
| 13 | SkColor c1, |
| 14 | SkColor c2, |
| 15 | int size) { |
| 16 | SkBitmap bm; |
| 17 | bm.allocN32Pixels(2 * size, 2 * size); |
| 18 | bm.eraseColor(c1); |
| 19 | bm.eraseArea(SkIRect::MakeLTRB(0, 0, size, size), c2); |
| 20 | bm.eraseArea(SkIRect::MakeLTRB(size, size, 2 * size, 2 * size), c2); |
| 21 | SkMatrix matrix; |
| 22 | matrix.setScale(0.75f, 0.75f); |
| 23 | matrix.preRotate(30.0f); |
reed | 1a9b964 | 2016-03-13 14:13:58 -0700 | [diff] [blame] | 24 | paint->setShader( |
| 25 | SkShader::MakeBitmapShader(bm, SkShader::kRepeat_TileMode, SkShader::kRepeat_TileMode, |
| 26 | &matrix)); |
halcanary | 05b48e2 | 2015-11-16 10:51:21 -0800 | [diff] [blame] | 27 | } |
| 28 | |
| 29 | static void exercise_draw_pos_text(SkCanvas* canvas, |
| 30 | const char* text, |
| 31 | SkScalar x, SkScalar y, |
| 32 | const SkPaint& paint) { |
| 33 | size_t textLen = strlen(text); |
| 34 | int count = paint.countText(text, textLen); |
| 35 | SkAutoTArray<SkScalar> widths(count); |
| 36 | paint.getTextWidths(text, textLen, &widths[0]); |
| 37 | SkAutoTArray<SkPoint> pos(count); |
| 38 | for (int i = 0; i < count; ++i) { |
| 39 | pos[i].set(x, y); |
| 40 | x += widths[i]; |
| 41 | } |
| 42 | canvas->drawPosText(text, textLen, &pos[0], paint); |
| 43 | } |
| 44 | |
| 45 | static void exercise_draw_pos_text_h(SkCanvas* canvas, |
| 46 | const char* text, |
| 47 | SkScalar x, SkScalar y, |
| 48 | const SkPaint& paint) { |
| 49 | size_t textLen = strlen(text); |
| 50 | int count = paint.countText(text, textLen); |
| 51 | SkAutoTArray<SkScalar> widths(count); |
| 52 | paint.getTextWidths(text, textLen, &widths[0]); |
| 53 | SkAutoTArray<SkScalar> pos(count); |
| 54 | for (int i = 0; i < count; ++i) { |
| 55 | pos[i] = x; |
| 56 | x += widths[i]; |
| 57 | } |
| 58 | canvas->drawPosTextH(text, textLen, &pos[0], y, paint); |
| 59 | } |
| 60 | |
| 61 | static void test_text(SkCanvas* canvas, SkScalar size, |
| 62 | SkColor color, SkScalar Y) { |
| 63 | SkPaint type; |
| 64 | type.setTextSize(24); |
| 65 | sk_tool_utils::set_portable_typeface(&type); |
| 66 | type.setColor(color); |
| 67 | const char text[] = "HELLO WORLD"; |
| 68 | canvas->drawText(text, strlen(text), 32, size / 2 + Y, type); |
| 69 | SkScalar lineSpacing = type.getFontSpacing(); |
| 70 | exercise_draw_pos_text(canvas, text, 32, size / 2 + Y + lineSpacing, type); |
| 71 | exercise_draw_pos_text_h(canvas, text, 32, |
| 72 | size / 2 + Y + 2 * lineSpacing, type); |
| 73 | } |
| 74 | |
| 75 | // If this GM works correctly, the cyan layer should be lined up with |
| 76 | // the objects below it. |
| 77 | DEF_SIMPLE_GM(skbug_257, canvas, 512, 512) { |
| 78 | const SkScalar size = 256; |
| 79 | SkAutoCanvasRestore autoCanvasRestore0(canvas, true); |
| 80 | const SkScalar scale = 1.00168f; |
| 81 | canvas->scale(scale, scale); |
| 82 | { |
| 83 | SkPaint checker; |
| 84 | rotated_checkerboard_shader(&checker, SK_ColorWHITE, SK_ColorBLACK, 16); |
| 85 | checker.setAntiAlias(true); |
| 86 | |
| 87 | SkAutoCanvasRestore autoCanvasRestore(canvas, true); |
| 88 | canvas->clear(0xFFCECFCE); |
| 89 | SkScalar translate = 225364.0f; |
| 90 | canvas->translate(0, -translate); |
| 91 | |
| 92 | // Test rects |
| 93 | SkRect rect = SkRect::MakeLTRB(8, 8 + translate, size - 8, |
| 94 | size - 8 + translate); |
| 95 | canvas->drawRect(rect, checker); |
| 96 | |
| 97 | // Test Paths |
| 98 | canvas->translate(size, 0); |
| 99 | SkRRect rrect; |
| 100 | SkVector radii[4] = {{40, 40}, {40, 40}, {40, 40}, {40, 40}}; |
| 101 | rrect.setRectRadii(rect, radii); |
| 102 | canvas->drawRRect(rrect, checker); |
| 103 | |
| 104 | // Test Points |
| 105 | canvas->translate(-size, size); |
| 106 | SkScalar delta = 1.0 / 64.0; |
| 107 | SkPoint points[8] = {{size / 2, 8 + translate}, |
| 108 | {size / 2, 8 + translate + delta}, |
| 109 | {8, size / 2 + translate}, |
| 110 | {8, size / 2 + translate + delta}, |
| 111 | {size / 2, size - 8 + translate}, |
| 112 | {size / 2, size - 8 + translate + delta}, |
| 113 | {size - 8, size / 2 + translate}, |
| 114 | {size - 8, size / 2 + translate + delta}}; |
| 115 | checker.setStyle(SkPaint::kStroke_Style); |
| 116 | checker.setStrokeWidth(8); |
| 117 | checker.setStrokeCap(SkPaint::kRound_Cap); |
| 118 | canvas->drawPoints(SkCanvas::kLines_PointMode, 8, points, checker); |
| 119 | |
| 120 | // Test Text |
| 121 | canvas->translate(size, 0); |
| 122 | test_text(canvas, size, SK_ColorBLACK, translate); |
| 123 | } |
| 124 | // reference points (without the huge translations). |
| 125 | SkPaint stroke; |
| 126 | stroke.setStyle(SkPaint::kStroke_Style); |
| 127 | stroke.setStrokeWidth(5); |
| 128 | stroke.setColor(SK_ColorCYAN); |
| 129 | canvas->drawCircle(size / 2, size / 2, size / 2 - 10, stroke); |
| 130 | canvas->drawCircle(3 * size / 2, size / 2, size / 2 - 10, stroke); |
| 131 | canvas->drawCircle(size / 2, 384, size / 2 - 10, stroke); |
| 132 | canvas->translate(size, size); |
| 133 | test_text(canvas, size, SK_ColorCYAN, 0.0f); |
| 134 | } |