use font for measuring

Bug: skia:
Change-Id: Ie1cd247af06af515e078017d0716e345b1efc3fd
Reviewed-on: https://skia-review.googlesource.com/c/177076
Reviewed-by: Mike Reed <reed@google.com>
Commit-Queue: Mike Reed <reed@google.com>
This commit is contained in:
Mike Reed 2018-12-12 18:10:38 -05:00 committed by Skia Commit-Bot
parent 987210d454
commit 17c574a193
4 changed files with 23 additions and 14 deletions

View File

@ -12,6 +12,7 @@
#include "SkCodec.h"
#include "SkColor.h"
#include "SkCommandLineFlags.h"
#include "SkFont.h"
#include "SkPaint.h"
#include "SkString.h"
#include "Resources.h"
@ -25,10 +26,11 @@ namespace {
constexpr SkScalar kOffset = 5.0f;
canvas->drawColor(SK_ColorRED);
SkPaint paint;
SkFont font;
SkRect bounds;
paint.measureText(errorText.c_str(), errorText.size(), &bounds);
canvas->drawString(errorText, kOffset, bounds.height() + kOffset,
paint);
font.measureText(errorText.c_str(), errorText.size(), kUTF8_SkTextEncoding, &bounds);
canvas->drawSimpleText(errorText.c_str(), errorText.size(), kUTF8_SkTextEncoding,
kOffset, bounds.height() + kOffset, font, paint);
}
}

View File

@ -7,21 +7,22 @@
#include "SkAnnotation.h"
#include "SkData.h"
#include "SkFont.h"
#include "gm.h"
static void draw_url_annotated_text_with_box(
SkCanvas* canvas, const void* text,
SkScalar x, SkScalar y, const SkPaint& paint, const char* url) {
SkScalar x, SkScalar y, const SkFont& font, const char* url) {
size_t byteLength = strlen(static_cast<const char*>(text));
SkRect bounds;
(void)paint.measureText(text, byteLength, &bounds);
(void)font.measureText(text, byteLength, kUTF8_SkTextEncoding, &bounds);
bounds.offset(x, y);
sk_sp<SkData> urlData(SkData::MakeWithCString(url));
SkAnnotateRectWithURL(canvas, bounds, urlData.get());
SkPaint shade;
shade.setColor(0x80346180);
canvas->drawRect(bounds, shade);
canvas->drawText(text, byteLength, x, y, paint);
canvas->drawSimpleText(text, byteLength, kUTF8_SkTextEncoding, x, y, font, SkPaint());
}
DEF_SIMPLE_GM(annotated_text, canvas, 512, 512) {
@ -29,13 +30,14 @@ DEF_SIMPLE_GM(annotated_text, canvas, 512, 512) {
canvas->clear(SK_ColorWHITE);
canvas->clipRect(SkRect::MakeXYWH(64, 64, 256, 256));
canvas->clear(0xFFEEEEEE);
SkPaint p;
p.setTextSize(40);
SkFont font;
font.setEdging(SkFont::Edging::kAlias);
font.setSize(40);
const char text[] = "Click this link!";
const char url[] = "https://www.google.com/";
draw_url_annotated_text_with_box(canvas, text, 200.0f, 80.0f, p, url);
draw_url_annotated_text_with_box(canvas, text, 200.0f, 80.0f, font, url);
canvas->saveLayer(nullptr, nullptr);
canvas->rotate(90);
draw_url_annotated_text_with_box(canvas, text, 150.0f, -55.0f, p, url);
draw_url_annotated_text_with_box(canvas, text, 150.0f, -55.0f, font, url);
canvas->restore();
}

View File

@ -5,6 +5,7 @@
* found in the LICENSE file.
*/
#include <SkFont.h>
#include "gm.h"
#include "sk_tool_utils.h"
#include "SkArithmeticImageFilter.h"
@ -52,15 +53,15 @@ static sk_sp<SkImage> make_dst() {
}
static void show_k_text(SkCanvas* canvas, SkScalar x, SkScalar y, const SkScalar k[]) {
SkFont font(sk_tool_utils::create_portable_typeface(), 24);
font.setEdging(SkFont::Edging::kAntiAlias);
SkPaint paint;
paint.setTextSize(SkIntToScalar(24));
paint.setAntiAlias(true);
sk_tool_utils::set_portable_typeface(&paint);
for (int i = 0; i < 4; ++i) {
SkString str;
str.appendScalar(k[i]);
SkScalar width = paint.measureText(str.c_str(), str.size());
canvas->drawString(str, x, y + paint.getTextSize(), paint);
SkScalar width = font.measureText(str.c_str(), str.size(), kUTF8_SkTextEncoding);
canvas->drawSimpleText(str.c_str(), str.size(), kUTF8_SkTextEncoding, x, y + font.getSize(), font, paint);
x += width + SkIntToScalar(10);
}
}

View File

@ -34,6 +34,7 @@ static void test_cachedfont(skiatest::Reporter* reporter,
REPORTER_ASSERT(reporter, paint.getHinting() == p.getHinting());
}
#ifdef SK_SUPPORT_LEGACY_PAINT_TEXTMEASURE
static void test_fontmetrics(skiatest::Reporter* reporter,
const SkPaint& paint, const SkFont& font) {
SkFontMetrics fm0, fm1;
@ -50,6 +51,7 @@ static void test_fontmetrics(skiatest::Reporter* reporter,
CMP(fLeading);
#undef CMP
}
#endif
static void test_cachedfont(skiatest::Reporter* reporter) {
static const char* const faces[] = {
@ -93,6 +95,7 @@ static void test_cachedfont(skiatest::Reporter* reporter) {
const SkFont font(SkFont::LEGACY_ExtractFromPaint(paint));
test_cachedfont(reporter, paint, font);
#ifdef SK_SUPPORT_LEGACY_PAINT_TEXTMEASURE
test_fontmetrics(reporter, paint, font);
SkRect pbounds, fbounds;
@ -103,6 +106,7 @@ static void test_cachedfont(skiatest::Reporter* reporter) {
&fbounds);
REPORTER_ASSERT(reporter, pwidth == fwidth);
REPORTER_ASSERT(reporter, pbounds == fbounds);
#endif
}
}
}