Skip to content

A fast, helpful, and open-source document parser

fix several projection bugs

Logan Markewich049d57f2adbe
diff --git a/src/engines/pdf/pdfjs.ts b/src/engines/pdf/pdfjs.ts
index 3666efb..dd0ec39 100644
--- a/src/engines/pdf/pdfjs.ts
+++ b/src/engines/pdf/pdfjs.ts
@@ -87,30 +87,6 @@ function applyTransformation(
};
}
-/**
- * Decompose transformation matrix to get scale factors using SVD
- * This computes the singular values of the 2D transformation matrix
- */
-function singularValueDecompose2dScale(m: number[]): { x: number; y: number } {
- // Create transpose of the 2x2 part of the matrix
- const transpose = [m[0], m[2], m[1], m[3]];
-
- // Multiply matrix m with its transpose to get eigenvalues
- const a = m[0] * transpose[0] + m[1] * transpose[2];
- const b = m[0] * transpose[1] + m[1] * transpose[3];
- const c = m[2] * transpose[0] + m[3] * transpose[2];
- const d = m[2] * transpose[1] + m[3] * transpose[3];
-
- // Solve the second degree polynomial to get roots (eigenvalues)
- const first = (a + d) / 2;
- const second = Math.sqrt((a + d) ** 2 - 4 * (a * d - c * b)) / 2;
- const sx = first + second || 1;
- const sy = first - second || 1;
-
- // Scale values are the square roots of the eigenvalues
- return { x: Math.sqrt(sx), y: Math.sqrt(sy) };
-}
-
// Pre-compiled regex patterns for string decoding
const BUGGY_FONT_MARKER_REGEX = /:->\|>_(\d+)_\d+_<\|<-:/g;
const BUGGY_FONT_MARKER_CHECK = ":->|>";
@@ -268,11 +244,18 @@ export class PdfJsEngine implements PdfEngine {
// Get lower-left corner (text space origin)
const ll = applyTransformation({ x: 0, y: 0 }, cm);
- // Get scale factors to properly size the bounding box
- const scale = singularValueDecompose2dScale(item.transform);
-
- // Get upper-right corner
- const ur = applyTransformation({ x: item.width / scale.x, y: item.height / scale.y }, cm);
+ // Extract scale factors directly from matrix components (not SVD).
+ // For matrix [a, b, c, d, tx, ty]:
+ // - Horizontal scale = sqrt(a² + b²)
+ // - Vertical scale = sqrt(c² + d²)
+ // This correctly preserves axis association unlike SVD which returns
+ // singular values sorted by magnitude (causing x/y swap for some fonts).
+ const scaleX = Math.sqrt(item.transform[0] ** 2 + item.transform[1] ** 2);
+ const scaleY = Math.sqrt(item.transform[2] ** 2 + item.transform[3] ** 2);
+
+ // Get upper-right corner by first converting width/height to text space
+ // (dividing by the scale factors), then transforming to viewport space
+ const ur = applyTransformation({ x: item.width / scaleX, y: item.height / scaleY }, cm);
// Calculate final bounding box in viewport space
const left = Math.min(ll.x, ur.x);
diff --git a/src/processing/gridProjection.ts b/src/processing/gridProjection.ts
index ab539a6..61a05e1 100644
--- a/src/processing/gridProjection.ts
+++ b/src/processing/gridProjection.ts
@@ -666,9 +666,17 @@ export function bboxToLine(
medianWidth: number,
medianHeight: number
): ProjectionTextBox[][] {
- // sort lines on first y axis then x axis (top - left)
+ // Y-tolerance for sorting: items within this threshold are considered same line
+ // This handles floating point precision issues between columns (e.g., 334.7400 vs 334.7399)
+ const Y_SORT_TOLERANCE = 2.0;
+
+ // Note: We keep whitespace items as they may be needed for proper word separation.
+ // The spacing calculation handles gaps between items.
+
+ // sort lines on first y axis then x axis (top - left)
+ // Use Y tolerance so items on same visual line sort by x regardless of tiny y differences
textBbox.sort((a, b) => {
- if (a.y == b.y) {
+ if (Math.abs(a.y - b.y) < Y_SORT_TOLERANCE) {
return a.x - b.x;
}
return a.y - b.y;
@@ -752,6 +760,7 @@ export function bboxToLine(
break;
}
}
+
if (
!lineCollide &&
((bbox.y + bbox.h * 0.5 >= lineMinY && bbox.y + bbox.h * 0.5 <= lineMaxY) ||
@@ -1140,6 +1149,17 @@ export function projectToGrid(
const xDelta = bbox.x - (prevBbox.x + prevBbox.w);
const prevCharWidth = prevBbox.w / prevBbox.strLength;
if (xDelta > prevCharWidth * 2) {
+ // Check if both items are clearly within the same column
+ // Use start x-coordinate for left column check, not x + w, to handle
+ // items that extend near the column boundary
+ const pageMidpoint = page.width * 0.5;
+ const prevInLeftColumn = prevBbox.x < pageMidpoint - 30;
+ const bboxInLeftColumn = bbox.x < pageMidpoint - 30;
+ const prevInRightColumn = prevBbox.x > pageMidpoint + 10;
+ const bboxInRightColumn = bbox.x > pageMidpoint + 10;
+ const bothInSameColumn =
+ (prevInLeftColumn && bboxInLeftColumn) || (prevInRightColumn && bboxInRightColumn);
+
// insert column spacing if any of:
// - gap is more than an approximate tab (8x average char width)
// - previous bbox is right snap
@@ -1152,9 +1172,12 @@ export function projectToGrid(
(prevBbox.snap && prevBbox.snap === "right") ||
(bbox.snap && prevBbox.snap)
) {
- bbox.shouldSpace = COLUMN_SPACES;
+ // If both items are in the same column, limit spacing to avoid
+ // preserving justified text gaps from PDFs
+ bbox.shouldSpace = bothInSameColumn ? FLOATING_SPACES : COLUMN_SPACES;
} else {
- bbox.shouldSpace = FLOATING_SPACES;
+ // For items in the same column, use minimal spacing
+ bbox.shouldSpace = bothInSameColumn ? 1 : FLOATING_SPACES;
}
}
} else {
@@ -1235,7 +1258,19 @@ export function projectToGrid(
if (!bbox.forceUnsnapped) {
const floatingAnchor = forwardAnchors.floating[Math.round(bbox.x)];
if (floatingAnchor && targetX < floatingAnchor) {
- targetX = floatingAnchor;
+ // Only apply floating anchor alignment if it doesn't create excessive gaps
+ // For items within a single column (not near the page midpoint), limit the gap
+ const pageMidpoint = page.width * 0.5;
+ const isInLeftColumn = bbox.x + bbox.w < pageMidpoint - 20;
+ const isInRightColumn = bbox.x > pageMidpoint + 20;
+ const isClearlyInOneColumn = isInLeftColumn || isInRightColumn;
+
+ // Limit floating anchor adjustment to avoid excessive gaps in justified text
+ const maxFloatingGap = isClearlyInOneColumn ? 4 : floatingAnchor;
+ const adjustedAnchor = Math.min(floatingAnchor, targetX + maxFloatingGap);
+ if (adjustedAnchor > targetX) {
+ targetX = adjustedAnchor;
+ }
}
}