Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(135)

Unified Diff: src/core/SkGlyphCache.cpp

Issue 1654883003: add helper to create fancy underlines (Closed) Base URL: https://skia.googlesource.com/skia.git@master
Patch Set: make bounds const Created 4 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: src/core/SkGlyphCache.cpp
diff --git a/src/core/SkGlyphCache.cpp b/src/core/SkGlyphCache.cpp
index 1512dbf0e5dd0a627369e74d32ce804945dd6bfc..56836524a993bb36eca90f4a4f29527b95921c1a 100644
--- a/src/core/SkGlyphCache.cpp
+++ b/src/core/SkGlyphCache.cpp
@@ -224,6 +224,170 @@ const SkPath* SkGlyphCache::findPath(const SkGlyph& glyph) {
return glyph.fPath;
}
+#include "../pathops/SkPathOpsCubic.h"
+#include "../pathops/SkPathOpsQuad.h"
+
+static bool quad_in_bounds(const SkScalar* pts, const SkScalar bounds[2]) {
+ SkScalar min = SkTMin(SkTMin(pts[0], pts[2]), pts[4]);
+ if (bounds[1] < min) {
+ return false;
+ }
+ SkScalar max = SkTMax(SkTMax(pts[0], pts[2]), pts[4]);
+ return bounds[0] < max;
+}
+
+static bool cubic_in_bounds(const SkScalar* pts, const SkScalar bounds[2]) {
+ SkScalar min = SkTMin(SkTMin(SkTMin(pts[0], pts[2]), pts[4]), pts[6]);
+ if (bounds[1] < min) {
+ return false;
+ }
+ SkScalar max = SkTMax(SkTMax(SkTMax(pts[0], pts[2]), pts[4]), pts[6]);
+ return bounds[0] < max;
+}
+
+static void offset_results(const SkGlyph::Intercept* intercept, SkScalar scale, SkScalar xPos,
+ SkScalar* array, int* count) {
+ if (array) {
+ array += *count;
+ for (int index = 0; index < 2; index++) {
+ *array++ = intercept->fInterval[index] * scale + xPos;
+ }
+ }
+ *count += 2;
+}
+
+static void add_interval(SkScalar val, SkGlyph::Intercept* intercept) {
+ intercept->fInterval[0] = SkTMin(intercept->fInterval[0], val);
+ intercept->fInterval[1] = SkTMax(intercept->fInterval[1], val);
+}
+
+static void add_points(const SkPoint* pts, int ptCount, const SkScalar bounds[2],
+ bool yAxis, SkGlyph::Intercept* intercept) {
+ for (int i = 0; i < ptCount; ++i) {
+ SkScalar val = *(&pts[i].fY - yAxis);
+ if (bounds[0] < val && val < bounds[1]) {
+ add_interval(*(&pts[i].fX + yAxis), intercept);
+ }
+ }
+}
+
+static void add_line(const SkPoint pts[2], SkScalar axis, bool yAxis,
+ SkGlyph::Intercept* intercept) {
+ SkScalar t = yAxis ? (axis - pts[0].fX) / (pts[1].fX - pts[0].fX)
+ : (axis - pts[0].fY) / (pts[1].fY - pts[0].fY);
+ if (0 <= t && t < 1) { // this handles divide by zero above
+ add_interval(yAxis ? pts[0].fY + t * (pts[1].fY - pts[0].fY)
+ : pts[0].fX + t * (pts[1].fX - pts[0].fX), intercept);
+ }
+}
+
+static void add_quad(const SkPoint pts[2], SkScalar axis, bool yAxis,
+ SkGlyph::Intercept* intercept) {
+ SkDQuad quad;
+ quad.set(pts);
+ double roots[2];
+ int count = yAxis ? quad.verticalIntersect(axis, roots)
+ : quad.horizontalIntersect(axis, roots);
+ while (--count >= 0) {
+ SkPoint pt = quad.ptAtT(roots[count]).asSkPoint();
+ add_interval(*(&pt.fX + yAxis), intercept);
+ }
+}
+
+static void add_cubic(const SkPoint pts[3], SkScalar axis, bool yAxis,
+ SkGlyph::Intercept* intercept) {
+ SkDCubic cubic;
+ cubic.set(pts);
+ double roots[3];
+ int count = yAxis ? cubic.verticalIntersect(axis, roots)
+ : cubic.horizontalIntersect(axis, roots);
+ while (--count >= 0) {
+ SkPoint pt = cubic.ptAtT(roots[count]).asSkPoint();
+ add_interval(*(&pt.fX + yAxis), intercept);
+ }
+}
+
+static const SkGlyph::Intercept* match_bounds(const SkGlyph* glyph, const SkScalar bounds[2]) {
+ const SkGlyph::Intercept* intercept = glyph->fIntercept;
+ while (intercept) {
+ if (bounds[0] == intercept->fBounds[0] && bounds[1] == intercept->fBounds[1]) {
+ return intercept;
+ }
+ intercept = intercept->fNext;
+ }
+ return nullptr;
+}
+
+void SkGlyphCache::findIntercepts(const SkScalar bounds[2], SkScalar scale, SkScalar xPos,
+ bool yAxis, SkGlyph* glyph, SkScalar* array, int* count) {
+ const SkGlyph::Intercept* match = match_bounds(glyph, bounds);
+
+ if (match) {
+ if (match->fInterval[0] < match->fInterval[1]) {
+ offset_results(match, scale, xPos, array, count);
+ }
+ return;
+ }
+
+ SkGlyph::Intercept* intercept =
+ (SkGlyph::Intercept* ) fGlyphAlloc.allocThrow(sizeof(SkGlyph::Intercept));
+ intercept->fNext = glyph->fIntercept;
+ intercept->fBounds[0] = bounds[0];
+ intercept->fBounds[1] = bounds[1];
+ intercept->fInterval[0] = SK_ScalarMax;
+ intercept->fInterval[1] = SK_ScalarMin;
+ glyph->fIntercept = intercept;
+ const SkPath* path = glyph->fPath;
+ const SkRect& pathBounds = path->getBounds();
+ if (*(&pathBounds.fBottom - yAxis) < bounds[0] || bounds[1] < *(&pathBounds.fTop - yAxis)) {
+ return;
+ }
+ SkPath::Iter iter(*path, false);
+ SkPoint pts[4];
+ SkPath::Verb verb;
+ while (SkPath::kDone_Verb != (verb = iter.next(pts))) {
+ switch (verb) {
+ case SkPath::kMove_Verb:
+ break;
+ case SkPath::kLine_Verb:
+ add_line(pts, bounds[0], yAxis, intercept);
+ add_line(pts, bounds[1], yAxis, intercept);
+ add_points(pts, 2, bounds, yAxis, intercept);
+ break;
+ case SkPath::kQuad_Verb:
+ if (!quad_in_bounds(&pts[0].fY - yAxis, bounds)) {
+ break;
+ }
+ add_quad(pts, bounds[0], yAxis, intercept);
+ add_quad(pts, bounds[1], yAxis, intercept);
+ add_points(pts, 3, bounds, yAxis, intercept);
+ break;
+ case SkPath::kConic_Verb:
+ SkASSERT(0); // no support for text composed of conics
+ break;
+ case SkPath::kCubic_Verb:
+ if (!cubic_in_bounds(&pts[0].fY - yAxis, bounds)) {
+ break;
+ }
+ add_cubic(pts, bounds[0], yAxis, intercept);
+ add_cubic(pts, bounds[1], yAxis, intercept);
+ add_points(pts, 4, bounds, yAxis, intercept);
+ break;
+ case SkPath::kClose_Verb:
+ break;
+ default:
+ SkASSERT(0);
+ break;
+ }
+ }
+ if (intercept->fInterval[0] >= intercept->fInterval[1]) {
+ intercept->fInterval[0] = SK_ScalarMax;
+ intercept->fInterval[1] = SK_ScalarMin;
+ return;
+ }
+ offset_results(intercept, scale, xPos, array, count);
+}
+
void SkGlyphCache::dump() const {
const SkTypeface* face = fScalerContext->getTypeface();
const SkScalerContextRec& rec = fScalerContext->getRec();
« src/core/SkGlyph.h ('K') | « src/core/SkGlyphCache.h ('k') | src/core/SkPaint.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698