@@ -614,6 +614,8 @@ impl ParsedFont {
614
614
map. insert ( sp, ' ' ) ;
615
615
}
616
616
617
+ map. insert ( 0 , '\0' ) ;
618
+
617
619
map
618
620
}
619
621
@@ -651,7 +653,7 @@ impl ParsedFont {
651
653
}
652
654
653
655
/// Generates a new font file from the used glyph IDs
654
- pub fn subset ( & self , glyph_ids : & [ ( u16 , char ) ] ) -> Result < SubsetFont , String > {
656
+ pub fn subset ( & self , glyph_ids : & BTreeMap < u16 , char > ) -> Result < SubsetFont , String > {
655
657
let glyph_mapping = glyph_ids
656
658
. iter ( )
657
659
. enumerate ( )
@@ -669,11 +671,9 @@ impl ParsedFont {
669
671
. map_err ( |e| e. to_string ( ) ) ?;
670
672
671
673
// https://docs.rs/allsorts/latest/allsorts/subset/fn.subset.html
672
- // Glyph id 0, corresponding to the .notdef glyph must always be present.
673
- let mut ids = vec ! [ 0 ] ;
674
- for glyph_id in glyph_ids {
675
- ids. push ( glyph_id. 0 ) ;
676
- }
674
+ // Glyph id 0, corresponding to the .notdef glyph is always present,
675
+ // because it is returned from get_used_glyphs_ids.
676
+ let ids: Vec < _ > = glyph_ids. keys ( ) . copied ( ) . collect ( ) ;
677
677
678
678
let font = allsorts_subset_browser:: subset:: subset ( & provider, & ids, & SubsetProfile :: Web )
679
679
. map_err ( |e| e. to_string ( ) ) ?;
@@ -685,41 +685,30 @@ impl ParsedFont {
685
685
}
686
686
687
687
/// Replace this function in the ParsedFont implementation
688
- pub ( crate ) fn generate_cid_to_unicode_map (
688
+ pub ( crate ) fn generate_cmap_string (
689
689
& self ,
690
690
font_id : & FontId ,
691
- glyph_ids : & BTreeMap < u16 , char > ,
691
+ gid_to_cid_map : & [ ( u16 , u16 ) ] ,
692
692
) -> String {
693
693
// Convert the glyph_ids map to a ToUnicodeCMap structure
694
- let mut mappings: BTreeMap < u32 , Vec < u32 > > = BTreeMap :: new ( ) ;
695
- for ( & glyph_id , & unicode ) in glyph_ids {
696
- mappings . insert ( glyph_id as u32 , vec ! [ unicode as u32 ] ) ;
697
- }
694
+ let mappings = gid_to_cid_map
695
+ . iter ( )
696
+ . map ( | & ( gid , cp ) | ( gid as u32 , vec ! [ cp as u32 ] ) )
697
+ . collect ( ) ;
698
698
699
699
// Create the CMap and generate its string representation
700
700
let cmap = ToUnicodeCMap { mappings } ;
701
701
cmap. to_cmap_string ( & font_id. 0 )
702
702
}
703
703
704
- pub ( crate ) fn generate_cid_to_gid (
705
- & self ,
706
- glyph_ids : & BTreeMap < u16 , char > ,
707
- ) -> BTreeMap < u16 , u16 > {
708
- let mut mappings: BTreeMap < u16 , u16 > = BTreeMap :: new ( ) ;
709
-
710
- for ( & glyph_id, _) in glyph_ids {
711
- let cid = self . index_to_cid ( glyph_id + 1 ) . unwrap ( ) ;
712
-
713
- mappings. insert ( glyph_id, cid) ;
714
- }
715
-
716
- mappings
704
+ pub ( crate ) fn generate_gid_to_cid_map ( & self , glyph_ids : & [ u16 ] ) -> Vec < ( u16 , u16 ) > {
705
+ glyph_ids
706
+ . iter ( )
707
+ . filter_map ( |gid| self . index_to_cid ( * gid) . map ( |cid| ( * gid, cid) ) )
708
+ . collect ( )
717
709
}
718
710
719
- pub ( crate ) fn get_normalized_widths_ttf (
720
- & self ,
721
- glyph_ids : & BTreeMap < u16 , char > ,
722
- ) -> Vec < lopdf:: Object > {
711
+ pub ( crate ) fn get_normalized_widths_ttf ( & self , glyph_ids : & [ u16 ] ) -> Vec < lopdf:: Object > {
723
712
let mut widths_list = Vec :: new ( ) ;
724
713
let mut current_low_gid = 0 ;
725
714
let mut current_high_gid = 0 ;
@@ -728,16 +717,16 @@ impl ParsedFont {
728
717
// scale the font width so that it sort-of fits into an 1000 unit square
729
718
let percentage_font_scaling = 1000.0 / ( self . font_metrics . units_per_em as f32 ) ;
730
719
731
- for gid in glyph_ids. keys ( ) {
732
- let width = match self . get_glyph_width_internal ( * gid) {
720
+ for & gid in glyph_ids {
721
+ let width = match self . get_glyph_width_internal ( gid) {
733
722
Some ( s) => s,
734
723
None => match self . get_space_width ( ) {
735
724
Some ( w) => w,
736
725
None => 0 ,
737
726
} ,
738
727
} ;
739
728
740
- if * gid == current_high_gid {
729
+ if gid == current_high_gid {
741
730
// subsequent GID
742
731
current_width_vec. push ( Integer ( ( width as f32 * percentage_font_scaling) as i64 ) ) ;
743
732
current_high_gid += 1 ;
@@ -747,7 +736,7 @@ impl ParsedFont {
747
736
widths_list. push ( Array ( std:: mem:: take ( & mut current_width_vec) ) ) ;
748
737
749
738
current_width_vec. push ( Integer ( ( width as f32 * percentage_font_scaling) as i64 ) ) ;
750
- current_low_gid = * gid;
739
+ current_low_gid = gid;
751
740
current_high_gid = gid + 1 ;
752
741
}
753
742
}
@@ -757,32 +746,19 @@ impl ParsedFont {
757
746
widths_list. push ( Array ( std:: mem:: take ( & mut current_width_vec) ) ) ;
758
747
759
748
widths_list
760
- /*
761
- let mut cmap = glyph_ids.iter()
762
- .filter_map(|(glyph_id, c)| {
763
- let (glyph_width, glyph_height) = self.get_glyph_size(*glyph_id)?;
764
- let k = *glyph_id as u32;
765
- let v = (*c as u32, glyph_width.abs() as u32, glyph_height.abs() as u32);
766
- Some((k, v))
767
- }).collect::<BTreeMap<_, _>>();
768
-
769
- cmap.insert(0, (0, 1000, 1000));
770
-
771
- widths.push((*glyph_id, width));
772
- */
773
749
}
774
750
775
751
pub ( crate ) fn get_normalized_widths_cff (
776
752
& self ,
777
- cid_to_gid : & BTreeMap < u16 , u16 > ,
753
+ gid_to_cid_map : & [ ( u16 , u16 ) ] ,
778
754
) -> Vec < lopdf:: Object > {
779
755
let mut widths_list = Vec :: new ( ) ;
780
756
781
757
// scale the font width so that it sort-of fits into an 1000 unit square
782
758
let percentage_font_scaling = 1000.0 / ( self . font_metrics . units_per_em as f32 ) ;
783
759
784
- for ( gid, cid) in cid_to_gid {
785
- let width = match self . get_glyph_width_internal ( * gid + 1 ) {
760
+ for & ( gid, cid) in gid_to_cid_map {
761
+ let width = match self . get_glyph_width_internal ( gid) {
786
762
Some ( s) => s,
787
763
None => match self . get_space_width ( ) {
788
764
Some ( w) => w,
@@ -792,8 +768,8 @@ impl ParsedFont {
792
768
793
769
let width = ( width as f32 * percentage_font_scaling) as i64 ;
794
770
795
- widths_list. push ( Integer ( * cid as i64 ) ) ;
796
- widths_list. push ( Integer ( * cid as i64 ) ) ;
771
+ widths_list. push ( Integer ( cid as i64 ) ) ;
772
+ widths_list. push ( Integer ( cid as i64 ) ) ;
797
773
widths_list. push ( Integer ( width) ) ;
798
774
}
799
775
0 commit comments