Skip to content

Commit

Permalink
Merge pull request #23 from T0mstone/main
Browse files Browse the repository at this point in the history
Add cmap6 support
  • Loading branch information
simoncozens committed Dec 27, 2023
2 parents d96a0ba + 72760c7 commit a9965a7
Showing 1 changed file with 65 additions and 0 deletions.
65 changes: 65 additions & 0 deletions fonttools-rs/src/tables/cmap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,60 @@ impl Deserialize for cmap4 {
}
}

#[allow(non_camel_case_types, non_snake_case)]
#[derive(Clone, Debug, PartialEq, Eq, Serialize)]
/// A format 6 cmap subtable, used for mapping 16-bit characters
/// to glyph indexes when the character codes for a font fall into a single contiguous range.
pub struct cmap6 {
format: uint16,
length: uint16,
language: uint16,
firstCode: uint16,
entryCount: uint16,
glyphIdArray: Vec<uint16>,
}

impl cmap6 {
/// Creates a new cmap6 subtable for a given language ID, from a list of glyph IDs,
/// taken to correspond to a contiguous range of 16-bit characters, starting with `first_code`.
pub fn from_list(language_id: uint16, first_code: uint16, glyph_ids: Vec<uint16>) -> Self {
Self {
format: 6,
length: (glyph_ids.len() * 2 + 10) as uint16,
language: language_id,
firstCode: first_code,
entryCount: glyph_ids.len() as uint16,
glyphIdArray: glyph_ids,
}
}

fn to_mapping(&self) -> BTreeMap<uint32, uint16> {
(self.firstCode as uint32..)
.zip(self.glyphIdArray.iter().copied())
.collect()
}
}

impl Deserialize for cmap6 {
fn from_bytes(c: &mut ReaderContext) -> Result<Self, DeserializationError> {
let format: uint16 = c.de()?;
let length: uint16 = c.de()?;
let language: uint16 = c.de()?;
let first_code: uint16 = c.de()?;
let entry_count: uint16 = c.de()?;
let remainder = length as usize - 10;
let glyph_id_array: Vec<u16> = c.de_counted(remainder).unwrap_or_default();
Ok(cmap6 {
format,
length,
language,
firstCode: first_code,
entryCount: entry_count,
glyphIdArray: glyph_id_array,
})
}
}

tables!(
cmap12 {
uint16 format
Expand Down Expand Up @@ -627,6 +681,17 @@ impl Deserialize for cmap {
uvs_mapping: None,
});
}
[0x0, 0x06] => {
let subtable: cmap6 = c.de()?;
subtables.push(CmapSubtable {
format: 6,
platformID: er.platformID,
encodingID: er.encodingID,
languageID: subtable.language,
mapping: subtable.to_mapping(),
uvs_mapping: None,
});
}
[0x0, 0x0c] => {
let subtable: cmap12 = c.de()?;
subtables.push(CmapSubtable {
Expand Down

0 comments on commit a9965a7

Please sign in to comment.