Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use pdfkit instead of canvas #496

Merged
merged 2 commits into from
Mar 13, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 3 additions & 14 deletions badge.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,7 @@ var fs = require('fs');
var path = require('path');
var SVGO = require('svgo');
var dot = require('dot');

// Initialize what will be used for automatic text measurement.
var Canvas = require('canvas');
var canvasElement = new Canvas(0, 0); // Width and height are irrelevant.
var canvasContext = canvasElement.getContext('2d');
var CanvasFont = Canvas.Font;
try {
var opensans = new CanvasFont('Verdana',
path.join(__dirname, 'Verdana.ttf'));
canvasContext.addFont(opensans);
} catch(e) {}
canvasContext.font = '11px Verdana, "DejaVu Sans"';
var measureTextWidth = require('./measure-text');

// cache templates.
var templates = {};
Expand Down Expand Up @@ -75,9 +64,9 @@ function makeImage(data, cb) {
data.logoPadding = 0;
}
data.widths = [
(canvasContext.measureText(data.text[0]).width|0) + 10
(measureTextWidth(data.text[0])|0) + 10
+ data.logoWidth + data.logoPadding,
(canvasContext.measureText(data.text[1]).width|0) + 10,
(measureTextWidth(data.text[1])|0) + 10,
];
if (data.links === undefined) {
data.links = ['', ''];
Expand Down
19 changes: 19 additions & 0 deletions measure-text.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
'use strict';

var path = require('path');
var fs = require('fs');
var PDFDocument = require('pdfkit');

var doc = (new PDFDocument({size:'A4', layout:'landscape'}));
try {
doc = doc.font(path.join(__dirname, 'Verdana.ttf'));
} catch (ex) {
doc = doc.font('Helvetica-Bold')
console.warn('Could not load font file "Verdana.ttf", text widths will therefore be approximate', ex);
}
doc = doc.fontSize(11);

module.exports = measure;
function measure(str) {
return doc.widthOfString(str);
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
"dependencies": {
"dot": "~1.0.3",
"svgo": "~0.5.1",
"canvas": "~1.1.2",
"pdfkit": "~0.7.1",
"phantomjs": "~1.9.2-6",
"es6-promise": "~2.1.0",
"request": "~2.55.0",
Expand Down