Skip to content

Commit

Permalink
Fix subset tag for color fonts (#3960)
Browse files Browse the repository at this point in the history
  • Loading branch information
elegaanz committed Apr 19, 2024
1 parent b945742 commit 0bb45b3
Showing 1 changed file with 7 additions and 2 deletions.
9 changes: 7 additions & 2 deletions crates/typst-pdf/src/font.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use std::collections::BTreeMap;
use std::hash::Hash;
use std::sync::Arc;

use ecow::{eco_format, EcoString};
Expand Down Expand Up @@ -153,6 +154,7 @@ fn write_color_fonts(ctx: &mut PdfContext) {
let glyph_count = end - start;
let subset = &color_font.glyphs[start..end];
let mut widths = Vec::new();
let mut gids = Vec::new();

let scale_factor = font.ttf().units_per_em() as f32;

Expand Down Expand Up @@ -182,6 +184,7 @@ fn write_color_fonts(ctx: &mut PdfContext) {

// Use this stream as instructions to draw the glyph.
glyphs_to_instructions.push(instructions_stream_ref);
gids.push(color_glyph.gid);
}

// Write the Type3 font object.
Expand Down Expand Up @@ -229,10 +232,12 @@ fn write_color_fonts(ctx: &mut PdfContext) {
ctx.pdf.cmap(cmap_ref, &cmap.finish());

// Write the font descriptor.
gids.sort();
let subset_tag = subset_tag(&gids);
let postscript_name = font
.find_name(name_id::POST_SCRIPT_NAME)
.unwrap_or_else(|| "unknown".to_string());
let base_font = eco_format!("COLOR{font_index:x}+{postscript_name}");
let base_font = eco_format!("{subset_tag}+{postscript_name}");
write_font_descriptor(&mut ctx.pdf, descriptor_ref, &font, &base_font);

// Write the widths array
Expand Down Expand Up @@ -312,7 +317,7 @@ fn subset_font(font: &Font, glyphs: &[u16]) -> Arc<Vec<u8>> {
}

/// Produce a unique 6 letter tag for a glyph set.
fn subset_tag(glyphs: &BTreeMap<u16, EcoString>) -> EcoString {
fn subset_tag<T: Hash>(glyphs: &T) -> EcoString {
const LEN: usize = 6;
const BASE: u128 = 26;
let mut hash = typst::util::hash128(&glyphs);
Expand Down

0 comments on commit 0bb45b3

Please sign in to comment.