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

feat(primitives): SVG parser/renderer #893

Merged
merged 43 commits into from
Jun 12, 2020

Conversation

glennsl
Copy link
Member

@glennsl glennsl commented Jun 4, 2020

2020-06-04-234927_800x480_scrot
2020-06-04-234937_800x480_scrot

This adds a new SVG project that implements parser and renderer for a decent part of the SVG spec. It also adds an SVGString component and an SVG Example with a bunch of SVG examples to demo and test. Not all of them work, and some have been duplicated and tweaked a bit to work around missing features, but these are good test cases for future development.

Here's a selective list of features that have been implemented, and that IMO would be good additions to have, for anyone wanting to contribute:

  • Geometry
    • <circle>
    • <ellipse>
    • <line>
    • <path>
      • M (Move To, absolute)
      • m (Move To, relative)
      • L (Line To, absolute)
      • l (Line To, relative)
      • H (Horizontal Line, absolute)
      • h (Horizontal Line, relative)
      • V (Vertical Line, absolute)
      • v (Vertical Line, relative)
      • C (Cubic Bezier Curve, absolute)
      • c (Cubic Bezier Curve, relative)
      • S (Cubic Bezier Curve, implied start control point, absolute)
        • Following C
        • Following c
        • Following S
        • Following s
      • s (Cubic Bezier Curve, implied start control point, relative)
        • Following C
        • Following c
        • Following S
        • Following s
      • Q (Quadratic Bezier Curve, absolute)
      • q (Quadratic Bezier Curve, relative)
      • T (Quadratic Bezier Curve, implied control point, absolute)
        • Following Q
        • Following q
        • Following T
        • Following t
      • t (Quadratic Bezier Curve, implied control point, relative)
        • Following Q
        • Following q
        • Following T
        • Following t
      • A (Elliptical Arc Curve, absolute)
      • a (Elliptical Arc Curve, relative)
      • Z, z (Close Path)
      • argument chaining/implied commands
    • <polygon>
    • <polyline>
    • <rect>
      • rounded corners
  • Text
  • <g>
  • <defs>
  • <use>
  • Paint
    • Color
      • Hex
      • Named colors
      • functions (rgb(...))
      • currentColor
    • funciri (i.e. references)
    • inherit
    • gradients
  • Presentation attributes
    • fill
    • stroke
    • stroke-width
    • stroke-*
    • fill-*
    • opacity
    • ...
  • Style
  • Units
    • User coordinates (i.e. unitless)
    • em
    • ex
    • px
    • in
    • cm
    • mm
    • pt
    • pc
    • %
    • auto (element-specific behaviour, partly implemented)
  • Transforms
    • viewBox
    • preserveAspectRatio
    • rotate
    • translate
    • scale
    • skew
    • matrix

@@ -23,7 +23,7 @@ module Sample = {
<View style=containerStyle>
<Canvas
style=outerBox
render={canvasContext => {
render={(canvasContext, _dimensions) => {
Copy link
Member Author

Choose a reason for hiding this comment

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

Breaking change: The dimensions of the Canvas element is now passed into the render function.

Comment on lines +31 to +33
| `pc(_) => failwith("TODO - userCoord")
| `percentage(_) => failwith("TODO - userCoord percentage")
| `auto => failwith("TODO - userCoord auto");
Copy link
Member Author

Choose a reason for hiding this comment

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

A lot of cases in the parser and renderer currently raise Failures. This is very useful during development to catch unsupported features, and it's often also not obvious what a reasonable default would be. These exceptions are also caught in the SVGString component and logged, so they shouldn't crash the app at least. The final implementation should ideally not raise any exceptions though.

Comment on lines -250 to +305
type arcSize =
| Small
| Large;
type arcSize = [ | `small | `large];

let arcSize =
let arcSize: T.typ(arcSize) =
skiaCEnum(
"sk_path_arc_size_t",
[
(Small, "SMALL_SK_PATH_ARC_SIZE"),
(Large, "LARGE_SK_PATH_ARC_SIZE"),
(`small, "SMALL_SK_PATH_ARC_SIZE"),
(`large, "LARGE_SK_PATH_ARC_SIZE"),
],
);

type pathDirection =
| Clockwise
| CounterClockwise;
type pathDirection = [ | `cw | `ccw];

let pathDirection =
let pathDirection: T.typ(pathDirection) =
skiaCEnum(
"sk_path_direction_t",
[
(Clockwise, "CW_SK_PATH_DIRECTION"),
(CounterClockwise, "CCW_SK_PATH_DIRECTION"),
],
[(`cw, "CW_SK_PATH_DIRECTION"), (`ccw, "CCW_SK_PATH_DIRECTION")],
Copy link
Member Author

Choose a reason for hiding this comment

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

Note these breaking changes to the Skia API, because I couldn't figure out how to access the variant constructors without relying on type inference. I also think polymorphic variants are more convenient and natural for variants that are just used as flag/enum arguments.

@glennsl
Copy link
Member Author

glennsl commented Jun 12, 2020

Could someone approve this so I can get it merged to keep it from going stale again? It doesn't really interfere with much else, so it's very low risk.

@Et7f3
Copy link
Member

Et7f3 commented Jun 12, 2020

The TODO list isn't completed ? DO you want I review it ?

@glennsl
Copy link
Member Author

glennsl commented Jun 12, 2020

It's not really a TODO list, but rather a list of what's been implemented and what's not. If you'd like to review it that'd be great!

src/UI/CanvasNode.re Show resolved Hide resolved
examples/SVGExample.re Outdated Show resolved Hide resolved
src/SVG/SimpleXml.re Outdated Show resolved Hide resolved
src/SVG/Model.re Outdated Show resolved Hide resolved
glennsl and others added 4 commits June 12, 2020 20:09
Co-authored-by: Et7f3 <cadeaudeelie@gmail.com>
Co-authored-by: Et7f3 <cadeaudeelie@gmail.com>
@glennsl glennsl merged commit 5912a7b into revery-ui:master Jun 12, 2020
@glennsl glennsl deleted the feat/primitives/svg branch June 12, 2020 20:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants