blob: 20001a4f8a0c4a251f333a830fe6a5770d4c0843 [file] [log] [blame]
joshualitt0a42e682015-12-10 13:20:58 -08001/*
2 * Copyright 2015 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 "GrTextUtils.h"
Brian Salomonf856fd12016-12-16 14:24:34 -05009#include "GrAtlasGlyphCache.h"
joshualitt29677982015-12-11 06:08:59 -080010#include "GrAtlasTextBlob.h"
joshualitt0a42e682015-12-10 13:20:58 -080011#include "GrBlurUtils.h"
joshualitt0d2199b2016-01-20 06:36:09 -080012#include "GrCaps.h"
joshualitt0a42e682015-12-10 13:20:58 -080013#include "GrContext.h"
Brian Osman11052242016-10-27 14:47:55 -040014#include "GrRenderTargetContext.h"
Brian Salomon6f1d36c2017-01-13 12:02:17 -050015#include "GrSurfaceContextPriv.h"
joshualitt0d2199b2016-01-20 06:36:09 -080016#include "SkDistanceFieldGen.h"
Brian Salomon6f1d36c2017-01-13 12:02:17 -050017#include "SkDrawFilter.h"
joshualitt0a42e682015-12-10 13:20:58 -080018#include "SkDrawProcs.h"
joshualitt29677982015-12-11 06:08:59 -080019#include "SkFindAndPlaceGlyph.h"
joshualitt0a42e682015-12-10 13:20:58 -080020#include "SkGlyphCache.h"
Brian Osman3b655982017-03-07 16:58:08 -050021#include "SkGr.h"
joshualitt0a42e682015-12-10 13:20:58 -080022#include "SkPaint.h"
23#include "SkRect.h"
Brian Salomon6f1d36c2017-01-13 12:02:17 -050024#include "SkTextBlobRunIterator.h"
joshualitt0a42e682015-12-10 13:20:58 -080025#include "SkTextMapStateProc.h"
26#include "SkTextToPathIter.h"
27
joshualitt0d2199b2016-01-20 06:36:09 -080028namespace {
29static const int kMinDFFontSize = 18;
30static const int kSmallDFFontSize = 32;
31static const int kSmallDFFontLimit = 32;
32static const int kMediumDFFontSize = 72;
33static const int kMediumDFFontLimit = 72;
34static const int kLargeDFFontSize = 162;
35#ifdef SK_BUILD_FOR_ANDROID
36static const int kLargeDFFontLimit = 384;
37#else
38static const int kLargeDFFontLimit = 2 * kLargeDFFontSize;
39#endif
40};
41
Brian Salomon6f1d36c2017-01-13 12:02:17 -050042bool GrTextUtils::Paint::toGrPaint(GrMaskFormat maskFormat, GrRenderTargetContext* rtc,
43 const SkMatrix& viewMatrix, GrPaint* grPaint) const {
Robert Phillips26c90e02017-03-14 14:39:29 -040044 // TODO: this is the last use of GrSurfaceContextPriv
Brian Salomon6f1d36c2017-01-13 12:02:17 -050045 GrContext* context = rtc->surfPriv().getContext();
46 if (kARGB_GrMaskFormat == maskFormat) {
47 return SkPaintToGrPaintWithPrimitiveColor(context, rtc, this->skPaint(), grPaint);
48 } else {
49 return SkPaintToGrPaint(context, rtc, this->skPaint(), viewMatrix, grPaint);
50 }
51}
52
Brian Osmanec8f8b02017-05-11 10:57:37 -040053void GrTextUtils::Paint::initFilteredColor() {
54 // This mirrors the logic in skpaint_to_grpaint_impl for handling paint colors
55 if (fDstColorSpace) {
56 GrColor4f filteredColor = SkColorToUnpremulGrColor4f(fPaint->getColor(), fDstColorSpace,
57 fColorXformFromSRGB);
58 if (fPaint->getColorFilter()) {
59 filteredColor = GrColor4f::FromSkColor4f(
60 fPaint->getColorFilter()->filterColor4f(filteredColor.toSkColor4f()));
61 }
62 fFilteredPremulColor = filteredColor.premul().toGrColor();
Brian Osmanec8f8b02017-05-11 10:57:37 -040063 } else {
64 SkColor filteredSkColor = fPaint->getColor();
65 if (fPaint->getColorFilter()) {
66 filteredSkColor = fPaint->getColorFilter()->filterColor(filteredSkColor);
67 }
68 fFilteredPremulColor = SkColorToPremulGrColor(filteredSkColor);
Brian Osmanec8f8b02017-05-11 10:57:37 -040069 }
70}
71
Brian Salomon6f1d36c2017-01-13 12:02:17 -050072bool GrTextUtils::RunPaint::modifyForRun(const SkTextBlobRunIterator& run) {
73 if (!fModifiedPaint.isValid()) {
74 fModifiedPaint.init(fOriginalPaint->skPaint());
75 fPaint = fModifiedPaint.get();
76 } else if (fFilter) {
77 // We have to reset before applying the run because the filter could have arbitrary
78 // changed the paint.
79 *fModifiedPaint.get() = fOriginalPaint->skPaint();
80 }
81 run.applyFontToPaint(fModifiedPaint.get());
82
83 if (fFilter) {
84 if (!fFilter->filter(fModifiedPaint.get(), SkDrawFilter::kText_Type)) {
85 // A false return from filter() means we should abort the current draw.
86 return false;
87 }
88 // The draw filter could have changed either the paint color or color filter.
89 this->initFilteredColor();
90 }
91 fModifiedPaint.get()->setFlags(FilterTextFlags(fProps, *fModifiedPaint.get()));
92 return true;
93}
94
95void GrTextUtils::DrawBmpText(GrAtlasTextBlob* blob, int runIndex, GrAtlasGlyphCache* fontCache,
96 const SkSurfaceProps& props, const GrTextUtils::Paint& paint,
97 uint32_t scalerContextFlags, const SkMatrix& viewMatrix,
98 const char text[], size_t byteLength, SkScalar x, SkScalar y) {
joshualitt29677982015-12-11 06:08:59 -080099 SkASSERT(byteLength == 0 || text != nullptr);
100
101 // nothing to draw
102 if (text == nullptr || byteLength == 0) {
103 return;
104 }
105
joshualitta6bf4c52016-01-19 06:59:29 -0800106 // Ensure the blob is set for bitmaptext
107 blob->setHasBitmap();
108
Brian Salomonf856fd12016-12-16 14:24:34 -0500109 GrAtlasTextStrike* currStrike = nullptr;
joshualitt29677982015-12-11 06:08:59 -0800110
Brian Salomon6f1d36c2017-01-13 12:02:17 -0500111 SkGlyphCache* cache = blob->setupCache(runIndex, props, scalerContextFlags, paint, &viewMatrix);
joshualitt29677982015-12-11 06:08:59 -0800112 SkFindAndPlaceGlyph::ProcessText(
Brian Salomon6f1d36c2017-01-13 12:02:17 -0500113 paint.skPaint().getTextEncoding(), text, byteLength,
114 {x, y}, viewMatrix, paint.skPaint().getTextAlign(),
joshualitt29677982015-12-11 06:08:59 -0800115 cache,
116 [&](const SkGlyph& glyph, SkPoint position, SkPoint rounding) {
Brian Salomon6f1d36c2017-01-13 12:02:17 -0500117 position += rounding;
118 BmpAppendGlyph(
119 blob, runIndex, fontCache, &currStrike, glyph,
120 SkScalarFloorToInt(position.fX), SkScalarFloorToInt(position.fY),
Brian Osmanec8f8b02017-05-11 10:57:37 -0400121 paint.filteredPremulColor(), cache);
joshualitt29677982015-12-11 06:08:59 -0800122 }
123 );
joshualitte76b4bb32015-12-28 07:23:58 -0800124
125 SkGlyphCache::AttachCache(cache);
joshualitt29677982015-12-11 06:08:59 -0800126}
127
Brian Salomon6f1d36c2017-01-13 12:02:17 -0500128void GrTextUtils::DrawBmpPosText(GrAtlasTextBlob* blob, int runIndex, GrAtlasGlyphCache* fontCache,
129 const SkSurfaceProps& props, const GrTextUtils::Paint& paint,
130 uint32_t scalerContextFlags, const SkMatrix& viewMatrix,
131 const char text[], size_t byteLength, const SkScalar pos[],
132 int scalarsPerPosition, const SkPoint& offset) {
joshualitt29677982015-12-11 06:08:59 -0800133 SkASSERT(byteLength == 0 || text != nullptr);
134 SkASSERT(1 == scalarsPerPosition || 2 == scalarsPerPosition);
135
136 // nothing to draw
137 if (text == nullptr || byteLength == 0) {
138 return;
139 }
140
joshualitta6bf4c52016-01-19 06:59:29 -0800141 // Ensure the blob is set for bitmaptext
142 blob->setHasBitmap();
143
Brian Salomonf856fd12016-12-16 14:24:34 -0500144 GrAtlasTextStrike* currStrike = nullptr;
joshualitt29677982015-12-11 06:08:59 -0800145
Brian Salomon6f1d36c2017-01-13 12:02:17 -0500146 SkGlyphCache* cache = blob->setupCache(runIndex, props, scalerContextFlags, paint, &viewMatrix);
joshualitt29677982015-12-11 06:08:59 -0800147
148 SkFindAndPlaceGlyph::ProcessPosText(
Brian Salomon6f1d36c2017-01-13 12:02:17 -0500149 paint.skPaint().getTextEncoding(), text, byteLength,
joshualitt29677982015-12-11 06:08:59 -0800150 offset, viewMatrix, pos, scalarsPerPosition,
Brian Salomon6f1d36c2017-01-13 12:02:17 -0500151 paint.skPaint().getTextAlign(), cache,
joshualitt29677982015-12-11 06:08:59 -0800152 [&](const SkGlyph& glyph, SkPoint position, SkPoint rounding) {
153 position += rounding;
154 BmpAppendGlyph(
155 blob, runIndex, fontCache, &currStrike, glyph,
156 SkScalarFloorToInt(position.fX), SkScalarFloorToInt(position.fY),
Brian Osmanec8f8b02017-05-11 10:57:37 -0400157 paint.filteredPremulColor(), cache);
joshualitt29677982015-12-11 06:08:59 -0800158 }
159 );
joshualitte76b4bb32015-12-28 07:23:58 -0800160
161 SkGlyphCache::AttachCache(cache);
joshualitt29677982015-12-11 06:08:59 -0800162}
163
164void GrTextUtils::BmpAppendGlyph(GrAtlasTextBlob* blob, int runIndex,
Brian Salomonf856fd12016-12-16 14:24:34 -0500165 GrAtlasGlyphCache* fontCache,
166 GrAtlasTextStrike** strike, const SkGlyph& skGlyph,
bsalomonc2878e22016-05-17 13:18:03 -0700167 int vx, int vy, GrColor color, SkGlyphCache* cache) {
joshualitt29677982015-12-11 06:08:59 -0800168 if (!*strike) {
bsalomonc2878e22016-05-17 13:18:03 -0700169 *strike = fontCache->getStrike(cache);
joshualitt29677982015-12-11 06:08:59 -0800170 }
171
172 GrGlyph::PackedID id = GrGlyph::Pack(skGlyph.getGlyphID(),
173 skGlyph.getSubXFixed(),
174 skGlyph.getSubYFixed(),
175 GrGlyph::kCoverage_MaskStyle);
bsalomonc2878e22016-05-17 13:18:03 -0700176 GrGlyph* glyph = (*strike)->getGlyph(skGlyph, id, cache);
joshualitt29677982015-12-11 06:08:59 -0800177 if (!glyph) {
178 return;
179 }
180
181 int x = vx + glyph->fBounds.fLeft;
182 int y = vy + glyph->fBounds.fTop;
183
184 // keep them as ints until we've done the clip-test
185 int width = glyph->fBounds.width();
186 int height = glyph->fBounds.height();
187
188 SkRect r;
189 r.fLeft = SkIntToScalar(x);
190 r.fTop = SkIntToScalar(y);
191 r.fRight = r.fLeft + SkIntToScalar(width);
192 r.fBottom = r.fTop + SkIntToScalar(height);
193
bsalomonc2878e22016-05-17 13:18:03 -0700194 blob->appendGlyph(runIndex, r, color, *strike, glyph, cache, skGlyph,
Jim Van Verth08576e62016-11-16 10:15:23 -0500195 SkIntToScalar(vx), SkIntToScalar(vy), 1.0f, true);
joshualitt29677982015-12-11 06:08:59 -0800196}
197
joshualitt0d2199b2016-01-20 06:36:09 -0800198bool GrTextUtils::CanDrawAsDistanceFields(const SkPaint& skPaint, const SkMatrix& viewMatrix,
199 const SkSurfaceProps& props, const GrShaderCaps& caps) {
200 // TODO: support perspective (need getMaxScale replacement)
201 if (viewMatrix.hasPerspective()) {
202 return false;
203 }
204
205 SkScalar maxScale = viewMatrix.getMaxScale();
Brian Salomon6f1d36c2017-01-13 12:02:17 -0500206 SkScalar scaledTextSize = maxScale * skPaint.getTextSize();
joshualitt0d2199b2016-01-20 06:36:09 -0800207 // Hinted text looks far better at small resolutions
208 // Scaling up beyond 2x yields undesireable artifacts
209 if (scaledTextSize < kMinDFFontSize ||
210 scaledTextSize > kLargeDFFontLimit) {
211 return false;
212 }
213
214 bool useDFT = props.isUseDeviceIndependentFonts();
215#if SK_FORCE_DISTANCE_FIELD_TEXT
216 useDFT = true;
217#endif
218
219 if (!useDFT && scaledTextSize < kLargeDFFontSize) {
220 return false;
221 }
222
223 // rasterizers and mask filters modify alpha, which doesn't
224 // translate well to distance
225 if (skPaint.getRasterizer() || skPaint.getMaskFilter() || !caps.shaderDerivativeSupport()) {
226 return false;
227 }
228
229 // TODO: add some stroking support
230 if (skPaint.getStyle() != SkPaint::kFill_Style) {
231 return false;
232 }
233
234 return true;
235}
236
237void GrTextUtils::InitDistanceFieldPaint(GrAtlasTextBlob* blob,
238 SkPaint* skPaint,
239 SkScalar* textRatio,
240 const SkMatrix& viewMatrix) {
241 // getMaxScale doesn't support perspective, so neither do we at the moment
242 SkASSERT(!viewMatrix.hasPerspective());
243 SkScalar maxScale = viewMatrix.getMaxScale();
244 SkScalar textSize = skPaint->getTextSize();
245 SkScalar scaledTextSize = textSize;
246 // if we have non-unity scale, we need to choose our base text size
247 // based on the SkPaint's text size multiplied by the max scale factor
248 // TODO: do we need to do this if we're scaling down (i.e. maxScale < 1)?
249 if (maxScale > 0 && !SkScalarNearlyEqual(maxScale, SK_Scalar1)) {
250 scaledTextSize *= maxScale;
251 }
252
253 // We have three sizes of distance field text, and within each size 'bucket' there is a floor
254 // and ceiling. A scale outside of this range would require regenerating the distance fields
255 SkScalar dfMaskScaleFloor;
256 SkScalar dfMaskScaleCeil;
257 if (scaledTextSize <= kSmallDFFontLimit) {
258 dfMaskScaleFloor = kMinDFFontSize;
259 dfMaskScaleCeil = kSmallDFFontLimit;
260 *textRatio = textSize / kSmallDFFontSize;
261 skPaint->setTextSize(SkIntToScalar(kSmallDFFontSize));
262 } else if (scaledTextSize <= kMediumDFFontLimit) {
263 dfMaskScaleFloor = kSmallDFFontLimit;
264 dfMaskScaleCeil = kMediumDFFontLimit;
265 *textRatio = textSize / kMediumDFFontSize;
266 skPaint->setTextSize(SkIntToScalar(kMediumDFFontSize));
267 } else {
268 dfMaskScaleFloor = kMediumDFFontLimit;
269 dfMaskScaleCeil = kLargeDFFontLimit;
270 *textRatio = textSize / kLargeDFFontSize;
271 skPaint->setTextSize(SkIntToScalar(kLargeDFFontSize));
272 }
273
274 // Because there can be multiple runs in the blob, we want the overall maxMinScale, and
275 // minMaxScale to make regeneration decisions. Specifically, we want the maximum minimum scale
276 // we can tolerate before we'd drop to a lower mip size, and the minimum maximum scale we can
277 // tolerate before we'd have to move to a large mip size. When we actually test these values
278 // we look at the delta in scale between the new viewmatrix and the old viewmatrix, and test
279 // against these values to decide if we can reuse or not(ie, will a given scale change our mip
280 // level)
281 SkASSERT(dfMaskScaleFloor <= scaledTextSize && scaledTextSize <= dfMaskScaleCeil);
joshualitt323c2eb2016-01-20 06:48:47 -0800282 blob->setMinAndMaxScale(dfMaskScaleFloor / scaledTextSize, dfMaskScaleCeil / scaledTextSize);
joshualitt0d2199b2016-01-20 06:36:09 -0800283
284 skPaint->setLCDRenderText(false);
285 skPaint->setAutohinted(false);
286 skPaint->setHinting(SkPaint::kNormal_Hinting);
287 skPaint->setSubpixelText(true);
288}
289
290void GrTextUtils::DrawDFText(GrAtlasTextBlob* blob, int runIndex,
Brian Salomonf856fd12016-12-16 14:24:34 -0500291 GrAtlasGlyphCache* fontCache, const SkSurfaceProps& props,
Brian Salomon6f1d36c2017-01-13 12:02:17 -0500292 const GrTextUtils::Paint& paint, uint32_t scalerContextFlags,
joshualitt0d2199b2016-01-20 06:36:09 -0800293 const SkMatrix& viewMatrix,
294 const char text[], size_t byteLength,
295 SkScalar x, SkScalar y) {
296 SkASSERT(byteLength == 0 || text != nullptr);
297
298 // nothing to draw
299 if (text == nullptr || byteLength == 0) {
300 return;
301 }
302
Brian Salomon6f1d36c2017-01-13 12:02:17 -0500303 const SkPaint& skPaint = paint.skPaint();
robertphillipse34f17d2016-07-19 07:59:22 -0700304 SkPaint::GlyphCacheProc glyphCacheProc = SkPaint::GetGlyphCacheProc(skPaint.getTextEncoding(),
305 skPaint.isDevKernText(),
306 true);
joshualitt0d2199b2016-01-20 06:36:09 -0800307 SkAutoDescriptor desc;
reeda9322c22016-04-12 06:47:05 -0700308 SkScalerContextEffects effects;
brianosman32f77822016-04-07 06:25:45 -0700309 // We apply the fake-gamma by altering the distance in the shader, so we ignore the
brianosmana1e8f8d2016-04-08 06:47:54 -0700310 // passed-in scaler context flags. (It's only used when we fall-back to bitmap text).
reeda9322c22016-04-12 06:47:05 -0700311 skPaint.getScalerContextDescriptor(&effects, &desc, props, SkPaint::kNone_ScalerContextFlags,
312 nullptr);
313 SkGlyphCache* origPaintCache = SkGlyphCache::DetachCache(skPaint.getTypeface(), effects,
joshualitt0d2199b2016-01-20 06:36:09 -0800314 desc.getDesc());
315
316 SkTArray<SkScalar> positions;
317
318 const char* textPtr = text;
benjaminwagner6b3eacb2016-03-24 19:07:58 -0700319 SkScalar stopX = 0;
320 SkScalar stopY = 0;
321 SkScalar origin = 0;
joshualitt0d2199b2016-01-20 06:36:09 -0800322 switch (skPaint.getTextAlign()) {
benjaminwagner6b3eacb2016-03-24 19:07:58 -0700323 case SkPaint::kRight_Align: origin = SK_Scalar1; break;
324 case SkPaint::kCenter_Align: origin = SK_ScalarHalf; break;
joshualitt0d2199b2016-01-20 06:36:09 -0800325 case SkPaint::kLeft_Align: origin = 0; break;
326 }
327
328 SkAutoKern autokern;
329 const char* stop = text + byteLength;
330 while (textPtr < stop) {
331 // don't need x, y here, since all subpixel variants will have the
332 // same advance
benjaminwagnerd936f632016-02-23 10:44:31 -0800333 const SkGlyph& glyph = glyphCacheProc(origPaintCache, &textPtr);
joshualitt0d2199b2016-01-20 06:36:09 -0800334
benjaminwagner6b3eacb2016-03-24 19:07:58 -0700335 SkScalar width = SkFloatToScalar(glyph.fAdvanceX) + autokern.adjust(glyph);
336 positions.push_back(stopX + origin * width);
joshualitt0d2199b2016-01-20 06:36:09 -0800337
benjaminwagner6b3eacb2016-03-24 19:07:58 -0700338 SkScalar height = SkFloatToScalar(glyph.fAdvanceY);
339 positions.push_back(stopY + origin * height);
joshualitt0d2199b2016-01-20 06:36:09 -0800340
341 stopX += width;
342 stopY += height;
343 }
344 SkASSERT(textPtr == stop);
345
346 SkGlyphCache::AttachCache(origPaintCache);
347
348 // now adjust starting point depending on alignment
benjaminwagner6b3eacb2016-03-24 19:07:58 -0700349 SkScalar alignX = stopX;
350 SkScalar alignY = stopY;
joshualitt0d2199b2016-01-20 06:36:09 -0800351 if (skPaint.getTextAlign() == SkPaint::kCenter_Align) {
352 alignX = SkScalarHalf(alignX);
353 alignY = SkScalarHalf(alignY);
354 } else if (skPaint.getTextAlign() == SkPaint::kLeft_Align) {
355 alignX = 0;
356 alignY = 0;
357 }
358 x -= alignX;
359 y -= alignY;
360 SkPoint offset = SkPoint::Make(x, y);
361
Brian Salomon6f1d36c2017-01-13 12:02:17 -0500362 DrawDFPosText(blob, runIndex, fontCache, props, paint, scalerContextFlags, viewMatrix, text,
363 byteLength, positions.begin(), 2, offset);
joshualitt0d2199b2016-01-20 06:36:09 -0800364}
365
Brian Salomon6f1d36c2017-01-13 12:02:17 -0500366void GrTextUtils::DrawDFPosText(GrAtlasTextBlob* blob, int runIndex, GrAtlasGlyphCache* fontCache,
367 const SkSurfaceProps& props, const GrTextUtils::Paint& paint,
368 uint32_t scalerContextFlags, const SkMatrix& viewMatrix,
369 const char text[], size_t byteLength, const SkScalar pos[],
370 int scalarsPerPosition, const SkPoint& offset) {
joshualitt0d2199b2016-01-20 06:36:09 -0800371 SkASSERT(byteLength == 0 || text != nullptr);
372 SkASSERT(1 == scalarsPerPosition || 2 == scalarsPerPosition);
373
374 // nothing to draw
375 if (text == nullptr || byteLength == 0) {
376 return;
377 }
378
379 SkTDArray<char> fallbackTxt;
380 SkTDArray<SkScalar> fallbackPos;
381
382 // Setup distance field paint and text ratio
383 SkScalar textRatio;
Brian Salomon6f1d36c2017-01-13 12:02:17 -0500384 SkPaint dfPaint(paint);
joshualitt0d2199b2016-01-20 06:36:09 -0800385 GrTextUtils::InitDistanceFieldPaint(blob, &dfPaint, &textRatio, viewMatrix);
386 blob->setHasDistanceField();
Brian Salomon6f1d36c2017-01-13 12:02:17 -0500387 blob->setSubRunHasDistanceFields(runIndex, paint.skPaint().isLCDRenderText());
joshualitt0d2199b2016-01-20 06:36:09 -0800388
Brian Salomonf856fd12016-12-16 14:24:34 -0500389 GrAtlasTextStrike* currStrike = nullptr;
joshualitt0d2199b2016-01-20 06:36:09 -0800390
brianosman32f77822016-04-07 06:25:45 -0700391 // We apply the fake-gamma by altering the distance in the shader, so we ignore the
brianosmana1e8f8d2016-04-08 06:47:54 -0700392 // passed-in scaler context flags. (It's only used when we fall-back to bitmap text).
393 SkGlyphCache* cache = blob->setupCache(runIndex, props, SkPaint::kNone_ScalerContextFlags,
bungemanf6d1e602016-02-22 13:20:28 -0800394 dfPaint, nullptr);
robertphillipse34f17d2016-07-19 07:59:22 -0700395 SkPaint::GlyphCacheProc glyphCacheProc = SkPaint::GetGlyphCacheProc(dfPaint.getTextEncoding(),
396 dfPaint.isDevKernText(),
397 true);
joshualitt0d2199b2016-01-20 06:36:09 -0800398
399 const char* stop = text + byteLength;
400
401 if (SkPaint::kLeft_Align == dfPaint.getTextAlign()) {
402 while (text < stop) {
403 const char* lastText = text;
404 // the last 2 parameters are ignored
benjaminwagnerd936f632016-02-23 10:44:31 -0800405 const SkGlyph& glyph = glyphCacheProc(cache, &text);
joshualitt0d2199b2016-01-20 06:36:09 -0800406
407 if (glyph.fWidth) {
408 SkScalar x = offset.x() + pos[0];
409 SkScalar y = offset.y() + (2 == scalarsPerPosition ? pos[1] : 0);
410
Brian Salomon6f1d36c2017-01-13 12:02:17 -0500411 if (!DfAppendGlyph(blob, runIndex, fontCache, &currStrike, glyph, x, y,
Brian Osmanec8f8b02017-05-11 10:57:37 -0400412 paint.filteredPremulColor(), cache, textRatio, viewMatrix)) {
joshualitt0d2199b2016-01-20 06:36:09 -0800413 // couldn't append, send to fallback
414 fallbackTxt.append(SkToInt(text-lastText), lastText);
415 *fallbackPos.append() = pos[0];
416 if (2 == scalarsPerPosition) {
417 *fallbackPos.append() = pos[1];
418 }
419 }
420 }
421 pos += scalarsPerPosition;
422 }
423 } else {
424 SkScalar alignMul = SkPaint::kCenter_Align == dfPaint.getTextAlign() ? SK_ScalarHalf
425 : SK_Scalar1;
426 while (text < stop) {
427 const char* lastText = text;
428 // the last 2 parameters are ignored
benjaminwagnerd936f632016-02-23 10:44:31 -0800429 const SkGlyph& glyph = glyphCacheProc(cache, &text);
joshualitt0d2199b2016-01-20 06:36:09 -0800430
431 if (glyph.fWidth) {
432 SkScalar x = offset.x() + pos[0];
433 SkScalar y = offset.y() + (2 == scalarsPerPosition ? pos[1] : 0);
434
benjaminwagner6b3eacb2016-03-24 19:07:58 -0700435 SkScalar advanceX = SkFloatToScalar(glyph.fAdvanceX) * alignMul * textRatio;
436 SkScalar advanceY = SkFloatToScalar(glyph.fAdvanceY) * alignMul * textRatio;
joshualitt0d2199b2016-01-20 06:36:09 -0800437
Brian Salomon6f1d36c2017-01-13 12:02:17 -0500438 if (!DfAppendGlyph(blob, runIndex, fontCache, &currStrike, glyph, x - advanceX,
Brian Osmanec8f8b02017-05-11 10:57:37 -0400439 y - advanceY, paint.filteredPremulColor(), cache, textRatio,
joshualitt0d2199b2016-01-20 06:36:09 -0800440 viewMatrix)) {
441 // couldn't append, send to fallback
442 fallbackTxt.append(SkToInt(text-lastText), lastText);
443 *fallbackPos.append() = pos[0];
444 if (2 == scalarsPerPosition) {
445 *fallbackPos.append() = pos[1];
446 }
447 }
448 }
449 pos += scalarsPerPosition;
450 }
451 }
452
453 SkGlyphCache::AttachCache(cache);
454 if (fallbackTxt.count()) {
455 blob->initOverride(runIndex);
Brian Salomon6f1d36c2017-01-13 12:02:17 -0500456 GrTextUtils::DrawBmpPosText(blob, runIndex, fontCache, props, paint, scalerContextFlags,
457 viewMatrix, fallbackTxt.begin(), fallbackTxt.count(),
joshualitt0d2199b2016-01-20 06:36:09 -0800458 fallbackPos.begin(), scalarsPerPosition, offset);
459 }
460}
461
Brian Salomonf856fd12016-12-16 14:24:34 -0500462bool GrTextUtils::DfAppendGlyph(GrAtlasTextBlob* blob, int runIndex, GrAtlasGlyphCache* cache,
463 GrAtlasTextStrike** strike, const SkGlyph& skGlyph,
joshualitt0d2199b2016-01-20 06:36:09 -0800464 SkScalar sx, SkScalar sy, GrColor color,
bsalomonc2878e22016-05-17 13:18:03 -0700465 SkGlyphCache* glyphCache,
joshualitt0d2199b2016-01-20 06:36:09 -0800466 SkScalar textRatio, const SkMatrix& viewMatrix) {
467 if (!*strike) {
bsalomonc2878e22016-05-17 13:18:03 -0700468 *strike = cache->getStrike(glyphCache);
joshualitt0d2199b2016-01-20 06:36:09 -0800469 }
470
471 GrGlyph::PackedID id = GrGlyph::Pack(skGlyph.getGlyphID(),
472 skGlyph.getSubXFixed(),
473 skGlyph.getSubYFixed(),
474 GrGlyph::kDistance_MaskStyle);
bsalomonc2878e22016-05-17 13:18:03 -0700475 GrGlyph* glyph = (*strike)->getGlyph(skGlyph, id, glyphCache);
joshualitt0d2199b2016-01-20 06:36:09 -0800476 if (!glyph) {
477 return true;
478 }
479
480 // fallback to color glyph support
481 if (kA8_GrMaskFormat != glyph->fMaskFormat) {
482 return false;
483 }
484
485 SkScalar dx = SkIntToScalar(glyph->fBounds.fLeft + SK_DistanceFieldInset);
486 SkScalar dy = SkIntToScalar(glyph->fBounds.fTop + SK_DistanceFieldInset);
487 SkScalar width = SkIntToScalar(glyph->fBounds.width() - 2 * SK_DistanceFieldInset);
488 SkScalar height = SkIntToScalar(glyph->fBounds.height() - 2 * SK_DistanceFieldInset);
489
490 SkScalar scale = textRatio;
491 dx *= scale;
492 dy *= scale;
493 width *= scale;
494 height *= scale;
495 sx += dx;
496 sy += dy;
497 SkRect glyphRect = SkRect::MakeXYWH(sx, sy, width, height);
498
bsalomonc2878e22016-05-17 13:18:03 -0700499 blob->appendGlyph(runIndex, glyphRect, color, *strike, glyph, glyphCache, skGlyph,
Jim Van Verth08576e62016-11-16 10:15:23 -0500500 sx - dx, sy - dy, scale, false);
joshualitt0d2199b2016-01-20 06:36:09 -0800501 return true;
502}
503
Brian Salomon6f1d36c2017-01-13 12:02:17 -0500504void GrTextUtils::DrawTextAsPath(GrContext* context, GrRenderTargetContext* rtc, const GrClip& clip,
505 const SkPaint& paint, const SkMatrix& viewMatrix,
joshualitt0a42e682015-12-10 13:20:58 -0800506 const char text[], size_t byteLength, SkScalar x, SkScalar y,
507 const SkIRect& clipBounds) {
Brian Salomon6f1d36c2017-01-13 12:02:17 -0500508 SkTextToPathIter iter(text, byteLength, paint, true);
joshualitt0a42e682015-12-10 13:20:58 -0800509
510 SkMatrix matrix;
511 matrix.setScale(iter.getPathScale(), iter.getPathScale());
512 matrix.postTranslate(x, y);
513
514 const SkPath* iterPath;
515 SkScalar xpos, prevXPos = 0;
516
517 while (iter.next(&iterPath, &xpos)) {
518 matrix.postTranslate(xpos - prevXPos, 0);
519 if (iterPath) {
520 const SkPaint& pnt = iter.getPaint();
Brian Osman11052242016-10-27 14:47:55 -0400521 GrBlurUtils::drawPathWithMaskFilter(context, rtc, clip, *iterPath,
joshualitt0a42e682015-12-10 13:20:58 -0800522 pnt, viewMatrix, &matrix, clipBounds, false);
523 }
524 prevXPos = xpos;
525 }
526}
527
528void GrTextUtils::DrawPosTextAsPath(GrContext* context,
Brian Osman11052242016-10-27 14:47:55 -0400529 GrRenderTargetContext* rtc,
joshualitt0a42e682015-12-10 13:20:58 -0800530 const SkSurfaceProps& props,
531 const GrClip& clip,
532 const SkPaint& origPaint, const SkMatrix& viewMatrix,
533 const char text[], size_t byteLength,
534 const SkScalar pos[], int scalarsPerPosition,
535 const SkPoint& offset, const SkIRect& clipBounds) {
536 // setup our std paint, in hopes of getting hits in the cache
537 SkPaint paint(origPaint);
538 SkScalar matrixScale = paint.setupForAsPaths();
539
540 SkMatrix matrix;
541 matrix.setScale(matrixScale, matrixScale);
542
543 // Temporarily jam in kFill, so we only ever ask for the raw outline from the cache.
544 paint.setStyle(SkPaint::kFill_Style);
545 paint.setPathEffect(nullptr);
546
robertphillipse34f17d2016-07-19 07:59:22 -0700547 SkPaint::GlyphCacheProc glyphCacheProc = SkPaint::GetGlyphCacheProc(paint.getTextEncoding(),
548 paint.isDevKernText(),
549 true);
benjaminwagnerd936f632016-02-23 10:44:31 -0800550 SkAutoGlyphCache autoCache(paint, &props, nullptr);
551 SkGlyphCache* cache = autoCache.getCache();
joshualitt0a42e682015-12-10 13:20:58 -0800552
553 const char* stop = text + byteLength;
554 SkTextAlignProc alignProc(paint.getTextAlign());
555 SkTextMapStateProc tmsProc(SkMatrix::I(), offset, scalarsPerPosition);
556
557 // Now restore the original settings, so we "draw" with whatever style/stroking.
558 paint.setStyle(origPaint.getStyle());
Mike Reed693fdbd2017-01-12 10:13:40 -0500559 paint.setPathEffect(origPaint.refPathEffect());
joshualitt0a42e682015-12-10 13:20:58 -0800560
561 while (text < stop) {
benjaminwagnerd936f632016-02-23 10:44:31 -0800562 const SkGlyph& glyph = glyphCacheProc(cache, &text);
joshualitt0a42e682015-12-10 13:20:58 -0800563 if (glyph.fWidth) {
564 const SkPath* path = cache->findPath(glyph);
565 if (path) {
566 SkPoint tmsLoc;
567 tmsProc(pos, &tmsLoc);
568 SkPoint loc;
569 alignProc(tmsLoc, glyph, &loc);
570
571 matrix[SkMatrix::kMTransX] = loc.fX;
572 matrix[SkMatrix::kMTransY] = loc.fY;
Brian Osman11052242016-10-27 14:47:55 -0400573 GrBlurUtils::drawPathWithMaskFilter(context, rtc, clip, *path, paint,
joshualitt0a42e682015-12-10 13:20:58 -0800574 viewMatrix, &matrix, clipBounds, false);
575 }
576 }
577 pos += scalarsPerPosition;
578 }
579}
joshualitt8e84a1e2016-02-16 11:09:25 -0800580
581bool GrTextUtils::ShouldDisableLCD(const SkPaint& paint) {
reed374772b2016-10-05 17:33:02 -0700582 return paint.getMaskFilter() ||
joshualitt8e84a1e2016-02-16 11:09:25 -0800583 paint.getRasterizer() ||
584 paint.getPathEffect() ||
585 paint.isFakeBoldText() ||
586 paint.getStyle() != SkPaint::kFill_Style;
587}
588
589uint32_t GrTextUtils::FilterTextFlags(const SkSurfaceProps& surfaceProps, const SkPaint& paint) {
590 uint32_t flags = paint.getFlags();
591
592 if (!paint.isLCDRenderText() || !paint.isAntiAlias()) {
593 return flags;
594 }
595
596 if (kUnknown_SkPixelGeometry == surfaceProps.pixelGeometry() || ShouldDisableLCD(paint)) {
597 flags &= ~SkPaint::kLCDRenderText_Flag;
598 flags |= SkPaint::kGenA8FromLCD_Flag;
599 }
600
601 return flags;
602}