@@ -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,9 @@ pub struct ConversionOptions {
148
155
///
149
156
/// _Default:_ `true`.
150
157
pub embed_text : bool ,
158
+
159
+ /// Whether to enforce PDF/A rules.
160
+ pub pdfa : bool ,
151
161
}
152
162
153
163
impl Default for ConversionOptions {
@@ -156,6 +166,7 @@ impl Default for ConversionOptions {
156
166
compress : true ,
157
167
raster_scale : 1.5 ,
158
168
embed_text : true ,
169
+ pdfa : false ,
159
170
}
160
171
}
161
172
}
@@ -190,7 +201,7 @@ pub fn to_pdf(
190
201
conversion_options : ConversionOptions ,
191
202
page_options : PageOptions ,
192
203
) -> Result < Vec < u8 > > {
193
- let mut ctx = Context :: new ( tree, conversion_options) ;
204
+ let mut ctx = Context :: new ( tree, conversion_options) ? ;
194
205
let mut pdf = Pdf :: new ( ) ;
195
206
196
207
let dpi_ratio = 72.0 / page_options. dpi ;
@@ -210,7 +221,7 @@ pub fn to_pdf(
210
221
// Generate main content
211
222
let mut rc = ResourceContainer :: new ( ) ;
212
223
let mut content = Content :: new ( ) ;
213
- content. save_state ( ) ;
224
+ content. save_state_checked ( ) ? ;
214
225
content. transform ( dpi_transform. to_pdf_transform ( ) ) ;
215
226
tree_to_stream ( tree, & mut pdf, & mut content, & mut ctx, & mut rc) ?;
216
227
content. restore_state ( ) ;
@@ -347,7 +358,7 @@ pub fn to_chunk(
347
358
) -> Result < ( Chunk , Ref ) > {
348
359
let mut chunk = Chunk :: new ( ) ;
349
360
350
- let mut ctx = Context :: new ( tree, conversion_options) ;
361
+ let mut ctx = Context :: new ( tree, conversion_options) ? ;
351
362
let x_ref = tree_to_xobject ( tree, & mut chunk, & mut ctx) ?;
352
363
ctx. write_global_objects ( & mut chunk) ?;
353
364
Ok ( ( chunk, x_ref) )
0 commit comments