Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions js/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ Helper functions for HTML.
| opts.cellSize | <code>number</code> | default: 2 |
| opts.margin | <code>number</code> | default: cellSize * 4 |
| opts.scalable | <code>boolean</code> | default: false |
| opts.crispEdges | <code>boolean</code> \| <code>'auto'</code> | default: auto; applies <code>crispEdges</code> automatically for whole-number <code>cellSize</code> values |

#### renderTo2dContext(context, cellSize) => <code>void</code>

Expand Down
2 changes: 1 addition & 1 deletion js/dist/qrcode.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ interface QRCode {
createImgTag(cellSize?: number, margin?: number) : string;
createSvgTag(cellSize?: number, margin?: number) : string;
createSvgTag(opts? : { cellSize?: number, margin?: number,
scalable?: boolean }) : string;
scalable?: boolean, crispEdges?: boolean | 'auto' }) : string;
createDataURL(cellSize?: number, margin?: number) : string;
createTableTag(cellSize?: number, margin?: number) : string;
createASCII(cellSize?: number, margin?: number) : string;
Expand Down
4 changes: 4 additions & 0 deletions js/dist/qrcode.js
Original file line number Diff line number Diff line change
Expand Up @@ -507,6 +507,7 @@ var qrcode = function() {

cellSize = cellSize || 2;
margin = (typeof margin == 'undefined')? cellSize * 4 : margin;
var crispEdges = typeof opts.crispEdges == 'undefined' ? 'auto' : opts.crispEdges;

// Compose alt property surrogate
alt = (typeof alt === 'string') ? {text: alt} : alt || {};
Expand All @@ -528,6 +529,9 @@ var qrcode = function() {
qrSvg += !opts.scalable ? ' width="' + size + 'px" height="' + size + 'px"' : '';
qrSvg += ' viewBox="0 0 ' + size + ' ' + size + '" ';
qrSvg += ' preserveAspectRatio="xMinYMin meet"';
qrSvg += crispEdges === true ||
(crispEdges === 'auto' && Math.abs(cellSize - Math.round(cellSize)) < 0.001) ?
' shape-rendering="crispEdges"' : '';
qrSvg += (title.text || alt.text) ? ' role="img" aria-labelledby="' +
escapeXml([title.id, alt.id].join(' ').trim() ) + '"' : '';
qrSvg += '>';
Expand Down
4 changes: 4 additions & 0 deletions js/dist/qrcode.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -505,6 +505,7 @@ export const qrcode = function(typeNumber, errorCorrectionLevel) {

cellSize = cellSize || 2;
margin = (typeof margin == 'undefined')? cellSize * 4 : margin;
const crispEdges = typeof opts.crispEdges == 'undefined' ? 'auto' : opts.crispEdges;

// Compose alt property surrogate
alt = (typeof alt === 'string') ? {text: alt} : alt || {};
Expand All @@ -526,6 +527,9 @@ export const qrcode = function(typeNumber, errorCorrectionLevel) {
qrSvg += !opts.scalable ? ' width="' + size + 'px" height="' + size + 'px"' : '';
qrSvg += ' viewBox="0 0 ' + size + ' ' + size + '" ';
qrSvg += ' preserveAspectRatio="xMinYMin meet"';
qrSvg += crispEdges === true ||
(crispEdges === 'auto' && Math.abs(cellSize - Math.round(cellSize)) < 0.001) ?
' shape-rendering="crispEdges"' : '';
qrSvg += (title.text || alt.text) ? ' role="img" aria-labelledby="' +
escapeXml([title.id, alt.id].join(' ').trim() ) + '"' : '';
qrSvg += '>';
Expand Down
4 changes: 4 additions & 0 deletions js/experiment/vt/src/main/qrcode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -516,6 +516,7 @@ const qrcode : QRCodeFactory = function(typeNumber : number, errorCorrectionLeve

cellSize = cellSize || 2;
margin = (typeof margin == 'undefined')? cellSize * 4 : margin;
const crispEdges = typeof opts.crispEdges == 'undefined' ? 'auto' : opts.crispEdges;

// Compose alt property surrogate
alt = (typeof alt === 'string') ? {text: alt} : alt || {};
Expand All @@ -537,6 +538,9 @@ const qrcode : QRCodeFactory = function(typeNumber : number, errorCorrectionLeve
qrSvg += !opts.scalable ? ' width="' + size + 'px" height="' + size + 'px"' : '';
qrSvg += ' viewBox="0 0 ' + size + ' ' + size + '" ';
qrSvg += ' preserveAspectRatio="xMinYMin meet"';
qrSvg += crispEdges === true ||
(crispEdges === 'auto' && Math.abs(cellSize - Math.round(cellSize)) < 0.001) ?
' shape-rendering="crispEdges"' : '';
qrSvg += (title.text || alt.text) ? ' role="img" aria-labelledby="' +
escapeXml([title.id, alt.id].join(' ').trim() ) + '"' : '';
qrSvg += '>';
Expand Down
1 change: 1 addition & 0 deletions js/experiment/vt/src/main/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ export type QRSvgTagOpts = {
cellSize?: number,
margin?: number,
scalable?: boolean,
crispEdges?: boolean | 'auto',
alt?: any,
title?: any
};
Expand Down
16 changes: 16 additions & 0 deletions js/experiment/vt/src/test/qrcode-test-impl.js
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,22 @@ export const misc = function(qrcode) {
expect(!!qr.createSvgTag({}) ).to.be.true;
});

it('svg tag auto crispEdges for whole-number cell size', function(){
const qr = qrcode(1, 'H');
qr.addData('{SVG}');
qr.make();
expect(qr.createSvgTag({cellSize : 2}) ).to.contain(' shape-rendering="crispEdges"');
expect(qr.createSvgTag({cellSize : 2.5}) ).not.to.contain(' shape-rendering="crispEdges"');
});

it('svg tag can force crispEdges on and off', function(){
const qr = qrcode(1, 'H');
qr.addData('{SVG}');
qr.make();
expect(qr.createSvgTag({cellSize : 2.5, crispEdges : true}) ).to.contain(' shape-rendering="crispEdges"');
expect(qr.createSvgTag({cellSize : 2, crispEdges : false}) ).not.to.contain(' shape-rendering="crispEdges"');
});

it('svg tag by object (for coverage)', function(){

let count = 0;
Expand Down
16 changes: 16 additions & 0 deletions js/test/qrcode-test-impl.js
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,22 @@ export const misc = function(qrcode) {
expect(!!qr.createSvgTag({}) ).to.be.true;
});

it('svg tag auto crispEdges for whole-number cell size', function(){
const qr = qrcode(1, 'H');
qr.addData('{SVG}');
qr.make();
expect(qr.createSvgTag({cellSize : 2}) ).to.contain(' shape-rendering="crispEdges"');
expect(qr.createSvgTag({cellSize : 2.5}) ).not.to.contain(' shape-rendering="crispEdges"');
});

it('svg tag can force crispEdges on and off', function(){
const qr = qrcode(1, 'H');
qr.addData('{SVG}');
qr.make();
expect(qr.createSvgTag({cellSize : 2.5, crispEdges : true}) ).to.contain(' shape-rendering="crispEdges"');
expect(qr.createSvgTag({cellSize : 2, crispEdges : false}) ).not.to.contain(' shape-rendering="crispEdges"');
});

it('svg tag by object (for coverage)', function(){

let count = 0;
Expand Down