Skip to content

Commit

Permalink
Split svg_string from svg_path (#24)
Browse files Browse the repository at this point in the history
* add

* Update Cargo.lock

* Update lib.rs

* Update lib.rs

* Update CI.yaml

* add

* Update CI.yaml

* add

* Update CI.yaml

* add

* add

* add

* Update CI.yaml

* Update CI.yaml

* Revert "Update CI.yaml"

This reverts commit 93209d4.

* Update CI.yaml

* ad

* Update CI.yaml

* Update CI.yaml

* add
  • Loading branch information
baseplate-admin authored May 6, 2024
1 parent b2211be commit c048f21
Show file tree
Hide file tree
Showing 7 changed files with 41 additions and 68 deletions.
8 changes: 4 additions & 4 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ updates:
schedule:
interval: "monthly"
open-pull-requests-limit: 100
- package-ecosystem: "github-actions" # See documentation for possible values
directory: "/" # Location of package manifests
schedule:
interval: "daily"
# - package-ecosystem: "github-actions" # See documentation for possible values
# directory: "/" # Location of package manifests
# schedule:
# interval: "daily"
open-pull-requests-limit: 100
- package-ecosystem: "pip" # See documentation for possible values
directory: "/" # Location of package manifests
Expand Down
50 changes: 5 additions & 45 deletions .github/workflows/CI.yaml
Original file line number Diff line number Diff line change
@@ -1,30 +1,27 @@
# This file is autogenerated by maturin v1.5.1
# To update, run
#
# maturin generate-ci --pytest github
# maturin generate-ci github
#
name: CI

on:
push:
branches:
- "*"
- main
- master
tags:
- '*'
pull_request:
workflow_dispatch:

env:
RUST_BACKTRACE: "full"

permissions:
contents: read

jobs:
linux:
runs-on: ${{ matrix.platform.runner }}
strategy:
fail-fast: false
matrix:
platform:
- runner: ubuntu-latest
Expand All @@ -35,6 +32,8 @@ jobs:
target: aarch64
- runner: ubuntu-latest
target: armv7
- runner: ubuntu-latest
target: s390x
- runner: ubuntu-latest
target: ppc64le
steps:
Expand All @@ -54,29 +53,6 @@ jobs:
with:
name: wheels-linux-${{ matrix.platform.target }}
path: dist
- name: pytest
if: ${{ startsWith(matrix.platform.target, 'x86_64') }}
shell: bash
run: |
set -e
pip install resvg_py --find-links dist --force-reinstall
pip install pytest
pytest -rP
- name: pytest
if: ${{ !startsWith(matrix.platform.target, 'x86') && matrix.platform.target != 'ppc64' }}
uses: uraimo/run-on-arch-action@v2.7.2
with:
arch: ${{ matrix.platform.target }}
distro: ubuntu22.04
githubToken: ${{ github.token }}
install: |
apt-get update
apt-get install -y --no-install-recommends python3 python3-pip
pip3 install -U pip pytest
run: |
set -e
pip3 install resvg_py --find-links dist --force-reinstall
pytest -rP

windows:
runs-on: ${{ matrix.platform.runner }}
Expand Down Expand Up @@ -104,14 +80,6 @@ jobs:
with:
name: wheels-windows-${{ matrix.platform.target }}
path: dist
- name: pytest
if: ${{ !startsWith(matrix.platform.target, 'aarch64') }}
shell: bash
run: |
set -e
pip install resvg_py --find-links dist --force-reinstall
pip install pytest
pytest -rP

macos:
runs-on: ${{ matrix.platform.runner }}
Expand All @@ -138,14 +106,6 @@ jobs:
with:
name: wheels-macos-${{ matrix.platform.target }}
path: dist
- name: pytest
if: ${{ !startsWith(matrix.platform.target, 'aarch64') }}
shell: bash
run: |
set -e
pip install resvg_py --find-links dist --force-reinstall
pip install pytest
pytest -rP

sdist:
runs-on: ubuntu-latest
Expand Down
41 changes: 27 additions & 14 deletions src/rust/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,8 @@ fn resvg_magic(mut options: Opts, svg_string: String) -> Result<Vec<u8>, String>
#[pyfunction]
#[pyo3(name = "svg_to_base64")]
fn svg_to_base64(
svg: String,
svg_string: Option<String>,
svg_path: Option<String>,
// Control width, height, zoom, dpi
width: Option<u32>,
height: Option<u32>,
Expand All @@ -161,19 +162,31 @@ fn svg_to_base64(
// Background
background: Option<String>,
) -> PyResult<String> {
let svg_string: String;
let mut _svg_string = String::new();

if std::path::Path::new(&svg).exists() {
let mut svg_data = std::fs::read(&svg).expect("failed to open the provided file");
if svg_data.starts_with(&[0x1f, 0x8b]) {
svg_data =
resvg::usvg::decompress_svgz(&svg_data).expect("can't decompress the svg file");
};
svg_string = std::str::from_utf8(&svg_data)
.expect("can't convert bytes to utf-8")
.to_owned();
} else {
svg_string = svg;
if let Some(svg_string) = svg_string {
_svg_string = svg_string;
}

// Only check for path if provided string is empty
if _svg_string.is_empty() {
if let Some(svg_path) = svg_path {
if std::path::Path::new(&svg_path).exists() {
let mut svg_data =
std::fs::read(&svg_path).expect("failed to open the provided file");
if svg_data.starts_with(&[0x1f, 0x8b]) {
svg_data = resvg::usvg::decompress_svgz(&svg_data)
.expect("can't decompress the svg file");
};
_svg_string = std::str::from_utf8(&svg_data)
.expect("can't convert bytes to utf-8")
.to_owned();
}
}
}

if _svg_string.is_empty() {
panic!("`svg_string` is empty or `svg_path` contains empty invalid svg");
}

let mut fit_to = FitTo::Original;
Expand Down Expand Up @@ -259,7 +272,7 @@ fn svg_to_base64(
font_files,
font_dirs,
};
let pixmap = resvg_magic(options, svg_string.trim().to_owned()).unwrap();
let pixmap = resvg_magic(options, _svg_string.trim().to_owned()).unwrap();
Ok(general_purpose::STANDARD.encode(&pixmap))
}

Expand Down
2 changes: 1 addition & 1 deletion tests/test_complex_camera.py

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion tests/test_multiple_layer_svg.py

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion tests/test_normal_shape.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@


def test_rectangle():
base64 = resvg_py.svg_to_base64(svg_string)
base64 = resvg_py.svg_to_base64(svg_string=svg_string)
assert (
base64
== "iVBORw0KGgoAAAANSUhEUgAAASwAAACCCAYAAAD8HPVfAAAIBElEQVR4Ae3gAZAkSZIkSRKLqpm7R0REZmZmVlVVVVV3d3d3d/fMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMdHd3d3dXV1VVVVVmZkZGRIS7m5kKz0xmV3d1d3dPz8zMzMxMorjq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/N8R/KB8Hvhp4L6666n+/rwE+G7TLVf8TIP5T+MHARwNvDTyIq6763+MZwE8DXw26lav+J0H8p/NLA68NvDbwYOCluOqq/zn+BrgV+G3gt0F/zVX/UyGuuuqqq/53QFx11VVX/e+AuOqqq6763wFx1VVXXfW/A+Kqq6666n8HxFVXXXXV/w6Iq6666qr/HRBXXXXVVf87IK666qqr/ndAXHXVVVf974C46qqrrvrfAXHVVVdd9b8D4qqrrrrqfwfEVVddddX/Doirrrrqqv8dEFddddVV/zsgrrrqqqv+d0BcddVVV/3vgLjqqquu+t8BcdVVV131vwP/CCSk/oOTVh6qAAAAAElFTkSuQmCC"
Expand Down
4 changes: 2 additions & 2 deletions tests/test_path.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,15 @@ def test_path():
path = os.path.join(BASE_DIR, "acid.svg")
print(path)

base = resvg_py.svg_to_base64(path)
base = resvg_py.svg_to_base64(svg_path=path)
assert base == svg_output


def test_gzip_path():
path = os.path.join(BASE_DIR, "acid.svg.gz")
print(path)

base = resvg_py.svg_to_base64(path)
base = resvg_py.svg_to_base64(svg_path=path)
assert base == svg_output


Expand Down

0 comments on commit c048f21

Please sign in to comment.