• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
8 #include "include/core/SkBitmap.h"
9 #include "include/core/SkCanvas.h"
10 #include "include/core/SkColor.h"
11 #include "include/core/SkFont.h"
12 #include "include/core/SkFontStyle.h"
13 #include "include/core/SkGraphics.h"
14 #include "include/core/SkPaint.h"
15 #include "include/core/SkPoint.h"
16 #include "include/core/SkRect.h"
17 #include "include/core/SkRefCnt.h"
18 #include "include/core/SkStream.h"
19 #include "include/core/SkTypeface.h"
20 #include "include/core/SkTypes.h"
21 #include "src/core/SkFontDescriptor.h"
22 #include "tests/Test.h"
23 
24 #include <memory>
25 #include <utility>
26 
27 static const SkColor bgColor = SK_ColorWHITE;
28 
create(SkBitmap * bm,SkIRect bound)29 static void create(SkBitmap* bm, SkIRect bound) {
30     bm->allocN32Pixels(bound.width(), bound.height());
31 }
32 
drawBG(SkCanvas * canvas)33 static void drawBG(SkCanvas* canvas) {
34     canvas->drawColor(bgColor);
35 }
36 
37 /** Assumes that the ref draw was completely inside ref canvas --
38     implies that everything outside is "bgColor".
39     Checks that all overlap is the same and that all non-overlap on the
40     ref is "bgColor".
41  */
compare(const SkBitmap & ref,const SkIRect & iref,const SkBitmap & test,const SkIRect & itest)42 static bool compare(const SkBitmap& ref, const SkIRect& iref,
43                     const SkBitmap& test, const SkIRect& itest)
44 {
45     const int xOff = itest.fLeft - iref.fLeft;
46     const int yOff = itest.fTop - iref.fTop;
47 
48     for (int y = 0; y < test.height(); ++y) {
49         for (int x = 0; x < test.width(); ++x) {
50             SkColor testColor = test.getColor(x, y);
51             int refX = x + xOff;
52             int refY = y + yOff;
53             SkColor refColor;
54             if (refX >= 0 && refX < ref.width() &&
55                 refY >= 0 && refY < ref.height())
56             {
57                 refColor = ref.getColor(refX, refY);
58             } else {
59                 refColor = bgColor;
60             }
61             if (refColor != testColor) {
62                 return false;
63             }
64         }
65     }
66     return true;
67 }
68 
DEF_TEST(FontHostStream,reporter)69 DEF_TEST(FontHostStream, reporter) {
70     {
71         SkPaint paint;
72         paint.setColor(SK_ColorGRAY);
73 
74         SkFont font(SkTypeface::MakeFromName("Georgia", SkFontStyle()), 30);
75         font.setEdging(SkFont::Edging::kAlias);
76 
77         const SkIRect origRect = SkIRect::MakeWH(64, 64);
78         SkBitmap origBitmap;
79         create(&origBitmap, origRect);
80         SkCanvas origCanvas(origBitmap);
81 
82         SkPoint point = SkPoint::Make(24, 32);
83 
84         // Test: origTypeface and streamTypeface from orig data draw the same
85         drawBG(&origCanvas);
86         origCanvas.drawString("A", point.fX, point.fY, font, paint);
87 
88         sk_sp<SkTypeface> typeface = font.refTypefaceOrDefault();
89 
90         {
91             SkDynamicMemoryWStream wstream;
92             typeface->serialize(&wstream, SkTypeface::SerializeBehavior::kDoIncludeData);
93             std::unique_ptr<SkStreamAsset> stream = wstream.detachAsStream();
94             sk_sp<SkTypeface> deserializedTypeface = SkTypeface::MakeDeserialize(&*stream);
95             if (!deserializedTypeface) {
96                 REPORTER_ASSERT(reporter, deserializedTypeface);
97                 return;
98             }
99 
100             SkFontDescriptor desc;
101             bool mustSerializeData = false;
102             deserializedTypeface->getFontDescriptor(&desc, &mustSerializeData);
103             REPORTER_ASSERT(reporter, mustSerializeData);
104 
105             SkBitmap deserializedBitmap;
106             create(&deserializedBitmap, origRect);
107             SkCanvas deserializedCanvas(deserializedBitmap);
108 
109             font.setTypeface(deserializedTypeface);
110             drawBG(&deserializedCanvas);
111             deserializedCanvas.drawString("A", point.fX, point.fY, font, paint);
112 
113             REPORTER_ASSERT(reporter, compare(origBitmap, origRect, deserializedBitmap, origRect));
114         }
115 
116         {
117             int ttcIndex;
118             std::unique_ptr<SkStreamAsset> fontData = typeface->openStream(&ttcIndex);
119             if (!fontData) {
120                 REPORTER_ASSERT(reporter, fontData);
121                 return;
122             }
123 
124             sk_sp<SkTypeface> streamTypeface(SkTypeface::MakeFromStream(std::move(fontData)));
125             if (!streamTypeface) {
126                 // TODO: enable assert after SkTypeface::MakeFromStream uses factories
127                 //REPORTER_ASSERT(reporter, streamTypeface);
128                 return;
129             }
130 
131             SkBitmap streamBitmap;
132             create(&streamBitmap, origRect);
133             SkCanvas streamCanvas(streamBitmap);
134 
135             SkFontDescriptor desc;
136             bool mustSerializeData = false;
137             streamTypeface->getFontDescriptor(&desc, &mustSerializeData);
138             REPORTER_ASSERT(reporter, mustSerializeData);
139 
140             font.setTypeface(streamTypeface);
141             drawBG(&streamCanvas);
142             streamCanvas.drawString("A", point.fX, point.fY, font, paint);
143 
144             REPORTER_ASSERT(reporter, compare(origBitmap, origRect, streamBitmap, origRect));
145         }
146     }
147     //Make sure the typeface is deleted and removed.
148     SkGraphics::PurgeFontCache();
149 }
150