Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable line join styles and miter limit. #3777

Merged
merged 9 commits into from
Jun 16, 2017
Merged

Conversation

gspencergoog
Copy link
Contributor

This fixes flutter/flutter#7199 and also enables setting of the miter limit.

A separate pull request for the flutter/flutter repo will enable it there once this is committed.

This adds StrokeJoin as a type, and the Paint.strokeJoin property, so that the caller can set the join type for paths and rectangles. Canvas.drawPoints is unaffected by this property (unfortunately, in my opinion) when drawing points as lines.

@gspencergoog
Copy link
Contributor Author

FYI, The associated flutter/flutter PR is flutter/flutter#10742

@@ -271,6 +271,7 @@ enum FilterQuality {

/// Styles to use for line endings.
///
/// Must be in the same order as the SkPaint::Cap enum.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This shouldn't be a dartdoc, it won't make sense to our users. It should be a regular comment (and indeed there already is such a comment two lines later).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Duh. :-P

@@ -286,6 +287,26 @@ enum StrokeCap {
square,
}

/// Styles to use for line joins.
///
/// Note that this only affects line joins for polygons drawn by
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, noted. :-) Sorry, I did read that, I just forgot.

/// [Canvas.drawPath] and rectangles, not points drawn as lines with
/// [Canvas.drawPoints].
///
/// Must be in the same order as the SkPaint::Join enum.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this should be a regular comment, matching the style of the other enums here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

/// line segments to give a beveled appearance.
bevel,
}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

these docs are great.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

/// The limit for miters to be drawn on segments when the join is set to
/// [StrokeJoin.miter] and the [style] is set to [PaintingStyle.stroke]. If
/// this limit is exceeded, then a [StrokeJoin.bevel] join will be drawn
/// instead. Note that this may cause some 'popping' of the corners of a
Copy link
Contributor

@Hixie Hixie Jun 15, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: single space after period, not double
avoid "Note that" (in this case you can probably replace ". Note that" with just a semicolon).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

///
/// Defaults to zero, which means that a reasonable value will be picked
/// instead of using zero as the limit, which would draw a bevel all the
/// time.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add some discussion about what the "reasonable value" is based on?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, it's actually always 4. But I didn't want to give a specific value, since it's defined in Skia, and might change. Well, probably not, but still, it could theoretically change.

https://github.com/google/skia/blob/master/src/core/SkPaintDefaults.h#L32

In thinking about this, though, if zero is specified for the miter limit, then we don't set the value in Flutter, and Skia substitutes 4.0 sort of magically and unpredictably.

So I changed it: Flutter now has its own default value (4.0, duh. :-)) and then I check the passed value against 4.0 in C++ to decide whether or not we actually set it. I assume it's not OK to include SkPaintDefaults.h directly, since it's not a public header (because then I'd compare it against SkPaintDefaults_MiterLimit directly).

This makes the whole thing more "normal" in my mind: it has a valid, documentable default that's 4.0, and you actually can set a miter limit of zero which you couldn't before (which basically means do bevel all the time), and the C++ code is using the actual Skia default to decide if it should actually set it on the SkPaint, so it's still efficient.

I also updated the two other "magic" defaults in there (color and blend mode) to use constants so they could be better documented.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cc @abarth who may have opinions here.
Maybe Skia should expose these constants? It seems like a valid use case.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, Skia should probably document the default.

if (strokeJoin != StrokeJoin.miter)
result.write(' $strokeJoin');
if (strokeMiterLimit != 0.0)
result.write(' $strokeMiterLimit');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just the number on its own will be weird. I would recommend:

      if (strokeJoin == StrokeJoin.miter) {
        if (strokeMiterLimit != 0.0)
          result.write(' $strokeJoin up to $strokeMiterLimit');
      } else {
        result.write(' $strokeJoin');
      }

...or some such.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

@Hixie
Copy link
Contributor

Hixie commented Jun 15, 2017

LGTM

@gspencergoog
Copy link
Contributor Author

Ian, please take another look at the defaults changes, and then commit if you like it.

Thanks for the review!

@@ -552,20 +615,26 @@ class Paint {
result.write(' hairline');
if (strokeCap != StrokeCap.butt)
result.write(' $strokeCap');
if (strokeJoin == StrokeJoin.miter) {
if (strokeMiterLimit != _kStrokeMiterLimitDefault)
result.write('$strokeJoin up to $strokeMiterLimit');
Copy link
Contributor

@Hixie Hixie Jun 16, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

While we're here (and sorry to make you change this oven and over), can we actually make a slight tweak to this?
Printing doubles ends up with ugly strings of digits. What we normally do is print them to 1dp.

This would mean changing the strokeWidth case above to:

         result.write(' ${strokeWidth.toStringAsFixed(1)}');

...and this case to:

          result.write(' $strokeJoin up to ${strokeMiterLimit.toStringAsFixed(1)}');

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

also I just noticed you're missing a space before $strokeJoin.

Copy link
Contributor Author

@gspencergoog gspencergoog Jun 16, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good idea, and good catch on the space. Done. Also, no problem about changing it until it's right.

@@ -375,6 +398,7 @@ class Paint {
_data.setInt32(_kIsAntiAliasOffset, encoded, _kFakeHostEndian);
}

// Must be kept in sync with the default in paint.cc
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nitty nit nit: You don't have a period here at the end of the comment, but you do in the C++ comment. I'd add a period here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

@@ -399,6 +423,7 @@ class Paint {
_data.setInt32(_kColorOffset, encoded, _kFakeHostEndian);
}

// Must be kept in sync with the default in paint.cc
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok i feel like i'm just yanking your chain now but you put the period on the comment above and didn't here...

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LOL. I also found another one, just to spare you the task of commenting on each and every one. [Sorry, I should have done a search the first time.]

@Hixie
Copy link
Contributor

Hixie commented Jun 16, 2017

Thanks :-)

Will land on green.

@Hixie Hixie merged commit 5403f65 into flutter:master Jun 16, 2017
ianloic added a commit that referenced this pull request Jun 16, 2017
ianloic added a commit that referenced this pull request Jun 16, 2017
…3785)

* Revert "Enable line join styles and miter limit. (#3777)"

This reverts commit 5403f65.

* Revert "Revert "Update switches to use StringView." (#3784)"

This reverts commit 80f039f.

* Revert "Initial integration of libtxt with Flutter alongside Blink. (#3771)"

This reverts commit c548c65.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
4 participants