Skip to content

Commit f27a800

Browse files
committed
Add documentation
1 parent 6ebb344 commit f27a800

File tree

7 files changed

+62
-34
lines changed

7 files changed

+62
-34
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "ttf2mesh"
3-
version = "0.0.2"
3+
version = "0.0.3"
44
edition = "2018"
55
description = "Rust API for ttf2mesh"
66
repository = "https://github.com/blaind/ttf2mesh-rs"

examples/ttf2check.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ fn main() {
2626
println!(
2727
"- {} = {}",
2828
char,
29-
match font.glyph_by_char(char) {
29+
match font.glyph_from_char(char) {
3030
Ok(mut glyph) => {
3131
let len2dvert = match glyph.to_2d_mesh(ttf2mesh::Quality::Medium) {
3232
Ok(mesh) => mesh.vertices_len(),

examples/ttf2mesh.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ fn main() {
4040

4141
for char in utf8_string.chars() {
4242
println!("Mesh data char {:?}", char);
43-
let mut glyph = match font.glyph_by_char(char) {
43+
let mut glyph = match font.glyph_from_char(char) {
4444
Ok(g) => g,
4545
Err(_) => {
4646
println!("- can not find glyph in the font file");

src/error.rs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,21 @@
1+
/// Represents an error by the library
12
#[derive(Debug)]
23
pub enum Error {
4+
/// Font could not be loaded. The library doesn't support all font types
35
FontLoadError,
6+
7+
/// Font could not be exported to an obj file
48
ObjExportError,
9+
10+
/// Mesh generation failed
511
Glyph2MeshError,
12+
13+
/// Glyph is not found in the font file
614
GlyphNotFound,
7-
NoCharacterFound,
15+
16+
/// Quality could not be parsed from input
817
QualityParse,
18+
19+
/// File to be opened was not found
920
FileNotFound,
1021
}

src/glyph.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ 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
78
pub struct Glyph<'a> {
89
inner: &'a mut sys::ttf_glyph,
910
}

src/lib.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -55,16 +55,16 @@ mod tests {
5555
}
5656

5757
#[test]
58-
fn test_get_glyph_by_char() {
58+
fn test_get_glyph_from_char() {
5959
let mut font = TTFFile::from_buffer_vec(read_font(None)).unwrap();
6060
let char = "A".chars().next().unwrap();
61-
let _ = font.glyph_by_char(char).unwrap();
61+
let _ = font.glyph_from_char(char).unwrap();
6262
}
6363

6464
#[test]
6565
fn test_to_3d_mesh() {
6666
let mut font = TTFFile::from_buffer_vec(read_font(None)).unwrap();
67-
let mut glyph = font.glyph_by_char("€".chars().next().unwrap()).unwrap();
67+
let mut glyph = font.glyph_from_char("€".chars().next().unwrap()).unwrap();
6868
let mesh = glyph.to_3d_mesh(Quality::Low, 0.5).unwrap();
6969

7070
let mut sizes = Vec::new();
@@ -96,7 +96,7 @@ mod tests {
9696
#[test]
9797
fn test_to_2d_mesh() {
9898
let mut font = TTFFile::from_buffer_vec(read_font(None)).unwrap();
99-
let mut glyph = font.glyph_by_char("€".chars().next().unwrap()).unwrap();
99+
let mut glyph = font.glyph_from_char("€".chars().next().unwrap()).unwrap();
100100

101101
let mut sizes = Vec::new();
102102
let mesh = glyph.to_2d_mesh(Quality::Low).unwrap();
@@ -136,7 +136,7 @@ mod tests {
136136
let char = "€".chars().next().unwrap();
137137

138138
b.iter(|| {
139-
let _ = font.glyph_by_char(char).unwrap();
139+
let _ = font.glyph_from_char(char).unwrap();
140140
});
141141
}
142142

@@ -145,7 +145,7 @@ mod tests {
145145
let mut font = TTFFile::from_buffer_vec(read_font(None)).unwrap();
146146

147147
let char = "€".chars().next().unwrap();
148-
let mut glyph = font.glyph_by_char(char).unwrap();
148+
let mut glyph = font.glyph_from_char(char).unwrap();
149149

150150
b.iter(|| {
151151
let _ = glyph.to_3d_mesh(Quality::Low, 0.1).unwrap();
@@ -157,7 +157,7 @@ mod tests {
157157
let mut font = TTFFile::from_buffer_vec(read_font(None)).unwrap();
158158

159159
let char = "€".chars().next().unwrap();
160-
let mut glyph = font.glyph_by_char(char).unwrap();
160+
let mut glyph = font.glyph_from_char(char).unwrap();
161161

162162
b.iter(|| {
163163
let _ = glyph.to_3d_mesh(Quality::High, 0.1).unwrap();
@@ -169,7 +169,7 @@ mod tests {
169169
let mut font = TTFFile::from_buffer_vec(read_font(None)).unwrap();
170170

171171
let char = "€".chars().next().unwrap();
172-
let mut glyph = font.glyph_by_char(char).unwrap();
172+
let mut glyph = font.glyph_from_char(char).unwrap();
173173

174174
b.iter(|| {
175175
let _ = glyph.to_2d_mesh(Quality::Low).unwrap();
@@ -181,7 +181,7 @@ mod tests {
181181
let mut font = TTFFile::from_buffer_vec(read_font(None)).unwrap();
182182

183183
let char = "€".chars().next().unwrap();
184-
let mut glyph = font.glyph_by_char(char).unwrap();
184+
let mut glyph = font.glyph_from_char(char).unwrap();
185185

186186
b.iter(|| {
187187
let _ = glyph.to_2d_mesh(Quality::High).unwrap();

src/ttf.rs

Lines changed: 37 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -6,31 +6,42 @@ use ttf2mesh_sys as sys;
66

77
use crate::{path_to_cstring, Error, Glyph, Quality};
88

9+
/// A decoded TTF file instance
10+
///
11+
/// Usage - opening a file:
12+
/// ```rust
13+
/// # use ttf2mesh::{TTFFile, Quality};
14+
///
15+
/// // from a file
16+
/// let ttf = TTFFile::from_file("./fonts/FiraMono-Medium.ttf").unwrap();
17+
///
18+
/// // from a buffer
19+
/// let my_vec = std::fs::read("./fonts/FiraMono-Medium.ttf").unwrap();
20+
/// let mut ttf = TTFFile::from_buffer_vec(my_vec).unwrap();
21+
///
22+
/// // how many fonts?
23+
/// assert_eq!(ttf.glyph_count(), 1485);
24+
///
25+
/// // export all glyphs as 2d meshes to a .obj file
26+
/// ttf.export_to_obj("/dev/null", Quality::Low).unwrap();
27+
///
28+
/// // generate 2d mesh for a glyph
29+
/// let mut glyph = ttf.glyph_from_char('€').unwrap();
30+
/// let mesh = glyph.to_2d_mesh(Quality::Medium).unwrap();
31+
/// assert_eq!(mesh.vertices_len(), 56);
32+
/// assert_eq!(mesh.iter_vertices().next().unwrap().value(), (0.555, 0.656));
33+
///
34+
/// assert_eq!(mesh.faces_len(), 54);
35+
/// assert_eq!(mesh.iter_faces().next().unwrap().value(), (53, 52, 5));
36+
/// ```
937
pub struct TTFFile {
1038
ttf: *mut sys::ttf_file,
1139
}
1240

1341
impl TTFFile {
14-
//pub fn from_system_font() -> Result<TTFFile, Error> {
15-
/*
16-
// list all system fonts by filename mask:
17-
18-
ttf_t **list = ttf_list_system_fonts("DejaVuSans*|Ubuntu*|FreeSerif*|Arial*|Cour*");
19-
if (list == NULL) return false; // no memory in system
20-
if (list[0] == NULL) return false; // no fonts were found
21-
22-
// load the first font from the list
23-
24-
ttf_load_from_file(list[0]->filename, &font, false);
25-
ttf_free_list(list);
26-
if (font == NULL) return false;
27-
28-
printf("font \"%s\" loaded\n", font->names.full_name);
29-
return true;
30-
*/
31-
//}
32-
33-
// data needs to be mutable (modified by ttf_*), hence vec
42+
/// Load TTF font from a memory buffer
43+
///
44+
/// Has to take ownership since the buffer is being modified at runtime
3445
pub fn from_buffer_vec(data: Vec<u8>) -> Result<TTFFile, Error> {
3546
let mut ttf = MaybeUninit::uninit();
3647
let error = unsafe {
@@ -44,6 +55,7 @@ impl TTFFile {
4455
Self::load(ttf, error)
4556
}
4657

58+
/// Load TTF font from a file
4759
pub fn from_file<P: AsRef<Path>>(path: P) -> Result<TTFFile, Error> {
4860
if !Path::new(path.as_ref().as_os_str()).exists() {
4961
return Err(Error::FileNotFound);
@@ -68,6 +80,7 @@ impl TTFFile {
6880
})
6981
}
7082

83+
/// Export all glyphs to a .obj -file
7184
pub fn export_to_obj<P: AsRef<Path>>(
7285
&mut self,
7386
obj_path: P,
@@ -86,7 +99,8 @@ impl TTFFile {
8699
Ok(())
87100
}
88101

89-
pub fn glyph_by_char<'a>(&'a mut self, char: char) -> Result<Glyph<'a>, Error> {
102+
/// Get a glyph for a character
103+
pub fn glyph_from_char<'a>(&'a mut self, char: char) -> Result<Glyph<'a>, Error> {
90104
let mut bytes = [0; 2];
91105
char.encode_utf16(&mut bytes);
92106

@@ -99,10 +113,12 @@ impl TTFFile {
99113
self.glyph_by_index(index.try_into().unwrap())
100114
}
101115

116+
/// Total count of glyphs in a ttf file
102117
pub fn glyph_count(&self) -> usize {
103118
unsafe { *self.ttf }.nglyphs.try_into().unwrap()
104119
}
105120

121+
/// Get a glyph by its index. See also [`glyph_from_char`]
106122
pub fn glyph_by_index<'a>(&'a mut self, index: usize) -> Result<Glyph<'a>, Error> {
107123
let glyphs = unsafe { slice::from_raw_parts_mut((*self.ttf).glyphs, self.glyph_count()) };
108124

0 commit comments

Comments
 (0)