1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
use crate::bindgen::{FPDF_FONT, FPDF_FONT_TRUETYPE, FPDF_FONT_TYPE1};
use crate::bindings::PdfiumLibraryBindings;
use crate::document::PdfDocument;
use crate::error::{PdfiumError, PdfiumInternalError};
use crate::page::PdfPoints;
use crate::utils::mem::create_byte_buffer;
use bitflags::bitflags;
use std::os::raw::{c_char, c_int, c_uint};
bitflags! {
struct FpdfFontDescriptorFlags: u32 {
const FIXED_PITCH_BIT_1 = 0b00000000000000000000000000000001;
const SERIF_BIT_2 = 0b00000000000000000000000000000010;
const SYMBOLIC_BIT_3 = 0b00000000000000000000000000000100;
const SCRIPT_BIT_4 = 0b00000000000000000000000000001000;
const NON_SYMBOLIC_BIT_6 = 0b00000000000000000000000000100000;
const ITALIC_BIT_7 = 0b00000000000000000000000001000000;
const ALL_CAP_BIT_17 = 0b00000000000000010000000000000000;
const SMALL_CAP_BIT_18 = 0b00000000000000100000000000000000;
const FORCE_BOLD_BIT_19 = 0b00000000000001000000000000000000;
}
}
#[derive(Copy, Clone, Debug, PartialEq)]
pub enum PdfFontBuiltin {
TimesRoman,
TimesBold,
TimesItalic,
TimesBoldItalic,
Helvetica,
HelveticaBold,
HelveticaOblique,
HelveticaBoldOblique,
Courier,
CourierBold,
CourierOblique,
CourierBoldOblique,
Symbol,
ZapfDingbats,
}
impl PdfFontBuiltin {
pub fn to_pdf_font_name(&self) -> &str {
match self {
PdfFontBuiltin::TimesRoman => "Times-Roman",
PdfFontBuiltin::TimesBold => "Times-Bold",
PdfFontBuiltin::TimesItalic => "Times-Italic",
PdfFontBuiltin::TimesBoldItalic => "Times-BoldItalic",
PdfFontBuiltin::Helvetica => "Helvetica",
PdfFontBuiltin::HelveticaBold => "Helvetica-Bold",
PdfFontBuiltin::HelveticaOblique => "Helvetica-Oblique",
PdfFontBuiltin::HelveticaBoldOblique => "Helvetica-BoldOblique",
PdfFontBuiltin::Courier => "Courier",
PdfFontBuiltin::CourierBold => "Courier-Bold",
PdfFontBuiltin::CourierOblique => "Courier-Oblique",
PdfFontBuiltin::CourierBoldOblique => "Courier-BoldOblique",
PdfFontBuiltin::Symbol => "Symbol",
PdfFontBuiltin::ZapfDingbats => "ZapfDingbats",
}
}
}
#[derive(Copy, Clone, Debug, PartialEq)]
pub enum PdfFontWeight {
Weight100,
Weight200,
Weight300,
Weight400Normal,
Weight500,
Weight600,
Weight700Bold,
Weight800,
Weight900,
Custom(u32),
}
impl PdfFontWeight {
pub(crate) fn from_pdfium(value: c_int) -> Option<PdfFontWeight> {
match value {
-1 => None,
100 => Some(PdfFontWeight::Weight100),
200 => Some(PdfFontWeight::Weight200),
300 => Some(PdfFontWeight::Weight300),
400 => Some(PdfFontWeight::Weight400Normal),
500 => Some(PdfFontWeight::Weight500),
600 => Some(PdfFontWeight::Weight600),
700 => Some(PdfFontWeight::Weight700Bold),
800 => Some(PdfFontWeight::Weight800),
900 => Some(PdfFontWeight::Weight900),
other => Some(PdfFontWeight::Custom(other as u32)),
}
}
}
pub struct PdfFont<'a> {
built_in: Option<PdfFontBuiltin>,
handle: FPDF_FONT,
bindings: &'a dyn PdfiumLibraryBindings,
}
impl<'a> PdfFont<'a> {
#[inline]
pub(crate) fn from_pdfium(handle: FPDF_FONT, bindings: &'a dyn PdfiumLibraryBindings) -> Self {
PdfFont {
built_in: None,
handle,
bindings,
}
}
#[inline]
pub fn new_built_in(document: &'a PdfDocument<'a>, font: PdfFontBuiltin) -> Self {
let mut result = Self::from_pdfium(
document
.get_bindings()
.FPDFText_LoadStandardFont(*document.get_handle(), font.to_pdf_font_name()),
document.get_bindings(),
);
result.built_in = Some(font);
result
}
#[inline]
pub fn times_roman(document: &'a PdfDocument<'a>) -> Self {
Self::new_built_in(document, PdfFontBuiltin::TimesRoman)
}
#[inline]
pub fn times_bold(document: &'a PdfDocument<'a>) -> Self {
Self::new_built_in(document, PdfFontBuiltin::TimesBold)
}
#[inline]
pub fn times_italic(document: &'a PdfDocument<'a>) -> Self {
Self::new_built_in(document, PdfFontBuiltin::TimesItalic)
}
#[inline]
pub fn times_bold_italic(document: &'a PdfDocument<'a>) -> Self {
Self::new_built_in(document, PdfFontBuiltin::TimesBoldItalic)
}
#[inline]
pub fn helvetica(document: &'a PdfDocument<'a>) -> Self {
Self::new_built_in(document, PdfFontBuiltin::Helvetica)
}
#[inline]
pub fn helvetica_bold(document: &'a PdfDocument<'a>) -> Self {
Self::new_built_in(document, PdfFontBuiltin::HelveticaBold)
}
#[inline]
pub fn helvetica_oblique(document: &'a PdfDocument<'a>) -> Self {
Self::new_built_in(document, PdfFontBuiltin::HelveticaOblique)
}
#[inline]
pub fn helvetica_bold_oblique(document: &'a PdfDocument<'a>) -> Self {
Self::new_built_in(document, PdfFontBuiltin::HelveticaBoldOblique)
}
#[inline]
pub fn courier(document: &'a PdfDocument<'a>) -> Self {
Self::new_built_in(document, PdfFontBuiltin::Courier)
}
#[inline]
pub fn courier_bold(document: &'a PdfDocument<'a>) -> Self {
Self::new_built_in(document, PdfFontBuiltin::CourierBold)
}
#[inline]
pub fn courier_oblique(document: &'a PdfDocument<'a>) -> Self {
Self::new_built_in(document, PdfFontBuiltin::CourierOblique)
}
#[inline]
pub fn courier_bold_oblique(document: &'a PdfDocument<'a>) -> Self {
Self::new_built_in(document, PdfFontBuiltin::CourierBoldOblique)
}
#[inline]
pub fn symbol(document: &'a PdfDocument<'a>) -> Self {
Self::new_built_in(document, PdfFontBuiltin::Symbol)
}
#[inline]
pub fn zapf_dingbats(document: &'a PdfDocument<'a>) -> Self {
Self::new_built_in(document, PdfFontBuiltin::ZapfDingbats)
}
pub fn new_type1_from_bytes(
document: &'a PdfDocument<'a>,
font_data: &[u8],
is_cid_font: bool,
) -> Result<Self, PdfiumError> {
Self::new_font_from_bytes(document, font_data, FPDF_FONT_TYPE1, is_cid_font)
}
pub fn new_true_type_from_bytes(
document: &'a PdfDocument<'a>,
font_data: &[u8],
is_cid_font: bool,
) -> Result<Self, PdfiumError> {
Self::new_font_from_bytes(document, font_data, FPDF_FONT_TRUETYPE, is_cid_font)
}
#[inline]
pub(crate) fn new_font_from_bytes(
document: &'a PdfDocument<'a>,
font_data: &[u8],
font_type: c_uint,
is_cid_font: bool,
) -> Result<Self, PdfiumError> {
let bindings = document.get_bindings();
let handle = bindings.FPDFText_LoadFont(
*document.get_handle(),
font_data.as_ptr(),
font_data.len() as c_uint,
font_type as c_int,
bindings.bool_to_pdfium(is_cid_font),
);
if handle.is_null() {
if let Some(error) = bindings.get_pdfium_last_error() {
Err(PdfiumError::PdfiumLibraryInternalError(error))
} else {
Err(PdfiumError::PdfiumLibraryInternalError(
PdfiumInternalError::Unknown,
))
}
} else {
Ok(PdfFont::from_pdfium(handle, bindings))
}
}
#[inline]
pub(crate) fn get_handle(&self) -> &FPDF_FONT {
&self.handle
}
pub fn name(&self) -> String {
let buffer_length =
self.bindings
.FPDFFont_GetFontName(self.handle, std::ptr::null_mut(), 0);
if buffer_length == 0 {
return String::new();
}
let mut buffer = create_byte_buffer(buffer_length as usize);
let result = self.bindings.FPDFFont_GetFontName(
self.handle,
buffer.as_mut_ptr() as *mut c_char,
buffer_length,
);
assert_eq!(result, buffer_length);
String::from_utf8(buffer)
.map(|str| str.trim_end_matches(char::from(0)).to_owned())
.unwrap_or_else(|_| String::new())
}
pub fn weight(&self) -> Result<PdfFontWeight, PdfiumError> {
PdfFontWeight::from_pdfium(self.bindings.FPDFFont_GetWeight(self.handle)).ok_or_else(|| {
PdfiumError::PdfiumLibraryInternalError(
self.bindings
.get_pdfium_last_error()
.unwrap_or(PdfiumInternalError::Unknown),
)
})
}
pub fn italic_angle(&self) -> Result<i32, PdfiumError> {
let mut angle = 0;
if self.bindings.is_true(
self.bindings
.FPDFFont_GetItalicAngle(self.handle, &mut angle),
) {
Ok(angle)
} else {
Err(PdfiumError::PdfiumLibraryInternalError(
self.bindings
.get_pdfium_last_error()
.unwrap_or(PdfiumInternalError::Unknown),
))
}
}
pub fn ascent(&self, font_size: PdfPoints) -> Result<PdfPoints, PdfiumError> {
let mut ascent = 0.0;
if self.bindings.is_true(self.bindings.FPDFFont_GetAscent(
self.handle,
font_size.value,
&mut ascent,
)) {
Ok(PdfPoints::new(ascent))
} else {
Err(PdfiumError::PdfiumLibraryInternalError(
self.bindings
.get_pdfium_last_error()
.unwrap_or(PdfiumInternalError::Unknown),
))
}
}
pub fn descent(&self, font_size: PdfPoints) -> Result<PdfPoints, PdfiumError> {
let mut descent = 0.0;
if self.bindings.is_true(self.bindings.FPDFFont_GetDescent(
self.handle,
font_size.value,
&mut descent,
)) {
Ok(PdfPoints::new(descent))
} else {
Err(PdfiumError::PdfiumLibraryInternalError(
self.bindings
.get_pdfium_last_error()
.unwrap_or(PdfiumInternalError::Unknown),
))
}
}
#[inline]
fn get_flags_bits(&self) -> FpdfFontDescriptorFlags {
FpdfFontDescriptorFlags::from_bits_truncate(
self.bindings.FPDFFont_GetFlags(self.handle) as u32
)
}
pub fn is_fixed_pitch(&self) -> bool {
self.get_flags_bits()
.contains(FpdfFontDescriptorFlags::FIXED_PITCH_BIT_1)
}
#[inline]
pub fn is_proportional_pitch(&self) -> bool {
!self.is_fixed_pitch()
}
pub fn is_serif(&self) -> bool {
self.get_flags_bits()
.contains(FpdfFontDescriptorFlags::SERIF_BIT_2)
}
#[inline]
pub fn is_sans_serif(&self) -> bool {
!self.is_serif()
}
pub fn is_symbolic(&self) -> bool {
self.get_flags_bits()
.contains(FpdfFontDescriptorFlags::SYMBOLIC_BIT_3)
}
pub fn is_non_symbolic(&self) -> bool {
self.get_flags_bits()
.contains(FpdfFontDescriptorFlags::NON_SYMBOLIC_BIT_6)
}
pub fn is_cursive(&self) -> bool {
self.get_flags_bits()
.contains(FpdfFontDescriptorFlags::SCRIPT_BIT_4)
}
pub fn is_italic(&self) -> bool {
self.get_flags_bits()
.contains(FpdfFontDescriptorFlags::ITALIC_BIT_7)
}
pub fn is_all_caps(&self) -> bool {
self.get_flags_bits()
.contains(FpdfFontDescriptorFlags::ALL_CAP_BIT_17)
}
pub fn is_small_caps(&self) -> bool {
self.get_flags_bits()
.contains(FpdfFontDescriptorFlags::SMALL_CAP_BIT_18)
}
pub fn is_bold_reenforced(&self) -> bool {
self.get_flags_bits()
.contains(FpdfFontDescriptorFlags::FORCE_BOLD_BIT_19)
}
}
impl<'a> Drop for PdfFont<'a> {
#[inline]
fn drop(&mut self) {
self.bindings.FPDFFont_Close(self.handle);
}
}