svg

MoonBit SVG eDSL

svg
Download zip
Author
Version
0.1.2
License
Apache-2.0
Last updated
11 months ago
Downloads
35

Dependencies

#SVG

A MoonBit SVG embedded Domain-Specific Language (eDSL) for generating SVG markup programmatically.

#Overview

This library provides a clean, type-safe way to generate SVG content in MoonBit. It offers:

  • Attribute handling through an enum-based approach (Key["value"])
  • Path generation utilities
  • SVG transformation functions
  • Easy composition with the + operator

#Installation

moon add CAIMEOX/svg

#Usage

// Create a red rectangle
let rect = @svg.rect([
@svg.Width["100"],
@svg.Height["50"],
@svg.Fill["red"]
])

// Create a circle
let circle = @svg.circle([
@svg.Cx["50"],
@svg.Cy["50"],
@svg.R["25"],
@svg.Fill["blue"]
])

// Combine elements
let combined = rect + circle

Complete SVG Document

// Create a complete SVG document
fn create_svg() -> @svg.Element {
let content = @svg.rect([@svg.Width["100%"], @svg.Height["100%"], @svg.Fill["#f0f0f0"]]) +
@svg.circle([@svg.Cx["50%"], @svg.Cy["50%"], @svg.R["40%"], @svg.Fill["#3355cc"]])

@svg.doc_type() +
@svg.svg11(content).with_attr([
@svg.Version["1.1"],
@svg.Width["300"],
@svg.Height["200"],
@svg.ViewBox["0 0 100 100"]
])
}

#Path Construction

The library provides functions for SVG path data:

// Create a triangular path
let triangle = @svg.path([
@svg.Fill["yellow"],
@svg.Stroke["black"],
@svg.StrokeWidth["2"],
@svg.D[@svg.ma(10, 10) + @svg.la(50, 10) + @svg.la(30, 40) + @svg.z()]
])

#Comprehensive Example

Here's a more complete example that creates a simple logo:

fn sample_logo() -> @svg.Element {
// Create multiple path elements to form a logo
@svg.path([
@svg.Fill["#3366CC"],
@svg.D[@svg.ma(0, 50) + @svg.la(25, 0) + @svg.la(50, 50) + @svg.la(25, 100) + @svg.la(0, 50) + @svg.z()]
]) +
@svg.path([
@svg.Fill["#6633CC"],
@svg.D[@svg.ma(50, 50) + @svg.la(75, 0) + @svg.la(100, 50) + @svg.la(75, 100) + @svg.la(50, 50) + @svg.z()]
]) +
@svg.circle([
@svg.Cx["50"],
@svg.Cy["50"],
@svg.R["15"],
@svg.Fill["#CC3366"]
])
}

// Wrap with SVG document structure
fn create_logo_svg() -> @svg.Element {
@svg.doc_type() +
@svg.svg11(sample_logo()).with_attr([
@svg.Version["1.1"],
@svg.Width["200"],
@svg.Height["200"],
@svg.ViewBox["0 0 100 100"]
])
}

More examples can be found in the render_test.mbt file.

RealFloat

pub(open) trait RealFloat :
Num
+ Show {
}

Attribute

type Attribute

impl Eq for Attribute
impl Hash for Attribute
impl Show for Attribute

Element

type Element

impl Add for Element
impl Show for Element

Element::doc_type

fn Element::doc_type(name : String) -> Element

Element::make

fn Element::make(self : Element, name : String) -> Element

Element::make_empty

fn Element::make_empty(name : String) -> Element

Element::new

fn Element::new(run : (
HashMap
[String, String]) -> String) -> Element

Element::str

fn Element::str(str : String) -> Element

Element::svg11

fn Element::svg11(self : Element) -> Element

Element::with_attr

fn Element::with_attr(self : Element, attr : Array[Attribute]) -> Element

Tag

pub(all) enum Tag {
AccentHeight
Accumulate
Additive
AlignmentBaseline
Alphabetic
Amplitude
ArabicForm
Ascent
AttributeName
AttributeType
Azimuth
BaseFrequency
Baseprofile
BaselineShift
Bbox
Begin
Bias
By
CalcMode
CapHeight
Class
Clip
ClipPath
ClipRule
ClipPathUnits
Color
ColorInterpolation
ColorInterpolationFilters
ColorProfile
ColorRendering
ContentScriptType
ContentStyleType
Cursor
Cx
Cy
D
Descent
DiffuseConstant
Direction
Display
Divisor
DominantBaseline
Dur
Dx
Dy
EdgeMode
Elevation
EnableBackground
End
Exponent
ExternalResourcesRequired
Fill
FillOpacity
FillRule
Filter
FilterRes
FilterUnits
FloodColor
FloodOpacity
FontFamily
FontSize
FontSizeAdjust
FontStretch
FontStyle
FontVariant
FontWeight
Format
From
Fx
Fy
G1
G2
GlyphName
GlyphOrientationHorizontal
GlyphOrientationVertical
GradientTransform
GradientUnits
Hanging
Height
HorizAdvX
HorizOriginX
HorizOriginY
Id
Ideographic
ImageRendering
In
In2
Intercept
K
K1
K2
K3
K4
KernelMatrix
KernelUnitLength
Kerning
KeyPoints
KeySplines
KeyTimes
Lang
LengthAdjust
LetterSpacing
LightingColor
LimitingConeAngle
Local
MarkerEnd
MarkerMid
MarkerStart
MarkerHeight
MarkerUnits
MarkerWidth
MaskContentUnits
MaskUnits
Mathematical
Max
Media
Method
Min
Mode
Name
NumOctaves
Offset
Onabort
Onactivate
Onbegin
Onclick
Onend
Onerror
Onfocusin
Onfocusout
Onload
Onmousedown
Onmousemove
Onmouseout
Onmouseover
Onmouseup
Onrepeat
Onresize
Onscroll
Onunload
Onzoom
Opacity
Operator
Order
Orient
Orientation
Origin
Overflow
OverlinePosition
OverlineThickness
Panose1
PaintOrder
Path
PathLength
PatternContentUnits
PatternTransform
PatternUnits
PointerEvents
Points
PointsAtX
PointsAtY
PointsAtZ
PreserveAlpha
PreserveAspectRatio
PrimitiveUnits
R
Radius
RefX
RefY
RenderingIntent
RepeatCount
RepeatDur
RequiredExtensions
RequiredFeatures
Restart
Result
Rotate
Rx
Ry
Scale
Seed
ShapeRendering
Slope
Spacing
SpecularConstant
SpecularExponent
SpreadMethod
StartOffset
StdDeviation
Stemh
Stemv
StitchTiles
StopColor
StopOpacity
StrikethroughPosition
StrikethroughThickness
String
Stroke
StrokeDasharray
StrokeDashoffset
StrokeLinecap
StrokeLinejoin
StrokeMiterlimit
StrokeOpacity
StrokeWidth
Style
SurfaceScale
SystemLanguage
TableValues
Target
TargetX
TargetY
TextAnchor
TextDecoration
TextRendering
TextLength
To
Transform
Type
U1
U2
UnderlinePosition
UnderlineThickness
Unicode
UnicodeBidi
UnicodeRange
UnitsPerEm
VAlphabetic
VHanging
VIdeographic
VMathematical
Values
Version
VertAdvY
VertOriginX
VertOriginY
ViewBox
ViewTarget
Visibility
Width
Widths
WordSpacing
WritingMode
X
XHeight
X1
X2
XChannelSelector
XlinkActuate
XlinkArcrole
XlinkHref
XlinkRole
XlinkShow
XlinkTitle
XlinkType
XmlBase
XmlLang
XmlSpace
Y
Y1
Y2
YChannelselector
Z
ZoomAndPan
}

impl Eq for Tag
impl Hash for Tag
impl Show for Tag

Tag::op_get

fn Tag::op_get(self : Tag, att : String) -> Attribute

fn a(attrs : Array[Attribute], child? : Element) -> Element

a element

animate

fn animate(attrs : Array[Attribute], child? : Element) -> Element

animate element

animateMotion

fn animateMotion(attrs : Array[Attribute], child? : Element) -> Element

animateMotion element

animateTransform

fn animateTransform(attrs : Array[Attribute], child? : Element) -> Element

animateTransform element

circle

fn circle(attrs : Array[Attribute], child? : Element) -> Element

circle element

clipPath

fn clipPath(attrs : Array[Attribute], child? : Element) -> Element

clipPath element or attribute

colorProfile

fn colorProfile(attrs : Array[Attribute], child? : Element) -> Element

color-profile element

cursor

fn cursor(attrs : Array[Attribute], child? : Element) -> Element

cursor element

defs

fn defs(attrs : Array[Attribute], child? : Element) -> Element

defs element

desc

fn desc(attrs : Array[Attribute], child? : Element) -> Element

desc element

doc_type

fn doc_type() -> Element

SVG element creation functions

ellipse

fn ellipse(attrs : Array[Attribute], child? : Element) -> Element

ellipse element

feBlend

fn feBlend(attrs : Array[Attribute], child? : Element) -> Element

feBlend element

feColorMatrix

fn feColorMatrix(attrs : Array[Attribute], child? : Element) -> Element

feColorMatrix element

feComponentTransfer

fn feComponentTransfer(attrs : Array[Attribute], child? : Element) -> Element

feComponentTransfer element

feComposite

fn feComposite(attrs : Array[Attribute], child? : Element) -> Element

feComposite element

feConvolveMatrix

fn feConvolveMatrix(attrs : Array[Attribute], child? : Element) -> Element

feConvolveMatrix element

feDiffuseLighting

fn feDiffuseLighting(attrs : Array[Attribute], child? : Element) -> Element

feDiffuseLighting element

feDisplacementMap

fn feDisplacementMap(attrs : Array[Attribute], child? : Element) -> Element

feDisplacementMap element

feDistantLight

fn feDistantLight(attrs : Array[Attribute], child? : Element) -> Element

feDistantLight element

feFlood

fn feFlood(attrs : Array[Attribute], child? : Element) -> Element

feFlood element

feFuncA

fn feFuncA(attrs : Array[Attribute], child? : Element) -> Element

feFuncA element

feFuncB

fn feFuncB(attrs : Array[Attribute], child? : Element) -> Element

feFuncB element

feFuncG

fn feFuncG(attrs : Array[Attribute], child? : Element) -> Element

feFuncG element

feFuncR

fn feFuncR(attrs : Array[Attribute], child? : Element) -> Element

feFuncR element

feGaussianBlur

fn feGaussianBlur(attrs : Array[Attribute], child? : Element) -> Element

feGaussianBlur element

feImage

fn feImage(attrs : Array[Attribute], child? : Element) -> Element

feImage element

feMerge

fn feMerge(attrs : Array[Attribute], child? : Element) -> Element

feMerge element

feMergeNode

fn feMergeNode(attrs : Array[Attribute], child? : Element) -> Element

feMergeNode element

feMorphology

fn feMorphology(attrs : Array[Attribute], child? : Element) -> Element

feMorphology element

feOffset

fn feOffset(attrs : Array[Attribute], child? : Element) -> Element

feOffset element

fePointLight

fn fePointLight(attrs : Array[Attribute], child? : Element) -> Element

fePointLight element

feSpecularLighting

fn feSpecularLighting(attrs : Array[Attribute], child? : Element) -> Element

feSpecularLighting element

feSpotLight

fn feSpotLight(attrs : Array[Attribute], child? : Element) -> Element

feSpotLight element

feTile

fn feTile(attrs : Array[Attribute], child? : Element) -> Element

feTile element

feTurbulence

fn feTurbulence(attrs : Array[Attribute], child? : Element) -> Element

feTurbulence element

filter

fn filter(attrs : Array[Attribute], child? : Element) -> Element

filter element

font

fn font(attrs : Array[Attribute], child? : Element) -> Element

font element

fontFace

fn fontFace(attrs : Array[Attribute], child? : Element) -> Element

font-face element

fontFaceFormat

fn fontFaceFormat(attrs : Array[Attribute]) -> Element

font-face-format element

fontFaceName

fn fontFaceName(attrs : Array[Attribute]) -> Element

font-face-name element

fontFaceSrc

fn fontFaceSrc(attrs : Array[Attribute], child? : Element) -> Element

font-face-src element

fontFaceUri

fn fontFaceUri(attrs : Array[Attribute], child? : Element) -> Element

font-face-uri element

foreignObject

fn foreignObject(attrs : Array[Attribute], child? : Element) -> Element

foreignObject element
fn g(attrs : Array[Attribute], child? : Element) -> Element

g element

glyph

fn glyph(attrs : Array[Attribute], child? : Element) -> Element

glyph element or attribute

glyphRef

fn glyphRef(attrs : Array[Attribute]) -> Element

glyphRef element

hkern

fn hkern(attrs : Array[Attribute]) -> Element

hkern element

image

fn image(attrs : Array[Attribute], child? : Element) -> Element

image element

line

fn line(attrs : Array[Attribute], child? : Element) -> Element

line element

linearGradient

fn linearGradient(attrs : Array[Attribute], child? : Element) -> Element

linearGradient element

marker

fn marker(attrs : Array[Attribute], child? : Element) -> Element

marker element

mask

fn mask(attrs : Array[Attribute], child? : Element) -> Element

mask element or attribute

metadata

fn metadata(attrs : Array[Attribute], child? : Element) -> Element

metadata element

missingGlyph

fn missingGlyph(attrs : Array[Attribute], child? : Element) -> Element

missing-glyph element

mpath

fn mpath(attrs : Array[Attribute], child? : Element) -> Element

mpath element

new

fn new(attr : String, value : String) -> Attribute

path

fn path(attrs : Array[Attribute], child? : Element) -> Element

path element

pattern

fn pattern(attrs : Array[Attribute], child? : Element) -> Element

pattern element

polygon

fn polygon(attrs : Array[Attribute], child? : Element) -> Element

polygon element

polyline

fn polyline(attrs : Array[Attribute], child? : Element) -> Element

polyline element

radialGradient

fn radialGradient(attrs : Array[Attribute], child? : Element) -> Element

radialGradient element

rect

fn rect(attrs : Array[Attribute], child? : Element) -> Element

rect element

script

fn script(attrs : Array[Attribute], child? : Element) -> Element

script element

set

fn set(attrs : Array[Attribute], child? : Element) -> Element

set element

stop

fn stop(attrs : Array[Attribute], child? : Element) -> Element

stop element

style

fn style(attrs : Array[Attribute], child? : Element) -> Element

style element

svg

fn svg(attrs : Array[Attribute], child? : Element) -> Element

svg element

switch

fn switch(attrs : Array[Attribute], child? : Element) -> Element

switch element

symbol

fn symbol(attrs : Array[Attribute], child? : Element) -> Element

symbol element

term

fn term(name : String, arr : Array[Attribute], child? : Element) -> Element

text

fn text(attrs : Array[Attribute], child? : Element) -> Element

text element

textPath

fn textPath(attrs : Array[Attribute], child? : Element) -> Element

textPath element

title

fn title(attrs : Array[Attribute], child? : Element) -> Element

title element

tref

fn tref(attrs : Array[Attribute], child? : Element) -> Element

tref element

tspan

fn tspan(attrs : Array[Attribute], child? : Element) -> Element

tspan element

use_

fn use_(attrs : Array[Attribute], child? : Element) -> Element

use element

view

fn view(attrs : Array[Attribute], child? : Element) -> Element

view element

vkern

fn vkern(attrs : Array[Attribute]) -> Element

vkern element
fn z() -> String

closepath