diff --git a/CHANGELOG.md b/CHANGELOG.md index 75a7cbe38..1535a0a26 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/crates/c-api/lib.rs b/crates/c-api/lib.rs index dbd9ecea3..58813bc92 100644 --- a/crates/c-api/lib.rs +++ b/crates/c-api/lib.rs @@ -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)] diff --git a/crates/resvg/examples/custom_usvg_tree.rs b/crates/resvg/examples/custom_usvg_tree.rs index 36a2bb178..789004bc5 100644 --- a/crates/resvg/examples/custom_usvg_tree.rs +++ b/crates/resvg/examples/custom_usvg_tree.rs @@ -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(); diff --git a/crates/resvg/tests/integration/main.rs b/crates/resvg/tests/integration/main.rs index d010e264b..718c5e3d7 100644 --- a/crates/resvg/tests/integration/main.rs +++ b/crates/resvg/tests/integration/main.rs @@ -10,6 +10,10 @@ mod extra; const IMAGE_SIZE: u32 = 300; static GLOBAL_FONTDB: Lazy> = 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"); @@ -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) {} +} diff --git a/crates/usvg-parser/src/filter.rs b/crates/usvg-parser/src/filter.rs index 4b6c9db84..1967e0e6a 100644 --- a/crates/usvg-parser/src/filter.rs +++ b/crates/usvg-parser/src/filter.rs @@ -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::(AId::Href) { + if let Some(node) = fe.try_attribute::(AId::Href) { let mut state = state.clone(); state.fe_image_link = true; let mut root = Group::default(); @@ -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."); diff --git a/crates/usvg-parser/src/image.rs b/crates/usvg-parser/src/image.rs index c0f9f65a8..acacb0c2e 100644 --- a/crates/usvg-parser/src/image.rs +++ b/crates/usvg-parser/src/image.rs @@ -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)?; diff --git a/crates/usvg-parser/src/svgtree/mod.rs b/crates/usvg-parser/src/svgtree/mod.rs index c977d731f..371d04bf7 100644 --- a/crates/usvg-parser/src/svgtree/mod.rs +++ b/crates/usvg-parser/src/svgtree/mod.rs @@ -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>(&self, aid: AId) -> Option { + 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> { let value = self.attribute(aid)?; diff --git a/crates/usvg-parser/src/text.rs b/crates/usvg-parser/src/text.rs index f91114ae4..dbebe3f23 100644 --- a/crates/usvg-parser/src/text.rs +++ b/crates/usvg-parser/src/text.rs @@ -688,7 +688,7 @@ fn convert_baseline_shift(node: SvgNode, state: &converter::State) -> Vec(AId::BaselineShift) { + if let Some(len) = n.try_attribute::(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)); diff --git a/crates/usvg-parser/src/units.rs b/crates/usvg-parser/src/units.rs index 951a7cbfe..dc46ac1fe 100644 --- a/crates/usvg-parser/src/units.rs +++ b/crates/usvg-parser/src/units.rs @@ -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::(AId::FontSize) { + if let Some(length) = n.try_attribute::(AId::FontSize) { let dpi = state.opt.dpi; let n = length.number as f32; font_size = match length.unit { diff --git a/crates/usvg-tree/src/lib.rs b/crates/usvg-tree/src/lib.rs index c9fbf2839..73a27e457 100644 --- a/crates/usvg-tree/src/lib.rs +++ b/crates/usvg-tree/src/lib.rs @@ -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`? diff --git a/crates/usvg/src/lib.rs b/crates/usvg/src/lib.rs index eb0d0706a..07380bf85 100644 --- a/crates/usvg/src/lib.rs +++ b/crates/usvg/src/lib.rs @@ -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, } } } @@ -105,7 +105,7 @@ pub trait TreePostProc { fn postprocess( &mut self, steps: PostProcessingSteps, - #[cfg(feature = "text")] fontdb: &fontdb::Database + #[cfg(feature = "text")] fontdb: &fontdb::Database, ); } @@ -113,14 +113,13 @@ 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); } } diff --git a/crates/usvg/src/main.rs b/crates/usvg/src/main.rs index f69cf8279..906d45791 100644 --- a/crates/usvg/src/main.rs +++ b/crates/usvg/src/main.rs @@ -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 = "\ diff --git a/crates/usvg/tests/write.rs b/crates/usvg/tests/write.rs index 43333ee51..01f19cb7f 100644 --- a/crates/usvg/tests/write.rs +++ b/crates/usvg/tests/write.rs @@ -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> =