Skip to content

Draw a plain borderless free text box as PDF.js does - #118

Open
KleiKodesh wants to merge 1 commit into
embedpdf:embedpdf/mainfrom
KleiKodesh:fix/free-text-plain-box
Open

KleiKodesh wants to merge 1 commit into
embedpdf:embedpdf/mainfrom
KleiKodesh:fix/free-text-plain-box

Conversation

@KleiKodesh

Copy link
Copy Markdown

A free text annotation without /BS always got the default 1 pt border, even when its /Border array says 0. PDF.js writes exactly that kind of box: /Border [0 0 0], /Contents, /DA, no /RC, and for non-Latin text no /AP at all. Generating its appearance then:

  • drew a 1 pt frame the author never asked for;
  • inset the text plate by 2 pt on every side;
  • wrapped the text, because PDF.js sizes the rect to the text as it measured it, and clipped the wrapped line away. A Hebrew "שלום עולם" showed as a framed "שלום".

Changes:

  • Without /BS, a free text box's border width comes from /Border (ISO 32000-1 12.5.2), as the callout path already reads it.
  • A plain box (no /RC) with no border lays out as PDF.js draws it (FreeTextAnnotation / FakeUnicodeFont.createAppearance): lines break only at line breaks, and text wider or taller than the box is drawn smaller, by the smaller of the two ratios. Each paragraph reads in its text's direction, so right-to-left text lines up from the right edge when /Q asks for it.
  • CPDF_RichTextLayout::Options::shrink_to_fit carries the new layout mode; auto size (/DA size 0) keeps its ladder.

Boxes with a border, with /RC, or written by Acrobat lay out as before.

Tests

  • New unit test CPDFGenerateAPTest.FreeTextWithoutBorderShrinksToFit: a 40 pt wide box with /Border [0 0 0] and "hello world" at 12 pt draws no border stroke and a font size below 12.
  • Not built or run locally (no emscripten/clang toolchain on the machine this was written on); CI hasn't run yet.

A free text box without /BS took the default 1 pt border even when its
/Border array says 0, so a box PDF.js wrote (/Border [0 0 0], no /AP,
no /RC) got a frame and a text plate inset by 2 pt on every side. Its
rect fits the text as PDF.js measured it, so the text then wrapped and
the second line was clipped away.

- Without /BS, the box's border width is the /Border array's, as the
  callout already reads it.
- A plain box (no /RC) whose border is 0 lays out as PDF.js draws it:
  lines break only at line breaks, text too wide or too tall for the
  box is drawn smaller to fit (CPDF_RichTextLayout::Options::
  shrink_to_fit), and each paragraph reads in its text's direction.
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