2011-07-28 14:26:00 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2011 Google Inc.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
|
|
|
*/
|
2008-12-17 15:59:43 +00:00
|
|
|
#include "SampleCode.h"
|
|
|
|
#include "SkView.h"
|
|
|
|
#include "SkCanvas.h"
|
|
|
|
#include "SkGradientShader.h"
|
|
|
|
#include "SkPath.h"
|
|
|
|
#include "SkRegion.h"
|
|
|
|
#include "SkShader.h"
|
|
|
|
#include "SkUtils.h"
|
|
|
|
#include "Sk1DPathEffect.h"
|
|
|
|
#include "SkCornerPathEffect.h"
|
|
|
|
#include "SkPathMeasure.h"
|
|
|
|
#include "SkRandom.h"
|
|
|
|
#include "SkColorPriv.h"
|
|
|
|
#include "SkColorFilter.h"
|
|
|
|
#include "SkDither.h"
|
|
|
|
|
|
|
|
// exercise scale/linear/devkern
|
|
|
|
struct Setting {
|
|
|
|
bool fLinearText;
|
|
|
|
bool fDevKernText;
|
|
|
|
};
|
|
|
|
|
|
|
|
static const Setting gSettings[] = {
|
2014-08-26 18:30:01 +00:00
|
|
|
{ false, false },
|
|
|
|
{ false, true },
|
|
|
|
{ true, false },
|
|
|
|
{ true, true },
|
2008-12-17 15:59:43 +00:00
|
|
|
};
|
|
|
|
|
2011-06-01 12:42:36 +00:00
|
|
|
static void doMeasure(SkCanvas* canvas, const SkPaint& paint, const char text[]) {
|
2015-08-27 14:41:13 +00:00
|
|
|
SkScalar dy = paint.getFontMetrics(nullptr);
|
2008-12-17 15:59:43 +00:00
|
|
|
|
|
|
|
size_t len = strlen(text);
|
|
|
|
SkAutoTMalloc<SkScalar> autoWidths(len);
|
|
|
|
SkScalar* widths = autoWidths.get();
|
|
|
|
SkAutoTMalloc<SkRect> autoRects(len);
|
|
|
|
SkRect* rects = autoRects.get();
|
|
|
|
SkRect bounds;
|
|
|
|
|
|
|
|
SkPaint p(paint);
|
2011-05-19 19:58:58 +00:00
|
|
|
for (size_t i = 0; i < SK_ARRAY_COUNT(gSettings); i++) {
|
2008-12-17 15:59:43 +00:00
|
|
|
p.setLinearText(gSettings[i].fLinearText);
|
|
|
|
p.setDevKernText(gSettings[i].fDevKernText);
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
int n = p.getTextWidths(text, len, widths, rects);
|
2014-08-26 18:30:01 +00:00
|
|
|
SkScalar w = p.measureText(text, len, &bounds);
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
p.setStyle(SkPaint::kFill_Style);
|
|
|
|
p.setColor(0x8888FF88);
|
|
|
|
canvas->drawRect(bounds, p);
|
|
|
|
p.setColor(0xFF000000);
|
|
|
|
canvas->drawText(text, len, 0, 0, p);
|
|
|
|
|
|
|
|
p.setStyle(SkPaint::kStroke_Style);
|
|
|
|
p.setStrokeWidth(0);
|
|
|
|
p.setColor(0xFFFF0000);
|
|
|
|
SkScalar x = 0;
|
|
|
|
for (int j = 0; j < n; j++) {
|
|
|
|
SkRect r = rects[j];
|
|
|
|
r.offset(x, 0);
|
|
|
|
canvas->drawRect(r, p);
|
|
|
|
x += widths[j];
|
|
|
|
}
|
|
|
|
|
|
|
|
p.setColor(0xFF0000FF);
|
|
|
|
canvas->drawLine(0, 0, w, 0, p);
|
|
|
|
p.setStrokeWidth(SkIntToScalar(4));
|
|
|
|
canvas->drawPoint(x, 0, p);
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
canvas->translate(0, dy);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-06-01 12:42:36 +00:00
|
|
|
class MeasureView : public SampleView {
|
2008-12-17 15:59:43 +00:00
|
|
|
public:
|
|
|
|
SkPaint fPaint;
|
|
|
|
|
2012-08-23 18:19:56 +00:00
|
|
|
MeasureView() {
|
2008-12-17 15:59:43 +00:00
|
|
|
fPaint.setAntiAlias(true);
|
|
|
|
fPaint.setTextSize(SkIntToScalar(64));
|
2011-06-01 12:42:36 +00:00
|
|
|
this->setBGColor(0xFFDDDDDD);
|
2008-12-17 15:59:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
|
|
|
// overrides from SkEventSink
|
2011-06-01 12:42:36 +00:00
|
|
|
virtual bool onQuery(SkEvent* evt) {
|
|
|
|
if (SampleCode::TitleQ(*evt)) {
|
2008-12-17 15:59:43 +00:00
|
|
|
SampleCode::TitleR(evt, "Measure");
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return this->INHERITED::onQuery(evt);
|
|
|
|
}
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2011-06-01 12:42:36 +00:00
|
|
|
virtual void onDrawContent(SkCanvas* canvas) {
|
2008-12-17 15:59:43 +00:00
|
|
|
canvas->translate(fPaint.getTextSize(), fPaint.getTextSize());
|
|
|
|
doMeasure(canvas, fPaint, "Hamburgefons");
|
|
|
|
}
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
private:
|
2011-06-01 12:42:36 +00:00
|
|
|
typedef SampleView INHERITED;
|
2008-12-17 15:59:43 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
static SkView* MyFactory() { return new MeasureView; }
|
|
|
|
static SkViewRegister reg(MyFactory);
|