svg2img 0.2.2 → 0.2.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/Cargo.toml +5 -0
- data/README.md +1 -0
- data/ext/svg2img/Cargo.toml +3 -3
- data/ext/svg2img/src/lib.rs +27 -3
- data/lib/svg2img/version.rb +1 -1
- data/tmp/arm64-darwin23/stage/Cargo.toml +5 -0
- data/tmp/arm64-darwin23/stage/ext/svg2img/Cargo.toml +3 -3
- metadata +1 -4
- data/tmp/arm64-darwin23/stage/tmp/arm64-darwin23/stage/Cargo.lock +0 -1607
- data/tmp/arm64-darwin23/stage/tmp/arm64-darwin23/stage/Cargo.toml +0 -7
- data/tmp/arm64-darwin23/stage/tmp/arm64-darwin23/stage/ext/svg2img/Cargo.toml +0 -17
    
        checksums.yaml
    CHANGED
    
    | @@ -1,7 +1,7 @@ | |
| 1 1 | 
             
            ---
         | 
| 2 2 | 
             
            SHA256:
         | 
| 3 | 
            -
              metadata.gz:  | 
| 4 | 
            -
              data.tar.gz:  | 
| 3 | 
            +
              metadata.gz: 8d6841dd1aea05aa5ed5827d948ca86cf1e36d17e984ceb80a599aec782c1f15
         | 
| 4 | 
            +
              data.tar.gz: 3378c9fc7c35f206fa0fe2b09a17dbc9c3418b32209bbeb4fbbbf303b6c167c3
         | 
| 5 5 | 
             
            SHA512:
         | 
| 6 | 
            -
              metadata.gz:  | 
| 7 | 
            -
              data.tar.gz:  | 
| 6 | 
            +
              metadata.gz: 2504c062ea1b85efe8a4d2f6dcfa4689e75ad82734ceb575b82472bc6313397d4c0869d6658a6ac28f6198ca42fbb4d5a55138d504c31575197ce8fdf7a92838
         | 
| 7 | 
            +
              data.tar.gz: 353861ba80c505ea21b6fab8d269bd46ce9807297b6ef96f2a332cf0b7c8a268bba2985aa2116cff5afce6be4e0f7043724babad45646ec24248400d1e582a2b
         | 
    
        data/Cargo.toml
    CHANGED
    
    
    
        data/README.md
    CHANGED
    
    | @@ -73,6 +73,7 @@ send_data(png_data, type: 'image/png', disposition: 'inline') | |
| 73 73 | 
             
            - `format` - output format, one of `:png`, `:jpg`, `:webp`, `:gif`
         | 
| 74 74 | 
             
            - `output_path` - path to the output image. If not provided, a temporary file will be created and the path to it will be returned.
         | 
| 75 75 | 
             
            - `size` - size of the output image as a proc that receives the width and height of the SVG and returns an array with the width and height of the output image. If the provides size has a different aspect ratio than the SVG, the image will be resized to fit in the center of the provided size. If not provided, the output image will have the same size as the SVG.
         | 
| 76 | 
            +
            - `super_sampling` - supersample factor. The output image will be rendered `super_sampling` times larger than the SVG and then resized to the desired size. This can be used to improve the quality of the output image. Default is 2. Must be a power of 2. 1 means no super sampling.
         | 
| 76 77 |  | 
| 77 78 | 
             
            ## Development
         | 
| 78 79 |  | 
    
        data/ext/svg2img/Cargo.toml
    CHANGED
    
    | @@ -1,10 +1,10 @@ | |
| 1 1 | 
             
            [package]
         | 
| 2 | 
            -
            name = "svg2img"
         | 
| 3 | 
            -
            version = "0.1.0"
         | 
| 4 | 
            -
            edition = "2021"
         | 
| 5 2 | 
             
            authors = ["Orvar Segerström <orvarsegerstrom@gmail.com>"]
         | 
| 3 | 
            +
            edition = "2021"
         | 
| 6 4 | 
             
            license = "MIT"
         | 
| 5 | 
            +
            name = "svg2img"
         | 
| 7 6 | 
             
            publish = false
         | 
| 7 | 
            +
            version = "0.1.0"
         | 
| 8 8 |  | 
| 9 9 | 
             
            [lib]
         | 
| 10 10 | 
             
            crate-type = ["cdylib"]
         | 
    
        data/ext/svg2img/src/lib.rs
    CHANGED
    
    | @@ -27,12 +27,23 @@ fn process_svg_rb(svg: String, options: magnus::RHash) -> Result<String, magnus: | |
| 27 27 | 
             
                        }
         | 
| 28 28 | 
             
                    };
         | 
| 29 29 | 
             
                }
         | 
| 30 | 
            +
             | 
| 31 | 
            +
                let super_sampling = get_option::<u32>(&options, "super_sampling")?.unwrap_or(2);
         | 
| 32 | 
            +
                // super_sampling must me a power of 2
         | 
| 33 | 
            +
                if super_sampling == 0 || super_sampling & (super_sampling - 1) != 0 {
         | 
| 34 | 
            +
                    return Err(magnus::Error::new(
         | 
| 35 | 
            +
                        magnus::exception::arg_error(),
         | 
| 36 | 
            +
                        "svg2img: Invalid super_sampling value, must be a power of 2",
         | 
| 37 | 
            +
                    ));
         | 
| 38 | 
            +
                }
         | 
| 39 | 
            +
             | 
| 30 40 | 
             
                let options = Options {
         | 
| 31 41 | 
             
                    size: get_option::<Proc>(&options, "size")?
         | 
| 32 42 | 
             
                        .map(convert_size_proc)
         | 
| 33 43 | 
             
                        .unwrap_or_else(default_size),
         | 
| 34 | 
            -
                    format,
         | 
| 35 44 | 
             
                    output_path: get_option(&options, "output_path")?,
         | 
| 45 | 
            +
                    format,
         | 
| 46 | 
            +
                    super_sampling,
         | 
| 36 47 | 
             
                };
         | 
| 37 48 | 
             
                process_svg(svg, options)
         | 
| 38 49 | 
             
                    .map_err(|err| magnus::Error::new(magnus::exception::runtime_error(), format!("{err:?}")))
         | 
| @@ -97,10 +108,17 @@ struct Options { | |
| 97 108 | 
             
                size: ProcessSize,
         | 
| 98 109 | 
             
                format: image::ImageFormat,
         | 
| 99 110 | 
             
                output_path: Option<String>,
         | 
| 111 | 
            +
                super_sampling: u32,
         | 
| 100 112 | 
             
            }
         | 
| 101 113 |  | 
| 102 114 | 
             
            fn process_svg(svg: String, options: Options) -> Result<String, anyhow::Error> {
         | 
| 103 | 
            -
                let  | 
| 115 | 
            +
                let image = image_from_svg(svg.as_bytes(), options.size, options.super_sampling)?;
         | 
| 116 | 
            +
             | 
| 117 | 
            +
                let mut image = image.resize_exact(
         | 
| 118 | 
            +
                    image.width() / options.super_sampling,
         | 
| 119 | 
            +
                    image.height() / options.super_sampling,
         | 
| 120 | 
            +
                    image::imageops::FilterType::Lanczos3,
         | 
| 121 | 
            +
                );
         | 
| 104 122 |  | 
| 105 123 | 
             
                if options.format == image::ImageFormat::Jpeg {
         | 
| 106 124 | 
             
                    // Convert from rgba8 to rgb8
         | 
| @@ -133,7 +151,11 @@ fn process_svg(svg: String, options: Options) -> Result<String, anyhow::Error> { | |
| 133 151 | 
             
                Ok(output_path)
         | 
| 134 152 | 
             
            }
         | 
| 135 153 |  | 
| 136 | 
            -
            fn image_from_svg( | 
| 154 | 
            +
            fn image_from_svg(
         | 
| 155 | 
            +
                bytes: &[u8],
         | 
| 156 | 
            +
                size: ProcessSize,
         | 
| 157 | 
            +
                super_sampling: u32,
         | 
| 158 | 
            +
            ) -> Result<DynamicImage, anyhow::Error> {
         | 
| 137 159 | 
             
                let svg = resvg::usvg::Tree::from_data(bytes, &resvg::usvg::Options::default())
         | 
| 138 160 | 
             
                    .context("Failed to parse SVG")?;
         | 
| 139 161 | 
             
                let svg_width = svg.size().width();
         | 
| @@ -141,6 +163,8 @@ fn image_from_svg(bytes: &[u8], size: ProcessSize) -> Result<DynamicImage, anyho | |
| 141 163 | 
             
                let svg_ratio = svg_width / svg_height;
         | 
| 142 164 |  | 
| 143 165 | 
             
                let (image_width, image_height) = size(svg_width as u32, svg_height as u32)?;
         | 
| 166 | 
            +
                let image_width = image_width * super_sampling;
         | 
| 167 | 
            +
                let image_height = image_height * super_sampling;
         | 
| 144 168 | 
             
                let image_ratio = image_width as f32 / image_height as f32;
         | 
| 145 169 |  | 
| 146 170 | 
             
                let scale = if svg_ratio > image_ratio {
         | 
    
        data/lib/svg2img/version.rb
    CHANGED
    
    
| @@ -1,10 +1,10 @@ | |
| 1 1 | 
             
            [package]
         | 
| 2 | 
            -
            name = "svg2img"
         | 
| 3 | 
            -
            version = "0.1.0"
         | 
| 4 | 
            -
            edition = "2021"
         | 
| 5 2 | 
             
            authors = ["Orvar Segerström <orvarsegerstrom@gmail.com>"]
         | 
| 3 | 
            +
            edition = "2021"
         | 
| 6 4 | 
             
            license = "MIT"
         | 
| 5 | 
            +
            name = "svg2img"
         | 
| 7 6 | 
             
            publish = false
         | 
| 7 | 
            +
            version = "0.1.0"
         | 
| 8 8 |  | 
| 9 9 | 
             
            [lib]
         | 
| 10 10 | 
             
            crate-type = ["cdylib"]
         | 
    
        metadata
    CHANGED
    
    | @@ -1,7 +1,7 @@ | |
| 1 1 | 
             
            --- !ruby/object:Gem::Specification
         | 
| 2 2 | 
             
            name: svg2img
         | 
| 3 3 | 
             
            version: !ruby/object:Gem::Version
         | 
| 4 | 
            -
              version: 0.2. | 
| 4 | 
            +
              version: 0.2.3
         | 
| 5 5 | 
             
            platform: ruby
         | 
| 6 6 | 
             
            authors:
         | 
| 7 7 | 
             
            - Orvar Segerström
         | 
| @@ -44,9 +44,6 @@ files: | |
| 44 44 | 
             
            - tmp/arm64-darwin23/stage/Cargo.lock
         | 
| 45 45 | 
             
            - tmp/arm64-darwin23/stage/Cargo.toml
         | 
| 46 46 | 
             
            - tmp/arm64-darwin23/stage/ext/svg2img/Cargo.toml
         | 
| 47 | 
            -
            - tmp/arm64-darwin23/stage/tmp/arm64-darwin23/stage/Cargo.lock
         | 
| 48 | 
            -
            - tmp/arm64-darwin23/stage/tmp/arm64-darwin23/stage/Cargo.toml
         | 
| 49 | 
            -
            - tmp/arm64-darwin23/stage/tmp/arm64-darwin23/stage/ext/svg2img/Cargo.toml
         | 
| 50 47 | 
             
            homepage: https://github.com/0rvar/svg2img-rb
         | 
| 51 48 | 
             
            licenses:
         | 
| 52 49 | 
             
            - MIT
         |