Skip to content

perf(engine): binary-search auto-size font fitting + deprecate unused adjustFontSizeToFit (F12)#146

Merged
DemchaAV merged 2 commits into
developfrom
perf/autosize-binary-search
Jun 9, 2026
Merged

perf(engine): binary-search auto-size font fitting + deprecate unused adjustFontSizeToFit (F12)#146
DemchaAV merged 2 commits into
developfrom
perf/autosize-binary-search

Conversation

@DemchaAV

@DemchaAV DemchaAV commented Jun 9, 2026

Copy link
Copy Markdown
Owner

Auto-size font fitting binary-searches the size grid (the optimization)

A paragraph with .autoSize(maxSize, minSize) resolved its font size by scanning every step from max down to min and re-measuring the longest line at each candidate (default step 0.5 → up to ~50 measurements). Line width is linear in font size, so the fit predicate is monotonic — the search now binary-searches the grid for the same boundary in ~log2(n) measurements (≈6 instead of ~50), the same pattern as the F2 fitCharacters fix.

  • Byte-identical — it returns the same grid size the linear scan did (for the default 0.5 step, maxSize - i*step equals the scan's repeated subtraction exactly). Guarded by the 11 existing auto-size integration / snapshot tests, including LayoutSnapshotExtractorTest which snapshots the resolved size. No public API or behaviour change.

Deprecate the unused, buggy adjustFontSizeToFit

Font#adjustFontSizeToFit (and the PdfFont / WordFont implementations) is dead code — zero callers — and the PdfFont implementation is incorrect: it re-measures with the unchanged style, so textWidth never shrinks and the loop runs to the minimum size regardless of fit. Marked @Deprecated rather than deleted because engine.font.Font is in the japicmp binary-compat baseline (deletion is a next-major change); the javadoc points to the canonical layout-compiler auto-size path. Dropped a dead local in PdfFont while there.

Verification

  • ./mvnw verify -pl . — BUILD SUCCESS, 1151 tests, 0 failures (checkstyle + SpotBugs + javadoc); auto-size suite unchanged (proves byte-identity).
  • @Deprecated is binary-compatible, so the japicmp gate stays green.

DemchaAV added 2 commits June 9, 2026 01:22
…adjustFontSizeToFit

Auto-size paragraphs (.autoSize(...)) resolved the font size by scanning every step
from max down to min, re-measuring the line at each candidate (up to ~50 measures).
Line width is linear in font size so the fit is monotonic; binary-search the grid
for the same boundary in ~log2(n) measures instead of n. Byte-identical: returns the
same grid size the linear scan did (covered by the existing auto-size integration
and snapshot tests).

Also deprecate Font.adjustFontSizeToFit (+ the PdfFont/WordFont implementations):
unused and incorrect — the PdfFont impl re-measured with the unchanged style, so it
always returned the minimum size. Kept for binary compatibility; canonical auto-size
is the layout compiler path above. Dropped a dead local while there.

Finding 12.
@DemchaAV DemchaAV merged commit efdcce7 into develop Jun 9, 2026
11 checks passed
@DemchaAV DemchaAV deleted the perf/autosize-binary-search branch June 9, 2026 00:40
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.

1 participant