Skip to content

Commit

Permalink
Fix scale oddity with svg and AA workaround - #330
Browse files Browse the repository at this point in the history
  • Loading branch information
metafloor committed Mar 19, 2024
1 parent b108cb6 commit 0d7d316
Show file tree
Hide file tree
Showing 14 changed files with 160 additions and 83 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ or as SVG (all platforms).
## Status

* Current bwip-js version is 4.3.1 (2024-03-16)
* Current bwip-js version is 4.3.2 (2024-03-19)
* Current BWIPP version is 2024-01-03
* Node.js compatibility: 0.12+
* Browser compatibility: Edge, Firefox, Chrome
Expand Down
2 changes: 1 addition & 1 deletion dist/bwip-js-gen.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Type definitions for bwip-js 4.3.1 (2024-03-16)
// Type definitions for bwip-js 4.3.2 (2024-03-19)
//
// THIS DEFINITION FILE IS MACHINE GENERATED - DO NOT EDIT
//
Expand Down
33 changes: 22 additions & 11 deletions dist/bwip-js-gen.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
import { bwipp_auspost,bwipp_azteccode,bwipp_azteccodecompact,bwipp_aztecrune,bwipp_bc412,bwipp_channelcode,bwipp_codablockf,bwipp_code11,bwipp_code128,bwipp_code16k,bwipp_code2of5,bwipp_code32,bwipp_code39,bwipp_code39ext,bwipp_code49,bwipp_code93,bwipp_code93ext,bwipp_codeone,bwipp_coop2of5,bwipp_daft,bwipp_databarexpanded,bwipp_databarexpandedcomposite,bwipp_databarexpandedstacked,bwipp_databarexpandedstackedcomposite,bwipp_databarlimited,bwipp_databarlimitedcomposite,bwipp_databaromni,bwipp_databaromnicomposite,bwipp_databarstacked,bwipp_databarstackedcomposite,bwipp_databarstackedomni,bwipp_databarstackedomnicomposite,bwipp_databartruncated,bwipp_databartruncatedcomposite,bwipp_datalogic2of5,bwipp_datamatrix,bwipp_datamatrixrectangular,bwipp_datamatrixrectangularextension,bwipp_dotcode,bwipp_ean13,bwipp_ean13composite,bwipp_ean14,bwipp_ean2,bwipp_ean5,bwipp_ean8,bwipp_ean8composite,bwipp_flattermarken,bwipp_gs1_128,bwipp_gs1_128composite,bwipp_gs1_cc,bwipp_gs1datamatrix,bwipp_gs1datamatrixrectangular,bwipp_gs1dldatamatrix,bwipp_gs1dlqrcode,bwipp_gs1dotcode,bwipp_gs1northamericancoupon,bwipp_gs1qrcode,bwipp_hanxin,bwipp_hibcazteccode,bwipp_hibccodablockf,bwipp_hibccode128,bwipp_hibccode39,bwipp_hibcdatamatrix,bwipp_hibcdatamatrixrectangular,bwipp_hibcmicropdf417,bwipp_hibcpdf417,bwipp_hibcqrcode,bwipp_iata2of5,bwipp_identcode,bwipp_industrial2of5,bwipp_interleaved2of5,bwipp_isbn,bwipp_ismn,bwipp_issn,bwipp_itf14,bwipp_jabcode,bwipp_japanpost,bwipp_kix,bwipp_leitcode,bwipp_mailmark,bwipp_mands,bwipp_matrix2of5,bwipp_maxicode,bwipp_micropdf417,bwipp_microqrcode,bwipp_msi,bwipp_onecode,bwipp_pdf417,bwipp_pdf417compact,bwipp_pharmacode,bwipp_pharmacode2,bwipp_planet,bwipp_plessey,bwipp_posicode,bwipp_postnet,bwipp_pzn,bwipp_qrcode,bwipp_rationalizedCodabar,bwipp_raw,bwipp_rectangularmicroqrcode,bwipp_royalmail,bwipp_sscc18,bwipp_swissqrcode,bwipp_symbol,bwipp_telepen,bwipp_telepennumeric,bwipp_ultracode,bwipp_upca,bwipp_upcacomposite,bwipp_upce,bwipp_upcecomposite,bwipp_lookup,bwipp_encode,BWIPP_VERSION } from './bwipp.mjs';

// exports.js
const BWIPJS_VERSION = '4.3.1 (2024-03-16)';
const BWIPJS_VERSION = '4.3.2 (2024-03-19)';


// bwipjs.toSVG(options)
Expand Down Expand Up @@ -1491,6 +1491,11 @@ function DrawingSVG() {
var clips = [];
var lines = {};

// We adjust the drawing coordinates by 0.5px when stroke width is odd.
// But this creates an odd effect with scale. When scale is even, we
// need to add 0.5; when scale is odd, subtract 0.5.
var scalex, scaley;

// Magic number to approximate an ellipse/circle using 4 cubic beziers.
var ELLIPSE_MAGIC = 0.55228475 - 0.00045;

Expand All @@ -1511,6 +1516,8 @@ function DrawingSVG() {

// Make no adjustments
scale(sx, sy) {
scalex = sx;
scaley = sy;
},
// Measure text.
// `font` is the font name typically OCR-A or OCR-B.
Expand Down Expand Up @@ -1577,34 +1584,37 @@ function DrawingSVG() {
// Unconnected stroked lines are used to draw the bars in linear barcodes.
// No line cap should be applied. These lines are always orthogonal.
line(x0, y0, x1, y1, lw, rgb) {
// Try to get non-blurry lines...
x0 = x0|0;
y0 = y0|0;
x1 = x1|0;
y1 = y1|0;
lw = Math.round(lw);
lw = Math.round(lw) || 1;

// Try to keep the lines "crisp" by using with the SVG line drawing spec to
// our advantage.
// our advantage and adjust the coordinates by half pixel when stroke width
// is odd. Work around an odd effect with scale. When scale is even, we
// need to add 0.5; when scale is odd, subtract 0.5.
if (lw & 1) {
if (x0 == x1) {
x0 += 0.5;
x1 += 0.5;
let dx = (scalex&1) ? -0.5 : 0.5;
x0 += dx;
x1 += dx;
}
if (y0 == y1) {
y0 += 0.5;
y1 += 0.5;
let dy = (scaley&1) ? -0.5 : 0.5;
y0 += dy;
y1 += dy;
}
}
// The svg path does not include the start pixel, but the bwip-js drawing does.
// The svg path does not include the start pixel, but the built-in drawing does.
if (x0 == x1) {
y0++;
} else if (y0 == y1) {
x0++;
}

// Group together all lines of the same width and emit as single paths.
// Dramatically reduces resulting text size.
// Dramatically reduces the svg text size.
var key = '' + lw + '#' + rgb;
if (!lines[key]) {
lines[key] = '<path stroke="#' + rgb + '" stroke-width="' + lw + '" d="';
Expand Down Expand Up @@ -1661,7 +1671,8 @@ function DrawingSVG() {
'Z';
},
// PostScript's default fill rule is non-zero but there are never intersecting
// regions so use even-odd as it is easier to work with.
// regions. The built-in drawing uses even-odd for simplicity - we match that
// to be consistent.
fill(rgb) {
if (path) {
svg += path + '" fill="#' + rgb + '" fill-rule="evenodd"' +
Expand Down
2 changes: 1 addition & 1 deletion dist/bwip-js-min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/bwip-js-node.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Type definitions for bwip-js 4.3.1 (2024-03-16)
// Type definitions for bwip-js 4.3.2 (2024-03-19)
//
// THIS DEFINITION FILE IS MACHINE GENERATED - DO NOT EDIT
//
Expand Down
33 changes: 22 additions & 11 deletions dist/bwip-js-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@


// exports.js
const BWIPJS_VERSION = '4.3.1 (2024-03-16)';
const BWIPJS_VERSION = '4.3.2 (2024-03-19)';

var url = require('url');
var PNG_ZLIB = require('zlib');
Expand Down Expand Up @@ -42027,6 +42027,11 @@ function DrawingSVG() {
var clips = [];
var lines = {};

// We adjust the drawing coordinates by 0.5px when stroke width is odd.
// But this creates an odd effect with scale. When scale is even, we
// need to add 0.5; when scale is odd, subtract 0.5.
var scalex, scaley;

// Magic number to approximate an ellipse/circle using 4 cubic beziers.
var ELLIPSE_MAGIC = 0.55228475 - 0.00045;

Expand All @@ -42047,6 +42052,8 @@ function DrawingSVG() {

// Make no adjustments
scale(sx, sy) {
scalex = sx;
scaley = sy;
},
// Measure text.
// `font` is the font name typically OCR-A or OCR-B.
Expand Down Expand Up @@ -42113,34 +42120,37 @@ function DrawingSVG() {
// Unconnected stroked lines are used to draw the bars in linear barcodes.
// No line cap should be applied. These lines are always orthogonal.
line(x0, y0, x1, y1, lw, rgb) {
// Try to get non-blurry lines...
x0 = x0|0;
y0 = y0|0;
x1 = x1|0;
y1 = y1|0;
lw = Math.round(lw);
lw = Math.round(lw) || 1;

// Try to keep the lines "crisp" by using with the SVG line drawing spec to
// our advantage.
// our advantage and adjust the coordinates by half pixel when stroke width
// is odd. Work around an odd effect with scale. When scale is even, we
// need to add 0.5; when scale is odd, subtract 0.5.
if (lw & 1) {
if (x0 == x1) {
x0 += 0.5;
x1 += 0.5;
let dx = (scalex&1) ? -0.5 : 0.5;
x0 += dx;
x1 += dx;
}
if (y0 == y1) {
y0 += 0.5;
y1 += 0.5;
let dy = (scaley&1) ? -0.5 : 0.5;
y0 += dy;
y1 += dy;
}
}
// The svg path does not include the start pixel, but the bwip-js drawing does.
// The svg path does not include the start pixel, but the built-in drawing does.
if (x0 == x1) {
y0++;
} else if (y0 == y1) {
x0++;
}

// Group together all lines of the same width and emit as single paths.
// Dramatically reduces resulting text size.
// Dramatically reduces the svg text size.
var key = '' + lw + '#' + rgb;
if (!lines[key]) {
lines[key] = '<path stroke="#' + rgb + '" stroke-width="' + lw + '" d="';
Expand Down Expand Up @@ -42197,7 +42207,8 @@ function DrawingSVG() {
'Z';
},
// PostScript's default fill rule is non-zero but there are never intersecting
// regions so use even-odd as it is easier to work with.
// regions. The built-in drawing uses even-odd for simplicity - we match that
// to be consistent.
fill(rgb) {
if (path) {
svg += path + '" fill="#' + rgb + '" fill-rule="evenodd"' +
Expand Down
33 changes: 22 additions & 11 deletions dist/bwip-js-node.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
import { bwipp_auspost,bwipp_azteccode,bwipp_azteccodecompact,bwipp_aztecrune,bwipp_bc412,bwipp_channelcode,bwipp_codablockf,bwipp_code11,bwipp_code128,bwipp_code16k,bwipp_code2of5,bwipp_code32,bwipp_code39,bwipp_code39ext,bwipp_code49,bwipp_code93,bwipp_code93ext,bwipp_codeone,bwipp_coop2of5,bwipp_daft,bwipp_databarexpanded,bwipp_databarexpandedcomposite,bwipp_databarexpandedstacked,bwipp_databarexpandedstackedcomposite,bwipp_databarlimited,bwipp_databarlimitedcomposite,bwipp_databaromni,bwipp_databaromnicomposite,bwipp_databarstacked,bwipp_databarstackedcomposite,bwipp_databarstackedomni,bwipp_databarstackedomnicomposite,bwipp_databartruncated,bwipp_databartruncatedcomposite,bwipp_datalogic2of5,bwipp_datamatrix,bwipp_datamatrixrectangular,bwipp_datamatrixrectangularextension,bwipp_dotcode,bwipp_ean13,bwipp_ean13composite,bwipp_ean14,bwipp_ean2,bwipp_ean5,bwipp_ean8,bwipp_ean8composite,bwipp_flattermarken,bwipp_gs1_128,bwipp_gs1_128composite,bwipp_gs1_cc,bwipp_gs1datamatrix,bwipp_gs1datamatrixrectangular,bwipp_gs1dldatamatrix,bwipp_gs1dlqrcode,bwipp_gs1dotcode,bwipp_gs1northamericancoupon,bwipp_gs1qrcode,bwipp_hanxin,bwipp_hibcazteccode,bwipp_hibccodablockf,bwipp_hibccode128,bwipp_hibccode39,bwipp_hibcdatamatrix,bwipp_hibcdatamatrixrectangular,bwipp_hibcmicropdf417,bwipp_hibcpdf417,bwipp_hibcqrcode,bwipp_iata2of5,bwipp_identcode,bwipp_industrial2of5,bwipp_interleaved2of5,bwipp_isbn,bwipp_ismn,bwipp_issn,bwipp_itf14,bwipp_jabcode,bwipp_japanpost,bwipp_kix,bwipp_leitcode,bwipp_mailmark,bwipp_mands,bwipp_matrix2of5,bwipp_maxicode,bwipp_micropdf417,bwipp_microqrcode,bwipp_msi,bwipp_onecode,bwipp_pdf417,bwipp_pdf417compact,bwipp_pharmacode,bwipp_pharmacode2,bwipp_planet,bwipp_plessey,bwipp_posicode,bwipp_postnet,bwipp_pzn,bwipp_qrcode,bwipp_rationalizedCodabar,bwipp_raw,bwipp_rectangularmicroqrcode,bwipp_royalmail,bwipp_sscc18,bwipp_swissqrcode,bwipp_symbol,bwipp_telepen,bwipp_telepennumeric,bwipp_ultracode,bwipp_upca,bwipp_upcacomposite,bwipp_upce,bwipp_upcecomposite,bwipp_lookup,bwipp_encode,BWIPP_VERSION } from './bwipp.mjs';

// exports.js
const BWIPJS_VERSION = '4.3.1 (2024-03-16)';
const BWIPJS_VERSION = '4.3.2 (2024-03-19)';

import url from 'url';
import PNG_ZLIB from 'zlib';
Expand Down Expand Up @@ -1781,6 +1781,11 @@ function DrawingSVG() {
var clips = [];
var lines = {};

// We adjust the drawing coordinates by 0.5px when stroke width is odd.
// But this creates an odd effect with scale. When scale is even, we
// need to add 0.5; when scale is odd, subtract 0.5.
var scalex, scaley;

// Magic number to approximate an ellipse/circle using 4 cubic beziers.
var ELLIPSE_MAGIC = 0.55228475 - 0.00045;

Expand All @@ -1801,6 +1806,8 @@ function DrawingSVG() {

// Make no adjustments
scale(sx, sy) {
scalex = sx;
scaley = sy;
},
// Measure text.
// `font` is the font name typically OCR-A or OCR-B.
Expand Down Expand Up @@ -1867,34 +1874,37 @@ function DrawingSVG() {
// Unconnected stroked lines are used to draw the bars in linear barcodes.
// No line cap should be applied. These lines are always orthogonal.
line(x0, y0, x1, y1, lw, rgb) {
// Try to get non-blurry lines...
x0 = x0|0;
y0 = y0|0;
x1 = x1|0;
y1 = y1|0;
lw = Math.round(lw);
lw = Math.round(lw) || 1;

// Try to keep the lines "crisp" by using with the SVG line drawing spec to
// our advantage.
// our advantage and adjust the coordinates by half pixel when stroke width
// is odd. Work around an odd effect with scale. When scale is even, we
// need to add 0.5; when scale is odd, subtract 0.5.
if (lw & 1) {
if (x0 == x1) {
x0 += 0.5;
x1 += 0.5;
let dx = (scalex&1) ? -0.5 : 0.5;
x0 += dx;
x1 += dx;
}
if (y0 == y1) {
y0 += 0.5;
y1 += 0.5;
let dy = (scaley&1) ? -0.5 : 0.5;
y0 += dy;
y1 += dy;
}
}
// The svg path does not include the start pixel, but the bwip-js drawing does.
// The svg path does not include the start pixel, but the built-in drawing does.
if (x0 == x1) {
y0++;
} else if (y0 == y1) {
x0++;
}

// Group together all lines of the same width and emit as single paths.
// Dramatically reduces resulting text size.
// Dramatically reduces the svg text size.
var key = '' + lw + '#' + rgb;
if (!lines[key]) {
lines[key] = '<path stroke="#' + rgb + '" stroke-width="' + lw + '" d="';
Expand Down Expand Up @@ -1951,7 +1961,8 @@ function DrawingSVG() {
'Z';
},
// PostScript's default fill rule is non-zero but there are never intersecting
// regions so use even-odd as it is easier to work with.
// regions. The built-in drawing uses even-odd for simplicity - we match that
// to be consistent.
fill(rgb) {
if (path) {
svg += path + '" fill="#' + rgb + '" fill-rule="evenodd"' +
Expand Down
2 changes: 1 addition & 1 deletion dist/bwip-js-rn.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Type definitions for bwip-js 4.3.1 (2024-03-16)
// Type definitions for bwip-js 4.3.2 (2024-03-19)
//
// THIS DEFINITION FILE IS MACHINE GENERATED - DO NOT EDIT
//
Expand Down
Loading

0 comments on commit 0d7d316

Please sign in to comment.