2014-12-01 05:42:47 -05:00
|
|
|
define [
|
|
|
|
"base"
|
|
|
|
], (App) ->
|
|
|
|
# App = angular.module 'PDFRenderer', ['pdfAnnotations', 'pdfTextLayer']
|
|
|
|
|
2014-12-08 09:46:59 -05:00
|
|
|
App.factory 'PDFRenderer', ['$q', '$timeout', 'pdfAnnotations', 'pdfTextLayer', 'pdfSpinner', ($q, $timeout, pdfAnnotations, pdfTextLayer, pdfSpinner) ->
|
2014-12-01 05:42:47 -05:00
|
|
|
|
|
|
|
class PDFRenderer
|
2014-12-05 07:14:40 -05:00
|
|
|
JOB_QUEUE_INTERVAL: 25
|
2015-01-13 11:38:40 -05:00
|
|
|
PAGE_LOAD_TIMEOUT: 60*1000
|
|
|
|
PAGE_RENDER_TIMEOUT: 60*1000
|
2014-12-01 05:42:47 -05:00
|
|
|
|
|
|
|
constructor: (@url, @options) ->
|
2015-01-13 11:39:49 -05:00
|
|
|
# PDFJS.disableFontFace = true # avoids repaints, uses worker more
|
2014-12-09 08:41:49 -05:00
|
|
|
# PDFJS.disableAutoFetch = true # enable this to prevent loading whole file
|
|
|
|
# PDFJS.disableStream
|
|
|
|
# PDFJS.disableRange
|
2014-12-01 05:42:47 -05:00
|
|
|
@scale = @options.scale || 1
|
2014-12-09 08:46:44 -05:00
|
|
|
@pdfjs = PDFJS.getDocument @url, null, null, @options.progressCallback
|
|
|
|
@document = $q.when(@pdfjs)
|
2014-12-01 05:42:47 -05:00
|
|
|
@navigateFn = @options.navigateFn
|
2014-12-08 09:46:59 -05:00
|
|
|
@spinner = new pdfSpinner
|
2014-12-01 05:42:47 -05:00
|
|
|
@resetState()
|
2015-01-13 11:28:24 -05:00
|
|
|
@document.then (pdfDocument) =>
|
|
|
|
pdfDocument.getDownloadInfo().then () =>
|
|
|
|
@options.loadedCallback()
|
2015-01-12 11:45:24 -05:00
|
|
|
@errorCallback = @options.errorCallback
|
2015-01-20 09:28:14 -05:00
|
|
|
@pageSizeChangeCallback = @options.pageSizeChangeCallback
|
2015-01-12 11:45:24 -05:00
|
|
|
@pdfjs.catch (exception) =>
|
|
|
|
# console.log 'ERROR in get document', exception
|
|
|
|
@errorCallback(exception)
|
2014-12-01 05:42:47 -05:00
|
|
|
|
|
|
|
resetState: () ->
|
|
|
|
@complete = []
|
|
|
|
@timeout = []
|
2014-12-09 08:46:44 -05:00
|
|
|
@pageLoad = []
|
2014-12-01 05:42:47 -05:00
|
|
|
@renderTask = []
|
|
|
|
@renderQueue = []
|
|
|
|
@jobs = 0
|
|
|
|
|
|
|
|
getNumPages: () ->
|
|
|
|
@document.then (pdfDocument) ->
|
|
|
|
pdfDocument.numPages
|
|
|
|
|
|
|
|
getPage: (pageNum) ->
|
2015-01-12 11:45:24 -05:00
|
|
|
@document.then (pdfDocument) ->
|
|
|
|
# console.log 'got pdf document, now getting Page', pageNum
|
2014-12-01 05:42:47 -05:00
|
|
|
pdfDocument.getPage(pageNum)
|
|
|
|
|
|
|
|
getPdfViewport: (pageNum, scale) ->
|
|
|
|
scale ?= @scale
|
2015-01-26 08:05:21 -05:00
|
|
|
@document.then (pdfDocument) =>
|
2014-12-01 05:42:47 -05:00
|
|
|
pdfDocument.getPage(pageNum).then (page) ->
|
|
|
|
viewport = page.getViewport scale
|
2015-01-26 08:05:21 -05:00
|
|
|
, (error) =>
|
|
|
|
@errorCallback?(error)
|
2014-12-01 05:42:47 -05:00
|
|
|
|
|
|
|
getDestinations: () ->
|
|
|
|
@document.then (pdfDocument) ->
|
|
|
|
pdfDocument.getDestinations()
|
|
|
|
|
2014-12-09 08:43:25 -05:00
|
|
|
getDestination: (dest) ->
|
2015-03-31 09:54:36 -04:00
|
|
|
@document.then (pdfDocument) ->
|
|
|
|
pdfDocument.getDestination(dest)
|
2015-01-26 08:05:21 -05:00
|
|
|
, (error) =>
|
|
|
|
@errorCallback?(error)
|
2014-12-09 08:43:25 -05:00
|
|
|
|
2014-12-01 05:42:47 -05:00
|
|
|
getPageIndex: (ref) ->
|
2015-01-26 08:05:21 -05:00
|
|
|
@document.then (pdfDocument) =>
|
2014-12-01 05:42:47 -05:00
|
|
|
pdfDocument.getPageIndex(ref).then (idx) ->
|
|
|
|
idx
|
2015-01-26 08:05:21 -05:00
|
|
|
, (error) =>
|
|
|
|
@errorCallback?(error)
|
2014-12-01 05:42:47 -05:00
|
|
|
|
|
|
|
getScale: () ->
|
|
|
|
@scale
|
|
|
|
|
|
|
|
setScale: (@scale) ->
|
|
|
|
@resetState()
|
|
|
|
|
2014-12-05 07:14:40 -05:00
|
|
|
triggerRenderQueue: (interval = @JOB_QUEUE_INTERVAL) ->
|
2015-01-26 07:13:56 -05:00
|
|
|
@queueTimer = setTimeout () =>
|
|
|
|
@queueTimer = null
|
2014-12-01 05:42:47 -05:00
|
|
|
@processRenderQueue()
|
2014-12-05 07:14:40 -05:00
|
|
|
, interval
|
2014-12-01 05:42:47 -05:00
|
|
|
|
2014-12-09 08:46:44 -05:00
|
|
|
removeCompletedJob: (taskRef, pagenum) ->
|
2014-12-01 05:42:47 -05:00
|
|
|
# may need to clean up deferred object here
|
2014-12-09 08:46:44 -05:00
|
|
|
delete taskRef[pagenum]
|
2014-12-01 05:42:47 -05:00
|
|
|
@jobs = @jobs - 1
|
2014-12-05 07:14:40 -05:00
|
|
|
@triggerRenderQueue(0)
|
2014-12-01 05:42:47 -05:00
|
|
|
|
2015-01-19 11:55:44 -05:00
|
|
|
renderPages: (pages) ->
|
|
|
|
return if @shuttingDown
|
|
|
|
@renderQueue = for page in pages
|
|
|
|
{
|
|
|
|
'element': page.elementChildren
|
|
|
|
'pagenum': page.pageNum
|
|
|
|
}
|
|
|
|
@triggerRenderQueue()
|
|
|
|
|
2015-01-20 16:33:49 -05:00
|
|
|
renderPage: (page) ->
|
|
|
|
return if @shuttingDown
|
|
|
|
current = {
|
|
|
|
'element': page.elementChildren
|
|
|
|
'pagenum': page.pageNum
|
|
|
|
}
|
|
|
|
@renderQueue.push current
|
|
|
|
@processRenderQueue()
|
|
|
|
|
2014-12-01 05:42:47 -05:00
|
|
|
processRenderQueue: () ->
|
2015-01-12 11:45:24 -05:00
|
|
|
return if @shuttingDown
|
2014-12-01 05:42:47 -05:00
|
|
|
return if @jobs > 0
|
2014-12-04 11:52:11 -05:00
|
|
|
current = @renderQueue.shift()
|
2014-12-01 05:42:47 -05:00
|
|
|
return unless current?
|
|
|
|
[element, pagenum] = [current.element, current.pagenum]
|
2014-12-04 11:52:11 -05:00
|
|
|
# if task is underway or complete, go to the next entry in the
|
|
|
|
# render queue
|
2014-12-09 08:46:44 -05:00
|
|
|
# console.log 'processing renderq', pagenum, @renderTask[pagenum], @complete[pagenum]
|
|
|
|
if @pageLoad[pagenum] or @renderTask[pagenum] or @complete[pagenum]
|
2014-12-04 11:52:11 -05:00
|
|
|
@processRenderQueue()
|
|
|
|
return
|
2014-12-01 05:42:47 -05:00
|
|
|
@jobs = @jobs + 1
|
|
|
|
|
2014-12-08 09:46:59 -05:00
|
|
|
element.canvas.addClass('pdfng-loading')
|
2015-01-26 07:13:56 -05:00
|
|
|
spinTimer = setTimeout () =>
|
2015-01-19 09:24:52 -05:00
|
|
|
@spinner.add(element.canvas)
|
|
|
|
, 100
|
2014-12-01 05:42:47 -05:00
|
|
|
|
2014-12-09 08:46:44 -05:00
|
|
|
completeRef = @complete
|
|
|
|
renderTaskRef = @renderTask
|
2015-01-12 11:45:24 -05:00
|
|
|
# console.log 'started page load', pagenum
|
|
|
|
|
|
|
|
timedOut = false
|
|
|
|
timer = $timeout () =>
|
2015-02-09 06:18:46 -05:00
|
|
|
Raven?.captureMessage?('pdfng page load timed out after ' + @PAGE_LOAD_TIMEOUT + 'ms (1% sample)') if Math.random() < 0.01
|
2015-01-12 11:45:24 -05:00
|
|
|
# console.log 'page load timed out', pagenum
|
|
|
|
timedOut = true
|
2015-01-26 07:13:56 -05:00
|
|
|
clearTimeout(spinTimer)
|
2015-01-12 11:45:24 -05:00
|
|
|
@spinner.stop(element.canvas)
|
|
|
|
# @jobs = @jobs - 1
|
|
|
|
# @triggerRenderQueue(0)
|
2015-01-26 08:05:21 -05:00
|
|
|
@errorCallback?('timeout')
|
2015-01-12 12:02:51 -05:00
|
|
|
, @PAGE_LOAD_TIMEOUT
|
2014-12-01 05:42:47 -05:00
|
|
|
|
2014-12-09 08:46:44 -05:00
|
|
|
@pageLoad[pagenum] = @getPage(pagenum)
|
2015-01-12 11:45:24 -05:00
|
|
|
|
2014-12-09 08:46:44 -05:00
|
|
|
@pageLoad[pagenum].then (pageObject) =>
|
2015-01-12 11:45:24 -05:00
|
|
|
# console.log 'in page load success', pagenum
|
|
|
|
$timeout.cancel(timer)
|
2015-01-26 07:13:56 -05:00
|
|
|
clearTimeout(spinTimer)
|
2014-12-09 08:46:44 -05:00
|
|
|
@renderTask[pagenum] = @doRender element, pagenum, pageObject
|
|
|
|
@renderTask[pagenum].then () =>
|
|
|
|
# complete
|
2015-01-12 11:45:24 -05:00
|
|
|
# console.log 'render task success', pagenum
|
2014-12-09 08:46:44 -05:00
|
|
|
completeRef[pagenum] = true
|
|
|
|
@removeCompletedJob renderTaskRef, pagenum
|
|
|
|
, () =>
|
2015-01-12 11:45:24 -05:00
|
|
|
# console.log 'render task failed', pagenum
|
2014-12-09 08:46:44 -05:00
|
|
|
# rejected
|
|
|
|
@removeCompletedJob renderTaskRef, pagenum
|
2015-01-12 11:45:24 -05:00
|
|
|
.catch (error) ->
|
|
|
|
# console.log 'in page load error', pagenum, 'timedOut=', timedOut
|
|
|
|
$timeout.cancel(timer)
|
2015-01-26 07:13:56 -05:00
|
|
|
clearTimeout(spinTimer)
|
2015-01-12 11:45:24 -05:00
|
|
|
# console.log 'ERROR', error
|
2014-12-01 05:42:47 -05:00
|
|
|
|
|
|
|
doRender: (element, pagenum, page) ->
|
|
|
|
self = this
|
|
|
|
scale = @scale
|
|
|
|
|
|
|
|
if (not scale?)
|
2014-12-02 06:42:57 -05:00
|
|
|
# console.log 'scale is undefined, returning'
|
2014-12-01 05:42:47 -05:00
|
|
|
return
|
|
|
|
|
2014-12-08 09:46:59 -05:00
|
|
|
canvas = $('<canvas class="pdf-canvas pdfng-rendering"></canvas>')
|
2014-12-01 05:42:47 -05:00
|
|
|
|
|
|
|
viewport = page.getViewport (scale)
|
|
|
|
|
|
|
|
devicePixelRatio = window.devicePixelRatio || 1
|
|
|
|
|
|
|
|
ctx = canvas[0].getContext '2d'
|
|
|
|
backingStoreRatio = ctx.webkitBackingStorePixelRatio ||
|
|
|
|
ctx.mozBackingStorePixelRatio ||
|
|
|
|
ctx.msBackingStorePixelRatio ||
|
|
|
|
ctx.oBackingStorePixelRatio ||
|
|
|
|
ctx.backingStorePixelRatio || 1
|
|
|
|
pixelRatio = devicePixelRatio / backingStoreRatio
|
|
|
|
|
|
|
|
scaledWidth = (Math.floor(viewport.width) * pixelRatio) | 0
|
|
|
|
scaledHeight = (Math.floor(viewport.height) * pixelRatio) | 0
|
|
|
|
|
|
|
|
newWidth = Math.floor(viewport.width)
|
|
|
|
newHeight = Math.floor(viewport.height)
|
|
|
|
|
|
|
|
canvas[0].height = scaledHeight
|
|
|
|
canvas[0].width = scaledWidth
|
|
|
|
|
|
|
|
canvas.height(newHeight + 'px')
|
|
|
|
canvas.width(newWidth + 'px')
|
|
|
|
|
2015-01-20 09:28:14 -05:00
|
|
|
oldHeight = element.canvas.height()
|
|
|
|
oldWidth = element.canvas.width()
|
|
|
|
if newHeight != oldHeight or newWidth != oldWidth
|
|
|
|
element.canvas.height(newHeight + 'px')
|
|
|
|
element.canvas.width(newWidth + 'px')
|
|
|
|
element.container.height(newHeight + 'px')
|
|
|
|
element.container.width(newWidth + 'px')
|
|
|
|
@pageSizeChangeCallback?(pagenum, newHeight - oldHeight)
|
2014-12-01 05:42:47 -05:00
|
|
|
|
|
|
|
if pixelRatio != 1
|
|
|
|
ctx.scale(pixelRatio, pixelRatio)
|
|
|
|
|
|
|
|
textLayer = new pdfTextLayer({
|
|
|
|
textLayerDiv: element.text[0]
|
|
|
|
viewport: viewport
|
|
|
|
})
|
|
|
|
|
|
|
|
annotationsLayer = new pdfAnnotations({
|
|
|
|
annotations: element.annotations[0]
|
|
|
|
viewport: viewport
|
|
|
|
navigateFn: @navigateFn
|
|
|
|
})
|
2014-12-02 06:03:07 -05:00
|
|
|
|
|
|
|
element.canvas.replaceWith(canvas)
|
2014-12-01 05:42:47 -05:00
|
|
|
|
2015-01-12 11:45:24 -05:00
|
|
|
# console.log 'staring page render', pagenum
|
|
|
|
|
2014-12-09 08:46:44 -05:00
|
|
|
result = page.render {
|
2014-12-01 05:42:47 -05:00
|
|
|
canvasContext: ctx
|
|
|
|
viewport: viewport
|
|
|
|
}
|
2014-12-09 08:46:44 -05:00
|
|
|
|
2015-01-12 11:45:24 -05:00
|
|
|
timedOut = false
|
|
|
|
|
2015-01-12 12:02:51 -05:00
|
|
|
timer = $timeout () =>
|
2015-02-09 06:18:46 -05:00
|
|
|
Raven?.captureMessage?('pdfng page render timed out after ' + @PAGE_RENDER_TIMEOUT + 'ms (1% sample)') if Math.random() < 0.01
|
2015-01-12 11:45:24 -05:00
|
|
|
# console.log 'page render timed out', pagenum
|
|
|
|
timedOut = true
|
|
|
|
result.cancel()
|
2015-01-12 12:02:51 -05:00
|
|
|
, @PAGE_RENDER_TIMEOUT
|
2015-01-12 11:45:24 -05:00
|
|
|
|
2014-12-09 08:46:44 -05:00
|
|
|
result.then () ->
|
2015-01-12 11:45:24 -05:00
|
|
|
# console.log 'page rendered', pagenum
|
|
|
|
$timeout.cancel(timer)
|
2014-12-08 09:46:59 -05:00
|
|
|
canvas.removeClass('pdfng-rendering')
|
2014-12-02 06:03:07 -05:00
|
|
|
page.getTextContent().then (textContent) ->
|
|
|
|
textLayer.setTextContent textContent
|
2015-01-12 11:45:24 -05:00
|
|
|
, (error) ->
|
2015-01-26 08:05:21 -05:00
|
|
|
self.errorCallback?(error)
|
2014-12-02 06:03:07 -05:00
|
|
|
page.getAnnotations().then (annotations) ->
|
|
|
|
annotationsLayer.setAnnotations annotations
|
2015-01-12 11:45:24 -05:00
|
|
|
, (error) ->
|
2015-01-26 08:05:21 -05:00
|
|
|
self.errorCallback?(error)
|
2015-01-12 11:45:24 -05:00
|
|
|
.catch (error) ->
|
|
|
|
# console.log 'page render failed', pagenum, error
|
|
|
|
$timeout.cancel(timer)
|
|
|
|
if timedOut
|
|
|
|
# console.log 'calling ERROR callback - was timeout'
|
|
|
|
self.errorCallback?('timeout')
|
|
|
|
else if error != 'cancelled'
|
|
|
|
# console.log 'calling ERROR callback'
|
|
|
|
self.errorCallback?(error)
|
2014-12-01 05:42:47 -05:00
|
|
|
|
2014-12-09 08:46:44 -05:00
|
|
|
return result
|
|
|
|
|
|
|
|
destroy: () ->
|
2014-12-09 09:39:58 -05:00
|
|
|
# console.log 'in pdf renderer destroy', @renderQueue
|
2014-12-09 08:46:44 -05:00
|
|
|
@shuttingDown = true
|
2015-01-26 07:13:56 -05:00
|
|
|
clearTimeout @queueTimer if @queueTimer?
|
2014-12-09 08:46:44 -05:00
|
|
|
@renderQueue = []
|
|
|
|
for task in @renderTask
|
|
|
|
task.cancel() if task?
|
|
|
|
@pdfjs.then (document) ->
|
|
|
|
document.cleanup()
|
|
|
|
document.destroy()
|
|
|
|
|
2014-12-01 05:42:47 -05:00
|
|
|
]
|