diff --git a/.github/ISSUE_TEMPLATE/1_Bug_report.md b/.github/ISSUE_TEMPLATE/1_Bug_report.md index f43bd87394..524884316f 100644 --- a/.github/ISSUE_TEMPLATE/1_Bug_report.md +++ b/.github/ISSUE_TEMPLATE/1_Bug_report.md @@ -4,9 +4,10 @@ about: Report errors and problems --- -Are you experiencing this issue on shields.io or your own instance? - - [ ] shields.io - - [ ] My instance +Are you experiencing an issue with... + - [ ] [shields.io](https://shields.io/#/) + - [ ] My own instance + - [ ] [gh-badges NPM package](https://www.npmjs.com/package/gh-badges) :beetle: **Description** diff --git a/README.md b/README.md index c8674842c3..24d40f1a8c 100644 --- a/README.md +++ b/README.md @@ -88,28 +88,24 @@ badge build passed :green .png > mybadge.png ``` ```js -const badge = require('gh-badges') +const { BadgeFactory } = require('gh-badges') + +const bf = new BadgeFactory({ fontPath: '/path/to/Verdana.ttf' }) -// Optional step, to have accurate text width computation. const format = { text: ['build', 'passed'], colorscheme: 'green', template: 'flat', } -badge.loadFont('/path/to/Verdana.ttf', err => { - badge(format, (svg, err) => { - // svg is a string containing your badge - })}) +const svg = bf.create(format) ``` View the [documentation for gh-badges][gh-badges doc]. -**Note:** The badge library was last released in 2016. - [![npm version](http://img.shields.io/npm/v/gh-badges.svg)](https://npmjs.org/package/gh-badges) -[gh-badges doc]: doc/gh-badges.md +[gh-badges doc]: https://github.com/badges/shields/blob/master/doc/gh-badges.md Development diff --git a/doc/gh-badges.md b/doc/gh-badges.md index 9cdcd2cd77..f650e861a9 100644 --- a/doc/gh-badges.md +++ b/doc/gh-badges.md @@ -49,3 +49,30 @@ same dark grey, so you can rely on that default value by not providing a 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. + +Text Width Computation +---------------------- + +`BadgeFactory`'s constructor takes an optional boolean +`precomputeWidths` parameter which defaults to `false`. + +Pre-computing the font width table adds some overhead to constructing the +`BadgeFactory` object (so will slow down generation of a single image), +but will speed up each badge generation if you are creating a lot of images. +As a rule of thumb: + +If you are generating just one image, use: + +```js +const bf = new BadgeFactory( + { fontPath: '/path/to/Verdana.ttf' } +) +``` + +If you are generating many images with a single instance of `BadgeFactory`: + +```js +const bf = new BadgeFactory( + { fontPath: '/path/to/Verdana.ttf', precomputeWidths: true } +) +``` diff --git a/lib/gh-badges.js b/lib/gh-badges.js new file mode 100644 index 0000000000..d60218663c --- /dev/null +++ b/lib/gh-badges.js @@ -0,0 +1,33 @@ +'use strict' + +const { makeBadge } = require('./make-badge') +const { PDFKitTextMeasurer, QuickTextMeasurer } = require('./text-measurer') + +class BadgeFactory { + constructor({ fontPath, fallbackFontPath, precomputeWidths = false }) { + this.measurer = precomputeWidths + ? new QuickTextMeasurer(fontPath, fallbackFontPath) + : new PDFKitTextMeasurer(fontPath, fallbackFontPath) + } + + /** + * Create a badge + * + * @param {object} format - Object specifying badge data + * @param {string[]} format.text + * @param {string} format.colorscheme + * @param {string} format.colorA + * @param {string} format.colorB + * @param {string} format.format + * @param {string} format.template + * @return {string} Badge in SVG or JSON format + * @see https://github.com/badges/shields/blob/master/doc/gh-badges.md + */ + create(format) { + return makeBadge(this.measurer, format) + } +} + +module.exports = { + BadgeFactory, +} diff --git a/package-lock.json b/package-lock.json index 46d425f6c2..269d3517bc 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "gh-badges", - "version": "1.3.0", + "version": "2.0.0-beta1", "lockfileVersion": 1, "requires": true, "dependencies": { @@ -2428,7 +2428,6 @@ "version": "3.2.0", "resolved": "https://registry.npmjs.org/check-node-version/-/check-node-version-3.2.0.tgz", "integrity": "sha512-mJu4dADRf+NUeOyGgFTXaLtjyyffD3Eej2RA9IEk1CdHmoVurErLD++e/Ps6uKfsB273ky+0Z9NlOiuplxuNdw==", - "dev": true, "requires": { "chalk": "^2.3.0", "map-values": "^1.0.1", @@ -2443,7 +2442,6 @@ "version": "3.2.1", "resolved": "https://registry.npmjs.org/ansi-styles/-/ansi-styles-3.2.1.tgz", "integrity": "sha512-VT0ZI6kZRdTh8YyJw3SMbYm/u+NqfsAxEpWO0Pf9sq8/e94WxxOpPKx9FR1FlyCtOVDNOQ+8ntlqFxiRc+r5qA==", - "dev": true, "requires": { "color-convert": "^1.9.0" } @@ -2452,7 +2450,6 @@ "version": "2.4.1", "resolved": "https://registry.npmjs.org/chalk/-/chalk-2.4.1.tgz", "integrity": "sha512-ObN6h1v2fTJSmUXoS3nMQ92LbDK9be4TV+6G+omQlGJFdcUX5heKi1LZ1YnRMIgwTLEj3E24bT6tYni50rlCfQ==", - "dev": true, "requires": { "ansi-styles": "^3.2.1", "escape-string-regexp": "^1.0.5", @@ -2462,14 +2459,12 @@ "has-flag": { "version": "3.0.0", "resolved": "https://registry.npmjs.org/has-flag/-/has-flag-3.0.0.tgz", - "integrity": "sha1-tdRU3CGZriJWmfNGfloH87lVuv0=", - "dev": true + "integrity": "sha1-tdRU3CGZriJWmfNGfloH87lVuv0=" }, "supports-color": { "version": "5.4.0", "resolved": "https://registry.npmjs.org/supports-color/-/supports-color-5.4.0.tgz", "integrity": "sha512-zjaXglF5nnWpsq470jSv6P9DwPvgLkuapYmfDm3JWOm0vkNTVF2tI4UrN2r6jH1qM/uc/WtxYY1hYoA2dOKj5w==", - "dev": true, "requires": { "has-flag": "^3.0.0" } @@ -8221,8 +8216,7 @@ "map-values": { "version": "1.0.1", "resolved": "https://registry.npmjs.org/map-values/-/map-values-1.0.1.tgz", - "integrity": "sha1-douOecAJvytk/ugG4ip7HEGQyZA=", - "dev": true + "integrity": "sha1-douOecAJvytk/ugG4ip7HEGQyZA=" }, "map-visit": { "version": "1.0.0", @@ -8557,8 +8551,7 @@ "minimist": { "version": "1.2.0", "resolved": "https://registry.npmjs.org/minimist/-/minimist-1.2.0.tgz", - "integrity": "sha1-o1AIsg9BOD7sH7kU9M1d95omQoQ=", - "dev": true + "integrity": "sha1-o1AIsg9BOD7sH7kU9M1d95omQoQ=" }, "mississippi": { "version": "2.0.0", @@ -10604,8 +10597,7 @@ "object-filter": { "version": "1.0.2", "resolved": "https://registry.npmjs.org/object-filter/-/object-filter-1.0.2.tgz", - "integrity": "sha1-rwt5f/6+r4pSxmN87b6IFs/sG8g=", - "dev": true + "integrity": "sha1-rwt5f/6+r4pSxmN87b6IFs/sG8g=" }, "object-keys": { "version": "1.0.11", @@ -10625,7 +10617,6 @@ "version": "4.0.4", "resolved": "https://registry.npmjs.org/object.assign/-/object.assign-4.0.4.tgz", "integrity": "sha1-scnMBE7xuf5jYG/BQau7MuFHMMw=", - "dev": true, "requires": { "define-properties": "^1.1.2", "function-bind": "^1.1.0", @@ -12177,8 +12168,7 @@ "run-parallel": { "version": "1.1.9", "resolved": "https://registry.npmjs.org/run-parallel/-/run-parallel-1.1.9.tgz", - "integrity": "sha512-DEqnSRTDw/Tc3FXf49zedI638Z9onwUotBMiUFKmrO2sdFKIbXamXGQ3Axd4qgphxKB4kw/qP1w5kTxnfU1B9Q==", - "dev": true + "integrity": "sha512-DEqnSRTDw/Tc3FXf49zedI638Z9onwUotBMiUFKmrO2sdFKIbXamXGQ3Axd4qgphxKB4kw/qP1w5kTxnfU1B9Q==" }, "run-queue": { "version": "1.0.3", diff --git a/package.json b/package.json index 2347e26dce..cc1d941446 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "gh-badges", - "version": "1.3.0", + "version": "2.0.0-beta1", "description": "Official Shields.io badge library.", "keywords": [ "GitHub", @@ -16,7 +16,7 @@ }, "license": "CC0-1.0", "author": "Thaddée Tyl ", - "main": "lib/make-badge.js", + "main": "lib/gh-badges.js", "repository": { "type": "git", "url": "https://github.com/badges/shields" @@ -24,6 +24,7 @@ "dependencies": { "camp": "~17.2.1", "chalk": "^2.4.1", + "check-node-version": "^3.1.0", "chrome-web-store-item-property": "~1.1.2", "dot": "~1.1.2", "emojic": "^1.1.14", @@ -113,6 +114,7 @@ "lib/lru-cache.js", "lib/text-measurer.js", "lib/svg-to-img.js", + "lib/defaults.js", "templates", "logo" ], @@ -127,7 +129,6 @@ "chai-as-promised": "^7.1.1", "chai-string": "^1.4.0", "chainsmoker": "^0.1.0", - "check-node-version": "^3.1.0", "child-process-promise": "^2.2.1", "classnames": "^2.2.5", "concurrently": "^4.0.1",