andreasgal/pdf.js — pull request #738 by arturadib (2011-10-31 21:15:32+00:00)
Title: Text selection

Presently works OK (not perfect) with TraceMonkey, ECMA262, and a few others.

Open `viewer.html` as usual to test it. Note that there's now a `

` on top of our ``.

To see what's happening "under the hood", change from `color:transparent` to `color:blue` in `viewer.css: .textLayer > div`. (Warning: it's not pretty, it's hacky :)).

It's hacky because we can't simply add one (absolutely-positioned) `

` or `` per word to the DOM as that would be prohibitively slow and memory-consuming. So we need some heuristics to render one `div` per `showSpacedText` line, and to make that DOM width match the canvas text width (I've borrowed some ideas from Crocodoc).

Another hack is that because of this width matching, we need to call `.offsetWidth` for every `

` we're inserting - which is _slow_ and _locks the UI_. So I'm rendering the text layer lazily using a combination of `setTimeout` and `clearTimeout`, so that we only render things when we have stopped loading pages for a while (currently 500 ms).

Known issues:

+ Apparently we're selecting glyphs and not characters, so sometimes things like `fi` are selected as a single character
+ Some characters come out totally messed up (`pdkids.pdf`)
+ Some PDFs are not working at all (`i9.pdf`)

Help and feedback always welcome :)

@vingtetun @brendandahl @notmasteryet @jviereck