Skip to content

Commit

Permalink
feat: do not panic if wrap_text assert fails
Browse files Browse the repository at this point in the history
  • Loading branch information
zimond committed Sep 20, 2023
1 parent 436ee47 commit 08dc828
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 13 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "fontkit"
version = "0.4.4"
version = "0.4.5"
edition = "2021"
authors = ["Zimon Dai <daizhuoxian@gmail.com>"]
description = "A simple library for font loading and indexing"
Expand Down
7 changes: 7 additions & 0 deletions src/error.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use thiserror::Error;

use crate::PositionedChar;

#[derive(Error, Debug)]
pub enum Error {
#[error("Unrecognized buffer")]
Expand All @@ -20,6 +22,11 @@ pub enum Error {
#[cfg(feature = "woff2")]
#[error(transparent)]
Woff2(#[from] woff2::decode::DecodeError),
#[error("Metrics mismatch: values {value:?} metrics {metrics:?}")]
MetricsMismatch {
value: Vec<char>,
metrics: Vec<PositionedChar>,
},
}

#[cfg(node)]
Expand Down
29 changes: 17 additions & 12 deletions src/metrics/compose.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use textwrap::Options;
use textwrap::WordSplitter::NoHyphenation;
use unicode_normalization::UnicodeNormalization;

use crate::{FontKey, TextMetrics};
use crate::{Error, FontKey, TextMetrics};

#[derive(Debug, Clone)]
pub struct Line<T> {
Expand Down Expand Up @@ -156,7 +156,7 @@ where
}
}

pub fn wrap_text(&mut self, width: f32) {
pub fn wrap_text(&mut self, width: f32) -> Result<(), Error> {
let rtl = self.lines.iter().all(|line| {
line.spans.iter().all(|span| {
span.metrics
Expand Down Expand Up @@ -232,7 +232,7 @@ where
if approved_spans.is_empty() {
if failed_with_no_acception {
// Failed to fit a span twice, fail
return;
return Ok(());
} else {
failed_with_no_acception = true;
}
Expand Down Expand Up @@ -374,14 +374,18 @@ where
if !span.metrics.value.is_empty() {
current_line.spans.push(span.clone());
}
assert_eq!(
span.metrics.value.nfc().count(),
span.metrics.positions.len()
);
assert_eq!(
new_span.metrics.value.nfc().count(),
new_span.metrics.positions.len()
);
if span.metrics.value.nfc().count() != span.metrics.positions.len() {
return Err(Error::MetricsMismatch {
value: span.metrics.value.nfc().collect(),
metrics: span.metrics.positions.clone(),
});
}
if new_span.metrics.value.nfc().count() != new_span.metrics.positions.len() {
return Err(Error::MetricsMismatch {
value: new_span.metrics.value.nfc().collect(),
metrics: new_span.metrics.positions.clone(),
});
}
// Create a new line
result.push(std::mem::replace(
&mut current_line,
Expand Down Expand Up @@ -413,10 +417,11 @@ where
result.push(current_line);
}
if result.is_empty() || result[0].spans.is_empty() {
return;
return Ok(());
}
self.lines = result;
log::trace!("adjust result: {}", self.value_string());
Ok(())
}

pub fn valid(&self) -> bool {
Expand Down

0 comments on commit 08dc828

Please sign in to comment.