Home | History | Annotate | Download | only in samplecode
      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  */
      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"
     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 
     44 static void doMeasure(SkCanvas* canvas, const SkPaint& paint, const char text[]) {
     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);
     55     for (size_t i = 0; i < SK_ARRAY_COUNT(gSettings); i++) {
     56         p.setLinearText(gSettings[i].fLinearText);
     57         p.setDevKernText(gSettings[i].fDevKernText);
     58         SkScalar scale = gSettings[i].fScale;
     59 
     60         int n = p.getTextWidths(text, len, widths, rects);
     61         SkScalar w = p.measureText(text, len, &bounds, scale);
     62 
     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);
     84 
     85         canvas->translate(0, dy);
     86     }
     87 }
     88 
     89 class MeasureView : public SampleView {
     90 public:
     91     SkPaint fPaint;
     92 
     93 	MeasureView() {
     94         fPaint.setAntiAlias(true);
     95         fPaint.setTextSize(SkIntToScalar(64));
     96         this->setBGColor(0xFFDDDDDD);
     97     }
     98 
     99 protected:
    100     // overrides from SkEventSink
    101     virtual bool onQuery(SkEvent* evt) {
    102         if (SampleCode::TitleQ(*evt)) {
    103             SampleCode::TitleR(evt, "Measure");
    104             return true;
    105         }
    106         return this->INHERITED::onQuery(evt);
    107     }
    108 
    109     virtual void onDrawContent(SkCanvas* canvas) {
    110         canvas->translate(fPaint.getTextSize(), fPaint.getTextSize());
    111         doMeasure(canvas, fPaint, "Hamburgefons");
    112     }
    113 
    114 private:
    115     typedef SampleView INHERITED;
    116 };
    117 
    118 //////////////////////////////////////////////////////////////////////////////
    119 
    120 static SkView* MyFactory() { return new MeasureView; }
    121 static SkViewRegister reg(MyFactory);
    122 
    123