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

Add support for SVG path segment formats, document the path function #79

Open
wants to merge 2 commits into
base: feature/no-org
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 31 additions & 2 deletions src/thi/ng/geom/svg/core.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -52,15 +52,19 @@
(repeat n)
(interpose " "))))

;; TODO add missing path segment types (Q, T)

(def path-segment-formats
{:M ["M" *fmt-vec* " "]
:m ["m" *fmt-vec* " "]
:L ["L" *fmt-vec* " "]
:l ["l" *fmt-vec* " "]
:C ["C" *fmt-vec* " " *fmt-vec* " " *fmt-vec* " "]
:c ["c" *fmt-vec* " " *fmt-vec* " " *fmt-vec* " "]
:Q ["Q" *fmt-vec* " " *fmt-vec* " "]
:q ["q" *fmt-vec* " " *fmt-vec* " "]
:S ["S" *fmt-vec* " " *fmt-vec* " "]
:s ["s" *fmt-vec* " " *fmt-vec* " "]
:T ["T" *fmt-vec* " "]
:t ["t" *fmt-vec* " "]
:A ["A" *fmt-vec* " " *ff* " " str " " str " " *fmt-vec* " "]
:a ["a" *fmt-vec* " " *ff* " " str " " str " " *fmt-vec* " "]
:Z ["Z"]
Expand Down Expand Up @@ -189,6 +193,31 @@
(into [:g (svg-attribs attribs nil)] body))

(defn path
"Represents an SVG path element.
`segments` is a sequence of path segments, in the format
[<command> & args]
that corresponds to the SVG path language, eg.

(path [[:M [0 0]] [:l [20 30]] [:Z]]
{:fill \"red\"})

For reference, the available commands are as follows.
Each has an uppercase (absolute coordinates)
and lowercase (relative coordinates) variant.

| Command | Name | Arguments |
|---------+--------------------------+-------------------------------------------------------|
| M/m | moveto | [pt] |
| L/l | lineto | [pt] |
| H/h | horizontal lineto | [x] |
| V/v | vertical lineto | [y] |
| C/c | cubic curveto | [pt1 pt2 endpt] |
| S/s | smooth cubic curveto | [pt2 endpt] |
| Q/q | quadratic curveto | [pt1 endpt] |
| T/t | smooth quadratic curveto | [endpt] |
| A/a | elliptical arc | [rx ry x-axis-rotation large-arc-flag sweep-flag x y] |
| Z/z | closepath | [] |
"
([segments]
(path segments nil))
([segments attribs]
Expand Down