epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2011 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 | */ |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 7 | #include "SampleCode.h" |
| 8 | #include "SkView.h" |
| 9 | #include "SkCanvas.h" |
| 10 | #include "SkGradientShader.h" |
| 11 | #include "SkPath.h" |
| 12 | #include "SkRegion.h" |
| 13 | #include "SkShader.h" |
| 14 | #include "SkUtils.h" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 15 | #include "Sk1DPathEffect.h" |
| 16 | #include "SkCornerPathEffect.h" |
| 17 | #include "SkPathMeasure.h" |
| 18 | #include "SkRandom.h" |
| 19 | #include "SkColorPriv.h" |
| 20 | #include "SkColorFilter.h" |
| 21 | #include "SkDither.h" |
| 22 | |
| 23 | // exercise scale/linear/devkern |
| 24 | struct Setting { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 25 | bool fLinearText; |
| 26 | bool fDevKernText; |
| 27 | }; |
| 28 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 29 | static const Setting gSettings[] = { |
reed | 99ae881 | 2014-08-26 11:30:01 -0700 | [diff] [blame] | 30 | { false, false }, |
| 31 | { false, true }, |
| 32 | { true, false }, |
| 33 | { true, true }, |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 34 | }; |
| 35 | |
reed@google.com | 81e3d7f | 2011-06-01 12:42:36 +0000 | [diff] [blame] | 36 | static void doMeasure(SkCanvas* canvas, const SkPaint& paint, const char text[]) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 37 | SkScalar dy = paint.getFontMetrics(nullptr); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 38 | |
| 39 | size_t len = strlen(text); |
| 40 | SkAutoTMalloc<SkScalar> autoWidths(len); |
| 41 | SkScalar* widths = autoWidths.get(); |
| 42 | SkAutoTMalloc<SkRect> autoRects(len); |
| 43 | SkRect* rects = autoRects.get(); |
| 44 | SkRect bounds; |
| 45 | |
| 46 | SkPaint p(paint); |
senorblanco@chromium.org | 64cc579 | 2011-05-19 19:58:58 +0000 | [diff] [blame] | 47 | for (size_t i = 0; i < SK_ARRAY_COUNT(gSettings); i++) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 48 | p.setLinearText(gSettings[i].fLinearText); |
| 49 | p.setDevKernText(gSettings[i].fDevKernText); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 50 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 51 | int n = p.getTextWidths(text, len, widths, rects); |
reed | 99ae881 | 2014-08-26 11:30:01 -0700 | [diff] [blame] | 52 | SkScalar w = p.measureText(text, len, &bounds); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 53 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 54 | p.setStyle(SkPaint::kFill_Style); |
| 55 | p.setColor(0x8888FF88); |
| 56 | canvas->drawRect(bounds, p); |
| 57 | p.setColor(0xFF000000); |
| 58 | canvas->drawText(text, len, 0, 0, p); |
| 59 | |
| 60 | p.setStyle(SkPaint::kStroke_Style); |
| 61 | p.setStrokeWidth(0); |
| 62 | p.setColor(0xFFFF0000); |
| 63 | SkScalar x = 0; |
| 64 | for (int j = 0; j < n; j++) { |
| 65 | SkRect r = rects[j]; |
| 66 | r.offset(x, 0); |
| 67 | canvas->drawRect(r, p); |
| 68 | x += widths[j]; |
| 69 | } |
| 70 | |
| 71 | p.setColor(0xFF0000FF); |
| 72 | canvas->drawLine(0, 0, w, 0, p); |
| 73 | p.setStrokeWidth(SkIntToScalar(4)); |
| 74 | canvas->drawPoint(x, 0, p); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 75 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 76 | canvas->translate(0, dy); |
| 77 | } |
| 78 | } |
| 79 | |
reed@google.com | 81e3d7f | 2011-06-01 12:42:36 +0000 | [diff] [blame] | 80 | class MeasureView : public SampleView { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 81 | public: |
| 82 | SkPaint fPaint; |
| 83 | |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 84 | MeasureView() { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 85 | fPaint.setAntiAlias(true); |
| 86 | fPaint.setTextSize(SkIntToScalar(64)); |
reed@google.com | 81e3d7f | 2011-06-01 12:42:36 +0000 | [diff] [blame] | 87 | this->setBGColor(0xFFDDDDDD); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | protected: |
| 91 | // overrides from SkEventSink |
reed@google.com | 81e3d7f | 2011-06-01 12:42:36 +0000 | [diff] [blame] | 92 | virtual bool onQuery(SkEvent* evt) { |
| 93 | if (SampleCode::TitleQ(*evt)) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 94 | SampleCode::TitleR(evt, "Measure"); |
| 95 | return true; |
| 96 | } |
| 97 | return this->INHERITED::onQuery(evt); |
| 98 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 99 | |
reed@google.com | 81e3d7f | 2011-06-01 12:42:36 +0000 | [diff] [blame] | 100 | virtual void onDrawContent(SkCanvas* canvas) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 101 | canvas->translate(fPaint.getTextSize(), fPaint.getTextSize()); |
| 102 | doMeasure(canvas, fPaint, "Hamburgefons"); |
| 103 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 104 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 105 | private: |
reed@google.com | 81e3d7f | 2011-06-01 12:42:36 +0000 | [diff] [blame] | 106 | typedef SampleView INHERITED; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 107 | }; |
| 108 | |
| 109 | ////////////////////////////////////////////////////////////////////////////// |
| 110 | |
| 111 | static SkView* MyFactory() { return new MeasureView; } |
| 112 | static SkViewRegister reg(MyFactory); |