147 lines
4.7 KiB
Rust
147 lines
4.7 KiB
Rust
//! Actual PK-device font matrix, widths and pixel paths.
|
|
use crate::{
|
|
pdf_device::divide_scaled,
|
|
pk::{PkFont, PkGlyph},
|
|
tfm::TfmMetrics,
|
|
};
|
|
use std::collections::BTreeMap;
|
|
|
|
#[derive(Clone, Debug, PartialEq, Eq, Hash, serde::Serialize, serde::Deserialize)]
|
|
pub struct PkFontProgram {
|
|
pub name: String,
|
|
pub font: PkFont,
|
|
/// FontMatrix scalar in units of 10^-5.
|
|
pub scale: i64,
|
|
/// Actual Type3 /Widths, in units of 10^-2 pixel.
|
|
pub widths: BTreeMap<u8, i64>,
|
|
/// get_pk_char_width, in TeX sp; these advance the real PDF cursor.
|
|
pub advances: BTreeMap<u8, i64>,
|
|
}
|
|
impl PkFontProgram {
|
|
pub fn new(
|
|
name: String,
|
|
font: PkFont,
|
|
metrics: &TfmMetrics,
|
|
resolution: u32,
|
|
digits: u8,
|
|
) -> Result<Self, String> {
|
|
if !(72..=8000).contains(&resolution) || digits > 4 {
|
|
return Err("invalid native PK device configuration".into());
|
|
}
|
|
let size = divide_scaled(metrics.size_sp(), 6_578_176, 6).1;
|
|
let pk_scale = divide_scaled(72, i64::from(resolution), 5 + digits).0;
|
|
let font_bp = divide_scaled(size, 6_578_176, digits + 2).0;
|
|
if font_bp <= 0 {
|
|
return Err("PK font size rounds to zero at PDF device precision".into());
|
|
}
|
|
let scale = divide_scaled(pk_scale, font_bp, 0).0;
|
|
if scale <= 0 {
|
|
return Err("PK FontMatrix rounds to zero".into());
|
|
}
|
|
let mut widths = BTreeMap::new();
|
|
let mut advances = BTreeMap::new();
|
|
for slot in 0..=255u8 {
|
|
if metrics.has_slot(slot) && font.glyphs.contains_key(&u32::from(slot)) {
|
|
let w = divide_scaled(
|
|
divide_scaled(metrics.width(char::from(slot)), size, 7).0,
|
|
scale,
|
|
0,
|
|
)
|
|
.0;
|
|
widths.insert(slot, w);
|
|
advances.insert(
|
|
slot,
|
|
((scale as f64 / 100_000.0) * (w as f64 / 100.0) * size as f64) as i64,
|
|
);
|
|
}
|
|
}
|
|
Ok(Self {
|
|
name,
|
|
font,
|
|
scale,
|
|
widths,
|
|
advances,
|
|
})
|
|
}
|
|
pub fn glyph(&self, slot: u8) -> Option<&PkGlyph> {
|
|
self.font.glyphs.get(&u32::from(slot))
|
|
}
|
|
pub fn glyph_bbox(&self, slot: u8) -> Option<[i32; 4]> {
|
|
let g = self.glyph(slot)?;
|
|
if g.width == 0 || g.height == 0 {
|
|
Some([
|
|
0,
|
|
0,
|
|
(self.widths[&slot] as f64 / 100.0).round() as i32 + 1,
|
|
1,
|
|
])
|
|
} else {
|
|
Some(g.bbox())
|
|
}
|
|
}
|
|
pub fn glyph_path(&self, slot: u8) -> Result<String, String> {
|
|
let g = self
|
|
.glyph(slot)
|
|
.ok_or_else(|| format!("PK font {} has no glyph {slot}", self.name))?;
|
|
let s = self.scale as f64 / 100.0;
|
|
let mut path = String::new();
|
|
for y in 0..g.height {
|
|
let mut x = 0;
|
|
while x < g.width {
|
|
if !g.pixel(x, y) {
|
|
x += 1;
|
|
continue;
|
|
}
|
|
let start = x;
|
|
while x < g.width && g.pixel(x, y) {
|
|
x += 1;
|
|
}
|
|
let left = (i64::from(start) - i64::from(g.x_offset)) as f64 * s;
|
|
let bottom = (i64::from(g.y_offset) - i64::from(y)) as f64 * s;
|
|
let w = f64::from(x - start) * s;
|
|
path.push_str(&format!("M{left:.5} {bottom:.5}h{w:.5}v{s:.5}h{:.5}Z", -w));
|
|
}
|
|
}
|
|
Ok(path)
|
|
}
|
|
}
|
|
|
|
/// pdfTeX's float-sized request and kpathsea's half-magstep correction.
|
|
pub fn requested_dpi(resolution: u32, size: i64, design_size: i64) -> Result<u32, String> {
|
|
if resolution == 0 || size <= 0 || design_size <= 0 {
|
|
return Err("invalid PK density request".into());
|
|
}
|
|
let requested = ((resolution as f32) * (size as f32 / design_size as f32)).round() as u32;
|
|
let negative = requested < resolution;
|
|
for m in 0..40 {
|
|
let mut n = m;
|
|
let mut t = if n % 2 == 1 {
|
|
n -= 1;
|
|
1.095445115
|
|
} else {
|
|
1.0
|
|
};
|
|
while n > 8 {
|
|
n -= 8;
|
|
t *= 2.0736;
|
|
}
|
|
while n > 0 {
|
|
n -= 2;
|
|
t *= 1.2;
|
|
}
|
|
let dpi = (0.5
|
|
+ if negative {
|
|
f64::from(resolution) / t
|
|
} else {
|
|
f64::from(resolution) * t
|
|
}) as u32;
|
|
if dpi.abs_diff(requested) <= 1 {
|
|
return Ok(dpi);
|
|
}
|
|
if (negative && dpi < requested) || (!negative && dpi > requested) {
|
|
break;
|
|
}
|
|
}
|
|
Ok(requested)
|
|
}
|