1cb93a386Sopenharmony_ci/* 2cb93a386Sopenharmony_ci * Copyright 2011 Google Inc. 3cb93a386Sopenharmony_ci * 4cb93a386Sopenharmony_ci * Use of this source code is governed by a BSD-style license that can be 5cb93a386Sopenharmony_ci * found in the LICENSE file. 6cb93a386Sopenharmony_ci */ 7cb93a386Sopenharmony_ci 8cb93a386Sopenharmony_ci#include "include/core/SkBitmap.h" 9cb93a386Sopenharmony_ci#include "include/core/SkCanvas.h" 10cb93a386Sopenharmony_ci#include "include/core/SkColor.h" 11cb93a386Sopenharmony_ci#include "include/core/SkFont.h" 12cb93a386Sopenharmony_ci#include "include/core/SkGraphics.h" 13cb93a386Sopenharmony_ci#include "include/core/SkPaint.h" 14cb93a386Sopenharmony_ci#include "include/core/SkPoint.h" 15cb93a386Sopenharmony_ci#include "include/core/SkRect.h" 16cb93a386Sopenharmony_ci#include "include/core/SkStream.h" 17cb93a386Sopenharmony_ci#include "include/core/SkTypeface.h" 18cb93a386Sopenharmony_ci#include "include/core/SkTypes.h" 19cb93a386Sopenharmony_ci#include "src/core/SkFontDescriptor.h" 20cb93a386Sopenharmony_ci#include "src/core/SkFontPriv.h" 21cb93a386Sopenharmony_ci#include "tests/Test.h" 22cb93a386Sopenharmony_ci 23cb93a386Sopenharmony_cistatic const SkColor bgColor = SK_ColorWHITE; 24cb93a386Sopenharmony_ci 25cb93a386Sopenharmony_cistatic void create(SkBitmap* bm, SkIRect bound) { 26cb93a386Sopenharmony_ci bm->allocN32Pixels(bound.width(), bound.height()); 27cb93a386Sopenharmony_ci} 28cb93a386Sopenharmony_ci 29cb93a386Sopenharmony_cistatic void drawBG(SkCanvas* canvas) { 30cb93a386Sopenharmony_ci canvas->drawColor(bgColor); 31cb93a386Sopenharmony_ci} 32cb93a386Sopenharmony_ci 33cb93a386Sopenharmony_ci/** Assumes that the ref draw was completely inside ref canvas -- 34cb93a386Sopenharmony_ci implies that everything outside is "bgColor". 35cb93a386Sopenharmony_ci Checks that all overlap is the same and that all non-overlap on the 36cb93a386Sopenharmony_ci ref is "bgColor". 37cb93a386Sopenharmony_ci */ 38cb93a386Sopenharmony_cistatic bool compare(const SkBitmap& ref, const SkIRect& iref, 39cb93a386Sopenharmony_ci const SkBitmap& test, const SkIRect& itest) 40cb93a386Sopenharmony_ci{ 41cb93a386Sopenharmony_ci const int xOff = itest.fLeft - iref.fLeft; 42cb93a386Sopenharmony_ci const int yOff = itest.fTop - iref.fTop; 43cb93a386Sopenharmony_ci 44cb93a386Sopenharmony_ci for (int y = 0; y < test.height(); ++y) { 45cb93a386Sopenharmony_ci for (int x = 0; x < test.width(); ++x) { 46cb93a386Sopenharmony_ci SkColor testColor = test.getColor(x, y); 47cb93a386Sopenharmony_ci int refX = x + xOff; 48cb93a386Sopenharmony_ci int refY = y + yOff; 49cb93a386Sopenharmony_ci SkColor refColor; 50cb93a386Sopenharmony_ci if (refX >= 0 && refX < ref.width() && 51cb93a386Sopenharmony_ci refY >= 0 && refY < ref.height()) 52cb93a386Sopenharmony_ci { 53cb93a386Sopenharmony_ci refColor = ref.getColor(refX, refY); 54cb93a386Sopenharmony_ci } else { 55cb93a386Sopenharmony_ci refColor = bgColor; 56cb93a386Sopenharmony_ci } 57cb93a386Sopenharmony_ci if (refColor != testColor) { 58cb93a386Sopenharmony_ci return false; 59cb93a386Sopenharmony_ci } 60cb93a386Sopenharmony_ci } 61cb93a386Sopenharmony_ci } 62cb93a386Sopenharmony_ci return true; 63cb93a386Sopenharmony_ci} 64cb93a386Sopenharmony_ci 65cb93a386Sopenharmony_ciDEF_TEST(FontHostStream, reporter) { 66cb93a386Sopenharmony_ci { 67cb93a386Sopenharmony_ci SkPaint paint; 68cb93a386Sopenharmony_ci paint.setColor(SK_ColorGRAY); 69cb93a386Sopenharmony_ci 70cb93a386Sopenharmony_ci SkFont font(SkTypeface::MakeFromName("Georgia", SkFontStyle()), 30); 71cb93a386Sopenharmony_ci font.setEdging(SkFont::Edging::kAlias); 72cb93a386Sopenharmony_ci 73cb93a386Sopenharmony_ci SkIRect origRect = SkIRect::MakeWH(64, 64); 74cb93a386Sopenharmony_ci SkBitmap origBitmap; 75cb93a386Sopenharmony_ci create(&origBitmap, origRect); 76cb93a386Sopenharmony_ci SkCanvas origCanvas(origBitmap); 77cb93a386Sopenharmony_ci 78cb93a386Sopenharmony_ci SkIRect streamRect = SkIRect::MakeWH(64, 64); 79cb93a386Sopenharmony_ci SkBitmap streamBitmap; 80cb93a386Sopenharmony_ci create(&streamBitmap, streamRect); 81cb93a386Sopenharmony_ci SkCanvas streamCanvas(streamBitmap); 82cb93a386Sopenharmony_ci 83cb93a386Sopenharmony_ci SkPoint point = SkPoint::Make(24, 32); 84cb93a386Sopenharmony_ci 85cb93a386Sopenharmony_ci // Test: origTypeface and streamTypeface from orig data draw the same 86cb93a386Sopenharmony_ci drawBG(&origCanvas); 87cb93a386Sopenharmony_ci origCanvas.drawString("A", point.fX, point.fY, font, paint); 88cb93a386Sopenharmony_ci 89cb93a386Sopenharmony_ci sk_sp<SkTypeface> typeface = font.refTypefaceOrDefault(); 90cb93a386Sopenharmony_ci int ttcIndex; 91cb93a386Sopenharmony_ci std::unique_ptr<SkStreamAsset> fontData = typeface->openStream(&ttcIndex); 92cb93a386Sopenharmony_ci if (!fontData) { 93cb93a386Sopenharmony_ci // We're using a SkTypeface that can't give us a stream. 94cb93a386Sopenharmony_ci // This happens with portable or system fonts. End the test now. 95cb93a386Sopenharmony_ci return; 96cb93a386Sopenharmony_ci } 97cb93a386Sopenharmony_ci 98cb93a386Sopenharmony_ci sk_sp<SkTypeface> streamTypeface(SkTypeface::MakeFromStream(std::move(fontData))); 99cb93a386Sopenharmony_ci 100cb93a386Sopenharmony_ci SkFontDescriptor desc; 101cb93a386Sopenharmony_ci bool isLocalStream = false; 102cb93a386Sopenharmony_ci streamTypeface->getFontDescriptor(&desc, &isLocalStream); 103cb93a386Sopenharmony_ci REPORTER_ASSERT(reporter, !isLocalStream); 104cb93a386Sopenharmony_ci 105cb93a386Sopenharmony_ci font.setTypeface(streamTypeface); 106cb93a386Sopenharmony_ci drawBG(&streamCanvas); 107cb93a386Sopenharmony_ci streamCanvas.drawString("A", point.fX, point.fY, font, paint); 108cb93a386Sopenharmony_ci 109cb93a386Sopenharmony_ci REPORTER_ASSERT(reporter, 110cb93a386Sopenharmony_ci compare(origBitmap, origRect, streamBitmap, streamRect)); 111cb93a386Sopenharmony_ci } 112cb93a386Sopenharmony_ci //Make sure the typeface is deleted and removed. 113cb93a386Sopenharmony_ci SkGraphics::PurgeFontCache(); 114cb93a386Sopenharmony_ci} 115