1
- use std:: cell:: RefCell ;
2
1
use crate :: font:: { Font , FontIdentifier } ;
3
2
use crate :: graphics_state:: GraphicsStates ;
4
3
use crate :: object:: cid_font:: CIDFont ;
@@ -22,6 +21,7 @@ use float_cmp::approx_eq;
22
21
use pdf_writer:: types:: TextRenderingMode ;
23
22
use pdf_writer:: { Content , Finish , Name , Str , TextStr } ;
24
23
use skrifa:: GlyphId ;
24
+ use std:: cell:: RefCell ;
25
25
use std:: ops:: Range ;
26
26
use std:: sync:: Arc ;
27
27
use tiny_skia_path:: { FiniteF32 , NormalizedF32 , Path , PathSegment , Rect , Size , Transform } ;
@@ -369,7 +369,7 @@ impl ContentBuilder {
369
369
370
370
let font_container = sc. create_or_get_font_container ( font. clone ( ) ) ;
371
371
372
- let spanned = TextSpanner :: new ( glyphs, text) ;
372
+ let spanned = TextSpanner :: new ( glyphs, text, font_container ) ;
373
373
374
374
for fragment in spanned {
375
375
if let Some ( text) = fragment. actual_text ( ) {
@@ -379,11 +379,7 @@ impl ContentBuilder {
379
379
actual_text. properties ( ) . actual_text ( TextStr ( text) ) ;
380
380
}
381
381
382
- let segmented = GlyphGrouper :: new (
383
- font_container,
384
- fragment. glyphs ( ) ,
385
- text,
386
- ) ;
382
+ let segmented = GlyphGrouper :: new ( font_container, fragment. glyphs ( ) ) ;
387
383
388
384
for glyph_group in segmented {
389
385
sb. encode_consecutive_glyph_run (
@@ -770,58 +766,54 @@ impl Glyph {
770
766
}
771
767
}
772
768
773
- pub enum PdfFont < ' a > {
774
- Type3 ( & ' a Type3Font ) ,
775
- CID ( & ' a CIDFont ) ,
769
+ pub enum PdfFontMut < ' a > {
770
+ Type3 ( & ' a mut Type3Font ) ,
771
+ CID ( & ' a mut CIDFont ) ,
776
772
}
777
773
778
- impl PdfFont < ' _ > {
774
+ impl < ' a > PdfFontMut < ' a > {
779
775
pub fn identifier ( & self ) -> FontIdentifier {
780
776
match self {
781
- PdfFont :: Type3 ( t3) => t3. identifier ( ) ,
782
- PdfFont :: CID ( cid) => cid. identifier ( ) ,
777
+ PdfFontMut :: Type3 ( t3) => t3. identifier ( ) ,
778
+ PdfFontMut :: CID ( cid) => cid. identifier ( ) ,
783
779
}
784
780
}
785
781
786
782
pub fn to_font_units ( & self , val : f32 ) -> f32 {
787
783
match self {
788
- PdfFont :: Type3 ( t3) => t3. to_pdf_font_units ( val) ,
789
- PdfFont :: CID ( cid) => cid. to_pdf_font_units ( val) ,
784
+ PdfFontMut :: Type3 ( t3) => t3. to_pdf_font_units ( val) ,
785
+ PdfFontMut :: CID ( cid) => cid. to_pdf_font_units ( val) ,
790
786
}
791
787
}
792
788
793
789
pub fn advance_width ( & self , pdf_glyph : PDFGlyph ) -> Option < f32 > {
794
790
match ( self , pdf_glyph) {
795
- ( PdfFont :: Type3 ( t3) , PDFGlyph :: Type3 ( gid) ) => t3. advance_width ( gid) ,
796
- ( PdfFont :: CID ( cid_font) , PDFGlyph :: CID ( cid) ) => cid_font. advance_width ( cid) ,
791
+ ( PdfFontMut :: Type3 ( t3) , PDFGlyph :: Type3 ( gid) ) => t3. advance_width ( gid) ,
792
+ ( PdfFontMut :: CID ( cid_font) , PDFGlyph :: CID ( cid) ) => cid_font. advance_width ( cid) ,
797
793
_ => None ,
798
794
}
799
795
}
800
- }
801
796
802
- pub enum PdfFontMut < ' a > {
803
- Type3 ( & ' a mut Type3Font ) ,
804
- CID ( & ' a mut CIDFont ) ,
805
- }
806
-
807
- impl PdfFontMut < ' _ > {
808
- fn pdf_font ( & self ) -> PdfFont {
809
- match self {
810
- PdfFontMut :: Type3 ( t3) => PdfFont :: Type3 ( t3) ,
811
- PdfFontMut :: CID ( cid) => PdfFont :: CID ( cid) ,
797
+ pub fn get_codepoints ( & self , pdf_glyph : PDFGlyph ) -> Option < & str > {
798
+ match ( self , pdf_glyph) {
799
+ ( PdfFontMut :: Type3 ( t3) , PDFGlyph :: Type3 ( gid) ) => t3. get_codepoints ( gid) ,
800
+ ( PdfFontMut :: CID ( cid_font) , PDFGlyph :: CID ( cid) ) => cid_font. get_codepoints ( cid) ,
801
+ _ => None ,
812
802
}
813
803
}
814
804
815
- pub fn identifier ( & self ) -> FontIdentifier {
816
- self . pdf_font ( ) . identifier ( )
817
- }
818
-
819
- pub fn to_font_units ( & self , val : f32 ) -> f32 {
820
- self . pdf_font ( ) . to_font_units ( val)
821
- }
822
-
823
- pub fn advance_width ( & self , pdf_glyph : PDFGlyph ) -> Option < f32 > {
824
- self . pdf_font ( ) . advance_width ( pdf_glyph)
805
+ pub fn set_codepoints ( & mut self , pdf_glyph : PDFGlyph , text : String ) -> Option < ( ) > {
806
+ match ( self , pdf_glyph) {
807
+ ( PdfFontMut :: Type3 ( t3) , PDFGlyph :: Type3 ( gid) ) => {
808
+ t3. set_codepoints ( gid, text) ;
809
+ Some ( ( ) )
810
+ }
811
+ ( PdfFontMut :: CID ( cid_font) , PDFGlyph :: CID ( cid) ) => {
812
+ cid_font. set_codepoints ( cid, text) ;
813
+ Some ( ( ) )
814
+ }
815
+ _ => None ,
816
+ }
825
817
}
826
818
}
827
819
@@ -846,43 +838,80 @@ impl TextSpan<'_> {
846
838
}
847
839
}
848
840
849
- pub struct TextSpanner < ' a > {
841
+ pub struct TextSpanner < ' a , ' b > {
850
842
slice : & ' a [ Glyph ] ,
843
+ font_container : & ' b RefCell < FontContainer > ,
851
844
text : & ' a str ,
852
845
}
853
846
854
- impl < ' a > TextSpanner < ' a > {
855
- pub fn new ( slice : & ' a [ Glyph ] , text : & ' a str ) -> Self {
856
- Self { slice, text }
847
+ impl < ' a , ' b > TextSpanner < ' a , ' b > {
848
+ pub fn new (
849
+ slice : & ' a [ Glyph ] ,
850
+ text : & ' a str ,
851
+ font_container : & ' b RefCell < FontContainer > ,
852
+ ) -> Self {
853
+ Self {
854
+ slice,
855
+ text,
856
+ font_container,
857
+ }
857
858
}
858
859
}
859
860
860
- impl < ' a > Iterator for TextSpanner < ' a > {
861
+ impl < ' a > Iterator for TextSpanner < ' a , ' _ > {
861
862
type Item = TextSpan < ' a > ;
862
863
863
864
fn next ( & mut self ) -> Option < Self :: Item > {
864
- let func = |g : & Glyph | g. range . clone ( ) ;
865
+ let func = |g : & Glyph | {
866
+ let mut font_container = self . font_container . borrow_mut ( ) ;
867
+ let pdf_glyph = font_container. add_glyph ( g. glyph_id ) ;
868
+ let font_identifier = font_container. font_identifier ( g. glyph_id ) . unwrap ( ) ;
869
+ let mut pdf_font = font_container
870
+ . get_from_identifier_mut ( font_identifier. clone ( ) )
871
+ . unwrap ( ) ;
872
+
873
+ let range = g. range . clone ( ) ;
874
+ let text = & self . text [ range. clone ( ) ] ;
875
+ let codepoints = pdf_font. get_codepoints ( pdf_glyph) ;
876
+ let incompatible_codepoint = codepoints. is_some ( ) && codepoints != Some ( text) ;
877
+
878
+ if !incompatible_codepoint {
879
+ pdf_font. set_codepoints ( pdf_glyph, text. to_string ( ) ) ;
880
+ }
865
881
866
- let mut same_range = None ;
882
+ ( range, incompatible_codepoint)
883
+ } ;
884
+
885
+ let mut use_span = None ;
867
886
let mut count = 1 ;
868
887
869
888
let mut iter = self . slice . iter ( ) ;
870
- let first = ( func) ( iter. next ( ) ?) ;
871
- let mut last_range = first. clone ( ) ;
889
+ let ( first_range, first_incompatible) = ( func) ( iter. next ( ) ?) ;
890
+
891
+ let mut last_range = first_range. clone ( ) ;
872
892
873
893
while let Some ( next) = iter. next ( ) {
874
- let next_range = func ( next) ;
894
+ let ( next_range, next_incompatible ) = func ( next) ;
875
895
876
- match same_range {
896
+ match use_span {
877
897
None => {
878
- same_range = Some ( last_range == next_range) ;
898
+ if first_incompatible {
899
+ use_span = Some ( true ) ;
900
+ break ;
901
+ }
902
+
903
+ use_span = Some ( last_range == next_range) ;
879
904
}
880
905
Some ( true ) => {
881
- if last_range != next_range {
906
+ if next_incompatible || last_range != next_range {
882
907
break ;
883
908
}
884
909
}
885
910
Some ( false ) => {
911
+ if next_incompatible {
912
+ break ;
913
+ }
914
+
886
915
if last_range == next_range {
887
916
count -= 1 ;
888
917
break ;
@@ -897,8 +926,8 @@ impl<'a> Iterator for TextSpanner<'a> {
897
926
let ( head, tail) = self . slice . split_at ( count) ;
898
927
self . slice = tail;
899
928
900
- let fragment = match same_range . unwrap_or ( false ) {
901
- true => TextSpan :: Spanned ( head, & self . text [ first ] ) ,
929
+ let fragment = match use_span . unwrap_or ( false ) {
930
+ true => TextSpan :: Spanned ( head, & self . text [ first_range ] ) ,
902
931
false => TextSpan :: Unspanned ( head) ,
903
932
} ;
904
933
Some ( fragment)
@@ -931,15 +960,13 @@ impl GlyphGroup {
931
960
pub struct GlyphGrouper < ' a , ' b > {
932
961
font_container : & ' b RefCell < FontContainer > ,
933
962
slice : & ' a [ Glyph ] ,
934
- text : & ' a str ,
935
963
}
936
964
937
965
impl < ' a , ' b > GlyphGrouper < ' a , ' b > {
938
- pub fn new ( font_container : & ' b RefCell < FontContainer > , slice : & ' a [ Glyph ] , text : & ' a str ) -> Self {
966
+ pub fn new ( font_container : & ' b RefCell < FontContainer > , slice : & ' a [ Glyph ] ) -> Self {
939
967
Self {
940
968
font_container,
941
969
slice,
942
- text,
943
970
}
944
971
}
945
972
}
@@ -956,8 +983,7 @@ impl<'a> Iterator for GlyphGrouper<'a, '_> {
956
983
}
957
984
958
985
let func = |g : & Glyph | {
959
- let mut font_container = self . font_container . borrow_mut ( ) ;
960
- font_container. add_glyph ( g. glyph_id ) ;
986
+ let font_container = self . font_container . borrow_mut ( ) ;
961
987
let font_identifier = font_container. font_identifier ( g. glyph_id ) . unwrap ( ) ;
962
988
963
989
GlyphProps {
@@ -1003,12 +1029,10 @@ impl<'a> Iterator for GlyphGrouper<'a, '_> {
1003
1029
let pdf_glyph = match pdf_font {
1004
1030
PdfFontMut :: Type3 ( ref mut t3) => {
1005
1031
let gid = t3. get_gid ( g. glyph_id ) . unwrap ( ) ;
1006
- t3. set_codepoints ( gid, self . text [ g. range . clone ( ) ] . to_string ( ) ) ;
1007
1032
PDFGlyph :: Type3 ( gid)
1008
1033
}
1009
1034
PdfFontMut :: CID ( ref mut cid_font) => {
1010
1035
let cid = cid_font. get_cid ( g. glyph_id ) . unwrap ( ) ;
1011
- cid_font. set_codepoints ( cid, self . text [ g. range . clone ( ) ] . to_string ( ) ) ;
1012
1036
PDFGlyph :: CID ( cid)
1013
1037
}
1014
1038
} ;
0 commit comments