Skip to content

Commit f22c5de

Browse files
committedJun 11, 2024·
merge with main
1 parent 1411c4c commit f22c5de

File tree

1 file changed

+13
-10
lines changed

1 file changed

+13
-10
lines changed
 

‎src/render/text.rs

+13-10
Original file line numberDiff line numberDiff line change
@@ -154,13 +154,12 @@ pub fn write_font(
154154

155155
font_descriptor.finish();
156156

157-
let cmap = create_cmap(&ttf, glyph_set, glyph_remapper);
157+
let cmap =
158+
create_cmap(&ttf, glyph_set, glyph_remapper).ok_or(SubsetError(font.id))?;
158159
chunk.cmap(cmap_ref, &cmap.finish());
159160

160161
// Subset and write the font's bytes.
161-
let glyphs: Vec<_> = glyph_set.keys().copied().collect();
162-
let data = subset_font(&font.face_data, font.face_index, &glyphs, font.id)?;
163-
let data = subset_font(&font.face_data, font.face_index, &glyph_remapper);
162+
let data = subset_font(&font.face_data, font.face_index, glyph_remapper, font.id)?;
164163

165164
let mut stream = chunk.stream(data_ref, &data);
166165
stream.filter(Filter::FlateDecode);
@@ -173,7 +172,11 @@ pub fn write_font(
173172
}
174173

175174
/// Create a /ToUnicode CMap.
176-
fn create_cmap(ttf: &Face, glyph_set: &mut BTreeMap<u16, String>, glyph_remapper: &GlyphRemapper) -> UnicodeCmap {
175+
fn create_cmap(
176+
ttf: &Face,
177+
glyph_set: &mut BTreeMap<u16, String>,
178+
glyph_remapper: &GlyphRemapper,
179+
) -> Option<UnicodeCmap> {
177180
// For glyphs that have codepoints mapping to them in the font's cmap table,
178181
// we prefer them over pre-existing text mappings from the document. Only
179182
// things that don't have a corresponding codepoint (or only a private-use
@@ -200,24 +203,24 @@ fn create_cmap(ttf: &Face, glyph_set: &mut BTreeMap<u16, String>, glyph_remapper
200203
// Produce a reverse mapping from glyphs' CIDs to unicode strings.
201204
let mut cmap = UnicodeCmap::new(CMAP_NAME, SYSTEM_INFO);
202205
for (&g, text) in glyph_set.iter() {
203-
let new_gid = glyph_remapper.get(g).unwrap();
206+
let new_gid = glyph_remapper.get(g)?;
204207
if !text.is_empty() {
205208
cmap.pair_with_multiple(new_gid, text.chars());
206209
}
207210
}
208211

209-
cmap
212+
Some(cmap)
210213
}
211214

212215
fn subset_font(
213216
font_data: &[u8],
214217
index: u32,
215-
glyphs: &[u16],
218+
glyph_remapper: &GlyphRemapper,
216219
id: fontdb::ID,
217220
) -> Result<Vec<u8>> {
218-
fn subset_font(font_data: &[u8], index: u32, glyph_remapper: &GlyphRemapper) -> Vec<u8> {
219221
let data = font_data;
220-
let subsetted = subsetter::subset(data, index, glyph_remapper).unwrap();
222+
let subsetted =
223+
subsetter::subset(data, index, glyph_remapper).map_err(|_| SubsetError(id))?;
221224
let mut data = subsetted.as_ref();
222225

223226
// Extract the standalone CFF font program if applicable.

0 commit comments

Comments
 (0)