yuang-framework-ui-pc
Version:
yuang-framework-ui-pc Library
91 lines (90 loc) • 2.77 kB
JavaScript
;
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
const qrcodegen = require("./qrcodegen");
const ERROR_LEVEL_MAP = {
L: qrcodegen.QrCode.Ecc.LOW,
M: qrcodegen.QrCode.Ecc.MEDIUM,
Q: qrcodegen.QrCode.Ecc.QUARTILE,
H: qrcodegen.QrCode.Ecc.HIGH
};
const DEFAULT_IMG_SCALE = 0.1;
const SUPPORTS_PATH2D = function() {
try {
new Path2D().addPath(new Path2D());
} catch (e) {
return false;
}
return true;
}();
function getImageSettings(imageSettings, size, margin, cells) {
if (imageSettings == null || !imageSettings.src) {
return null;
}
const numCells = cells.length + margin * 2;
const defaultSize = Math.floor(size * DEFAULT_IMG_SCALE);
const scale = numCells / size;
const w = (imageSettings.width || defaultSize) * scale;
const h = (imageSettings.height || defaultSize) * scale;
const x = imageSettings.x == null ? cells.length / 2 - w / 2 : imageSettings.x * scale;
const y = imageSettings.y == null ? cells.length / 2 - h / 2 : imageSettings.y * scale;
let excavation = null;
if (imageSettings.excavate) {
const floorX = Math.floor(x);
const floorY = Math.floor(y);
const ceilW = Math.ceil(w + x - floorX);
const ceilH = Math.ceil(h + y - floorY);
excavation = { x: floorX, y: floorY, w: ceilW, h: ceilH };
}
return { x, y, h, w, excavation };
}
function excavateModules(modules, excavation) {
return modules.slice().map((row, y) => {
if (y < excavation.y || y >= excavation.y + excavation.h) {
return row;
}
return row.map((cell, x) => {
if (x < excavation.x || x >= excavation.x + excavation.w) {
return cell;
}
return false;
});
});
}
function generatePath(modules, margin = 0) {
const ops = [];
modules.forEach(function(row, y) {
let start = null;
row.forEach(function(cell, x) {
if (!cell && start !== null) {
ops.push(
`M${start + margin} ${y + margin}h${x - start}v1H${start + margin}z`
);
start = null;
return;
}
if (x === row.length - 1) {
if (!cell) {
return;
}
if (start === null) {
ops.push(`M${x + margin},${y + margin} h1v1H${x + margin}z`);
} else {
ops.push(
`M${start + margin},${y + margin} h${x + 1 - start}v1H${start + margin}z`
);
}
return;
}
if (cell && start === null) {
start = x;
}
});
});
return ops.join("");
}
exports.DEFAULT_IMG_SCALE = DEFAULT_IMG_SCALE;
exports.ERROR_LEVEL_MAP = ERROR_LEVEL_MAP;
exports.SUPPORTS_PATH2D = SUPPORTS_PATH2D;
exports.excavateModules = excavateModules;
exports.generatePath = generatePath;
exports.getImageSettings = getImageSettings;