Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't "improve" glyph sets #79

Merged
merged 2 commits into from
Jul 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 1 addition & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ pdfium-render = "0.8.6"
termcolor = "1.2"
usvg = { version = "0.42.0", default-features = false }
tiny-skia = "0.11.4"
unicode-properties = "0.1.1"
resvg = { version = "0.42.0", default-features = false }
subsetter = {git = "https://github.com/typst/subsetter", rev = "4e0058b"}
ttf-parser = { version = "0.21.1" }
Expand All @@ -50,13 +49,12 @@ bench = false
[features]
default = ["image", "filters", "text"]
text = ["usvg/text", "resvg/text", "dep:siphasher",
"dep:subsetter", "dep:ttf-parser", "dep:unicode-properties",
"dep:subsetter", "dep:ttf-parser",
"dep:fontdb"]
image = ["dep:image"]
filters = ["image", "dep:tiny-skia", "resvg/raster-images"]

[dependencies]
unicode-properties = { workspace = true, optional = true }
miniz_oxide = { workspace = true }
once_cell = { workspace = true }
pdf-writer = { workspace = true }
Expand Down
4 changes: 2 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ Among the unsupported features are currently:
- The `spreadMethod` attribute of gradients
- Raster images are not color managed but use PDF's DeviceRGB color space
- A number of features that were added in SVG2, See
[here](https://github.com/RazrFalcon/resvg/blob/master/docs/svg2-changelog.md) for a more
comprehensive list.
[here](https://github.com/RazrFalcon/resvg/blob/master/docs/svg2-changelog.md) for a more
comprehensive list.
*/

mod render;
Expand Down
28 changes: 1 addition & 27 deletions src/render/text.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ use std::hash::Hash;
use std::sync::Arc;
use subsetter::GlyphRemapper;
use ttf_parser::{name_id, Face, GlyphId, PlatformId, Tag};
use unicode_properties::{GeneralCategory, UnicodeGeneralCategory};
use usvg::{Fill, Group, ImageKind, Node, PaintOrder, Stroke, Transform};

const CFF: Tag = Tag::from_bytes(b"CFF ");
Expand Down Expand Up @@ -154,8 +153,7 @@ pub fn write_font(

font_descriptor.finish();

let cmap =
create_cmap(&ttf, glyph_set, glyph_remapper).ok_or(SubsetError(font.id))?;
let cmap = create_cmap(glyph_set, glyph_remapper).ok_or(SubsetError(font.id))?;
chunk.cmap(cmap_ref, &cmap.finish());

// Subset and write the font's bytes.
Expand All @@ -173,33 +171,9 @@ pub fn write_font(

/// Create a /ToUnicode CMap.
fn create_cmap(
ttf: &Face,
glyph_set: &mut BTreeMap<u16, String>,
glyph_remapper: &GlyphRemapper,
) -> Option<UnicodeCmap> {
// For glyphs that have codepoints mapping to them in the font's cmap table,
// we prefer them over pre-existing text mappings from the document. Only
// things that don't have a corresponding codepoint (or only a private-use
// one) like the "Th" in Linux Libertine get the text of their first
// occurrences in the document instead.
for subtable in ttf.tables().cmap.into_iter().flat_map(|table| table.subtables) {
if !subtable.is_unicode() {
continue;
}

subtable.codepoints(|n| {
let Some(c) = std::char::from_u32(n) else { return };
if c.general_category() == GeneralCategory::PrivateUse {
return;
}

let Some(GlyphId(g)) = ttf.glyph_index(c) else { return };
if glyph_set.contains_key(&g) {
glyph_set.insert(g, c.into());
}
});
}

// Produce a reverse mapping from glyphs' CIDs to unicode strings.
let mut cmap = UnicodeCmap::new(CMAP_NAME, SYSTEM_INFO);
for (&g, text) in glyph_set.iter() {
Expand Down