Skip to content

Commit

Permalink
Do not show needless warnings when parsing some attributes.
Browse files Browse the repository at this point in the history
Closes #676
  • Loading branch information
RazrFalcon committed Jan 21, 2024
1 parent c41cc73 commit aefc979
Show file tree
Hide file tree
Showing 13 changed files with 66 additions and 14 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ This changelog also contains important changes in dependencies.

### Fixed
- Mark `mask-type` as a presentation attribute.
- Do not show needless warnings when parsing some attributes.

## [0.37.0] - 2023-12-16
### Added
Expand Down
2 changes: 1 addition & 1 deletion crates/c-api/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ use std::os::raw::c_char;
use std::slice;

use resvg::tiny_skia;
use resvg::usvg::{self, TreeParsing, TreePostProc};
#[cfg(feature = "text")]
use resvg::usvg::fontdb;
use resvg::usvg::{self, TreeParsing, TreePostProc};

/// @brief List of possible errors.
#[repr(C)]
Expand Down
5 changes: 4 additions & 1 deletion crates/resvg/examples/custom_usvg_tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,10 @@ fn main() {
)));
path.fill = fill;
tree.root.children.push(usvg::Node::Path(Box::new(path)));
tree.postprocess(usvg::PostProcessingSteps::default(), &fontdb::Database::new());
tree.postprocess(
usvg::PostProcessingSteps::default(),
&fontdb::Database::new(),
);

let pixmap_size = tree.size.to_int_size();
let mut pixmap = tiny_skia::Pixmap::new(pixmap_size.width(), pixmap_size.height()).unwrap();
Expand Down
36 changes: 36 additions & 0 deletions crates/resvg/tests/integration/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ mod extra;
const IMAGE_SIZE: u32 = 300;

static GLOBAL_FONTDB: Lazy<std::sync::Mutex<fontdb::Database>> = Lazy::new(|| {
if let Ok(()) = log::set_logger(&LOGGER) {
log::set_max_level(log::LevelFilter::Warn);
}

let mut fontdb = fontdb::Database::new();
fontdb.load_fonts_dir("tests/fonts");
fontdb.set_serif_family("Noto Serif");
Expand Down Expand Up @@ -216,3 +220,35 @@ fn demultiply_alpha(data: &mut [RGBA8]) {
p.r = (p.r as f64 / a + 0.5) as u8;
}
}

/// A simple stderr logger.
static LOGGER: SimpleLogger = SimpleLogger;
struct SimpleLogger;
impl log::Log for SimpleLogger {
fn enabled(&self, metadata: &log::Metadata) -> bool {
metadata.level() <= log::LevelFilter::Warn
}

fn log(&self, record: &log::Record) {
if self.enabled(record.metadata()) {
let target = if !record.target().is_empty() {
record.target()
} else {
record.module_path().unwrap_or_default()
};

let line = record.line().unwrap_or(0);
let args = record.args();

match record.level() {
log::Level::Error => eprintln!("Error (in {}:{}): {}", target, line, args),
log::Level::Warn => eprintln!("Warning (in {}:{}): {}", target, line, args),
log::Level::Info => eprintln!("Info (in {}:{}): {}", target, line, args),
log::Level::Debug => eprintln!("Debug (in {}:{}): {}", target, line, args),
log::Level::Trace => eprintln!("Trace (in {}:{}): {}", target, line, args),
}
}
}

fn flush(&self) {}
}
4 changes: 2 additions & 2 deletions crates/usvg-parser/src/filter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -693,7 +693,7 @@ fn convert_image(fe: SvgNode, state: &converter::State, cache: &mut converter::C
.find_attribute(AId::ImageRendering)
.unwrap_or(state.opt.image_rendering);

if let Some(node) = fe.attribute::<SvgNode>(AId::Href) {
if let Some(node) = fe.try_attribute::<SvgNode>(AId::Href) {
let mut state = state.clone();
state.fe_image_link = true;
let mut root = Group::default();
Expand Down Expand Up @@ -722,7 +722,7 @@ fn convert_image(fe: SvgNode, state: &converter::State, cache: &mut converter::C
};
}

let href = match fe.attribute(AId::Href) {
let href = match fe.try_attribute(AId::Href) {
Some(s) => s,
_ => {
log::warn!("The 'feImage' element lacks the 'xlink:href' attribute. Skipped.");
Expand Down
2 changes: 1 addition & 1 deletion crates/usvg-parser/src/image.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ enum ImageFormat {

pub(crate) fn convert(node: SvgNode, state: &converter::State, parent: &mut Group) -> Option<()> {
let href = node
.attribute(AId::Href)
.try_attribute(AId::Href)
.log_none(|| log::warn!("Image lacks the 'xlink:href' attribute. Skipped."))?;

let kind = get_href_data(href, state.opt)?;
Expand Down
12 changes: 12 additions & 0 deletions crates/usvg-parser/src/svgtree/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,18 @@ impl<'a, 'input: 'a> SvgNode<'a, 'input> {
}
}

/// Returns an attribute value.
///
/// Same as `SvgNode::attribute`, but doesn't show a warning.
pub fn try_attribute<T: FromValue<'a, 'input>>(&self, aid: AId) -> Option<T> {
let value = self
.attributes()
.iter()
.find(|a| a.name == aid)
.map(|a| a.value.as_str())?;
T::parse(*self, aid, value)
}

#[inline]
fn node_attribute(&self, aid: AId) -> Option<SvgNode<'a, 'input>> {
let value = self.attribute(aid)?;
Expand Down
2 changes: 1 addition & 1 deletion crates/usvg-parser/src/text.rs
Original file line number Diff line number Diff line change
Expand Up @@ -688,7 +688,7 @@ fn convert_baseline_shift(node: SvgNode, state: &converter::State) -> Vec<Baseli
.take_while(|n| n.tag_name() != Some(EId::Text))
.collect();
for n in nodes {
if let Some(len) = n.attribute::<Length>(AId::BaselineShift) {
if let Some(len) = n.try_attribute::<Length>(AId::BaselineShift) {
if len.unit == LengthUnit::Percent {
let n = crate::units::resolve_font_size(n, state) * (len.number as f32 / 100.0);
shift.push(BaselineShift::Number(n));
Expand Down
2 changes: 1 addition & 1 deletion crates/usvg-parser/src/units.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ pub(crate) fn resolve_font_size(node: SvgNode, state: &converter::State) -> f32
let mut font_size = state.opt.font_size;
for n in nodes.iter().rev().skip(1) {
// skip Root
if let Some(length) = n.attribute::<Length>(AId::FontSize) {
if let Some(length) = n.try_attribute::<Length>(AId::FontSize) {
let dpi = state.opt.dpi;
let n = length.number as f32;
font_size = match length.unit {
Expand Down
3 changes: 2 additions & 1 deletion crates/usvg-tree/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1565,7 +1565,8 @@ impl Group {
path.bounding_box = path.data.compute_tight_bounds();
path.stroke_bounding_box = path.calculate_stroke_bounding_box();
if path.stroke_bounding_box.is_none() {
path.stroke_bounding_box = path.bounding_box.and_then(|r| r.to_non_zero_rect());
path.stroke_bounding_box =
path.bounding_box.and_then(|r| r.to_non_zero_rect());
}
}
// TODO: should we account for `preserveAspectRatio`?
Expand Down
7 changes: 3 additions & 4 deletions crates/usvg/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ pub struct PostProcessingSteps {
impl Default for PostProcessingSteps {
fn default() -> Self {
Self {
convert_text_into_paths: true
convert_text_into_paths: true,
}
}
}
Expand All @@ -105,22 +105,21 @@ pub trait TreePostProc {
fn postprocess(
&mut self,
steps: PostProcessingSteps,
#[cfg(feature = "text")] fontdb: &fontdb::Database
#[cfg(feature = "text")] fontdb: &fontdb::Database,
);
}

impl TreePostProc for usvg_tree::Tree {
fn postprocess(
&mut self,
steps: PostProcessingSteps,
#[cfg(feature = "text")] fontdb: &fontdb::Database
#[cfg(feature = "text")] fontdb: &fontdb::Database,
) {
self.calculate_abs_transforms();

if steps.convert_text_into_paths {
#[cfg(feature = "text")]
{

usvg_text_layout::convert_text(&mut self.root, &fontdb);
}
}
Expand Down
2 changes: 1 addition & 1 deletion crates/usvg/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use std::path::PathBuf;
use std::process;

use pico_args::Arguments;
use usvg::{TreeWriting, TreePostProc};
use usvg::{TreePostProc, TreeWriting};
use usvg_parser::TreeParsing;

const HELP: &str = "\
Expand Down
2 changes: 1 addition & 1 deletion crates/usvg/tests/write.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use once_cell::sync::Lazy;

use usvg::{TreeWriting, TreePostProc};
use usvg::{TreePostProc, TreeWriting};
use usvg_parser::TreeParsing;

static GLOBAL_FONTDB: Lazy<std::sync::Mutex<usvg_text_layout::fontdb::Database>> =
Expand Down

0 comments on commit aefc979

Please sign in to comment.