use std::collections::{HashMap, HashSet};
use serde::{Deserialize, Serialize};
use crate::{CompileRequest, Engine, Error, Result, query_json};
pub const PROTOCOL_VERSION: &str = "ctypst-measure-v1";
pub const QUERY_LABEL: &str = "ctypst-measure-v1";
const PROGRAM_PATH: &str = "/ctypst/measure-v1.typ";
const REQUEST_PATH: &str = "/ctypst/request.json";
const PROGRAM_SOURCE: &str = include_str!("../typst/measure-v1.typ");
const CALIBRATION_ID: &str = "__calibration";
const ASSET_HASH: u64 = fnv1a64(PROGRAM_SOURCE.as_bytes());
#[derive(Clone, Copy, Debug, Deserialize, Eq, PartialEq, Serialize)]
#[serde(rename_all = "lowercase")]
pub enum Weight {
Regular,
Bold,
}
#[derive(Clone, Debug, Deserialize, PartialEq, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct MeasureItem {
pub id: String,
pub text: String,
pub font_size: f64,
pub weight: Weight,
pub usable_width_pt: f64,
}
#[derive(Clone, Debug, Deserialize, PartialEq, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct MeasureFormat {
pub font: String,
pub base_font_size: f64,
pub entry_heading_size: f64,
pub leading_value: f64,
pub leading_relative: bool,
pub margin_left: f64,
pub margin_right: f64,
pub page_size: String,
}
#[derive(Clone, Copy, Debug, Deserialize, PartialEq, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct MeasureCalibration {
pub cap_ratio_regular: f64,
pub advance_ratio_regular: f64,
pub cap_ratio_bold: f64,
pub advance_ratio_bold: f64,
}
#[derive(Clone, Debug, PartialEq, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct MeasureResult {
pub id: String,
pub width_pt: f64,
pub height_pt: f64,
pub lines: u64,
pub char_budget: Option<i64>,
}
pub struct MeasureClient {
engine: Engine,
font_hash: u64,
tag: Option<String>,
results: HashMap<String, MeasureResult>,
calibration: Option<MeasureCalibration>,
compiles: u64,
}
impl MeasureClient {
#[must_use = "a measurement client does nothing until it measures"]
pub fn new(fonts: impl IntoIterator<Item = impl AsRef<[u8]>>) -> Result<Self> {
let collected: Vec<Vec<u8>> = fonts
.into_iter()
.map(|font| font.as_ref().to_vec())
.collect();
let mut font_hash = 0xcbf2_9ce4_8422_2325_u64;
for bytes in &collected {
font_hash = fnv1a64_update(font_hash, bytes);
}
let engine = Engine::builder()
.fonts(collected.iter().map(Vec::as_slice))
.source(PROGRAM_PATH, PROGRAM_SOURCE)
.map_err(|error| Error::Measure(format!("cannot install {PROTOCOL_VERSION}: {error}")))?
.build()?;
Ok(Self {
engine,
font_hash,
tag: None,
results: HashMap::new(),
calibration: None,
compiles: 0,
})
}
pub fn measure_all(
&mut self,
format: &MeasureFormat,
items: &[MeasureItem],
) -> Result<Vec<MeasureResult>> {
validate(format, items)?;
if items.is_empty() {
return Ok(Vec::new());
}
let tag = format_tag(self.font_hash, format);
if self.tag.as_deref() != Some(&tag) {
self.results.clear();
self.calibration = None;
self.tag = Some(tag);
}
let misses: Vec<MeasureItem> = items
.iter()
.filter(|item| {
!self
.results
.contains_key(&item_key(self.font_hash, format, item))
})
.cloned()
.collect();
if !misses.is_empty() {
self.compile_misses(format, &misses)?;
}
items
.iter()
.map(|item| {
self.results
.get(&item_key(self.font_hash, format, item))
.cloned()
.ok_or_else(|| Error::Measure(format!("cache omitted fragment {}", item.id)))
})
.collect()
}
#[must_use]
pub fn calibration(&self) -> Option<MeasureCalibration> {
self.calibration
}
#[must_use]
pub fn compile_count(&self) -> u64 {
self.compiles
}
fn compile_misses(&mut self, format: &MeasureFormat, misses: &[MeasureItem]) -> Result<()> {
let request = serde_json::json!({
"version": PROTOCOL_VERSION,
"format": normalized_format(format),
"items": misses,
});
let source = serde_json::to_string(&request)
.map_err(|error| Error::Measure(format!("cannot serialize request: {error}")))?;
let document = self
.engine
.compile(CompileRequest::new(PROGRAM_PATH.to_owned()).binary_file(REQUEST_PATH, source))
.map_err(|error| Error::Measure(format!("{PROTOCOL_VERSION} run failed: {error}")))?
.document;
self.compiles += 1;
let values = query_json(&document, QUERY_LABEL)
.map_err(|error| Error::Measure(format!("{PROTOCOL_VERSION} query failed: {error}")))?;
decode_response(self.font_hash, format, misses, &values, &mut self.results)?;
let calibration = decode_calibration(&values)?;
self.calibration = Some(calibration);
Ok(())
}
}
#[must_use]
pub fn char_budget(text: &str, width_pt: f64, usable_width_pt: f64) -> Option<i64> {
if text.is_empty() || width_pt <= 0.0 {
return None;
}
#[allow(clippy::cast_precision_loss)]
let units = text.encode_utf16().count() as f64;
#[allow(clippy::cast_possible_truncation)]
let budget = (units * (usable_width_pt / width_pt - 1.0)).round() as i64;
Some(budget)
}
#[must_use]
pub fn leading_em(leading_value: f64, leading_relative: bool, base_font_size: f64) -> f64 {
if leading_relative {
leading_value
} else {
(leading_value / base_font_size * 10_000.0).round() / 10_000.0
}
}
pub(crate) const fn fnv1a64(bytes: &[u8]) -> u64 {
fnv1a64_update(0xcbf2_9ce4_8422_2325_u64, bytes)
}
pub(crate) const fn fnv1a64_update(mut hash: u64, bytes: &[u8]) -> u64 {
let mut index = 0;
while index < bytes.len() {
hash ^= bytes[index] as u64;
hash = hash.wrapping_mul(0x0100_0000_01b3);
index += 1;
}
hash
}
fn effective_font(format: &MeasureFormat) -> &str {
if format.font.is_empty() {
"Archivo"
} else {
&format.font
}
}
fn effective_page_size(format: &MeasureFormat) -> &str {
if format.page_size.is_empty() {
"a4"
} else {
&format.page_size
}
}
fn normalized_format(format: &MeasureFormat) -> serde_json::Value {
serde_json::json!({
"font": effective_font(format),
"baseFontSize": format.base_font_size,
"entryHeadingSize": format.entry_heading_size,
"leadingEm": leading_em(format.leading_value, format.leading_relative, format.base_font_size),
"marginLeft": format.margin_left,
"marginRight": format.margin_right,
"pageSize": effective_page_size(format),
})
}
fn format_tag(font_hash: u64, format: &MeasureFormat) -> String {
format!(
"{PROTOCOL_VERSION}/{ASSET_HASH:016x}/{}/{font_hash:016x}/{}",
env!("CARGO_PKG_VERSION"),
serde_json::to_string(&normalized_format(format)).unwrap_or_default(),
)
}
fn item_key(font_hash: u64, format: &MeasureFormat, item: &MeasureItem) -> String {
format!(
"{}/{}",
format_tag(font_hash, format),
serde_json::to_string(item).unwrap_or_default(),
)
}
fn validate(format: &MeasureFormat, items: &[MeasureItem]) -> Result<()> {
let fail = |what: &str| Error::Measure(format!("invalid {PROTOCOL_VERSION} request: {what}"));
for (field, value) in [
("base_font_size", format.base_font_size),
("entry_heading_size", format.entry_heading_size),
("leading_value", format.leading_value),
("margin_left", format.margin_left),
("margin_right", format.margin_right),
] {
if !value.is_finite() {
return Err(fail(&format!("{field} is not finite")));
}
}
if format.base_font_size <= 0.0 || format.entry_heading_size <= 0.0 {
return Err(fail("font sizes must be positive"));
}
if format.leading_value < 0.0 {
return Err(fail("leading must not be negative"));
}
if format.margin_left < 0.0 || format.margin_right < 0.0 {
return Err(fail("margins must not be negative"));
}
let leading = leading_em(
format.leading_value,
format.leading_relative,
format.base_font_size,
);
if !leading.is_finite() || leading <= 0.0 {
return Err(fail("resolved leading must be positive"));
}
let mut seen = HashSet::new();
for item in items {
if item.id.is_empty() {
return Err(fail("item id is empty"));
}
if !seen.insert(item.id.as_str()) {
return Err(fail(&format!("duplicate item id {}", item.id)));
}
if !item.font_size.is_finite() || item.font_size <= 0.0 {
return Err(fail(&format!("item {} has no positive font size", item.id)));
}
if !item.usable_width_pt.is_finite() || item.usable_width_pt <= 0.0 {
return Err(fail(&format!("item {} has no positive width", item.id)));
}
}
Ok(())
}
#[derive(Deserialize)]
struct ResultRow {
w: f64,
h: f64,
lines: u64,
}
#[derive(Deserialize)]
struct CalibrationRow {
ratios: RatioRow,
}
#[derive(Clone, Copy, Deserialize)]
struct RatioRow {
#[serde(rename = "cap-reg")]
cap_reg: f64,
#[serde(rename = "adv-reg")]
adv_reg: f64,
#[serde(rename = "cap-bold")]
cap_bold: f64,
#[serde(rename = "adv-bold")]
adv_bold: f64,
}
fn decode_response(
font_hash: u64,
format: &MeasureFormat,
misses: &[MeasureItem],
values: &[serde_json::Value],
results: &mut HashMap<String, MeasureResult>,
) -> Result<()> {
let mut rows: HashMap<&str, &serde_json::Value> = HashMap::new();
for value in values {
let id = value
.get("id")
.and_then(serde_json::Value::as_str)
.ok_or_else(|| Error::Measure(format!("{PROTOCOL_VERSION} result has no id")))?;
if id == CALIBRATION_ID {
continue;
}
if rows.insert(id, value).is_some() {
return Err(Error::Measure(format!(
"{PROTOCOL_VERSION} duplicated fragment {id}"
)));
}
}
let wanted: HashSet<&str> = misses.iter().map(|item| item.id.as_str()).collect();
let found: HashSet<&str> = rows.keys().copied().collect();
if found != wanted {
let missing: Vec<&&str> = wanted.difference(&found).collect();
let extra: Vec<&&str> = found.difference(&wanted).collect();
return Err(Error::Measure(format!(
"{PROTOCOL_VERSION} returned an incomplete fragment set (missing {missing:?}, unexpected {extra:?})"
)));
}
for item in misses {
let raw = rows.get(item.id.as_str()).copied().ok_or_else(|| {
Error::Measure(format!("{PROTOCOL_VERSION} omitted fragment {}", item.id))
})?;
let row: ResultRow = serde_json::from_value((*raw).clone()).map_err(|error| {
Error::Measure(format!("fragment {} is malformed: {error}", item.id))
})?;
if !row.w.is_finite() || row.w < 0.0 || !row.h.is_finite() || row.h < 0.0 {
return Err(Error::Measure(format!(
"fragment {} has invalid dimensions",
item.id
)));
}
if row.lines < 1 {
return Err(Error::Measure(format!(
"fragment {} reports no lines",
item.id
)));
}
results.insert(
item_key(font_hash, format, item),
MeasureResult {
id: item.id.clone(),
width_pt: row.w,
height_pt: row.h,
lines: row.lines,
char_budget: char_budget(&item.text, row.w, item.usable_width_pt),
},
);
}
Ok(())
}
fn decode_calibration(values: &[serde_json::Value]) -> Result<MeasureCalibration> {
let mut found: Option<MeasureCalibration> = None;
for value in values {
if value.get("id").and_then(serde_json::Value::as_str) != Some(CALIBRATION_ID) {
continue;
}
if found.is_some() {
return Err(Error::Measure(format!(
"{PROTOCOL_VERSION} duplicated calibration"
)));
}
let row: CalibrationRow = serde_json::from_value(value.clone())
.map_err(|error| Error::Measure(format!("calibration is malformed: {error}")))?;
for ratio in [
row.ratios.cap_reg,
row.ratios.adv_reg,
row.ratios.cap_bold,
row.ratios.adv_bold,
] {
if !ratio.is_finite() {
return Err(Error::Measure(format!(
"{PROTOCOL_VERSION} calibration is not finite"
)));
}
}
found = Some(MeasureCalibration {
cap_ratio_regular: row.ratios.cap_reg,
advance_ratio_regular: row.ratios.adv_reg,
cap_ratio_bold: row.ratios.cap_bold,
advance_ratio_bold: row.ratios.adv_bold,
});
}
found.ok_or_else(|| Error::Measure(format!("{PROTOCOL_VERSION} omitted calibration")))
}