Skip to content

Commit 8cb2483

Browse files
committed
Allow font bytes to have multiple owners
Signed-off-by: Victor Porof <[email protected]>
1 parent f470811 commit 8cb2483

File tree

4 files changed

+27
-15
lines changed

4 files changed

+27
-15
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -24,5 +24,6 @@
2424
rusti.sh
2525
watch.sh
2626
/examples/**/target
27+
.vscode
2728

2829
Cargo.lock

Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
[package]
22

33
name = "freetype-rs"
4-
version = "0.16.0"
4+
version = "0.17.0"
55
authors = ["Coeuvre <[email protected]>"]
66
keywords = ["freetype", "font", "glyph"]
77
description = "Bindings for FreeType font library"

src/face.rs

+16-10
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use std::fmt;
22
use std::ffi::CStr;
3+
use std::rc::Rc;
34
use { ffi, FtResult, GlyphSlot, Matrix, Vector };
4-
use std::marker::PhantomData;
55

66
#[repr(u32)]
77
#[derive(Copy, Clone)]
@@ -38,14 +38,14 @@ bitflags! {
3838
}
3939

4040
#[derive(Eq, PartialEq, Hash)]
41-
pub struct Face<'a> {
41+
pub struct Face {
4242
library_raw: ffi::FT_Library,
4343
raw: ffi::FT_Face,
4444
glyph: GlyphSlot,
45-
_phantom: PhantomData<&'a ()>
45+
bytes: Option<Rc<Vec<u8>>>
4646
}
4747

48-
impl<'a> Clone for Face<'a> {
48+
impl Clone for Face {
4949
fn clone(&self) -> Self {
5050
let err = unsafe {
5151
ffi::FT_Reference_Library(self.library_raw)
@@ -59,18 +59,23 @@ impl<'a> Clone for Face<'a> {
5959
if err != ffi::FT_Err_Ok {
6060
panic!("Failed to reference face");
6161
}
62-
Face { ..*self }
62+
Face {
63+
library_raw: self.library_raw,
64+
raw: self.raw,
65+
glyph: self.glyph,
66+
bytes: self.bytes.clone()
67+
}
6368
}
6469
}
6570

66-
impl<'a> Face<'a> {
67-
pub unsafe fn from_raw(library_raw: ffi::FT_Library, raw: ffi::FT_Face) -> Self {
71+
impl Face {
72+
pub unsafe fn from_raw(library_raw: ffi::FT_Library, raw: ffi::FT_Face, bytes: Option<Rc<Vec<u8>>>) -> Self {
6873
ffi::FT_Reference_Library(library_raw);
6974
Face {
7075
library_raw: library_raw,
7176
raw: raw,
7277
glyph: GlyphSlot::from_raw(library_raw, (*raw).glyph),
73-
_phantom: PhantomData
78+
bytes: bytes,
7479
}
7580
}
7681

@@ -354,15 +359,15 @@ impl<'a> Face<'a> {
354359
}
355360
}
356361

357-
impl<'a> fmt::Debug for Face<'a> {
362+
impl fmt::Debug for Face {
358363
fn fmt(&self, form: &mut fmt::Formatter) -> fmt::Result {
359364
let name = self.style_name().unwrap_or("[unknown name]".to_owned());
360365
try!(form.write_str("Font Face: "));
361366
form.write_str(&name[..])
362367
}
363368
}
364369

365-
impl<'a> Drop for Face<'a> {
370+
impl Drop for Face {
366371
fn drop(&mut self) {
367372
let err = unsafe {
368373
ffi::FT_Done_Face(self.raw)
@@ -376,5 +381,6 @@ impl<'a> Drop for Face<'a> {
376381
if err != ffi::FT_Err_Ok {
377382
panic!("Failed to drop library")
378383
}
384+
self.bytes = None;
379385
}
380386
}

src/library.rs

+9-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use std::ffi::{ CString, OsStr };
22
use std::ptr::null_mut;
3+
use std::rc::Rc;
34
use libc::{ self, c_void, c_long, size_t };
45
use { Face, FtResult, Error };
56
use ffi;
@@ -69,7 +70,7 @@ impl Library {
6970

7071
/// Open a font file using its pathname. `face_index` should be 0 if there is only 1 font
7172
/// in the file.
72-
pub fn new_face<P>(&self, path: P, face_index: isize) -> FtResult<Face<'static>>
73+
pub fn new_face<P>(&self, path: P, face_index: isize) -> FtResult<Face>
7374
where P: AsRef<OsStr>
7475
{
7576
let mut face = null_mut();
@@ -82,22 +83,26 @@ impl Library {
8283
ffi::FT_New_Face(self.raw, path.as_ptr() as *const _, face_index as ffi::FT_Long, &mut face)
8384
};
8485
if err == ffi::FT_Err_Ok {
85-
Ok(unsafe { Face::from_raw(self.raw, face) })
86+
Ok(unsafe { Face::from_raw(self.raw, face, None) })
8687
} else {
8788
Err(err.into())
8889
}
8990
}
9091

9192
/// Similar to `new_face`, but loads file data from a byte array in memory
92-
pub fn new_memory_face<'a>(&self, buffer: &'a [u8], face_index: isize) -> FtResult<Face<'a>> {
93+
pub fn new_memory_face<T>(&self, buffer: T, face_index: isize) -> FtResult<Face>
94+
where
95+
T: Into<Rc<Vec<u8>>>
96+
{
9397
let mut face = null_mut();
98+
let buffer = buffer.into();
9499

95100
let err = unsafe {
96101
ffi::FT_New_Memory_Face(self.raw, buffer.as_ptr(), buffer.len() as ffi::FT_Long,
97102
face_index as ffi::FT_Long, &mut face)
98103
};
99104
if err == ffi::FT_Err_Ok {
100-
Ok(unsafe { Face::from_raw(self.raw, face) })
105+
Ok(unsafe { Face::from_raw(self.raw, face, Some(buffer)) })
101106
} else {
102107
Err(err.into())
103108
}

0 commit comments

Comments
 (0)