From 2a24338c777462e04a2b26295f9c034155ee8f3e Mon Sep 17 00:00:00 2001 From: halcanary Date: Wed, 9 Sep 2015 08:16:41 -0700 Subject: GM: replace boilerplate with macros I have verified locally that nothing draws differently. Motivation: * SK_SIMPLE_GM makes it easier to write a GM. * Reducing 1100 lines of code makes maintenance easier. * Simple GMs are easy to convert to Fiddles. Review URL: https://codereview.chromium.org/1333553002 --- gm/glyph_pos.cpp | 118 ++++++++++++++++++------------------------------------- 1 file changed, 39 insertions(+), 79 deletions(-) (limited to 'gm/glyph_pos.cpp') diff --git a/gm/glyph_pos.cpp b/gm/glyph_pos.cpp index 15229b8dab..ed61c7634b 100644 --- a/gm/glyph_pos.cpp +++ b/gm/glyph_pos.cpp @@ -17,60 +17,36 @@ static const SkScalar kTextHeight = 14.0f; static const char kText[] = "Proportional Hamburgefons #% fi"; -namespace skiagm { - -class GlyphPosGM : public GM { -public: - GlyphPosGM(SkScalar strokeWidth, SkPaint::Style strokeStyle) - : fStrokeWidth(strokeWidth) - , fStrokeStyle(strokeStyle) { - } - -protected: - - SkString onShortName() override { - SkString str("glyph_pos"); - if (fStrokeWidth == 0.0f) { - str.append("_h"); // h == Hairline. - } else { - str.append("_n"); // n == Normal. - } - if (fStrokeStyle == SkPaint::kStroke_Style) { - str.append("_s"); - } else if (fStrokeStyle == SkPaint::kFill_Style) { - str.append("_f"); - } else { - str.append("_b"); // b == Both. - } - return str; - } - - SkISize onISize() override { return SkISize::Make(800, 600); } - - void onDraw(SkCanvas* canvas) override { - +static void drawTestCase(SkCanvas* canvas, + SkScalar textScale, + SkScalar strokeWidth, + SkPaint::Style strokeStyle); + +static void draw_gm(SkCanvas* canvas, + SkScalar strokeWidth, + SkPaint::Style strokeStyle) { // There's a black pixel at 40, 40 for reference. canvas->drawPoint(40.0f, 40.0f, SK_ColorBLACK); // Two reference images. canvas->translate(50.0f, 50.0f); - drawTestCase(canvas, 1.0f); + drawTestCase(canvas, 1.0f, strokeWidth, strokeStyle); canvas->translate(0.0f, 50.0f); - drawTestCase(canvas, 3.0f); + drawTestCase(canvas, 3.0f, strokeWidth, strokeStyle); // Uniform scaling test. canvas->translate(0.0f, 100.0f); canvas->save(); canvas->scale(3.0f, 3.0f); - drawTestCase(canvas, 1.0f); + drawTestCase(canvas, 1.0f, strokeWidth, strokeStyle); canvas->restore(); // Non-uniform scaling test. canvas->translate(0.0f, 100.0f); canvas->save(); canvas->scale(3.0f, 6.0f); - drawTestCase(canvas, 1.0f); + drawTestCase(canvas, 1.0f, strokeWidth, strokeStyle); canvas->restore(); // Skew test. @@ -82,7 +58,7 @@ protected: skew.setSkewX(8.0f / 25.0f); skew.setSkewY(2.0f / 25.0f); canvas->concat(skew); - drawTestCase(canvas, 1.0f); + drawTestCase(canvas, 1.0f, strokeWidth, strokeStyle); canvas->restore(); // Perspective test. @@ -94,23 +70,26 @@ protected: perspective.setSkewX(8.0f / 25.0f); perspective.setSkewY(2.0f / 25.0f); - canvas->concat(perspective); - drawTestCase(canvas, 1.0f); + drawTestCase(canvas, 1.0f, strokeWidth, strokeStyle); canvas->restore(); - } +} - void drawTestCase(SkCanvas* canvas, SkScalar textScale) { +static void drawTestCase(SkCanvas* canvas, + SkScalar textScale, + SkScalar strokeWidth, + SkPaint::Style strokeStyle) { SkPaint paint; paint.setColor(SK_ColorBLACK); paint.setAntiAlias(true); paint.setTextSize(kTextHeight * textScale); sk_tool_utils::set_portable_typeface(&paint); - paint.setStrokeWidth(fStrokeWidth); - paint.setStyle(fStrokeStyle); + paint.setStrokeWidth(strokeWidth); + paint.setStyle(strokeStyle); - // This demonstrates that we can not measure the text if there's a device transform. The - // canvas total matrix will end up being a device transform. + // This demonstrates that we can not measure the text if + // there's a device transform. The canvas total matrix will + // end up being a device transform. bool drawRef = !(canvas->getTotalMatrix().getType() & ~(SkMatrix::kIdentity_Mask | SkMatrix::kTranslate_Mask)); @@ -132,8 +111,8 @@ protected: // Black text is the testcase, eg. the text. paint.setColor(SK_ColorBLACK); - paint.setStrokeWidth(fStrokeWidth); - paint.setStyle(fStrokeStyle); + paint.setStrokeWidth(strokeWidth); + paint.setStyle(strokeStyle); canvas->drawText(kText, sizeof(kText) - 1, 0.0f, 0.0f, paint); if (drawRef) { @@ -151,42 +130,23 @@ protected: w += widths[i]; } } - } - -private: - SkScalar fStrokeWidth; - SkPaint::Style fStrokeStyle; - - typedef GM INHERITED; -}; - -////////////////////////////////////////////////////////////////////////////// - -static GM* GlyphPosHairlineStrokeAndFillFactory(void*) { - return new GlyphPosGM(0.0f, SkPaint::kStrokeAndFill_Style); } -static GM* GlyphPosStrokeAndFillFactory(void*) { - return new GlyphPosGM(1.2f, SkPaint::kStrokeAndFill_Style); + +DEF_SIMPLE_GM(glyph_pos_h_b, c, 800, 600) { + draw_gm(c, 0.0f, SkPaint::kStrokeAndFill_Style); } -static GM* GlyphPosHairlineStrokeFactory(void*) { - return new GlyphPosGM(0.0f, SkPaint::kStroke_Style); +DEF_SIMPLE_GM(glyph_pos_n_b, c, 800, 600) { + draw_gm(c, 1.2f, SkPaint::kStrokeAndFill_Style); } -static GM* GlyphPosStrokeFactory(void*) { - return new GlyphPosGM(1.2f, SkPaint::kStroke_Style); +DEF_SIMPLE_GM(glyph_pos_h_s, c, 800, 600) { + draw_gm(c, 0.0f, SkPaint::kStroke_Style); } -static GM* GlyphPosHairlineFillFactory(void*) { - return new GlyphPosGM(0.0f, SkPaint::kFill_Style); +DEF_SIMPLE_GM(glyph_pos_n_s, c, 800, 600) { + draw_gm(c, 1.2f, SkPaint::kStroke_Style); } -static GM* GlyphPosFillFactory(void*) { - return new GlyphPosGM(1.2f, SkPaint::kFill_Style); +DEF_SIMPLE_GM(glyph_pos_h_f, c, 800, 600) { + draw_gm(c, 0.0f, SkPaint::kFill_Style); } - -static GMRegistry reg1(GlyphPosHairlineStrokeAndFillFactory); -static GMRegistry reg2(GlyphPosStrokeAndFillFactory); -static GMRegistry reg3(GlyphPosHairlineStrokeFactory); -static GMRegistry reg4(GlyphPosStrokeFactory); -static GMRegistry reg5(GlyphPosHairlineFillFactory); -static GMRegistry reg6(GlyphPosFillFactory); - - +DEF_SIMPLE_GM(glyph_pos_n_f, c, 800, 600) { + draw_gm(c, 1.2f, SkPaint::kFill_Style); } -- cgit v1.2.3