Flutter Engine
The Flutter Engine
FontHostStreamTest.cpp
Go to the documentation of this file.
1/*
2 * Copyright 2011 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
11#include "include/core/SkFont.h"
17#include "include/core/SkRect.h"
23#include "src/core/SkFontPriv.h"
24#include "tests/Test.h"
26
27#include <memory>
28#include <utility>
29
31
32static void create(SkBitmap* bm, SkIRect bound) {
33 bm->allocN32Pixels(bound.width(), bound.height());
34}
35
36static void drawBG(SkCanvas* canvas) {
37 canvas->drawColor(bgColor);
38}
39
40/** Assumes that the ref draw was completely inside ref canvas --
41 implies that everything outside is "bgColor".
42 Checks that all overlap is the same and that all non-overlap on the
43 ref is "bgColor".
44 */
45static bool compare(const SkBitmap& ref, const SkIRect& iref,
46 const SkBitmap& test, const SkIRect& itest)
47{
48 const int xOff = itest.fLeft - iref.fLeft;
49 const int yOff = itest.fTop - iref.fTop;
50
51 for (int y = 0; y < test.height(); ++y) {
52 for (int x = 0; x < test.width(); ++x) {
53 SkColor testColor = test.getColor(x, y);
54 int refX = x + xOff;
55 int refY = y + yOff;
56 SkColor refColor;
57 if (refX >= 0 && refX < ref.width() &&
58 refY >= 0 && refY < ref.height())
59 {
60 refColor = ref.getColor(refX, refY);
61 } else {
62 refColor = bgColor;
63 }
64 if (refColor != testColor) {
65 return false;
66 }
67 }
68 }
69 return true;
70}
71
72DEF_TEST(FontHostStream, reporter) {
73 {
75 paint.setColor(SK_ColorGRAY);
76
79
80 const SkIRect origRect = SkIRect::MakeWH(64, 64);
81 SkBitmap origBitmap;
82 create(&origBitmap, origRect);
83 SkCanvas origCanvas(origBitmap);
84
85 SkPoint point = SkPoint::Make(24, 32);
86
87 // Test: origTypeface and streamTypeface from orig data draw the same
88 drawBG(&origCanvas);
89 origCanvas.drawString("A", point.fX, point.fY, font, paint);
90
92 sk_sp<SkTypeface> typeface = font.refTypeface();
93 SkASSERT_RELEASE(typeface);
94
95 {
98 std::unique_ptr<SkStreamAsset> stream = wstream.detachAsStream();
99 sk_sp<SkTypeface> deserializedTypeface = SkTypeface::MakeDeserialize(&*stream, mgr);
100 if (!deserializedTypeface) {
101 REPORTER_ASSERT(reporter, deserializedTypeface);
102 return;
103 }
104
106 bool mustSerializeData = false;
107 deserializedTypeface->getFontDescriptor(&desc, &mustSerializeData);
108 REPORTER_ASSERT(reporter, mustSerializeData);
109
110 SkBitmap deserializedBitmap;
111 create(&deserializedBitmap, origRect);
112 SkCanvas deserializedCanvas(deserializedBitmap);
113
114 font.setTypeface(deserializedTypeface);
115 drawBG(&deserializedCanvas);
116 deserializedCanvas.drawString("A", point.fX, point.fY, font, paint);
117
118 REPORTER_ASSERT(reporter, compare(origBitmap, origRect, deserializedBitmap, origRect));
119 }
120
121 {
122 int ttcIndex;
123 std::unique_ptr<SkStreamAsset> fontData = typeface->openStream(&ttcIndex);
124 if (!fontData) {
125 REPORTER_ASSERT(reporter, fontData);
126 return;
127 }
128
129 sk_sp<SkTypeface> streamTypeface(mgr->makeFromStream(std::move(fontData), 0));
130 if (!streamTypeface) {
131 // TODO: enable assert after SkTypeface::MakeFromStream uses factories
132 //REPORTER_ASSERT(reporter, streamTypeface);
133 return;
134 }
135
136 SkBitmap streamBitmap;
137 create(&streamBitmap, origRect);
138 SkCanvas streamCanvas(streamBitmap);
139
141 bool mustSerializeData = false;
142 streamTypeface->getFontDescriptor(&desc, &mustSerializeData);
143 REPORTER_ASSERT(reporter, mustSerializeData);
144
145 font.setTypeface(streamTypeface);
146 drawBG(&streamCanvas);
147 streamCanvas.drawString("A", point.fX, point.fY, font, paint);
148
149 REPORTER_ASSERT(reporter, compare(origBitmap, origRect, streamBitmap, origRect));
150 }
151 }
152 //Make sure the typeface is deleted and removed.
154}
static void create(SkBitmap *bm, SkIRect bound)
DEF_TEST(FontHostStream, reporter)
static bool compare(const SkBitmap &ref, const SkIRect &iref, const SkBitmap &test, const SkIRect &itest)
static void drawBG(SkCanvas *canvas)
static const SkColor bgColor
reporter
Definition: FontMgrTest.cpp:39
#define SkASSERT_RELEASE(cond)
Definition: SkAssert.h:100
uint32_t SkColor
Definition: SkColor.h:37
constexpr SkColor SK_ColorGRAY
Definition: SkColor.h:113
constexpr SkColor SK_ColorWHITE
Definition: SkColor.h:122
#define REPORTER_ASSERT(r, cond,...)
Definition: Test.h:286
SkColor getColor(int x, int y) const
Definition: SkBitmap.h:874
int width() const
Definition: SkBitmap.h:149
void allocN32Pixels(int width, int height, bool isOpaque=false)
Definition: SkBitmap.cpp:232
int height() const
Definition: SkBitmap.h:158
void drawColor(SkColor color, SkBlendMode mode=SkBlendMode::kSrcOver)
Definition: SkCanvas.h:1182
void drawString(const char str[], SkScalar x, SkScalar y, const SkFont &font, const SkPaint &paint)
Definition: SkCanvas.h:1803
std::unique_ptr< SkStreamAsset > detachAsStream()
Definition: SkStream.cpp:876
sk_sp< SkTypeface > makeFromStream(std::unique_ptr< SkStreamAsset >, int ttcIndex=0) const
Definition: SkFontMgr.cpp:127
Definition: SkFont.h:35
@ kAlias
no transparent pixels on glyph edges
static void PurgeFontCache()
Definition: SkGraphics.cpp:72
void getFontDescriptor(SkFontDescriptor *desc, bool *isLocal) const
Definition: SkTypeface.h:326
void serialize(SkWStream *, SerializeBehavior=SerializeBehavior::kIncludeDataIfLocal) const
Definition: SkTypeface.cpp:202
static sk_sp< SkTypeface > MakeDeserialize(SkStream *, sk_sp< SkFontMgr > lastResortMgr)
Definition: SkTypeface.cpp:241
std::unique_ptr< SkStreamAsset > openStream(int *ttcIndex) const
Definition: SkTypeface.cpp:332
const Paint & paint
Definition: color_source.cc:38
double y
double x
sk_sp< SkTypeface > CreateTestTypeface(const char *name, SkFontStyle style)
sk_sp< SkFontMgr > TestFontMgr()
font
Font Metadata and Metrics.
Definition: SkRect.h:32
constexpr int32_t height() const
Definition: SkRect.h:165
int32_t fTop
smaller y-axis bounds
Definition: SkRect.h:34
constexpr int32_t width() const
Definition: SkRect.h:158
static constexpr SkIRect MakeWH(int32_t w, int32_t h)
Definition: SkRect.h:56
int32_t fLeft
smaller x-axis bounds
Definition: SkRect.h:33
float fX
x-axis value
Definition: SkPoint_impl.h:164
static constexpr SkPoint Make(float x, float y)
Definition: SkPoint_impl.h:173
float fY
y-axis value
Definition: SkPoint_impl.h:165