pub fn compile(map: &CharacterMap, ctx: &Context) -> Bytes {
    let records = ctx.cmap_encoding_records.as_ref().unwrap_or_else(|| {
        let exceeds_bmp = map.keys().last().map_or(false, |&x| x > MAX_BMP_SCALER);
        if exceeds_bmp {
            &DEFAULT_FULL_RECORDS
        } else {
            &DEFAULT_BMP_RECORDS
        }
    });

    let mut subtables: HashMap<RecordFormat, Bytes> = records
        .iter()
        .map(|x| x.format)
        .unique()
        .map(|format| {
            let subtable = match format {
                RecordFormat::Format4 => format4::compile(map, ctx),
                RecordFormat::Format12 => format12::compile(map),
            };

            (format, subtable)
        })
        .collect();

    let fixed_size = CONSTANT_SIZE + (records.len() * ENCODING_RECORD_SIZE);
    let length = fixed_size + subtables.values().map(Bytes::len).sum::<usize>();
    let mut buf = BytesMut::with_capacity(length);

    let version: u16 = 0;
    buf.put_u16(version);

    let num_tables: u16 = records.len() as u16;
    buf.put_u16(num_tables);

    let mut subtable_offsets: HashMap<RecordFormat, u32> = HashMap::new();
    let mut subtable_offset: u32 = fixed_size as u32;

    for record in records {
        let platform_id = record.encoding.platform_id();
        buf.put_u16(platform_id);

        let encoding_id = record.encoding.encoding_id();
        buf.put_u16(encoding_id);

        if let Some(&offset) = subtable_offsets.get(&record.format) {
            // reuse already registered subtable
            buf.put_u32(offset);
        } else {
            // register subtable format
            buf.put_u32(subtable_offset);
            subtable_offsets.insert(record.format, subtable_offset);
            subtable_offset += subtables[&record.format].len() as u32;
        }
    }

    for format in records.iter().map(|x| x.format).unique() {
        let subtable = subtables.get_mut(&format).unwrap();
        buf.put(subtable);
    }

    assert_eq!(length, buf.len());

    buf.freeze()
}
/// Returns a `cmap` table for the given character map.
///
/// # Example
///
/// ```
/// # use informa::ctx::Context;
/// # use informa::data::CharacterMap;
/// # use informa::sfnt::tables::cmap::compile;
/// let ctx = Context::default();
/// let mut map = CharacterMap::new();
/// map.insert('A', 1);
/// map.insert('B', 2);
/// map.insert('C', 3);
/// let table = compile(&map, &ctx);
/// ```
Returns a `cmap` table for the given character map.
Returns a ` cmap ` table for the given character map .
pub fn compile(map: &CharacterMap, ctx: &Context) -> Bytes {
    let records = ctx.cmap_encoding_records.as_ref().unwrap_or_else(|| {
        let exceeds_bmp = map.keys().last().map_or(false, |&x| x > MAX_BMP_SCALER);
        if exceeds_bmp {
            &DEFAULT_FULL_RECORDS
        } else {
            &DEFAULT_BMP_RECORDS
        }
    });
    let mut subtables: HashMap<RecordFormat, Bytes> = records
        .iter()
        .map(|x| x.format)
        .unique()
        .map(|format| {
            let subtable = match format {
                RecordFormat::Format4 => format4::compile(map, ctx),
                RecordFormat::Format12 => format12::compile(map),
            };
            (format, subtable)
        })
        .collect();
    let fixed_size = CONSTANT_SIZE + (records.len() * ENCODING_RECORD_SIZE);
    let length = fixed_size + subtables.values().map(Bytes::len).sum::<usize>();
    let mut buf = BytesMut::with_capacity(length);
    let version: u16 = 0;
    buf.put_u16(version);
    let num_tables: u16 = records.len() as u16;
    buf.put_u16(num_tables);
    let mut subtable_offsets: HashMap<RecordFormat, u32> = HashMap::new();
    let mut subtable_offset: u32 = fixed_size as u32;
    for record in records {
        let platform_id = record.encoding.platform_id();
        buf.put_u16(platform_id);
        let encoding_id = record.encoding.encoding_id();
        buf.put_u16(encoding_id);
        if let Some(&offset) = subtable_offsets.get(&record.format) {
            // reuse already registered subtable
            buf.put_u32(offset);
        } else {
            // register subtable format
            buf.put_u32(subtable_offset);
            subtable_offsets.insert(record.format, subtable_offset);
            subtable_offset += subtables[&record.format].len() as u32;
        }
    }
    for format in records.iter().map(|x| x.format).unique() {
        let subtable = subtables.get_mut(&format).unwrap();
        buf.put(subtable);
    }
    assert_eq!(length, buf.len());
    buf.freeze()
}
pub fn compile ( map : & CharacterMap , ctx : & Context ) -> Bytes { let records = ctx . cmap_encoding_records . as_ref ( ) . unwrap_or_else ( | | { let exceeds_bmp = map . keys ( ) . last ( ) . map_or ( false , | & x | x > MAX_BMP_SCALER ) ; if exceeds_bmp { & DEFAULT_FULL_RECORDS } else { & DEFAULT_BMP_RECORDS } } ) ; let mut subtables : HashMap < RecordFormat , Bytes > = records . iter ( ) . map ( | x | x . format ) . unique ( ) . map ( | format | { let subtable = match format { RecordFormat :: Format4 => format4 :: compile ( map , ctx ) , RecordFormat :: Format12 => format12 :: compile ( map ) , } ; ( format , subtable ) } ) . collect ( ) ; let fixed_size = CONSTANT_SIZE + ( records . len ( ) * ENCODING_RECORD_SIZE ) ; let length = fixed_size + subtables . values ( ) . map ( Bytes :: len ) . sum :: < usize > ( ) ; let mut buf = BytesMut :: with_capacity ( length ) ; let version : u16 = 0 ; buf . put_u16 ( version ) ; let num_tables : u16 = records . len ( ) as u16 ; buf . put_u16 ( num_tables ) ; let mut subtable_offsets : HashMap < RecordFormat , u32 > = HashMap :: new ( ) ; let mut subtable_offset : u32 = fixed_size as u32 ; for record in records { let platform_id = record . encoding . platform_id ( ) ; buf . put_u16 ( platform_id ) ; let encoding_id = record . encoding . encoding_id ( ) ; buf . put_u16 ( encoding_id ) ; if let Some ( & offset ) = subtable_offsets . get ( & record . format ) { // reuse already registered subtable buf . put_u32 ( offset ) ; } else { // register subtable format buf . put_u32 ( subtable_offset ) ; subtable_offsets . insert ( record . format , subtable_offset ) ; subtable_offset += subtables [ & record . format ] . len ( ) as u32 ; } } for format in records . iter ( ) . map ( | x | x . format ) . unique ( ) { let subtable = subtables . get_mut ( & format ) . unwrap ( ) ; buf . put ( subtable ) ; } assert_eq ! ( length buf len ( ) ) ; buf . freeze ( ) }
Returns a `cmap` table for the given character map.
Returns a ` cmap ` table for the given character map .
map
&CharacterMap
ctx
&Context
map
&CharacterMap
ctx
&Context
example
None
use informa::ctx::context;
None
use informa::data::charactermap;
None
use informa::sfnt::tables::cmap::compile;
let ctx = Context::default();
let mut map = CharacterMap::new();
map.insert('A', 1);
map.insert('B', 2);
map.insert('C', 3);
let table = compile(↦, &ctx);
```
let ctx = Context :: default () ; let mut map = CharacterMap :: new () ; map . insert ( ' A ' 1 ) ; map . insert ( ' B ' 2 ) ; map . insert ( ' C ' 3 ) ; let table = compile ( ↦ &ctx ) ; ` ` `