kkinnunen | c6cb56f | 2014-06-24 00:12:27 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2014 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 "GrStencilAndCoverTextContext.h" |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 9 | #include "GrAtlasTextContext.h" |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 10 | #include "GrDrawContext.h" |
kkinnunen | c6cb56f | 2014-06-24 00:12:27 -0700 | [diff] [blame] | 11 | #include "GrDrawTarget.h" |
kkinnunen | c6cb56f | 2014-06-24 00:12:27 -0700 | [diff] [blame] | 12 | #include "GrPath.h" |
cdalton | b85a0aa | 2014-07-21 15:32:44 -0700 | [diff] [blame] | 13 | #include "GrPathRange.h" |
bsalomon | d309e7a | 2015-04-30 14:18:54 -0700 | [diff] [blame] | 14 | #include "GrResourceProvider.h" |
jvanverth | aab626c | 2014-10-16 08:04:39 -0700 | [diff] [blame] | 15 | #include "SkAutoKern.h" |
kkinnunen | c6cb56f | 2014-06-24 00:12:27 -0700 | [diff] [blame] | 16 | #include "SkDraw.h" |
| 17 | #include "SkDrawProcs.h" |
| 18 | #include "SkGlyphCache.h" |
| 19 | #include "SkGpuDevice.h" |
| 20 | #include "SkPath.h" |
| 21 | #include "SkTextMapStateProc.h" |
cdalton | 855d83f | 2014-09-18 13:51:53 -0700 | [diff] [blame] | 22 | #include "SkTextFormatParams.h" |
kkinnunen | c6cb56f | 2014-06-24 00:12:27 -0700 | [diff] [blame] | 23 | |
bsalomon | 1fcc01c | 2015-09-09 09:48:06 -0700 | [diff] [blame] | 24 | #include "batches/GrDrawPathBatch.h" |
| 25 | |
joshualitt | 6e8cd96 | 2015-03-20 10:30:14 -0700 | [diff] [blame] | 26 | GrStencilAndCoverTextContext::GrStencilAndCoverTextContext(GrContext* context, |
robertphillips | fcf7829 | 2015-06-19 11:49:52 -0700 | [diff] [blame] | 27 | const SkSurfaceProps& surfaceProps) |
cdalton | 3bd909a | 2015-10-05 14:57:20 -0700 | [diff] [blame^] | 28 | : INHERITED(context, surfaceProps) { |
kkinnunen | c6cb56f | 2014-06-24 00:12:27 -0700 | [diff] [blame] | 29 | } |
| 30 | |
joshualitt | 6e8cd96 | 2015-03-20 10:30:14 -0700 | [diff] [blame] | 31 | GrStencilAndCoverTextContext* |
robertphillips | f6703fa | 2015-09-01 05:36:47 -0700 | [diff] [blame] | 32 | GrStencilAndCoverTextContext::Create(GrContext* context, const SkSurfaceProps& surfaceProps) { |
| 33 | GrStencilAndCoverTextContext* textContext = |
| 34 | new GrStencilAndCoverTextContext(context, surfaceProps); |
| 35 | textContext->fFallbackTextContext = GrAtlasTextContext::Create(context, surfaceProps); |
jvanverth | 8c27a18 | 2014-10-14 08:45:50 -0700 | [diff] [blame] | 36 | |
| 37 | return textContext; |
| 38 | } |
| 39 | |
kkinnunen | c6cb56f | 2014-06-24 00:12:27 -0700 | [diff] [blame] | 40 | GrStencilAndCoverTextContext::~GrStencilAndCoverTextContext() { |
| 41 | } |
| 42 | |
cdalton | e68f736 | 2015-03-25 14:02:37 -0700 | [diff] [blame] | 43 | bool GrStencilAndCoverTextContext::canDraw(const GrRenderTarget* rt, |
| 44 | const GrClip& clip, |
| 45 | const GrPaint& paint, |
| 46 | const SkPaint& skPaint, |
| 47 | const SkMatrix& viewMatrix) { |
| 48 | if (skPaint.getRasterizer()) { |
jvanverth | 0fedb19 | 2014-10-08 09:07:27 -0700 | [diff] [blame] | 49 | return false; |
| 50 | } |
cdalton | e68f736 | 2015-03-25 14:02:37 -0700 | [diff] [blame] | 51 | if (skPaint.getMaskFilter()) { |
jvanverth | 0fedb19 | 2014-10-08 09:07:27 -0700 | [diff] [blame] | 52 | return false; |
| 53 | } |
kkinnunen | 50b58e6 | 2015-05-18 23:02:07 -0700 | [diff] [blame] | 54 | if (SkPathEffect* pe = skPaint.getPathEffect()) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 55 | if (pe->asADash(nullptr) != SkPathEffect::kDash_DashType) { |
kkinnunen | 50b58e6 | 2015-05-18 23:02:07 -0700 | [diff] [blame] | 56 | return false; |
| 57 | } |
jvanverth | 0fedb19 | 2014-10-08 09:07:27 -0700 | [diff] [blame] | 58 | } |
cdalton | 7d5c950 | 2015-10-03 13:28:35 -0700 | [diff] [blame] | 59 | // No hairlines. They would require new paths with customized strokes for every new draw matrix. |
| 60 | return SkPaint::kStroke_Style != skPaint.getStyle() || 0 != skPaint.getStrokeWidth(); |
jvanverth | 0fedb19 | 2014-10-08 09:07:27 -0700 | [diff] [blame] | 61 | } |
| 62 | |
robertphillips | f6703fa | 2015-09-01 05:36:47 -0700 | [diff] [blame] | 63 | void GrStencilAndCoverTextContext::onDrawText(GrDrawContext* dc, GrRenderTarget* rt, |
joshualitt | 570d2f8 | 2015-02-25 13:19:48 -0800 | [diff] [blame] | 64 | const GrClip& clip, |
joshualitt | 25d9c15 | 2015-02-18 12:29:52 -0800 | [diff] [blame] | 65 | const GrPaint& paint, |
cdalton | 20b373c | 2014-12-01 08:38:55 -0800 | [diff] [blame] | 66 | const SkPaint& skPaint, |
joshualitt | 5531d51 | 2014-12-17 15:50:11 -0800 | [diff] [blame] | 67 | const SkMatrix& viewMatrix, |
cdalton | 20b373c | 2014-12-01 08:38:55 -0800 | [diff] [blame] | 68 | const char text[], |
| 69 | size_t byteLength, |
joshualitt | 6e8cd96 | 2015-03-20 10:30:14 -0700 | [diff] [blame] | 70 | SkScalar x, SkScalar y, |
| 71 | const SkIRect& regionClipBounds) { |
cdalton | 3bd909a | 2015-10-05 14:57:20 -0700 | [diff] [blame^] | 72 | TextRun run(skPaint); |
| 73 | run.setText(text, byteLength, x, y, fContext, &fSurfaceProps); |
| 74 | run.draw(dc, rt, clip, paint, viewMatrix, regionClipBounds, fFallbackTextContext, skPaint); |
| 75 | } |
jvanverth | aab626c | 2014-10-16 08:04:39 -0700 | [diff] [blame] | 76 | |
cdalton | 3bd909a | 2015-10-05 14:57:20 -0700 | [diff] [blame^] | 77 | void GrStencilAndCoverTextContext::onDrawPosText(GrDrawContext* dc, GrRenderTarget* rt, |
| 78 | const GrClip& clip, |
| 79 | const GrPaint& paint, |
| 80 | const SkPaint& skPaint, |
| 81 | const SkMatrix& viewMatrix, |
| 82 | const char text[], |
| 83 | size_t byteLength, |
| 84 | const SkScalar pos[], |
| 85 | int scalarsPerPosition, |
| 86 | const SkPoint& offset, |
| 87 | const SkIRect& regionClipBounds) { |
| 88 | TextRun run(skPaint); |
| 89 | run.setPosText(text, byteLength, pos, scalarsPerPosition, offset, fContext, &fSurfaceProps); |
| 90 | run.draw(dc, rt, clip, paint, viewMatrix, regionClipBounds, fFallbackTextContext, skPaint); |
| 91 | } |
| 92 | |
| 93 | //////////////////////////////////////////////////////////////////////////////////////////////////// |
| 94 | |
| 95 | GrStencilAndCoverTextContext::TextRun::TextRun(const SkPaint& fontAndStroke) |
| 96 | : fStroke(fontAndStroke), |
| 97 | fFont(fontAndStroke) { |
| 98 | SkASSERT(!fStroke.isHairlineStyle()); // Hairlines are not supported. |
| 99 | |
| 100 | // Setting to "fill" ensures that no strokes get baked into font outlines. (We use the GPU path |
| 101 | // rendering API for stroking). |
| 102 | fFont.setStyle(SkPaint::kFill_Style); |
| 103 | |
| 104 | if (fFont.isFakeBoldText() && SkStrokeRec::kStroke_Style != fStroke.getStyle()) { |
| 105 | // Instead of letting fake bold get baked into the glyph outlines, do it with GPU stroke. |
| 106 | SkScalar fakeBoldScale = SkScalarInterpFunc(fFont.getTextSize(), |
| 107 | kStdFakeBoldInterpKeys, |
| 108 | kStdFakeBoldInterpValues, |
| 109 | kStdFakeBoldInterpLength); |
| 110 | SkScalar extra = SkScalarMul(fFont.getTextSize(), fakeBoldScale); |
| 111 | fStroke.setStrokeStyle(fStroke.needToApply() ? fStroke.getWidth() + extra : extra, |
| 112 | true /*strokeAndFill*/); |
| 113 | |
| 114 | fFont.setFakeBoldText(false); |
jvanverth | aab626c | 2014-10-16 08:04:39 -0700 | [diff] [blame] | 115 | } |
| 116 | |
cdalton | 3bd909a | 2015-10-05 14:57:20 -0700 | [diff] [blame^] | 117 | if (!fFont.getPathEffect() && !fStroke.isDashed()) { |
| 118 | // We can draw the glyphs from canonically sized paths. |
| 119 | fTextRatio = fFont.getTextSize() / SkPaint::kCanonicalTextSizeForPaths; |
| 120 | fTextInverseRatio = SkPaint::kCanonicalTextSizeForPaths / fFont.getTextSize(); |
jvanverth | aab626c | 2014-10-16 08:04:39 -0700 | [diff] [blame] | 121 | |
cdalton | 3bd909a | 2015-10-05 14:57:20 -0700 | [diff] [blame^] | 122 | // Compensate for the glyphs being scaled by fTextRatio. |
| 123 | if (!fStroke.isFillStyle()) { |
| 124 | fStroke.setStrokeStyle(fStroke.getWidth() / fTextRatio, |
| 125 | SkStrokeRec::kStrokeAndFill_Style == fStroke.getStyle()); |
| 126 | } |
| 127 | |
| 128 | fFont.setLinearText(true); |
| 129 | fFont.setLCDRenderText(false); |
| 130 | fFont.setAutohinted(false); |
| 131 | fFont.setHinting(SkPaint::kNo_Hinting); |
| 132 | fFont.setSubpixelText(true); |
| 133 | fFont.setTextSize(SkIntToScalar(SkPaint::kCanonicalTextSizeForPaths)); |
| 134 | |
| 135 | fUsingRawGlyphPaths = SK_Scalar1 == fFont.getTextScaleX() && |
| 136 | 0 == fFont.getTextSkewX() && |
| 137 | !fFont.isFakeBoldText() && |
| 138 | !fFont.isVerticalText(); |
| 139 | } else { |
| 140 | fTextRatio = fTextInverseRatio = 1.0f; |
| 141 | fUsingRawGlyphPaths = false; |
| 142 | } |
| 143 | |
| 144 | // When drawing from canonically sized paths, the actual local coords are fTextRatio * coords. |
| 145 | fLocalMatrix.setScale(fTextRatio, fTextRatio); |
| 146 | } |
| 147 | |
| 148 | GrStencilAndCoverTextContext::TextRun::~TextRun() { |
| 149 | } |
| 150 | |
| 151 | void GrStencilAndCoverTextContext::TextRun::setText(const char text[], size_t byteLength, |
| 152 | SkScalar x, SkScalar y, GrContext* ctx, |
| 153 | const SkSurfaceProps* surfaceProps) { |
| 154 | SkASSERT(byteLength == 0 || text != nullptr); |
| 155 | |
| 156 | SkAutoGlyphCacheNoGamma autoGlyphCache(fFont, surfaceProps, nullptr); |
| 157 | SkGlyphCache* glyphCache = autoGlyphCache.getCache(); |
| 158 | |
| 159 | fDraw.reset(GrPathRangeDraw::Create(this->createGlyphs(ctx, glyphCache), |
| 160 | GrPathRendering::kTranslate_PathTransformType, |
| 161 | fFont.countText(text, byteLength))); |
| 162 | |
| 163 | SkDrawCacheProc glyphCacheProc = fFont.getDrawCacheProc(); |
jvanverth | aab626c | 2014-10-16 08:04:39 -0700 | [diff] [blame] | 164 | |
jvanverth | aab626c | 2014-10-16 08:04:39 -0700 | [diff] [blame] | 165 | const char* stop = text + byteLength; |
| 166 | |
| 167 | // Measure first if needed. |
cdalton | 3bd909a | 2015-10-05 14:57:20 -0700 | [diff] [blame^] | 168 | if (fFont.getTextAlign() != SkPaint::kLeft_Align) { |
jvanverth | aab626c | 2014-10-16 08:04:39 -0700 | [diff] [blame] | 169 | SkFixed stopX = 0; |
| 170 | SkFixed stopY = 0; |
| 171 | |
| 172 | const char* textPtr = text; |
| 173 | while (textPtr < stop) { |
| 174 | // We don't need x, y here, since all subpixel variants will have the |
| 175 | // same advance. |
cdalton | 3bd909a | 2015-10-05 14:57:20 -0700 | [diff] [blame^] | 176 | const SkGlyph& glyph = glyphCacheProc(glyphCache, &textPtr, 0, 0); |
jvanverth | aab626c | 2014-10-16 08:04:39 -0700 | [diff] [blame] | 177 | |
| 178 | stopX += glyph.fAdvanceX; |
| 179 | stopY += glyph.fAdvanceY; |
| 180 | } |
| 181 | SkASSERT(textPtr == stop); |
| 182 | |
| 183 | SkScalar alignX = SkFixedToScalar(stopX) * fTextRatio; |
| 184 | SkScalar alignY = SkFixedToScalar(stopY) * fTextRatio; |
| 185 | |
cdalton | 3bd909a | 2015-10-05 14:57:20 -0700 | [diff] [blame^] | 186 | if (fFont.getTextAlign() == SkPaint::kCenter_Align) { |
jvanverth | aab626c | 2014-10-16 08:04:39 -0700 | [diff] [blame] | 187 | alignX = SkScalarHalf(alignX); |
| 188 | alignY = SkScalarHalf(alignY); |
| 189 | } |
| 190 | |
| 191 | x -= alignX; |
| 192 | y -= alignY; |
| 193 | } |
| 194 | |
| 195 | SkAutoKern autokern; |
| 196 | |
| 197 | SkFixed fixedSizeRatio = SkScalarToFixed(fTextRatio); |
| 198 | |
| 199 | SkFixed fx = SkScalarToFixed(x); |
| 200 | SkFixed fy = SkScalarToFixed(y); |
| 201 | while (text < stop) { |
cdalton | 3bd909a | 2015-10-05 14:57:20 -0700 | [diff] [blame^] | 202 | const SkGlyph& glyph = glyphCacheProc(glyphCache, &text, 0, 0); |
bungeman | d709ea8 | 2015-03-17 07:23:39 -0700 | [diff] [blame] | 203 | fx += SkFixedMul(autokern.adjust(glyph), fixedSizeRatio); |
jvanverth | aab626c | 2014-10-16 08:04:39 -0700 | [diff] [blame] | 204 | if (glyph.fWidth) { |
bsalomon | 1fcc01c | 2015-09-09 09:48:06 -0700 | [diff] [blame] | 205 | this->appendGlyph(glyph, SkPoint::Make(SkFixedToScalar(fx), SkFixedToScalar(fy))); |
jvanverth | aab626c | 2014-10-16 08:04:39 -0700 | [diff] [blame] | 206 | } |
| 207 | |
bungeman | d709ea8 | 2015-03-17 07:23:39 -0700 | [diff] [blame] | 208 | fx += SkFixedMul(glyph.fAdvanceX, fixedSizeRatio); |
| 209 | fy += SkFixedMul(glyph.fAdvanceY, fixedSizeRatio); |
jvanverth | aab626c | 2014-10-16 08:04:39 -0700 | [diff] [blame] | 210 | } |
jvanverth | aab626c | 2014-10-16 08:04:39 -0700 | [diff] [blame] | 211 | } |
| 212 | |
cdalton | 3bd909a | 2015-10-05 14:57:20 -0700 | [diff] [blame^] | 213 | void GrStencilAndCoverTextContext::TextRun::setPosText(const char text[], size_t byteLength, |
| 214 | const SkScalar pos[], int scalarsPerPosition, |
| 215 | const SkPoint& offset, GrContext* ctx, |
| 216 | const SkSurfaceProps* surfaceProps) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 217 | SkASSERT(byteLength == 0 || text != nullptr); |
kkinnunen | c6cb56f | 2014-06-24 00:12:27 -0700 | [diff] [blame] | 218 | SkASSERT(1 == scalarsPerPosition || 2 == scalarsPerPosition); |
| 219 | |
cdalton | 3bd909a | 2015-10-05 14:57:20 -0700 | [diff] [blame^] | 220 | SkAutoGlyphCacheNoGamma autoGlyphCache(fFont, surfaceProps, nullptr); |
| 221 | SkGlyphCache* glyphCache = autoGlyphCache.getCache(); |
kkinnunen | c6cb56f | 2014-06-24 00:12:27 -0700 | [diff] [blame] | 222 | |
cdalton | 3bd909a | 2015-10-05 14:57:20 -0700 | [diff] [blame^] | 223 | fDraw.reset(GrPathRangeDraw::Create(this->createGlyphs(ctx, glyphCache), |
| 224 | GrPathRendering::kTranslate_PathTransformType, |
| 225 | fFont.countText(text, byteLength))); |
kkinnunen | c6cb56f | 2014-06-24 00:12:27 -0700 | [diff] [blame] | 226 | |
cdalton | 3bd909a | 2015-10-05 14:57:20 -0700 | [diff] [blame^] | 227 | SkDrawCacheProc glyphCacheProc = fFont.getDrawCacheProc(); |
kkinnunen | c6cb56f | 2014-06-24 00:12:27 -0700 | [diff] [blame] | 228 | |
kkinnunen | c6cb56f | 2014-06-24 00:12:27 -0700 | [diff] [blame] | 229 | const char* stop = text + byteLength; |
kkinnunen | c6cb56f | 2014-06-24 00:12:27 -0700 | [diff] [blame] | 230 | |
cdalton | 38e13ad | 2014-11-07 06:02:15 -0800 | [diff] [blame] | 231 | SkTextMapStateProc tmsProc(SkMatrix::I(), offset, scalarsPerPosition); |
cdalton | 3bd909a | 2015-10-05 14:57:20 -0700 | [diff] [blame^] | 232 | SkTextAlignProc alignProc(fFont.getTextAlign()); |
cdalton | 38e13ad | 2014-11-07 06:02:15 -0800 | [diff] [blame] | 233 | while (text < stop) { |
cdalton | 3bd909a | 2015-10-05 14:57:20 -0700 | [diff] [blame^] | 234 | const SkGlyph& glyph = glyphCacheProc(glyphCache, &text, 0, 0); |
cdalton | 38e13ad | 2014-11-07 06:02:15 -0800 | [diff] [blame] | 235 | if (glyph.fWidth) { |
| 236 | SkPoint tmsLoc; |
| 237 | tmsProc(pos, &tmsLoc); |
| 238 | SkPoint loc; |
| 239 | alignProc(tmsLoc, glyph, &loc); |
kkinnunen | c6cb56f | 2014-06-24 00:12:27 -0700 | [diff] [blame] | 240 | |
bsalomon | 1fcc01c | 2015-09-09 09:48:06 -0700 | [diff] [blame] | 241 | this->appendGlyph(glyph, loc); |
kkinnunen | c6cb56f | 2014-06-24 00:12:27 -0700 | [diff] [blame] | 242 | } |
cdalton | 38e13ad | 2014-11-07 06:02:15 -0800 | [diff] [blame] | 243 | pos += scalarsPerPosition; |
kkinnunen | c6cb56f | 2014-06-24 00:12:27 -0700 | [diff] [blame] | 244 | } |
kkinnunen | c6cb56f | 2014-06-24 00:12:27 -0700 | [diff] [blame] | 245 | } |
| 246 | |
cdalton | 3bd909a | 2015-10-05 14:57:20 -0700 | [diff] [blame^] | 247 | GrPathRange* GrStencilAndCoverTextContext::TextRun::createGlyphs(GrContext* ctx, |
| 248 | SkGlyphCache* glyphCache) { |
| 249 | SkTypeface* typeface = fUsingRawGlyphPaths ? fFont.getTypeface() |
| 250 | : glyphCache->getScalerContext()->getTypeface(); |
| 251 | const SkDescriptor* desc = fUsingRawGlyphPaths ? nullptr : &glyphCache->getDescriptor(); |
kkinnunen | 50b58e6 | 2015-05-18 23:02:07 -0700 | [diff] [blame] | 252 | |
| 253 | static const GrUniqueKey::Domain kPathGlyphDomain = GrUniqueKey::GenerateDomain(); |
cdalton | 3bd909a | 2015-10-05 14:57:20 -0700 | [diff] [blame^] | 254 | int strokeDataCount = fStroke.computeUniqueKeyFragmentData32Cnt(); |
kkinnunen | 50b58e6 | 2015-05-18 23:02:07 -0700 | [diff] [blame] | 255 | GrUniqueKey glyphKey; |
| 256 | GrUniqueKey::Builder builder(&glyphKey, kPathGlyphDomain, 2 + strokeDataCount); |
| 257 | reinterpret_cast<uint32_t&>(builder[0]) = desc ? desc->getChecksum() : 0; |
| 258 | reinterpret_cast<uint32_t&>(builder[1]) = typeface ? typeface->uniqueID() : 0; |
| 259 | if (strokeDataCount > 0) { |
cdalton | 3bd909a | 2015-10-05 14:57:20 -0700 | [diff] [blame^] | 260 | fStroke.asUniqueKeyFragment(&builder[2]); |
kkinnunen | 50b58e6 | 2015-05-18 23:02:07 -0700 | [diff] [blame] | 261 | } |
bsalomon | 24db3b1 | 2015-01-23 04:24:04 -0800 | [diff] [blame] | 262 | builder.finish(); |
cdalton | 855d83f | 2014-09-18 13:51:53 -0700 | [diff] [blame] | 263 | |
| 264 | SkAutoTUnref<GrPathRange> glyphs( |
kkinnunen | 50b58e6 | 2015-05-18 23:02:07 -0700 | [diff] [blame] | 265 | static_cast<GrPathRange*>( |
| 266 | ctx->resourceProvider()->findAndRefResourceByUniqueKey(glyphKey))); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 267 | if (nullptr == glyphs) { |
cdalton | 3bd909a | 2015-10-05 14:57:20 -0700 | [diff] [blame^] | 268 | glyphs.reset(ctx->resourceProvider()->createGlyphs(typeface, desc, fStroke)); |
kkinnunen | 50b58e6 | 2015-05-18 23:02:07 -0700 | [diff] [blame] | 269 | ctx->resourceProvider()->assignUniqueKeyToResource(glyphKey, glyphs); |
| 270 | } else { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 271 | SkASSERT(nullptr == desc || glyphs->isEqualTo(*desc)); |
cdalton | 855d83f | 2014-09-18 13:51:53 -0700 | [diff] [blame] | 272 | } |
| 273 | |
| 274 | return glyphs.detach(); |
| 275 | } |
| 276 | |
cdalton | 3bd909a | 2015-10-05 14:57:20 -0700 | [diff] [blame^] | 277 | inline void GrStencilAndCoverTextContext::TextRun::appendGlyph(const SkGlyph& glyph, |
| 278 | const SkPoint& pos) { |
bsalomon | 1fcc01c | 2015-09-09 09:48:06 -0700 | [diff] [blame] | 279 | // Stick the glyphs we can't draw into the fallback arrays. |
| 280 | if (SkMask::kARGB32_Format == glyph.fMaskFormat) { |
| 281 | fFallbackIndices.push_back(glyph.getGlyphID()); |
cdalton | 7d5c950 | 2015-10-03 13:28:35 -0700 | [diff] [blame] | 282 | fFallbackPositions.push_back(pos); |
bsalomon | 1fcc01c | 2015-09-09 09:48:06 -0700 | [diff] [blame] | 283 | } else { |
cdalton | 7d5c950 | 2015-10-03 13:28:35 -0700 | [diff] [blame] | 284 | float translate[] = { fTextInverseRatio * pos.x(), fTextInverseRatio * pos.y() }; |
bsalomon | 1fcc01c | 2015-09-09 09:48:06 -0700 | [diff] [blame] | 285 | fDraw->append(glyph.getGlyphID(), translate); |
cdalton | b2808cd | 2014-07-25 14:13:57 -0700 | [diff] [blame] | 286 | } |
cdalton | 20b373c | 2014-12-01 08:38:55 -0800 | [diff] [blame] | 287 | } |
| 288 | |
cdalton | 3bd909a | 2015-10-05 14:57:20 -0700 | [diff] [blame^] | 289 | void GrStencilAndCoverTextContext::TextRun::draw(GrDrawContext* dc, |
| 290 | GrRenderTarget* rt, |
| 291 | const GrClip& clip, |
| 292 | const GrPaint& paint, |
| 293 | const SkMatrix& viewMatrix, |
| 294 | const SkIRect& regionClipBounds, |
| 295 | GrTextContext* fallbackTextContext, |
| 296 | const SkPaint& originalSkPaint) const { |
| 297 | SkASSERT(fDraw); |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 298 | |
cdalton | 3bd909a | 2015-10-05 14:57:20 -0700 | [diff] [blame^] | 299 | if (fDraw->count()) { |
| 300 | GrPipelineBuilder pipelineBuilder(paint, rt, clip); |
| 301 | SkASSERT(rt->isStencilBufferMultisampled() || !paint.isAntiAlias()); |
| 302 | pipelineBuilder.setState(GrPipelineBuilder::kHWAntialias_Flag, paint.isAntiAlias()); |
joshualitt | 7b670db | 2015-07-09 13:25:02 -0700 | [diff] [blame] | 303 | |
| 304 | GR_STATIC_CONST_SAME_STENCIL(kStencilPass, |
| 305 | kZero_StencilOp, |
vbuzinov | c5d58f0 | 2015-08-21 05:24:24 -0700 | [diff] [blame] | 306 | kKeep_StencilOp, |
joshualitt | 7b670db | 2015-07-09 13:25:02 -0700 | [diff] [blame] | 307 | kNotEqual_StencilFunc, |
| 308 | 0xffff, |
| 309 | 0x0000, |
| 310 | 0xffff); |
| 311 | |
| 312 | *pipelineBuilder.stencil() = kStencilPass; |
| 313 | |
cdalton | 3bd909a | 2015-10-05 14:57:20 -0700 | [diff] [blame^] | 314 | SkMatrix drawMatrix(viewMatrix); |
| 315 | drawMatrix.preScale(fTextRatio, fTextRatio); |
| 316 | |
| 317 | dc->drawPathsFromRange(&pipelineBuilder, drawMatrix, fLocalMatrix, paint.getColor(), fDraw, |
| 318 | GrPathRendering::kWinding_FillType); |
kkinnunen | c6cb56f | 2014-06-24 00:12:27 -0700 | [diff] [blame] | 319 | } |
| 320 | |
bsalomon | 1fcc01c | 2015-09-09 09:48:06 -0700 | [diff] [blame] | 321 | if (fFallbackIndices.count()) { |
| 322 | SkASSERT(fFallbackPositions.count() == fFallbackIndices.count()); |
cdalton | 20b373c | 2014-12-01 08:38:55 -0800 | [diff] [blame] | 323 | |
cdalton | 3bd909a | 2015-10-05 14:57:20 -0700 | [diff] [blame^] | 324 | enum { kPreservedFlags = SkPaint::kFakeBoldText_Flag | SkPaint::kLinearText_Flag | |
| 325 | SkPaint::kLCDRenderText_Flag | SkPaint::kAutoHinting_Flag }; |
| 326 | |
| 327 | SkPaint fallbackSkPaint(originalSkPaint); |
cdalton | 7d5c950 | 2015-10-03 13:28:35 -0700 | [diff] [blame] | 328 | fStroke.applyToPaint(&fallbackSkPaint); |
| 329 | if (!fStroke.isFillStyle()) { |
| 330 | fallbackSkPaint.setStrokeWidth(fStroke.getWidth() * fTextRatio); |
cdalton | 20b373c | 2014-12-01 08:38:55 -0800 | [diff] [blame] | 331 | } |
cdalton | 7d5c950 | 2015-10-03 13:28:35 -0700 | [diff] [blame] | 332 | fallbackSkPaint.setTextAlign(SkPaint::kLeft_Align); // Align has already been accounted for. |
| 333 | fallbackSkPaint.setTextEncoding(SkPaint::kGlyphID_TextEncoding); |
cdalton | 3bd909a | 2015-10-05 14:57:20 -0700 | [diff] [blame^] | 334 | fallbackSkPaint.setHinting(fFont.getHinting()); |
| 335 | fallbackSkPaint.setFlags((fFont.getFlags() & kPreservedFlags) | |
| 336 | (originalSkPaint.getFlags() & ~kPreservedFlags)); |
cdalton | 7d5c950 | 2015-10-03 13:28:35 -0700 | [diff] [blame] | 337 | // No need for subpixel positioning with bitmap glyphs. TODO: revisit if non-bitmap color |
| 338 | // glyphs show up and https://code.google.com/p/skia/issues/detail?id=4408 gets resolved. |
| 339 | fallbackSkPaint.setSubpixelText(false); |
cdalton | 3bd909a | 2015-10-05 14:57:20 -0700 | [diff] [blame^] | 340 | fallbackSkPaint.setTextSize(fFont.getTextSize() * fTextRatio); |
cdalton | 20b373c | 2014-12-01 08:38:55 -0800 | [diff] [blame] | 341 | |
cdalton | 3bd909a | 2015-10-05 14:57:20 -0700 | [diff] [blame^] | 342 | fallbackTextContext->drawPosText(dc, rt, clip, paint, fallbackSkPaint, viewMatrix, |
| 343 | (char*)fFallbackIndices.begin(), |
| 344 | sizeof(uint16_t) * fFallbackIndices.count(), |
| 345 | fFallbackPositions[0].asScalars(), 2, SkPoint::Make(0, 0), |
| 346 | regionClipBounds); |
cdalton | 20b373c | 2014-12-01 08:38:55 -0800 | [diff] [blame] | 347 | } |
kkinnunen | c6cb56f | 2014-06-24 00:12:27 -0700 | [diff] [blame] | 348 | } |