Skip to content

Wrong bbox in location for vertical font#708

Merged
MaximPlusov merged 2 commits into
integrationfrom
vertical-font-support
Jul 16, 2026
Merged

Wrong bbox in location for vertical font#708
MaximPlusov merged 2 commits into
integrationfrom
vertical-font-support

Conversation

@Vladimir-Burshnev

@Vladimir-Burshnev Vladimir-Burshnev commented Jul 14, 2026

Copy link
Copy Markdown
Contributor

Closes: https://trello.com/c/SCBNY4Sg
pdf.js PR: veraPDF/pdf.js#30
veraPDF-validation PR: veraPDF/veraPDF-validation#735

Summary by CodeRabbit

  • New Features

    • Added support for vertical metrics in CID fonts, including lookup of per-CID vertical widths.
    • Added detection of vertical writing modes and vertical-width queries with default fallback metrics.
    • Improved interpretation of vertical metric ranges alongside individual CID mappings.
  • Bug Fixes

    • Enhanced robustness when vertical-width/metrics data is missing or malformed, with safer handling of invalid entries and improved diagnostic logging.

@coderabbitai

coderabbitai Bot commented Jul 14, 2026

Copy link
Copy Markdown

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 87d3f084-fd4f-43cd-ab38-846270d0483d

📥 Commits

Reviewing files that changed from the base of the PR and between 2db26a8 and 9032a2a.

📒 Files selected for processing (2)
  • src/main/java/org/verapdf/pd/font/CIDW2Array.java
  • src/main/java/org/verapdf/pd/font/CIDWArray.java
🚧 Files skipped from review as they are similar to previous changes (2)
  • src/main/java/org/verapdf/pd/font/CIDW2Array.java
  • src/main/java/org/verapdf/pd/font/CIDWArray.java

📝 Walkthrough

Walkthrough

This PR adds vertical metrics support for CID fonts through new W2 parsing and metric classes, PDCIDFont vertical-width APIs, and DW2 fallback handling. It also adds null checks and standardized logging for invalid CID W-array entries.

Changes

Vertical Metrics for CID Fonts

Layer / File(s) Summary
Vertical metrics data model
src/main/java/org/verapdf/pd/font/CIDVerticalMetrics.java, src/main/java/org/verapdf/pd/font/CIDW2ArrayRange.java
Adds immutable vertical metric values and inclusive CID ranges with displacement accessors.
W2 array parsing and displacement lookup
src/main/java/org/verapdf/pd/font/CIDW2Array.java
Parses W2 entries into explicit mappings and CID ranges, extracts metric components, and resolves displacement by CID.
PDCIDFont vertical width integration
src/main/java/org/verapdf/pd/font/PDCIDFont.java
Adds vertical orientation detection, lazy W2 lookup, and DW2-derived default vertical metrics with constant fallback.
CIDWArray invalid-input handling
src/main/java/org/verapdf/pd/font/CIDWArray.java
Adds nullable cidBegin checks and standardizes invalid W-array logging and early returns.

Estimated code review effort: 3 (Moderate) | ~25 minutes

Sequence Diagram(s)

sequenceDiagram
  participant PDCIDFont
  participant CIDW2Array
  participant CIDW2ArrayRange

  PDCIDFont->>CIDW2Array: construct from W2 array
  CIDW2Array->>CIDW2Array: populate mappings and ranges
  PDCIDFont->>CIDW2Array: getDisplacement(cid)
  CIDW2Array->>CIDW2ArrayRange: check CID membership
  CIDW2ArrayRange-->>CIDW2Array: return displacement
  CIDW2Array-->>PDCIDFont: return displacement or null
  PDCIDFont->>PDCIDFont: apply DW2 or constant fallback
Loading
🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 75.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title is relevant and clearly points to the vertical-font bounding box fix described by the changeset.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch vertical-font-support

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 2

🧹 Nitpick comments (2)
src/main/java/org/verapdf/pd/font/PDCIDFont.java (2)

146-157: 🚀 Performance & Scalability | 🔵 Trivial | 💤 Low value

Cache the absence of W2 to avoid repeated lookups.

If the W2 array is missing or invalid, this.verticalMetrics remains null. As a result, every subsequent call to getVerticalWidth() will repeat the dictionary.getKey(ASAtom.W2) lookup. Consider caching the absence of the W2 array (e.g., by assigning a singleton empty CIDW2Array or using a boolean flag) to avoid redundant dictionary lookups per character.

Note: getWidth() exhibits the exact same pattern for the W array, so this optimization could be applied to both.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@src/main/java/org/verapdf/pd/font/PDCIDFont.java` around lines 146 - 157,
Update getVerticalWidth so missing or invalid W2 data is cached instead of
leaving verticalMetrics null; use the existing CIDW2Array mechanism or a
dedicated flag, while preserving the default width result. Apply the same
absence-caching optimization to getWidth for the W array if it has the
equivalent lazy lookup pattern.

175-186: 🚀 Performance & Scalability | 🔵 Trivial | ⚡ Quick win

Cache default vertical metrics and fix indentation.

getDefaultVMetrics() performs dictionary lookups and potentially allocates new Double[2] on every call. Since it is invoked by getVerticalWidth() per character, this can introduce a significant performance overhead. Consider caching the parsed DW2 metrics during initialization, similar to what is done for verticalMetrics.

Additionally:

  • Returning the mutable static array DEFAULT_CID_FONT_VERTICAL_WIDTH as a fallback risks accidental global state corruption if a downstream caller modifies the returned array. Caching a read-only or cloned copy can prevent this.
  • Lines 179, 181, and 183 inconsistently use tabs for indentation instead of spaces.
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@src/main/java/org/verapdf/pd/font/PDCIDFont.java` around lines 175 - 186,
Cache the parsed DW2 metrics during PDCIDFont initialization and have
getDefaultVMetrics() reuse that cached value instead of repeating dictionary
lookups and allocations. Ensure fallback results do not expose the mutable
DEFAULT_CID_FONT_VERTICAL_WIDTH array by returning a safe immutable or cloned
representation, while preserving the existing default-metrics behavior.
Normalize the indentation of the affected assignments and null checks to spaces.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@src/main/java/org/verapdf/pd/font/CIDW2Array.java`:
- Line 41: Update the logger declaration in CIDW2Array to use CIDW2Array.class,
then review the invalid W2-entry logging sites in the relevant methods and
change Level.FINE to Level.SEVERE if these errors should follow the PR’s unified
severity; keep the severity unchanged only if the existing behavior is
explicitly intentional.

In `@src/main/java/org/verapdf/pd/font/CIDWArray.java`:
- Around line 57-59: Update the cidBegin null-check in CIDWArray parsing to
return immediately after logging the invalid W array, matching the existing
width == null handling. This must prevent subsequent cidBegin.intValue() calls
and abort parsing before the token sequence becomes desynchronized.

---

Nitpick comments:
In `@src/main/java/org/verapdf/pd/font/PDCIDFont.java`:
- Around line 146-157: Update getVerticalWidth so missing or invalid W2 data is
cached instead of leaving verticalMetrics null; use the existing CIDW2Array
mechanism or a dedicated flag, while preserving the default width result. Apply
the same absence-caching optimization to getWidth for the W array if it has the
equivalent lazy lookup pattern.
- Around line 175-186: Cache the parsed DW2 metrics during PDCIDFont
initialization and have getDefaultVMetrics() reuse that cached value instead of
repeating dictionary lookups and allocations. Ensure fallback results do not
expose the mutable DEFAULT_CID_FONT_VERTICAL_WIDTH array by returning a safe
immutable or cloned representation, while preserving the existing
default-metrics behavior. Normalize the indentation of the affected assignments
and null checks to spaces.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: c8dcd7ac-10fd-405e-969e-673d8cf832fa

📥 Commits

Reviewing files that changed from the base of the PR and between 57633dc and 2db26a8.

📒 Files selected for processing (5)
  • src/main/java/org/verapdf/pd/font/CIDVerticalMetrics.java
  • src/main/java/org/verapdf/pd/font/CIDW2Array.java
  • src/main/java/org/verapdf/pd/font/CIDW2ArrayRange.java
  • src/main/java/org/verapdf/pd/font/CIDWArray.java
  • src/main/java/org/verapdf/pd/font/PDCIDFont.java

Comment thread src/main/java/org/verapdf/pd/font/CIDW2Array.java Outdated
Comment thread src/main/java/org/verapdf/pd/font/CIDWArray.java
@MaximPlusov
MaximPlusov force-pushed the vertical-font-support branch from 2db26a8 to 8870bc3 Compare July 15, 2026 13:50
@MaximPlusov
MaximPlusov force-pushed the vertical-font-support branch from 8870bc3 to 9032a2a Compare July 16, 2026 18:55
@MaximPlusov
MaximPlusov merged commit ab43cc9 into integration Jul 16, 2026
8 of 9 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants