Commit 3b64a2ac authored by RazrFalcon's avatar RazrFalcon

resvg-qt updated.

parent 05298982
......@@ -33,15 +33,17 @@ log = "0.4.5"
usvg = { git = "https://github.com/RazrFalcon/usvg", rev = "14342a5" }
#usvg = { path = "../usvg" }
unicode-segmentation = "1.2.1"
# cairo backend
cairo-rs = { version = "0.5", features = ["png"], optional = true }
gdk-pixbuf = { version = "0.5", optional = true }
pango = { version = "0.5", optional = true }
pangocairo = { version = "0.6", optional = true }
# qt backend
#resvg-qt = { version = "0.3", optional = true }
resvg-qt = { git = "https://github.com/RazrFalcon/resvg-qt", rev = "0f80423", optional = true }
#resvg-qt = { path = "../resvg-qt", optional = true }
resvg-qt = { git = "https://github.com/RazrFalcon/resvg-qt", rev = "fd0ed4f", optional = true }
[features]
cairo-backend = ["cairo-rs", "pango", "pangocairo", "gdk-pixbuf"]
......
......@@ -16,6 +16,7 @@ use usvg::prelude::*;
// self
use prelude::*;
use {
backend_utils,
layers,
OutputImage,
Render,
......@@ -377,8 +378,8 @@ fn _calc_node_bbox(
usvg::NodeKind::Text(ref text) => {
let mut bbox = Rect::new_bbox();
let mut fm = text::PangoFontMetrics::new(opt, cr);
text::draw_blocks(text, &mut fm, |block| {
let blocks = backend_utils::text::prepare_blocks(text, &mut fm);
backend_utils::text::draw_blocks(blocks, |block| {
cr.new_path();
let context = text::init_pango_context(opt, cr);
......
......@@ -26,7 +26,6 @@ use super::{
stroke,
};
pub use backend_utils::text::draw_blocks;
trait PangoScale {
fn scale(&self) -> f64;
......@@ -83,7 +82,8 @@ pub fn draw(
cr: &cairo::Context,
) -> Rect {
let mut fm = PangoFontMetrics::new(opt, cr);
draw_blocks(text_node, &mut fm, |block| draw_block(tree, block, opt, cr))
let blocks = text::prepare_blocks(text_node, &mut fm);
text::draw_blocks(blocks, |block| draw_block(tree, block, opt, cr))
}
pub fn init_pango_context(opt: &Options, cr: &cairo::Context) -> pango::Context {
......
......@@ -21,13 +21,13 @@ pub fn apply(
opt: &Options,
bbox: Rect,
layers: &mut QtLayers,
p: &qt::Painter,
p: &mut qt::Painter,
) {
let clip_img = try_opt!(layers.get(), ());
let mut clip_img = clip_img.borrow_mut();
clip_img.fill(0, 0, 0, 255);
let clip_p = qt::Painter::new(&clip_img);
let mut clip_p = qt::Painter::new(&mut clip_img);
clip_p.set_transform(&p.get_transform());
clip_p.apply_transform(&cp.transform.to_native());
......@@ -43,13 +43,13 @@ pub fn apply(
match *node.borrow() {
usvg::NodeKind::Path(ref path_node) => {
path::draw(&node.tree(), path_node, opt, &clip_p);
path::draw(&node.tree(), path_node, opt, &mut clip_p);
}
usvg::NodeKind::Text(ref text) => {
text::draw(&node.tree(), text, opt, &clip_p);
text::draw(&node.tree(), text, opt, &mut clip_p);
}
usvg::NodeKind::Group(ref g) => {
clip_group(&node, g, opt, bbox, layers, &clip_p);
clip_group(&node, g, opt, bbox, layers, &mut clip_p);
}
_ => {}
}
......@@ -62,7 +62,7 @@ pub fn apply(
if let Some(ref id) = cp.clip_path {
if let Some(ref clip_node) = node.tree().defs_by_id(id) {
if let usvg::NodeKind::ClipPath(ref cp) = *clip_node.borrow() {
apply(clip_node, cp, opt, bbox, layers, &p);
apply(clip_node, cp, opt, bbox, layers, p);
}
}
}
......@@ -78,7 +78,7 @@ fn clip_group(
opt: &Options,
bbox: Rect,
layers: &mut QtLayers,
p: &qt::Painter,
p: &mut qt::Painter,
) {
if let Some(ref id) = g.clip_path {
if let Some(ref clip_node) = node.tree().defs_by_id(id) {
......@@ -91,11 +91,11 @@ fn clip_group(
let mut clip_img = clip_img.borrow_mut();
clip_img.fill(0, 0, 0, 0);
let clip_p = qt::Painter::new(&clip_img);
let mut clip_p = qt::Painter::new(&mut clip_img);
clip_p.set_transform(&p.get_transform());
draw_group_child(&node, opt, &clip_p);
draw_group_child(&node, opt, &mut clip_p);
apply(clip_node, cp, opt, bbox, layers, &clip_p);
apply(clip_node, cp, opt, bbox, layers, &mut clip_p);
clip_p.end();
p.set_transform(&qt::Transform::default());
......@@ -111,7 +111,7 @@ fn clip_group(
fn draw_group_child(
node: &usvg::Node,
opt: &Options,
p: &qt::Painter,
p: &mut qt::Painter,
) {
if let Some(child) = node.first_child() {
p.apply_transform(&child.transform().to_native());
......
......@@ -19,7 +19,7 @@ pub fn apply(
fill: &Option<usvg::Fill>,
opt: &Options,
bbox: Rect,
p: &qt::Painter,
p: &mut qt::Painter,
) {
match *fill {
Some(ref fill) => {
......
......@@ -14,7 +14,7 @@ use backend_utils::image;
pub fn draw(
image: &usvg::Image,
opt: &Options,
p: &qt::Painter,
p: &mut qt::Painter,
) -> Rect {
if image.visibility != usvg::Visibility::Visible {
return image.view_box.rect;
......@@ -32,7 +32,7 @@ pub fn draw(
fn draw_raster(
image: &usvg::Image,
opt: &Options,
p: &qt::Painter,
p: &mut qt::Painter,
) {
let img = match image.data {
usvg::ImageData::Path(ref path) => {
......@@ -86,7 +86,7 @@ fn draw_raster(
fn draw_svg(
image: &usvg::Image,
opt: &Options,
p: &qt::Painter,
p: &mut qt::Painter,
) {
let (tree, sub_opt) = try_opt!(image::load_sub_svg(image, opt), ());
......
......@@ -17,14 +17,14 @@ pub fn apply(
opt: &Options,
bbox: Rect,
layers: &mut QtLayers,
sub_p: &qt::Painter,
p: &qt::Painter,
sub_p: &mut qt::Painter,
p: &mut qt::Painter,
) {
let mask_img = try_opt!(layers.get(), ());
let mut mask_img = mask_img.borrow_mut();
{
let mask_p = qt::Painter::new(&mask_img);
let mut mask_p = qt::Painter::new(&mut mask_img);
mask_p.set_transform(&p.get_transform());
let r = if mask.units == usvg::Units::ObjectBoundingBox {
......@@ -39,7 +39,7 @@ pub fn apply(
mask_p.apply_transform(&qt::Transform::from_bbox(bbox));
}
super::render_group(node, opt, layers, &mask_p);
super::render_group(node, opt, layers, &mut mask_p);
}
mask::image_to_mask(&mut mask_img.data_mut(), layers.image_size(), None);
......
......@@ -12,6 +12,7 @@ use usvg::prelude::*;
// self
use prelude::*;
use {
backend_utils,
layers,
OutputImage,
Render,
......@@ -21,7 +22,7 @@ use {
macro_rules! try_create_image {
($size:expr, $ret:expr) => {
try_opt_warn!(
qt::Image::new($size.width as u32, $size.height as u32),
qt::Image::new_rgba_premultiplied($size.width as u32, $size.height as u32),
$ret,
"Failed to create a {}x{} image.", $size.width, $size.height
);
......@@ -110,10 +111,10 @@ pub fn render_to_image(
tree: &usvg::Tree,
opt: &Options,
) -> Option<qt::Image> {
let (img, img_size) = create_root_image(tree.svg_node().size.to_screen_size(), opt)?;
let (mut img, img_size) = create_root_image(tree.svg_node().size.to_screen_size(), opt)?;
let painter = qt::Painter::new(&img);
render_to_canvas(tree, opt, img_size, &painter);
let mut painter = qt::Painter::new(&mut img);
render_to_canvas(tree, opt, img_size, &mut painter);
painter.end();
Some(img)
......@@ -136,10 +137,10 @@ pub fn render_node_to_image(
aspect: usvg::AspectRatio::default(),
};
let (img, img_size) = create_root_image(node_bbox.size().to_screen_size(), opt)?;
let (mut img, img_size) = create_root_image(node_bbox.size().to_screen_size(), opt)?;
let painter = qt::Painter::new(&img);
render_node_to_canvas(node, opt, vbox, img_size, &painter);
let mut painter = qt::Painter::new(&mut img);
render_node_to_canvas(node, opt, vbox, img_size, &mut painter);
painter.end();
Some(img)
......@@ -150,7 +151,7 @@ pub fn render_to_canvas(
tree: &usvg::Tree,
opt: &Options,
img_size: ScreenSize,
painter: &qt::Painter,
painter: &mut qt::Painter,
) {
render_node_to_canvas(&tree.root(), opt, tree.svg_node().view_box,
img_size, painter);
......@@ -162,11 +163,11 @@ pub fn render_node_to_canvas(
opt: &Options,
view_box: usvg::ViewBox,
img_size: ScreenSize,
painter: &qt::Painter,
painter: &mut qt::Painter,
) {
let mut layers = create_layers(img_size, opt);
apply_viewbox_transform(view_box, img_size, &painter);
apply_viewbox_transform(view_box, img_size, painter);
let curr_ts = painter.get_transform();
let mut ts = utils::abs_transform(node);
......@@ -203,7 +204,7 @@ fn create_root_image(
fn apply_viewbox_transform(
view_box: usvg::ViewBox,
img_size: ScreenSize,
painter: &qt::Painter,
painter: &mut qt::Painter,
) {
let ts = utils::view_box_to_transform(view_box.rect, view_box.aspect, img_size.to_size());
painter.apply_transform(&ts.to_native());
......@@ -213,7 +214,7 @@ fn render_node(
node: &usvg::Node,
opt: &Options,
layers: &mut QtLayers,
p: &qt::Painter,
p: &mut qt::Painter,
) -> Option<Rect> {
match *node.borrow() {
usvg::NodeKind::Svg(_) => {
......@@ -239,7 +240,7 @@ fn render_group(
parent: &usvg::Node,
opt: &Options,
layers: &mut QtLayers,
p: &qt::Painter,
p: &mut qt::Painter,
) -> Rect {
let curr_ts = p.get_transform();
let mut g_bbox = Rect::new_bbox();
......@@ -264,20 +265,20 @@ fn render_group_impl(
g: &usvg::Group,
opt: &Options,
layers: &mut QtLayers,
p: &qt::Painter,
p: &mut qt::Painter,
) -> Option<Rect> {
let sub_img = layers.get()?;
let sub_img = sub_img.borrow_mut();
let mut sub_img = sub_img.borrow_mut();
let sub_p = qt::Painter::new(&sub_img);
let mut sub_p = qt::Painter::new(&mut sub_img);
sub_p.set_transform(&p.get_transform());
let bbox = render_group(node, opt, layers, &sub_p);
let bbox = render_group(node, opt, layers, &mut sub_p);
if let Some(ref id) = g.clip_path {
if let Some(clip_node) = node.tree().defs_by_id(id) {
if let usvg::NodeKind::ClipPath(ref cp) = *clip_node.borrow() {
clippath::apply(&clip_node, cp, opt, bbox, layers, &sub_p);
clippath::apply(&clip_node, cp, opt, bbox, layers, &mut sub_p);
}
}
}
......@@ -285,7 +286,7 @@ fn render_group_impl(
if let Some(ref id) = g.mask {
if let Some(mask_node) = node.tree().defs_by_id(id) {
if let usvg::NodeKind::Mask(ref mask) = *mask_node.borrow() {
mask::apply(&mask_node, mask, opt, bbox, layers, &sub_p, p);
mask::apply(&mask_node, mask, opt, bbox, layers, &mut sub_p, p);
}
}
}
......@@ -316,19 +317,19 @@ pub fn calc_node_bbox(
) -> Option<Rect> {
// Unwrap can't fail, because `None` will be returned only on OOM,
// and we cannot hit it with a such small image.
let mut img = qt::Image::new(1, 1).unwrap();
let mut img = qt::Image::new_rgba_premultiplied(1, 1).unwrap();
img.set_dpi(opt.usvg.dpi);
let p = qt::Painter::new(&img);
let mut p = qt::Painter::new(&mut img);
let abs_ts = utils::abs_transform(node);
_calc_node_bbox(node, opt, abs_ts, &p)
_calc_node_bbox(node, opt, abs_ts, &mut p)
}
fn _calc_node_bbox(
node: &usvg::Node,
opt: &Options,
ts: usvg::Transform,
p: &qt::Painter,
p: &mut qt::Painter,
) -> Option<Rect> {
let mut ts2 = ts;
ts2.append(&node.transform());
......@@ -340,8 +341,8 @@ fn _calc_node_bbox(
usvg::NodeKind::Text(ref text) => {
let mut bbox = Rect::new_bbox();
let mut fm = text::QtFontMetrics::new(p);
text::draw_blocks(text, &mut fm, |block| {
let blocks = backend_utils::text::prepare_blocks(text, &mut fm);
backend_utils::text::draw_blocks(blocks, |block| {
let mut p_path = qt::PainterPath::new();
p_path.add_text(0.0, 0.0, &block.font, &block.text);
......
......@@ -18,7 +18,7 @@ pub fn draw(
tree: &usvg::Tree,
path: &usvg::Path,
opt: &Options,
p: &qt::Painter,
p: &mut qt::Painter,
) -> Rect {
let mut p_path = qt::PainterPath::new();
......
......@@ -40,7 +40,7 @@ pub fn apply(
img.set_dpi(opt.usvg.dpi);
img.fill(0, 0, 0, 0);
let p = qt::Painter::new(&img);
let mut p = qt::Painter::new(&mut img);
p.scale(sx, sy);
if let Some(vbox) = pattern.view_box {
......@@ -55,7 +55,7 @@ pub fn apply(
}
let mut layers = super::create_layers(img_size, opt);
super::render_group(pattern_node, opt, &mut layers, &p);
super::render_group(pattern_node, opt, &mut layers, &mut p);
p.end();
let img = if opacity.fuzzy_ne(&1.0) {
......@@ -66,7 +66,7 @@ pub fn apply(
let mut img2 = try_create_image!(img_size, ());
img2.fill(0, 0, 0, 0);
let p2 = qt::Painter::new(&img2);
let mut p2 = qt::Painter::new(&mut img2);
p2.set_opacity(*opacity);
p2.draw_image(0.0, 0.0, &img);
p2.end();
......
......@@ -19,7 +19,7 @@ pub fn apply(
stroke: &Option<usvg::Stroke>,
opt: &Options,
bbox: Rect,
p: &qt::Painter,
p: &mut qt::Painter,
) {
match *stroke {
Some(ref stroke) => {
......
......@@ -18,15 +18,13 @@ use super::{
stroke,
};
pub use backend_utils::text::draw_blocks;
pub struct QtFontMetrics<'a> {
p: &'a qt::Painter,
p: &'a mut qt::Painter,
}
impl<'a> QtFontMetrics<'a> {
pub fn new(p: &'a qt::Painter) -> Self {
pub fn new(p: &'a mut qt::Painter) -> Self {
QtFontMetrics { p }
}
}
......@@ -58,17 +56,17 @@ pub fn draw(
tree: &usvg::Tree,
text_node: &usvg::Text,
opt: &Options,
p: &qt::Painter,
p: &mut qt::Painter,
) -> Rect {
let mut fm = QtFontMetrics::new(p);
draw_blocks(text_node, &mut fm, |block| draw_block(tree, block, opt, p))
let blocks = text::prepare_blocks(text_node, &mut QtFontMetrics::new(p));
text::draw_blocks(blocks, |block| draw_block(tree, block, opt, p))
}
fn draw_block(
tree: &usvg::Tree,
block: &text::TextBlock<qt::Font>,
opt: &Options,
p: &qt::Painter,
p: &mut qt::Painter,
) {
p.set_font(&block.font);
let font_metrics = p.font_metrics();
......@@ -173,7 +171,7 @@ fn draw_line(
fill: &Option<usvg::Fill>,
stroke: &Option<usvg::Stroke>,
opt: &Options,
p: &qt::Painter,
p: &mut qt::Painter,
) {
fill::apply(tree, fill, opt, r, p);
stroke::apply(tree, stroke, opt, r, p);
......
......@@ -31,14 +31,11 @@ pub trait FontMetrics<Font> {
}
pub fn draw_blocks<Font, Draw>(
text_kind: &usvg::Text,
font_metrics: &mut FontMetrics<Font>,
blocks: Vec<TextBlock<Font>>,
mut draw: Draw,
) -> Rect
where Draw: FnMut(&TextBlock<Font>)
{
let blocks = prepare_blocks(text_kind, font_metrics);
let mut bbox = Rect::new_bbox();
for block in blocks {
bbox.expand(block.bbox);
......@@ -51,7 +48,7 @@ pub fn draw_blocks<Font, Draw>(
bbox
}
fn prepare_blocks<Font>(
pub fn prepare_blocks<Font>(
text_kind: &usvg::Text,
font_metrics: &mut FontMetrics<Font>,
) -> Vec<TextBlock<Font>> {
......
a-opacity-002.svg
a-visibility-003.svg
a-visibility-004.svg
a-visibility-005.svg
a-visibility-006.svg
a-visibility-007.svg
e-use-022.svg
a-opacity-002.svg
a-visibility-003.svg
a-visibility-004.svg
a-visibility-005.svg
a-visibility-006.svg
a-visibility-007.svg
e-use-022.svg
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment