Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions mupdf-sys/wrapper.c
Original file line number Diff line number Diff line change
Expand Up @@ -1117,6 +1117,37 @@ fz_pixmap *mupdf_display_list_to_pixmap(fz_context *ctx, fz_display_list *list,
return pixmap;
}

fz_buffer *mupdf_display_list_to_svg(fz_context *ctx, fz_display_list *list, fz_matrix ctm, fz_cookie *cookie, mupdf_error_t **errptr)
{
fz_rect mediabox = fz_bound_display_list(ctx, list);
fz_device *dev = NULL;
fz_buffer *buf = NULL;
fz_output *out = NULL;
fz_var(out);
fz_var(dev);
fz_var(buf);
fz_rect tbounds = mediabox;
tbounds = fz_transform_rect(tbounds, ctm);
fz_try(ctx)
{
buf = fz_new_buffer(ctx, 1024);
out = fz_new_output_with_buffer(ctx, buf);
dev = fz_new_svg_device(ctx, out, tbounds.x1 - tbounds.x0, tbounds.y1 - tbounds.y0, FZ_SVG_TEXT_AS_PATH, 1);
fz_run_display_list(ctx, list, dev, ctm, tbounds, cookie);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not completely sure if mediabox or tbounds should be used for the area here.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure either, I guess we'll know better when it gets used in real world applications.

fz_close_device(ctx, dev);
}
fz_always(ctx)
{
fz_drop_device(ctx, dev);
fz_drop_output(ctx, out);
}
fz_catch(ctx)
{
mupdf_save_error(ctx, errptr);
}
return buf;
}

fz_stext_page *mupdf_display_list_to_text_page(fz_context *ctx, fz_display_list *list, int flags, mupdf_error_t **errptr)
{
fz_stext_page *text_page = NULL;
Expand Down
36 changes: 33 additions & 3 deletions src/display_list.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use std::{ffi::CString, ptr::NonNull};
use std::{ffi::CString, io::Read, ptr::NonNull};

use mupdf_sys::*;

use crate::{
array::FzArray, context, rust_vec_from_ffi_ptr, Colorspace, Cookie, Device, Error, Image,
Matrix, Pixmap, Quad, Rect, TextPage, TextPageFlags,
array::FzArray, context, rust_vec_from_ffi_ptr, Buffer, Colorspace, Cookie, Device, Error,
Image, Matrix, Pixmap, Quad, Rect, TextPage, TextPageFlags,
};

#[derive(Debug)]
Expand Down Expand Up @@ -40,6 +40,36 @@ impl DisplayList {
.map(|inner| unsafe { Pixmap::from_raw(inner) })
}

pub fn to_svg(&self, ctm: &Matrix) -> Result<String, Error> {
let inner = unsafe {
ffi_try!(mupdf_display_list_to_svg(
context(),
self.inner,
ctm.into(),
ptr::null_mut()
))
}?;
let mut buf = unsafe { Buffer::from_raw(inner) };
let mut svg = String::new();
buf.read_to_string(&mut svg)?;
Ok(svg)
}

pub fn to_svg_with_cookie(&self, ctm: &Matrix, cookie: &Cookie) -> Result<String, Error> {
let inner = unsafe {
ffi_try!(mupdf_display_list_to_svg(
context(),
self.inner,
ctm.into(),
cookie.inner
))
}?;
let mut buf = unsafe { Buffer::from_raw(inner) };
let mut svg = String::new();
buf.read_to_string(&mut svg)?;
Ok(svg)
}

pub fn to_text_page(&self, opts: TextPageFlags) -> Result<TextPage, Error> {
let inner = unsafe {
ffi_try!(mupdf_display_list_to_text_page(
Expand Down
Loading