use owned_ttf_parser::{AsFaceRef, Face, GlyphId, OwnedFace, Tag};
use crate::error::Error;
#[derive(Clone, Copy, Debug, PartialEq)]
pub enum PathCommand {
MoveTo {
x: f64,
y: f64,
},
LineTo {
x: f64,
y: f64,
},
QuadTo {
x1: f64,
y1: f64,
x: f64,
y: f64,
},
CurveTo {
x1: f64,
y1: f64,
x2: f64,
y2: f64,
x: f64,
y: f64,
},
Close,
}
impl PathCommand {
pub fn type_letter(&self) -> &'static str {
match self {
PathCommand::MoveTo { .. } => "M",
PathCommand::LineTo { .. } => "L",
PathCommand::QuadTo { .. } => "Q",
PathCommand::CurveTo { .. } => "C",
PathCommand::Close => "Z",
}
}
}
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct BBox {
pub x1: f64,
pub y1: f64,
pub x2: f64,
pub y2: f64,
}
#[derive(Clone, Copy, Debug)]
struct GlyfPoint {
x: f64,
y: f64,
on_curve: bool,
last_of_contour: bool,
}
pub struct VectorFont {
face: OwnedFace,
pub src: String,
pub units_per_em: f64,
pub ascender: f64,
pub descender: f64,
pub line_gap: f64,
pub cap_height: f64,
pub x_height: f64,
kern_subtables: Option<Vec<usize>>,
}
impl VectorFont {
pub fn parse(data: Vec<u8>, src: impl Into<String>) -> Result<Self, Error> {
let src = src.into();
let face = OwnedFace::from_vec(data, 0).map_err(|source| Error::Parse {
src: src.clone(),
source,
})?;
let (upem, asc, desc, gap, cap, xh) = {
let f = face.as_face_ref();
let tables = f.tables();
let os2 = tables.os2;
let hhea_asc = tables.hhea.ascender as f64;
let hhea_desc = tables.hhea.descender as f64;
let hhea_gap = tables.hhea.line_gap as f64;
let or0 = |a: f64, b: f64, c: f64| {
if a != 0.0 {
a
} else if b != 0.0 {
b
} else {
c
}
};
let asc = or0(
os2.map(|t| t.typographic_ascender() as f64).unwrap_or(0.0),
hhea_asc,
hhea_asc,
);
let desc = or0(
os2.map(|t| t.typographic_descender() as f64).unwrap_or(0.0),
hhea_desc,
hhea_desc,
);
let gap = or0(
os2.map(|t| t.typographic_line_gap() as f64).unwrap_or(0.0),
hhea_gap,
0.0,
);
let cap = os2
.and_then(|t| t.capital_height())
.map(|v| v as f64)
.unwrap_or(0.0);
let xh = os2
.and_then(|t| t.x_height())
.map(|v| v as f64)
.unwrap_or(0.0);
(f.units_per_em() as f64, asc, desc, gap, cap, xh)
};
let kern_subtables = {
let f = face.as_face_ref();
f.raw_face()
.table(Tag::from_bytes(b"GPOS"))
.map(collect_kern_subtables)
};
Ok(VectorFont {
face,
src,
units_per_em: upem,
ascender: asc,
descender: desc,
line_gap: gap,
cap_height: cap,
x_height: xh,
kern_subtables,
})
}
fn face(&self) -> &Face<'_> {
self.face.as_face_ref()
}
pub fn has_gpos(&self) -> bool {
self.kern_subtables.is_some()
}
pub fn has_kern(&self) -> bool {
self.face()
.raw_face()
.table(Tag::from_bytes(b"kern"))
.is_some()
}
pub fn glyph_for_char(&self, ch: char) -> GlyphId {
self.face().glyph_index(ch).unwrap_or(GlyphId(0))
}
pub fn advance_width(&self, ch: char) -> f64 {
let gid = self.glyph_for_char(ch);
self.face().glyph_hor_advance(gid).unwrap_or(0) as f64
}
pub fn bounding_box(&self, ch: char) -> Option<BBox> {
let gid = self.glyph_for_char(ch);
let b = path_bounding_box(&self.glyph_path(gid));
if b.x2 > b.x1 && b.y2 > b.y1 {
Some(b)
} else {
None
}
}
pub fn glyph_path(&self, gid: GlyphId) -> Vec<PathCommand> {
let mut points = Vec::new();
self.collect_points(gid, &mut points, &mut Vec::new());
contour_path(&points)
}
pub fn glyph_path_y_down(&self, gid: GlyphId) -> Vec<PathCommand> {
self.glyph_path(gid)
.into_iter()
.filter_map(|c| match c {
PathCommand::MoveTo { x, y } => Some(PathCommand::MoveTo { x, y: -y }),
PathCommand::LineTo { x, y } => Some(PathCommand::LineTo { x, y: -y }),
PathCommand::QuadTo { x1, y1, x, y } => Some(PathCommand::QuadTo {
x1,
y1: -y1,
x,
y: -y,
}),
PathCommand::CurveTo {
x1,
y1,
x2,
y2,
x,
y,
} => Some(PathCommand::CurveTo {
x1,
y1: -y1,
x2,
y2: -y2,
x,
y: -y,
}),
PathCommand::Close => None,
})
.collect()
}
pub fn measure(&self, text: &str, font_size: f64) -> f64 {
crate::text_builder::vec_measure_run(self, text, 0.0, font_size / self.units_per_em)
}
pub fn kerning(&self, left: char, right: char) -> f64 {
let l = self.glyph_for_char(left);
let r = self.glyph_for_char(right);
self.kerning_by_gid(l, r)
}
pub(crate) fn kerning_by_gid(&self, left: GlyphId, right: GlyphId) -> f64 {
let Some(subtables) = self.kern_subtables.as_ref() else {
return 0.0;
};
let face = self.face();
let Some(gpos) = face.raw_face().table(Tag::from_bytes(b"GPOS")) else {
return 0.0;
};
for &off in subtables {
let Some(sub) = gpos.get(off..) else { continue };
match pair_pos_lookup(sub, left, right) {
PairPosResult::NotCovered => continue,
PairPosResult::NoPair => continue,
PairPosResult::Value(v) => return v as f64,
}
}
0.0
}
fn collect_points(&self, gid: GlyphId, out: &mut Vec<GlyfPoint>, resolving: &mut Vec<u16>) {
let face = self.face();
let Some(glyf) = face.raw_face().table(Tag::from_bytes(b"glyf")) else {
return;
};
let Some(loca) = face.raw_face().table(Tag::from_bytes(b"loca")) else {
return;
};
let Some(head) = face.raw_face().table(Tag::from_bytes(b"head")) else {
return;
};
if head.len() < 52 {
return;
}
let long = read_i16(head, 50) != 0;
let Some(range) = loca_range(loca, gid.0, long) else {
return;
};
let Some(data) = glyf.get(range) else { return };
if data.len() < 10 {
return;
}
let ncont = read_i16(data, 0);
if ncont > 0 {
parse_simple_glyph(data, ncont as usize, out);
} else if ncont < 0 {
resolving.push(gid.0);
for comp in parse_composite(data) {
if resolving.contains(&comp.glyph_index) {
continue;
}
let mut sub = Vec::new();
self.collect_points(GlyphId(comp.glyph_index), &mut sub, resolving);
for p in sub.iter_mut() {
let (x, y) = (p.x, p.y);
p.x = comp.x_scale * x + comp.scale10 * y + comp.dx;
p.y = comp.scale01 * x + comp.y_scale * y + comp.dy;
}
out.extend_from_slice(&sub);
}
resolving.pop();
}
}
}
fn loca_range(loca: &[u8], gid: u16, long: bool) -> Option<std::ops::Range<usize>> {
let (start, end) = if long {
let at = gid as usize * 4;
if at + 8 > loca.len() {
return None;
}
(
u32::from_be_bytes([loca[at], loca[at + 1], loca[at + 2], loca[at + 3]]) as usize,
u32::from_be_bytes([loca[at + 4], loca[at + 5], loca[at + 6], loca[at + 7]]) as usize,
)
} else {
let at = gid as usize * 2;
if at + 4 > loca.len() {
return None;
}
(
read_u16(loca, at) as usize * 2,
read_u16(loca, at + 2) as usize * 2,
)
};
if start >= end {
None
} else {
Some(start..end)
}
}
fn contour_path(points: &[GlyfPoint]) -> Vec<PathCommand> {
let mut out = Vec::new();
let mut contour: Vec<GlyfPoint> = Vec::new();
for p in points {
contour.push(*p);
if !p.last_of_contour {
continue;
}
emit_contour(&contour, &mut out);
contour.clear();
}
out
}
fn emit_contour(contour: &[GlyfPoint], out: &mut Vec<PathCommand>) {
if contour.is_empty() {
return;
}
let mut curr = contour[contour.len() - 1];
let mut next = contour[0];
if curr.on_curve {
out.push(PathCommand::MoveTo {
x: curr.x,
y: curr.y,
});
} else if next.on_curve {
out.push(PathCommand::MoveTo {
x: next.x,
y: next.y,
});
} else {
out.push(PathCommand::MoveTo {
x: (curr.x + next.x) * 0.5,
y: (curr.y + next.y) * 0.5,
});
}
for i in 0..contour.len() {
curr = next;
next = contour[(i + 1) % contour.len()];
if curr.on_curve {
out.push(PathCommand::LineTo {
x: curr.x,
y: curr.y,
});
} else {
let (nx, ny) = if next.on_curve {
(next.x, next.y)
} else {
((curr.x + next.x) * 0.5, (curr.y + next.y) * 0.5)
};
out.push(PathCommand::QuadTo {
x1: curr.x,
y1: curr.y,
x: nx,
y: ny,
});
}
}
out.push(PathCommand::Close);
}
pub fn path_bounding_box(commands: &[PathCommand]) -> BBox {
let mut b = Builder::new();
let (mut start_x, mut start_y) = (0.0, 0.0);
let (mut prev_x, mut prev_y) = (0.0, 0.0);
for cmd in commands {
match *cmd {
PathCommand::MoveTo { x, y } => {
b.add_point(Some(x), Some(y));
start_x = x;
start_y = y;
prev_x = x;
prev_y = y;
}
PathCommand::LineTo { x, y } => {
b.add_point(Some(x), Some(y));
prev_x = x;
prev_y = y;
}
PathCommand::QuadTo { x1, y1, x, y } => {
b.add_quad(prev_x, prev_y, x1, y1, x, y);
prev_x = x;
prev_y = y;
}
PathCommand::CurveTo {
x1,
y1,
x2,
y2,
x,
y,
} => {
b.add_bezier(prev_x, prev_y, x1, y1, x2, y2, x, y);
prev_x = x;
prev_y = y;
}
PathCommand::Close => {
prev_x = start_x;
prev_y = start_y;
}
}
}
if b.empty {
b.add_point(Some(0.0), Some(0.0));
}
BBox {
x1: b.x1,
y1: b.y1,
x2: b.x2,
y2: b.y2,
}
}
struct Builder {
empty: bool,
x1: f64,
y1: f64,
x2: f64,
y2: f64,
}
impl Builder {
fn new() -> Self {
Builder {
empty: true,
x1: 0.0,
y1: 0.0,
x2: 0.0,
y2: 0.0,
}
}
fn add_point(&mut self, x: Option<f64>, y: Option<f64>) {
if self.empty {
if let Some(x) = x {
self.x1 = x;
self.x2 = x;
}
if let Some(y) = y {
self.y1 = y;
self.y2 = y;
}
if x.is_some() && y.is_some() {
self.empty = false;
}
return;
}
if let Some(x) = x {
if x < self.x1 {
self.x1 = x;
}
if x > self.x2 {
self.x2 = x;
}
}
if let Some(y) = y {
if y < self.y1 {
self.y1 = y;
}
if y > self.y2 {
self.y2 = y;
}
}
}
fn add_quad(&mut self, x0: f64, y0: f64, x1: f64, y1: f64, x: f64, y: f64) {
let cp1x = x0 + 2.0 / 3.0 * (x1 - x0);
let cp1y = y0 + 2.0 / 3.0 * (y1 - y0);
let cp2x = cp1x + 1.0 / 3.0 * (x - x0);
let cp2y = cp1y + 1.0 / 3.0 * (y - y0);
self.add_bezier(x0, y0, cp1x, cp1y, cp2x, cp2y, x, y);
}
#[allow(clippy::too_many_arguments)]
fn add_bezier(&mut self, x0: f64, y0: f64, x1: f64, y1: f64, x2: f64, y2: f64, x: f64, y: f64) {
let p0 = [x0, y0];
let p1 = [x1, y1];
let p2 = [x2, y2];
let p3 = [x, y];
self.add_point(Some(x0), Some(y0));
self.add_point(Some(x), Some(y));
for i in 0..2 {
let b = 6.0 * p0[i] - 12.0 * p1[i] + 6.0 * p2[i];
let a = -3.0 * p0[i] + 9.0 * p1[i] - 9.0 * p2[i] + 3.0 * p3[i];
let c = 3.0 * p1[i] - 3.0 * p0[i];
let mut add = |v: f64, axis: usize| {
if axis == 0 {
self.add_point(Some(v), None)
} else {
self.add_point(None, Some(v))
}
};
if a == 0.0 {
if b == 0.0 {
continue;
}
let t = -c / b;
if 0.0 < t && t < 1.0 {
add(derive(p0[i], p1[i], p2[i], p3[i], t), i);
}
continue;
}
let b2ac = b * b - 4.0 * c * a;
if b2ac < 0.0 {
continue;
}
let t1 = (-b + b2ac.sqrt()) / (2.0 * a);
if 0.0 < t1 && t1 < 1.0 {
add(derive(p0[i], p1[i], p2[i], p3[i], t1), i);
}
let t2 = (-b - b2ac.sqrt()) / (2.0 * a);
if 0.0 < t2 && t2 < 1.0 {
add(derive(p0[i], p1[i], p2[i], p3[i], t2), i);
}
}
}
}
fn derive(v0: f64, v1: f64, v2: f64, v3: f64, t: f64) -> f64 {
(1.0 - t).powi(3) * v0
+ 3.0 * (1.0 - t).powi(2) * t * v1
+ 3.0 * (1.0 - t) * t.powi(2) * v2
+ t.powi(3) * v3
}
fn read_i16(d: &[u8], at: usize) -> i16 {
i16::from_be_bytes([d[at], d[at + 1]])
}
fn read_u16(d: &[u8], at: usize) -> u16 {
u16::from_be_bytes([d[at], d[at + 1]])
}
fn parse_simple_glyph(data: &[u8], ncont: usize, out: &mut Vec<GlyfPoint>) {
let mut p = 10;
let mut end_pts = Vec::with_capacity(ncont);
for _ in 0..ncont {
if p + 2 > data.len() {
return;
}
end_pts.push(read_u16(data, p) as usize);
p += 2;
}
if p + 2 > data.len() {
return;
}
let instr_len = read_u16(data, p) as usize;
p += 2 + instr_len;
let n = match end_pts.last() {
Some(&last) => last + 1,
None => return,
};
let mut flags: Vec<u8> = Vec::with_capacity(n);
while flags.len() < n {
if p >= data.len() {
return;
}
let flag = data[p];
p += 1;
flags.push(flag);
if flag & 8 != 0 {
if p >= data.len() {
return;
}
let repeat = data[p];
p += 1;
for _ in 0..repeat {
flags.push(flag);
}
}
}
flags.truncate(n);
let base = out.len();
for (i, flag) in flags.iter().enumerate().take(n) {
out.push(GlyfPoint {
x: 0.0,
y: 0.0,
on_curve: flag & 1 != 0,
last_of_contour: end_pts.contains(&i),
});
}
let coord = |p: &mut usize, flag: u8, prev: f64, short: u8, same: u8| -> f64 {
if flag & short != 0 {
if *p >= data.len() {
return prev;
}
let v = data[*p] as f64;
*p += 1;
prev + if flag & same == 0 { -v } else { v }
} else if flag & same != 0 {
prev
} else {
if *p + 2 > data.len() {
return prev;
}
let v = read_i16(data, *p) as f64;
*p += 2;
prev + v
}
};
let mut px = 0.0;
for i in 0..n {
px = coord(&mut p, flags[i], px, 0x02, 0x10);
out[base + i].x = px;
}
let mut py = 0.0;
for i in 0..n {
py = coord(&mut p, flags[i], py, 0x04, 0x20);
out[base + i].y = py;
}
}
struct Component {
glyph_index: u16,
x_scale: f64,
scale01: f64,
scale10: f64,
y_scale: f64,
dx: f64,
dy: f64,
}
fn parse_composite(data: &[u8]) -> Vec<Component> {
let mut out = Vec::new();
let mut p = 10;
loop {
if p + 4 > data.len() {
break;
}
let flags = read_u16(data, p);
let glyph_index = read_u16(data, p + 2);
p += 4;
let mut c = Component {
glyph_index,
x_scale: 1.0,
scale01: 0.0,
scale10: 0.0,
y_scale: 1.0,
dx: 0.0,
dy: 0.0,
};
if flags & 1 != 0 {
if flags & 2 != 0 {
c.dx = read_i16(data, p) as f64;
c.dy = read_i16(data, p + 2) as f64;
}
p += 4;
} else {
if flags & 2 != 0 {
c.dx = data[p] as i8 as f64;
c.dy = data[p + 1] as i8 as f64;
}
p += 2;
}
let f2dot14 = |at: usize| read_i16(data, at) as f64 / 16384.0;
if flags & 8 != 0 {
c.x_scale = f2dot14(p);
c.y_scale = c.x_scale;
p += 2;
} else if flags & 64 != 0 {
c.x_scale = f2dot14(p);
c.y_scale = f2dot14(p + 2);
p += 4;
} else if flags & 128 != 0 {
c.x_scale = f2dot14(p);
c.scale01 = f2dot14(p + 2);
c.scale10 = f2dot14(p + 4);
c.y_scale = f2dot14(p + 6);
p += 8;
}
out.push(c);
if flags & 32 == 0 {
break;
}
}
out
}
fn collect_kern_subtables(gpos: &[u8]) -> Vec<usize> {
let mut out = Vec::new();
if gpos.len() < 10 {
return out;
}
let script_list_off = read_u16(gpos, 4) as usize;
let feature_list_off = read_u16(gpos, 6) as usize;
let lookup_list_off = read_u16(gpos, 8) as usize;
let Some(sl) = gpos.get(script_list_off..) else {
return out;
};
if sl.len() < 2 {
return out;
}
let script_count = read_u16(sl, 0) as usize;
let mut tags = Vec::with_capacity(script_count);
for i in 0..script_count {
let at = 2 + i * 6;
if at + 6 > sl.len() {
return out;
}
tags.push((
[sl[at], sl[at + 1], sl[at + 2], sl[at + 3]],
read_u16(sl, at + 4) as usize,
));
}
let pick = tags
.iter()
.find(|(t, _)| t == b"DFLT")
.or_else(|| tags.iter().find(|(t, _)| t == b"latn"));
let Some(&(_, script_off)) = pick else {
return out;
};
let Some(st) = sl.get(script_off..) else {
return out;
};
if st.len() < 2 {
return out;
}
let default_lang_sys_off = read_u16(st, 0) as usize;
if default_lang_sys_off == 0 {
return out;
}
let Some(ls) = st.get(default_lang_sys_off..) else {
return out;
};
if ls.len() < 6 {
return out;
}
let feature_index_count = read_u16(ls, 4) as usize;
let mut feature_indices = Vec::with_capacity(feature_index_count);
for i in 0..feature_index_count {
let at = 6 + i * 2;
if at + 2 > ls.len() {
return out;
}
feature_indices.push(read_u16(ls, at) as usize);
}
let Some(fl) = gpos.get(feature_list_off..) else {
return out;
};
if fl.len() < 2 {
return out;
}
let feature_count = read_u16(fl, 0) as usize;
let mut feature_off = None;
for idx in feature_indices {
if idx >= feature_count {
continue;
}
let at = 2 + idx * 6;
if at + 6 > fl.len() {
continue;
}
if &fl[at..at + 4] == b"kern" {
feature_off = Some(read_u16(fl, at + 4) as usize);
break;
}
}
let Some(feature_off) = feature_off else {
return out;
};
let Some(ft) = fl.get(feature_off..) else {
return out;
};
if ft.len() < 4 {
return out;
}
let lookup_count = read_u16(ft, 2) as usize;
let mut lookup_indices = Vec::with_capacity(lookup_count);
for i in 0..lookup_count {
let at = 4 + i * 2;
if at + 2 > ft.len() {
return out;
}
lookup_indices.push(read_u16(ft, at) as usize);
}
let Some(ll) = gpos.get(lookup_list_off..) else {
return out;
};
if ll.len() < 2 {
return out;
}
let total = read_u16(ll, 0) as usize;
for idx in lookup_indices {
if idx >= total {
continue;
}
let at = 2 + idx * 2;
if at + 2 > ll.len() {
continue;
}
let lookup_off = read_u16(ll, at) as usize;
let abs = lookup_list_off + lookup_off;
let Some(lt) = gpos.get(abs..) else { continue };
if lt.len() < 6 {
continue;
}
if read_u16(lt, 0) != 2 {
continue;
}
let sub_count = read_u16(lt, 4) as usize;
for j in 0..sub_count {
let sat = 6 + j * 2;
if sat + 2 > lt.len() {
break;
}
out.push(abs + read_u16(lt, sat) as usize);
}
}
out
}
enum PairPosResult {
NotCovered,
NoPair,
Value(i16),
}
fn pair_pos_lookup(sub: &[u8], left: GlyphId, right: GlyphId) -> PairPosResult {
if sub.len() < 4 {
return PairPosResult::NotCovered;
}
let format = read_u16(sub, 0);
let coverage_off = read_u16(sub, 2) as usize;
let Some(cov) = sub.get(coverage_off..) else {
return PairPosResult::NotCovered;
};
let Some(cov_index) = coverage_index(cov, left) else {
return PairPosResult::NotCovered;
};
match format {
1 => {
if sub.len() < 10 {
return PairPosResult::NotCovered;
}
let v1_format = read_u16(sub, 4);
let v2_format = read_u16(sub, 6);
let set_count = read_u16(sub, 8) as usize;
if cov_index as usize >= set_count {
return PairPosResult::NoPair;
}
let at = 10 + cov_index as usize * 2;
if at + 2 > sub.len() {
return PairPosResult::NoPair;
}
let set_off = read_u16(sub, at) as usize;
let Some(set) = sub.get(set_off..) else {
return PairPosResult::NoPair;
};
if set.len() < 2 {
return PairPosResult::NoPair;
}
let pair_count = read_u16(set, 0) as usize;
let rec_size = 2 + value_size(v1_format) + value_size(v2_format);
for k in 0..pair_count {
let at = 2 + k * rec_size;
if at + rec_size > set.len() {
break;
}
if read_u16(set, at) == right.0 {
return PairPosResult::Value(x_advance(set, at + 2, v1_format));
}
}
PairPosResult::NoPair
}
2 => {
if sub.len() < 16 {
return PairPosResult::NotCovered;
}
let v1_format = read_u16(sub, 4);
let v2_format = read_u16(sub, 6);
let cd1_off = read_u16(sub, 8) as usize;
let cd2_off = read_u16(sub, 10) as usize;
let class1_count = read_u16(sub, 12) as usize;
let class2_count = read_u16(sub, 14) as usize;
let c1 = sub
.get(cd1_off..)
.map(|d| glyph_class(d, left))
.unwrap_or(0) as usize;
let c2 = sub
.get(cd2_off..)
.map(|d| glyph_class(d, right))
.unwrap_or(0) as usize;
if c1 >= class1_count || c2 >= class2_count {
return PairPosResult::Value(0);
}
let rec_size = value_size(v1_format) + value_size(v2_format);
let at = 16 + (c1 * class2_count + c2) * rec_size;
if at + rec_size > sub.len() {
return PairPosResult::Value(0);
}
PairPosResult::Value(x_advance(sub, at, v1_format))
}
_ => PairPosResult::NoPair,
}
}
fn value_size(format: u16) -> usize {
2 * (format & 0xFF).count_ones() as usize
}
fn x_advance(d: &[u8], at: usize, format: u16) -> i16 {
if format & 0x0004 == 0 {
return 0;
}
let before = (format & 0x0003).count_ones() as usize;
let off = at + before * 2;
if off + 2 > d.len() {
return 0;
}
read_i16(d, off)
}
fn coverage_index(cov: &[u8], glyph: GlyphId) -> Option<u16> {
if cov.len() < 4 {
return None;
}
match read_u16(cov, 0) {
1 => {
let count = read_u16(cov, 2) as usize;
let (mut lo, mut hi) = (0usize, count);
while lo < hi {
let mid = (lo + hi) / 2;
let at = 4 + mid * 2;
if at + 2 > cov.len() {
return None;
}
let g = read_u16(cov, at);
if g == glyph.0 {
return Some(mid as u16);
} else if g < glyph.0 {
lo = mid + 1;
} else {
hi = mid;
}
}
None
}
2 => {
let count = read_u16(cov, 2) as usize;
for i in 0..count {
let at = 4 + i * 6;
if at + 6 > cov.len() {
return None;
}
let start = read_u16(cov, at);
let end = read_u16(cov, at + 2);
if glyph.0 >= start && glyph.0 <= end {
return Some(read_u16(cov, at + 4) + glyph.0 - start);
}
}
None
}
_ => None,
}
}
fn glyph_class(d: &[u8], glyph: GlyphId) -> u16 {
if d.len() < 4 {
return 0;
}
match read_u16(d, 0) {
1 => {
let start = read_u16(d, 2);
let count = read_u16(d, 4) as usize;
if glyph.0 >= start && (glyph.0 as usize) < start as usize + count {
let at = 6 + (glyph.0 - start) as usize * 2;
if at + 2 <= d.len() {
return read_u16(d, at);
}
}
0
}
2 => {
let count = read_u16(d, 2) as usize;
for i in 0..count {
let at = 4 + i * 6;
if at + 6 > d.len() {
return 0;
}
if glyph.0 >= read_u16(d, at) && glyph.0 <= read_u16(d, at + 2) {
return read_u16(d, at + 4);
}
}
0
}
_ => 0,
}
}