77//! [Tectonic]: https://tectonic-typesetting.github.io/
88
99#![ deny( clippy:: undocumented_unsafe_blocks) ]
10- #![ allow( clippy:: unnecessary_cast, missing_docs) ]
1110
1211use std:: marker:: PhantomData ;
1312use std:: mem:: ManuallyDrop ;
1413use std:: ptr:: NonNull ;
1514
15+ #[ doc( hidden) ]
1616pub mod sys;
1717
1818pub use sys:: gr_encform as EncForm ;
@@ -27,21 +27,26 @@ pub const BREAK_BEFORE_WORD: i32 = sys::gr_breakBeforeWord as i32;
2727#[ allow( clippy:: unnecessary_cast) ]
2828pub const BREAK_WORD : i32 = sys:: gr_breakWord as i32 ;
2929
30+ /// Error on attempting to set a feature to an invalid value, or setting an invalid feature.
3031pub struct FeatErr ( ( ) ) ;
3132
33+ /// An owned label for a feature
3234pub struct Label ( usize , NonNull < u8 > ) ;
3335
3436impl Label {
37+ /// Get the contents of this label as a slice
3538 pub fn as_bytes ( & self ) -> & [ u8 ] {
3639 // SAFETY: Pointer in `self.1` is guaranteed to be a valid array of length `self.0`
3740 unsafe { std:: slice:: from_raw_parts ( self . 1 . as_ptr ( ) , self . 0 ) }
3841 }
3942
43+ /// Get the contents of this label as a string
4044 pub fn as_str ( & self ) -> & str {
4145 // SAFETY: Array returned by `as_bytes` is guaranteed a valid UTF-8 array on Label creation.
4246 unsafe { std:: str:: from_utf8_unchecked ( self . as_bytes ( ) ) }
4347 }
4448
49+ /// Convert this label into a raw pointer
4550 pub fn into_raw ( self ) -> * mut u8 {
4651 let this = ManuallyDrop :: new ( self ) ;
4752 this. 1 . as_ptr ( )
@@ -55,6 +60,7 @@ impl Drop for Label {
5560 }
5661}
5762
63+ /// A feature of a [`Face`](FaceRef)
5864#[ derive( Copy , Clone ) ]
5965pub struct FeatureRef < ' a > (
6066 NonNull < sys:: gr_feature_ref > ,
@@ -66,26 +72,32 @@ impl FeatureRef<'_> {
6672 self . 0 . as_ptr ( ) . cast_const ( )
6773 }
6874
75+ /// Get the ID of this feature
6976 pub fn id ( self ) -> u32 {
7077 // SAFETY: Contained pointer guaranteed valid
7178 unsafe { sys:: gr_fref_id ( self . as_ptr ( ) ) }
7279 }
7380
81+ /// Get the number of values in this feature
7482 pub fn num_values ( self ) -> usize {
7583 // SAFETY: Contained pointer guaranteed valid
7684 unsafe { sys:: gr_fref_n_values ( self . as_ptr ( ) ) as usize }
7785 }
7886
87+ /// Get the value at a given index in this feature
7988 pub fn value ( self , idx : usize ) -> i16 {
8089 // SAFETY: Contained pointer guaranteed valid
8190 unsafe { sys:: gr_fref_value ( self . as_ptr ( ) , idx as u16 ) }
8291 }
8392
93+ /// Get the value for a feature-value
8494 pub fn feat_value ( self , feat : FeatureValRef < ' _ > ) -> u16 {
8595 // SAFETY: Contained pointer guaranteed valid
8696 unsafe { sys:: gr_fref_feature_value ( self . as_ptr ( ) , feat. as_ptr ( ) ) }
8797 }
8898
99+ /// Set the value for a feature-value. Errors if value or feature-value is invalid for this
100+ /// feature.
89101 pub fn set_feat_value ( self , mut feat : FeatureValMut < ' _ > , value : u16 ) -> Result < ( ) , FeatErr > {
90102 // SAFETY: Contained pointer guaranteed valid
91103 // Feat pointer guaranteed valid
@@ -98,6 +110,7 @@ impl FeatureRef<'_> {
98110 }
99111 }
100112
113+ /// Get the label for this feature for a given language
101114 pub fn label ( self , lang_id : u16 ) -> Option < Label > {
102115 let mut actual_id = lang_id;
103116 let mut len = 0 ;
@@ -113,6 +126,7 @@ impl FeatureRef<'_> {
113126 NonNull :: new ( ptr. cast ( ) ) . map ( |ptr| Label ( len as usize , ptr) )
114127 }
115128
129+ /// Get the label for the value of this feature at a given index for a given language
116130 pub fn value_label ( self , idx : usize , lang_id : u16 ) -> Option < Label > {
117131 let mut actual_id = lang_id;
118132 let mut len = 0 ;
@@ -130,6 +144,7 @@ impl FeatureRef<'_> {
130144 }
131145}
132146
147+ /// A feature-value reference. See [`FeatureVal`].
133148#[ derive( Copy , Clone ) ]
134149pub struct FeatureValRef < ' a > (
135150 NonNull < sys:: gr_feature_val > ,
@@ -142,6 +157,7 @@ impl FeatureValRef<'_> {
142157 }
143158}
144159
160+ /// A feature-value mutable reference. See [`FeatureVal`].
145161pub struct FeatureValMut < ' a > (
146162 NonNull < sys:: gr_feature_val > ,
147163 PhantomData < & ' a mut sys:: gr_feature_val > ,
@@ -153,13 +169,17 @@ impl FeatureValMut<'_> {
153169 }
154170}
155171
172+ /// A feature-value. Graphite isn't very clear on what this represents, but it seems to combine
173+ /// all possible values for a given language.
156174pub struct FeatureVal ( NonNull < sys:: gr_feature_val > ) ;
157175
158176impl FeatureVal {
177+ /// Convert into a borrowed reference.
159178 pub fn as_ref ( & self ) -> FeatureValRef < ' _ > {
160179 FeatureValRef ( self . 0 , PhantomData )
161180 }
162181
182+ /// Convert into a mutable reference.
163183 pub fn as_mut ( & mut self ) -> FeatureValMut < ' _ > {
164184 FeatureValMut ( self . 0 , PhantomData )
165185 }
@@ -172,6 +192,7 @@ impl Drop for FeatureVal {
172192 }
173193}
174194
195+ /// A typographic face - a typeface combined with a style.
175196#[ derive( Copy , Clone ) ]
176197pub struct FaceRef < ' a > ( NonNull < sys:: gr_face > , PhantomData < & ' a sys:: gr_face > ) ;
177198
@@ -191,30 +212,35 @@ impl<'a> FaceRef<'a> {
191212 self . 0 . as_ptr ( ) . cast_const ( )
192213 }
193214
215+ /// Number of feature references in this face
194216 pub fn num_feature_refs ( self ) -> usize {
195217 // SAFETY: Contained pointer guaranteed valid
196218 unsafe { sys:: gr_face_n_fref ( self . as_ptr ( ) ) as usize }
197219 }
198220
221+ /// Get the feature reference at a given index
199222 pub fn feature_ref ( self , idx : usize ) -> Option < FeatureRef < ' a > > {
200223 // SAFETY: Contained pointer guaranteed valid
201224 let ptr = unsafe { sys:: gr_face_fref ( self . as_ptr ( ) , idx as u16 ) } ;
202225 NonNull :: new ( ptr. cast_mut ( ) ) . map ( |p| FeatureRef ( p, PhantomData ) )
203226 }
204227
228+ /// Find a feature reference matching a feature ID
205229 pub fn find_feature_ref ( self , feat_id : u32 ) -> Option < FeatureRef < ' a > > {
206230 // SAFETY: Contained pointer guaranteed valid
207231 let ptr = unsafe { sys:: gr_face_find_fref ( self . as_ptr ( ) , feat_id) } ;
208232 NonNull :: new ( ptr. cast_mut ( ) ) . map ( |p| FeatureRef ( p, PhantomData ) )
209233 }
210234
235+ /// Get the feature value for a specific language
211236 pub fn feature_val_for_lang ( self , lang : u32 ) -> FeatureVal {
212237 // SAFETY: Contained pointer guaranteed valid
213238 let ptr = unsafe { sys:: gr_face_featureval_for_lang ( self . as_ptr ( ) , lang) } ;
214239 FeatureVal ( NonNull :: new ( ptr. cast ( ) ) . unwrap ( ) )
215240 }
216241}
217242
243+ /// A reference to a [`Font`].
218244#[ derive( Copy , Clone ) ]
219245pub struct FontRef < ' a > ( NonNull < sys:: gr_font > , PhantomData < & ' a sys:: gr_font > ) ;
220246
@@ -224,15 +250,18 @@ impl FontRef<'_> {
224250 }
225251}
226252
253+ /// A [`Face`] that has been associated with a specific size.
227254pub struct Font ( NonNull < sys:: gr_font > ) ;
228255
229256impl Font {
257+ /// Create a new front from a face and point size.
230258 pub fn new ( pt_size : f32 , face : FaceRef < ' _ > ) -> Option < Font > {
231259 // SAFETY: Face pointer guaranteed valid
232260 let ptr = unsafe { sys:: gr_make_font ( pt_size, face. as_ptr ( ) ) } ;
233261 NonNull :: new ( ptr. cast ( ) ) . map ( Font )
234262 }
235263
264+ /// Convert into a borrowed reference.
236265 pub fn as_ref ( & self ) -> FontRef < ' _ > {
237266 FontRef ( self . 0 , PhantomData )
238267 }
@@ -249,13 +278,17 @@ mod sealed {
249278 pub trait Sealed { }
250279
251280 impl Sealed for str { }
252- impl Sealed for ( * const u16 , usize ) { }
281+ impl Sealed for [ u16 ] { }
253282}
254283
284+ /// Trait for types that may be used to create a [`Segment`]
255285#[ allow( clippy:: len_without_is_empty) ]
256286pub trait StrEnc : sealed:: Sealed {
287+ /// [`EncForm`] that encodes this type
257288 fn enc ( & self ) -> EncForm ;
289+ /// Raw pointer to string data
258290 fn as_ptr ( & self ) -> * const ( ) ;
291+ /// Length of this segment
259292 fn len ( & self ) -> usize ;
260293}
261294
@@ -273,20 +306,21 @@ impl StrEnc for str {
273306 }
274307}
275308
276- impl StrEnc for ( * const u16 , usize ) {
309+ impl StrEnc for [ u16 ] {
277310 fn enc ( & self ) -> EncForm {
278311 EncForm :: utf16
279312 }
280313
281314 fn as_ptr ( & self ) -> * const ( ) {
282- self . 0 . cast ( )
315+ self . as_ptr ( ) . cast ( )
283316 }
284317
285318 fn len ( & self ) -> usize {
286- self . 1
319+ self . len ( )
287320 }
288321}
289322
323+ /// A reference to a [`Segment`].
290324#[ derive( Copy , Clone ) ]
291325pub struct SegmentRef < ' a > ( NonNull < sys:: gr_segment > , PhantomData < & ' a sys:: gr_segment > ) ;
292326
@@ -295,41 +329,48 @@ impl<'a> SegmentRef<'a> {
295329 self . 0 . as_ptr ( ) . cast_const ( )
296330 }
297331
332+ /// Get the first slot in the segment
298333 pub fn first_slot ( self ) -> Slot {
299334 // SAFETY: Contained pointer guaranteed valid
300335 let ptr = unsafe { sys:: gr_seg_first_slot ( self . as_ptr ( ) . cast_mut ( ) ) } ;
301336 Slot ( self . 0 , NonNull :: new ( ptr. cast_mut ( ) ) . unwrap ( ) )
302337 }
303338
339+ /// Get the last slot in the segment
304340 pub fn last_slot ( self ) -> Slot {
305341 // SAFETY: Contained pointer guaranteed valid
306342 let ptr = unsafe { sys:: gr_seg_last_slot ( self . as_ptr ( ) . cast_mut ( ) ) } ;
307343 Slot ( self . 0 , NonNull :: new ( ptr. cast_mut ( ) ) . unwrap ( ) )
308344 }
309345
346+ /// Get the character info at a given index
310347 pub fn cinfo ( self , idx : usize ) -> CharInfoRef < ' a > {
311348 // SAFETY: Contained pointer guaranteed valid
312349 let ptr = unsafe { sys:: gr_seg_cinfo ( self . as_ptr ( ) , idx as u32 ) } ;
313350 CharInfoRef ( NonNull :: new ( ptr. cast_mut ( ) ) . unwrap ( ) , PhantomData )
314351 }
315352
353+ /// Get the next slot after the provided slot
316354 pub fn next ( self , slot : & Slot ) -> Option < Slot > {
317355 assert_eq ! ( self . as_ptr( ) . cast_mut( ) , slot. 0 . as_ptr( ) . cast( ) ) ;
318356 // SAFETY: Slot pointer guaranteed valid
319357 let ptr = unsafe { sys:: gr_slot_next_in_segment ( slot. as_ptr ( ) ) } ;
320358 NonNull :: new ( ptr. cast_mut ( ) ) . map ( |ptr| Slot ( self . 0 , ptr) )
321359 }
322360
361+ /// Get the index of the provided slot
323362 pub fn index ( self , slot : & Slot ) -> usize {
324363 assert_eq ! ( self . as_ptr( ) . cast_mut( ) , slot. 0 . as_ptr( ) . cast( ) ) ;
325364 // SAFETY: Slot pointer guaranteed valid
326365 unsafe { sys:: gr_slot_index ( slot. as_ptr ( ) ) as usize }
327366 }
328367}
329368
369+ /// A segment of text, with all necessary information for shaping
330370pub struct Segment ( NonNull < sys:: gr_segment > ) ;
331371
332372impl Segment {
373+ /// Create a new segment from all necessary information and a string to shape.
333374 pub fn new < S : ?Sized + StrEnc > (
334375 font : FontRef < ' _ > ,
335376 face : FaceRef < ' _ > ,
@@ -356,6 +397,7 @@ impl Segment {
356397 NonNull :: new ( ptr. cast ( ) ) . map ( Segment )
357398 }
358399
400+ /// Convert into a borrowed reference.
359401 pub fn as_ref ( & self ) -> SegmentRef < ' _ > {
360402 SegmentRef ( self . 0 , PhantomData )
361403 }
@@ -368,6 +410,7 @@ impl Drop for Segment {
368410 }
369411}
370412
413+ /// A single slot in a [`Segment`].
371414#[ derive( Clone , PartialEq ) ]
372415pub struct Slot ( NonNull < sys:: gr_segment > , NonNull < sys:: gr_slot > ) ;
373416
@@ -377,6 +420,7 @@ impl Slot {
377420 }
378421}
379422
423+ /// Information about a specific character in a [`Segment`].
380424#[ derive( Copy , Clone ) ]
381425pub struct CharInfoRef < ' a > (
382426 NonNull < sys:: gr_char_info > ,
@@ -388,11 +432,13 @@ impl CharInfoRef<'_> {
388432 self . 0 . as_ptr ( ) . cast_const ( )
389433 }
390434
435+ /// Get the break weight for this character.
391436 pub fn break_weight ( self ) -> i32 {
392437 // SAFETY: Contained pointer guaranteed valid
393438 unsafe { sys:: gr_cinfo_break_weight ( self . as_ptr ( ) ) as i32 }
394439 }
395440
441+ /// Get the code unit index for this character
396442 pub fn base ( self ) -> usize {
397443 // SAFETY: Contained pointer guaranteed valid
398444 unsafe { sys:: gr_cinfo_base ( self . as_ptr ( ) ) }
0 commit comments