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>
diff --git a/gm/animatedGif.cpp b/gm/animatedGif.cpp
index f2fb09d..72116eb 100644
--- a/gm/animatedGif.cpp
+++ b/gm/animatedGif.cpp
@@ -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 @@
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);
}
}
diff --git a/gm/annotated_text.cpp b/gm/annotated_text.cpp
index 70ee9f3..c501464 100644
--- a/gm/annotated_text.cpp
+++ b/gm/annotated_text.cpp
@@ -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 @@
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();
}
diff --git a/gm/arithmode.cpp b/gm/arithmode.cpp
index 7ebce7e..a765223 100644
--- a/gm/arithmode.cpp
+++ b/gm/arithmode.cpp
@@ -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 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);
}
}
diff --git a/tests/FontObjTest.cpp b/tests/FontObjTest.cpp
index 8085e07..3d0e4ce 100644
--- a/tests/FontObjTest.cpp
+++ b/tests/FontObjTest.cpp
@@ -34,6 +34,7 @@
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 @@
CMP(fLeading);
#undef CMP
}
+#endif
static void test_cachedfont(skiatest::Reporter* reporter) {
static const char* const faces[] = {
@@ -93,6 +95,7 @@
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 @@
&fbounds);
REPORTER_ASSERT(reporter, pwidth == fwidth);
REPORTER_ASSERT(reporter, pbounds == fbounds);
+#endif
}
}
}