Skip to content
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

Pdf document stream experiments #54

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
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
6 changes: 4 additions & 2 deletions skia-safe/src/core/document.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
use crate::{interop::DynamicMemoryWStream, prelude::*, Canvas, Data, Rect, Size};
use std::{pin::Pin, ptr};

use core::fmt;
use skia_bindings::{self as sb, SkDocument, SkRefCntBase};
use std::{pin::Pin, ptr};

use crate::{interop::DynamicMemoryWStream, prelude::*, Canvas, Data, Rect, Size};

pub struct Document<State = state::Open> {
// note: order matters here, first the document must be
Expand Down
18 changes: 12 additions & 6 deletions skia-safe/src/docs/pdf_document.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
pub mod pdf {
use std::{ffi::CString, fmt, mem, ptr};

use crate::{
interop::{AsStr, DynamicMemoryWStream, SetStr},
prelude::*,
scalar, DateTime, Document,
};

use skia_bindings::{
self as sb, SkPDF_AttributeList, SkPDF_Metadata, SkPDF_StructureElementNode,
};
use std::{ffi::CString, fmt, mem, ptr};

pub type AttributeList = Handle<SkPDF_AttributeList>;
unsafe_send_sync!(AttributeList);
Expand Down Expand Up @@ -286,9 +288,13 @@ pub mod pdf {
}
}

#[test]
fn create_attribute_list() {
use pdf::AttributeList;
let mut _al = AttributeList::default();
_al.append_float_array("Owner", "Name", &[1.0, 2.0, 3.0]);
#[cfg(test)]
mod tests {
use super::pdf::AttributeList;

#[test]
fn create_attribute_list() {
let mut _al = AttributeList::default();
_al.append_float_array("Owner", "Name", &[1.0, 2.0, 3.0]);
}
}
48 changes: 30 additions & 18 deletions skia-safe/src/interop/stream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use crate::{prelude::*, Data};
use skia_bindings::{
self as sb, SkDynamicMemoryWStream, SkMemoryStream, SkStream, SkStreamAsset, SkWStream,
};
use std::{ffi, fmt, io, marker::PhantomData, mem, ptr};
use std::{ffi, fmt, io, marker::PhantomData, mem, ops::Deref, pin::Pin, ptr};

/// Trait representing an Skia allocated Stream type with a base class of SkStream.
#[repr(transparent)]
Expand Down Expand Up @@ -355,15 +355,22 @@ impl<'a> RustStream<'a> {
}

#[allow(unused)]
pub struct RustWStream<'a> {
inner: Handle<sb::RustWStream>,
_phantom: PhantomData<&'a mut ()>,
pub struct RustWStream<W: Deref> {
writer: Pin<Box<W>>,
rust_stream: Handle<sb::RustWStream>,
}

#[allow(unused)]
impl RustWStream<'_> {
impl<W: Deref> RustWStream<W> {
pub fn stream_mut(&mut self) -> &mut SkWStream {
self.inner.native_mut().base_mut()
self.rust_stream.native_mut().base_mut()
}

pub fn into_writer(mut self) -> W
where
W: Deref,
{
drop(self.rust_stream);
*unsafe { Pin::into_inner_unchecked(self.writer) }
}
}

Expand All @@ -375,21 +382,26 @@ impl NativeDrop for sb::RustWStream {
}
}

impl<'a> RustWStream<'a> {
pub fn new<T: io::Write>(writer: &'a mut T) -> Self {
impl<W: Deref> RustWStream<W> {
pub(crate) fn new(writer: W) -> Self
where
W: io::Write,
{
let mut writer = Box::pin(writer);
let writer_ptr: *mut W = unsafe { writer.as_mut().get_unchecked_mut() };
return RustWStream {
inner: Handle::construct(|ptr| unsafe {
writer,
rust_stream: Handle::construct(|ptr| unsafe {
sb::C_RustWStream_construct(
ptr,
writer as *mut T as *mut ffi::c_void,
Some(write_trampoline::<T>),
Some(flush_trampoline::<T>),
writer_ptr as *mut ffi::c_void,
Some(write_trampoline::<W>),
Some(flush_trampoline::<W>),
);
}),
_phantom: PhantomData,
};

unsafe extern "C" fn write_trampoline<T: io::Write>(
unsafe extern "C" fn write_trampoline<W: io::Write>(
val: *mut ffi::c_void,
buf: *const ffi::c_void,
count: usize,
Expand All @@ -398,7 +410,7 @@ impl<'a> RustWStream<'a> {
return true;
}
let buf: &[u8] = std::slice::from_raw_parts(buf as _, count as _);
let val: &mut T = &mut *(val as *mut _);
let val: &mut W = &mut *(val as *mut _);

// This is OK because we just abort if it panics anyway.
let mut val = std::panic::AssertUnwindSafe(val);
Expand All @@ -423,8 +435,8 @@ impl<'a> RustWStream<'a> {
}
}

unsafe extern "C" fn flush_trampoline<T: io::Write>(val: *mut ffi::c_void) {
let val: &mut T = &mut *(val as *mut _);
unsafe extern "C" fn flush_trampoline<W: io::Write>(val: *mut ffi::c_void) {
let val: &mut W = &mut *(val as *mut _);
// This is OK because we just abort if it panics anyway.
let mut val = std::panic::AssertUnwindSafe(val);
match std::panic::catch_unwind(move || {
Expand Down