From 219f3622ba32a1cb6380212a2ec1947513fbfeb9 Mon Sep 17 00:00:00 2001 From: Ben Wagner Date: Mon, 17 Jul 2017 15:32:25 -0400 Subject: Add strikeout font metrics. This also updates create_test_font so that it can be built, compiles, and uses SkFontStyle instead of SkTypeface::Style. BUG=b/63669723 Change-Id: I6eb0f851853f4721cf8e5052255b5b6750c3257f Reviewed-on: https://skia-review.googlesource.com/24740 Reviewed-by: Cary Clark Reviewed-by: Mike Reed Reviewed-by: Mike Klein Commit-Queue: Ben Wagner --- gm/fontmgr.cpp | 67 ++++++++++++++++++++++++++++++++++++++++++++++------------ 1 file changed, 53 insertions(+), 14 deletions(-) (limited to 'gm/fontmgr.cpp') diff --git a/gm/fontmgr.cpp b/gm/fontmgr.cpp index bef26f3c18..6bed35426e 100644 --- a/gm/fontmgr.cpp +++ b/gm/fontmgr.cpp @@ -230,18 +230,55 @@ public: } static void show_bounds(SkCanvas* canvas, const SkPaint& paint, SkScalar x, SkScalar y, - SkColor boundsColor) { - const char str[] = "jyHO[]{}@-_&%$"; + SkColor boundsColor) + { + SkPaint glyphPaint(paint); + SkRect fontBounds = glyphPaint.getFontBounds(); + fontBounds.offset(x, y); + SkPaint boundsPaint(glyphPaint); + boundsPaint.setColor(boundsColor); + canvas->drawRect(fontBounds, boundsPaint); + + SkPaint::FontMetrics fm; + glyphPaint.getFontMetrics(&fm); + SkPaint metricsPaint(boundsPaint); + metricsPaint.setStyle(SkPaint::kFill_Style); + metricsPaint.setAlpha(0x40); + if ((fm.fFlags & SkPaint::FontMetrics::kUnderlinePositionIsValid_Flag) && + (fm.fFlags & SkPaint::FontMetrics::kUnderlinePositionIsValid_Flag)) + { + SkRect underline{ fontBounds.fLeft, fm.fUnderlinePosition+y, + fontBounds.fRight, fm.fUnderlinePosition+y + fm.fUnderlineThickness }; + canvas->drawRect(underline, metricsPaint); + } - for (int i = 0; str[i]; ++i) { - canvas->drawText(&str[i], 1, x, y, paint); + if ((fm.fFlags & SkPaint::FontMetrics::kStrikeoutPositionIsValid_Flag) && + (fm.fFlags & SkPaint::FontMetrics::kStrikeoutPositionIsValid_Flag)) + { + SkRect strikeout{ fontBounds.fLeft, fm.fStrikeoutPosition+y - fm.fStrikeoutThickness, + fontBounds.fRight, fm.fStrikeoutPosition+y }; + canvas->drawRect(strikeout, metricsPaint); } - SkRect r = paint.getFontBounds(); - r.offset(x, y); - SkPaint p(paint); - p.setColor(boundsColor); - canvas->drawRect(r, p); + SkGlyphID left = 0, right = 0, top = 0, bottom = 0; + { + int numGlyphs = glyphPaint.getTypeface()->countGlyphs(); + SkRect min = {0, 0, 0, 0}; + glyphPaint.setTextEncoding(SkPaint::kGlyphID_TextEncoding); + for (int i = 0; i < numGlyphs; ++i) { + SkGlyphID glyphId = i; + SkRect cur; + glyphPaint.measureText(&glyphId, sizeof(glyphId), &cur); + if (cur.fLeft < min.fLeft ) { min.fLeft = cur.fLeft; left = i; } + if (cur.fTop < min.fTop ) { min.fTop = cur.fTop ; top = i; } + if (min.fRight < cur.fRight ) { min.fRight = cur.fRight; right = i; } + if (min.fBottom < cur.fBottom) { min.fBottom = cur.fBottom; bottom = i; } + } + } + SkGlyphID str[] = { left, right, top, bottom }; + for (size_t i = 0; i < SK_ARRAY_COUNT(str); ++i) { + canvas->drawText(&str[i], sizeof(str[0]), x, y, glyphPaint); + } } protected: @@ -270,21 +307,23 @@ protected: int index = 0; SkScalar x = 0, y = 0; - canvas->translate(80, 120); + canvas->translate(10, 120); for (int i = 0; i < count; ++i) { sk_sp set(fm->createStyleSet(i)); - for (int j = 0; j < set->count(); ++j) { + for (int j = 0; j < set->count() && j < 3; ++j) { paint.setTypeface(sk_sp(set->createTypeface(j))); if (paint.getTypeface()) { + SkRect fontBounds = paint.getFontBounds(); + x -= fontBounds.fLeft; show_bounds(canvas, paint, x, y, boundsColors[index & 1]); + x += fontBounds.fRight + 20; index += 1; - x += 160; - if (0 == (index % 6)) { + if (x > 900) { x = 0; y += 160; } - if (index >= 30) { + if (y >= 700) { return; } } -- cgit v1.2.3