@@ -76,13 +76,25 @@ extern "C" {
76
76
fn MD5_final ( outbuf : * mut u8 , ctx : * mut MD5_CONTEXT ) ;
77
77
}
78
78
pub type size_t = u64 ;
79
- #[ derive( Copy , Clone ) ]
79
+
80
+ use std:: ffi:: { CStr , CString } ;
81
+
82
+ #[ derive( Clone ) ]
80
83
#[ repr( C ) ]
81
84
pub struct pdf_color {
82
85
pub num_components : i32 ,
83
- pub spot_color_name : * mut i8 ,
86
+ pub spot_color_name : Option < CString > ,
84
87
pub values : [ f64 ; 4 ] ,
85
88
}
89
+ impl pdf_color {
90
+ pub const fn new ( ) -> Self {
91
+ Self {
92
+ num_components : 0 ,
93
+ spot_color_name : None ,
94
+ values : [ 0. ; 4 ] ,
95
+ }
96
+ }
97
+ }
86
98
#[ derive( Copy , Clone ) ]
87
99
#[ repr( C ) ]
88
100
pub struct pdf_colorspace {
@@ -160,9 +172,9 @@ pub struct C2RustUnnamed_1 {
160
172
pub major : i32 ,
161
173
pub minor : i32 ,
162
174
}
163
- #[ derive( Copy , Clone ) ]
175
+ #[ derive( Clone ) ]
164
176
#[ repr( C ) ]
165
- pub struct C2RustUnnamed_2 {
177
+ pub struct ColorStack {
166
178
pub current : i32 ,
167
179
pub stroke : [ pdf_color ; 128 ] ,
168
180
pub fill : [ pdf_color ; 128 ] ,
@@ -225,7 +237,7 @@ pub unsafe extern "C" fn pdf_color_rgbcolor(color: &mut pdf_color, r: f64, g: f6
225
237
color. values [ 1 ] = g;
226
238
color. values [ 2 ] = b;
227
239
color. num_components = 3i32 ;
228
- color. spot_color_name = 0 as * mut i8 ;
240
+ color. spot_color_name = None ;
229
241
0i32
230
242
}
231
243
#[ no_mangle]
@@ -257,20 +269,32 @@ pub unsafe extern "C" fn pdf_color_cmykcolor(
257
269
color. values [ 2 ] = y;
258
270
color. values [ 3 ] = k;
259
271
color. num_components = 4i32 ;
260
- color. spot_color_name = 0 as * mut i8 ;
272
+ color. spot_color_name = None ;
261
273
0i32
262
274
}
263
275
#[ no_mangle]
264
- pub unsafe extern "C" fn pdf_color_graycolor ( color : & mut pdf_color , mut g : f64 ) -> i32 {
276
+ pub unsafe extern "C" fn pdf_color_graycolor ( color : & mut pdf_color , g : f64 ) -> i32 {
265
277
if g < 0.0f64 || g > 1.0f64 {
266
278
warn ! ( "Invalid color value specified: gray={}" , g) ;
267
279
return -1i32 ;
268
280
}
269
281
color. values [ 0 ] = g;
270
282
color. num_components = 1i32 ;
271
- color. spot_color_name = 0 as * mut i8 ;
283
+ color. spot_color_name = None ;
272
284
0i32
273
285
}
286
+ pub fn pdf_color_graycolor_new ( g : f64 ) -> Result < pdf_color , i32 > {
287
+ if g < 0. || g > 1. {
288
+ warn ! ( "Invalid color value specified: gray={}" , g) ;
289
+ return Err ( -1 ) ;
290
+ }
291
+ Ok ( pdf_color {
292
+ values : [ g, 0. , 0. , 0. ] ,
293
+ num_components : 1 ,
294
+ spot_color_name : None ,
295
+ } )
296
+ }
297
+
274
298
#[ no_mangle]
275
299
pub unsafe extern "C" fn pdf_color_spotcolor (
276
300
color : & mut pdf_color ,
@@ -284,20 +308,12 @@ pub unsafe extern "C" fn pdf_color_spotcolor(
284
308
color. values [ 0 ] = c;
285
309
color. values [ 1 ] = 0.0f64 ;
286
310
color. num_components = 2i32 ;
287
- color. spot_color_name = name;
311
+ color. spot_color_name = Some ( CStr :: from_ptr ( name) . to_owned ( ) ) ;
288
312
0i32
289
313
}
290
314
#[ no_mangle]
291
- pub unsafe extern "C" fn pdf_color_copycolor (
292
- mut color1 : * mut pdf_color ,
293
- mut color2 : * const pdf_color ,
294
- ) {
295
- assert ! ( !color1. is_null( ) && !color2. is_null( ) ) ;
296
- memcpy (
297
- color1 as * mut libc:: c_void ,
298
- color2 as * const libc:: c_void ,
299
- :: std:: mem:: size_of :: < pdf_color > ( ) as u64 ,
300
- ) ;
315
+ pub unsafe extern "C" fn pdf_color_copycolor ( color1 : & mut pdf_color , color2 : & pdf_color ) {
316
+ * color1 = color2. clone ( ) ;
301
317
}
302
318
/* Brighten up a color. f == 0 means no change, f == 1 means white. */
303
319
#[ no_mangle]
@@ -367,7 +383,7 @@ pub unsafe extern "C" fn pdf_color_to_string(
367
383
len = sprintf (
368
384
buffer,
369
385
b" /%s %c%c %g %c%c\x00 " as * const u8 as * const i8 ,
370
- color. spot_color_name ,
386
+ color. spot_color_name . as_ref ( ) . unwrap ( ) . as_ptr ( ) ,
371
387
'C' as i32 | mask as i32 ,
372
388
'S' as i32 | mask as i32 ,
373
389
( color. values [ 0 ] / 0.001f64 + 0.5f64 ) . floor ( ) * 0.001f64 ,
@@ -438,8 +454,11 @@ pub unsafe extern "C" fn pdf_color_compare(color1: &pdf_color, color2: &pdf_colo
438
454
return -1i32 ;
439
455
}
440
456
}
441
- if !color1. spot_color_name . is_null ( ) && !color2. spot_color_name . is_null ( ) {
442
- return strcmp ( color1. spot_color_name , color2. spot_color_name ) ;
457
+ if color1. spot_color_name . is_some ( ) && color2. spot_color_name . is_some ( ) {
458
+ return strcmp (
459
+ color1. spot_color_name . as_ref ( ) . unwrap ( ) . as_ptr ( ) ,
460
+ color2. spot_color_name . as_ref ( ) . unwrap ( ) . as_ptr ( ) ,
461
+ ) ;
443
462
}
444
463
0i32
445
464
}
@@ -490,28 +509,23 @@ pub unsafe extern "C" fn pdf_color_is_valid(color: &pdf_color) -> bool {
490
509
}
491
510
}
492
511
if pdf_color_type ( color) == -2i32 {
493
- if color. spot_color_name . is_null ( )
494
- || * color. spot_color_name . offset ( 0 ) as i32 == '\u{0}' as i32
512
+ if color. spot_color_name . is_none ( )
513
+ || * color. spot_color_name . as_ref ( ) . unwrap ( ) . as_ptr ( ) . offset ( 0 ) as i32 == '\u{0}' as i32
495
514
{
496
515
warn ! ( "Invalid spot color: empty name" ) ;
497
516
return false ;
498
517
}
499
518
}
500
519
true
501
520
}
502
- static mut color_stack: C2RustUnnamed_2 = C2RustUnnamed_2 {
521
+ /* static mut color_stack: ColorStack = ColorStack {
503
522
current: 0,
504
- stroke : [ pdf_color {
505
- num_components : 0 ,
506
- spot_color_name : 0 as * const i8 as * mut i8 ,
507
- values : [ 0. ; 4 ] ,
508
- } ; 128 ] ,
509
- fill : [ pdf_color {
510
- num_components : 0 ,
511
- spot_color_name : 0 as * const i8 as * mut i8 ,
512
- values : [ 0. ; 4 ] ,
513
- } ; 128 ] ,
514
- } ;
523
+ stroke: unsafe { core::mem::zeroed() },//[pdf_color::new(); 128],
524
+ fill: unsafe { core::mem::zeroed() },//[pdf_color::new(); 128],
525
+ };*/
526
+ static mut color_stack: ColorStack =
527
+ unsafe { std:: mem:: transmute ( [ 0u8 ; std:: mem:: size_of :: < ColorStack > ( ) ] ) } ;
528
+
515
529
#[ no_mangle]
516
530
pub unsafe extern "C" fn pdf_color_clear_stack ( ) {
517
531
if color_stack. current > 0 {
@@ -523,8 +537,8 @@ pub unsafe extern "C" fn pdf_color_clear_stack() {
523
537
if !( fresh4 != 0 ) {
524
538
break ;
525
539
}
526
- free ( color_stack. stroke [ color_stack. current as usize ] . spot_color_name as * mut libc:: c_void ) ;
527
- free ( color_stack. fill [ color_stack. current as usize ] . spot_color_name as * mut libc:: c_void ) ;
540
+ // free(color_stack.stroke[color_stack.current as usize].spot_color_name as *mut libc::c_void);
541
+ // free(color_stack.fill[color_stack.current as usize].spot_color_name as *mut libc::c_void);
528
542
}
529
543
color_stack. current = 0 ;
530
544
pdf_color_graycolor ( & mut color_stack. stroke [ 0 ] , 0.0f64 ) ;
0 commit comments