@appcorp/fusion-storybook 0.5.40 → 0.6.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/attendance-register/documents/download.d.ts +2 -0
- package/attendance-register/documents/download.js +13 -0
- package/attendance-register/documents/image-text.d.ts +37 -0
- package/attendance-register/documents/image-text.js +103 -0
- package/attendance-register/documents/pdf.d.ts +40 -0
- package/attendance-register/documents/pdf.js +425 -0
- package/attendance-register/documents/zip-files.d.ts +2 -0
- package/attendance-register/documents/zip-files.js +5 -0
- package/attendance-register/documents/zip.d.ts +13 -0
- package/attendance-register/documents/zip.js +29 -0
- package/attendance-register/domain/calendar.d.ts +28 -0
- package/attendance-register/domain/calendar.js +150 -0
- package/attendance-register/domain/locale.d.ts +26 -0
- package/attendance-register/domain/locale.js +87 -0
- package/attendance-register/domain/register.d.ts +50 -0
- package/attendance-register/domain/register.js +116 -0
- package/attendance-register/domain/student-import.d.ts +51 -0
- package/attendance-register/domain/student-import.js +231 -0
- package/attendance-register/domain/week.d.ts +40 -0
- package/attendance-register/domain/week.js +90 -0
- package/attendance-register/index.d.ts +21 -0
- package/attendance-register/index.js +19 -0
- package/attendance-register/react/register-preview.d.ts +34 -0
- package/attendance-register/react/register-preview.js +88 -0
- package/attendance-register/react/use-attendance-register.d.ts +33 -0
- package/attendance-register/react/use-attendance-register.js +106 -0
- package/attendance-register/types.d.ts +90 -0
- package/attendance-register/types.js +22 -0
- package/base-modules/attendance/page.d.ts +2 -0
- package/base-modules/attendance/page.js +21 -1
- package/constants.d.ts +2 -3
- package/constants.js +2 -3
- package/package.json +3 -1
- package/tsconfig.build.tsbuildinfo +1 -1
- package/types/communication.d.ts +3 -59
- package/types/communication.js +2 -2
- package/types/user-management.d.ts +0 -6
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
/** Small browser download helper shared by both register tools. */
|
|
2
|
+
export function downloadBlob(blob, filename) {
|
|
3
|
+
if (typeof document === "undefined")
|
|
4
|
+
return;
|
|
5
|
+
const url = URL.createObjectURL(blob);
|
|
6
|
+
const link = document.createElement("a");
|
|
7
|
+
link.href = url;
|
|
8
|
+
link.download = filename;
|
|
9
|
+
document.body.appendChild(link);
|
|
10
|
+
link.click();
|
|
11
|
+
document.body.removeChild(link);
|
|
12
|
+
URL.revokeObjectURL(url);
|
|
13
|
+
}
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import type { Student } from "../types";
|
|
2
|
+
export declare function needsNameImage(students: Student[]): boolean;
|
|
3
|
+
export interface TextImageRun {
|
|
4
|
+
text: string;
|
|
5
|
+
/** Left edge of the run's box, in millimetres from the image's left edge. */
|
|
6
|
+
boxLeftMm: number;
|
|
7
|
+
boxWidthMm: number;
|
|
8
|
+
align?: "left" | "center" | "right";
|
|
9
|
+
}
|
|
10
|
+
export interface TextImageOptions {
|
|
11
|
+
runs: TextImageRun[];
|
|
12
|
+
widthMm: number;
|
|
13
|
+
heightMm: number;
|
|
14
|
+
fontPt: number;
|
|
15
|
+
/** Alphabetic baseline measured from the top of the image. */
|
|
16
|
+
baselineMm: number;
|
|
17
|
+
color?: string;
|
|
18
|
+
paddingMm?: number;
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* Draws one or more short text runs on a transparent canvas so complex scripts
|
|
22
|
+
* can be placed into the PDF. Used for the weekday row and the header meta line.
|
|
23
|
+
*/
|
|
24
|
+
export declare function renderTextImage({ runs, widthMm, heightMm, fontPt, baselineMm, color, paddingMm, }: TextImageOptions): string | null;
|
|
25
|
+
interface NameImageOptions {
|
|
26
|
+
names: Student[];
|
|
27
|
+
/** Printed name column width in millimetres. */
|
|
28
|
+
widthMm: number;
|
|
29
|
+
/** Row height in millimetres. */
|
|
30
|
+
rowHeightMm: number;
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* Renders the visible names as a transparent PNG. Returns null outside the
|
|
34
|
+
* browser, when there is nothing to draw, or when a 2D context is unavailable.
|
|
35
|
+
*/
|
|
36
|
+
export declare function renderNameImage({ names, widthMm, rowHeightMm, }: NameImageOptions): string | null;
|
|
37
|
+
export {};
|
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* jsPDF cannot shape complex scripts: Arabic/Urdu stay unjoined, Indic conjuncts
|
|
3
|
+
* break, and the embedded font has no CJK glyphs. For those rosters we draw only
|
|
4
|
+
* the name column on a canvas (the browser shapes text correctly) and place it
|
|
5
|
+
* over the vector table as a transparent image.
|
|
6
|
+
*/
|
|
7
|
+
const NEEDS_NAME_IMAGE = /[\p{Script=Arabic}\p{Script=Hebrew}\p{Script=Devanagari}\p{Script=Bengali}\p{Script=Gurmukhi}\p{Script=Gujarati}\u0B00-\u0B7F\p{Script=Tamil}\p{Script=Telugu}\p{Script=Kannada}\p{Script=Malayalam}\p{Script=Sinhala}\p{Script=Thai}\p{Script=Lao}\p{Script=Myanmar}\p{Script=Khmer}\p{Script=Han}\p{Script=Hiragana}\p{Script=Katakana}\p{Script=Hangul}\p{Script=Cyrillic}\p{Script=Greek}\p{Script=Armenian}\p{Script=Georgian}\p{Script=Ethiopic}\u1EA0-\u1EF9]/u;
|
|
8
|
+
const RTL_NAME = /[\p{Script=Arabic}\p{Script=Hebrew}]/u;
|
|
9
|
+
const FIRST_LETTER = /\p{L}/u;
|
|
10
|
+
/** Paragraph direction follows the first strong character, as browsers do. */
|
|
11
|
+
function isRtlText(text) {
|
|
12
|
+
const first = FIRST_LETTER.exec(text);
|
|
13
|
+
return first ? RTL_NAME.test(first[0]) : false;
|
|
14
|
+
}
|
|
15
|
+
/** High enough for print: 12 px/mm is roughly 305 dpi. */
|
|
16
|
+
const PX_PER_MM = 12;
|
|
17
|
+
/** 8pt in millimetres: complex scripts need more height than Latin to stay legible. */
|
|
18
|
+
const FONT_SIZE_MM = 8 * 0.3528;
|
|
19
|
+
const TEXT_COLOR = "#373737";
|
|
20
|
+
const FONT_FAMILY = "system-ui,-apple-system,'Segoe UI',Roboto,'Noto Sans','Noto Sans Arabic','Noto Sans Devanagari',sans-serif";
|
|
21
|
+
export function needsNameImage(students) {
|
|
22
|
+
return students.some((student) => NEEDS_NAME_IMAGE.test(student.name));
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* Draws one or more short text runs on a transparent canvas so complex scripts
|
|
26
|
+
* can be placed into the PDF. Used for the weekday row and the header meta line.
|
|
27
|
+
*/
|
|
28
|
+
export function renderTextImage({ runs, widthMm, heightMm, fontPt, baselineMm, color = "#374151", paddingMm = 1, }) {
|
|
29
|
+
var _a;
|
|
30
|
+
if (typeof document === "undefined" || heightMm <= 0 || runs.length === 0)
|
|
31
|
+
return null;
|
|
32
|
+
const width = Math.max(1, Math.ceil(widthMm * PX_PER_MM));
|
|
33
|
+
const height = Math.max(1, Math.ceil(heightMm * PX_PER_MM));
|
|
34
|
+
const canvas = document.createElement("canvas");
|
|
35
|
+
canvas.width = width;
|
|
36
|
+
canvas.height = height;
|
|
37
|
+
const ctx = canvas.getContext("2d");
|
|
38
|
+
if (!ctx)
|
|
39
|
+
return null;
|
|
40
|
+
ctx.font = `${fontPt * 0.3528 * PX_PER_MM}px ${FONT_FAMILY}`;
|
|
41
|
+
ctx.fillStyle = color;
|
|
42
|
+
ctx.textBaseline = "alphabetic";
|
|
43
|
+
const baseline = baselineMm * PX_PER_MM;
|
|
44
|
+
const padding = paddingMm * PX_PER_MM;
|
|
45
|
+
for (const run of runs) {
|
|
46
|
+
if (!run.text)
|
|
47
|
+
continue;
|
|
48
|
+
const rtl = isRtlText(run.text);
|
|
49
|
+
const align = (_a = run.align) !== null && _a !== void 0 ? _a : "left";
|
|
50
|
+
const left = run.boxLeftMm * PX_PER_MM;
|
|
51
|
+
const boxWidth = run.boxWidthMm * PX_PER_MM;
|
|
52
|
+
const x = align === "center"
|
|
53
|
+
? left + boxWidth / 2
|
|
54
|
+
: align === "right"
|
|
55
|
+
? left + boxWidth - padding
|
|
56
|
+
: left + padding;
|
|
57
|
+
const maxWidth = Math.max(1, boxWidth - padding * 2);
|
|
58
|
+
ctx.direction = rtl ? "rtl" : "ltr";
|
|
59
|
+
ctx.textAlign = align;
|
|
60
|
+
ctx.fillText(fitText(ctx, run.text, maxWidth), x, baseline);
|
|
61
|
+
}
|
|
62
|
+
return canvas.toDataURL("image/png");
|
|
63
|
+
}
|
|
64
|
+
function fitText(ctx, text, maxWidth) {
|
|
65
|
+
if (ctx.measureText(text).width <= maxWidth)
|
|
66
|
+
return text;
|
|
67
|
+
const characters = Array.from(text);
|
|
68
|
+
while (characters.length > 1 &&
|
|
69
|
+
ctx.measureText(`${characters.join("")}…`).width > maxWidth) {
|
|
70
|
+
characters.pop();
|
|
71
|
+
}
|
|
72
|
+
return `${characters.join("")}…`;
|
|
73
|
+
}
|
|
74
|
+
/**
|
|
75
|
+
* Renders the visible names as a transparent PNG. Returns null outside the
|
|
76
|
+
* browser, when there is nothing to draw, or when a 2D context is unavailable.
|
|
77
|
+
*/
|
|
78
|
+
export function renderNameImage({ names, widthMm, rowHeightMm, }) {
|
|
79
|
+
if (typeof document === "undefined" || names.length === 0)
|
|
80
|
+
return null;
|
|
81
|
+
const width = Math.max(1, Math.ceil(widthMm * PX_PER_MM));
|
|
82
|
+
const height = Math.max(1, Math.ceil(names.length * rowHeightMm * PX_PER_MM));
|
|
83
|
+
const canvas = document.createElement("canvas");
|
|
84
|
+
canvas.width = width;
|
|
85
|
+
canvas.height = height;
|
|
86
|
+
const ctx = canvas.getContext("2d");
|
|
87
|
+
if (!ctx)
|
|
88
|
+
return null;
|
|
89
|
+
const rowHeightPx = rowHeightMm * PX_PER_MM;
|
|
90
|
+
const paddingX = 1 * PX_PER_MM;
|
|
91
|
+
ctx.font = `${FONT_SIZE_MM * PX_PER_MM}px ${FONT_FAMILY}`;
|
|
92
|
+
ctx.fillStyle = TEXT_COLOR;
|
|
93
|
+
ctx.textBaseline = "middle";
|
|
94
|
+
names.forEach((student, index) => {
|
|
95
|
+
const rtl = isRtlText(student.name);
|
|
96
|
+
ctx.direction = rtl ? "rtl" : "ltr";
|
|
97
|
+
ctx.textAlign = rtl ? "right" : "left";
|
|
98
|
+
const x = rtl ? width - paddingX : paddingX;
|
|
99
|
+
const y = index * rowHeightPx + rowHeightPx / 2;
|
|
100
|
+
ctx.fillText(fitText(ctx, student.name, width - paddingX * 2), x, y);
|
|
101
|
+
});
|
|
102
|
+
return canvas.toDataURL("image/png");
|
|
103
|
+
}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import jsPDF from "jspdf";
|
|
2
|
+
import { RegisterConfig, WeekGroup } from "../types";
|
|
3
|
+
type RGB = [number, number, number];
|
|
4
|
+
export interface AttendancePdfHeaderArgs {
|
|
5
|
+
pdf: jsPDF;
|
|
6
|
+
config: RegisterConfig;
|
|
7
|
+
week?: WeekGroup;
|
|
8
|
+
pageWidth: number;
|
|
9
|
+
pageHeight: number;
|
|
10
|
+
margin: number;
|
|
11
|
+
fontFamily: string;
|
|
12
|
+
accentColor: RGB;
|
|
13
|
+
}
|
|
14
|
+
export interface AttendancePdfFooterArgs {
|
|
15
|
+
pdf: jsPDF;
|
|
16
|
+
config: RegisterConfig;
|
|
17
|
+
page: number;
|
|
18
|
+
totalPages: number;
|
|
19
|
+
pageWidth: number;
|
|
20
|
+
pageHeight: number;
|
|
21
|
+
margin: number;
|
|
22
|
+
fontFamily: string;
|
|
23
|
+
}
|
|
24
|
+
export interface AttendancePdfOptions {
|
|
25
|
+
/** PDF font family. Defaults to jsPDF's built-in Helvetica. */
|
|
26
|
+
fontFamily?: string;
|
|
27
|
+
/** Registers a custom font before drawing. Call `pdf.addFont` here. */
|
|
28
|
+
registerFont?: (pdf: jsPDF) => void;
|
|
29
|
+
accentColor?: RGB;
|
|
30
|
+
/** Draws a branded header and returns the Y position for body content. */
|
|
31
|
+
drawHeader?: (args: AttendancePdfHeaderArgs) => number;
|
|
32
|
+
/** Draws a branded footer for one page. */
|
|
33
|
+
drawFooter?: (args: AttendancePdfFooterArgs) => void;
|
|
34
|
+
}
|
|
35
|
+
export declare function createAttendanceRegisterPdf(config: RegisterConfig, options?: AttendancePdfOptions): jsPDF;
|
|
36
|
+
export declare function downloadAttendanceRegisterPdf(config: RegisterConfig, options?: AttendancePdfOptions): void;
|
|
37
|
+
/** Backwards-compatible aliases for the original schoolkit tool API. */
|
|
38
|
+
export declare const generateAttendancePDF: typeof createAttendanceRegisterPdf;
|
|
39
|
+
export declare const downloadAttendancePDF: typeof downloadAttendanceRegisterPdf;
|
|
40
|
+
export {};
|
|
@@ -0,0 +1,425 @@
|
|
|
1
|
+
import jsPDF from "jspdf";
|
|
2
|
+
import { MONTHS, } from "../types";
|
|
3
|
+
import { getDaysInMonth, getWeekGroups } from "../domain/calendar";
|
|
4
|
+
import { formatMonthYear, isRtlLocale, needsRasterLabels } from "../domain/locale";
|
|
5
|
+
import { needsNameImage, renderNameImage, renderTextImage } from "./image-text";
|
|
6
|
+
import { PAGE_MARGIN, PDF_BOTTOM_BAND, PDF_HEADER_ROW_HEIGHT, PDF_WEEKDAY_ROW_HEIGHT, calculateColumnWidths, formatClassLabel, getAttendanceMark, getNoSchoolSet, getRegisterFilename, getRegisterSummary, getRowHeight, isNonWorkingDay, } from "../domain/register";
|
|
7
|
+
const DEFAULT_ACCENT = [245, 158, 76];
|
|
8
|
+
const GRAY_HEADER = [229, 231, 235];
|
|
9
|
+
const GRAY_HEADER_TEXT = [55, 65, 81];
|
|
10
|
+
const GRAY_CELL = [243, 244, 246];
|
|
11
|
+
const GRAY_TEXT = [107, 114, 128];
|
|
12
|
+
const BORDER = [180, 160, 140];
|
|
13
|
+
const INK = [55, 55, 55];
|
|
14
|
+
function setColor(pdf, color, kind) {
|
|
15
|
+
if (kind === "fill")
|
|
16
|
+
pdf.setFillColor(...color);
|
|
17
|
+
else if (kind === "text")
|
|
18
|
+
pdf.setTextColor(...color);
|
|
19
|
+
else
|
|
20
|
+
pdf.setDrawColor(...color);
|
|
21
|
+
}
|
|
22
|
+
function fitText(pdf, text, width) {
|
|
23
|
+
const available = Math.max(0, width - 2);
|
|
24
|
+
if (pdf.getTextWidth(text) <= available)
|
|
25
|
+
return text;
|
|
26
|
+
let value = text;
|
|
27
|
+
while (value.length > 1 && pdf.getTextWidth(`${value}…`) > available) {
|
|
28
|
+
value = value.slice(0, -1);
|
|
29
|
+
}
|
|
30
|
+
return `${value}…`;
|
|
31
|
+
}
|
|
32
|
+
function drawCell(pdf, x, y, width, height, text, options = {}) {
|
|
33
|
+
const { align = "center", bold = false, fontSize = 6, fill = [255, 255, 255], textColor = INK, fontFamily = "helvetica", } = options;
|
|
34
|
+
setColor(pdf, fill, "fill");
|
|
35
|
+
pdf.rect(x, y, width, height, "F");
|
|
36
|
+
setColor(pdf, BORDER, "draw");
|
|
37
|
+
pdf.setLineWidth(0.2);
|
|
38
|
+
pdf.rect(x, y, width, height, "S");
|
|
39
|
+
pdf.setFont(fontFamily, bold ? "bold" : "normal");
|
|
40
|
+
pdf.setFontSize(fontSize);
|
|
41
|
+
setColor(pdf, textColor, "text");
|
|
42
|
+
const value = align === "center" ? text : fitText(pdf, text, width);
|
|
43
|
+
const textX = align === "center" ? x + width / 2 : align === "right" ? x + width - 1 : x + 1;
|
|
44
|
+
const textY = y + height / 2 + fontSize * 0.22;
|
|
45
|
+
pdf.text(value, textX, textY, { align });
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* Places the serial, name, day and Present blocks. In RTL the visual order is
|
|
49
|
+
* Present | days (day 1 nearest the name) | Name | #.
|
|
50
|
+
*/
|
|
51
|
+
function getRegisterGeometry(columns, widths, startX, rtl) {
|
|
52
|
+
const serialWidth = widths.serial;
|
|
53
|
+
const nameWidth = widths.name;
|
|
54
|
+
const dayWidth = widths.day;
|
|
55
|
+
const presentWidth = widths.total;
|
|
56
|
+
const daysWidth = dayWidth * columns.length;
|
|
57
|
+
const dayColumns = rtl ? [...columns].reverse() : columns;
|
|
58
|
+
if (!rtl) {
|
|
59
|
+
const serialX = startX;
|
|
60
|
+
const nameX = serialX + serialWidth;
|
|
61
|
+
const daysX = nameX + nameWidth;
|
|
62
|
+
return {
|
|
63
|
+
serial: { x: serialX, width: serialWidth },
|
|
64
|
+
name: { x: nameX, width: nameWidth },
|
|
65
|
+
days: { x: daysX, width: dayWidth, columns: dayColumns },
|
|
66
|
+
present: { x: daysX + daysWidth, width: presentWidth },
|
|
67
|
+
};
|
|
68
|
+
}
|
|
69
|
+
const presentX = startX;
|
|
70
|
+
const daysX = presentX + presentWidth;
|
|
71
|
+
const nameX = daysX + daysWidth;
|
|
72
|
+
const serialX = nameX + nameWidth;
|
|
73
|
+
return {
|
|
74
|
+
serial: { x: serialX, width: serialWidth },
|
|
75
|
+
name: { x: nameX, width: nameWidth },
|
|
76
|
+
days: { x: daysX, width: dayWidth, columns: dayColumns },
|
|
77
|
+
present: { x: presentX, width: presentWidth },
|
|
78
|
+
};
|
|
79
|
+
}
|
|
80
|
+
function drawHeaderRows(pdf, columns, widths, startX, y, noSchool, rtl, accent, fontFamily, drawWeekdays = true) {
|
|
81
|
+
const geometry = getRegisterGeometry(columns, widths, startX, rtl);
|
|
82
|
+
drawCell(pdf, geometry.serial.x, y, geometry.serial.width, PDF_HEADER_ROW_HEIGHT, "#", {
|
|
83
|
+
fill: accent,
|
|
84
|
+
textColor: [255, 255, 255],
|
|
85
|
+
bold: true,
|
|
86
|
+
fontSize: 6.5,
|
|
87
|
+
fontFamily,
|
|
88
|
+
});
|
|
89
|
+
drawCell(pdf, geometry.name.x, y, geometry.name.width, PDF_HEADER_ROW_HEIGHT, "Student Name", {
|
|
90
|
+
align: rtl ? "right" : "left",
|
|
91
|
+
fill: accent,
|
|
92
|
+
textColor: [255, 255, 255],
|
|
93
|
+
bold: true,
|
|
94
|
+
fontSize: 6.5,
|
|
95
|
+
fontFamily,
|
|
96
|
+
});
|
|
97
|
+
geometry.days.columns.forEach((day, index) => {
|
|
98
|
+
const x = geometry.days.x + index * geometry.days.width;
|
|
99
|
+
const nonWorking = day === null || isNonWorkingDay(day, noSchool);
|
|
100
|
+
drawCell(pdf, x, y, geometry.days.width, PDF_HEADER_ROW_HEIGHT, day ? String(day.day) : "", {
|
|
101
|
+
fill: nonWorking ? GRAY_HEADER : accent,
|
|
102
|
+
textColor: nonWorking ? GRAY_HEADER_TEXT : [255, 255, 255],
|
|
103
|
+
bold: true,
|
|
104
|
+
fontSize: 6.5,
|
|
105
|
+
fontFamily,
|
|
106
|
+
});
|
|
107
|
+
});
|
|
108
|
+
drawCell(pdf, geometry.present.x, y, geometry.present.width, PDF_HEADER_ROW_HEIGHT, "Present", {
|
|
109
|
+
fill: accent,
|
|
110
|
+
textColor: [255, 255, 255],
|
|
111
|
+
bold: true,
|
|
112
|
+
fontSize: 6,
|
|
113
|
+
fontFamily,
|
|
114
|
+
});
|
|
115
|
+
const weekdayY = y + PDF_HEADER_ROW_HEIGHT;
|
|
116
|
+
drawCell(pdf, geometry.serial.x, weekdayY, geometry.serial.width, PDF_WEEKDAY_ROW_HEIGHT, "", {
|
|
117
|
+
fill: GRAY_HEADER,
|
|
118
|
+
textColor: GRAY_HEADER_TEXT,
|
|
119
|
+
fontSize: 5.5,
|
|
120
|
+
fontFamily,
|
|
121
|
+
});
|
|
122
|
+
drawCell(pdf, geometry.name.x, weekdayY, geometry.name.width, PDF_WEEKDAY_ROW_HEIGHT, "", {
|
|
123
|
+
fill: GRAY_HEADER,
|
|
124
|
+
textColor: GRAY_HEADER_TEXT,
|
|
125
|
+
fontSize: 5.5,
|
|
126
|
+
fontFamily,
|
|
127
|
+
});
|
|
128
|
+
geometry.days.columns.forEach((day, index) => {
|
|
129
|
+
var _a;
|
|
130
|
+
const x = geometry.days.x + index * geometry.days.width;
|
|
131
|
+
const nonWorking = day === null || isNonWorkingDay(day, noSchool);
|
|
132
|
+
drawCell(pdf, x, weekdayY, geometry.days.width, PDF_WEEKDAY_ROW_HEIGHT, drawWeekdays ? ((_a = day === null || day === void 0 ? void 0 : day.weekday) !== null && _a !== void 0 ? _a : "") : "", {
|
|
133
|
+
fill: nonWorking ? GRAY_HEADER : [255, 255, 255],
|
|
134
|
+
textColor: GRAY_HEADER_TEXT,
|
|
135
|
+
fontSize: 5.5,
|
|
136
|
+
fontFamily,
|
|
137
|
+
});
|
|
138
|
+
});
|
|
139
|
+
drawCell(pdf, geometry.present.x, weekdayY, geometry.present.width, PDF_WEEKDAY_ROW_HEIGHT, "", {
|
|
140
|
+
fill: GRAY_HEADER,
|
|
141
|
+
textColor: GRAY_HEADER_TEXT,
|
|
142
|
+
fontSize: 5.5,
|
|
143
|
+
fontFamily,
|
|
144
|
+
});
|
|
145
|
+
return weekdayY + PDF_WEEKDAY_ROW_HEIGHT;
|
|
146
|
+
}
|
|
147
|
+
function drawRegisterTable(pdf, config, students, columns, startX, startY, pageWidth, pageHeight, noSchool, rowHeight, accent, fontFamily, drawNames = true, drawWeekdays = true) {
|
|
148
|
+
const rtl = isRtlLocale(config.locale);
|
|
149
|
+
const widths = calculateColumnWidths(columns.length, pageWidth - PAGE_MARGIN * 2);
|
|
150
|
+
const geometry = getRegisterGeometry(columns, widths, startX, rtl);
|
|
151
|
+
const weekdayY = startY + PDF_HEADER_ROW_HEIGHT;
|
|
152
|
+
const headerBottom = drawHeaderRows(pdf, columns, widths, startX, startY, noSchool, rtl, accent, fontFamily, drawWeekdays);
|
|
153
|
+
if (!drawWeekdays) {
|
|
154
|
+
const dayWidth = geometry.days.width;
|
|
155
|
+
const image = renderTextImage({
|
|
156
|
+
runs: geometry.days.columns.map((day, index) => {
|
|
157
|
+
var _a;
|
|
158
|
+
return ({
|
|
159
|
+
text: (_a = day === null || day === void 0 ? void 0 : day.weekday) !== null && _a !== void 0 ? _a : "",
|
|
160
|
+
boxLeftMm: index * dayWidth,
|
|
161
|
+
boxWidthMm: dayWidth,
|
|
162
|
+
align: "center",
|
|
163
|
+
});
|
|
164
|
+
}),
|
|
165
|
+
widthMm: dayWidth * geometry.days.columns.length,
|
|
166
|
+
heightMm: PDF_WEEKDAY_ROW_HEIGHT,
|
|
167
|
+
fontPt: 5.5,
|
|
168
|
+
baselineMm: 3.3,
|
|
169
|
+
color: "#374151",
|
|
170
|
+
paddingMm: 0.2,
|
|
171
|
+
});
|
|
172
|
+
if (image) {
|
|
173
|
+
pdf.addImage(image, "PNG", geometry.days.x, weekdayY, dayWidth * geometry.days.columns.length, PDF_WEEKDAY_ROW_HEIGHT, undefined, "FAST");
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
const rowLimit = pageHeight - PAGE_MARGIN - PDF_BOTTOM_BAND;
|
|
177
|
+
// Only draw what actually fits; the caller paginates using the returned count
|
|
178
|
+
// instead of silently dropping the remainder.
|
|
179
|
+
const maxRows = Math.max(0, Math.floor((rowLimit - headerBottom) / rowHeight));
|
|
180
|
+
const rows = students.slice(0, maxRows);
|
|
181
|
+
let y = headerBottom;
|
|
182
|
+
rows.forEach((student, index) => {
|
|
183
|
+
drawCell(pdf, geometry.serial.x, y, geometry.serial.width, rowHeight, String(index + 1), { fontSize: 5.8, fontFamily });
|
|
184
|
+
drawCell(pdf, geometry.name.x, y, geometry.name.width, rowHeight, drawNames ? student.name : "", { align: rtl ? "right" : "left", fontSize: 6, fontFamily });
|
|
185
|
+
geometry.days.columns.forEach((day, columnIndex) => {
|
|
186
|
+
const nonWorkingCell = day === null || isNonWorkingDay(day, noSchool);
|
|
187
|
+
const blankCell = day === null;
|
|
188
|
+
const mark = day ? getAttendanceMark(config, student.id, day.day) : undefined;
|
|
189
|
+
const value = blankCell ? "" : nonWorkingCell ? "—" : (mark !== null && mark !== void 0 ? mark : "");
|
|
190
|
+
drawCell(pdf, geometry.days.x + columnIndex * geometry.days.width, y, geometry.days.width, rowHeight, value, {
|
|
191
|
+
fill: nonWorkingCell ? GRAY_CELL : [255, 255, 255],
|
|
192
|
+
textColor: nonWorkingCell ? GRAY_TEXT : INK,
|
|
193
|
+
fontSize: nonWorkingCell ? 5.5 : mark ? 6.5 : 6,
|
|
194
|
+
fontFamily,
|
|
195
|
+
});
|
|
196
|
+
});
|
|
197
|
+
drawCell(pdf, geometry.present.x, y, geometry.present.width, rowHeight, "", { fontSize: 6, fontFamily });
|
|
198
|
+
y += rowHeight;
|
|
199
|
+
});
|
|
200
|
+
return {
|
|
201
|
+
drawn: rows.length,
|
|
202
|
+
nameX: geometry.name.x,
|
|
203
|
+
nameTop: headerBottom,
|
|
204
|
+
nameWidth: geometry.name.width,
|
|
205
|
+
};
|
|
206
|
+
}
|
|
207
|
+
/**
|
|
208
|
+
* Draws the browser-rendered names over the vector table. The image is
|
|
209
|
+
* transparent, so cell fills and borders underneath stay visible.
|
|
210
|
+
*/
|
|
211
|
+
function addNameOverlay(pdf, names, table, rowHeight) {
|
|
212
|
+
try {
|
|
213
|
+
const image = renderNameImage({
|
|
214
|
+
names,
|
|
215
|
+
widthMm: table.nameWidth,
|
|
216
|
+
rowHeightMm: rowHeight,
|
|
217
|
+
});
|
|
218
|
+
if (!image)
|
|
219
|
+
return;
|
|
220
|
+
pdf.addImage(image, "PNG", table.nameX, table.nameTop, table.nameWidth, table.drawn * rowHeight, undefined, "FAST");
|
|
221
|
+
}
|
|
222
|
+
catch (error) {
|
|
223
|
+
console.error("Attendance name image failed; names may be missing.", error);
|
|
224
|
+
}
|
|
225
|
+
}
|
|
226
|
+
function drawKey(pdf, pageHeight, margin, pageWidth, rtl, fontFamily, keyText) {
|
|
227
|
+
pdf.setFont(fontFamily, "normal");
|
|
228
|
+
pdf.setFontSize(6.5);
|
|
229
|
+
setColor(pdf, GRAY_TEXT, "text");
|
|
230
|
+
pdf.text(keyText, rtl ? pageWidth - margin : margin, pageHeight - margin - 17, {
|
|
231
|
+
align: rtl ? "right" : "left",
|
|
232
|
+
});
|
|
233
|
+
}
|
|
234
|
+
function drawSummary(pdf, config, pageWidth, pageHeight, margin, rtl, fontFamily) {
|
|
235
|
+
const summary = getRegisterSummary(config);
|
|
236
|
+
const y = pageHeight - margin - 27;
|
|
237
|
+
const x = rtl ? pageWidth - margin : margin;
|
|
238
|
+
const align = rtl ? "right" : "left";
|
|
239
|
+
setColor(pdf, [220, 200, 180], "draw");
|
|
240
|
+
pdf.setLineWidth(0.3);
|
|
241
|
+
pdf.line(margin, y, pageWidth - margin, y);
|
|
242
|
+
pdf.setFont(fontFamily, "bold");
|
|
243
|
+
pdf.setFontSize(7.5);
|
|
244
|
+
setColor(pdf, INK, "text");
|
|
245
|
+
pdf.text(`Students: ${summary.totalStudents} | Days: ${summary.totalDays} | Working days: ${summary.workingDays} | Weekends: ${summary.weekends} | No school: ${summary.noSchoolDays}`, x, y + 5, { align });
|
|
246
|
+
pdf.setFont(fontFamily, "normal");
|
|
247
|
+
pdf.setFontSize(6.5);
|
|
248
|
+
setColor(pdf, GRAY_TEXT, "text");
|
|
249
|
+
pdf.text("Class Teacher: ______________________________ Checked by: _____________________________", x, y + 15, { align });
|
|
250
|
+
}
|
|
251
|
+
function drawRasterText(pdf, text, x, top, width, height, fontPt, baseline, color, align = "left") {
|
|
252
|
+
const image = renderTextImage({
|
|
253
|
+
runs: [{ text, boxLeftMm: 0, boxWidthMm: width, align }],
|
|
254
|
+
widthMm: width,
|
|
255
|
+
heightMm: height,
|
|
256
|
+
fontPt,
|
|
257
|
+
baselineMm: baseline,
|
|
258
|
+
color,
|
|
259
|
+
});
|
|
260
|
+
if (!image)
|
|
261
|
+
return false;
|
|
262
|
+
pdf.addImage(image, "PNG", x, top, width, height, undefined, "FAST");
|
|
263
|
+
return true;
|
|
264
|
+
}
|
|
265
|
+
function drawDefaultHeader({ config, pdf, week, pageWidth, margin, fontFamily, accentColor, }) {
|
|
266
|
+
const monthLabel = formatMonthYear(MONTHS.indexOf(config.month), config.year, config.locale);
|
|
267
|
+
const toolName = week ? "Weekly Attendance Register" : "Monthly Attendance Register";
|
|
268
|
+
const meta = week
|
|
269
|
+
? `Week ${week.weekNumber}: ${week.startDay}–${week.endDay} ${monthLabel} | Class: ${formatClassLabel(config)}`
|
|
270
|
+
: `Class: ${formatClassLabel(config)} | ${monthLabel}`;
|
|
271
|
+
const raster = needsRasterLabels(config.locale);
|
|
272
|
+
let y = margin + 2;
|
|
273
|
+
if (!raster) {
|
|
274
|
+
pdf.setFont(fontFamily, "bold");
|
|
275
|
+
pdf.setFontSize(14);
|
|
276
|
+
setColor(pdf, accentColor, "text");
|
|
277
|
+
pdf.text(config.schoolName || "School Name", margin, y);
|
|
278
|
+
pdf.setFont(fontFamily, "normal");
|
|
279
|
+
pdf.setFontSize(9);
|
|
280
|
+
setColor(pdf, GRAY_TEXT, "text");
|
|
281
|
+
pdf.text(toolName, margin, y + 6);
|
|
282
|
+
pdf.setFontSize(8);
|
|
283
|
+
pdf.text(meta, margin, y + 10.5);
|
|
284
|
+
y += 14.5;
|
|
285
|
+
}
|
|
286
|
+
else {
|
|
287
|
+
drawRasterText(pdf, config.schoolName || "School Name", margin, y - 3, pageWidth - margin * 2, 6, 14, 4.8, "#F59E4C", "left");
|
|
288
|
+
drawRasterText(pdf, toolName, margin, y + 3, pageWidth - margin * 2, 4, 9, 3.2, "#6B7280", "left");
|
|
289
|
+
drawRasterText(pdf, meta, margin, y + 7.5, pageWidth - margin * 2, 4, 8, 3.2, "#9CA3AF", "left");
|
|
290
|
+
y += 14.5;
|
|
291
|
+
}
|
|
292
|
+
setColor(pdf, [229, 231, 235], "draw");
|
|
293
|
+
pdf.setLineWidth(0.3);
|
|
294
|
+
pdf.line(margin, y, pageWidth - margin, y);
|
|
295
|
+
return y + 3;
|
|
296
|
+
}
|
|
297
|
+
function drawDefaultFooter({ pdf, config, page, totalPages, pageWidth, pageHeight, margin, fontFamily, }) {
|
|
298
|
+
const dateStr = new Date().toLocaleDateString("en-US", {
|
|
299
|
+
year: "numeric",
|
|
300
|
+
month: "short",
|
|
301
|
+
day: "numeric",
|
|
302
|
+
});
|
|
303
|
+
const y = pageHeight - 8;
|
|
304
|
+
setColor(pdf, [229, 231, 235], "draw");
|
|
305
|
+
pdf.setLineWidth(0.2);
|
|
306
|
+
pdf.line(margin, y - 3, pageWidth - margin, y - 3);
|
|
307
|
+
pdf.setFont(fontFamily, "normal");
|
|
308
|
+
pdf.setFontSize(6.5);
|
|
309
|
+
setColor(pdf, GRAY_TEXT, "text");
|
|
310
|
+
pdf.text("Attendance Register", margin, y);
|
|
311
|
+
pdf.text(`Page ${page} of ${totalPages} | ${dateStr} | ${config.schoolName || "School"}`, pageWidth - margin, y, { align: "right" });
|
|
312
|
+
}
|
|
313
|
+
export function createAttendanceRegisterPdf(config, options = {}) {
|
|
314
|
+
const { fontFamily = "helvetica", registerFont, accentColor = DEFAULT_ACCENT, drawHeader, drawFooter, } = options;
|
|
315
|
+
const isMonthly = config.layout === "monthly";
|
|
316
|
+
const pdf = new jsPDF({
|
|
317
|
+
orientation: isMonthly ? "landscape" : "portrait",
|
|
318
|
+
format: config.format,
|
|
319
|
+
unit: "mm",
|
|
320
|
+
});
|
|
321
|
+
registerFont === null || registerFont === void 0 ? void 0 : registerFont(pdf);
|
|
322
|
+
const pageWidth = pdf.internal.pageSize.getWidth();
|
|
323
|
+
const pageHeight = pdf.internal.pageSize.getHeight();
|
|
324
|
+
const margin = PAGE_MARGIN;
|
|
325
|
+
const days = getDaysInMonth(config.month, config.year, config.weekendDays, config.locale);
|
|
326
|
+
const weeks = getWeekGroups(config.month, config.year, config.weekStartsOn, config.weekendDays, config.locale);
|
|
327
|
+
const noSchool = getNoSchoolSet(config);
|
|
328
|
+
const rowHeight = getRowHeight(config.density);
|
|
329
|
+
const students = config.students;
|
|
330
|
+
// Complex scripts are rendered by the browser and overlaid as an image.
|
|
331
|
+
const useNameImage = needsNameImage(students);
|
|
332
|
+
const rasterLabels = needsRasterLabels(config.locale);
|
|
333
|
+
const rtlLayout = isRtlLocale(config.locale);
|
|
334
|
+
const keyText = config.attendance
|
|
335
|
+
? "Key: P = Present A = Absent L = Late E = Excused H = Half day — = Weekend / No school"
|
|
336
|
+
: "Key: P = Present A = Absent L = Late — = Weekend / No school";
|
|
337
|
+
const header = drawHeader !== null && drawHeader !== void 0 ? drawHeader : drawDefaultHeader;
|
|
338
|
+
const footer = drawFooter !== null && drawFooter !== void 0 ? drawFooter : drawDefaultFooter;
|
|
339
|
+
// Pagination is driven by what the table actually drew. A new page is added
|
|
340
|
+
// only while students remain, so nobody can be silently truncated.
|
|
341
|
+
if (isMonthly) {
|
|
342
|
+
let cursor = 0;
|
|
343
|
+
let pageIndex = 0;
|
|
344
|
+
do {
|
|
345
|
+
if (pageIndex > 0)
|
|
346
|
+
pdf.addPage();
|
|
347
|
+
const headerY = header({
|
|
348
|
+
pdf,
|
|
349
|
+
config,
|
|
350
|
+
pageWidth,
|
|
351
|
+
pageHeight,
|
|
352
|
+
margin,
|
|
353
|
+
fontFamily,
|
|
354
|
+
accentColor,
|
|
355
|
+
});
|
|
356
|
+
const pageStart = cursor;
|
|
357
|
+
const table = drawRegisterTable(pdf, config, students.slice(cursor), days, margin, headerY + 2, pageWidth, pageHeight, noSchool, rowHeight, accentColor, fontFamily, !useNameImage, !rasterLabels);
|
|
358
|
+
cursor += table.drawn;
|
|
359
|
+
if (useNameImage && table.drawn > 0) {
|
|
360
|
+
addNameOverlay(pdf, students.slice(pageStart, cursor), table, rowHeight);
|
|
361
|
+
}
|
|
362
|
+
drawKey(pdf, pageHeight, margin, pageWidth, rtlLayout, fontFamily, keyText);
|
|
363
|
+
pageIndex += 1;
|
|
364
|
+
if (table.drawn === 0)
|
|
365
|
+
break;
|
|
366
|
+
} while (cursor < students.length);
|
|
367
|
+
}
|
|
368
|
+
else {
|
|
369
|
+
let isFirstWeek = true;
|
|
370
|
+
for (const week of weeks) {
|
|
371
|
+
if (!isFirstWeek)
|
|
372
|
+
pdf.addPage();
|
|
373
|
+
isFirstWeek = false;
|
|
374
|
+
let cursor = 0;
|
|
375
|
+
let isFirstPageOfWeek = true;
|
|
376
|
+
do {
|
|
377
|
+
if (!isFirstPageOfWeek)
|
|
378
|
+
pdf.addPage();
|
|
379
|
+
const headerY = header({
|
|
380
|
+
pdf,
|
|
381
|
+
config,
|
|
382
|
+
week,
|
|
383
|
+
pageWidth,
|
|
384
|
+
pageHeight,
|
|
385
|
+
margin,
|
|
386
|
+
fontFamily,
|
|
387
|
+
accentColor,
|
|
388
|
+
});
|
|
389
|
+
const pageStart = cursor;
|
|
390
|
+
const table = drawRegisterTable(pdf, config, students.slice(cursor), week.slots, margin, headerY + 2, pageWidth, pageHeight, noSchool, rowHeight, accentColor, fontFamily, !useNameImage, !rasterLabels);
|
|
391
|
+
cursor += table.drawn;
|
|
392
|
+
if (useNameImage && table.drawn > 0) {
|
|
393
|
+
addNameOverlay(pdf, students.slice(pageStart, cursor), table, rowHeight);
|
|
394
|
+
}
|
|
395
|
+
isFirstPageOfWeek = false;
|
|
396
|
+
drawKey(pdf, pageHeight, margin, pageWidth, rtlLayout, fontFamily, keyText);
|
|
397
|
+
if (table.drawn === 0)
|
|
398
|
+
break;
|
|
399
|
+
} while (cursor < students.length);
|
|
400
|
+
}
|
|
401
|
+
}
|
|
402
|
+
const totalPages = pdf.getNumberOfPages();
|
|
403
|
+
pdf.setPage(totalPages);
|
|
404
|
+
drawSummary(pdf, config, pageWidth, pageHeight, margin, rtlLayout, fontFamily);
|
|
405
|
+
for (let page = 1; page <= totalPages; page += 1) {
|
|
406
|
+
pdf.setPage(page);
|
|
407
|
+
footer({
|
|
408
|
+
pdf,
|
|
409
|
+
config,
|
|
410
|
+
page,
|
|
411
|
+
totalPages,
|
|
412
|
+
pageWidth,
|
|
413
|
+
pageHeight,
|
|
414
|
+
margin,
|
|
415
|
+
fontFamily,
|
|
416
|
+
});
|
|
417
|
+
}
|
|
418
|
+
return pdf;
|
|
419
|
+
}
|
|
420
|
+
export function downloadAttendanceRegisterPdf(config, options = {}) {
|
|
421
|
+
createAttendanceRegisterPdf(config, options).save(getRegisterFilename(config, "pdf"));
|
|
422
|
+
}
|
|
423
|
+
/** Backwards-compatible aliases for the original schoolkit tool API. */
|
|
424
|
+
export const generateAttendancePDF = createAttendanceRegisterPdf;
|
|
425
|
+
export const downloadAttendancePDF = downloadAttendanceRegisterPdf;
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { RegisterConfig } from "../types";
|
|
2
|
+
import { AttendancePdfOptions } from "./pdf";
|
|
3
|
+
export interface RegisterZipResult {
|
|
4
|
+
blob: Blob;
|
|
5
|
+
filename: string;
|
|
6
|
+
fileCount: number;
|
|
7
|
+
}
|
|
8
|
+
/**
|
|
9
|
+
* Generates one PDF per config and returns a ZIP blob. Both apps use this for
|
|
10
|
+
* "download every class" batch exports.
|
|
11
|
+
*/
|
|
12
|
+
export declare function zipRegisterPdfs(configs: RegisterConfig[], options?: AttendancePdfOptions): RegisterZipResult;
|
|
13
|
+
export declare function downloadRegisterZip(configs: RegisterConfig[], options?: AttendancePdfOptions): RegisterZipResult;
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { getBatchFilename, getRegisterFilename } from "../domain/register";
|
|
2
|
+
import { createAttendanceRegisterPdf, } from "./pdf";
|
|
3
|
+
import { downloadBlob } from "./download";
|
|
4
|
+
import { zipFiles } from "./zip-files";
|
|
5
|
+
/**
|
|
6
|
+
* Generates one PDF per config and returns a ZIP blob. Both apps use this for
|
|
7
|
+
* "download every class" batch exports.
|
|
8
|
+
*/
|
|
9
|
+
export function zipRegisterPdfs(configs, options = {}) {
|
|
10
|
+
const files = {};
|
|
11
|
+
for (const config of configs) {
|
|
12
|
+
const pdf = createAttendanceRegisterPdf(config, options);
|
|
13
|
+
files[getRegisterFilename(config, "pdf")] = new Uint8Array(pdf.output("arraybuffer"));
|
|
14
|
+
}
|
|
15
|
+
const first = configs[0];
|
|
16
|
+
const filename = first ? getBatchFilename(first.month, first.year) : "attendance_registers.zip";
|
|
17
|
+
const zipped = zipFiles(files);
|
|
18
|
+
const bytes = new Uint8Array(zipped);
|
|
19
|
+
return {
|
|
20
|
+
blob: new Blob([bytes.buffer], { type: "application/zip" }),
|
|
21
|
+
filename,
|
|
22
|
+
fileCount: configs.length,
|
|
23
|
+
};
|
|
24
|
+
}
|
|
25
|
+
export function downloadRegisterZip(configs, options = {}) {
|
|
26
|
+
const result = zipRegisterPdfs(configs, options);
|
|
27
|
+
downloadBlob(result.blob, result.filename);
|
|
28
|
+
return result;
|
|
29
|
+
}
|