Commit ff8e000e authored by Robert Knight's avatar Robert Knight Committed by Nick Stenning

Speed up PDF anchoring (#171)

When anchoring many annotations on a PDF, the text of the same page may
be requested many times.

The text is initially fetched via an async call to
PDFViewerApplication.pdfViewer.getPageTextContent() and the result is
then cached for future use.

Previously the cache was only updated once this expensive call
returned a result. Consequently when anchoring many annotations at once,
there would be a lot of unnecessary cache misses and repeated calls to
PDFViewerApplication.pdfViewer.getPageTextContent()

Fix the problem by storing a promise for the result in the cache when
the text for a given page is requested the first time. Consequently the
text for each page will only be fetched once.
parent 29417489
......@@ -7,6 +7,8 @@ RenderingStates = require('../pdfjs-rendering-states')
{TextPositionAnchor, TextQuoteAnchor} = require('./types')
# Caches for performance
# Map of page index to Promise<string>
pageTextCache = {}
quotePositionCache = {}
......@@ -28,7 +30,7 @@ getPage = (pageIndex) ->
getPageTextContent = (pageIndex) ->
if pageTextCache[pageIndex]?
return Promise.resolve(pageTextCache[pageIndex])
return pageTextCache[pageIndex]
else
joinItems = ({items}) ->
# Skip empty items since PDF-js leaves their text layer divs blank.
......@@ -37,11 +39,11 @@ getPageTextContent = (pageIndex) ->
# See the appendText method of TextLayerBuilder in pdf.js.
nonEmpty = (item.str for item in items when /\S/.test(item.str))
textContent = nonEmpty.join('')
pageTextCache[pageIndex] = textContent
return textContent
return PDFViewerApplication.pdfViewer.getPageTextContent(pageIndex)
pageTextCache[pageIndex] = PDFViewerApplication.pdfViewer.getPageTextContent(pageIndex)
.then(joinItems)
return pageTextCache[pageIndex]
getPageOffset = (pageIndex) ->
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment