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/SkMatrix.h" 13#include "include/core/SkPaint.h" 14#include "include/core/SkPathEffect.h" 15#include "include/core/SkPoint.h" 16#include "include/core/SkRect.h" 17#include "include/core/SkRefCnt.h" 18#include "include/core/SkScalar.h" 19#include "include/core/SkSurface.h" 20#include "include/core/SkTextBlob.h" 21#include "include/core/SkTypeface.h" 22#include "include/core/SkTypes.h" 23#include "include/effects/SkDashPathEffect.h" 24#include "tests/Test.h" 25 26#include <cmath> 27 28static const SkColor bgColor = SK_ColorWHITE; 29 30static void create(SkBitmap* bm, SkIRect bound) { 31 bm->allocN32Pixels(bound.width(), bound.height()); 32} 33 34/** Assumes that the ref draw was completely inside ref canvas -- 35 implies that everything outside is "bgColor". 36 Checks that all overlap is the same and that all non-overlap on the 37 ref is "bgColor". 38 */ 39static bool compare(const SkBitmap& ref, const SkIRect& iref, 40 const SkBitmap& test, const SkIRect& itest) 41{ 42 const int xOff = itest.fLeft - iref.fLeft; 43 const int yOff = itest.fTop - iref.fTop; 44 45 for (int y = 0; y < test.height(); ++y) { 46 for (int x = 0; x < test.width(); ++x) { 47 SkColor testColor = test.getColor(x, y); 48 int refX = x + xOff; 49 int refY = y + yOff; 50 SkColor refColor; 51 if (refX >= 0 && refX < ref.width() && 52 refY >= 0 && refY < ref.height()) 53 { 54 refColor = ref.getColor(refX, refY); 55 } else { 56 refColor = bgColor; 57 } 58 if (refColor != testColor) { 59 return false; 60 } 61 } 62 } 63 return true; 64} 65 66/** Test that drawing glyphs with empty paths is different from drawing glyphs without paths. */ 67DEF_TEST(DrawText_dashout, reporter) { 68 SkIRect size = SkIRect::MakeWH(64, 64); 69 70 SkBitmap drawTextBitmap; 71 create(&drawTextBitmap, size); 72 SkCanvas drawTextCanvas(drawTextBitmap); 73 74 SkBitmap drawDashedTextBitmap; 75 create(&drawDashedTextBitmap, size); 76 SkCanvas drawDashedTextCanvas(drawDashedTextBitmap); 77 78 SkBitmap emptyBitmap; 79 create(&emptyBitmap, size); 80 SkCanvas emptyCanvas(emptyBitmap); 81 82 SkPoint point = SkPoint::Make(25.0f, 25.0f); 83 SkFont font(nullptr, 20); 84 font.setEdging(SkFont::Edging::kSubpixelAntiAlias); 85 font.setSubpixel(true); 86 87 SkPaint paint; 88 paint.setColor(SK_ColorGRAY); 89 paint.setStyle(SkPaint::kStroke_Style); 90 91 // Draw a stroked "A" without a dash which will draw something. 92 drawTextCanvas.drawColor(SK_ColorWHITE); 93 drawTextCanvas.drawString("A", point.fX, point.fY, font, paint); 94 95 // Draw an "A" but with a dash which will never draw anything. 96 paint.setStrokeWidth(2); 97 constexpr SkScalar bigInterval = 10000; 98 static constexpr SkScalar intervals[] = { 1, bigInterval }; 99 paint.setPathEffect(SkDashPathEffect::Make(intervals, SK_ARRAY_COUNT(intervals), 2)); 100 101 drawDashedTextCanvas.drawColor(SK_ColorWHITE); 102 drawDashedTextCanvas.drawString("A", point.fX, point.fY, font, paint); 103 104 // Draw nothing. 105 emptyCanvas.drawColor(SK_ColorWHITE); 106 107 REPORTER_ASSERT(reporter, !compare(drawTextBitmap, size, emptyBitmap, size)); 108 REPORTER_ASSERT(reporter, compare(drawDashedTextBitmap, size, emptyBitmap, size)); 109} 110 111// Test drawing text at some unusual coordinates. 112// We measure success by not crashing or asserting. 113DEF_TEST(DrawText_weirdCoordinates, r) { 114 auto surface = SkSurface::MakeRasterN32Premul(10,10); 115 auto canvas = surface->getCanvas(); 116 117 SkScalar oddballs[] = { 0.0f, (float)INFINITY, (float)NAN, 34359738368.0f }; 118 119 for (auto x : oddballs) { 120 canvas->drawString("a", +x, 0.0f, SkFont(), SkPaint()); 121 canvas->drawString("a", -x, 0.0f, SkFont(), SkPaint()); 122 } 123 for (auto y : oddballs) { 124 canvas->drawString("a", 0.0f, +y, SkFont(), SkPaint()); 125 canvas->drawString("a", 0.0f, -y, SkFont(), SkPaint()); 126 } 127} 128 129// Test drawing text with some unusual matricies. 130// We measure success by not crashing or asserting. 131DEF_TEST(DrawText_weirdMatricies, r) { 132 auto surface = SkSurface::MakeRasterN32Premul(100,100); 133 auto canvas = surface->getCanvas(); 134 135 SkFont font; 136 font.setEdging(SkFont::Edging::kSubpixelAntiAlias); 137 138 struct { 139 SkScalar textSize; 140 SkScalar matrix[9]; 141 } testCases[] = { 142 // 2x2 singular 143 {10, { 0, 0, 0, 0, 0, 0, 0, 0, 1}}, 144 {10, { 0, 0, 0, 0, 1, 0, 0, 0, 1}}, 145 {10, { 0, 0, 0, 1, 0, 0, 0, 0, 1}}, 146 {10, { 0, 0, 0, 1, 1, 0, 0, 0, 1}}, 147 {10, { 0, 1, 0, 0, 1, 0, 0, 0, 1}}, 148 {10, { 1, 0, 0, 0, 0, 0, 0, 0, 1}}, 149 {10, { 1, 0, 0, 1, 0, 0, 0, 0, 1}}, 150 {10, { 1, 1, 0, 0, 0, 0, 0, 0, 1}}, 151 {10, { 1, 1, 0, 1, 1, 0, 0, 0, 1}}, 152 // See https://bugzilla.mozilla.org/show_bug.cgi?id=1305085 . 153 { 1, {10, 20, 0, 20, 40, 0, 0, 0, 1}}, 154 }; 155 156 for (const auto& testCase : testCases) { 157 font.setSize(testCase.textSize); 158 const SkScalar(&m)[9] = testCase.matrix; 159 SkMatrix mat; 160 mat.setAll(m[0], m[1], m[2], m[3], m[4], m[5], m[6], m[7], m[8]); 161 canvas->setMatrix(mat); 162 canvas->drawString("Hamburgefons", 10, 10, font, SkPaint()); 163 } 164} 165 166// This produces no glyphs, and is to check that buffers from previous draws don't get 167// reused. 168DEF_TEST(DrawText_noglyphs, r) { 169 auto surface = SkSurface::MakeRasterN32Premul(100,100); 170 auto canvas = surface->getCanvas(); 171 auto text = "Hamburgfons"; 172 { 173 // scoped to ensure blob is deleted. 174 auto blob = SkTextBlob::MakeFromText(text, strlen(text), SkFont()); 175 canvas->drawTextBlob(blob, 10, 10, SkPaint()); 176 } 177 canvas->drawString( 178 "\x0d\xf3\xf2\xf2\xe9\x0d\x0d\x0d\x05\x0d\x0d\xe3\xe3\xe3\xe3\xe3\xe3\xe3\xe3\xe3", 179 10, 20, SkFont(), SkPaint()); 180} 181