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