An SVG toolkit based on resvg

Overview

rusty-svg

npm package build status

An SVG toolkit based on resvg

This module is compiled to WASM and currently only supports Node.js

Comparing with the backend ReSVG, this module removes text support as it requires complex font loading logic.

Example

Run the following command, it will convert tiger.svg to tiger.png

node tests/index.js
SVG PNG
Tiger.svg Tiger.png
Comments
  • 缺少 xmlns:xlink 命名空间时报错

    缺少 xmlns:xlink 命名空间时报错

    <svg xmlns="http://www.w3.org/2000/svg" id="svg" width="512" height="512" viewBox="0 0 8 8">
        <defs>
            <path id="a" d="M1,1 2,1 2,4 1,4 z" />
        </defs>
        <clipPath id="s">
            <use xlink:href="#a" />
        </clipPath>
        <g clip-path="url(#s)">
            <rect x="0" y="1" width="3" height="2" />
        </g>
    </svg>
    

    当存在 xlink:href 属性时,必须在 <svg> 中添加 xmlns:xlink="http://www.w3.org/1999/xlink"

    opened by yisibl 2
  • 基于 bounding box 设置 SVG 宽高

    基于 bounding box 设置 SVG 宽高

    当前生成的 PNG 效果如下图:

    image

    期望其最终效果是宽度填满,高度等比缩放。也就是说拿到蓝色框的宽高,再根据指定的宽度等比缩放。 image

    示例地址:https://www.iconfont.cn/illustrations/detail?spm=a313x.7781069.1998910419.d9df05512&cid=31456 SVG: https://iconfont.alicdn.com/s/e3430542-d8ce-4a0b-aa2b-91c08ec81b9e_origin.svg

    opened by yisibl 2
  • feat: performance improvement

    feat: performance improvement

    通过升级 resvg 0.15.0 性能上有很大的提升,这个 PR 通过优化编译参数,进一步提升性能,但牺牲了一些编译时间和增加 wasm 文件体积。

    Fixes #12

    wasm-pack build --release --target nodejs --out-name index --out-dir pkg
    

    在生成图片尺寸为 900px .render(900) 时的测试结果如下:

    测试 SVG: https://iconfont.alicdn.com/t/fde3869d-4282-4067-9bc8-25328c558f18_origin.svg

    优化方式 | wasm 文件体积 | 生成 PNG 耗时 | 编译时间 -- | -- | -- | -- opt-level = "s", codegen-units = 16 | 1,671,289 字节 | 3391 ms | 11.86s opt-level = 3,codegen-units = 16 | 1,979,951 字节 | 3410 ms | 49.55s opt-level = "s", codegen-units = 1 | 1,636,412 字节 | 2613 ms | 28.10s opt-level = 3, codegen-units = 1 | 1,956,108 字节 | 2272 ms | 16.81s

    这个 PR 选择了第四种方式。

    opened by yisibl 0
  • 带有滤镜的 SVG inner_bbox 不正确

    带有滤镜的 SVG inner_bbox 不正确

    插画库:https://www.iconfont.cn/illustrations/detail?cid=26447 SVG 文件:https://iconfont.alicdn.com/s/3dce29fe-78a1-4bed-b788-04805ceda6cf_origin.svg

    image 阴影底部被裁剪(右)

    opened by yisibl 0
  • to_string() always keep the original color format

    to_string() always keep the original color format

    input:

    <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 100 100">
      <rect fill="yellow" x="40" y="10" width="20" height="20"/>
      <rect fill="red" x="40" y="40" width="20" height="20" />
      <rect fill="blue" x="40" y="70" width="20" height="20" />
    </svg>
    

    output:

    <svg width="100" height="100" viewBox="0 0 100 100" xmlns="http://www.w3.org/2000/svg">
        <defs/>
        <path fill="rgba(255,255,0,255)" stroke="none" d="M 40 10 L 60 10 L 60 30 L 40 30 Z"/>
        <path fill="rgba(255,0,0,255)" stroke="none" d="M 40 40 L 60 40 L 60 60 L 40 60 Z"/>
        <path fill="rgba(0,0,255,255)" stroke="none" d="M 40 70 L 60 70 L 60 90 L 40 90 Z"/>
    </svg>
    

    I also hope that the extra stroke="none" can be removed.

    opened by yisibl 0
  • Apply transform support other element

    Apply transform support other element

    We need to remove the transform from the g element and apply it to the child elements.

    test case

    input:

    <svg width="339px" height="237px" viewBox="0 0 339 237" version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
        <g stroke="none" stroke-width="0" fill="none" fill-rule="evenodd">
            <g transform="translate(8.000000, 8.000000)">
                <rect stroke="#979797" stroke-width="15.21" fill="#F7B9B9" x="0" y="0" width="151" height="93"></rect>
                <ellipse stroke-opacity="0.546274038" stroke="#4D5490" stroke-width="10" fill="#94CFFA" cx="257" cy="49.5" rx="68" ry="49.5"></ellipse>
                <rect stroke="#141414" stroke-width="10" fill="#ddd" opacity="0.696607013" x="49" y="133" width="150" height="90" rx="8"></rect>
                <polygon stroke="#979797" stroke-width="5"  fill="#DDB9FE" points="277 202 248.786308 216.832816 254.174644 185.416408 231.349287 163.167184 262.893154 158.583592 277 130 291.106846 158.583592 322.650713 163.167184 299.825356 185.416408 305.213692 216.832816"></polygon>
            </g>
        </g>
    </svg>
    

    output:

    <svg width="338" height="236" viewBox="0 0 338 236" xmlns="http://www.w3.org/2000/svg">
        <defs/>
        <path fill="rgba(247,185,185,255)" fill-rule="evenodd" stroke="rgba(151,151,151,255)" stroke-width="15.21" d="M 8 8 L 159 8 L 159 101 L 8 101 Z"/>
        <path fill="rgba(148,207,250,255)" fill-rule="evenodd" stroke="rgba(77,84,144,255)" stroke-opacity="0.546274038" stroke-width="10" d="M 333 57.5 C 333 84.83809511662 302.55536298849 107 265 107 C 227.44463701151 107 197 84.83809511662 197 57.5 C 197 30.16190488338 227.44463701151 8 265 8 C 302.55536298849 8 333 30.16190488338 333 57.5 Z"/>
        <g opacity="0.696607013" transform="matrix(1 0 0 1 8 8)">
            <path fill="rgba(221,221,221,255)" fill-rule="evenodd" stroke="rgba(20,20,20,255)" stroke-width="10" d="M 57 133 L 191 133 C 195.41827799865 133 199 136.58172200135 199 141 L 199 215 C 199 219.41827799865 195.41827799865 223 191 223 L 57 223 C 52.58172200135 223 49 219.41827799865 49 215 L 49 141 C 49 136.58172200135 52.58172200135 133 57 133 Z"/>
        </g>
        <path fill="rgba(221,185,254,255)" fill-rule="evenodd" stroke="rgba(151,151,151,255)" stroke-width="5" d="M 285 210 L 256.786308 224.832816 L 262.174644 193.416408 L 239.349287 171.167184 L 270.893154 166.583592 L 285 138 L 299.106846 166.583592 L 330.650713 171.167184 L 307.825356 193.416408 L 313.213692 224.832816 Z"/>
    </svg>
    
    opened by yisibl 0
  • Apply gradient transform

    Apply gradient transform

    <svg id='svg' xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink"
         width="256" height="256"
         viewBox="0 0 24 24">
      <!-- gradientUnits defaults to objectBoundingBox -->
      <radialGradient id="radgrad" gradientTransform="rotate(-60 0.5, 0.5) translate(0, 0.2)">
        <stop offset="5%"  stop-color="red" />
        <stop offset="50%" stop-color="gold" />
        <stop offset="95%"  stop-color="red" />
      </radialGradient>
      <circle id="circ"
              cx="5" cy="12"
              r="5"
              fill="url(#radgrad)" />
      <g transform="rotate(120 15,12)">
        <use xlink:href="#circ" x="10" />
      </g>
    </svg>
    

    before

    image

    after to_string() or render()

    image

    opened by yisibl 2
  • 支持 stroke-dasharray、stroke-linecap、stroke-miterlimit

    支持 stroke-dasharray、stroke-linecap、stroke-miterlimit

    Test case

    <svg viewBox="0 0 30 10" xmlns="http://www.w3.org/2000/svg">
        <line x1="0" y1="9" x2="30" y2="9" stroke="black" stroke-dasharray="4 1 2 3" />
    </svg>
    
    <svg viewBox="0 0 30 10" xmlns="http://www.w3.org/2000/svg">
        <line x1="1" y1="1" x2="5" y2="1" stroke="black" stroke-linecap="butt" />
    </svg>
    

    此外还需要判断一下 opacity 和 stroke-opacity = 0 时,无需执行 OutlineStrokeToFill

    opened by yisibl 1
Owner
Zimon Dai
Rust Dev. Reach me via email
Zimon Dai
🔭 Annict での今期の視聴状況を SVG 画像として出力するサーバ (WIP)

annict-profile-card ?? Annict の視聴状況などを SVG 画像として出力する API サーバ (WIP) Annict GraphQL API を使用しています。

Nep 16 Dec 20, 2022
Convert your ascii diagram scribbles into happy little SVG

Svgbob Svgbob can create a nice graphical representation of your text diagrams. Svgbob provides a cli which takes text as an input and creates an svg

Jovansonlee Cesar 3.4k Dec 25, 2022
The tool to make svg with triangles by length from two points.

The tool to make svg with triangles by length from two points.

null 2 Sep 27, 2021
Convert UFO .glif files to SVG, whether they're part of a font or not

Convert UFO glyph files (.glif) to SVG There exists already an svg2glif, but for some reason not the opposite operation. My MFEKglif editor treats .gl

Modular Font Editor K 3 Apr 26, 2022
A Rust-implementation of devout’s tutorial to create an SVG graphic device using extendr.

A Rust-implementation of devout’s tutorial to create an SVG graphic device using extendr.

Hiroaki Yutani 1 Jan 25, 2022
Use free svg icons in your Dioxus projects easily with dioxus-free-icons.

dioxus-free-icons Use free svg icons in your Dioxus projects easily with dioxus-free-icons. More information about this crate can be found in the crat

Daiki Nishikawa 16 Jan 2, 2023
🎨 Example-based texture synthesis written in Rust 🦀

?? texture-synthesis A light Rust API for Multiresolution Stochastic Texture Synthesis [1], a non-parametric example-based algorithm for image generat

Embark 1.7k Dec 31, 2022
Signed distance field font and image command line tool based on OpenCL.

SDFTool Signed distance field font and image command line tool based on OpenCL. Build Windows Run cargo build --release in Visual Studio developer x64

弦语蝶梦 7 Oct 16, 2022
Rust based breadth first search maze image solver

maze_solver Rust based breadth first search maze image solver Works on black and white images with provided start and end points. Usage: maze_solver

null 0 Jan 31, 2022
Motion detection & video recording software based on OpenCV, built for research on Bumblebees

BombusCV Motion detection & video recording software based on OpenCV, built for research on Bumblebees (hence the name). Index Use case Examples Insta

Marco Radocchia 7 Dec 27, 2022
A high-performance SVG renderer, powered by Rust based resvg and napi-rs.

resvg-js resvg-js is a high-performance SVG renderer, powered by Rust based resvg and napi-rs. Fast, safe and zero dependencies! No need for node-gyp

一丝 744 Jan 7, 2023
SVG to PDF file conversion based on "svg2pdf" and "resvg" Rust projects

pysvg2pdf Blazingly Fast ™️ SVG to PDF file conversion for Python. This project is based on Rust's svg2pdf and resvg projects. The project uses pyo3 a

SuffleWaffle 4 Mar 28, 2024
resvg is an SVG rendering library.

resvg can be used as a Rust library, a C library and as a CLI application to render SVG files based on a static SVG Full 1.1 subset.

Evgeniy Reizner 1.8k Dec 30, 2022
Captcha based on SVG.

BioSvg Captcha based on SVG. Original idea SVG绘制原理与验证码 Usage cargo add biosvg let (answer, svg) = BiosvgBuilder::new() .length(4) .difficulty(

Reverier Xu 7 Aug 29, 2023
An SVG rendering library.

resvg resvg is an SVG rendering library. Purpose resvg can be used as a Rust library, a C library and as a CLI application to render SVG files based o

Evgeniy Reizner 1.8k Jan 7, 2023
svgcleaner could help you to clean up your SVG files from the unnecessary data.

svgcleaner svgcleaner helps you clean up your SVG files, keeping them free from unnecessary data. Table of Contents Purpose Goals Alternatives Charts

Evgeniy Reizner 1.5k Jan 9, 2023
🔭 Annict での今期の視聴状況を SVG 画像として出力するサーバ (WIP)

annict-profile-card ?? Annict の視聴状況などを SVG 画像として出力する API サーバ (WIP) Annict GraphQL API を使用しています。

Nep 16 Dec 20, 2022
Convert your ascii diagram scribbles into happy little SVG

Svgbob Svgbob can create a nice graphical representation of your text diagrams. Svgbob provides a cli which takes text as an input and creates an svg

Jovansonlee Cesar 3.4k Dec 25, 2022
The tool to make svg with triangles by length from two points.

The tool to make svg with triangles by length from two points.

null 2 Sep 27, 2021
Convert UFO .glif files to SVG, whether they're part of a font or not

Convert UFO glyph files (.glif) to SVG There exists already an svg2glif, but for some reason not the opposite operation. My MFEKglif editor treats .gl

Modular Font Editor K 3 Apr 26, 2022