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
|
2014-12-01 05:42:47 -05:00
|
|
|
|
|
|
|
constructor: (@url, @options) ->
|
2014-12-09 08:41:49 -05:00
|
|
|
PDFJS.disableFontFace = true # avoids repaints, uses worker more
|
|
|
|
# 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()
|
|
|
|
|
|
|
|
resetState: () ->
|
|
|
|
@page = []
|
|
|
|
@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) ->
|
|
|
|
# with promise caching
|
|
|
|
return @page[pageNum] if @page[pageNum]?
|
|
|
|
@page[pageNum] = @document.then (pdfDocument) ->
|
|
|
|
pdfDocument.getPage(pageNum)
|
|
|
|
|
|
|
|
getPdfViewport: (pageNum, scale) ->
|
|
|
|
scale ?= @scale
|
|
|
|
@document.then (pdfDocument) ->
|
|
|
|
pdfDocument.getPage(pageNum).then (page) ->
|
|
|
|
viewport = page.getViewport scale
|
|
|
|
|
|
|
|
getDestinations: () ->
|
|
|
|
@document.then (pdfDocument) ->
|
|
|
|
pdfDocument.getDestinations()
|
|
|
|
|
2014-12-09 08:43:25 -05:00
|
|
|
# Not available in pdf.js-1.0.712, in later versions there is a direct
|
|
|
|
# call for this - we should use it as soon as it is available in a
|
|
|
|
# stable version
|
|
|
|
getDestination: (dest) ->
|
|
|
|
@destinations = @document.then (pdfDocument) ->
|
|
|
|
pdfDocument.getDestinations()
|
|
|
|
return @destinations.then (all) ->
|
|
|
|
all[dest]
|
|
|
|
|
|
|
|
@document.then (pdfDocument) ->
|
|
|
|
pdfDocument.getDestination(dest)
|
|
|
|
|
|
|
|
|
2014-12-01 05:42:47 -05:00
|
|
|
getPageIndex: (ref) ->
|
|
|
|
@document.then (pdfDocument) ->
|
|
|
|
pdfDocument.getPageIndex(ref).then (idx) ->
|
|
|
|
idx
|
|
|
|
|
|
|
|
getScale: () ->
|
|
|
|
@scale
|
|
|
|
|
|
|
|
setScale: (@scale) ->
|
|
|
|
@resetState()
|
|
|
|
|
|
|
|
pause: (element, pagenum) ->
|
|
|
|
return if @complete[pagenum]
|
2014-12-09 08:46:44 -05:00
|
|
|
return if @shuttingDown
|
2014-12-01 05:42:47 -05:00
|
|
|
@renderQueue = @renderQueue.filter (q) ->
|
|
|
|
q.pagenum != pagenum
|
2014-12-08 09:46:59 -05:00
|
|
|
@spinner.stop(element.canvas)
|
2014-12-01 05:42:47 -05:00
|
|
|
|
2014-12-05 07:14:40 -05:00
|
|
|
triggerRenderQueue: (interval = @JOB_QUEUE_INTERVAL) ->
|
2014-12-01 05:42:47 -05:00
|
|
|
$timeout () =>
|
|
|
|
@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
|
|
|
|
|
|
|
renderPage: (element, pagenum) ->
|
2014-12-09 08:46:44 -05:00
|
|
|
return if @shuttingDown
|
2014-12-01 05:42:47 -05:00
|
|
|
current = {
|
|
|
|
'element': element
|
|
|
|
'pagenum': pagenum
|
|
|
|
}
|
|
|
|
@renderQueue.push(current)
|
|
|
|
@triggerRenderQueue()
|
|
|
|
|
|
|
|
processRenderQueue: () ->
|
|
|
|
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')
|
|
|
|
@spinner.add(element.canvas)
|
2014-12-01 05:42:47 -05:00
|
|
|
|
2014-12-09 08:46:44 -05:00
|
|
|
completeRef = @complete
|
|
|
|
renderTaskRef = @renderTask
|
2014-12-01 05:42:47 -05:00
|
|
|
|
2014-12-09 08:46:44 -05:00
|
|
|
@pageLoad[pagenum] = @getPage(pagenum)
|
|
|
|
@pageLoad[pagenum].then (pageObject) =>
|
|
|
|
@renderTask[pagenum] = @doRender element, pagenum, pageObject
|
|
|
|
@renderTask[pagenum].then () =>
|
|
|
|
# complete
|
|
|
|
completeRef[pagenum] = true
|
|
|
|
@removeCompletedJob renderTaskRef, pagenum
|
|
|
|
, () =>
|
|
|
|
# rejected
|
|
|
|
@removeCompletedJob renderTaskRef, pagenum
|
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')
|
|
|
|
|
2014-12-02 06:02:09 -05:00
|
|
|
element.canvas.height(newHeight)
|
|
|
|
element.canvas.width(newWidth)
|
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
|
|
|
|
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
|
|
|
|
|
|
|
result.then () ->
|
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
|
|
|
|
page.getAnnotations().then (annotations) ->
|
|
|
|
annotationsLayer.setAnnotations annotations
|
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
|
|
|
|
@renderQueue = []
|
|
|
|
for task in @renderTask
|
|
|
|
task.cancel() if task?
|
|
|
|
@pdfjs.then (document) ->
|
|
|
|
document.cleanup()
|
|
|
|
document.destroy()
|
|
|
|
|
2014-12-01 05:42:47 -05:00
|
|
|
]
|