Commit e6ce8bd4 authored by Mathieu Giraud's avatar Mathieu Giraud

js/app.js, js/lib: remove jspdf

Not used for several months
parent 48bd0681
......@@ -11,11 +11,6 @@ require(["jquery",
"d3.v3",
"jquery.form",
"StackBlur",
"jspdf",
"jspdf.plugin.addimage",
"jspdf.plugin.svgToPdf",
"jspdf.plugin.split_text_to_size",
"jspdf.plugin.standard_fonts_metrics",
"underscore",
"rgbcolor",
"file",
......
This diff is collapsed.
/** @preserve
jsPDF addImage plugin (JPEG only at this time)
Copyright (c) 2012 https://github.com/siefkenj/
*/
/**
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
* ====================================================================
*/
;(function(jsPDFAPI) {
'use strict'
var namespace = 'addImage_'
// takes a string imgData containing the raw bytes of
// a jpeg image and returns [width, height]
// Algorithm from: http://www.64lines.com/jpeg-width-height
var getJpegSize = function(imgData) {
'use strict'
var width, height;
// Verify we have a valid jpeg header 0xff,0xd8,0xff,0xe0,?,?,'J','F','I','F',0x00
if (!imgData.charCodeAt(0) === 0xff ||
!imgData.charCodeAt(1) === 0xd8 ||
!imgData.charCodeAt(2) === 0xff ||
!imgData.charCodeAt(3) === 0xe0 ||
!imgData.charCodeAt(6) === 'J'.charCodeAt(0) ||
!imgData.charCodeAt(7) === 'F'.charCodeAt(0) ||
!imgData.charCodeAt(8) === 'I'.charCodeAt(0) ||
!imgData.charCodeAt(9) === 'F'.charCodeAt(0) ||
!imgData.charCodeAt(10) === 0x00) {
throw new Error('getJpegSize requires a binary jpeg file')
}
var blockLength = imgData.charCodeAt(4)*256 + imgData.charCodeAt(5);
var i = 4, len = imgData.length;
while ( i < len ) {
i += blockLength;
if (imgData.charCodeAt(i) !== 0xff) {
throw new Error('getJpegSize could not find the size of the image');
}
if (imgData.charCodeAt(i+1) === 0xc0 || //(SOF) Huffman - Baseline DCT
imgData.charCodeAt(i+1) === 0xc1 || //(SOF) Huffman - Extended sequential DCT
imgData.charCodeAt(i+1) === 0xc2 || // Progressive DCT (SOF2)
imgData.charCodeAt(i+1) === 0xc3 || // Spatial (sequential) lossless (SOF3)
imgData.charCodeAt(i+1) === 0xc4 || // Differential sequential DCT (SOF5)
imgData.charCodeAt(i+1) === 0xc5 || // Differential progressive DCT (SOF6)
imgData.charCodeAt(i+1) === 0xc6 || // Differential spatial (SOF7)
imgData.charCodeAt(i+1) === 0xc7) {
height = imgData.charCodeAt(i+5)*256 + imgData.charCodeAt(i+6);
width = imgData.charCodeAt(i+7)*256 + imgData.charCodeAt(i+8);
return [width, height];
} else {
i += 2;
blockLength = imgData.charCodeAt(i)*256 + imgData.charCodeAt(i+1)
}
}
}
// Image functionality ported from pdf.js
, putImage = function(img) {
var objectNumber = this.internal.newObject()
, out = this.internal.write
, putStream = this.internal.putStream
img['n'] = objectNumber
out('<</Type /XObject')
out('/Subtype /Image')
out('/Width ' + img['w'])
out('/Height ' + img['h'])
if (img['cs'] === 'Indexed') {
out('/ColorSpace [/Indexed /DeviceRGB '
+ (img['pal'].length / 3 - 1) + ' ' + (objectNumber + 1)
+ ' 0 R]');
} else {
out('/ColorSpace /' + img['cs']);
if (img['cs'] === 'DeviceCMYK') {
out('/Decode [1 0 1 0 1 0 1 0]');
}
}
out('/BitsPerComponent ' + img['bpc']);
if ('f' in img) {
out('/Filter /' + img['f']);
}
if ('dp' in img) {
out('/DecodeParms <<' + img['dp'] + '>>');
}
if ('trns' in img && img['trns'].constructor == Array) {
var trns = '';
for ( var i = 0; i < img['trns'].length; i++) {
trns += (img[trns][i] + ' ' + img['trns'][i] + ' ');
out('/Mask [' + trns + ']');
}
}
if ('smask' in img) {
out('/SMask ' + (objectNumber + 1) + ' 0 R');
}
out('/Length ' + img['data'].length + '>>');
putStream(img['data']);
out('endobj');
}
, putResourcesCallback = function() {
var images = this.internal.collections[namespace + 'images']
for ( var i in images ) {
putImage.call(this, images[i])
}
}
, putXObjectsDictCallback = function(){
var images = this.internal.collections[namespace + 'images']
, out = this.internal.write
, image
for (var i in images) {
image = images[i]
out(
'/I' + image['i']
, image['n']
, '0'
, 'R'
)
}
}
jsPDFAPI.addImage = function(imageData, format, x, y, w, h) {
'use strict'
if (typeof imageData === 'object' && imageData.nodeType === 1) {
var canvas = document.createElement('canvas');
canvas.width = imageData.clientWidth;
canvas.height = imageData.clientHeight;
var ctx = canvas.getContext('2d');
if (!ctx) {
throw ('addImage requires canvas to be supported by browser.');
}
ctx.drawImage(imageData, 0, 0, canvas.width, canvas.height);
imageData = canvas.toDataURL('image/jpeg');
format = "JPEG";
}
if (format.toUpperCase() !== 'JPEG') {
throw new Error('addImage currently only supports format \'JPEG\', not \''+format+'\'');
}
var imageIndex
, images = this.internal.collections[namespace + 'images']
, coord = this.internal.getCoordinateString
, vcoord = this.internal.getVerticalCoordinateString;
// Detect if the imageData is raw binary or Data URL
if (imageData.substring(0, 23) === 'data:image/jpeg;base64,') {
imageData = atob(imageData.replace('data:image/jpeg;base64,', ''));
}
if (images){
// this is NOT the first time this method is ran on this instance of jsPDF object.
imageIndex = Object.keys ?
Object.keys(images).length :
(function(o){
var i = 0
for (var e in o){if(o.hasOwnProperty(e)){ i++ }}
return i
})(images)
} else {
// this is the first time this method is ran on this instance of jsPDF object.
imageIndex = 0
this.internal.collections[namespace + 'images'] = images = {}
this.internal.events.subscribe('putResources', putResourcesCallback)
this.internal.events.subscribe('putXobjectDict', putXObjectsDictCallback)
}
var dims = getJpegSize(imageData);
var info = {
w : dims[0],
h : dims[1],
cs : 'DeviceRGB',
bpc : 8,
f : 'DCTDecode',
i : imageIndex,
data : imageData
// n: objectNumber will be added by putImage code
};
images[imageIndex] = info
if (!w && !h) {
w = -96;
h = -96;
}
if (w < 0) {
w = (-1) * info['w'] * 72 / w / this.internal.scaleFactor;
}
if (h < 0) {
h = (-1) * info['h'] * 72 / h / this.internal.scaleFactor;
}
if (w === 0) {
w = h * info['w'] / info['h'];
}
if (h === 0) {
h = w * info['h'] / info['w'];
}
this.internal.write(
'q'
, coord(w)
, '0 0'
, coord(h) // TODO: check if this should be shifted by vcoord
, coord(x)
, vcoord(y + h)
, 'cm /I'+info['i']
, 'Do Q'
)
return this
}
})(jsPDF.API)
/** @preserve
jsPDF split_text_to_size plugin
Copyright (c) 2012 Willow Systems Corporation, willow-systems.com
MIT license.
*/
/**
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
* ====================================================================
*/
;(function(API) {
'use strict'
/**
Returns an array of length matching length of the 'word' string, with each
cell ocupied by the width of the char in that position.
@function
@param word {String}
@param widths {Object}
@param kerning {Object}
@returns {Array}
*/
var getCharWidthsArray = API.getCharWidthsArray = function(text, options){
if (!options) {
options = {}
}
var widths = options.widths ? options.widths : this.internal.getFont().metadata.Unicode.widths
, widthsFractionOf = widths.fof ? widths.fof : 1
, kerning = options.kerning ? options.kerning : this.internal.getFont().metadata.Unicode.kerning
, kerningFractionOf = kerning.fof ? kerning.fof : 1
// console.log("widths, kergnings", widths, kerning)
var i, l
, char_code
, char_width
, prior_char_code = 0 // for kerning
, default_char_width = widths[0] || widthsFractionOf
, output = []
for (i = 0, l = text.length; i < l; i++) {
char_code = text.charCodeAt(i)
output.push(
( widths[char_code] || default_char_width ) / widthsFractionOf +
( kerning[char_code] && kerning[char_code][prior_char_code] || 0 ) / kerningFractionOf
)
prior_char_code = char_code
}
return output
}
var getArraySum = function(array){
var i = array.length
, output = 0
while(i){
;i--;
output += array[i]
}
return output
}
/**
Returns a widths of string in a given font, if the font size is set as 1 point.
In other words, this is "proportional" value. For 1 unit of font size, the length
of the string will be that much.
Multiply by font size to get actual width in *points*
Then divide by 72 to get inches or divide by (72/25.6) to get 'mm' etc.
@public
@function
@param
@returns {Type}
*/
var getStringUnitWidth = API.getStringUnitWidth = function(text, options) {
return getArraySum(getCharWidthsArray.call(this, text, options))
}
/**
returns array of lines
*/
var splitLongWord = function(word, widths_array, firstLineMaxLen, maxLen){
var answer = []
// 1st, chop off the piece that can fit on the hanging line.
var i = 0
, l = word.length
, workingLen = 0
while (i !== l && workingLen + widths_array[i] < firstLineMaxLen){
workingLen += widths_array[i]
;i++;
}
// this is first line.
answer.push(word.slice(0, i))
// 2nd. Split the rest into maxLen pieces.
var startOfLine = i
workingLen = 0
while (i !== l){
if (workingLen + widths_array[i] > maxLen) {
answer.push(word.slice(startOfLine, i))
workingLen = 0
startOfLine = i
}
workingLen += widths_array[i]
;i++;
}
if (startOfLine !== i) {
answer.push(word.slice(startOfLine, i))
}
return answer
}
// Note, all sizing inputs for this function must be in "font measurement units"
// By default, for PDF, it's "point".
var splitParagraphIntoLines = function(text, maxlen, options){
// at this time works only on Western scripts, ones with space char
// separating the words. Feel free to expand.
if (!options) {
options = {}
}
var spaceCharWidth = getCharWidthsArray(' ', options)[0]
var words = text.split(' ')
var line = []
, lines = [line]
, line_length = options.textIndent || 0
, separator_length = 0
, current_word_length = 0
, word
, widths_array
var i, l, tmp
for (i = 0, l = words.length; i < l; i++) {
word = words[i]
widths_array = getCharWidthsArray(word, options)
current_word_length = getArraySum(widths_array)
if (line_length + separator_length + current_word_length > maxlen) {
if (current_word_length > maxlen) {
// this happens when you have space-less long URLs for example.
// we just chop these to size. We do NOT insert hiphens
tmp = splitLongWord(word, widths_array, maxlen - (line_length + separator_length), maxlen)
// first line we add to existing line object
line.push(tmp.shift()) // it's ok to have extra space indicator there
// last line we make into new line object
line = [tmp.pop()]
// lines in the middle we apped to lines object as whole lines
while(tmp.length){
lines.push([tmp.shift()]) // single fragment occupies whole line
}
current_word_length = getArraySum( widths_array.slice(word.length - line[0].length) )
} else {
// just put it on a new line
line = [word]
}
// now we attach new line to lines
lines.push(line)
line_length = current_word_length
separator_length = spaceCharWidth
} else {
line.push(word)
line_length += separator_length + current_word_length
separator_length = spaceCharWidth
}
}
var output = []
for (i = 0, l = lines.length; i < l; i++) {
output.push( lines[i].join(' ') )
}
return output
}
/**
Splits a given string into an array of strings. Uses 'size' value
(in measurement units declared as default for the jsPDF instance)
and the font's "widths" and "Kerning" tables, where availabe, to
determine display length of a given string for a given font.
We use character's 100% of unit size (height) as width when Width
table or other default width is not available.
@public
@function
@param text {String} Unencoded, regular JavaScript (Unicode, UTF-16 / UCS-2) string.
@param size {Number} Nominal number, measured in units default to this instance of jsPDF.
@param options {Object} Optional flags needed for chopper to do the right thing.
@returns {Array} with strings chopped to size.
*/
API.splitTextToSize = function(text, maxlen, options) {
'use strict'
if (!options) {
options = {}
}
var fsize = options.fontSize || this.internal.getFontSize()
, newOptions = (function(options){
var widths = {0:1}
, kerning = {}
if (!options.widths || !options.kerning) {
var f = this.internal.getFont(options.fontName, options.fontStyle)
, encoding = 'Unicode'
// NOT UTF8, NOT UTF16BE/LE, NOT UCS2BE/LE
// Actual JavaScript-native String's 16bit char codes used.
// no multi-byte logic here
if (f.metadata[encoding]) {
return {
widths: f.metadata[encoding].widths || widths
, kerning: f.metadata[encoding].kerning || kerning
}
}
} else {
return {
widths: options.widths
, kerning: options.kerning
}
}
// then use default values
return {
widths: widths
, kerning: kerning
}
}).call(this, options)
// first we split on end-of-line chars
var paragraphs
if (text.match(/[\n\r]/)) {
paragraphs = text.split(/\r\n|\r|\n/g)
} else {
paragraphs = [text]
}
// now we convert size (max length of line) into "font size units"
// at present time, the "font size unit" is always 'point'
// 'proportional' means, "in proportion to font size"
var fontUnit_maxLen = 1.0 * this.internal.scaleFactor * maxlen / fsize
// at this time, fsize is always in "points" regardless of the default measurement unit of the doc.
// this may change in the future?
// until then, proportional_maxlen is likely to be in 'points'
// If first line is to be indented (shorter or longer) than maxLen
// we indicate that by using CSS-style "text-indent" option.
// here it's in font units too (which is likely 'points')
// it can be negative (which makes the first line longer than maxLen)
newOptions.textIndent = options.textIndent ?
options.textIndent * 1.0 * this.internal.scaleFactor / fsize :
0
var i, l
, output = []
for (i = 0, l = paragraphs.length; i < l; i++) {
output = output.concat(
splitParagraphIntoLines(
paragraphs[i]
, fontUnit_maxLen
, newOptions
)
)
}
return output
}
})(jsPDF.API);
This diff is collapsed.
This diff is collapsed.
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