Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Hyphenation and Justification #44

Open
wants to merge 13 commits into
base: master
Choose a base branch
from

Conversation

AprilArcus
Copy link

This pull request rewrites measureText.js to support hyphenation via Bram Stein's hypher.js library, and changes the output format of the measureText() function to include fragment widths, which drawText() in CanvasUtils.js can use to set full-justified text blocks.

The original version contains an O(n2) while(words.length) { words.shift() } construction (measureText.js line 86). I believe this version has O(n) time complexity, but I still need to profile this version against the old one.

I've kept an eye toward including Knuth & Plass, high-quality O(n2) linebreaking in the future.

@AprilArcus
Copy link
Author

I did some coarse benchmarks of the measureText() function (bracketing it in calls to console.time() and console.timeEnd()) in a 1300x570 px viewport filled with the first 10k words of Moby Dick rendered at a font size of 6px. Times are average of five, as measured on a Sandy Bridge MacBook Air:

initial render reflow during horizontal resize
current code 223.0ms 243.3ms
pull request (hyphens: none) 115.9ms 16.9ms
pull request (hyphens: auto) 194.6ms 15.5ms

Lower reflow times represent gains from memoizing calls to Canvas.measureText() with high granularity. Overall frame rate does not improve much, since drawText() now spends much more time drawing text than measuring it. I wonder if caching rendered text as bitmaps might be a good direction to look into for further perf gains? As a point of reference, an OS X HTML5 <textarea> element reflows at 60fps in the same conditions, which was non-trivial in its time.

@tuxracer
Copy link
Contributor

tuxracer commented Jan 8, 2016

This breaks the timeline example

@AprilArcus
Copy link
Author

Thanks for bringing that to my attention; I've revised the careless re-ordering of parameters that lead to this problem.

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