From cb3da5cb17db07e85425ab545cc21316bfb7e7df Mon Sep 17 00:00:00 2001 From: Thaddee Tyl Date: Fri, 3 Jan 2014 12:29:54 +0100 Subject: [PATCH] Automatic measurement of text. --- img/build-error.svg | 2 +- img/build-failing.svg | 2 +- img/build-passed.svg | 2 +- img/build-passing.svg | 2 +- img/build-pending.svg | 2 +- img/build-unknown.svg | 2 +- img/cheez-whiz.svg | 2 +- img/coverage.svg | 2 +- img/dependencies-none.svg | 2 +- img/dependencies-out-of-date.svg | 2 +- img/dependencies-unknown.svg | 2 +- img/dependencies-up-to-date.svg | 2 +- img/dependencies-update.svg | 2 +- img/tips-per-week.svg | 2 +- make.js | 14 +++++++++++++- package.json | 1 + 16 files changed, 28 insertions(+), 15 deletions(-) diff --git a/img/build-error.svg b/img/build-error.svg index de96e6b..bd2d601 100644 --- a/img/build-error.svg +++ b/img/build-error.svg @@ -1 +1 @@ -builderror \ No newline at end of file +builderror \ No newline at end of file diff --git a/img/build-failing.svg b/img/build-failing.svg index 760f2d3..d4af07f 100644 --- a/img/build-failing.svg +++ b/img/build-failing.svg @@ -1 +1 @@ -buildfailing \ No newline at end of file +buildfailing \ No newline at end of file diff --git a/img/build-passed.svg b/img/build-passed.svg index 262d1fc..882749d 100644 --- a/img/build-passed.svg +++ b/img/build-passed.svg @@ -1 +1 @@ -buildpassed \ No newline at end of file +buildpassed \ No newline at end of file diff --git a/img/build-passing.svg b/img/build-passing.svg index 7c6a641..1aa0e1d 100644 --- a/img/build-passing.svg +++ b/img/build-passing.svg @@ -1 +1 @@ -buildpassing \ No newline at end of file +buildpassing \ No newline at end of file diff --git a/img/build-pending.svg b/img/build-pending.svg index 5397677..2928552 100644 --- a/img/build-pending.svg +++ b/img/build-pending.svg @@ -1 +1 @@ -buildpending \ No newline at end of file +buildpending \ No newline at end of file diff --git a/img/build-unknown.svg b/img/build-unknown.svg index d9efaa3..9cdd550 100644 --- a/img/build-unknown.svg +++ b/img/build-unknown.svg @@ -1 +1 @@ -buildunknown \ No newline at end of file +buildunknown \ No newline at end of file diff --git a/img/cheez-whiz.svg b/img/cheez-whiz.svg index a616eb3..69b0b53 100644 --- a/img/cheez-whiz.svg +++ b/img/cheez-whiz.svg @@ -1 +1 @@ -cheezwhiz \ No newline at end of file +cheezwhiz \ No newline at end of file diff --git a/img/coverage.svg b/img/coverage.svg index 7b3c41e..99fdb42 100644 --- a/img/coverage.svg +++ b/img/coverage.svg @@ -1 +1 @@ -coverage73% \ No newline at end of file +coverage73% \ No newline at end of file diff --git a/img/dependencies-none.svg b/img/dependencies-none.svg index 7fa5dbb..0f35cd2 100644 --- a/img/dependencies-none.svg +++ b/img/dependencies-none.svg @@ -1 +1 @@ -dependenciesnone \ No newline at end of file +dependenciesnone \ No newline at end of file diff --git a/img/dependencies-out-of-date.svg b/img/dependencies-out-of-date.svg index 0a7d72c..2850568 100644 --- a/img/dependencies-out-of-date.svg +++ b/img/dependencies-out-of-date.svg @@ -1 +1 @@ -dependenciesout of date \ No newline at end of file +dependenciesout of date \ No newline at end of file diff --git a/img/dependencies-unknown.svg b/img/dependencies-unknown.svg index e1fcab3..95c5752 100644 --- a/img/dependencies-unknown.svg +++ b/img/dependencies-unknown.svg @@ -1 +1 @@ -dependenciesunknown \ No newline at end of file +dependenciesunknown \ No newline at end of file diff --git a/img/dependencies-up-to-date.svg b/img/dependencies-up-to-date.svg index 81d9577..d695e6d 100644 --- a/img/dependencies-up-to-date.svg +++ b/img/dependencies-up-to-date.svg @@ -1 +1 @@ -dependenciesup-to-date \ No newline at end of file +dependenciesup-to-date \ No newline at end of file diff --git a/img/dependencies-update.svg b/img/dependencies-update.svg index 67170d8..add29f1 100644 --- a/img/dependencies-update.svg +++ b/img/dependencies-update.svg @@ -1 +1 @@ -dependenciesupdate! \ No newline at end of file +dependenciesupdate! \ No newline at end of file diff --git a/img/tips-per-week.svg b/img/tips-per-week.svg index 2f6029b..3c12376 100644 --- a/img/tips-per-week.svg +++ b/img/tips-per-week.svg @@ -1 +1 @@ -tips$21/week \ No newline at end of file +tips$21/week \ No newline at end of file diff --git a/make.js b/make.js index 31c844c..a065706 100644 --- a/make.js +++ b/make.js @@ -1,8 +1,16 @@ var fs = require('fs'); var path = require('path'); var Promise = require('es6-promise').Promise; -var dot = require('dot'); var SVGO = require('svgo'); + +// 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'); +canvasContext.font = '10px Verdana'; + +// Template crafting action below. +var dot = require('dot'); var badgeData = require('./badges.json'); var badges = badgeData.badges; var colorscheme = badgeData.colorschemes; @@ -26,6 +34,10 @@ function makeImage(name, data, cb) { data.colorA = colorscheme[data.colorscheme].colorA; data.colorB = colorscheme[data.colorscheme].colorB; } + data.widths = [ + (canvasContext.measureText(data.text[0]).width|0) + 10, + (canvasContext.measureText(data.text[1]).width|0) + 10, + ]; var result = imageTemplate(data); // Run the SVG through SVGO. optimize(result, function(object) { diff --git a/package.json b/package.json index 110b7d8..1793d3b 100644 --- a/package.json +++ b/package.json @@ -11,6 +11,7 @@ "dependencies": { "dot": ">=1.0.2", "svgo": "~0.4.2", + "canvas": "~1.1.2", "es6-promise": "~0.1.1" } }