1
0
forked from Yara724/api

YARA-994 and fixed metal plate bug

This commit is contained in:
SepehrYahyaee
2026-07-15 10:33:50 +03:30
parent 385757c3a0
commit da7a4f8890
3 changed files with 48 additions and 9 deletions

View File

@@ -22,20 +22,18 @@ const LABEL_COLUMN_X = COLON_X + 10;
function resolveFontFile(variant: "regular" | "bold"): string {
const file =
variant === "bold"
? "NotoKufiArabic-Bold.ttf"
: "NotoKufiArabic-Regular.ttf";
variant === "bold" ? "Vazirmatn-Bold.ttf" : "Vazirmatn-Regular.ttf";
const candidates = [
join(process.cwd(), "assets", file),
join(process.cwd(), "assets", "fonts", file),
join(process.cwd(), "dist", "fonts", file),
join(process.cwd(), "dist", "assets", file),
join(process.cwd(), "dist", "assets", "fonts", file),
join("/usr/share/fonts/truetype/noto", file),
];
for (const path of candidates) {
if (existsSync(path)) return path;
}
throw new Error(
`Persian PDF font not found (${file}). Place it under assets/fonts/ or install noto fonts on the host.`,
`Persian PDF font not found (${file}). Place it under assets/ or assets/fonts/.`,
);
}
@@ -46,6 +44,16 @@ export type PersianPdfDocument = InstanceType<typeof PDFDocument> & {
addBlank: (lines?: number) => void;
};
/** Ensure at least `neededPts` of vertical space remain; add a page if not. */
function ensureSpace(
doc: InstanceType<typeof PDFDocument>,
neededPts: number,
): void {
if (doc.y + neededPts > doc.page.maxY()) {
doc.addPage();
}
}
function drawRtlLine(
doc: InstanceType<typeof PDFDocument>,
text: string,
@@ -69,6 +77,9 @@ function drawKeyValueRow(
label: string,
value: string,
): void {
// Reserve one row height (font 10pt ≈ 14 pts with leading) before drawing.
ensureSpace(doc, 16);
const y = doc.y;
const { label: faLabel, value: faValue } = pdfKitRtlKeyValue(label, value);
@@ -118,6 +129,9 @@ export function createPersianPdfDocument(): PersianPdfDocument {
};
doc.addSection = (text: string) => {
// Reserve space for the gap + section heading + at least one data row below it,
// so the heading is never stranded alone at the bottom of a page.
ensureSpace(doc, 60);
doc.moveDown(0.5);
drawRtlLine(doc, text, 13, true);
doc.font("FaRegular").fontSize(10);

View File

@@ -8,18 +8,31 @@ export function normalizePersianPdfText(text: string): string {
.trim();
}
/**
* pdfkit renders glyphs strictly left-to-right.
* fontkit already reshapes each word's glyphs into visual (LTR) order,
* but it does NOT reorder words — so a multi-word RTL string like
* "نام صاحب خودرو" has its words placed LTR on the page and reads
* backwards. Reversing the word sequence here makes pdfkit emit the
* words in the correct visual order for a right-to-left PDF reader.
*/
function reverseRtlWords(text: string): string {
if (!/[\u0600-\u06FF]/.test(text)) return text;
return text.split(" ").reverse().join(" ");
}
export function pdfKitRtlKeyValue(
label: string,
value: string,
): { label: string; value: string } {
return {
label: normalizePersianPdfText(label),
value: normalizePersianPdfText(value),
label: reverseRtlWords(normalizePersianPdfText(label)),
value: reverseRtlWords(normalizePersianPdfText(value)),
};
}
export function pdfKitRtlParagraph(text: string): string {
return normalizePersianPdfText(text);
return reverseRtlWords(normalizePersianPdfText(text));
}
export function isMostlyAscii(text: string): boolean {