blob: 343a68c70f6e9c11a8c9fe447fa09385886ec53f [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
bungeman@google.coma5501992012-05-18 19:06:41 +00008#include "SkBitmap.h"
9#include "SkCanvas.h"
10#include "SkColor.h"
Mike Reed212e9062018-12-25 17:35:49 -050011#include "SkFont.h"
caseq26337e92014-06-30 12:14:52 -070012#include "SkFontDescriptor.h"
bungeman@google.coma5501992012-05-18 19:06:41 +000013#include "SkGraphics.h"
14#include "SkPaint.h"
Herb Derbyeb3f6742018-03-05 14:36:45 -050015#include "SkPaintPriv.h"
bungeman@google.coma5501992012-05-18 19:06:41 +000016#include "SkPoint.h"
17#include "SkRect.h"
robertphillips@google.comcb3b6152013-11-14 14:47:56 +000018#include "SkStream.h"
bungeman@google.coma5501992012-05-18 19:06:41 +000019#include "SkTypeface.h"
tfarina@chromium.orge4fafb12013-12-12 21:11:12 +000020#include "SkTypes.h"
tfarina@chromium.org8f6884a2014-01-24 20:56:26 +000021#include "Test.h"
bungeman@google.coma5501992012-05-18 19:06:41 +000022
23static const SkColor bgColor = SK_ColorWHITE;
24
commit-bot@chromium.org8ef51b92014-03-05 13:43:15 +000025static void create(SkBitmap* bm, SkIRect bound) {
26 bm->allocN32Pixels(bound.width(), bound.height());
bungeman@google.coma5501992012-05-18 19:06:41 +000027}
28
29static void drawBG(SkCanvas* canvas) {
30 canvas->drawColor(bgColor);
31}
32
33/** Assumes that the ref draw was completely inside ref canvas --
34 implies that everything outside is "bgColor".
35 Checks that all overlap is the same and that all non-overlap on the
36 ref is "bgColor".
37 */
38static bool compare(const SkBitmap& ref, const SkIRect& iref,
39 const SkBitmap& test, const SkIRect& itest)
40{
41 const int xOff = itest.fLeft - iref.fLeft;
42 const int yOff = itest.fTop - iref.fTop;
43
bungeman@google.coma5501992012-05-18 19:06:41 +000044 for (int y = 0; y < test.height(); ++y) {
45 for (int x = 0; x < test.width(); ++x) {
46 SkColor testColor = test.getColor(x, y);
47 int refX = x + xOff;
48 int refY = y + yOff;
49 SkColor refColor;
50 if (refX >= 0 && refX < ref.width() &&
51 refY >= 0 && refY < ref.height())
52 {
53 refColor = ref.getColor(refX, refY);
54 } else {
55 refColor = bgColor;
56 }
57 if (refColor != testColor) {
58 return false;
59 }
60 }
61 }
62 return true;
63}
64
tfarina@chromium.orge4fafb12013-12-12 21:11:12 +000065DEF_TEST(FontHostStream, reporter) {
bungeman@google.coma5501992012-05-18 19:06:41 +000066 {
67 SkPaint paint;
68 paint.setColor(SK_ColorGRAY);
rmistry@google.comd6176b02012-08-23 18:14:13 +000069
Mike Reed212e9062018-12-25 17:35:49 -050070 SkFont font(SkTypeface::MakeFromName("Georgia", SkFontStyle()), 30);
bungeman@google.coma5501992012-05-18 19:06:41 +000071
72 SkIRect origRect = SkIRect::MakeWH(64, 64);
73 SkBitmap origBitmap;
commit-bot@chromium.org8ef51b92014-03-05 13:43:15 +000074 create(&origBitmap, origRect);
bungeman@google.coma5501992012-05-18 19:06:41 +000075 SkCanvas origCanvas(origBitmap);
76
77 SkIRect streamRect = SkIRect::MakeWH(64, 64);
78 SkBitmap streamBitmap;
commit-bot@chromium.org8ef51b92014-03-05 13:43:15 +000079 create(&streamBitmap, streamRect);
bungeman@google.coma5501992012-05-18 19:06:41 +000080 SkCanvas streamCanvas(streamBitmap);
81
82 SkPoint point = SkPoint::Make(24, 32);
83
84 // Test: origTypeface and streamTypeface from orig data draw the same
85 drawBG(&origCanvas);
Mike Reed212e9062018-12-25 17:35:49 -050086 origCanvas.drawSimpleText("A", 1, kUTF8_SkTextEncoding, point.fX, point.fY, font, paint);
bungeman@google.coma5501992012-05-18 19:06:41 +000087
Herb Derbyeb3f6742018-03-05 14:36:45 -050088 sk_sp<SkTypeface> typeface = SkPaintPriv::RefTypefaceOrDefault(paint);
reed@google.comfed86bd2013-03-14 15:04:57 +000089 int ttcIndex;
Ben Wagner145dbcd2016-11-03 14:40:50 -040090 std::unique_ptr<SkStreamAsset> fontData(typeface->openStream(&ttcIndex));
Mike Kleincb6940b2017-11-09 13:45:10 -050091 if (!fontData) {
92 // We're using a SkTypeface that can't give us a stream.
93 // This happens with portable or system fonts. End the test now.
94 return;
95 }
96
Mike Reed271d1d92018-09-03 21:10:10 -040097 sk_sp<SkTypeface> streamTypeface(SkTypeface::MakeFromStream(std::move(fontData)));
caseq26337e92014-06-30 12:14:52 -070098
99 SkFontDescriptor desc;
100 bool isLocalStream = false;
101 streamTypeface->getFontDescriptor(&desc, &isLocalStream);
102 REPORTER_ASSERT(reporter, isLocalStream);
103
bungeman13b9c952016-05-12 10:09:30 -0700104 paint.setTypeface(streamTypeface);
bungeman@google.coma5501992012-05-18 19:06:41 +0000105 drawBG(&streamCanvas);
Mike Reed212e9062018-12-25 17:35:49 -0500106 streamCanvas.drawSimpleText("A", 1, kUTF8_SkTextEncoding, point.fX, point.fY, font, paint);
bungeman@google.coma5501992012-05-18 19:06:41 +0000107
108 REPORTER_ASSERT(reporter,
109 compare(origBitmap, origRect, streamBitmap, streamRect));
110 }
111 //Make sure the typeface is deleted and removed.
112 SkGraphics::PurgeFontCache();
113}