Skip to content

Commit

Permalink
feat: font reload should also parse woff/woff2 buffers
Browse files Browse the repository at this point in the history
  • Loading branch information
zimond committed May 20, 2024
1 parent 7b969be commit 88d514a
Showing 1 changed file with 18 additions and 17 deletions.
35 changes: 18 additions & 17 deletions src/font.rs
Original file line number Diff line number Diff line change
Expand Up @@ -157,22 +157,33 @@ impl Font {
}

#[cfg(feature = "parse")]
pub(super) fn from_buffer(mut buffer: &[u8]) -> Result<Vec<Self>, Error> {
pub(super) fn from_buffer(buffer: &[u8]) -> Result<Vec<Self>, Error> {
let mut variants = vec![Variant::Index(0)];
let mut result = if is_ttf(&buffer) {
let result = if is_ttf(&buffer) {
buffer.to_vec()
} else if is_otf(&buffer) {
variants = (0..ttf_parser::fonts_in_collection(&buffer).unwrap_or(1))
.map(|i| Variant::Index(i))
.collect();
buffer.to_vec()
} else {
vec![]
buffer.to_vec()
};
println!("{} {}", is_woff(&buffer), is_woff2(&buffer));
Ok(variants
.into_iter()
.map(|v| Font::from_buffer_with_variant(result.clone(), v))
.collect::<Result<Vec<_>, _>>()?
.into_iter()
.flatten()
.collect())
}

#[cfg(feature = "parse")]
fn from_buffer_with_variant(mut buffer: Vec<u8>, variant: Variant) -> Result<Vec<Self>, Error> {
#[cfg(feature = "woff2")]
if is_woff2(&buffer) {
result = woff2::convert_woff2_to_ttf(&mut buffer)?;
buffer = woff2::convert_woff2_to_ttf(&mut buffer.as_slice())?;
}
#[cfg(feature = "parse")]
if is_woff(&buffer) {
Expand All @@ -181,23 +192,12 @@ impl Font {
let reader = Cursor::new(buffer);
let mut otf_buf = Cursor::new(Vec::new());
crate::conv::woff::convert_woff_to_otf(reader, &mut otf_buf)?;
result = otf_buf.into_inner();
buffer = otf_buf.into_inner();
}
if result.is_empty() {
if buffer.is_empty() {
return Err(Error::UnsupportedMIME("unknown"));
}

Ok(variants
.into_iter()
.map(|v| Font::from_buffer_with_variant(result.clone(), v))
.collect::<Result<Vec<_>, _>>()?
.into_iter()
.flatten()
.collect())
}

#[cfg(feature = "parse")]
fn from_buffer_with_variant(buffer: Vec<u8>, variant: Variant) -> Result<Vec<Self>, Error> {
use ttf_parser::name_id;
let index = match variant {
Variant::Index(i) => i,
Expand Down Expand Up @@ -430,6 +430,7 @@ impl Font {
let mut buffer = Vec::new();
let mut file = std::fs::File::open(path)?;
file.read_to_end(&mut buffer).unwrap();
println!("{:?} {}", path, buffer.len());
let mut fonts = Font::from_buffer_with_variant(buffer, self.variant.clone())?;
fonts.truncate(1);
if let Some(font) = fonts.pop() {
Expand Down

0 comments on commit 88d514a

Please sign in to comment.