Jim Van Verth | 08576e6 | 2016-11-16 10:15:23 -0500 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2016 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 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 8 | #include "gm/gm.h" |
Ben Wagner | 7fde8e1 | 2019-05-01 17:28:53 -0400 | [diff] [blame] | 9 | #include "include/core/SkBlurTypes.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 10 | #include "include/core/SkCanvas.h" |
Ben Wagner | 7fde8e1 | 2019-05-01 17:28:53 -0400 | [diff] [blame] | 11 | #include "include/core/SkColor.h" |
| 12 | #include "include/core/SkFont.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 13 | #include "include/core/SkMaskFilter.h" |
Ben Wagner | 7fde8e1 | 2019-05-01 17:28:53 -0400 | [diff] [blame] | 14 | #include "include/core/SkPaint.h" |
| 15 | #include "include/core/SkRect.h" |
| 16 | #include "include/core/SkRefCnt.h" |
| 17 | #include "include/core/SkScalar.h" |
| 18 | #include "include/core/SkSize.h" |
| 19 | #include "include/core/SkString.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 20 | #include "include/core/SkTextBlob.h" |
Ben Wagner | 7fde8e1 | 2019-05-01 17:28:53 -0400 | [diff] [blame] | 21 | #include "include/core/SkTypeface.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 22 | #include "src/core/SkBlurMask.h" |
| 23 | #include "tools/ToolUtils.h" |
Jim Van Verth | 08576e6 | 2016-11-16 10:15:23 -0500 | [diff] [blame] | 24 | |
Ben Wagner | 7fde8e1 | 2019-05-01 17:28:53 -0400 | [diff] [blame] | 25 | #include <string.h> |
| 26 | |
Jim Van Verth | 08576e6 | 2016-11-16 10:15:23 -0500 | [diff] [blame] | 27 | #define WIDTH 800 |
| 28 | #define HEIGHT 800 |
| 29 | |
| 30 | static void draw_text(SkCanvas* canvas, sk_sp<SkTextBlob> blob, |
| 31 | const SkPaint& paint, const SkPaint& blurPaint, |
| 32 | const SkPaint& clearPaint) { |
| 33 | canvas->save(); |
| 34 | canvas->clipRect(SkRect::MakeLTRB(0, 0, 1081, 665)); |
| 35 | canvas->drawRect(SkRect::MakeLTRB(0, 0, 1081, 665), clearPaint); |
| 36 | // draw as blurred to push glyph to be too large for atlas |
| 37 | canvas->drawTextBlob(blob, 0, 256, blurPaint); |
| 38 | canvas->drawTextBlob(blob, 0, 477, paint); |
| 39 | canvas->restore(); |
| 40 | } |
| 41 | |
| 42 | // This test ensures that glyphs that are too large for the atlas |
| 43 | // are both translated and clipped correctly. |
| 44 | class ClipErrorGM : public skiagm::GM { |
| 45 | public: |
| 46 | ClipErrorGM() {} |
| 47 | |
| 48 | protected: |
| 49 | SkString onShortName() override { return SkString("cliperror"); } |
| 50 | |
| 51 | SkISize onISize() override { return SkISize::Make(WIDTH, HEIGHT); } |
| 52 | |
| 53 | void onDraw(SkCanvas* canvas) override { |
| 54 | SkPaint paint; |
| 55 | paint.setAntiAlias(true); |
Jim Van Verth | 08576e6 | 2016-11-16 10:15:23 -0500 | [diff] [blame] | 56 | |
Mike Klein | ea3f014 | 2019-03-20 11:12:10 -0500 | [diff] [blame] | 57 | SkFont font(ToolUtils::create_portable_typeface(), 256); |
Jim Van Verth | 08576e6 | 2016-11-16 10:15:23 -0500 | [diff] [blame] | 58 | |
| 59 | // setup up maskfilter |
| 60 | const SkScalar kSigma = SkBlurMask::ConvertRadiusToSigma(SkIntToScalar(50)); |
| 61 | |
| 62 | SkPaint blurPaint(paint); |
Mike Reed | 1be1f8d | 2018-03-14 13:01:17 -0400 | [diff] [blame] | 63 | blurPaint.setMaskFilter(SkMaskFilter::MakeBlur(kNormal_SkBlurStyle, kSigma)); |
Jim Van Verth | 08576e6 | 2016-11-16 10:15:23 -0500 | [diff] [blame] | 64 | |
Mike Reed | ea8900e | 2018-12-22 17:37:30 -0500 | [diff] [blame] | 65 | const char text[] = "hambur"; |
| 66 | auto blob = SkTextBlob::MakeFromText(text, strlen(text), font); |
Jim Van Verth | 08576e6 | 2016-11-16 10:15:23 -0500 | [diff] [blame] | 67 | |
| 68 | SkPaint clearPaint(paint); |
| 69 | clearPaint.setColor(SK_ColorWHITE); |
| 70 | |
| 71 | canvas->save(); |
| 72 | canvas->translate(0, 0); |
| 73 | canvas->clipRect(SkRect::MakeLTRB(0, 0, WIDTH, 256)); |
| 74 | draw_text(canvas, blob, paint, blurPaint, clearPaint); |
| 75 | canvas->restore(); |
| 76 | |
| 77 | canvas->save(); |
| 78 | canvas->translate(0, 256); |
| 79 | canvas->clipRect(SkRect::MakeLTRB(0, 256, WIDTH, 510)); |
| 80 | draw_text(canvas, blob, paint, blurPaint, clearPaint); |
| 81 | canvas->restore(); |
| 82 | } |
| 83 | |
| 84 | private: |
| 85 | typedef skiagm::GM INHERITED; |
| 86 | }; |
| 87 | |
| 88 | DEF_GM(return new ClipErrorGM;) |