@@ -10,6 +10,7 @@ use winapi::shared::minwindef::{BOOL, FALSE, TRUE};
1010use winapi:: shared:: winerror:: S_OK ;
1111use winapi:: um:: dwrite:: IDWriteFontCollectionLoader ;
1212use winapi:: um:: dwrite:: { IDWriteFont , IDWriteFontCollection , IDWriteFontFamily } ;
13+ use winapi:: um:: winnt:: HRESULT ;
1314use wio:: com:: ComPtr ;
1415
1516use super :: { DWriteFactory , Font , FontDescriptor , FontFace , FontFamily } ;
@@ -108,62 +109,91 @@ impl FontCollection {
108109 unsafe { ( * self . native . get ( ) ) . GetFontFamilyCount ( ) }
109110 }
110111
112+ #[ deprecated( note = "Use `font_family` instead." ) ]
111113 pub fn get_font_family ( & self , index : u32 ) -> FontFamily {
114+ self . font_family ( index) . unwrap ( )
115+ }
116+
117+ /// Returns the [`FontFamily`] at the given index.
118+ pub fn font_family ( & self , index : u32 ) -> Result < FontFamily , HRESULT > {
119+ let mut family: * mut IDWriteFontFamily = ptr:: null_mut ( ) ;
112120 unsafe {
113- let mut family: * mut IDWriteFontFamily = ptr:: null_mut ( ) ;
114121 let hr = ( * self . native . get ( ) ) . GetFontFamily ( index, & mut family) ;
115- assert ! ( hr == 0 ) ;
116- FontFamily :: take ( ComPtr :: from_raw ( family) )
122+ if hr != S_OK {
123+ return Err ( hr) ;
124+ }
125+ Ok ( FontFamily :: take ( ComPtr :: from_raw ( family) ) )
117126 }
118127 }
119128
120- // Find a font matching the given font descriptor in this
121- // font collection.
129+ #[ deprecated( note = "Use `font_from_descriptor` instead." ) ]
122130 pub fn get_font_from_descriptor ( & self , desc : & FontDescriptor ) -> Option < Font > {
123- if let Some ( family) = self . get_font_family_by_name ( & desc. family_name ) {
124- let font = family. get_first_matching_font ( desc. weight , desc. stretch , desc. style ) ;
131+ self . font_from_descriptor ( desc) . unwrap ( )
132+ }
133+
134+ /// Find a font matching the given font descriptor in this [`FontCollection`].
135+ pub fn font_from_descriptor ( & self , desc : & FontDescriptor ) -> Result < Option < Font > , HRESULT > {
136+ if let Some ( family) = self . font_family_by_name ( & desc. family_name ) ? {
137+ let font = family. first_matching_font ( desc. weight , desc. stretch , desc. style ) ?;
125138 // Exact matches only here
126139 if font. weight ( ) == desc. weight
127140 && font. stretch ( ) == desc. stretch
128141 && font. style ( ) == desc. style
129142 {
130- return Some ( font) ;
143+ return Ok ( Some ( font) ) ;
131144 }
132145 }
133146
134- None
147+ Ok ( None )
135148 }
136149
150+ #[ deprecated( note = "Use `font_from_face` instead." ) ]
137151 pub fn get_font_from_face ( & self , face : & FontFace ) -> Option < Font > {
152+ self . font_from_face ( face) . ok ( )
153+ }
154+
155+ /// Get a [`Font`] from the given [`FontFace`].
156+ pub fn font_from_face ( & self , face : & FontFace ) -> Result < Font , HRESULT > {
157+ let mut font: * mut IDWriteFont = ptr:: null_mut ( ) ;
138158 unsafe {
139- let mut font: * mut IDWriteFont = ptr:: null_mut ( ) ;
140159 let hr = ( * self . native . get ( ) ) . GetFontFromFontFace ( face. as_ptr ( ) , & mut font) ;
141- if hr != 0 {
142- return None ;
160+ if hr != S_OK {
161+ return Err ( hr ) ;
143162 }
144- Some ( Font :: take ( ComPtr :: from_raw ( font) ) )
163+ Ok ( Font :: take ( ComPtr :: from_raw ( font) ) )
145164 }
146165 }
147166
167+ #[ deprecated( note = "Use `font_family_by_name` instead." ) ]
148168 pub fn get_font_family_by_name ( & self , family_name : & str ) -> Option < FontFamily > {
169+ self . font_family_by_name ( family_name) . unwrap ( )
170+ }
171+
172+ /// Find a [`FontFamily`] with the given name. Returns `None` if no family
173+ /// with that name is found.
174+ pub fn font_family_by_name ( & self , family_name : & str ) -> Result < Option < FontFamily > , HRESULT > {
175+ let mut index: u32 = 0 ;
176+ let mut exists: BOOL = FALSE ;
149177 unsafe {
150- let mut index: u32 = 0 ;
151- let mut exists: BOOL = FALSE ;
152178 let hr = ( * self . native . get ( ) ) . FindFamilyName (
153179 family_name. to_wide_null ( ) . as_ptr ( ) ,
154180 & mut index,
155181 & mut exists,
156182 ) ;
157- assert ! ( hr == 0 ) ;
183+ if hr != S_OK {
184+ return Err ( hr) ;
185+ }
158186 if exists == FALSE {
159- return None ;
187+ return Ok ( None ) ;
160188 }
161189
162190 let mut family: * mut IDWriteFontFamily = ptr:: null_mut ( ) ;
163191 let hr = ( * self . native . get ( ) ) . GetFontFamily ( index, & mut family) ;
164- assert ! ( hr == 0 ) ;
192+ if hr != S_OK {
193+ return Err ( hr) ;
194+ }
165195
166- Some ( FontFamily :: take ( ComPtr :: from_raw ( family) ) )
196+ Ok ( Some ( FontFamily :: take ( ComPtr :: from_raw ( family) ) ) )
167197 }
168198 }
169199}
0 commit comments