Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
Functions | Variables
FontHostStreamTest.cpp File Reference
#include "include/core/SkBitmap.h"
#include "include/core/SkCanvas.h"
#include "include/core/SkColor.h"
#include "include/core/SkFont.h"
#include "include/core/SkFontMgr.h"
#include "include/core/SkFontStyle.h"
#include "include/core/SkGraphics.h"
#include "include/core/SkPaint.h"
#include "include/core/SkPoint.h"
#include "include/core/SkRect.h"
#include "include/core/SkRefCnt.h"
#include "include/core/SkStream.h"
#include "include/core/SkTypeface.h"
#include "include/core/SkTypes.h"
#include "src/core/SkFontDescriptor.h"
#include "src/core/SkFontPriv.h"
#include "tests/Test.h"
#include "tools/fonts/FontToolUtils.h"
#include <memory>
#include <utility>

Go to the source code of this file.

Functions

static void create (SkBitmap *bm, SkIRect bound)
 
static void drawBG (SkCanvas *canvas)
 
static bool compare (const SkBitmap &ref, const SkIRect &iref, const SkBitmap &test, const SkIRect &itest)
 
 DEF_TEST (FontHostStream, reporter)
 

Variables

static const SkColor bgColor = SK_ColorWHITE
 

Function Documentation

◆ compare()

static bool compare ( const SkBitmap ref,
const SkIRect iref,
const SkBitmap test,
const SkIRect itest 
)
static

Assumes that the ref draw was completely inside ref canvas – implies that everything outside is "bgColor". Checks that all overlap is the same and that all non-overlap on the ref is "bgColor".

Definition at line 45 of file FontHostStreamTest.cpp.

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}
static const SkColor bgColor
uint32_t SkColor
Definition SkColor.h:37
SkColor getColor(int x, int y) const
Definition SkBitmap.h:874
int width() const
Definition SkBitmap.h:149
int height() const
Definition SkBitmap.h:158
double y
double x
int32_t fTop
smaller y-axis bounds
Definition SkRect.h:34
int32_t fLeft
smaller x-axis bounds
Definition SkRect.h:33

◆ create()

static void create ( SkBitmap bm,
SkIRect  bound 
)
static

Definition at line 32 of file FontHostStreamTest.cpp.

32 {
33 bm->allocN32Pixels(bound.width(), bound.height());
34}
void allocN32Pixels(int width, int height, bool isOpaque=false)
Definition SkBitmap.cpp:232
constexpr int32_t height() const
Definition SkRect.h:165
constexpr int32_t width() const
Definition SkRect.h:158

◆ DEF_TEST()

DEF_TEST ( FontHostStream  ,
reporter   
)

Definition at line 72 of file FontHostStreamTest.cpp.

72 {
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 {
97 typeface->serialize(&wstream, SkTypeface::SerializeBehavior::kDoIncludeData);
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 bool compare(const SkBitmap &ref, const SkIRect &iref, const SkBitmap &test, const SkIRect &itest)
static void drawBG(SkCanvas *canvas)
reporter
#define SkASSERT_RELEASE(cond)
Definition SkAssert.h:100
constexpr SkColor SK_ColorGRAY
Definition SkColor.h:113
#define REPORTER_ASSERT(r, cond,...)
Definition Test.h:286
std::unique_ptr< SkStreamAsset > detachAsStream()
Definition SkStream.cpp:876
@ kAlias
no transparent pixels on glyph edges
static void PurgeFontCache()
static sk_sp< SkTypeface > MakeDeserialize(SkStream *, sk_sp< SkFontMgr > lastResortMgr)
const Paint & paint
sk_sp< SkTypeface > CreateTestTypeface(const char *name, SkFontStyle style)
sk_sp< SkFontMgr > TestFontMgr()
font
Font Metadata and Metrics.
static constexpr SkIRect MakeWH(int32_t w, int32_t h)
Definition SkRect.h:56
float fX
x-axis value
static constexpr SkPoint Make(float x, float y)
float fY
y-axis value

◆ drawBG()

static void drawBG ( SkCanvas canvas)
static

Definition at line 36 of file FontHostStreamTest.cpp.

36 {
37 canvas->drawColor(bgColor);
38}
void drawColor(SkColor color, SkBlendMode mode=SkBlendMode::kSrcOver)
Definition SkCanvas.h:1182

Variable Documentation

◆ bgColor

const SkColor bgColor = SK_ColorWHITE
static

Definition at line 30 of file FontHostStreamTest.cpp.