Skip to content

Commit 17c574a

Browse files
reed-at-googleSkia Commit-Bot
authored andcommitted
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>
1 parent 987210d commit 17c574a

File tree

4 files changed

+23
-14
lines changed

4 files changed

+23
-14
lines changed

gm/animatedGif.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include "SkCodec.h"
1313
#include "SkColor.h"
1414
#include "SkCommandLineFlags.h"
15+
#include "SkFont.h"
1516
#include "SkPaint.h"
1617
#include "SkString.h"
1718
#include "Resources.h"
@@ -25,10 +26,11 @@ namespace {
2526
constexpr SkScalar kOffset = 5.0f;
2627
canvas->drawColor(SK_ColorRED);
2728
SkPaint paint;
29+
SkFont font;
2830
SkRect bounds;
29-
paint.measureText(errorText.c_str(), errorText.size(), &bounds);
30-
canvas->drawString(errorText, kOffset, bounds.height() + kOffset,
31-
paint);
31+
font.measureText(errorText.c_str(), errorText.size(), kUTF8_SkTextEncoding, &bounds);
32+
canvas->drawSimpleText(errorText.c_str(), errorText.size(), kUTF8_SkTextEncoding,
33+
kOffset, bounds.height() + kOffset, font, paint);
3234
}
3335
}
3436

gm/annotated_text.cpp

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,35 +7,37 @@
77

88
#include "SkAnnotation.h"
99
#include "SkData.h"
10+
#include "SkFont.h"
1011
#include "gm.h"
1112

1213
static void draw_url_annotated_text_with_box(
1314
SkCanvas* canvas, const void* text,
14-
SkScalar x, SkScalar y, const SkPaint& paint, const char* url) {
15+
SkScalar x, SkScalar y, const SkFont& font, const char* url) {
1516
size_t byteLength = strlen(static_cast<const char*>(text));
1617
SkRect bounds;
17-
(void)paint.measureText(text, byteLength, &bounds);
18+
(void)font.measureText(text, byteLength, kUTF8_SkTextEncoding, &bounds);
1819
bounds.offset(x, y);
1920
sk_sp<SkData> urlData(SkData::MakeWithCString(url));
2021
SkAnnotateRectWithURL(canvas, bounds, urlData.get());
2122
SkPaint shade;
2223
shade.setColor(0x80346180);
2324
canvas->drawRect(bounds, shade);
24-
canvas->drawText(text, byteLength, x, y, paint);
25+
canvas->drawSimpleText(text, byteLength, kUTF8_SkTextEncoding, x, y, font, SkPaint());
2526
}
2627

2728
DEF_SIMPLE_GM(annotated_text, canvas, 512, 512) {
2829
SkAutoCanvasRestore autoCanvasRestore(canvas, true);
2930
canvas->clear(SK_ColorWHITE);
3031
canvas->clipRect(SkRect::MakeXYWH(64, 64, 256, 256));
3132
canvas->clear(0xFFEEEEEE);
32-
SkPaint p;
33-
p.setTextSize(40);
33+
SkFont font;
34+
font.setEdging(SkFont::Edging::kAlias);
35+
font.setSize(40);
3436
const char text[] = "Click this link!";
3537
const char url[] = "https://www.google.com/";
36-
draw_url_annotated_text_with_box(canvas, text, 200.0f, 80.0f, p, url);
38+
draw_url_annotated_text_with_box(canvas, text, 200.0f, 80.0f, font, url);
3739
canvas->saveLayer(nullptr, nullptr);
3840
canvas->rotate(90);
39-
draw_url_annotated_text_with_box(canvas, text, 150.0f, -55.0f, p, url);
41+
draw_url_annotated_text_with_box(canvas, text, 150.0f, -55.0f, font, url);
4042
canvas->restore();
4143
}

gm/arithmode.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
* found in the LICENSE file.
66
*/
77

8+
#include <SkFont.h>
89
#include "gm.h"
910
#include "sk_tool_utils.h"
1011
#include "SkArithmeticImageFilter.h"
@@ -52,15 +53,15 @@ static sk_sp<SkImage> make_dst() {
5253
}
5354

5455
static void show_k_text(SkCanvas* canvas, SkScalar x, SkScalar y, const SkScalar k[]) {
56+
SkFont font(sk_tool_utils::create_portable_typeface(), 24);
57+
font.setEdging(SkFont::Edging::kAntiAlias);
5558
SkPaint paint;
56-
paint.setTextSize(SkIntToScalar(24));
5759
paint.setAntiAlias(true);
58-
sk_tool_utils::set_portable_typeface(&paint);
5960
for (int i = 0; i < 4; ++i) {
6061
SkString str;
6162
str.appendScalar(k[i]);
62-
SkScalar width = paint.measureText(str.c_str(), str.size());
63-
canvas->drawString(str, x, y + paint.getTextSize(), paint);
63+
SkScalar width = font.measureText(str.c_str(), str.size(), kUTF8_SkTextEncoding);
64+
canvas->drawSimpleText(str.c_str(), str.size(), kUTF8_SkTextEncoding, x, y + font.getSize(), font, paint);
6465
x += width + SkIntToScalar(10);
6566
}
6667
}

tests/FontObjTest.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ static void test_cachedfont(skiatest::Reporter* reporter,
3434
REPORTER_ASSERT(reporter, paint.getHinting() == p.getHinting());
3535
}
3636

37+
#ifdef SK_SUPPORT_LEGACY_PAINT_TEXTMEASURE
3738
static void test_fontmetrics(skiatest::Reporter* reporter,
3839
const SkPaint& paint, const SkFont& font) {
3940
SkFontMetrics fm0, fm1;
@@ -50,6 +51,7 @@ static void test_fontmetrics(skiatest::Reporter* reporter,
5051
CMP(fLeading);
5152
#undef CMP
5253
}
54+
#endif
5355

5456
static void test_cachedfont(skiatest::Reporter* reporter) {
5557
static const char* const faces[] = {
@@ -93,6 +95,7 @@ static void test_cachedfont(skiatest::Reporter* reporter) {
9395
const SkFont font(SkFont::LEGACY_ExtractFromPaint(paint));
9496

9597
test_cachedfont(reporter, paint, font);
98+
#ifdef SK_SUPPORT_LEGACY_PAINT_TEXTMEASURE
9699
test_fontmetrics(reporter, paint, font);
97100

98101
SkRect pbounds, fbounds;
@@ -103,6 +106,7 @@ static void test_cachedfont(skiatest::Reporter* reporter) {
103106
&fbounds);
104107
REPORTER_ASSERT(reporter, pwidth == fwidth);
105108
REPORTER_ASSERT(reporter, pbounds == fbounds);
109+
#endif
106110
}
107111
}
108112
}

0 commit comments

Comments
 (0)