Skip to content

Commit

Permalink
Switch to pdfkit like shields.io
Browse files Browse the repository at this point in the history
  • Loading branch information
exogen committed Aug 29, 2016
1 parent f1e6d18 commit bde565f
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 19 deletions.
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
"description": "More advanced badges for your projects using Travis or Sauce Labs.",
"main": "lib/index.js",
"engines": {
"node": "~6.2.0",
"npm": "~3.10.0"
"node": "6.5.x",
"npm": "3.10.x"
},
"scripts": {
"add-font": "./scripts/add-font.sh",
Expand All @@ -22,7 +22,6 @@
"author": "Brian Beck",
"license": "MIT",
"dependencies": {
"canvas": "^1.3.4",
"compression": "^1.6.0",
"express": "^4.13.3",
"express-interceptor": "^1.1.1",
Expand All @@ -32,6 +31,7 @@
"lru-cache": "^4.0.1",
"marko": "^3.8.1",
"object-hash": "^1.1.3",
"pdfkit": "^0.8.0",
"pretty-bytes": "^3.0.0",
"request": "^2.67.0",
"svgo": "^0.6.1",
Expand Down
4 changes: 0 additions & 4 deletions scripts/deploy.sh
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,6 @@ npm run check-font
# Fail if the `heroku` remote isn't there.
git remote show heroku

# Combine the Node.js and Cairo buildpacks.
heroku buildpacks:set https://github.com/mojodna/heroku-buildpack-cairo.git || true
heroku buildpacks:add heroku/nodejs || true

git stash # Stash uncommitted changes.
git checkout -B deploy # Force branch creation/reset.
npm run build
Expand Down
22 changes: 10 additions & 12 deletions src/text.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,18 @@
import path from "path";
import Canvas, { Font } from "canvas";
import PDFDocument from "pdfkit";

// Reference: https://github.com/badges/shields
const element = new Canvas(0, 0);
const ctx = element.getContext("2d");
let doc = new PDFDocument({ size: "A4", layout: "landscape" });
try {
const fontFile = path.join(__dirname, "..", "Verdana.ttf");
const font = new Font("Verdana", fontFile);
ctx.addFont(font);
} catch(err) {
console.warn(`Failed to add font: ${err}`);
doc = doc.font(path.join(__dirname, "..", "Verdana.ttf"));
} catch (err) {
doc = doc.font("Helvetica-Bold");
console.warn("Could not load font file 'Verdana.ttf', " +
"text widths will therefore be approximate.");
}
ctx.font = "11px Verdana, \"DejaVu Sans\"";
doc = doc.fontSize(11);

export default function measureTextWidth(text) {
return ctx.measureText(text).width | 0;
export default function measureTextWidth(text, rounding = "floor") {
return Math[rounding](doc.widthOfString(text));
}

if (require.main === module) {
Expand Down

0 comments on commit bde565f

Please sign in to comment.