68 lines
2.2 KiB
JavaScript
68 lines
2.2 KiB
JavaScript
const { readPrintOptions } = require("./import-print.cjs");
|
|
const sides = new Set(["PrintTop", "PrintBottom", "PrintLeft", "PrintRight"]);
|
|
|
|
function updatePrintOptions(buffer, changes) {
|
|
const output = Buffer.from(buffer),
|
|
positions = new Map();
|
|
readPrintOptions(output, (row, fields) => {
|
|
const id = String(row.Index);
|
|
if (positions.has(id)) throw Error("인쇄 서식 번호 중복");
|
|
positions.set(id, fields);
|
|
});
|
|
for (const [id, values] of Object.entries(changes)) {
|
|
if (
|
|
!/^\d+$/.test(id) ||
|
|
!positions.has(id) ||
|
|
!values ||
|
|
typeof values !== "object" ||
|
|
Array.isArray(values)
|
|
)
|
|
throw Error(`인쇄 서식 변경 오류: ${id}`);
|
|
for (const [name, value] of Object.entries(values)) {
|
|
const field = positions.get(id)[name];
|
|
if (
|
|
sides.has(name) &&
|
|
field?.kind === 4 &&
|
|
field.width === 8 &&
|
|
typeof value === "number" &&
|
|
Number.isFinite(value) &&
|
|
value >= 0 &&
|
|
value <= 50
|
|
)
|
|
output.writeDoubleLE(value, field.at);
|
|
else if (
|
|
name === "PrintLineCount" &&
|
|
field?.kind === 1 &&
|
|
field.width === 4 &&
|
|
Number.isInteger(value) &&
|
|
value >= 1 &&
|
|
value <= 100
|
|
)
|
|
output.writeInt32LE(value, field.at);
|
|
else if (
|
|
name === "PrintGongMethod" &&
|
|
field?.kind === 1 &&
|
|
field.width === 4 &&
|
|
Number.isInteger(value) &&
|
|
[0, 1, 2].includes(value)
|
|
)
|
|
output.writeInt32LE(value, field.at);
|
|
else if (
|
|
["PrintApplyField", "PrintSumBottom"].includes(name) &&
|
|
field?.kind === 3 &&
|
|
field.width === 2 &&
|
|
Number.isInteger(value) &&
|
|
[0, 1].includes(value)
|
|
)
|
|
output.writeInt16LE(value, field.at);
|
|
else throw Error(`인쇄 서식 ${id}.${name}: 저장할 수 없는 값`);
|
|
}
|
|
}
|
|
const reread = new Map(readPrintOptions(output).map((row) => [String(row.Index), row]));
|
|
for (const [id, values] of Object.entries(changes))
|
|
for (const [key, value] of Object.entries(values))
|
|
if (reread.get(id)?.[key] !== value) throw Error(`인쇄 서식 ${id}.${key} 검산 실패`);
|
|
return output;
|
|
}
|
|
module.exports = { updatePrintOptions };
|