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