Skip to content

[4.0] fix(bbb-export-annotations): wrap exported text exactly as the client does - #26013

Merged
ramonlsouza merged 2 commits into
bigbluebutton:v4.0.x-developfrom
antobinary:fix-export-text-wrap-harfbuzz-40
Oct 16, 2026
Merged

ramonlsouza merged 2 commits into
bigbluebutton:v4.0.x-developfrom
antobinary:fix-export-text-wrap-harfbuzz-40

Conversation

@antobinary

Copy link
Copy Markdown
Member

What does this PR do?

Makes "download presentation with annotations" wrap width-constrained text annotations exactly as the client rendered them.

The export re-wrapped text by summing glyph advances with opentype.js, while the client wraps with the browser's text layout, which shapes text with HarfBuzz and applies the font's OpenType features. The draw font (Shantell Sans) carries GPOS kerning and GSUB contextual alternates that opentype.js does not apply, so the export measured long lines 2-3% wider than they rendered live. The margin from #24566 is a constant and cannot absorb an error that grows with line length: a line the browser laid out at 1340.8 px measured 1364.1 px in the export, against a 14 px margin. A second defect orphaned trailing punctuation: the wrapper split tokens at word boundaries (\b) and then broke the accumulated line at character granularity, which a browser never does.

Changes in bbb-export-annotations/shapes/Shape.js:

  • Measure text by shaping it with HarfBuzz (harfbuzzjs). With the font's default features the widths match Chrome exactly: 1340.8 px and 1239.1 px for the two critical lines of the report, on both sides.
  • Wrap where a browser wraps: at whitespace or after a hyphen; split inside a word only when the word alone exceeds the line; trailing whitespace hangs instead of forcing a wrap.
  • Keep a tolerance of one narrowest-glyph width in the "do not wrap" direction only, since that error mode cannot interleave text.
  • Parse each font once per process and reuse one scratch buffer. The previous code re-read and re-parsed the woff2 font for every shape.
  • Drop opentype.js from the dependencies.

About the replacement library

harfbuzzjs is the HarfBuzz project's own JavaScript build of HarfBuzz, compiled to WebAssembly: https://github.com/harfbuzz/harfbuzzjs. It is not a fork; it lives in the harfbuzz GitHub organization and is published by two HarfBuzz core developers (Ebrahim Byagowi and Khaled Hosny). HarfBuzz is the shaping engine inside Chrome, Firefox, Android and LibreOffice, so the export now measures text with the same engine the client's browser uses.

  • License: MIT. No native build step; a 1.3 MB package with a single .wasm file, loaded at import time. Works on Node 18 and later.
  • Maintained: 57 releases on npm since 2019, six of them between July and October 2026; version 1.6.3 (2026-10-06) bundles HarfBuzz 14.6.0.
  • Adoption: about 1.56 million npm downloads per week (opentype.js: about 2.9 million).
  • Caveat: the bundle is built with HB_TINY, which strips some non-shaping APIs (glyph names, for example). Shaping, which is all this change uses, is complete.

Before and after

Filler text, not the reporter's content. Draw font, size M, box width 1355 px, on a blank slide. Client and both exports use the same annotation.

Live client (27 lines):

client-live-whiteboard
client-live-whiteboard

Before: stock export of the same annotation. The periods of items 2 and 3 of section B are wrapped onto lines of their own, the block is two lines taller than in the client, and items 5 and 6 of section D fall below the page edge:

before-export-stock
before-export-stock

After: export with this PR. Same 27 lines as the client; item 6 is the last line and sits inside the page:

after-export-harfbuzz
after-export-harfbuzz

Closes Issue(s)

Follow-up to #24566.

Motivation

Reported on 3.0.39 with a 27-line text annotation: the export wrapped one line's trailing period and one short word onto lines of their own, so the shape grew by two lines and its last items fell below the page edge.

How to test

  1. Add a long text annotation in the draw font, drag its width handle so the longest lines just fit, and add enough lines for the block to reach the bottom of the slide.
  2. Download the presentation with annotations.
  3. Before: the PDF shows extra line breaks (often a lone period) and the bottom lines are missing. After: the PDF has the same line breaks as the client and nothing is cut.

Verified on a 3.0.39 server with the reported annotation at its live geometry: stock export 29 lines with the last two off-page, fixed export 27 lines matching the client. The exportedAnnotationsOverlap Playwright test still covers the overlap symptom. Result: PASS.

… does

Text annotations constrained to a width (autoSize=false) are re-wrapped by
the export using opentype.js glyph advance sums. The client wraps them with
the browser's text layout, which shapes the text with HarfBuzz and applies
the font's OpenType features. Shantell Sans, the "draw" font, carries
kerning in GPOS and contextual alternates in GSUB that opentype.js does not
apply, so the export measured long lines 2-3% wider than they rendered
live. The safety margin added for bigbluebutton#24566 (twice the narrowest glyph) is a
constant, while the error grows with line length: a line the browser laid
out at 1340.8 px measured 1364.1 px in the export, against a 14 px margin.

Reported on 3.0.39 with a 27-line German annotation: the export wrapped one
line's trailing period and the word "Dies" onto lines of their own, so the
shape grew by two lines and its last items fell below the page edge. The
orphaned period came from the tokenizer splitting at word boundaries (\b)
and then breaking the accumulated line at character granularity, which a
browser never does.

Measure with HarfBuzz instead (harfbuzzjs, WebAssembly, no native build).
Shaping with the font's default features reproduces Chrome's widths
exactly: 1340.8 px and 1239.1 px for the report's two critical lines in
both. Wrap where a browser wraps: at whitespace or after a hyphen, breaking
inside a word only when the word alone exceeds the line. Whitespace hangs
at the end of a line instead of forcing a wrap. Keep a tolerance of one
narrowest-glyph width in the "do not wrap" direction only, since that error
mode cannot interleave text. Fonts are parsed once per process and a single
scratch buffer serves every measurement; the previous code re-read and
re-parsed the woff2 font for every shape.

Verified on a 3.0.39 server with the reported annotation at its live
geometry: before the change the exported PDF had 29 lines with the last two
below the page edge; after it the 27 lines match the client and the last
line sits inside the page. The existing regression test
(exportedAnnotationsOverlap) still covers the overlap symptom.

opentype.js is no longer used and is dropped from the dependencies.
@antobinary antobinary added this to the Release 4.0 milestone Oct 8, 2026
@coderabbitai

coderabbitai Bot commented Oct 8, 2026 •

Copy link
Copy Markdown
Contributor

Review in Change Stack →

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration
  • Configuration used: Repository UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 8f45963a-09e0-4cd7-b661-261fea18c9ae
📥 Commits

Reviewing files that changed from the base of the PR and between c2f9fae and ef4cd8a.

📒 Files selected for processing (2)
  • bigbluebutton-tests/playwright/presentation/exportedAnnotationsOverlap.ts
  • bigbluebutton-tests/playwright/presentation/presentation.spec.ts

Included review availability: This review used your included allowance. Your plan provides up to 8 included reviews per hour; 1 remain after this review.


Walkthrough

The annotation exporter replaces opentype.js with harfbuzzjs for font measurement. It loads and caches WOFF2 fonts, measures text using shaped glyph advances, and changes wrapping to break at whitespace runs and hyphens. Oversized tokens are split into fitting prefixes. Empty paragraphs remain empty lines, and emitted lines have trailing whitespace trimmed. Playwright tests check annotation overlap and whether exported text preserves client line breaks.

Priority: ➖ Normal

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

Change: Bug fix

Sequence Diagram(s)

sequenceDiagram
  participant wrapText
  participant fontCache
  participant WOFF2Loader
  participant HarfBuzz
  wrapText->>fontCache: request font by path
  fontCache->>WOFF2Loader: load and decompress WOFF2 font
  WOFF2Loader->>HarfBuzz: create font and record units per em
  wrapText->>HarfBuzz: shape text and measure glyph advances
  HarfBuzz-->>wrapText: return glyph advances
  wrapText-->>wrapText: break text into lines
Loading

Suggested reviewers: imdt-claudiop

Merge Risk: ⚪ Minimal · up to ef4cd

The export line-break regression is covered for the tested annotation. No demonstrated issue remains to block merging after normal checks.

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly and concisely summarizes the main change: matching exported text wrapping to the client.
Description check ✅ Passed The description directly explains the HarfBuzz-based measurement, wrapping changes, dependency replacement, regression coverage, and verification results.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 1 functions across 3 files.
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 unit tests (beta)
  • Create a new PR
  • Autopilot · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

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.

@antobinary

Copy link
Copy Markdown
Member Author

I intend on pushing regression test as well. Promoted from draft to obtain CodeRabbit comments.

Regression coverage for the opentype.js wrap divergence fixed in the
previous commit. A 26-line synthetic German text annotation is typed
through the whiteboard, constrained to 2632 px at scale 0.5 on a blank
slide and exported in its current state. The rows of the exported page
are then compared with the client: every row between the annotation's
first and last line must be one of its 24 non-blank paragraphs, no row
may consist of punctuation alone, and the last row must be its last
line.

Two lines of the text are about 200 characters long on purpose. The
browser measures them at 2618 px and 2623 px, the old export at 2661 px
and 2666 px, so the fixture keeps 9 px of slack on the browser side
even where Chromium rounds glyph advances to whole pixels (this test
tree launches it without --font-render-hinting=none), while the old
export still wraps both lines. Verified red against the stock
bbb-export-annotations (26 rows instead of 24) and green with the fix,
on a 3.0.39 server.

The export step is shared with the overlap test and now opens the
public chat before waiting for the download link: the link arrives as
a chat message and is hidden when the sidebar is collapsed on login.
An unused constant is removed and the file is formatted with the
repository's eslint --fix.
@github-actions

Copy link
Copy Markdown

✅ Automated tests have passed!

@ramonlsouza
ramonlsouza merged commit 0830dff into bigbluebutton:v4.0.x-develop Oct 16, 2026
58 of 60 checks passed
@antobinary
antobinary deleted the fix-export-text-wrap-harfbuzz-40 branch October 16, 2026 16:31
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