blob: a36450f3dbd9fc5fdc0f1ef45f7c86860b82aba2 [file] [log] [blame]
bungeman@google.coma5501992012-05-18 19:06:41 +00001/*
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 */
bungeman@google.coma5501992012-05-18 19:06:41 +00007
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "include/core/SkBitmap.h"
9#include "include/core/SkCanvas.h"
10#include "include/core/SkColor.h"
11#include "include/core/SkFont.h"
Kevin Lubick1e971192023-11-10 16:14:44 -050012#include "include/core/SkFontMgr.h"
Kevin Lubick8c73a592022-10-17 15:25:35 -040013#include "include/core/SkFontStyle.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050014#include "include/core/SkGraphics.h"
15#include "include/core/SkPaint.h"
16#include "include/core/SkPoint.h"
17#include "include/core/SkRect.h"
Kevin Lubick8c73a592022-10-17 15:25:35 -040018#include "include/core/SkRefCnt.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050019#include "include/core/SkStream.h"
20#include "include/core/SkTypeface.h"
21#include "include/core/SkTypes.h"
22#include "src/core/SkFontDescriptor.h"
Kevin Lubicke5c37862023-10-16 17:21:15 -040023#include "src/core/SkFontPriv.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050024#include "tests/Test.h"
Kevin Lubick1e971192023-11-10 16:14:44 -050025#include "tools/fonts/FontToolUtils.h"
bungeman@google.coma5501992012-05-18 19:06:41 +000026
Kevin Lubick8c73a592022-10-17 15:25:35 -040027#include <memory>
28#include <utility>
29
Hal Canary21c348242019-01-09 12:51:52 -050030static const SkColor bgColor = SK_ColorWHITE;
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
bungeman@google.coma5501992012-05-18 19:06:41 +000051 for (int y = 0; y < test.height(); ++y) {
52 for (int x = 0; x < test.width(); ++x) {
Hal Canary21c348242019-01-09 12:51:52 -050053 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) {
bungeman@google.coma5501992012-05-18 19:06:41 +000065 return false;
66 }
67 }
68 }
69 return true;
70}
71
tfarina@chromium.orge4fafb12013-12-12 21:11:12 +000072DEF_TEST(FontHostStream, reporter) {
Hal Canary21c348242019-01-09 12:51:52 -050073 {
74 SkPaint paint;
75 paint.setColor(SK_ColorGRAY);
76
Kevin Lubick1e971192023-11-10 16:14:44 -050077 SkFont font(ToolUtils::CreateTestTypeface("Georgia", SkFontStyle()), 30);
Mike Reed22bee5a2019-01-09 21:48:09 -050078 font.setEdging(SkFont::Edging::kAlias);
Hal Canary21c348242019-01-09 12:51:52 -050079
Ben Wagnerb588fea2023-02-13 14:44:54 -050080 const SkIRect origRect = SkIRect::MakeWH(64, 64);
Hal Canary21c348242019-01-09 12:51:52 -050081 SkBitmap origBitmap;
82 create(&origBitmap, origRect);
83 SkCanvas origCanvas(origBitmap);
84
Hal Canary21c348242019-01-09 12:51:52 -050085 SkPoint point = SkPoint::Make(24, 32);
86
87 // Test: origTypeface and streamTypeface from orig data draw the same
88 drawBG(&origCanvas);
Mike Reed22bee5a2019-01-09 21:48:09 -050089 origCanvas.drawString("A", point.fX, point.fY, font, paint);
Hal Canary21c348242019-01-09 12:51:52 -050090
Kevin Lubick1e971192023-11-10 16:14:44 -050091 sk_sp<SkFontMgr> mgr = ToolUtils::TestFontMgr();
92 sk_sp<SkTypeface> typeface = font.refTypeface();
93 SkASSERT_RELEASE(typeface);
Ben Wagnerb588fea2023-02-13 14:44:54 -050094
95 {
96 SkDynamicMemoryWStream wstream;
97 typeface->serialize(&wstream, SkTypeface::SerializeBehavior::kDoIncludeData);
98 std::unique_ptr<SkStreamAsset> stream = wstream.detachAsStream();
Kevin Lubick1e971192023-11-10 16:14:44 -050099 sk_sp<SkTypeface> deserializedTypeface = SkTypeface::MakeDeserialize(&*stream, mgr);
Ben Wagnerb588fea2023-02-13 14:44:54 -0500100 if (!deserializedTypeface) {
101 REPORTER_ASSERT(reporter, deserializedTypeface);
102 return;
103 }
104
105 SkFontDescriptor desc;
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));
Hal Canary21c348242019-01-09 12:51:52 -0500119 }
120
Ben Wagnerb588fea2023-02-13 14:44:54 -0500121 {
122 int ttcIndex;
123 std::unique_ptr<SkStreamAsset> fontData = typeface->openStream(&ttcIndex);
124 if (!fontData) {
125 REPORTER_ASSERT(reporter, fontData);
126 return;
127 }
Hal Canary21c348242019-01-09 12:51:52 -0500128
Kevin Lubick1e971192023-11-10 16:14:44 -0500129 sk_sp<SkTypeface> streamTypeface(mgr->makeFromStream(std::move(fontData), 0));
Ben Wagnerb588fea2023-02-13 14:44:54 -0500130 if (!streamTypeface) {
131 // TODO: enable assert after SkTypeface::MakeFromStream uses factories
132 //REPORTER_ASSERT(reporter, streamTypeface);
133 return;
134 }
Hal Canary21c348242019-01-09 12:51:52 -0500135
Ben Wagnerb588fea2023-02-13 14:44:54 -0500136 SkBitmap streamBitmap;
137 create(&streamBitmap, origRect);
138 SkCanvas streamCanvas(streamBitmap);
Hal Canary21c348242019-01-09 12:51:52 -0500139
Ben Wagnerb588fea2023-02-13 14:44:54 -0500140 SkFontDescriptor desc;
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 }
bungeman@google.coma5501992012-05-18 19:06:41 +0000151 }
152 //Make sure the typeface is deleted and removed.
153 SkGraphics::PurgeFontCache();
154}