Skip to content

Commit 4d20482

Browse files
committed
More documentation
1 parent f27a800 commit 4d20482

File tree

3 files changed

+46
-2
lines changed

3 files changed

+46
-2
lines changed

src/glyph.rs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,17 @@ use ttf2mesh_sys as sys;
44

55
use crate::{mesh::Mesh, Error, Quality};
66

7-
/// Represents a glyph in truetype font file. Can be converted to a 2d or 3d mesh
7+
/// Represents a glyph in truetype font file. Can be converted to a 2d or 3d [`Mesh`]
8+
///
9+
/// Usage:
10+
/// ```rust
11+
/// # use ttf2mesh::{TTFFile, Quality};
12+
/// # let mut ttf = TTFFile::from_file("./fonts/FiraMono-Medium.ttf").unwrap();
13+
/// # let mut glyph = ttf.glyph_from_char('€').unwrap();
14+
/// let mesh_2d = glyph.to_2d_mesh(Quality::Medium).unwrap();
15+
/// // or
16+
/// let mesh_3d = glyph.to_3d_mesh(Quality::Medium, 2.).unwrap();
17+
/// ```
818
pub struct Glyph<'a> {
919
inner: &'a mut sys::ttf_glyph,
1020
}

src/mesh.rs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,34 @@ use crate::{
88
Error,
99
};
1010

11+
/// A (2d or 3d) mesh that has been generated from a [`Glyph`]
12+
///
13+
/// Usage:
14+
/// ```rust
15+
/// # use ttf2mesh::{TTFFile, Quality};
16+
/// # let mut ttf = TTFFile::from_file("./fonts/FiraMono-Medium.ttf").unwrap();
17+
/// # let mut glyph = ttf.glyph_from_char('€').unwrap();
18+
/// # let mesh_3d = glyph.to_3d_mesh(Quality::Medium, 2.).unwrap();
19+
///
20+
/// // vertices with for-loop
21+
/// for vertex in mesh_3d.iter_vertices() {
22+
/// let values: (f32, f32, f32) = vertex.value();
23+
/// // do something
24+
/// }
25+
///
26+
/// // or copying to a new vector
27+
/// let vertices = mesh_3d.iter_vertices()
28+
/// .map(|v| v.value())
29+
/// .collect::<Vec<(f32, f32, f32)>>();
30+
///
31+
/// let faces = mesh_3d.iter_faces()
32+
/// .map(|v| v.value())
33+
/// .collect::<Vec<(i32, i32, i32)>>();
34+
///
35+
/// let normals = mesh_3d.iter_normals().unwrap()
36+
/// .map(|v| v.value())
37+
/// .collect::<Vec<(f32, f32, f32)>>();
38+
/// ```
1139
pub struct Mesh<'a, T: MeshPointer<'a>> {
1240
inner: *mut T,
1341
_phantom: &'a PhantomData<T>,
@@ -106,20 +134,23 @@ impl<'a, T: MeshPointer<'a>> Mesh<'a, T> {
106134
})
107135
}
108136

137+
/// Get an iterator of mesh vertices
109138
pub fn iter_vertices(&'a self) -> MeshIterator<'a, <T as MeshPointer>::VertStruct> {
110139
let vertices =
111140
unsafe { slice::from_raw_parts((&*self.inner).get_vert_ptr(), self.vertices_len()) };
112141

113142
MeshIterator::new(vertices)
114143
}
115144

145+
/// Get an iterator of mesh faces (indices)
116146
pub fn iter_faces<'b>(&'a self) -> MeshIterator<'a, <T as MeshPointer>::FaceStruct> {
117147
let faces =
118148
unsafe { slice::from_raw_parts((&*self.inner).get_face_ptr(), self.faces_len()) };
119149

120150
MeshIterator::new(faces)
121151
}
122152

153+
/// Get an iterator of mesh normals. Only for 3d mesh, always None for 2d mesh
123154
pub fn iter_normals<'b>(
124155
&'a self,
125156
) -> Option<MeshIterator<'a, <T as MeshPointer>::NormalStruct>> {
@@ -133,14 +164,17 @@ impl<'a, T: MeshPointer<'a>> Mesh<'a, T> {
133164
Some(MeshIterator::new(normals))
134165
}
135166

167+
/// Get the count of vertices
136168
pub fn vertices_len(&self) -> usize {
137169
unsafe { &*self.inner }.get_vert_len()
138170
}
139171

172+
/// Get the count of faces (indices)
140173
pub fn faces_len(&self) -> usize {
141174
unsafe { &*self.inner }.get_face_len()
142175
}
143176

177+
/// Get the count of normals (always zero for 2d meshes)
144178
pub fn normals_len(&self) -> usize {
145179
unsafe { &*self.inner }.get_normals_len()
146180
}

src/ttf.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use crate::{path_to_cstring, Error, Glyph, Quality};
88

99
/// A decoded TTF file instance
1010
///
11-
/// Usage - opening a file:
11+
/// Usage:
1212
/// ```rust
1313
/// # use ttf2mesh::{TTFFile, Quality};
1414
///

0 commit comments

Comments
 (0)