skia2/gm/pdf_never_embed.cpp
Cary Clark 2a475eae62 add drawString helper to canvas
Many tests and examples use drawText with
a guess of how long the text is in bytes,
or a call to strlen(). Add a helper to
SkCanvas to simplify these examples.

Add another helper for SkString.

R=reed@google.com

Change-Id: I0204a31e938f065606f08ee7cd9a6b36db791ee2
Reviewed-on: https://skia-review.googlesource.com/13642
Commit-Queue: Cary Clark <caryclark@google.com>
Reviewed-by: Cary Clark <caryclark@google.com>
Reviewed-by: Mike Reed <reed@google.com>
Reviewed-by: Cary Clark <caryclark@skia.org>
2017-04-28 20:41:04 +00:00

58 lines
1.5 KiB
C++

/*
* Copyright 2015 Google Inc.
*
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*/
#include "Resources.h"
#include "SkTypeface.h"
#include "gm.h"
static void excercise_draw_pos_text(SkCanvas* canvas,
const char* text,
SkScalar x, SkScalar y,
const SkPaint& paint) {
size_t textLen = strlen(text);
SkAutoTArray<SkScalar> widths(SkToInt(textLen));
paint.getTextWidths(text, textLen, &widths[0]);
SkAutoTArray<SkPoint> pos(SkToInt(textLen));
for (int i = 0; i < SkToInt(textLen); ++i) {
pos[i].set(x, y);
x += widths[i];
}
canvas->drawPosText(text, textLen, &pos[0], paint);
}
DEF_SIMPLE_GM(pdf_never_embed, canvas, 512, 512) {
SkPaint p;
p.setTextSize(60);
p.setTypeface(MakeResourceAsTypeface("fonts/Roboto2-Regular_NoEmbed.ttf"));
p.setAntiAlias(true);
if (!p.getTypeface()) {
return;
}
const char text[] = "HELLO, WORLD!";
canvas->drawColor(SK_ColorWHITE);
excercise_draw_pos_text(canvas, text, 30, 90, p);
canvas->save();
canvas->rotate(45.0f);
p.setColor(0xF0800000);
excercise_draw_pos_text(canvas, text, 30, 45, p);
canvas->restore();
canvas->save();
canvas->scale(1, 4.0);
p.setColor(0xF0008000);
excercise_draw_pos_text(canvas, text, 15, 70, p);
canvas->restore();
canvas->scale(1.0, 0.5);
p.setColor(0xF0000080);
canvas->drawString(text, 30, 700, p);
}