use std::sync::Arc;
use crate::font::cid::{CidVerticalMetrics, CidWidths};
use crate::font::cmap::CMap;
use crate::font::widths::SimpleWidths;
#[derive(Debug, Clone, PartialEq)]
pub enum FontMetrics {
Simple(SimpleWidths),
CidIdentity(CidWidths),
CidCMap { widths: CidWidths, cmap: Arc<CMap> },
CidVertical {
widths: CidWidths,
vertical: CidVerticalMetrics,
cmap: Option<Arc<CMap>>,
},
}
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct VerticalPlacement {
pub advance: f32,
pub origin: (f32, f32),
}
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct DecodedCode {
pub char_code: u32,
pub byte_len: u8,
pub cid: u32,
pub advance: f32,
pub word_space: bool,
}
impl FontMetrics {
pub fn decode(&self, s: &[u8]) -> Vec<DecodedCode> {
match self {
FontMetrics::Simple(w) => s
.iter()
.map(|&b| DecodedCode {
char_code: b as u32,
byte_len: 1,
cid: b as u32,
advance: w.advance(b as u32),
word_space: b == 32,
})
.collect(),
FontMetrics::CidIdentity(w) => decode_identity(w, s),
FontMetrics::CidCMap { widths, cmap } => decode_cmap(widths, cmap, s),
FontMetrics::CidVertical { widths, cmap, .. } => match cmap {
Some(cmap) => decode_cmap(widths, cmap, s),
None => decode_identity(widths, s),
},
}
}
pub fn is_vertical(&self) -> bool {
matches!(self, FontMetrics::CidVertical { .. })
}
pub fn vertical(&self, cid: u32) -> Option<VerticalPlacement> {
let FontMetrics::CidVertical {
widths, vertical, ..
} = self
else {
return None;
};
Some(VerticalPlacement {
advance: vertical.advance(cid),
origin: vertical.origin(cid, widths.advance(cid)),
})
}
}
fn decode_identity(w: &CidWidths, s: &[u8]) -> Vec<DecodedCode> {
let mut out = Vec::with_capacity(s.len() / 2);
let mut i = 0;
while i + 1 < s.len() {
let code = ((s[i] as u32) << 8) | (s[i + 1] as u32);
out.push(DecodedCode {
char_code: code,
byte_len: 2,
cid: code,
advance: w.advance(code),
word_space: false,
});
i += 2;
}
if i < s.len() {
let code = (s[i] as u32) << 8;
out.push(DecodedCode {
char_code: code,
byte_len: 1,
cid: code,
advance: w.advance(code),
word_space: false,
});
}
out
}
fn decode_cmap(widths: &CidWidths, cmap: &CMap, s: &[u8]) -> Vec<DecodedCode> {
let mut out = Vec::new();
let mut i = 0;
while i < s.len() {
let (code, n) = cmap.next_code(&s[i..]);
if n == 0 {
break;
}
let cid = cmap.cid(code);
out.push(DecodedCode {
char_code: code,
byte_len: n as u8,
cid,
advance: widths.advance(cid),
word_space: n == 1 && code == 32,
});
i += n;
}
out
}
#[cfg(test)]
mod tests {
#![allow(clippy::unwrap_used, clippy::expect_used, clippy::panic)]
use super::*;
#[test]
fn simple_is_one_byte_per_code() {
let m = FontMetrics::Simple(SimpleWidths::new(65, vec![600.0], 250.0));
let d = m.decode(b"AB");
assert_eq!(d.len(), 2);
assert_eq!(
d[0],
DecodedCode {
char_code: 65,
byte_len: 1,
cid: 65,
advance: 600.0,
word_space: false,
}
);
assert_eq!(
d[1],
DecodedCode {
char_code: 66,
byte_len: 1,
cid: 66,
advance: 250.0,
word_space: false,
}
);
}
#[test]
fn space_flags_word_spacing() {
let m = FontMetrics::Simple(SimpleWidths::new(32, vec![300.0], 0.0));
assert!(m.decode(b" ")[0].word_space);
}
#[test]
fn identity_is_two_bytes_big_endian() {
let m = FontMetrics::CidIdentity(CidWidths::new(1000.0, vec![(0x0041, 0x0041, 700.0)]));
let d = m.decode(&[0x00, 0x41, 0x00, 0x10]);
assert_eq!(d.len(), 2);
assert_eq!(
d[0],
DecodedCode {
char_code: 0x41,
byte_len: 2,
cid: 0x41,
advance: 700.0,
word_space: false,
}
);
assert_eq!(
d[1],
DecodedCode {
char_code: 0x10,
byte_len: 2,
cid: 0x10,
advance: 1000.0,
word_space: false,
}
);
}
}