From 7ea0d811a547c345163b6f045749b858c747dd95 Mon Sep 17 00:00:00 2001 From: Thaddee Tyl Date: Wed, 7 Sep 2016 07:24:58 +0100 Subject: [PATCH] Allow specifying a custom font path in the library --- INSTALL.md | 27 +++++++++------------------ badge.js | 1 + measure-text.js | 25 +++++++++++++++++-------- package.json | 6 +++--- 4 files changed, 30 insertions(+), 29 deletions(-) diff --git a/INSTALL.md b/INSTALL.md index 12b6ae9..b30121b 100644 --- a/INSTALL.md +++ b/INSTALL.md @@ -18,10 +18,13 @@ npm install gh-badges ```js var badge = require('gh-badges'); -badge({ text: [ "build", "passed" ], colorscheme: "green" }, - function(svg, err) { - // svg is a String… of your badge. - }); +// Optional step, to have accurate text width computation. +badge.loadFont('/path/to/Verdana.ttf', function(err) { + badge({ text: ["build", "passed"], colorscheme: "green", template: "flat" }, + function(svg, err) { + // svg is a String of your badge. + }); +}); ``` # Use the CLI @@ -35,12 +38,11 @@ badge build passed :green .png > mybadge.png # Start the Server To run the server you will need the following executables on your Path: - [PhantomJS](http://www.phantomjs.org/) -- [Cairo](http://cairographics.org/) (runtime dependency for Canvas) On an OS X machine, [Homebrew](brew.sh) is a good package manager that will -allow you to install them. +allow you to install that. -On Ubuntu / Debian: `sudo apt-get install phantomjs libcairo2-dev libjpeg-turbo8-dev`. +On Ubuntu / Debian: `sudo apt-get install phantomjs`. ```bash git clone https://github.com/badges/shields.git @@ -104,17 +106,6 @@ You can also use the `"colorA"` and `"colorB"` fields directly in the badges if you don't want to make a color scheme for it. In that case, remove the `"colorscheme"` field altogether. -# Requirements - -Because of the usage of the npm module [canvas][canvas-pkg] *you need* to have -**cairo** installed. - -For more information check the [wiki][canvas-wiki] of the canvas project with -system-specific installation details. - -[canvas-pkg]: https://npmjs.org/package/canvas -[canvas-wiki]: https://github.com/LearnBoost/node-canvas/wiki/_pages - # Making your Heroku badge server Once you have installed the [Heroku Toolbelt][]: diff --git a/badge.js b/badge.js index e6049c1..588a1ee 100644 --- a/badge.js +++ b/badge.js @@ -99,3 +99,4 @@ function makeImage(data, cb) { } module.exports = makeImage; +module.exports.loadFont = measureTextWidth.loadFont; diff --git a/measure-text.js b/measure-text.js index f96c204..7bc9751 100644 --- a/measure-text.js +++ b/measure-text.js @@ -3,17 +3,26 @@ var path = require('path'); var fs = require('fs'); var PDFDocument = require('pdfkit'); +var doc = new PDFDocument({size:'A4', layout:'landscape'}); -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); +// Attempt to use a particular font. +// callback: (optional) takes an error if it failed. +function loadFont(path, callback) { + try { + doc = doc.font(path); + if (callback) { callback(null); } + } catch(err) { + doc = doc.font('Helvetica-Bold'); + if (callback) { callback(err); } + } } + +loadFont(path.join(__dirname, 'Verdana.ttf')); doc = doc.fontSize(11); -module.exports = measure; function measure(str) { return doc.widthOfString(str); -} \ No newline at end of file +} + +module.exports = measure; +module.exports.loadFont = loadFont; diff --git a/package.json b/package.json index a41fef4..2151b23 100644 --- a/package.json +++ b/package.json @@ -1,8 +1,8 @@ { "name": "gh-badges", - "version": "1.2.2", - "description": "GitHub badges implemented in SVG.", - "keywords": ["GitHub", "badge", "SVG", "image"], + "version": "1.3.0", + "description": "Official Shields.io badge library.", + "keywords": ["GitHub", "badge", "SVG", "image", "shields.io"], "homepage": "http://shields.io", "bugs": { "url": "https://github.com/badges/shields/issues",