Home | History | Annotate | Download | only in samplecode
      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  */
      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"
     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 {
     25     bool        fLinearText;
     26     bool        fDevKernText;
     27 };
     28 
     29 static const Setting gSettings[] = {
     30     { false,  false   },
     31     { false,  true    },
     32     { true,   false   },
     33     { true,   true    },
     34 };
     35 
     36 static void doMeasure(SkCanvas* canvas, const SkPaint& paint, const char text[]) {
     37     SkScalar    dy = paint.getFontMetrics(nullptr);
     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);
     47     for (size_t i = 0; i < SK_ARRAY_COUNT(gSettings); i++) {
     48         p.setLinearText(gSettings[i].fLinearText);
     49         p.setDevKernText(gSettings[i].fDevKernText);
     50 
     51         int n = p.getTextWidths(text, len, widths, rects);
     52         SkScalar w = p.measureText(text, len, &bounds);
     53 
     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);
     75 
     76         canvas->translate(0, dy);
     77     }
     78 }
     79 
     80 class MeasureView : public SampleView {
     81 public:
     82     SkPaint fPaint;
     83 
     84     MeasureView() {
     85         fPaint.setAntiAlias(true);
     86         fPaint.setTextSize(SkIntToScalar(64));
     87         this->setBGColor(0xFFDDDDDD);
     88     }
     89 
     90 protected:
     91     // overrides from SkEventSink
     92     virtual bool onQuery(SkEvent* evt) {
     93         if (SampleCode::TitleQ(*evt)) {
     94             SampleCode::TitleR(evt, "Measure");
     95             return true;
     96         }
     97         return this->INHERITED::onQuery(evt);
     98     }
     99 
    100     virtual void onDrawContent(SkCanvas* canvas) {
    101         canvas->translate(fPaint.getTextSize(), fPaint.getTextSize());
    102         doMeasure(canvas, fPaint, "Hamburgefons");
    103     }
    104 
    105 private:
    106     typedef SampleView INHERITED;
    107 };
    108 
    109 //////////////////////////////////////////////////////////////////////////////
    110 
    111 static SkView* MyFactory() { return new MeasureView; }
    112 static SkViewRegister reg(MyFactory);
    113