diff options
-rw-r--r-- | gm/texteffects.cpp | 113 | ||||
-rw-r--r-- | include/core/SkPaint.h | 35 | ||||
-rw-r--r-- | src/core/SkPaint.cpp | 106 |
3 files changed, 225 insertions, 29 deletions
diff --git a/gm/texteffects.cpp b/gm/texteffects.cpp index e199594004..f0d2752f74 100644 --- a/gm/texteffects.cpp +++ b/gm/texteffects.cpp @@ -9,6 +9,7 @@ #include "SkBlurMask.h" #include "SkBlurMaskFilter.h" #include "SkReadBuffer.h" +#include "SkTextBlob.h" #include "SkWriteBuffer.h" #include "SkLayerRasterizer.h" @@ -267,7 +268,7 @@ DEF_SIMPLE_GM(fancyunderline, canvas, 900, 1350) { const char* fam[] = { "sans-serif", "serif", "monospace" }; const char test[] = "aAjJgGyY_|{-(~[,]qQ}pP}zZ"; SkPoint textPt = { 10, 80 }; - for (int font = 0; font < 3; ++font) { + for (size_t font = 0; font < SK_ARRAY_COUNT(fam); ++font) { sk_tool_utils::set_portable_typeface(&paint, fam[font]); for (SkScalar textSize = 100; textSize > 10; textSize -= 20) { paint.setTextSize(textSize); @@ -311,7 +312,7 @@ DEF_SIMPLE_GM(fancyposunderline, canvas, 900, 1350) { const char* fam[] = { "sans-serif", "serif", "monospace" }; const char test[] = "aAjJgGyY_|{-(~[,]qQ}pP}zZ"; SkPoint textPt = { 10, 80 }; - for (int font = 0; font < 3; ++font) { + for (size_t font = 0; font < SK_ARRAY_COUNT(fam); ++font) { sk_tool_utils::set_portable_typeface(&paint, fam[font]); for (SkScalar textSize = 100; textSize > 10; textSize -= 20) { paint.setTextSize(textSize); @@ -348,6 +349,114 @@ DEF_SIMPLE_GM(fancyposunderline, canvas, 900, 1350) { } } +namespace { + +sk_sp<const SkTextBlob> MakeFancyBlob(const SkPaint& paint, const char* text) { + SkPaint blobPaint(paint); + + const size_t textLen = strlen(text); + const int glyphCount = blobPaint.textToGlyphs(text, textLen, nullptr); + SkAutoTArray<SkGlyphID> glyphs(glyphCount); + blobPaint.textToGlyphs(text, textLen, glyphs.get()); + + blobPaint.setTextEncoding(SkPaint::kGlyphID_TextEncoding); + const size_t glyphTextBytes = SkTo<uint32_t>(glyphCount) * sizeof(SkGlyphID); + const int widthCount = blobPaint.getTextWidths(glyphs.get(), glyphTextBytes, nullptr); + SkAssertResult(widthCount == glyphCount); + + SkAutoTArray<SkScalar> widths(glyphCount); + blobPaint.getTextWidths(glyphs.get(), glyphTextBytes, widths.get()); + + SkTextBlobBuilder blobBuilder; + int glyphIndex = 0; + SkScalar advance = 0; + + // Default-positioned run. + { + const int defaultRunLen = glyphCount / 3; + const SkTextBlobBuilder::RunBuffer& buf = blobBuilder.allocRun(blobPaint, + defaultRunLen, + advance, 0); + memcpy(buf.glyphs, glyphs.get(), SkTo<uint32_t>(defaultRunLen) * sizeof(SkGlyphID)); + + for (int i = 0; i < defaultRunLen; ++i) { + advance += widths[glyphIndex++]; + } + } + + // Horizontal-positioned run. + { + const int horizontalRunLen = glyphCount / 3; + const SkTextBlobBuilder::RunBuffer& buf = blobBuilder.allocRunPosH(blobPaint, + horizontalRunLen, + 0); + memcpy(buf.glyphs, glyphs.get() + glyphIndex, + SkTo<uint32_t>(horizontalRunLen) * sizeof(SkGlyphID)); + for (int i = 0; i < horizontalRunLen; ++i) { + buf.pos[i] = advance; + advance += widths[glyphIndex++]; + } + } + + // Full-positioned run. + { + const int fullRunLen = glyphCount - glyphIndex; + const SkTextBlobBuilder::RunBuffer& buf = blobBuilder.allocRunPos(blobPaint, fullRunLen); + memcpy(buf.glyphs, glyphs.get() + glyphIndex, + SkTo<uint32_t>(fullRunLen) * sizeof(SkGlyphID)); + for (int i = 0; i < fullRunLen; ++i) { + buf.pos[i * 2 + 0] = advance; // x offset + buf.pos[i * 2 + 1] = 0; // y offset + advance += widths[glyphIndex++]; + } + } + + return sk_sp<const SkTextBlob>(blobBuilder.build()); +} + +} // anonymous ns + +DEF_SIMPLE_GM(fancyblobunderline, canvas, 1480, 1380) { + SkPaint paint; + paint.setAntiAlias(true); + const char* fam[] = { "sans-serif", "serif", "monospace" }; + const char test[] = "aAjJgGyY_|{-(~[,]qQ}pP}zZ"; + const SkPoint blobOffset = { 10, 80 }; + + for (size_t font = 0; font < SK_ARRAY_COUNT(fam); ++font) { + sk_tool_utils::set_portable_typeface(&paint, fam[font]); + for (SkScalar textSize = 100; textSize > 10; textSize -= 20) { + paint.setTextSize(textSize); + const SkScalar uWidth = textSize / 15; + paint.setStrokeWidth(uWidth); + paint.setStyle(SkPaint::kFill_Style); + + sk_sp<const SkTextBlob> blob = MakeFancyBlob(paint, test); + canvas->drawTextBlob(blob.get(), blobOffset.x(), blobOffset.y(), paint); + + const SkScalar uPos = uWidth; + const SkScalar bounds[2] = { uPos - uWidth / 2, uPos + uWidth / 2 }; + const int interceptCount = paint.getTextBlobIntercepts(blob.get(), bounds, nullptr); + SkASSERT(!(interceptCount % 2)); + + SkTDArray<SkScalar> intercepts; + intercepts.setCount(interceptCount); + paint.getTextBlobIntercepts(blob.get(), bounds, intercepts.begin()); + + const SkScalar start = blob->bounds().left(); + const SkScalar end = blob->bounds().right(); + SkPath underline = create_underline(intercepts, start, end, uPos, uWidth, textSize); + underline.offset(blobOffset.x(), blobOffset.y()); + paint.setStyle(SkPaint::kStroke_Style); + canvas->drawPath(underline, paint); + + canvas->translate(0, textSize * 1.3f); + } + + canvas->translate(0, 60); + } +} + DEF_SIMPLE_GM(fancyunderlinebars, canvas, 1500, 460) { SkPaint paint; paint.setAntiAlias(true); diff --git a/include/core/SkPaint.h b/include/core/SkPaint.h index ef80b73f21..ea7eedd65f 100644 --- a/include/core/SkPaint.h +++ b/include/core/SkPaint.h @@ -33,6 +33,7 @@ class SkRasterizer; struct SkScalerContextEffects; class SkShader; class SkSurfaceProps; +class SkTextBlob; class SkTypeface; #define kBicubicFilterBitmap_Flag kHighQualityFilterBitmap_Flag @@ -964,6 +965,40 @@ public: int getPosTextIntercepts(const void* text, size_t length, const SkPoint pos[], const SkScalar bounds[2], SkScalar* intervals) const; + /** Return the number of intervals that intersect the intercept along the axis of the advance. + * The return count is zero or a multiple of two, and is at most the number of glyphs * 2 in + * string. The caller may pass nullptr for intervals to determine the size of the interval + * array, or may conservatively pre-allocate an array with length * 2 entries. The computed + * intervals are cached by glyph to improve performance for multiple calls. + * This permits constructing an underline that skips the descenders. + * + * @param text The text. + * @param length Number of bytes of text. + * @param xpos Array of x-positions, used to position each character. + * @param constY The shared Y coordinate for all of the positions. + * @param bounds The lower and upper line parallel to the advance. + * @param array If not null, the glyph bounds contained by the advance parallel lines. + * + * @return The number of intersections, which may be zero. + */ + int getPosTextHIntercepts(const void* text, size_t length, const SkScalar xpos[], + SkScalar constY, const SkScalar bounds[2], SkScalar* intervals) const; + + /** Return the number of intervals that intersect the intercept along the axis of the advance. + * The return count is zero or a multiple of two, and is at most the number of glyphs * 2 in + * text blob. The caller may pass nullptr for intervals to determine the size of the interval + * array. The computed intervals are cached by glyph to improve performance for multiple calls. + * This permits constructing an underline that skips the descenders. + * + * @param blob The text blob. + * @param bounds The lower and upper line parallel to the advance. + * @param array If not null, the glyph bounds contained by the advance parallel lines. + * + * @return The number of intersections, which may be zero. + */ + int getTextBlobIntercepts(const SkTextBlob* blob, const SkScalar bounds[2], + SkScalar* intervals) const; + /** * Return a rectangle that represents the union of the bounds of all * of the glyphs, but each one positioned at (0,0). This may be conservatively large, and diff --git a/src/core/SkPaint.cpp b/src/core/SkPaint.cpp index 528b1d0404..07c10d5a78 100644 --- a/src/core/SkPaint.cpp +++ b/src/core/SkPaint.cpp @@ -27,12 +27,14 @@ #include "SkShader.h" #include "SkStringUtils.h" #include "SkStroke.h" +#include "SkStrokeRec.h" +#include "SkSurfacePriv.h" +#include "SkTextBlob.h" +#include "SkTextBlobRunIterator.h" #include "SkTextFormatParams.h" #include "SkTextToPathIter.h" #include "SkTLazy.h" #include "SkTypeface.h" -#include "SkStrokeRec.h" -#include "SkSurfacePriv.h" #include "SkXfermode.h" static inline uint32_t set_clear_mask(uint32_t bits, bool cond, uint32_t mask) { @@ -1127,23 +1129,6 @@ void SkPaint::getTextPath(const void* textData, size_t length, } } -int SkPaint::getTextIntercepts(const void* textData, size_t length, - SkScalar x, SkScalar y, const SkScalar bounds[2], - SkScalar* array) const { - SkASSERT(length == 0 || textData != nullptr); - if (!length) { - return 0; - } - - const char* text = (const char*) textData; - SkTextInterceptsIter iter(text, length, *this, bounds, x, y, - SkTextInterceptsIter::TextType::kText); - int count = 0; - while (iter.next(array, &count)) { - } - return count; -} - void SkPaint::getPosTextPath(const void* textData, size_t length, const SkPoint pos[], SkPath* path) const { SkASSERT(length == 0 || textData != nullptr); @@ -1173,22 +1158,89 @@ void SkPaint::getPosTextPath(const void* textData, size_t length, } } -int SkPaint::getPosTextIntercepts(const void* textData, size_t length, const SkPoint pos[], - const SkScalar bounds[2], SkScalar* array) const { - SkASSERT(length == 0 || textData != nullptr); +template <SkTextInterceptsIter::TextType TextType, typename Func> +int GetTextIntercepts(const SkPaint& paint, const void* text, size_t length, + const SkScalar bounds[2], SkScalar* array, Func posMaker) { + SkASSERT(length == 0 || text != nullptr); if (!length) { return 0; } - const char* text = (const char*) textData; - SkTextInterceptsIter iter(text, length, *this, bounds, pos[0].fX, pos[0].fY, - SkTextInterceptsIter::TextType::kPosText); + const SkPoint pos0 = posMaker(0); + SkTextInterceptsIter iter(static_cast<const char*>(text), length, paint, bounds, + pos0.x(), pos0.y(), TextType); + int i = 0; int count = 0; while (iter.next(array, &count)) { - i++; - iter.setPosition(pos[i].fX, pos[i].fY); + if (TextType == SkTextInterceptsIter::TextType::kPosText) { + const SkPoint pos = posMaker(++i); + iter.setPosition(pos.x(), pos.y()); + } + } + + return count; +} + +int SkPaint::getTextIntercepts(const void* textData, size_t length, + SkScalar x, SkScalar y, const SkScalar bounds[2], + SkScalar* array) const { + + return GetTextIntercepts<SkTextInterceptsIter::TextType::kText>( + *this, textData, length, bounds, array, [&x, &y] (int) -> SkPoint { + return SkPoint::Make(x, y); + }); +} + +int SkPaint::getPosTextIntercepts(const void* textData, size_t length, const SkPoint pos[], + const SkScalar bounds[2], SkScalar* array) const { + + return GetTextIntercepts<SkTextInterceptsIter::TextType::kPosText>( + *this, textData, length, bounds, array, [&pos] (int i) -> SkPoint { + return pos[i]; + }); +} + +int SkPaint::getPosTextHIntercepts(const void* textData, size_t length, const SkScalar xpos[], + SkScalar constY, const SkScalar bounds[2], + SkScalar* array) const { + + return GetTextIntercepts<SkTextInterceptsIter::TextType::kPosText>( + *this, textData, length, bounds, array, [&xpos, &constY] (int i) -> SkPoint { + return SkPoint::Make(xpos[i], constY); + }); +} + +int SkPaint::getTextBlobIntercepts(const SkTextBlob* blob, const SkScalar bounds[2], + SkScalar* intervals) const { + int count = 0; + SkPaint runPaint(*this); + + SkTextBlobRunIterator it(blob); + while (!it.done()) { + it.applyFontToPaint(&runPaint); + const size_t runByteCount = it.glyphCount() * sizeof(SkGlyphID); + SkScalar* runIntervals = intervals ? intervals + count : nullptr; + + switch (it.positioning()) { + case SkTextBlob::kDefault_Positioning: + count += runPaint.getTextIntercepts(it.glyphs(), runByteCount, it.offset().x(), + it.offset().y(), bounds, runIntervals); + break; + case SkTextBlob::kHorizontal_Positioning: + count += runPaint.getPosTextHIntercepts(it.glyphs(), runByteCount, it.pos(), + it.offset().y(), bounds, runIntervals); + break; + case SkTextBlob::kFull_Positioning: + count += runPaint.getPosTextIntercepts(it.glyphs(), runByteCount, + reinterpret_cast<const SkPoint*>(it.pos()), + bounds, runIntervals); + break; + } + + it.next(); } + return count; } |