Match XDV PDF text origin and baseline rounding
This commit is contained in:
1 parent
90f174283e
commit
8aa079ec25
2 files changed
+47
-3
No files matched your search
@@ -12,6 +12,18 @@ use crate::{
|
||||
use std::collections::{BTreeMap, BTreeSet};
|
||||
|
||||
const ONE_BP: i64 = 65_782;
|
||||
const HUNDRED_BP_IN_SP: i64 = 6_578_176;
|
||||
|
||||
/// Convert a relative XDV text move to the PDF decimal and its device-space
|
||||
/// advance. The reference driver rounds the residual back to a whole sp.
|
||||
fn xdv_relative_move(value: i64, digits: u8) -> (i64, i64) {
|
||||
let digits = digits + 2;
|
||||
let (number, _) = divide_scaled(value, HUNDRED_BP_IN_SP, digits);
|
||||
let scale = 10i128.pow(u32::from(digits));
|
||||
let residual = i128::from(value) * scale - i128::from(number) * i128::from(HUNDRED_BP_IN_SP);
|
||||
let error = residual.signum() * ((residual.abs() + scale / 2) / scale);
|
||||
(number, value - error as i64)
|
||||
}
|
||||
pub(super) struct NativeText {
|
||||
font: Option<FontId>,
|
||||
effective_size: i64,
|
||||
@@ -95,11 +107,13 @@ impl NativeText {
|
||||
state.xdv_matrix = paint.xdv_matrix;
|
||||
state.vertical_cid = vertical;
|
||||
let (mut shift, shift_out) = state.horizontal_move(x, state.effective_size);
|
||||
let (v, v_out) = state.vertical_move(y, digits);
|
||||
let (v, v_out) = if state.xdv { (0, 0) } else { state.vertical_move(y, digits) };
|
||||
let reset_line = if state.xdv {
|
||||
let delta = x - state.start_h - state.delta_h;
|
||||
let scale = 10i64.pow(u32::from(digits) + 2);
|
||||
let baseline_tolerance = (HUNDRED_BP_IN_SP + scale / 2) / scale;
|
||||
!state.opened
|
||||
|| v != 0
|
||||
|| (state.v - y).abs() > baseline_tolerance
|
||||
|| matrix_changed
|
||||
|| delta.abs() as f64 > 3.0 * state.xdv_size(state.effective_size)
|
||||
} else {
|
||||
@@ -138,7 +152,14 @@ impl NativeText {
|
||||
state.v = height - v_out;
|
||||
state.expansion = font.expansion;
|
||||
} else {
|
||||
let (h, h_out) = divide_scaled(x - state.start_h, 6_578_176, digits + 2);
|
||||
let (h, h_out, v, v_out) = if state.xdv {
|
||||
let (h, h_out) = xdv_relative_move(x - state.start_h, digits);
|
||||
let (v, v_out) = xdv_relative_move(state.v - y, digits);
|
||||
(h, h_out, v, v_out)
|
||||
} else {
|
||||
let (h, h_out) = divide_scaled(x - state.start_h, HUNDRED_BP_IN_SP, digits + 2);
|
||||
(h, h_out, v, v_out)
|
||||
};
|
||||
if !out.measuring {
|
||||
out.push_str(&format!(
|
||||
"{} {} Td\n",
|
||||
|
||||
Reference in new issue
Block a user