skia2/samplecode/SampleMeasure.cpp
Ben Wagner b2c4ea6219 Move Views into Sample and Viewer.
What is left of the SkView system is used only by samples or viewer.
As a result, move it out of the Skia source tree and re-organize so it
is a bit easier to understand and use more shared code.

Move samplecode/ClockFaceView.cpp to samplecode/SampleTextEffects.cpp,
sice that's what's actually in it.

Move SkAnimTimer.h to tools/timer, since it's actually shared between gm
and samples.

Change-Id: I55dafd94c64e4f930ddbd19168e0f812af86c455
Reviewed-on: https://skia-review.googlesource.com/146161
Reviewed-by: Brian Osman <brianosman@google.com>
Commit-Queue: Ben Wagner <bungeman@google.com>
2018-08-09 15:25:32 +00:00

110 lines
2.8 KiB
C++

/*
* Copyright 2011 Google Inc.
*
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*/
#include "Sample.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[] = {
{ false, false },
{ false, true },
{ true, false },
{ true, true },
};
static void doMeasure(SkCanvas* canvas, const SkPaint& paint, const char text[]) {
SkScalar dy = paint.getFontMetrics(nullptr);
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);
for (size_t i = 0; i < SK_ARRAY_COUNT(gSettings); i++) {
p.setLinearText(gSettings[i].fLinearText);
p.setDevKernText(gSettings[i].fDevKernText);
int n = p.getTextWidths(text, len, widths, rects);
SkScalar w = p.measureText(text, len, &bounds);
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);
canvas->translate(0, dy);
}
}
class MeasureView : public Sample {
public:
SkPaint fPaint;
MeasureView() {
fPaint.setAntiAlias(true);
fPaint.setTextSize(SkIntToScalar(64));
this->setBGColor(0xFFDDDDDD);
}
protected:
virtual bool onQuery(Sample::Event* evt) {
if (Sample::TitleQ(*evt)) {
Sample::TitleR(evt, "Measure");
return true;
}
return this->INHERITED::onQuery(evt);
}
virtual void onDrawContent(SkCanvas* canvas) {
canvas->translate(fPaint.getTextSize(), fPaint.getTextSize());
doMeasure(canvas, fPaint, "Hamburgefons");
}
private:
typedef Sample INHERITED;
};
//////////////////////////////////////////////////////////////////////////////
DEF_SAMPLE( return new MeasureView(); )