Skip to content

Commit 278e542

Browse files
author
Eduardo Menezes Morais
committed
Fix new pages with flexible line height
1 parent cb39b72 commit 278e542

4 files changed

Lines changed: 119 additions & 235 deletions

File tree

dist/jspdf.amd.min.js

Lines changed: 31 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
;(function(){
2-
/** @preserve jsPDF 0.9.0rc2 ( 2013-08-06T13:32 commit ID e3938214f1ad4a4ab0009b7230531d255f20bdc5 )
2+
/** @preserve jsPDF 0.9.0rc2 ( 2013-08-06T14:54 commit ID 7ca766ff8f49a839d86faccebf95a1267bb8afcc )
33
Copyright (c) 2010-2012 James Hall, james@snapshotmedia.co.uk, https://github.com/MrRio/jsPDF
44
Copyright (c) 2012 Willow Systems Corporation, willow-systems.com
55
MIT license.
@@ -3634,6 +3634,7 @@ API.events.push([
36343634
/** ====================================================================
36353635
* jsPDF Cell plugin
36363636
* Copyright (c) 2013 Youssef Beddad, youssef.beddad@gmail.com
3637+
* 2013 Eduardo Menezes de Morais, eduardo.morais@usp.br
36373638
*
36383639
* Permission is hereby granted, free of charge, to any person obtaining
36393640
* a copy of this software and associated documentation files (the
@@ -3661,25 +3662,18 @@ API.events.push([
36613662
/*jslint browser:true */
36623663
/*global document: false, jsPDF */
36633664

3664-
var lnP = 0,
3665-
fontName,
3665+
var fontName,
36663666
fontSize,
36673667
fontStyle,
36683668
padding = 3,
3669+
margin = 10,
36693670
lastCellPos = { x: undefined, y: undefined, w: undefined, h: undefined, ln: undefined },
36703671
pages = 1,
3671-
newPage = false,
36723672
setLastCellPosition = function (x, y, w, h, ln) {
3673-
lastCellPos = { x: x, y: y, w: w, h: h, ln: ln };
3673+
lastCellPos = { 'x': x, 'y': y, 'w': w, 'h': h, 'ln': ln };
36743674
},
36753675
getLastCellPosition = function () {
36763676
return lastCellPos;
3677-
},
3678-
setLnP = function (x) {
3679-
lnP = x;
3680-
},
3681-
getLnP = function (x) {
3682-
return lnP;
36833677
};
36843678

36853679
jsPDFAPI.getTextDimensions = function (txt) {
@@ -3711,69 +3705,46 @@ API.events.push([
37113705
jsPDFAPI.cellAddPage = function () {
37123706
this.addPage();
37133707
setLastCellPosition(undefined, undefined, undefined, undefined, undefined);
3714-
newPage = true;
37153708
pages += 1;
3716-
setLnP(1);
37173709
};
37183710

37193711
jsPDFAPI.cellInitialize = function () {
37203712
lastCellPos = { x: undefined, y: undefined, w: undefined, h: undefined, ln: undefined };
37213713
pages = 1;
3722-
newPage = false;
3723-
setLnP(0);
37243714
};
37253715

37263716
jsPDFAPI.cell = function (x, y, w, h, txt, ln) {
3727-
this.lnMod = this.lnMod === undefined ? 0 : this.lnMod;
3728-
if (this.printingHeaderRow !== true && this.lnMod !== 0) {
3729-
ln = ln + this.lnMod;
3730-
}
3731-
3732-
if ((((ln * h) + y + (h * 2)) / pages) >= this.internal.pageSize.height && pages === 1 && !newPage) {
3733-
this.cellAddPage();
3717+
var curCell = getLastCellPosition();
3718+
3719+
// If this is not the first cell, we must change its position
3720+
if (curCell.ln !== undefined) {
3721+
3722+
if (curCell.ln === ln) {
3723+
//Same line
3724+
x = curCell.x + curCell.w;
3725+
y = curCell.y;
3726+
} else {
3727+
//New line
3728+
if ((curCell.y + curCell.h + h + margin) >= this.internal.pageSize.height) {
3729+
this.cellAddPage();
37343730

3735-
if (this.printHeaders && this.tableHeaderRow) {
3736-
this.printHeaderRow(ln);
3737-
this.lnMod += 1;
3738-
ln += 1;
3739-
}
3740-
} else if (newPage && getLastCellPosition().ln !== ln && getLnP() === getMaxLn()) {
3741-
this.cellAddPage();
3731+
if (this.printHeaders && this.tableHeaderRow) {
3732+
this.printHeaderRow(ln);
3733+
}
3734+
}
3735+
//We ignore the passed y: the lines may have diferent heights
3736+
y = (getLastCellPosition().y + getLastCellPosition().h);
37423737

3743-
if (this.printHeaders && this.tableHeaderRow) {
3744-
this.printHeaderRow(ln);
3745-
this.lnMod += 1;
3746-
ln += 1;
37473738
}
37483739
}
3749-
3750-
var curCell = getLastCellPosition(),
3751-
isNewLn = 1;
3752-
if (curCell.x !== undefined && curCell.ln === ln) {
3753-
x = curCell.x + curCell.w;
3754-
}
3755-
if (curCell.ln !== undefined && curCell.ln === ln) {
3756-
ln = curCell.ln;
3757-
isNewLn = 0;
3758-
}
3759-
if (curCell.y !== undefined) {
3760-
//We ignore the passed y: the lines may have diferent heights
3761-
if (isNewLn === 1)
3762-
y = (curCell.y + curCell.h);
3763-
else
3764-
y = curCell.y;
3765-
}
37663740

3767-
if (newPage) {
3768-
y = h * (getLnP() + isNewLn);
3769-
}
3741+
37703742
if (this.printingHeaderRow) {
37713743
this.rect(x, y, w, h, 'F');
37723744
} else {
37733745
this.rect(x, y, w, h);
37743746
}
37753747
this.text(txt, x + padding, y + this.internal.getLineHeight());
3776-
setLnP(getLnP() + isNewLn);
37773748
setLastCellPosition(x, y, w, h, ln);
37783749
return this;
37793750
};
@@ -3937,7 +3908,7 @@ API.events.push([
39373908
// Construct the header row
39383909
for (i = 0, ln = headerNames.length; i < ln; i += 1) {
39393910
header = headerNames[i];
3940-
tableHeaderConfigs.push([10, 10, columnWidths[header], lineHeight, String(headerPrompts.length ? headerPrompts[i] : header)]);
3911+
tableHeaderConfigs.push([margin, margin, columnWidths[header], lineHeight, String(headerPrompts.length ? headerPrompts[i] : header)]);
39413912
}
39423913

39433914
// Store the table header config
@@ -3955,18 +3926,18 @@ API.events.push([
39553926

39563927
for (j = 0, jln = headerNames.length; j < jln; j += 1) {
39573928
header = headerNames[j];
3958-
this.cell(10, 10, columnWidths[header], lineHeight, model[header], i + 2);
3929+
this.cell(margin, margin, columnWidths[header], lineHeight, model[header], i + 2);
39593930
}
39603931
}
39613932

39623933
return this;
39633934
};
39643935

39653936
/**
3966-
* Store the config for outputting a table header
3967-
* @param {Object[]} config
3968-
* An array of cell configs that would define a header row: Each config matches the config used by jsPDFAPI.cell
3969-
* except the ln parameter is excluded
3937+
* Calculate the height for containing the highest column
3938+
* @param {String[]} headerNames is the header, used as keys to the data
3939+
* @param {Integer[]} columnWidths is size of each column
3940+
* @param {Object[]} model is the line of data we want to calculate the height of
39703941
*/
39713942
jsPDFAPI.calculateLineHeight = function (headerNames, columnWidths, model) {
39723943
var header, lineHeight = 0;

dist/jspdf.min.js

Lines changed: 31 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/** @preserve jsPDF 0.9.0rc2 ( 2013-08-06T13:32 commit ID e3938214f1ad4a4ab0009b7230531d255f20bdc5 )
1+
/** @preserve jsPDF 0.9.0rc2 ( 2013-08-06T14:54 commit ID 7ca766ff8f49a839d86faccebf95a1267bb8afcc )
22
Copyright (c) 2010-2012 James Hall, james@snapshotmedia.co.uk, https://github.com/MrRio/jsPDF
33
Copyright (c) 2012 Willow Systems Corporation, willow-systems.com
44
MIT license.
@@ -3633,6 +3633,7 @@ API.events.push([
36333633
/** ====================================================================
36343634
* jsPDF Cell plugin
36353635
* Copyright (c) 2013 Youssef Beddad, youssef.beddad@gmail.com
3636+
* 2013 Eduardo Menezes de Morais, eduardo.morais@usp.br
36363637
*
36373638
* Permission is hereby granted, free of charge, to any person obtaining
36383639
* a copy of this software and associated documentation files (the
@@ -3660,25 +3661,18 @@ API.events.push([
36603661
/*jslint browser:true */
36613662
/*global document: false, jsPDF */
36623663

3663-
var lnP = 0,
3664-
fontName,
3664+
var fontName,
36653665
fontSize,
36663666
fontStyle,
36673667
padding = 3,
3668+
margin = 10,
36683669
lastCellPos = { x: undefined, y: undefined, w: undefined, h: undefined, ln: undefined },
36693670
pages = 1,
3670-
newPage = false,
36713671
setLastCellPosition = function (x, y, w, h, ln) {
3672-
lastCellPos = { x: x, y: y, w: w, h: h, ln: ln };
3672+
lastCellPos = { 'x': x, 'y': y, 'w': w, 'h': h, 'ln': ln };
36733673
},
36743674
getLastCellPosition = function () {
36753675
return lastCellPos;
3676-
},
3677-
setLnP = function (x) {
3678-
lnP = x;
3679-
},
3680-
getLnP = function (x) {
3681-
return lnP;
36823676
};
36833677

36843678
jsPDFAPI.getTextDimensions = function (txt) {
@@ -3710,69 +3704,46 @@ API.events.push([
37103704
jsPDFAPI.cellAddPage = function () {
37113705
this.addPage();
37123706
setLastCellPosition(undefined, undefined, undefined, undefined, undefined);
3713-
newPage = true;
37143707
pages += 1;
3715-
setLnP(1);
37163708
};
37173709

37183710
jsPDFAPI.cellInitialize = function () {
37193711
lastCellPos = { x: undefined, y: undefined, w: undefined, h: undefined, ln: undefined };
37203712
pages = 1;
3721-
newPage = false;
3722-
setLnP(0);
37233713
};
37243714

37253715
jsPDFAPI.cell = function (x, y, w, h, txt, ln) {
3726-
this.lnMod = this.lnMod === undefined ? 0 : this.lnMod;
3727-
if (this.printingHeaderRow !== true && this.lnMod !== 0) {
3728-
ln = ln + this.lnMod;
3729-
}
3730-
3731-
if ((((ln * h) + y + (h * 2)) / pages) >= this.internal.pageSize.height && pages === 1 && !newPage) {
3732-
this.cellAddPage();
3716+
var curCell = getLastCellPosition();
3717+
3718+
// If this is not the first cell, we must change its position
3719+
if (curCell.ln !== undefined) {
3720+
3721+
if (curCell.ln === ln) {
3722+
//Same line
3723+
x = curCell.x + curCell.w;
3724+
y = curCell.y;
3725+
} else {
3726+
//New line
3727+
if ((curCell.y + curCell.h + h + margin) >= this.internal.pageSize.height) {
3728+
this.cellAddPage();
37333729

3734-
if (this.printHeaders && this.tableHeaderRow) {
3735-
this.printHeaderRow(ln);
3736-
this.lnMod += 1;
3737-
ln += 1;
3738-
}
3739-
} else if (newPage && getLastCellPosition().ln !== ln && getLnP() === getMaxLn()) {
3740-
this.cellAddPage();
3730+
if (this.printHeaders && this.tableHeaderRow) {
3731+
this.printHeaderRow(ln);
3732+
}
3733+
}
3734+
//We ignore the passed y: the lines may have diferent heights
3735+
y = (getLastCellPosition().y + getLastCellPosition().h);
37413736

3742-
if (this.printHeaders && this.tableHeaderRow) {
3743-
this.printHeaderRow(ln);
3744-
this.lnMod += 1;
3745-
ln += 1;
37463737
}
37473738
}
3748-
3749-
var curCell = getLastCellPosition(),
3750-
isNewLn = 1;
3751-
if (curCell.x !== undefined && curCell.ln === ln) {
3752-
x = curCell.x + curCell.w;
3753-
}
3754-
if (curCell.ln !== undefined && curCell.ln === ln) {
3755-
ln = curCell.ln;
3756-
isNewLn = 0;
3757-
}
3758-
if (curCell.y !== undefined) {
3759-
//We ignore the passed y: the lines may have diferent heights
3760-
if (isNewLn === 1)
3761-
y = (curCell.y + curCell.h);
3762-
else
3763-
y = curCell.y;
3764-
}
37653739

3766-
if (newPage) {
3767-
y = h * (getLnP() + isNewLn);
3768-
}
3740+
37693741
if (this.printingHeaderRow) {
37703742
this.rect(x, y, w, h, 'F');
37713743
} else {
37723744
this.rect(x, y, w, h);
37733745
}
37743746
this.text(txt, x + padding, y + this.internal.getLineHeight());
3775-
setLnP(getLnP() + isNewLn);
37763747
setLastCellPosition(x, y, w, h, ln);
37773748
return this;
37783749
};
@@ -3936,7 +3907,7 @@ API.events.push([
39363907
// Construct the header row
39373908
for (i = 0, ln = headerNames.length; i < ln; i += 1) {
39383909
header = headerNames[i];
3939-
tableHeaderConfigs.push([10, 10, columnWidths[header], lineHeight, String(headerPrompts.length ? headerPrompts[i] : header)]);
3910+
tableHeaderConfigs.push([margin, margin, columnWidths[header], lineHeight, String(headerPrompts.length ? headerPrompts[i] : header)]);
39403911
}
39413912

39423913
// Store the table header config
@@ -3954,18 +3925,18 @@ API.events.push([
39543925

39553926
for (j = 0, jln = headerNames.length; j < jln; j += 1) {
39563927
header = headerNames[j];
3957-
this.cell(10, 10, columnWidths[header], lineHeight, model[header], i + 2);
3928+
this.cell(margin, margin, columnWidths[header], lineHeight, model[header], i + 2);
39583929
}
39593930
}
39603931

39613932
return this;
39623933
};
39633934

39643935
/**
3965-
* Store the config for outputting a table header
3966-
* @param {Object[]} config
3967-
* An array of cell configs that would define a header row: Each config matches the config used by jsPDFAPI.cell
3968-
* except the ln parameter is excluded
3936+
* Calculate the height for containing the highest column
3937+
* @param {String[]} headerNames is the header, used as keys to the data
3938+
* @param {Integer[]} columnWidths is size of each column
3939+
* @param {Object[]} model is the line of data we want to calculate the height of
39693940
*/
39703941
jsPDFAPI.calculateLineHeight = function (headerNames, columnWidths, model) {
39713942
var header, lineHeight = 0;

0 commit comments

Comments
 (0)