Portable binary image export for diagrams.mbt generated SVG and playback
///|
test "PNG is the default still format" {
let svg = "<svg width=\"32\" height=\"16\"><rect x=\"4\" y=\"4\" width=\"8\" height=\"8\" fill=\"red\"/></svg>"
let result = @raster.rasterize(
svg,
options=@raster.StillOptions::new(size=Width(128)),
)
assert_eq(result.diagnostics.length(), 0)
let image = result.image.unwrap()
assert_eq(image.media_type, "image/png")
assert_eq(image.width, 128)
assert_eq(image.height, 64)
}///|
test "JPEG requires an explicit opaque background" {
let svg = "<svg width=\"32\" height=\"16\"><rect width=\"8\" height=\"8\" fill=\"red\"/></svg>"
let result = @raster.rasterize(
svg,
options=@raster.StillOptions::new(
format=Jpeg(quality=85),
background=@raster.Rgba::white(),
theme=Light,
),
)
assert_eq(result.diagnostics.length(), 0)
assert_eq(result.image.unwrap().media_type, "image/jpeg")
}///|
test "export a bounded source-bound animation" {
let view = @swimlane.Swimlane::new("review", [
@swimlane.Node::new("A"), @swimlane.Node::new("B"),
], [@swimlane.Connection::new("AB", "A", "B")])
let scene = @svg.SvgRenderer::new().scene(@render.Diagram::Swimlane(view)).scene.unwrap()
let key = scene.catalog.connection("AB").unwrap().key
let plan = @animation.PlaybackPlan::scenario(scene.catalog,
@animation.Motion::serial([Step(key, 100L), Step(key, 100L)]),
).plan.unwrap()
let result = @raster.export_gif(scene, plan,
options=@raster.GifOptions::new(size=Width(64), frame_rate=10, loop_behavior=Once),
)
assert_eq(result.image.unwrap().media_type, "image/gif")
}pub(all) struct FontFace {
family : String
weight : Int
italic : Bool
data : Bytes
}pub(all) struct FontSet {
faces : ReadOnlyArray[FontFace]
fallback_families : ReadOnlyArray[String]
}fn FontSet::subset_for_svg(self : FontSet, source : String, limits? : RasterLimits) -> FontSubsetResultpub struct GifFrameSample {
time_ms : Int64
delay_centiseconds : Int
sample : Sample
occurrences : ReadOnlyArray[GifOccurrence]
}pub(all) struct GifOptions {
size : RasterSize
background : Rgba
theme : ExportTheme
interval : GifInterval
frame_rate : Int
loop_behavior : GifLoop
} derive(Eq, Debug)fn GifOptions::new(size? : RasterSize, background? : Rgba, theme? : ExportTheme, interval? : GifInterval, frame_rate? : Int, loop_behavior? : GifLoop) -> GifOptionspub struct GifSampler {
frame_count : Int
width : Int
height : Int
options : GifOptions
// private fields
}fn GifSampler::new(scene : Scene, plan : PlaybackPlan, options? : GifOptions, limits? : RasterLimits) -> GifSamplerResultpub(all) enum GifTarget {
Connection(id~ : String, path~ : String, mask~ : String?, point~ : ConnectorPoint)
Visit(NodeBindingKind)
}pub(all) struct RasterImage {
bytes : Bytes
media_type : String
width : Int
height : Int
format : RasterFormat
}fn Rasterizer::export_gif_data(self : Rasterizer, data : Json, plan? : Json, options? : GifOptions) -> RasterResultfn Rasterizer::rasterize(self : Rasterizer, source : String, options? : StillOptions) -> RasterResultpub(all) struct StillOptions {
format : StillFormat
size : RasterSize
background : Rgba
theme : ExportTheme
} derive(Eq, Debug)fn StillOptions::new(format? : StillFormat, size? : RasterSize, background? : Rgba, theme? : ExportTheme) -> StillOptionsfn export_gif(scene : Scene, plan : PlaybackPlan, options? : GifOptions, fonts? : FontSet, limits? : RasterLimits) -> RasterResultfn export_gif_data(data : Json, plan? : Json, options? : GifOptions, fonts? : FontSet, limits? : RasterLimits) -> RasterResultfn rasterize(source : String, options? : StillOptions, fonts? : FontSet, limits? : RasterLimits) -> RasterResult