Skip to content

Commit

Permalink
add the ability to switch fonts while creating pdf documents, setFont()
Browse files Browse the repository at this point in the history
  • Loading branch information
mklabs committed Apr 10, 2011
1 parent e36a7d7 commit 1fa06cf
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 18 deletions.
11 changes: 9 additions & 2 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -69,12 +69,13 @@ <h3>demo -
<span id = "pdfLink"></span>
<br/>
<textarea id = "theCode" class = "box">

/* create the PDF document */

var doc = new pdf();
doc.text(20, 20, 'hello, I am PDF.');
doc.setFont('Courier');
doc.text(20, 30, 'i was created in the browser using javascript.');
doc.setFont('Times-Roman');
doc.text(20, 40, 'i can also be created from node.js');

/* optional - set properties on the document */
Expand All @@ -88,10 +89,15 @@ <h3>demo -
doc.addPage();

doc.setFontSize(22);
doc.setFont('Verdana');
doc.text(20, 20, 'This is a title');

doc.setFontSize(16);
doc.text(20, 30, 'This is some normal sized text underneath.');
doc.text(20, 30, 'You can also override page fonts with text method', 'Times-Roman');
doc.text(20, 40, 'Monaco -This is some normal sized text underneath.', 'Monaco');
doc.text(20, 50, 'Times-Roman - This is some normal sized text underneath.', 'Times-Roman');
doc.text(20, 60, 'Helvetica - This is some normal sized text underneath.', 'Helvetica');
doc.text(20, 70, 'Courier - This is some normal sized text.', 'Courier');

doc.drawLine(100, 100, 100, 120, 1.0, 'dashed');
doc.drawLine(100, 100, 120, 100, 1.2, 'dotted');
Expand Down Expand Up @@ -121,6 +127,7 @@ <h3>demo -
// this seems to always work, except clicking the link destroys my FF instantly
$('#pdfLink').html('<a href = "'+pdfAsDataURI+'">'+fileName+'</a> <span class = "helper">right click and save file as pdf</span');


</textarea>
</form>
<iframe id = "theFrame"></iframe>
Expand Down
67 changes: 51 additions & 16 deletions lib/pdf.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,14 @@ var pdf = exports.pdf = function(){
var pageHeight;
var k; // Scale factor
var unit = 'mm'; // Default to mm for units
var fontNumber; // TODO: This is temp, replace with real font handling
var documentProperties = {};
var fontSize = 16; // Default font size
var pageFontSize = 16;
var font = 'Courier'; // Default font
var pageFont = font;
var fonts = {}; // fonts holder, namely use in putRessource
var fontIndex = 0; // F1, F2, etc. using setFont
var fontsNumber = {}; // object number holder for fonts

// Initilisation
if (unit == 'pt') {
Expand Down Expand Up @@ -112,7 +116,19 @@ var pdf = exports.pdf = function(){
}

var putResources = function() {
putFonts();
var f;
// Deal with fonts, defined in fonts by user (using setFont).
if(fontIndex) {
for( f in fonts ) {
putFonts(f);
}
} else {
// if fontIndex still 0, means that setFont was not used, fallback to default
fonts[font] = 1;
putFonts(font);
}


putImages();

//Resource dictionary
Expand All @@ -124,13 +140,12 @@ var pdf = exports.pdf = function(){
out('endobj');
}

var putFonts = function() {
// TODO: Only supports core font hardcoded to Helvetica
var putFonts = function(font) {
newObject();
fontNumber = objectNumber;
name = 'Helvetica';
fontsNumber[font] = objectNumber;

out('<</Type /Font');
out('/BaseFont /' + name);
out('/BaseFont /' + font);
out('/Subtype /Type1');
out('/Encoding /WinAnsiEncoding');
out('>>');
Expand Down Expand Up @@ -163,11 +178,17 @@ var pdf = exports.pdf = function(){
};

var putResourceDictionary = function() {
var i = 0, index, fx;

out('/ProcSet [/PDF /Text /ImageB /ImageC /ImageI]');
out('/Font <<');

// Do this for each font, the '1' bit is the index of the font
// fontNumber is currently the object number related to 'putFonts'
out('/F1 ' + fontNumber + ' 0 R');
// fontNumber is currently the object number related to 'putFonts'
for( index in fontsNumber ) {
out(fonts[index] + ' ' + fontsNumber[index] + ' 0 R');
}

out('>>');
out('/XObject <<');
putXobjectDict();
Expand Down Expand Up @@ -284,10 +305,10 @@ var pdf = exports.pdf = function(){
// Set line width
out(sprintf('%.2f w', (lineWidth * k)));

// Set font - TODO
// 16 is the font size
pageFontSize = fontSize;
out('BT /F1 ' + parseInt(fontSize) + '.00 Tf ET');
pageFont = font;
out('BT ' + fonts[font] + ' ' + parseInt(fontSize) + '.00 Tf ET');
}

// Add the first page automatically
Expand All @@ -302,13 +323,19 @@ var pdf = exports.pdf = function(){
addPage: function() {
_addPage();
},
text: function(x, y, text) {
// need page height
if(pageFontSize != fontSize) {
out('BT /F1 ' + parseInt(fontSize) + '.00 Tf ET');
text: function(x, y, text, f) {
if(f) {
this.setFont(f);
}

// need either page height or page font
if(pageFontSize !== fontSize || pageFont !== font) {
pageFontSize = fontSize;
pageFont = font;
}
var str = sprintf('BT %.2f %.2f Td (%s) Tj ET', x * k, (pageHeight - y) * k, pdfEscape(text));

var str = sprintf('BT %.2f %.2f Td (%s) Tj ET', x * k, (pageHeight - y) * k, pdfEscape(text))
out('BT ' + fonts[font] + ' ' + parseInt(fontSize, 10) + '.00 Tf ET');
out(str);
},
drawRect: function(x, y, w, h, style) {
Expand Down Expand Up @@ -339,6 +366,14 @@ var pdf = exports.pdf = function(){
},
setFontSize: function(size) {
fontSize = size;
},
setFont: function(f){
if( !(f in fonts) ) {
// if not known font yet, add in fonts array, then used in endDocument
// while putting ressource
fonts[f] = '/F' + (fontIndex++);
}
font = f;
}
}

Expand Down

0 comments on commit 1fa06cf

Please sign in to comment.