@@ -67,7 +67,7 @@ use usvg::{Size, Transform, Tree};
67
67
68
68
use crate :: render:: { tree_to_stream, tree_to_xobject} ;
69
69
use crate :: util:: context:: Context ;
70
- use crate :: util:: helper:: { deflate, RectExt , TransformExt } ;
70
+ use crate :: util:: helper:: { deflate, ContentExt , RectExt , TransformExt } ;
71
71
use crate :: util:: resources:: ResourceContainer ;
72
72
73
73
// The ICC profiles.
@@ -96,6 +96,11 @@ impl Default for PageOptions {
96
96
pub enum ConversionError {
97
97
/// The SVG image contains an unrecognized type of image.
98
98
InvalidImage ,
99
+ /// Text shaping resulted in a .notdef glyph. Can only occur if PDF/A
100
+ /// processing is enabled.
101
+ MissingGlyphs ,
102
+ /// Converting the SVG would require too much nesting depth.
103
+ TooMuchNesting ,
99
104
/// An unknown error occurred during the conversion. This could indicate a bug in the
100
105
/// svg2pdf.
101
106
UnknownError ,
@@ -111,6 +116,8 @@ impl Display for ConversionError {
111
116
fn fmt ( & self , f : & mut Formatter ) -> fmt:: Result {
112
117
match self {
113
118
Self :: InvalidImage => f. write_str ( "An unknown type of image appears in the SVG." ) ,
119
+ Self :: MissingGlyphs => f. write_str ( "A piece of text could not be displayed with any font." ) ,
120
+ Self :: TooMuchNesting => f. write_str ( "The SVG's nesting depth is too high." ) ,
114
121
Self :: UnknownError => f. write_str ( "An unknown error occurred during the conversion. This could indicate a bug in svg2pdf" ) ,
115
122
#[ cfg( feature = "text" ) ]
116
123
Self :: SubsetError ( _) => f. write_str ( "An error occurred while subsetting a font." ) ,
@@ -148,6 +155,13 @@ pub struct ConversionOptions {
148
155
///
149
156
/// _Default:_ `true`.
150
157
pub embed_text : bool ,
158
+
159
+ /// Whether to write chunks in PDF/A-2b compliant mode.
160
+ ///
161
+ /// **Note:** This currently only ensures that `to_chunk` does not generate
162
+ /// anything that is forbidden by PDF/A. It does _not_ turn the
163
+ /// free-standing PDF generated by `to_pdf` into a valid PDF/A.
164
+ pub pdfa : bool ,
151
165
}
152
166
153
167
impl Default for ConversionOptions {
@@ -156,6 +170,7 @@ impl Default for ConversionOptions {
156
170
compress : true ,
157
171
raster_scale : 1.5 ,
158
172
embed_text : true ,
173
+ pdfa : false ,
159
174
}
160
175
}
161
176
}
@@ -190,7 +205,7 @@ pub fn to_pdf(
190
205
conversion_options : ConversionOptions ,
191
206
page_options : PageOptions ,
192
207
) -> Result < Vec < u8 > > {
193
- let mut ctx = Context :: new ( tree, conversion_options) ;
208
+ let mut ctx = Context :: new ( tree, conversion_options) ? ;
194
209
let mut pdf = Pdf :: new ( ) ;
195
210
196
211
let dpi_ratio = 72.0 / page_options. dpi ;
@@ -210,7 +225,7 @@ pub fn to_pdf(
210
225
// Generate main content
211
226
let mut rc = ResourceContainer :: new ( ) ;
212
227
let mut content = Content :: new ( ) ;
213
- content. save_state ( ) ;
228
+ content. save_state_checked ( ) ? ;
214
229
content. transform ( dpi_transform. to_pdf_transform ( ) ) ;
215
230
tree_to_stream ( tree, & mut pdf, & mut content, & mut ctx, & mut rc) ?;
216
231
content. restore_state ( ) ;
@@ -347,7 +362,7 @@ pub fn to_chunk(
347
362
) -> Result < ( Chunk , Ref ) > {
348
363
let mut chunk = Chunk :: new ( ) ;
349
364
350
- let mut ctx = Context :: new ( tree, conversion_options) ;
365
+ let mut ctx = Context :: new ( tree, conversion_options) ? ;
351
366
let x_ref = tree_to_xobject ( tree, & mut chunk, & mut ctx) ?;
352
367
ctx. write_global_objects ( & mut chunk) ?;
353
368
Ok ( ( chunk, x_ref) )
0 commit comments