-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: Support all resvg options #12
Draft
edgarrmondragon
wants to merge
3
commits into
dev
Choose a base branch
from
feat/support-all-resvg-options
base: dev
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from 2 commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,186 @@ | ||
use pyo3::prelude::*; | ||
|
||
/// A shape rendering method. | ||
/// | ||
/// `shape-rendering` attribute in the SVG. | ||
#[derive(Clone)] | ||
#[pyclass] | ||
pub enum ShapeRendering { | ||
OptimizeSpeed, | ||
CrispEdges, | ||
GeometricPrecision, | ||
} | ||
|
||
impl From<ShapeRendering> for usvg::ShapeRendering { | ||
fn from(shape_rendering: ShapeRendering) -> Self { | ||
match shape_rendering { | ||
ShapeRendering::OptimizeSpeed => Self::OptimizeSpeed, | ||
ShapeRendering::CrispEdges => Self::CrispEdges, | ||
ShapeRendering::GeometricPrecision => Self::GeometricPrecision, | ||
} | ||
} | ||
} | ||
|
||
/// Specifies the default text rendering method. | ||
/// | ||
/// Will be used when an SVG element's `text-rendering` property is set to `auto`. | ||
/// | ||
/// Default: OptimizeLegibility | ||
#[derive(Clone)] | ||
#[pyclass] | ||
pub enum TextRendering { | ||
OptimizeSpeed, | ||
OptimizeLegibility, | ||
GeometricPrecision, | ||
} | ||
|
||
impl From<TextRendering> for usvg::TextRendering { | ||
fn from(text_rendering: TextRendering) -> Self { | ||
match text_rendering { | ||
TextRendering::OptimizeSpeed => Self::OptimizeSpeed, | ||
TextRendering::OptimizeLegibility => Self::OptimizeLegibility, | ||
TextRendering::GeometricPrecision => Self::GeometricPrecision, | ||
} | ||
} | ||
} | ||
|
||
/// An image rendering method. | ||
/// | ||
/// `image-rendering` attribute in the SVG. | ||
#[derive(Clone)] | ||
#[pyclass] | ||
pub enum ImageRendering { | ||
OptimizeQuality, | ||
OptimizeSpeed, | ||
} | ||
|
||
impl From<ImageRendering> for usvg::ImageRendering { | ||
fn from(image_rendering: ImageRendering) -> Self { | ||
match image_rendering { | ||
ImageRendering::OptimizeQuality => Self::OptimizeQuality, | ||
ImageRendering::OptimizeSpeed => Self::OptimizeSpeed, | ||
} | ||
} | ||
} | ||
|
||
/// SVG parsing and rendering options. | ||
/// | ||
/// TODO(edgarmondragon): Add more options. | ||
#[derive(Clone)] | ||
#[pyclass] | ||
pub struct SVGOptions { | ||
/// Target DPI. | ||
/// | ||
/// Impacts units conversion. | ||
/// | ||
/// Default: 96.0 | ||
pub dpi: f64, | ||
|
||
/// A default font family. | ||
/// | ||
/// Will be used when no `font-family` attribute is set in the SVG. | ||
/// | ||
/// Default: Times New Roman | ||
pub font_family: String, | ||
|
||
/// A default font size. | ||
/// | ||
/// Will be used when no `font-size` attribute is set in the SVG. | ||
/// | ||
/// Default: 12 | ||
pub font_size: f64, | ||
|
||
/// Languages to use when resolving `systemLanguage` conditional attributes. | ||
/// | ||
/// Format: en, en-US. | ||
/// | ||
/// Default: `[en]` | ||
pub languages: Option<Vec<String>>, | ||
|
||
/// Specifies the default shape rendering method. | ||
/// | ||
/// Will be used when an SVG element's `shape-rendering` property is set to `auto`. | ||
/// | ||
/// Default: GeometricPrecision | ||
pub shape_rendering: ShapeRendering, | ||
|
||
/// Specifies the default text rendering method. | ||
/// | ||
/// Will be used when an SVG element's `text-rendering` property is set to `auto`. | ||
/// | ||
/// Default: OptimizeLegibility | ||
pub text_rendering: TextRendering, | ||
|
||
/// Specifies the default image rendering method. | ||
/// | ||
/// Will be used when an SVG element's `image-rendering` property is set to `auto`. | ||
/// | ||
/// Default: OptimizeQuality | ||
pub image_rendering: ImageRendering, | ||
|
||
/// Directory that will be used during relative paths resolving. | ||
/// | ||
/// Expected to be the same as the directory that contains the SVG file, | ||
/// but can be set to any. | ||
/// | ||
/// Default: `None | ||
pub resources_dir: Option<std::path::PathBuf>, | ||
|
||
/// Default viewport width to assume if there is no `viewBox` attribute and | ||
/// the `width` is relative. | ||
/// | ||
/// Default: 100.0 | ||
pub default_width: f64, | ||
|
||
/// Default viewport height to assume if there is no `viewBox` attribute and | ||
/// the `height` is relative. | ||
/// | ||
/// Default: 100.0 | ||
pub default_height: f64, | ||
} | ||
|
||
|
||
#[pymethods] | ||
impl SVGOptions { | ||
#[new] | ||
#[pyo3( | ||
signature = ( | ||
*, | ||
dpi = 96.0, | ||
font_family = "Times New Roman".to_string(), | ||
font_size = 12.0, | ||
languages = None, | ||
shape_rendering = ShapeRendering::GeometricPrecision, | ||
text_rendering = TextRendering::OptimizeLegibility, | ||
image_rendering = ImageRendering::OptimizeQuality, | ||
resources_dir = None, | ||
default_width = 100.0, | ||
default_height = 100.0, | ||
) | ||
)] | ||
fn new( | ||
dpi: f64, | ||
font_family: String, | ||
font_size: f64, | ||
languages: Option<Vec<String>>, | ||
shape_rendering: ShapeRendering, | ||
text_rendering: TextRendering, | ||
image_rendering: ImageRendering, | ||
resources_dir: Option<std::path::PathBuf>, | ||
default_width: f64, | ||
default_height: f64, | ||
) -> Self { | ||
Self { | ||
dpi, | ||
font_family, | ||
font_size, | ||
languages, | ||
shape_rendering, | ||
text_rendering, | ||
image_rendering, | ||
resources_dir, | ||
default_width, | ||
default_height, | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@jboarman Do you have handy examples of input/output pairs that might be worth testing here?
There's a bunch of examples in the upstream Rust repo, but it's probably overkill to check all of them.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Until we implement the benchmark in issue #10, I think we can make use of a few random selections from the upstream repo that you identified in your comment.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Alright, I'll try to port a few of the test files to validate the options.