@@ -18,17 +18,30 @@ pub struct TestFont {
18
18
pub const TTF : FileType = FileType { pattern : "*.ttf" } ;
19
19
20
20
impl < ' a > FileTypeConvert < TestFont > for FileType < ' a > {
21
+ #[ cfg( not( target_family = "wasm" ) ) ]
21
22
fn from_testable ( & self , t : & Testable ) -> Option < TestFont > {
22
23
self . applies ( t)
23
24
. then ( || TestFont :: new ( & t. filename ) )
24
25
. transpose ( )
25
26
. unwrap_or ( None )
26
27
}
28
+
29
+ #[ cfg( target_family = "wasm" ) ]
30
+ fn from_testable ( & self , t : & Testable ) -> Option < TestFont > {
31
+ self . applies ( t)
32
+ . then ( || TestFont :: new_from_data ( & t. filename , t. contents . clone ( ) ) )
33
+ . transpose ( )
34
+ . unwrap_or ( None )
35
+ }
27
36
}
28
37
29
38
impl TestFont {
30
39
pub fn new ( filename : & str ) -> std:: io:: Result < TestFont > {
31
40
let font_data = std:: fs:: read ( filename) ?;
41
+ TestFont :: new_from_data ( filename, font_data)
42
+ }
43
+
44
+ pub fn new_from_data ( filename : & str , font_data : Vec < u8 > ) -> std:: io:: Result < TestFont > {
32
45
let mut fnt = TestFont {
33
46
filename : filename. to_string ( ) ,
34
47
font_data,
@@ -41,9 +54,23 @@ impl TestFont {
41
54
. mappings ( )
42
55
. map ( |( u, _gid) | u)
43
56
. collect ( ) ;
44
- fnt. _sibling_filenames = {
57
+
58
+ fnt. find_siblings ( ) ?;
59
+
60
+ if FontRef :: new ( & fnt. font_data ) . is_err ( ) {
61
+ return Err ( std:: io:: Error :: new (
62
+ ErrorKind :: InvalidData ,
63
+ "Can't parse font" ,
64
+ ) ) ;
65
+ }
66
+ Ok ( fnt)
67
+ }
68
+
69
+ #[ cfg( not( target_family = "wasm" ) ) ]
70
+ fn find_siblings ( & mut self ) -> std:: io:: Result < ( ) > {
71
+ self . _sibling_filenames = {
45
72
// All other TTF files in same directory
46
- let directory = Path :: new ( & fnt . filename )
73
+ let directory = Path :: new ( & self . filename )
47
74
. parent ( )
48
75
. ok_or ( std:: io:: Error :: new (
49
76
ErrorKind :: NotFound ,
@@ -59,19 +86,19 @@ impl TestFont {
59
86
paths
60
87
. flatten ( )
61
88
. filter ( |x| x. path ( ) . extension ( ) . map_or ( false , |ext| ext == "ttf" ) )
62
- . filter ( |x| x. path ( ) . to_string_lossy ( ) != fnt . filename )
89
+ . filter ( |x| x. path ( ) . to_string_lossy ( ) != self . filename )
63
90
. map ( |x| x. path ( ) . to_string_lossy ( ) . to_string ( ) )
64
91
. collect ( )
65
92
} ;
93
+ Ok ( ( ) )
94
+ }
66
95
67
- if FontRef :: new ( & fnt. font_data ) . is_err ( ) {
68
- return Err ( std:: io:: Error :: new (
69
- ErrorKind :: InvalidData ,
70
- "Can't parse font" ,
71
- ) ) ;
72
- }
73
- Ok ( fnt)
96
+ #[ cfg( target_family = "wasm" ) ]
97
+ fn find_siblings ( & mut self ) -> std:: io:: Result < ( ) > {
98
+ self . _sibling_filenames = vec ! [ ] ;
99
+ Ok ( ( ) )
74
100
}
101
+
75
102
pub fn font ( & self ) -> FontRef {
76
103
#[ allow( clippy:: expect_used) ] // We just tested for it in the initializer
77
104
FontRef :: new ( & self . font_data ) . expect ( "Can't happen" )
0 commit comments