blitz-pdf 0.1.1 → 0.1.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +266 -12
- package/THIRD_PARTY_NOTICES.md +4 -2
- package/dist/browser.d.ts +1 -1
- package/dist/browser.d.ts.map +1 -1
- package/dist/document-options.d.ts +36 -0
- package/dist/document-options.d.ts.map +1 -0
- package/dist/document-options.js +236 -0
- package/dist/document-options.js.map +1 -0
- package/dist/node.d.ts +1 -1
- package/dist/node.d.ts.map +1 -1
- package/dist/options.d.ts +35 -1
- package/dist/options.d.ts.map +1 -1
- package/dist/options.js +21 -14
- package/dist/options.js.map +1 -1
- package/dist/types.d.ts +71 -1
- package/dist/types.d.ts.map +1 -1
- package/dist/validation.d.ts +2 -0
- package/dist/validation.d.ts.map +1 -0
- package/dist/validation.js +7 -0
- package/dist/validation.js.map +1 -0
- package/examples/zugferd.mjs +41 -0
- package/licenses/RUST_DEPENDENCIES.txt +27 -0
- package/package.json +3 -2
- package/src/browser.ts +1 -1
- package/src/document-options.ts +218 -0
- package/src/node.ts +1 -1
- package/src/options.ts +18 -13
- package/src/types.ts +76 -1
- package/src/validation.ts +6 -0
- package/vendor/README.md +27 -2
- package/vendor/krilla/.cargo-ok +1 -0
- package/vendor/krilla/.cargo_vcs_info.json +6 -0
- package/vendor/krilla/Cargo.lock +1357 -0
- package/vendor/krilla/Cargo.toml +165 -0
- package/vendor/krilla/LICENSE_APACHE +202 -0
- package/vendor/krilla/LICENSE_MIT +21 -0
- package/vendor/krilla/README.md +156 -0
- package/vendor/krilla/examples/basic.rs +117 -0
- package/vendor/krilla/examples/empty_document.rs +25 -0
- package/vendor/krilla/examples/parley.rs +174 -0
- package/vendor/krilla/examples/simple_text.rs +118 -0
- package/vendor/krilla/examples/stream_builder.rs +93 -0
- package/vendor/krilla/icc/LICENSE +116 -0
- package/vendor/krilla/icc/README.md +1 -0
- package/vendor/krilla/icc/sGrey-v2-magic.icc +0 -0
- package/vendor/krilla/icc/sGrey-v4.icc +0 -0
- package/vendor/krilla/icc/sRGB-v2-magic.icc +0 -0
- package/vendor/krilla/icc/sRGB-v4.icc +0 -0
- package/vendor/krilla/src/chunk_container.rs +503 -0
- package/vendor/krilla/src/configure/PDF_A1.md +227 -0
- package/vendor/krilla/src/configure/PDF_A2.md +269 -0
- package/vendor/krilla/src/configure/PDF_A3.md +31 -0
- package/vendor/krilla/src/configure/PDF_A4.md +211 -0
- package/vendor/krilla/src/configure/PDF_UA1.md +202 -0
- package/vendor/krilla/src/configure/README.md +23 -0
- package/vendor/krilla/src/configure/mod.rs +197 -0
- package/vendor/krilla/src/configure/validate.rs +1244 -0
- package/vendor/krilla/src/configure/version.rs +134 -0
- package/vendor/krilla/src/content.rs +1352 -0
- package/vendor/krilla/src/data.rs +49 -0
- package/vendor/krilla/src/document.rs +156 -0
- package/vendor/krilla/src/error.rs +170 -0
- package/vendor/krilla/src/geom.rs +341 -0
- package/vendor/krilla/src/graphics/blend.rs +46 -0
- package/vendor/krilla/src/graphics/color.rs +552 -0
- package/vendor/krilla/src/graphics/graphic.rs +30 -0
- package/vendor/krilla/src/graphics/graphics_state.rs +251 -0
- package/vendor/krilla/src/graphics/icc.rs +218 -0
- package/vendor/krilla/src/graphics/image.rs +835 -0
- package/vendor/krilla/src/graphics/mask.rs +128 -0
- package/vendor/krilla/src/graphics/mod.rs +14 -0
- package/vendor/krilla/src/graphics/paint.rs +434 -0
- package/vendor/krilla/src/graphics/separation.rs +86 -0
- package/vendor/krilla/src/graphics/shading_function.rs +865 -0
- package/vendor/krilla/src/graphics/shading_pattern.rs +60 -0
- package/vendor/krilla/src/graphics/tiling_pattern.rs +119 -0
- package/vendor/krilla/src/graphics/xobject.rs +150 -0
- package/vendor/krilla/src/interactive/action.rs +68 -0
- package/vendor/krilla/src/interactive/annotation.rs +283 -0
- package/vendor/krilla/src/interactive/destination.rs +289 -0
- package/vendor/krilla/src/interactive/mod.rs +3 -0
- package/vendor/krilla/src/interchange/embed.rs +240 -0
- package/vendor/krilla/src/interchange/metadata.rs +564 -0
- package/vendor/krilla/src/interchange/mod.rs +4 -0
- package/vendor/krilla/src/interchange/outline.rs +263 -0
- package/vendor/krilla/src/interchange/tagging/fmt.rs +733 -0
- package/vendor/krilla/src/interchange/tagging/generate.toml +474 -0
- package/vendor/krilla/src/interchange/tagging/generated.rs +4754 -0
- package/vendor/krilla/src/interchange/tagging/mod.rs +1324 -0
- package/vendor/krilla/src/interchange/tagging/tag.rs +686 -0
- package/vendor/krilla/src/lib.rs +179 -0
- package/vendor/krilla/src/num.rs +26 -0
- package/vendor/krilla/src/page.rs +612 -0
- package/vendor/krilla/src/pdf.rs +313 -0
- package/vendor/krilla/src/resource.rs +436 -0
- package/vendor/krilla/src/serialize.rs +1101 -0
- package/vendor/krilla/src/stream.rs +368 -0
- package/vendor/krilla/src/surface.rs +664 -0
- package/vendor/krilla/src/text/cid.rs +488 -0
- package/vendor/krilla/src/text/font.rs +351 -0
- package/vendor/krilla/src/text/glyph/bitmap.rs +81 -0
- package/vendor/krilla/src/text/glyph/colr.rs +581 -0
- package/vendor/krilla/src/text/glyph/mod.rs +205 -0
- package/vendor/krilla/src/text/glyph/outline.rs +72 -0
- package/vendor/krilla/src/text/glyph/svg.rs +34 -0
- package/vendor/krilla/src/text/group.rs +463 -0
- package/vendor/krilla/src/text/mod.rs +232 -0
- package/vendor/krilla/src/text/shape.rs +102 -0
- package/vendor/krilla/src/text/type3.rs +514 -0
- package/vendor/krilla/src/util.rs +286 -0
- package/vendor/stylo/properties/longhands.toml +0 -1
- package/vendor/stylo/stylesheets/rule_parser.rs +1 -1
- package/wasm/blitz_pdf.js +10 -5
- package/wasm/blitz_pdf_bg.wasm +0 -0
|
@@ -0,0 +1,488 @@
|
|
|
1
|
+
use pdf_writer::types::{CidFontType, FontFlags, SystemInfo, UnicodeCmap};
|
|
2
|
+
use pdf_writer::writers::WMode;
|
|
3
|
+
use pdf_writer::{Finish, Name, Ref, Str};
|
|
4
|
+
use rustc_hash::FxHashMap;
|
|
5
|
+
use skrifa::instance::Size;
|
|
6
|
+
use skrifa::outline::DrawSettings;
|
|
7
|
+
use skrifa::prelude::LocationRef;
|
|
8
|
+
use skrifa::raw::tables::cff::Cff;
|
|
9
|
+
use skrifa::raw::{TableProvider, TopLevelTable};
|
|
10
|
+
use std::hash::Hash;
|
|
11
|
+
use std::ops::DerefMut;
|
|
12
|
+
use std::sync::Arc;
|
|
13
|
+
use subsetter::GlyphRemapper;
|
|
14
|
+
|
|
15
|
+
use super::{CIDIdentifier, FontIdentifier, PDF_UNITS_PER_EM};
|
|
16
|
+
use crate::chunk_container::ChunkContainer;
|
|
17
|
+
use crate::configure::ValidationError;
|
|
18
|
+
use crate::error::{KrillaError, KrillaResult};
|
|
19
|
+
use crate::geom::Rect;
|
|
20
|
+
use crate::serialize::SerializeContext;
|
|
21
|
+
use crate::stream::FilterStreamBuilder;
|
|
22
|
+
use crate::surface::Location;
|
|
23
|
+
use crate::text::outline::OutlineBuilder;
|
|
24
|
+
use crate::text::Font;
|
|
25
|
+
use crate::text::GlyphId;
|
|
26
|
+
use crate::util::{stable_hash128, SliceExt};
|
|
27
|
+
|
|
28
|
+
const SUBSET_TAG_LEN: usize = 6;
|
|
29
|
+
pub(crate) const IDENTITY_H: &str = "Identity-H";
|
|
30
|
+
pub(crate) const CMAP_NAME: Name = Name(b"Custom");
|
|
31
|
+
pub(crate) const SYSTEM_INFO: SystemInfo = SystemInfo {
|
|
32
|
+
registry: Str(b"Adobe"),
|
|
33
|
+
ordering: Str(b"Identity"),
|
|
34
|
+
supplement: 0,
|
|
35
|
+
};
|
|
36
|
+
|
|
37
|
+
pub(crate) type Cid = u16;
|
|
38
|
+
|
|
39
|
+
/// A shared function for CID fonts and Type3 fonts to write the cmap entries.
|
|
40
|
+
pub(crate) fn write_cmap_entry<G>(
|
|
41
|
+
font: &Font,
|
|
42
|
+
entry: Option<&(String, Option<Location>)>,
|
|
43
|
+
sc: &mut SerializeContext,
|
|
44
|
+
cmap: &mut UnicodeCmap<G>,
|
|
45
|
+
g: G,
|
|
46
|
+
) where
|
|
47
|
+
G: pdf_writer::types::GlyphId + Into<u32> + Copy,
|
|
48
|
+
{
|
|
49
|
+
match entry {
|
|
50
|
+
None => sc.register_validation_error(ValidationError::NoCodepointMapping(
|
|
51
|
+
font.clone(),
|
|
52
|
+
GlyphId::new(g.into()),
|
|
53
|
+
None,
|
|
54
|
+
)),
|
|
55
|
+
Some((text, loc)) => {
|
|
56
|
+
let mut invalid_codepoint = text.is_empty();
|
|
57
|
+
let mut invalid_code = None;
|
|
58
|
+
let mut private_unicode = None;
|
|
59
|
+
|
|
60
|
+
for c in text.chars() {
|
|
61
|
+
if matches!(c as u32, 0x0 | 0xFEFF | 0xFFFE) {
|
|
62
|
+
invalid_code = Some(c);
|
|
63
|
+
invalid_codepoint = true;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
if matches!(c as u32, 0xE000..=0xF8FF | 0xF0000..=0xFFFFD | 0x100000..=0x10FFFD) {
|
|
67
|
+
private_unicode = Some(c);
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
match invalid_code {
|
|
72
|
+
Some(c) => sc.register_validation_error(ValidationError::InvalidCodepointMapping(
|
|
73
|
+
font.clone(),
|
|
74
|
+
GlyphId::new(g.into()),
|
|
75
|
+
c,
|
|
76
|
+
*loc,
|
|
77
|
+
)),
|
|
78
|
+
None if invalid_codepoint => sc.register_validation_error(
|
|
79
|
+
ValidationError::NoCodepointMapping(font.clone(), GlyphId::new(g.into()), *loc),
|
|
80
|
+
),
|
|
81
|
+
_ => {}
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
if let Some(code) = private_unicode {
|
|
85
|
+
sc.register_validation_error(ValidationError::UnicodePrivateArea(
|
|
86
|
+
font.clone(),
|
|
87
|
+
GlyphId::new(g.into()),
|
|
88
|
+
code,
|
|
89
|
+
*loc,
|
|
90
|
+
));
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
if !text.is_empty() {
|
|
94
|
+
cmap.pair_with_multiple(g, text.chars());
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
/// A CID-keyed font.
|
|
101
|
+
#[derive(Debug)]
|
|
102
|
+
pub(crate) struct CIDFont {
|
|
103
|
+
/// The _actual_ underlying OTF font of the CID-keyed font.
|
|
104
|
+
font: Font,
|
|
105
|
+
/// A mapper that maps GIDs from the original font to CIDs, i.e. the corresponding GID in the font
|
|
106
|
+
/// subset. The subsetter will ensure that for CID-keyed CFF fonts, the CID-to-GID mapping
|
|
107
|
+
/// will be the identity mapping, regardless of what the mapping was in the original font. This
|
|
108
|
+
/// allows us to index both, CFF and glyf-based fonts, transparently using GIDs,
|
|
109
|
+
/// instead of having to distinguish according to the underlying font. See section
|
|
110
|
+
/// 9.7.4.2 for more information on how glyphs are indexed in a CID-keyed font.
|
|
111
|
+
glyph_remapper: GlyphRemapper,
|
|
112
|
+
/// A mapping from CIDs to their string in the original text.
|
|
113
|
+
cmap_entries: FxHashMap<u16, (String, Option<Location>)>,
|
|
114
|
+
/// The widths of the glyphs, _indexed by their CID_.
|
|
115
|
+
widths: Vec<f32>,
|
|
116
|
+
is_empty: bool,
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
impl CIDFont {
|
|
120
|
+
/// Create a new CID-keyed font.
|
|
121
|
+
pub(crate) fn new(font: Font) -> CIDFont {
|
|
122
|
+
// Always include the .notdef glyph. Will also always be included by the subsetter in
|
|
123
|
+
// the glyph remapper.
|
|
124
|
+
let widths = vec![font.advance_width(GlyphId::new(0)).unwrap_or(0.0)];
|
|
125
|
+
|
|
126
|
+
Self {
|
|
127
|
+
glyph_remapper: GlyphRemapper::new(),
|
|
128
|
+
cmap_entries: FxHashMap::default(),
|
|
129
|
+
widths,
|
|
130
|
+
font,
|
|
131
|
+
is_empty: true,
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
pub(crate) fn is_empty(&self) -> bool {
|
|
136
|
+
self.is_empty
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
pub(crate) fn font(&self) -> Font {
|
|
140
|
+
self.font.clone()
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
// Note that this refers to the units per em in PDF (which is always 1000), and not the
|
|
144
|
+
// units per em of the underlying font.
|
|
145
|
+
pub(crate) fn units_per_em(&self) -> f32 {
|
|
146
|
+
PDF_UNITS_PER_EM
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
#[inline]
|
|
150
|
+
pub(crate) fn get_cid(&self, glyph_id: GlyphId) -> Option<u16> {
|
|
151
|
+
self.glyph_remapper.get(glyph_id.to_u32() as u16)
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
/// Add a new glyph (if it has not already been added) and return its CID.
|
|
155
|
+
#[inline]
|
|
156
|
+
pub(crate) fn add_glyph(&mut self, glyph_id: GlyphId) -> Cid {
|
|
157
|
+
self.is_empty = false;
|
|
158
|
+
|
|
159
|
+
let new_id = self
|
|
160
|
+
.glyph_remapper
|
|
161
|
+
.remap(u16::try_from(glyph_id.to_u32()).unwrap());
|
|
162
|
+
|
|
163
|
+
// This means that the glyph ID has been newly assigned, and thus we need to add its width.
|
|
164
|
+
if new_id as usize >= self.widths.len() {
|
|
165
|
+
self.widths
|
|
166
|
+
.push(self.font.advance_width(glyph_id).unwrap_or(0.0));
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
new_id
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
#[inline]
|
|
173
|
+
pub(crate) fn get_codepoints(&self, cid: Cid) -> Option<&str> {
|
|
174
|
+
self.cmap_entries.get(&cid).map(|s| s.0.as_str())
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
#[inline]
|
|
178
|
+
pub(crate) fn set_codepoints(&mut self, cid: Cid, text: String, location: Option<Location>) {
|
|
179
|
+
self.cmap_entries.insert(cid, (text, location));
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
#[inline]
|
|
183
|
+
pub(crate) fn identifier(&self) -> FontIdentifier {
|
|
184
|
+
FontIdentifier::Cid(CIDIdentifier(self.font.clone()))
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
pub(crate) fn serialize(
|
|
188
|
+
&self,
|
|
189
|
+
sc: &mut SerializeContext,
|
|
190
|
+
chunk_container: &mut ChunkContainer,
|
|
191
|
+
root_ref: Ref,
|
|
192
|
+
) -> KrillaResult<()> {
|
|
193
|
+
let chunk = &mut chunk_container.non_stream.fonts;
|
|
194
|
+
let mut stream_chunk = sc.new_chunk();
|
|
195
|
+
|
|
196
|
+
let cid_ref = sc.new_ref();
|
|
197
|
+
let descriptor_ref = sc.new_ref();
|
|
198
|
+
let cmap_ref = sc.new_ref();
|
|
199
|
+
let cid_set_ref = sc.new_ref();
|
|
200
|
+
let data_ref = sc.new_ref();
|
|
201
|
+
|
|
202
|
+
let glyph_remapper = &self.glyph_remapper;
|
|
203
|
+
|
|
204
|
+
let is_glyf = self.font.font_ref().glyf().is_ok();
|
|
205
|
+
let is_cff = self.font.font_ref().cff().is_ok();
|
|
206
|
+
let is_cff2 = self.font.font_ref().cff2().is_ok();
|
|
207
|
+
|
|
208
|
+
if !is_glyf && !is_cff && !is_cff2 {
|
|
209
|
+
return Err(KrillaError::Font(
|
|
210
|
+
self.font.clone(),
|
|
211
|
+
"font is missing an outline table".to_string(),
|
|
212
|
+
));
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
// OS/2 fsType has a few bits that describe which kind of license the font has. Of
|
|
216
|
+
// particular interest is bit 2 "Restricted License embedding", which places restrictions on
|
|
217
|
+
// font embedding that are incompatible with some PDF standards.
|
|
218
|
+
//
|
|
219
|
+
// The OpenType spec intended for the different bits to be mutually exclusive. However, some
|
|
220
|
+
// fonts have multiple of the bits sets. The OpenType spec was thus adjusted to require
|
|
221
|
+
// mutual exclusion, allowing applications to assume the least-restrictive specified
|
|
222
|
+
// variant. Specifically, it also clarifies that "For Restricted License embedding to take
|
|
223
|
+
// effect, the Embedding permissions sub-field must have the value 2 (that is, only bit 1 is
|
|
224
|
+
// set)."
|
|
225
|
+
//
|
|
226
|
+
// Hence, we are not checking `fsType & 2 != 0` (as would be usual for bit flags),
|
|
227
|
+
// but rather `fsType & 0xF == 2`.
|
|
228
|
+
if self
|
|
229
|
+
.font
|
|
230
|
+
.font_ref()
|
|
231
|
+
.os2()
|
|
232
|
+
.is_ok_and(|os2| os2.fs_type() & 0xF == 2)
|
|
233
|
+
{
|
|
234
|
+
sc.register_validation_error(ValidationError::RestrictedLicense(self.font.clone()));
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
let (subsetted, global_bbox) = subset_font(self.font.clone(), glyph_remapper)?;
|
|
238
|
+
let num_glyphs = subsetted.num_glyphs();
|
|
239
|
+
let subsetted_data = subsetted.font_data().0;
|
|
240
|
+
|
|
241
|
+
let font_stream = {
|
|
242
|
+
let mut data = subsetted_data.as_ref().as_ref();
|
|
243
|
+
|
|
244
|
+
// If we have a CFF font, only embed the standalone CFF program.
|
|
245
|
+
let subsetted_ref = skrifa::FontRef::new(data).map_err(|_| {
|
|
246
|
+
KrillaError::Font(self.font.clone(), "failed to read font subset".to_string())
|
|
247
|
+
})?;
|
|
248
|
+
|
|
249
|
+
if let Some(cff) = subsetted_ref.data_for_tag(Cff::TAG) {
|
|
250
|
+
data = cff.as_bytes();
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
FilterStreamBuilder::new_from_binary_data(data).finish(&sc.serialize_settings())
|
|
254
|
+
};
|
|
255
|
+
|
|
256
|
+
let base_font = base_font_name(&self.font, &self.glyph_remapper);
|
|
257
|
+
let base_font_type0 = if is_cff {
|
|
258
|
+
format!("{base_font}-{IDENTITY_H}")
|
|
259
|
+
} else {
|
|
260
|
+
base_font.clone()
|
|
261
|
+
};
|
|
262
|
+
|
|
263
|
+
chunk
|
|
264
|
+
.type0_font(root_ref)
|
|
265
|
+
.base_font(Name(base_font_type0.as_bytes()))
|
|
266
|
+
.encoding_predefined(Name(IDENTITY_H.as_bytes()))
|
|
267
|
+
.descendant_font(cid_ref)
|
|
268
|
+
.to_unicode(cmap_ref);
|
|
269
|
+
|
|
270
|
+
let mut cid = chunk.cid_font(cid_ref);
|
|
271
|
+
cid.subtype(if is_cff {
|
|
272
|
+
CidFontType::Type0
|
|
273
|
+
} else {
|
|
274
|
+
CidFontType::Type2
|
|
275
|
+
});
|
|
276
|
+
cid.base_font(Name(base_font.as_bytes()));
|
|
277
|
+
cid.system_info(SYSTEM_INFO);
|
|
278
|
+
cid.font_descriptor(descriptor_ref);
|
|
279
|
+
cid.default_width(0.0);
|
|
280
|
+
|
|
281
|
+
if !is_cff {
|
|
282
|
+
cid.cid_to_gid_map_predefined(Name(b"Identity"));
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
// IN CID fonts, a upem value of 1000 is assumed for all fonts, so we need to convert.
|
|
286
|
+
let to_pdf_units = |v: f32| v / self.font.units_per_em() * self.units_per_em();
|
|
287
|
+
|
|
288
|
+
let mut first = 0;
|
|
289
|
+
let mut width_writer = cid.widths();
|
|
290
|
+
for (w, group) in self.widths.group_by_key(|&w| w) {
|
|
291
|
+
let end = first + group.len();
|
|
292
|
+
if w != 0.0 {
|
|
293
|
+
let last = end - 1;
|
|
294
|
+
width_writer.same(first as u16, last as u16, to_pdf_units(w));
|
|
295
|
+
}
|
|
296
|
+
first = end;
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
width_writer.finish();
|
|
300
|
+
cid.finish();
|
|
301
|
+
|
|
302
|
+
// The only reason we write this in the first place is that PDF/A-1b requires
|
|
303
|
+
// a CIDSet.
|
|
304
|
+
if !sc.serialize_settings().pdf_version().deprecates_cid_set() {
|
|
305
|
+
let cid_stream_data = {
|
|
306
|
+
// It's always guaranteed by the subsetter that CIDs start from 0 and are
|
|
307
|
+
// consecutive, so this encoding is very straight-forward.
|
|
308
|
+
let mut bytes = vec![];
|
|
309
|
+
bytes.extend([0xFFu8].repeat((num_glyphs / 8) as usize));
|
|
310
|
+
let padding = num_glyphs % 8;
|
|
311
|
+
if padding != 0 {
|
|
312
|
+
bytes.push(!(0xFF >> padding))
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
bytes
|
|
316
|
+
};
|
|
317
|
+
|
|
318
|
+
let cid_stream = FilterStreamBuilder::new_from_binary_data(&cid_stream_data)
|
|
319
|
+
.finish(&sc.serialize_settings());
|
|
320
|
+
let mut cid_set = stream_chunk.stream(cid_set_ref, cid_stream.encoded_data());
|
|
321
|
+
cid_stream.write_filters(cid_set.deref_mut());
|
|
322
|
+
cid_set.finish();
|
|
323
|
+
cid_stream.finish();
|
|
324
|
+
}
|
|
325
|
+
|
|
326
|
+
let mut flags = FontFlags::empty();
|
|
327
|
+
flags.set(
|
|
328
|
+
FontFlags::SERIF,
|
|
329
|
+
self.font
|
|
330
|
+
.postscript_name()
|
|
331
|
+
.is_some_and(|n| n.contains("Serif")),
|
|
332
|
+
);
|
|
333
|
+
flags.set(FontFlags::FIXED_PITCH, self.font.is_monospaced());
|
|
334
|
+
flags.set(FontFlags::ITALIC, self.font.italic_angle() != 0.0);
|
|
335
|
+
flags.insert(FontFlags::SYMBOLIC);
|
|
336
|
+
flags.insert(FontFlags::SMALL_CAP);
|
|
337
|
+
|
|
338
|
+
let bbox = {
|
|
339
|
+
Rect::from_ltrb(
|
|
340
|
+
to_pdf_units(global_bbox.left()),
|
|
341
|
+
to_pdf_units(global_bbox.top()),
|
|
342
|
+
to_pdf_units(global_bbox.right()),
|
|
343
|
+
to_pdf_units(global_bbox.bottom()),
|
|
344
|
+
)
|
|
345
|
+
.unwrap()
|
|
346
|
+
}
|
|
347
|
+
.to_pdf_rect();
|
|
348
|
+
|
|
349
|
+
let italic_angle = self.font.italic_angle();
|
|
350
|
+
let ascender = to_pdf_units(self.font.ascent());
|
|
351
|
+
let descender = to_pdf_units(self.font.descent());
|
|
352
|
+
let cap_height = self.font.cap_height().map(to_pdf_units).unwrap_or(ascender);
|
|
353
|
+
let stem_v = 10.0 + 0.244 * (self.font.weight() - 50.0);
|
|
354
|
+
|
|
355
|
+
let mut font_descriptor = chunk.font_descriptor(descriptor_ref);
|
|
356
|
+
font_descriptor
|
|
357
|
+
.name(Name(base_font.as_bytes()))
|
|
358
|
+
.flags(flags)
|
|
359
|
+
.bbox(bbox)
|
|
360
|
+
.italic_angle(italic_angle)
|
|
361
|
+
.ascent(ascender)
|
|
362
|
+
.descent(descender)
|
|
363
|
+
.cap_height(cap_height)
|
|
364
|
+
.stem_v(stem_v);
|
|
365
|
+
|
|
366
|
+
if !sc.serialize_settings().pdf_version().deprecates_cid_set() {
|
|
367
|
+
font_descriptor.cid_set(cid_set_ref);
|
|
368
|
+
}
|
|
369
|
+
|
|
370
|
+
if is_cff {
|
|
371
|
+
font_descriptor.font_file3(data_ref);
|
|
372
|
+
} else {
|
|
373
|
+
font_descriptor.font_file2(data_ref);
|
|
374
|
+
}
|
|
375
|
+
|
|
376
|
+
font_descriptor.finish();
|
|
377
|
+
|
|
378
|
+
let cmap = {
|
|
379
|
+
let mut cmap = UnicodeCmap::new(CMAP_NAME, SYSTEM_INFO);
|
|
380
|
+
|
|
381
|
+
// For the .notdef glyph, it's fine if no mapping exists, since it is included
|
|
382
|
+
// even if it was not referenced in the text.
|
|
383
|
+
for g in 1..self.glyph_remapper.num_gids() {
|
|
384
|
+
let entry = self.cmap_entries.get(&g);
|
|
385
|
+
write_cmap_entry(&self.font, entry, sc, &mut cmap, g);
|
|
386
|
+
}
|
|
387
|
+
|
|
388
|
+
cmap
|
|
389
|
+
};
|
|
390
|
+
|
|
391
|
+
let cmap_stream = cmap.finish();
|
|
392
|
+
let cmap_stream =
|
|
393
|
+
FilterStreamBuilder::new_from_content_stream(&cmap_stream, &sc.serialize_settings())
|
|
394
|
+
.finish(&sc.serialize_settings());
|
|
395
|
+
let mut cmap = stream_chunk.cmap(cmap_ref, cmap_stream.encoded_data());
|
|
396
|
+
cmap_stream.write_filters(cmap.deref_mut().deref_mut());
|
|
397
|
+
cmap.writing_mode(WMode::Horizontal);
|
|
398
|
+
cmap.finish();
|
|
399
|
+
|
|
400
|
+
let mut stream = stream_chunk.stream(data_ref, font_stream.encoded_data());
|
|
401
|
+
font_stream.write_filters(stream.deref_mut());
|
|
402
|
+
if is_cff {
|
|
403
|
+
stream.pair(Name(b"Subtype"), Name(b"CIDFontType0C"));
|
|
404
|
+
}
|
|
405
|
+
|
|
406
|
+
stream.finish();
|
|
407
|
+
chunk_container.streams.fonts.push(stream_chunk);
|
|
408
|
+
|
|
409
|
+
Ok(())
|
|
410
|
+
}
|
|
411
|
+
}
|
|
412
|
+
|
|
413
|
+
/// Create a tag for a font subset.
|
|
414
|
+
pub(crate) fn subset_tag<T: Hash>(data: &T) -> String {
|
|
415
|
+
const BASE: u128 = 26;
|
|
416
|
+
let mut hash = stable_hash128(data);
|
|
417
|
+
let mut letter = [b'A'; SUBSET_TAG_LEN];
|
|
418
|
+
for l in letter.iter_mut() {
|
|
419
|
+
*l = b'A' + (hash % BASE) as u8;
|
|
420
|
+
hash /= BASE;
|
|
421
|
+
}
|
|
422
|
+
std::str::from_utf8(&letter).unwrap().to_string()
|
|
423
|
+
}
|
|
424
|
+
|
|
425
|
+
pub(crate) fn base_font_name<T: Hash>(font: &Font, data: &T) -> String {
|
|
426
|
+
const REST_LEN: usize = SUBSET_TAG_LEN + 1 + 1 + IDENTITY_H.len();
|
|
427
|
+
|
|
428
|
+
let postscript_name = font.postscript_name().unwrap_or("unknown");
|
|
429
|
+
let max_len = 127 - REST_LEN;
|
|
430
|
+
let trimmed = &postscript_name[..postscript_name.len().min(max_len)];
|
|
431
|
+
let subset_tag = subset_tag(&data);
|
|
432
|
+
|
|
433
|
+
format!("{subset_tag}+{trimmed}")
|
|
434
|
+
}
|
|
435
|
+
|
|
436
|
+
#[cfg_attr(feature = "comemo", comemo::memoize)]
|
|
437
|
+
fn subset_font(font: Font, glyph_remapper: &GlyphRemapper) -> KrillaResult<(Font, Rect)> {
|
|
438
|
+
let mut bbox: Option<Rect> = None;
|
|
439
|
+
|
|
440
|
+
let variation_coordinates = font
|
|
441
|
+
.variation_coordinates()
|
|
442
|
+
.iter()
|
|
443
|
+
.map(|v| (subsetter::Tag::new(v.0.get()), v.1.get()))
|
|
444
|
+
.collect::<Vec<_>>();
|
|
445
|
+
let font = subsetter::subset_with_variations(
|
|
446
|
+
font.font_data().as_ref(),
|
|
447
|
+
font.index(),
|
|
448
|
+
&variation_coordinates,
|
|
449
|
+
glyph_remapper,
|
|
450
|
+
)
|
|
451
|
+
.map_err(|e| KrillaError::Font(font.clone(), format!("failed to subset font: {e}")))
|
|
452
|
+
.and_then(|data| {
|
|
453
|
+
Font::new(Arc::new(data).into(), 0).ok_or(KrillaError::Font(
|
|
454
|
+
font.clone(),
|
|
455
|
+
"failed to subset font".to_string(),
|
|
456
|
+
))
|
|
457
|
+
})?;
|
|
458
|
+
let global_bbox = font.bbox();
|
|
459
|
+
|
|
460
|
+
for g in 0..font.num_glyphs() {
|
|
461
|
+
if let Some(path_bbox) = compute_bbox(&font, skrifa::GlyphId::new(g)) {
|
|
462
|
+
bbox = bbox
|
|
463
|
+
.map(|mut r| {
|
|
464
|
+
r.expand(&path_bbox);
|
|
465
|
+
r
|
|
466
|
+
})
|
|
467
|
+
.or(Some(path_bbox));
|
|
468
|
+
}
|
|
469
|
+
}
|
|
470
|
+
|
|
471
|
+
Ok((font, bbox.unwrap_or(global_bbox)))
|
|
472
|
+
}
|
|
473
|
+
|
|
474
|
+
#[cfg_attr(feature = "comemo", comemo::memoize)]
|
|
475
|
+
fn compute_bbox(font: &Font, glyph: skrifa::GlyphId) -> Option<Rect> {
|
|
476
|
+
let outline_glyphs = font.outline_glyphs();
|
|
477
|
+
|
|
478
|
+
if let Some(outline_glyph) = outline_glyphs.get(glyph) {
|
|
479
|
+
let mut glyph_builder = OutlineBuilder::new();
|
|
480
|
+
let _ = outline_glyph.draw(
|
|
481
|
+
DrawSettings::unhinted(Size::unscaled(), LocationRef::default()),
|
|
482
|
+
&mut glyph_builder,
|
|
483
|
+
);
|
|
484
|
+
glyph_builder.finish().map(|p| Rect::from_tsp(p.bounds()))
|
|
485
|
+
} else {
|
|
486
|
+
None
|
|
487
|
+
}
|
|
488
|
+
}
|