7930a1f72911862777bdeb9d9f4f28bef0d92272
3 const th
= require('./template-helper');
12 fontFamily: 'DejaVu Sans,Verdana,Geneva,sans-serif',
18 * @param {number} n number
19 * @param {number} p precision
20 * @returns {number} rounded
22 function fixedRound(n
, p
= 2) {
23 return Number(n
.toFixed(p
));
28 * image/svg+xml;charset=utf-8 formatted badge with subscriber count for a topic
29 * @param {object} ctx - badge-specific context (not request context)
30 * @param {string} label label
31 * @param {string} message message
32 * @param {string} accessibleText accessible text
33 * @returns {string} svg element
35 module
.exports
= (ctx
, label
, message
, accessibleText
) => {
44 ctx
.verticalMargin
= fixedRound(ctx
.height
* 0.69);
45 ctx
.labelWidth
= fixedRound(ctx
.label
.length
* ctx
.charWidth
);
46 ctx
.messageWidth
= fixedRound(ctx
.message
.length
* ctx
.charWidth
);
47 ctx
.width
= ctx
.labelWidth
+ ctx
.messageWidth
;
48 ctx
.halfCharWidth
= fixedRound(ctx
.charWidth
* 0.5);
51 * This SVG content mostly replicates the output of the 'Plastic' badge
52 * renderer from https://github.com/badges/shields/tree/master/badge-maker which
53 * is under the http://creativecommons.org/publicdomain/zero/1.0/ license.
55 return `<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="${ctx.width}" height="${ctx.height}" role="img" aria-label="${th.xmlEscape(ctx.accessibleText)}">
56 <title>${th.xmlEscape(ctx.accessibleText)}</title>
57 <linearGradient id="s" x2="0" y2="100%">
58 <stop offset="0" stop-color="#fff" stop-opacity=".7"/>
59 <stop offset=".1" stop-color="#aaa" stop-opacity=".1"/>
60 <stop offset=".9" stop-color="#000" stop-opacity=".3"/>
61 <stop offset="1" stop-color="#000" stop-opacity=".5"/>
64 <rect width="${ctx.width}" height="${ctx.height}" rx="4" fill="#fff"/>
66 <g clip-path="url(#r)">
67 <rect width="${ctx.labelWidth}" height="${ctx.height}" fill="${ctx.labelColor}"/>
68 <rect x="${ctx.labelWidth}" width="${ctx.messageWidth}" height="${ctx.height}" fill="${ctx.messageColor}"/>
69 <rect width="${ctx.width}" height="${ctx.height}" fill="url(#s)"/>
71 <g fill="${ctx.color}" text-anchor="left" font-family="${ctx.fontFamily}" text-rendering="geometricPrecision" font-size="11" font-weight="bold">
72 <text x="${ctx.halfCharWidth}" y="${ctx.verticalMargin}">${th.xmlEscape(ctx.label)}</text>
73 <text x="${fixedRound(ctx.halfCharWidth + ctx.labelWidth)}" y="${ctx.verticalMargin}">${th.xmlEscape(ctx.message)}</text>