x4js 2.0.35 → 2.1.0-manual
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/README.md +21 -21
- package/package.json +39 -26
- package/src/components/base.scss +25 -89
- package/src/components/boxes/boxes.module.scss +54 -54
- package/src/components/boxes/boxes.ts +513 -513
- package/src/components/breadcrumb/breadcrumb.scss +56 -56
- package/src/components/breadcrumb/breadcrumb.ts +93 -93
- package/src/components/btngroup/btngroup.module.scss +40 -40
- package/src/components/btngroup/btngroup.ts +152 -152
- package/src/components/button/button.module.scss +172 -172
- package/src/components/button/button.ts +232 -232
- package/src/components/calendar/calendar.module.scss +162 -162
- package/src/components/calendar/calendar.ts +326 -326
- package/src/components/canvas/canvas.module.scss +24 -24
- package/src/components/canvas/canvas.ts +195 -195
- package/src/components/canvas/canvas_ex.ts +275 -275
- package/src/components/checkbox/check.svg +3 -3
- package/src/components/checkbox/checkbox.module.scss +141 -141
- package/src/components/checkbox/checkbox.ts +139 -139
- package/src/components/colorinput/colorinput.module.scss +64 -64
- package/src/components/colorinput/colorinput.ts +90 -90
- package/src/components/colorpicker/colorpicker.module.scss +132 -132
- package/src/components/colorpicker/colorpicker.ts +481 -481
- package/src/components/combobox/combobox.module.scss +145 -145
- package/src/components/combobox/combobox.ts +282 -282
- package/src/components/combobox/updown.svg +3 -3
- package/src/components/components.ts +45 -44
- package/src/components/dialog/dialog.module.scss +103 -105
- package/src/components/dialog/dialog.ts +233 -233
- package/src/components/filedrop/filedrop.module.scss +69 -69
- package/src/components/filedrop/filedrop.ts +130 -130
- package/src/components/form/form.module.scss +38 -38
- package/src/components/form/form.ts +172 -172
- package/src/components/gridview/gridview.module.scss +323 -337
- package/src/components/gridview/gridview.ts +1276 -1316
- package/src/components/header/header.module.scss +40 -40
- package/src/components/header/header.ts +141 -141
- package/src/components/icon/icon.module.scss +32 -32
- package/src/components/icon/icon.ts +165 -165
- package/src/components/image/image.module.scss +27 -27
- package/src/components/image/image.ts +168 -168
- package/src/components/input/input.module.scss +74 -74
- package/src/components/input/input.ts +537 -537
- package/src/components/keyboard/keyboard.module.scss +136 -136
- package/src/components/keyboard/keyboard.ts +549 -549
- package/src/components/label/label.module.scss +90 -91
- package/src/components/label/label.ts +101 -101
- package/src/components/link/link.module.scss +44 -44
- package/src/components/link/link.ts +87 -87
- package/src/components/listbox/listbox.module.scss +179 -179
- package/src/components/listbox/listbox.ts +596 -596
- package/src/components/menu/menu.module.scss +128 -128
- package/src/components/menu/menu.ts +174 -174
- package/src/components/messages/messages.module.scss +92 -146
- package/src/components/messages/messages.ts +237 -303
- package/src/components/normalize.scss +391 -391
- package/src/components/notification/notification.module.scss +83 -83
- package/src/components/notification/notification.ts +107 -107
- package/src/components/panel/panel.module.scss +66 -71
- package/src/components/panel/panel.ts +57 -57
- package/src/components/popup/popup.module.scss +51 -51
- package/src/components/popup/popup.ts +457 -457
- package/src/components/progress/progress.module.scss +56 -56
- package/src/components/progress/progress.ts +43 -43
- package/src/components/propgrid/progrid.module.scss +111 -111
- package/src/components/propgrid/propgrid.ts +300 -300
- package/src/components/propgrid/updown.svg +3 -3
- package/src/components/radio/radio.module.scss +163 -163
- package/src/components/radio/radio.svg +3 -3
- package/src/components/radio/radio.ts +141 -141
- package/src/components/rating/rating.module.scss +22 -22
- package/src/components/rating/rating.ts +131 -131
- package/src/components/select/select.module.scss +8 -8
- package/src/components/select/select.ts +134 -134
- package/src/components/shared.scss +141 -71
- package/src/components/sizers/sizer.module.scss +90 -112
- package/src/components/sizers/sizer.ts +131 -155
- package/src/components/slider/slider.module.scss +117 -117
- package/src/components/slider/slider.ts +197 -197
- package/src/components/spreadsheet/spreadsheet.module.scss +307 -307
- package/src/components/spreadsheet/spreadsheet.ts +1223 -1223
- package/src/components/switch/switch.module.scss +126 -126
- package/src/components/switch/switch.ts +61 -61
- package/src/components/tabs/tabs.module.scss +46 -67
- package/src/components/tabs/tabs.ts +229 -234
- package/src/components/textarea/textarea.module.scss +63 -63
- package/src/components/textarea/textarea.ts +131 -131
- package/src/components/textedit/textedit.module.scss +115 -115
- package/src/components/textedit/textedit.ts +122 -122
- package/src/components/themes.scss +90 -90
- package/src/components/tickline/tickline.module.scss +25 -25
- package/src/components/tickline/tickline.ts +81 -81
- package/src/components/tooltips/tooltips.scss +71 -71
- package/src/components/tooltips/tooltips.ts +120 -120
- package/src/components/treeview/treeview.module.scss +192 -192
- package/src/components/treeview/treeview.ts +484 -484
- package/src/components/viewport/viewport.module.scss +31 -31
- package/src/components/viewport/viewport.ts +41 -41
- package/src/core/component.ts +1299 -1299
- package/src/core/core_application.ts +361 -361
- package/src/core/core_colors.ts +512 -512
- package/src/core/core_data.ts +1297 -1310
- package/src/core/core_dom.ts +481 -481
- package/src/core/core_dragdrop.ts +225 -225
- package/src/core/core_element.ts +221 -221
- package/src/core/core_events.ts +214 -214
- package/src/core/core_i18n.ts +395 -395
- package/src/core/core_pdf.ts +454 -454
- package/src/core/core_react.ts +78 -78
- package/src/core/core_router.ts +296 -296
- package/src/core/core_state.ts +62 -62
- package/src/core/core_styles.ts +213 -213
- package/src/core/core_svg.ts +1042 -1042
- package/src/core/core_tools.ts +996 -996
- package/src/types/scss.d.ts +4 -4
- package/src/types/x4react.d.ts +8 -8
- package/src/x4.scss +19 -19
- package/src/x4.ts +36 -36
- package/src/x4tsx.d.ts +26 -26
- package/.vscode/launch.json +0 -14
- package/.vscode/settings.json +0 -2
- package/demo/assets/house-light.svg +0 -1
- package/demo/assets/radio.svg +0 -4
- package/demo/index.html +0 -12
- package/demo/main.scss +0 -23
- package/demo/main.ts +0 -324
- package/demo/package.json +0 -26
- package/demo/scss.d.ts +0 -4
- package/demo/svg.d.ts +0 -1
- package/demo/tsconfig.json +0 -14
- package/src/components/gridview/folder-open.svg +0 -1
- package/src/components/messages/spinner.svg +0 -1
- package/src/x4.d.ts +0 -10
- package/tsconfig.json +0 -11
package/src/core/core_pdf.ts
CHANGED
|
@@ -1,454 +1,454 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* ___ ___ __
|
|
3
|
-
* \ \/ / / _
|
|
4
|
-
* \ / /_| |_
|
|
5
|
-
* / \____ _|
|
|
6
|
-
* /__/\__\ |_|.2
|
|
7
|
-
*
|
|
8
|
-
* @file core_pdf.ts
|
|
9
|
-
* @author Etienne Cochard
|
|
10
|
-
*
|
|
11
|
-
* @copyright (c) 2026 R-libre ingenierie
|
|
12
|
-
*
|
|
13
|
-
* Use of this source code is governed by an MIT-style license
|
|
14
|
-
* that can be found in the LICENSE file or at https://opensource.org/licenses/MIT.
|
|
15
|
-
*
|
|
16
|
-
* taken from excellent
|
|
17
|
-
* https://github.com/Lulzx/tinypdf/tree/main
|
|
18
|
-
**/
|
|
19
|
-
|
|
20
|
-
import { Color } from './core_colors.js'
|
|
21
|
-
import { isArray, isFunction, isNumber, isPlainObject, isString } from './core_tools.js'
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
const WIDTHS: number[] = [
|
|
25
|
-
278, 278, 355, 556, 556, 889, 667, 191, 333, 333, 389, 584, 278, 333, 278, 278,
|
|
26
|
-
556, 556, 556, 556, 556, 556, 556, 556, 556, 556, 278, 278, 584, 584, 584, 556,
|
|
27
|
-
1015, 667, 667, 722, 722, 667, 611, 778, 722, 278, 500, 667, 556, 833, 722, 778,
|
|
28
|
-
667, 778, 722, 667, 611, 722, 667, 944, 667, 667, 611, 278, 278, 278, 469, 556,
|
|
29
|
-
333, 556, 556, 500, 556, 556, 278, 556, 556, 222, 222, 500, 222, 833, 556, 556,
|
|
30
|
-
556, 556, 333, 500, 278, 556, 500, 722, 500, 500, 500, 334, 260, 334, 584
|
|
31
|
-
]
|
|
32
|
-
|
|
33
|
-
export interface TextOptions {
|
|
34
|
-
align?: 'left' | 'center' | 'right'
|
|
35
|
-
width?: number
|
|
36
|
-
color?: string
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
export interface LinkOptions {
|
|
40
|
-
underline?: string
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
type PDFValue = null | boolean | number | string | PDFValue[] | Ref | { [key: string]: PDFValue }
|
|
44
|
-
|
|
45
|
-
interface PDFObject {
|
|
46
|
-
id: number
|
|
47
|
-
dict: Record<string, PDFValue>
|
|
48
|
-
stream: Uint8Array | null
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
export function measureText(str: string, size: number): number {
|
|
52
|
-
let width = 0
|
|
53
|
-
for (let i = 0; i < str.length; i++) {
|
|
54
|
-
const code = str.charCodeAt(i)
|
|
55
|
-
const w = (code >= 32 && code <= 126) ? WIDTHS[code - 32] : 556
|
|
56
|
-
width += w
|
|
57
|
-
}
|
|
58
|
-
return (width * size) / 1000
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
function fx( v:number, n:number ) {
|
|
62
|
-
return (+v.toFixed(n)).toString();
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
function formatColor( clr: Color, suffix: string ) : string {
|
|
66
|
-
const rgb = clr.toNumber( );
|
|
67
|
-
return `${fx((rgb>>16)&0xff,3)} ${fx((rgb>>8)&0xff,3)} ${fx(rgb&0xff,3)} ${suffix}`;
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
const STRING_MAP: Record<string,string> = {
|
|
72
|
-
'\\': '\\\\',
|
|
73
|
-
'(': '\\(',
|
|
74
|
-
')': '\\)',
|
|
75
|
-
'\r': '\\r',
|
|
76
|
-
'\n': '\\n'
|
|
77
|
-
};
|
|
78
|
-
|
|
79
|
-
function pdfString(str: string): string {
|
|
80
|
-
return '(' + str.replace( /[\\()\r\n]/g, s => STRING_MAP[s]) + ')';
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
function serialize(val: PDFValue): string {
|
|
84
|
-
if (val === null || val === undefined) {
|
|
85
|
-
return 'null'
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
if ( typeof val === 'boolean') {
|
|
89
|
-
return val ? 'true' : 'false'
|
|
90
|
-
}
|
|
91
|
-
|
|
92
|
-
if ( isNumber( val ) ) {
|
|
93
|
-
return fx(val,4);
|
|
94
|
-
}
|
|
95
|
-
|
|
96
|
-
if ( isString(val) ) {
|
|
97
|
-
if (val.startsWith('/')) {
|
|
98
|
-
return val // name
|
|
99
|
-
}
|
|
100
|
-
|
|
101
|
-
if (val.startsWith('(')) {
|
|
102
|
-
return val // already escaped string
|
|
103
|
-
}
|
|
104
|
-
|
|
105
|
-
return pdfString(val)
|
|
106
|
-
}
|
|
107
|
-
|
|
108
|
-
if (isArray(val)) {
|
|
109
|
-
return '[' + val.map(serialize).join(' ') + ']'
|
|
110
|
-
}
|
|
111
|
-
|
|
112
|
-
if (val instanceof Ref) {
|
|
113
|
-
return `${val.id} 0 R`
|
|
114
|
-
}
|
|
115
|
-
|
|
116
|
-
if ( isPlainObject(val) ) {
|
|
117
|
-
const pairs = Object.entries(val)
|
|
118
|
-
.filter( ([_, v]) => v !== undefined)
|
|
119
|
-
.map( ([k, v]) => `/${k} ${serialize(v as PDFValue)}`);
|
|
120
|
-
|
|
121
|
-
return '<<\n' + pairs.join('\n') + '\n>>'
|
|
122
|
-
}
|
|
123
|
-
|
|
124
|
-
return String(val)
|
|
125
|
-
}
|
|
126
|
-
|
|
127
|
-
class Ref {
|
|
128
|
-
id: number
|
|
129
|
-
constructor(id: number) { this.id = id }
|
|
130
|
-
}
|
|
131
|
-
|
|
132
|
-
interface PageContext {
|
|
133
|
-
text(str: string, x: number, y: number, size: number, opts?: TextOptions): void;
|
|
134
|
-
rect(x: number, y: number, w: number, h: number, fill: string): void;
|
|
135
|
-
line(x1: number, y1: number, x2: number, y2: number, stroke: string, lineWidth?: number): void;
|
|
136
|
-
image(jpegBytes: Uint8Array, x: number, y: number, w: number, h: number): void;
|
|
137
|
-
link(url: string, x: number, y: number, w: number, h: number, opts?: LinkOptions): void;
|
|
138
|
-
}
|
|
139
|
-
|
|
140
|
-
export class X4PDFBuilder {
|
|
141
|
-
|
|
142
|
-
private objects: PDFObject[] = [];
|
|
143
|
-
private pages: Ref[] = [];
|
|
144
|
-
private nextId = 1;
|
|
145
|
-
|
|
146
|
-
private _addObject(dict: Record<string, PDFValue>, streamBytes: Uint8Array = null): Ref {
|
|
147
|
-
const id = this.nextId++;
|
|
148
|
-
this.objects.push({ id, dict, stream: streamBytes });
|
|
149
|
-
return new Ref(id);
|
|
150
|
-
}
|
|
151
|
-
|
|
152
|
-
page( width: number, height: number, callback: (ctx: PageContext) => void ): void;
|
|
153
|
-
page( callback: (ctx: PageContext) => void ): void;
|
|
154
|
-
page( param1: number | ((ctx: PageContext) => void), param2?: number, param3?: (ctx: PageContext) => void ): void {
|
|
155
|
-
const ops: string[] = [];
|
|
156
|
-
const images: { name: string; ref: Ref }[] = [];
|
|
157
|
-
const links: { url: string; rect: number[] }[] = [];
|
|
158
|
-
let imageCount = 0;
|
|
159
|
-
|
|
160
|
-
const text = (str: string, x: number, y: number, size: number, opts: TextOptions = {}) => {
|
|
161
|
-
const { align = 'left', width: boxWidth, color = '#000000' } = opts;
|
|
162
|
-
|
|
163
|
-
let tx = x;
|
|
164
|
-
if (align !== 'left' && boxWidth !== undefined) {
|
|
165
|
-
const textWidth = measureText(str, size);
|
|
166
|
-
if (align === 'center') {
|
|
167
|
-
tx = x + (boxWidth - textWidth) / 2;
|
|
168
|
-
}
|
|
169
|
-
|
|
170
|
-
if (align === 'right') {
|
|
171
|
-
tx = x + boxWidth - textWidth;
|
|
172
|
-
}
|
|
173
|
-
}
|
|
174
|
-
|
|
175
|
-
const rgb = new Color(color);
|
|
176
|
-
if (rgb) {
|
|
177
|
-
ops.push( formatColor(rgb, 'rg') );
|
|
178
|
-
}
|
|
179
|
-
|
|
180
|
-
ops.push('BT')
|
|
181
|
-
ops.push(`/F1 ${size} Tf`)
|
|
182
|
-
ops.push(`${fx(tx,2)} ${fx(y,2)} Td`)
|
|
183
|
-
ops.push(`${str.startsWith('(') ? str : pdfString(str)} Tj`)
|
|
184
|
-
ops.push('ET')
|
|
185
|
-
}
|
|
186
|
-
|
|
187
|
-
const rect = (x: number, y: number, w: number, h: number, fill: string) => {
|
|
188
|
-
const rgb = new Color(fill)
|
|
189
|
-
if (rgb) {
|
|
190
|
-
ops.push( formatColor(rgb, 'rg' ) );
|
|
191
|
-
ops.push(`${fx(x,2)} ${fx(y,2)} ${fx(w,2)} ${fx(h,2)} re`);
|
|
192
|
-
ops.push('f');
|
|
193
|
-
}
|
|
194
|
-
}
|
|
195
|
-
|
|
196
|
-
const line = (x1: number, y1: number, x2: number, y2: number, stroke: string, lineWidth: number = 1) => {
|
|
197
|
-
const rgb = new Color(stroke)
|
|
198
|
-
if (rgb) {
|
|
199
|
-
ops.push(`${fx(lineWidth,2)} w`);
|
|
200
|
-
ops.push( formatColor( rgb,'RG') );
|
|
201
|
-
ops.push(`${fx(x1,2)} ${fx(y1,2)} m`);
|
|
202
|
-
ops.push(`${fx(x2,2)} ${fx(y2,2)} l`);
|
|
203
|
-
ops.push('S')
|
|
204
|
-
}
|
|
205
|
-
}
|
|
206
|
-
|
|
207
|
-
const image = (jpegBytes: Uint8Array, x: number, y: number, w: number, h: number) => {
|
|
208
|
-
let imgWidth = 0, imgHeight = 0
|
|
209
|
-
for (let i = 0; i < jpegBytes.length - 1; i++) {
|
|
210
|
-
if (jpegBytes[i] === 0xFF && (jpegBytes[i + 1] === 0xC0 || jpegBytes[i + 1] === 0xC2)) {
|
|
211
|
-
imgHeight = (jpegBytes[i + 5] << 8) | jpegBytes[i + 6]
|
|
212
|
-
imgWidth = (jpegBytes[i + 7] << 8) | jpegBytes[i + 8]
|
|
213
|
-
break
|
|
214
|
-
}
|
|
215
|
-
}
|
|
216
|
-
|
|
217
|
-
const imgName = `/Im${imageCount++}`
|
|
218
|
-
const imgRef = this._addObject({
|
|
219
|
-
Type: '/XObject',
|
|
220
|
-
Subtype: '/Image',
|
|
221
|
-
Width: imgWidth,
|
|
222
|
-
Height: imgHeight,
|
|
223
|
-
ColorSpace: '/DeviceRGB',
|
|
224
|
-
BitsPerComponent: 8,
|
|
225
|
-
Filter: '/DCTDecode',
|
|
226
|
-
Length: jpegBytes.length
|
|
227
|
-
}, jpegBytes)
|
|
228
|
-
|
|
229
|
-
images.push({ name: imgName, ref: imgRef })
|
|
230
|
-
|
|
231
|
-
ops.push('q')
|
|
232
|
-
ops.push(`${fx(w,2)} 0 0 ${fx(h,2)} ${fx(x,2)} ${fx(y,2)} cm`)
|
|
233
|
-
ops.push(`${imgName} Do`)
|
|
234
|
-
ops.push('Q')
|
|
235
|
-
}
|
|
236
|
-
|
|
237
|
-
const link = (url: string, x: number, y: number, w: number, h: number, opts: LinkOptions = {}) => {
|
|
238
|
-
links.push({ url, rect: [x, y, x + w, y + h] })
|
|
239
|
-
if (opts.underline) {
|
|
240
|
-
const rgb = new Color(opts.underline)
|
|
241
|
-
if (rgb) {
|
|
242
|
-
ops.push(`0.75 w`)
|
|
243
|
-
ops.push( formatColor(rgb, 'RG' ) );
|
|
244
|
-
ops.push(`${fx(x,2)} ${fx(y + 2,2)} m`);
|
|
245
|
-
ops.push(`${fx(x + w,2)} ${fx(y + 2,2)} l`);
|
|
246
|
-
ops.push('S')
|
|
247
|
-
}
|
|
248
|
-
}
|
|
249
|
-
}
|
|
250
|
-
|
|
251
|
-
const ctx: PageContext = {
|
|
252
|
-
text,
|
|
253
|
-
rect,
|
|
254
|
-
line,
|
|
255
|
-
image,
|
|
256
|
-
link
|
|
257
|
-
}
|
|
258
|
-
|
|
259
|
-
let width: number
|
|
260
|
-
let height: number
|
|
261
|
-
|
|
262
|
-
if ( isFunction( param1) ) {
|
|
263
|
-
width = 612
|
|
264
|
-
height = 792
|
|
265
|
-
param1( ctx );
|
|
266
|
-
}
|
|
267
|
-
else {
|
|
268
|
-
width = param1;
|
|
269
|
-
height = param2;
|
|
270
|
-
param3( ctx );
|
|
271
|
-
}
|
|
272
|
-
|
|
273
|
-
const content = ops.join('\n')
|
|
274
|
-
const contentBytes = new TextEncoder().encode(content)
|
|
275
|
-
|
|
276
|
-
const contentRef = this._addObject({ Length: contentBytes.length }, contentBytes)
|
|
277
|
-
|
|
278
|
-
const xobjects: Record<string, Ref> = {}
|
|
279
|
-
for (const img of images) {
|
|
280
|
-
xobjects[img.name.slice(1)] = img.ref
|
|
281
|
-
}
|
|
282
|
-
|
|
283
|
-
const annots: Ref[] = links.map(lnk => this._addObject({
|
|
284
|
-
Type: '/Annot',
|
|
285
|
-
Subtype: '/Link',
|
|
286
|
-
Rect: lnk.rect,
|
|
287
|
-
Border: [0, 0, 0],
|
|
288
|
-
A: { Type: '/Action', S: '/URI', URI: lnk.url }
|
|
289
|
-
}, null ) )
|
|
290
|
-
|
|
291
|
-
const pageRef = this._addObject({
|
|
292
|
-
Type: '/Page',
|
|
293
|
-
Parent: null,
|
|
294
|
-
MediaBox: [0, 0, width, height],
|
|
295
|
-
Contents: contentRef,
|
|
296
|
-
Resources: {
|
|
297
|
-
Font: { F1: null },
|
|
298
|
-
XObject: Object.keys(xobjects).length > 0 ? xobjects : undefined
|
|
299
|
-
},
|
|
300
|
-
Annots: annots.length > 0 ? annots : undefined
|
|
301
|
-
}, null )
|
|
302
|
-
|
|
303
|
-
this.pages.push(pageRef)
|
|
304
|
-
}
|
|
305
|
-
|
|
306
|
-
build(): Uint8Array {
|
|
307
|
-
const fontRef = this._addObject({
|
|
308
|
-
Type: '/Font',
|
|
309
|
-
Subtype: '/Type1',
|
|
310
|
-
BaseFont: '/Helvetica',
|
|
311
|
-
Encoding: '/WinAnsiEncoding'
|
|
312
|
-
})
|
|
313
|
-
|
|
314
|
-
const pagesRef = this._addObject({
|
|
315
|
-
Type: '/Pages',
|
|
316
|
-
Kids: this.pages,
|
|
317
|
-
Count: this.pages.length
|
|
318
|
-
})
|
|
319
|
-
|
|
320
|
-
for (const obj of this.objects) {
|
|
321
|
-
if (obj.dict.Type === '/Page') {
|
|
322
|
-
obj.dict.Parent = pagesRef
|
|
323
|
-
const resources = obj.dict.Resources as Record<string, PDFValue> | undefined
|
|
324
|
-
if (resources?.Font) {
|
|
325
|
-
(resources.Font as Record<string, PDFValue>).F1 = fontRef
|
|
326
|
-
}
|
|
327
|
-
}
|
|
328
|
-
}
|
|
329
|
-
|
|
330
|
-
const catalogRef = this._addObject({
|
|
331
|
-
Type: '/Catalog',
|
|
332
|
-
Pages: pagesRef
|
|
333
|
-
})
|
|
334
|
-
|
|
335
|
-
const parts: (string | Uint8Array)[] = []
|
|
336
|
-
const offsets: number[] = []
|
|
337
|
-
|
|
338
|
-
parts.push('%PDF-1.4\n%\xFF\xFF\xFF\xFF\n')
|
|
339
|
-
|
|
340
|
-
for (const obj of this.objects) {
|
|
341
|
-
offsets[obj.id] = parts.reduce((sum, p) => sum + (typeof p === 'string' ? new TextEncoder().encode(p).length : p.length), 0)
|
|
342
|
-
|
|
343
|
-
let content = `${obj.id} 0 obj\n${serialize(obj.dict)}\n`
|
|
344
|
-
if (obj.stream) {
|
|
345
|
-
content += 'stream\n'
|
|
346
|
-
parts.push(content)
|
|
347
|
-
parts.push(obj.stream)
|
|
348
|
-
parts.push('\nendstream\nendobj\n')
|
|
349
|
-
}
|
|
350
|
-
else {
|
|
351
|
-
content += 'endobj\n'
|
|
352
|
-
parts.push(content)
|
|
353
|
-
}
|
|
354
|
-
}
|
|
355
|
-
|
|
356
|
-
const xrefOffset = parts.reduce((sum, p) => sum + (typeof p === 'string' ? new TextEncoder().encode(p).length : p.length), 0)
|
|
357
|
-
|
|
358
|
-
let xref = `xref\n0 ${this.objects.length + 1}\n`
|
|
359
|
-
xref += '0000000000 65535 f \n'
|
|
360
|
-
for (let i = 1; i <= this.objects.length; i++) {
|
|
361
|
-
xref += String(offsets[i]).padStart(10, '0') + ' 00000 n \n'
|
|
362
|
-
}
|
|
363
|
-
parts.push(xref)
|
|
364
|
-
|
|
365
|
-
parts.push(`trailer\n${serialize({ Size: this.objects.length + 1, Root: catalogRef })}\n`)
|
|
366
|
-
parts.push(`startxref\n${xrefOffset}\n%%EOF\n`)
|
|
367
|
-
|
|
368
|
-
const totalLength = parts.reduce((sum, p) => sum + (typeof p === 'string' ? new TextEncoder().encode(p).length : p.length), 0)
|
|
369
|
-
const result = new Uint8Array(totalLength)
|
|
370
|
-
let offset = 0
|
|
371
|
-
for (const part of parts) {
|
|
372
|
-
const bytes = typeof part === 'string' ? new TextEncoder().encode(part) : part
|
|
373
|
-
result.set(bytes, offset)
|
|
374
|
-
offset += bytes.length
|
|
375
|
-
}
|
|
376
|
-
|
|
377
|
-
return result
|
|
378
|
-
}
|
|
379
|
-
}
|
|
380
|
-
|
|
381
|
-
/*
|
|
382
|
-
export function markdown(md: string, opts: { width?: number; height?: number; margin?: number } = {}): Uint8Array {
|
|
383
|
-
const W = opts.width ?? 612, H = opts.height ?? 792, M = opts.margin ?? 72
|
|
384
|
-
const doc = pdf(), textW = W - M * 2, bodySize = 11, lineH = bodySize * 1.5
|
|
385
|
-
type Item = { text: string; size: number; indent: number; spaceBefore: number; spaceAfter: number; rule?: boolean; color?: string }
|
|
386
|
-
const items: Item[] = []
|
|
387
|
-
|
|
388
|
-
const wrap = (text: string, size: number, maxW: number): string[] => {
|
|
389
|
-
const words = text.split(' '), lines: string[] = []
|
|
390
|
-
let line = ''
|
|
391
|
-
for (const word of words) {
|
|
392
|
-
const test = line ? line + ' ' + word : word
|
|
393
|
-
if (measureText(test, size) <= maxW) line = test
|
|
394
|
-
else { if (line) lines.push(line); line = word }
|
|
395
|
-
}
|
|
396
|
-
if (line) lines.push(line)
|
|
397
|
-
return lines.length ? lines : ['']
|
|
398
|
-
}
|
|
399
|
-
|
|
400
|
-
let prevType = 'start'
|
|
401
|
-
for (const raw of md.split('\n')) {
|
|
402
|
-
const line = raw.trimEnd()
|
|
403
|
-
if (/^#{1,3}\s/.test(line)) {
|
|
404
|
-
const lvl = line.match(/^#+/)![0].length
|
|
405
|
-
const size = [22, 16, 13][lvl - 1]
|
|
406
|
-
const before = prevType === 'start' ? 0 : [14, 12, 10][lvl - 1]
|
|
407
|
-
const wrapped = wrap(line.slice(lvl + 1), size, textW)
|
|
408
|
-
wrapped.forEach((l, i) => items.push({ text: l, size, indent: 0, spaceBefore: i === 0 ? before : 0, spaceAfter: 4, color: '#111111' }))
|
|
409
|
-
prevType = 'header'
|
|
410
|
-
} else if (/^[-*]\s/.test(line)) {
|
|
411
|
-
const wrapped = wrap(line.slice(2), bodySize, textW - 18)
|
|
412
|
-
wrapped.forEach((l, i) => items.push({ text: (i === 0 ? '- ' : ' ') + l, size: bodySize, indent: 12, spaceBefore: 0, spaceAfter: 2 }))
|
|
413
|
-
prevType = 'list'
|
|
414
|
-
} else if (/^\d+\.\s/.test(line)) {
|
|
415
|
-
const num = line.match(/^\d+/)![0]
|
|
416
|
-
const text = line.slice(num.length + 2)
|
|
417
|
-
const wrapped = wrap(text, bodySize, textW - 18)
|
|
418
|
-
wrapped.forEach((l, i) => items.push({ text: (i === 0 ? num + '. ' : ' ') + l, size: bodySize, indent: 12, spaceBefore: 0, spaceAfter: 2 }))
|
|
419
|
-
prevType = 'list'
|
|
420
|
-
} else if (/^(-{3,}|\*{3,}|_{3,})$/.test(line)) {
|
|
421
|
-
items.push({ text: '', size: bodySize, indent: 0, spaceBefore: 8, spaceAfter: 8, rule: true })
|
|
422
|
-
prevType = 'rule'
|
|
423
|
-
} else if (line.trim() === '') {
|
|
424
|
-
if (prevType !== 'start' && prevType !== 'blank') items.push({ text: '', size: bodySize, indent: 0, spaceBefore: 0, spaceAfter: 4 })
|
|
425
|
-
prevType = 'blank'
|
|
426
|
-
} else {
|
|
427
|
-
const wrapped = wrap(line, bodySize, textW)
|
|
428
|
-
wrapped.forEach((l, i) => items.push({ text: l, size: bodySize, indent: 0, spaceBefore: 0, spaceAfter: 4, color: '#111111' }))
|
|
429
|
-
prevType = 'para'
|
|
430
|
-
}
|
|
431
|
-
}
|
|
432
|
-
|
|
433
|
-
const pages: { items: Item[]; ys: number[] }[] = []
|
|
434
|
-
let y = H - M, pg: Item[] = [], ys: number[] = []
|
|
435
|
-
for (const item of items) {
|
|
436
|
-
const needed = item.spaceBefore + item.size + item.spaceAfter
|
|
437
|
-
if (y - needed < M) { pages.push({ items: pg, ys }); pg = []; ys = []; y = H - M }
|
|
438
|
-
y -= item.spaceBefore
|
|
439
|
-
ys.push(y); pg.push(item)
|
|
440
|
-
y -= item.size + item.spaceAfter
|
|
441
|
-
}
|
|
442
|
-
if (pg.length) pages.push({ items: pg, ys })
|
|
443
|
-
|
|
444
|
-
for (const { items: pi, ys: py } of pages) {
|
|
445
|
-
doc.page(W, H, ctx => {
|
|
446
|
-
pi.forEach((it, i) => {
|
|
447
|
-
if (it.rule) ctx.line(M, py[i], W - M, py[i], '#e0e0e0', 0.5)
|
|
448
|
-
else if (it.text) ctx.text(it.text, M + it.indent, py[i], it.size, { color: it.color })
|
|
449
|
-
})
|
|
450
|
-
})
|
|
451
|
-
}
|
|
452
|
-
return doc.build()
|
|
453
|
-
}
|
|
454
|
-
*/
|
|
1
|
+
/**
|
|
2
|
+
* ___ ___ __
|
|
3
|
+
* \ \/ / / _
|
|
4
|
+
* \ / /_| |_
|
|
5
|
+
* / \____ _|
|
|
6
|
+
* /__/\__\ |_|.2
|
|
7
|
+
*
|
|
8
|
+
* @file core_pdf.ts
|
|
9
|
+
* @author Etienne Cochard
|
|
10
|
+
*
|
|
11
|
+
* @copyright (c) 2026 R-libre ingenierie
|
|
12
|
+
*
|
|
13
|
+
* Use of this source code is governed by an MIT-style license
|
|
14
|
+
* that can be found in the LICENSE file or at https://opensource.org/licenses/MIT.
|
|
15
|
+
*
|
|
16
|
+
* taken from excellent
|
|
17
|
+
* https://github.com/Lulzx/tinypdf/tree/main
|
|
18
|
+
**/
|
|
19
|
+
|
|
20
|
+
import { Color } from './core_colors.js'
|
|
21
|
+
import { isArray, isFunction, isNumber, isPlainObject, isString } from './core_tools.js'
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
const WIDTHS: number[] = [
|
|
25
|
+
278, 278, 355, 556, 556, 889, 667, 191, 333, 333, 389, 584, 278, 333, 278, 278,
|
|
26
|
+
556, 556, 556, 556, 556, 556, 556, 556, 556, 556, 278, 278, 584, 584, 584, 556,
|
|
27
|
+
1015, 667, 667, 722, 722, 667, 611, 778, 722, 278, 500, 667, 556, 833, 722, 778,
|
|
28
|
+
667, 778, 722, 667, 611, 722, 667, 944, 667, 667, 611, 278, 278, 278, 469, 556,
|
|
29
|
+
333, 556, 556, 500, 556, 556, 278, 556, 556, 222, 222, 500, 222, 833, 556, 556,
|
|
30
|
+
556, 556, 333, 500, 278, 556, 500, 722, 500, 500, 500, 334, 260, 334, 584
|
|
31
|
+
]
|
|
32
|
+
|
|
33
|
+
export interface TextOptions {
|
|
34
|
+
align?: 'left' | 'center' | 'right'
|
|
35
|
+
width?: number
|
|
36
|
+
color?: string
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export interface LinkOptions {
|
|
40
|
+
underline?: string
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
type PDFValue = null | boolean | number | string | PDFValue[] | Ref | { [key: string]: PDFValue }
|
|
44
|
+
|
|
45
|
+
interface PDFObject {
|
|
46
|
+
id: number
|
|
47
|
+
dict: Record<string, PDFValue>
|
|
48
|
+
stream: Uint8Array | null
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
export function measureText(str: string, size: number): number {
|
|
52
|
+
let width = 0
|
|
53
|
+
for (let i = 0; i < str.length; i++) {
|
|
54
|
+
const code = str.charCodeAt(i)
|
|
55
|
+
const w = (code >= 32 && code <= 126) ? WIDTHS[code - 32] : 556
|
|
56
|
+
width += w
|
|
57
|
+
}
|
|
58
|
+
return (width * size) / 1000
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
function fx( v:number, n:number ) {
|
|
62
|
+
return (+v.toFixed(n)).toString();
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
function formatColor( clr: Color, suffix: string ) : string {
|
|
66
|
+
const rgb = clr.toNumber( );
|
|
67
|
+
return `${fx((rgb>>16)&0xff,3)} ${fx((rgb>>8)&0xff,3)} ${fx(rgb&0xff,3)} ${suffix}`;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
|
|
71
|
+
const STRING_MAP: Record<string,string> = {
|
|
72
|
+
'\\': '\\\\',
|
|
73
|
+
'(': '\\(',
|
|
74
|
+
')': '\\)',
|
|
75
|
+
'\r': '\\r',
|
|
76
|
+
'\n': '\\n'
|
|
77
|
+
};
|
|
78
|
+
|
|
79
|
+
function pdfString(str: string): string {
|
|
80
|
+
return '(' + str.replace( /[\\()\r\n]/g, s => STRING_MAP[s]) + ')';
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
function serialize(val: PDFValue): string {
|
|
84
|
+
if (val === null || val === undefined) {
|
|
85
|
+
return 'null'
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
if ( typeof val === 'boolean') {
|
|
89
|
+
return val ? 'true' : 'false'
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
if ( isNumber( val ) ) {
|
|
93
|
+
return fx(val,4);
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
if ( isString(val) ) {
|
|
97
|
+
if (val.startsWith('/')) {
|
|
98
|
+
return val // name
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
if (val.startsWith('(')) {
|
|
102
|
+
return val // already escaped string
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
return pdfString(val)
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
if (isArray(val)) {
|
|
109
|
+
return '[' + val.map(serialize).join(' ') + ']'
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
if (val instanceof Ref) {
|
|
113
|
+
return `${val.id} 0 R`
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
if ( isPlainObject(val) ) {
|
|
117
|
+
const pairs = Object.entries(val)
|
|
118
|
+
.filter( ([_, v]) => v !== undefined)
|
|
119
|
+
.map( ([k, v]) => `/${k} ${serialize(v as PDFValue)}`);
|
|
120
|
+
|
|
121
|
+
return '<<\n' + pairs.join('\n') + '\n>>'
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
return String(val)
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
class Ref {
|
|
128
|
+
id: number
|
|
129
|
+
constructor(id: number) { this.id = id }
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
interface PageContext {
|
|
133
|
+
text(str: string, x: number, y: number, size: number, opts?: TextOptions): void;
|
|
134
|
+
rect(x: number, y: number, w: number, h: number, fill: string): void;
|
|
135
|
+
line(x1: number, y1: number, x2: number, y2: number, stroke: string, lineWidth?: number): void;
|
|
136
|
+
image(jpegBytes: Uint8Array, x: number, y: number, w: number, h: number): void;
|
|
137
|
+
link(url: string, x: number, y: number, w: number, h: number, opts?: LinkOptions): void;
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
export class X4PDFBuilder {
|
|
141
|
+
|
|
142
|
+
private objects: PDFObject[] = [];
|
|
143
|
+
private pages: Ref[] = [];
|
|
144
|
+
private nextId = 1;
|
|
145
|
+
|
|
146
|
+
private _addObject(dict: Record<string, PDFValue>, streamBytes: Uint8Array = null): Ref {
|
|
147
|
+
const id = this.nextId++;
|
|
148
|
+
this.objects.push({ id, dict, stream: streamBytes });
|
|
149
|
+
return new Ref(id);
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
page( width: number, height: number, callback: (ctx: PageContext) => void ): void;
|
|
153
|
+
page( callback: (ctx: PageContext) => void ): void;
|
|
154
|
+
page( param1: number | ((ctx: PageContext) => void), param2?: number, param3?: (ctx: PageContext) => void ): void {
|
|
155
|
+
const ops: string[] = [];
|
|
156
|
+
const images: { name: string; ref: Ref }[] = [];
|
|
157
|
+
const links: { url: string; rect: number[] }[] = [];
|
|
158
|
+
let imageCount = 0;
|
|
159
|
+
|
|
160
|
+
const text = (str: string, x: number, y: number, size: number, opts: TextOptions = {}) => {
|
|
161
|
+
const { align = 'left', width: boxWidth, color = '#000000' } = opts;
|
|
162
|
+
|
|
163
|
+
let tx = x;
|
|
164
|
+
if (align !== 'left' && boxWidth !== undefined) {
|
|
165
|
+
const textWidth = measureText(str, size);
|
|
166
|
+
if (align === 'center') {
|
|
167
|
+
tx = x + (boxWidth - textWidth) / 2;
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
if (align === 'right') {
|
|
171
|
+
tx = x + boxWidth - textWidth;
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
const rgb = new Color(color);
|
|
176
|
+
if (rgb) {
|
|
177
|
+
ops.push( formatColor(rgb, 'rg') );
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
ops.push('BT')
|
|
181
|
+
ops.push(`/F1 ${size} Tf`)
|
|
182
|
+
ops.push(`${fx(tx,2)} ${fx(y,2)} Td`)
|
|
183
|
+
ops.push(`${str.startsWith('(') ? str : pdfString(str)} Tj`)
|
|
184
|
+
ops.push('ET')
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
const rect = (x: number, y: number, w: number, h: number, fill: string) => {
|
|
188
|
+
const rgb = new Color(fill)
|
|
189
|
+
if (rgb) {
|
|
190
|
+
ops.push( formatColor(rgb, 'rg' ) );
|
|
191
|
+
ops.push(`${fx(x,2)} ${fx(y,2)} ${fx(w,2)} ${fx(h,2)} re`);
|
|
192
|
+
ops.push('f');
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
const line = (x1: number, y1: number, x2: number, y2: number, stroke: string, lineWidth: number = 1) => {
|
|
197
|
+
const rgb = new Color(stroke)
|
|
198
|
+
if (rgb) {
|
|
199
|
+
ops.push(`${fx(lineWidth,2)} w`);
|
|
200
|
+
ops.push( formatColor( rgb,'RG') );
|
|
201
|
+
ops.push(`${fx(x1,2)} ${fx(y1,2)} m`);
|
|
202
|
+
ops.push(`${fx(x2,2)} ${fx(y2,2)} l`);
|
|
203
|
+
ops.push('S')
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
const image = (jpegBytes: Uint8Array, x: number, y: number, w: number, h: number) => {
|
|
208
|
+
let imgWidth = 0, imgHeight = 0
|
|
209
|
+
for (let i = 0; i < jpegBytes.length - 1; i++) {
|
|
210
|
+
if (jpegBytes[i] === 0xFF && (jpegBytes[i + 1] === 0xC0 || jpegBytes[i + 1] === 0xC2)) {
|
|
211
|
+
imgHeight = (jpegBytes[i + 5] << 8) | jpegBytes[i + 6]
|
|
212
|
+
imgWidth = (jpegBytes[i + 7] << 8) | jpegBytes[i + 8]
|
|
213
|
+
break
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
const imgName = `/Im${imageCount++}`
|
|
218
|
+
const imgRef = this._addObject({
|
|
219
|
+
Type: '/XObject',
|
|
220
|
+
Subtype: '/Image',
|
|
221
|
+
Width: imgWidth,
|
|
222
|
+
Height: imgHeight,
|
|
223
|
+
ColorSpace: '/DeviceRGB',
|
|
224
|
+
BitsPerComponent: 8,
|
|
225
|
+
Filter: '/DCTDecode',
|
|
226
|
+
Length: jpegBytes.length
|
|
227
|
+
}, jpegBytes)
|
|
228
|
+
|
|
229
|
+
images.push({ name: imgName, ref: imgRef })
|
|
230
|
+
|
|
231
|
+
ops.push('q')
|
|
232
|
+
ops.push(`${fx(w,2)} 0 0 ${fx(h,2)} ${fx(x,2)} ${fx(y,2)} cm`)
|
|
233
|
+
ops.push(`${imgName} Do`)
|
|
234
|
+
ops.push('Q')
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
const link = (url: string, x: number, y: number, w: number, h: number, opts: LinkOptions = {}) => {
|
|
238
|
+
links.push({ url, rect: [x, y, x + w, y + h] })
|
|
239
|
+
if (opts.underline) {
|
|
240
|
+
const rgb = new Color(opts.underline)
|
|
241
|
+
if (rgb) {
|
|
242
|
+
ops.push(`0.75 w`)
|
|
243
|
+
ops.push( formatColor(rgb, 'RG' ) );
|
|
244
|
+
ops.push(`${fx(x,2)} ${fx(y + 2,2)} m`);
|
|
245
|
+
ops.push(`${fx(x + w,2)} ${fx(y + 2,2)} l`);
|
|
246
|
+
ops.push('S')
|
|
247
|
+
}
|
|
248
|
+
}
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
const ctx: PageContext = {
|
|
252
|
+
text,
|
|
253
|
+
rect,
|
|
254
|
+
line,
|
|
255
|
+
image,
|
|
256
|
+
link
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
let width: number
|
|
260
|
+
let height: number
|
|
261
|
+
|
|
262
|
+
if ( isFunction( param1) ) {
|
|
263
|
+
width = 612
|
|
264
|
+
height = 792
|
|
265
|
+
param1( ctx );
|
|
266
|
+
}
|
|
267
|
+
else {
|
|
268
|
+
width = param1;
|
|
269
|
+
height = param2;
|
|
270
|
+
param3( ctx );
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
const content = ops.join('\n')
|
|
274
|
+
const contentBytes = new TextEncoder().encode(content)
|
|
275
|
+
|
|
276
|
+
const contentRef = this._addObject({ Length: contentBytes.length }, contentBytes)
|
|
277
|
+
|
|
278
|
+
const xobjects: Record<string, Ref> = {}
|
|
279
|
+
for (const img of images) {
|
|
280
|
+
xobjects[img.name.slice(1)] = img.ref
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
const annots: Ref[] = links.map(lnk => this._addObject({
|
|
284
|
+
Type: '/Annot',
|
|
285
|
+
Subtype: '/Link',
|
|
286
|
+
Rect: lnk.rect,
|
|
287
|
+
Border: [0, 0, 0],
|
|
288
|
+
A: { Type: '/Action', S: '/URI', URI: lnk.url }
|
|
289
|
+
}, null ) )
|
|
290
|
+
|
|
291
|
+
const pageRef = this._addObject({
|
|
292
|
+
Type: '/Page',
|
|
293
|
+
Parent: null,
|
|
294
|
+
MediaBox: [0, 0, width, height],
|
|
295
|
+
Contents: contentRef,
|
|
296
|
+
Resources: {
|
|
297
|
+
Font: { F1: null },
|
|
298
|
+
XObject: Object.keys(xobjects).length > 0 ? xobjects : undefined
|
|
299
|
+
},
|
|
300
|
+
Annots: annots.length > 0 ? annots : undefined
|
|
301
|
+
}, null )
|
|
302
|
+
|
|
303
|
+
this.pages.push(pageRef)
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
build(): Uint8Array {
|
|
307
|
+
const fontRef = this._addObject({
|
|
308
|
+
Type: '/Font',
|
|
309
|
+
Subtype: '/Type1',
|
|
310
|
+
BaseFont: '/Helvetica',
|
|
311
|
+
Encoding: '/WinAnsiEncoding'
|
|
312
|
+
})
|
|
313
|
+
|
|
314
|
+
const pagesRef = this._addObject({
|
|
315
|
+
Type: '/Pages',
|
|
316
|
+
Kids: this.pages,
|
|
317
|
+
Count: this.pages.length
|
|
318
|
+
})
|
|
319
|
+
|
|
320
|
+
for (const obj of this.objects) {
|
|
321
|
+
if (obj.dict.Type === '/Page') {
|
|
322
|
+
obj.dict.Parent = pagesRef
|
|
323
|
+
const resources = obj.dict.Resources as Record<string, PDFValue> | undefined
|
|
324
|
+
if (resources?.Font) {
|
|
325
|
+
(resources.Font as Record<string, PDFValue>).F1 = fontRef
|
|
326
|
+
}
|
|
327
|
+
}
|
|
328
|
+
}
|
|
329
|
+
|
|
330
|
+
const catalogRef = this._addObject({
|
|
331
|
+
Type: '/Catalog',
|
|
332
|
+
Pages: pagesRef
|
|
333
|
+
})
|
|
334
|
+
|
|
335
|
+
const parts: (string | Uint8Array)[] = []
|
|
336
|
+
const offsets: number[] = []
|
|
337
|
+
|
|
338
|
+
parts.push('%PDF-1.4\n%\xFF\xFF\xFF\xFF\n')
|
|
339
|
+
|
|
340
|
+
for (const obj of this.objects) {
|
|
341
|
+
offsets[obj.id] = parts.reduce((sum, p) => sum + (typeof p === 'string' ? new TextEncoder().encode(p).length : p.length), 0)
|
|
342
|
+
|
|
343
|
+
let content = `${obj.id} 0 obj\n${serialize(obj.dict)}\n`
|
|
344
|
+
if (obj.stream) {
|
|
345
|
+
content += 'stream\n'
|
|
346
|
+
parts.push(content)
|
|
347
|
+
parts.push(obj.stream)
|
|
348
|
+
parts.push('\nendstream\nendobj\n')
|
|
349
|
+
}
|
|
350
|
+
else {
|
|
351
|
+
content += 'endobj\n'
|
|
352
|
+
parts.push(content)
|
|
353
|
+
}
|
|
354
|
+
}
|
|
355
|
+
|
|
356
|
+
const xrefOffset = parts.reduce((sum, p) => sum + (typeof p === 'string' ? new TextEncoder().encode(p).length : p.length), 0)
|
|
357
|
+
|
|
358
|
+
let xref = `xref\n0 ${this.objects.length + 1}\n`
|
|
359
|
+
xref += '0000000000 65535 f \n'
|
|
360
|
+
for (let i = 1; i <= this.objects.length; i++) {
|
|
361
|
+
xref += String(offsets[i]).padStart(10, '0') + ' 00000 n \n'
|
|
362
|
+
}
|
|
363
|
+
parts.push(xref)
|
|
364
|
+
|
|
365
|
+
parts.push(`trailer\n${serialize({ Size: this.objects.length + 1, Root: catalogRef })}\n`)
|
|
366
|
+
parts.push(`startxref\n${xrefOffset}\n%%EOF\n`)
|
|
367
|
+
|
|
368
|
+
const totalLength = parts.reduce((sum, p) => sum + (typeof p === 'string' ? new TextEncoder().encode(p).length : p.length), 0)
|
|
369
|
+
const result = new Uint8Array(totalLength)
|
|
370
|
+
let offset = 0
|
|
371
|
+
for (const part of parts) {
|
|
372
|
+
const bytes = typeof part === 'string' ? new TextEncoder().encode(part) : part
|
|
373
|
+
result.set(bytes, offset)
|
|
374
|
+
offset += bytes.length
|
|
375
|
+
}
|
|
376
|
+
|
|
377
|
+
return result
|
|
378
|
+
}
|
|
379
|
+
}
|
|
380
|
+
|
|
381
|
+
/*
|
|
382
|
+
export function markdown(md: string, opts: { width?: number; height?: number; margin?: number } = {}): Uint8Array {
|
|
383
|
+
const W = opts.width ?? 612, H = opts.height ?? 792, M = opts.margin ?? 72
|
|
384
|
+
const doc = pdf(), textW = W - M * 2, bodySize = 11, lineH = bodySize * 1.5
|
|
385
|
+
type Item = { text: string; size: number; indent: number; spaceBefore: number; spaceAfter: number; rule?: boolean; color?: string }
|
|
386
|
+
const items: Item[] = []
|
|
387
|
+
|
|
388
|
+
const wrap = (text: string, size: number, maxW: number): string[] => {
|
|
389
|
+
const words = text.split(' '), lines: string[] = []
|
|
390
|
+
let line = ''
|
|
391
|
+
for (const word of words) {
|
|
392
|
+
const test = line ? line + ' ' + word : word
|
|
393
|
+
if (measureText(test, size) <= maxW) line = test
|
|
394
|
+
else { if (line) lines.push(line); line = word }
|
|
395
|
+
}
|
|
396
|
+
if (line) lines.push(line)
|
|
397
|
+
return lines.length ? lines : ['']
|
|
398
|
+
}
|
|
399
|
+
|
|
400
|
+
let prevType = 'start'
|
|
401
|
+
for (const raw of md.split('\n')) {
|
|
402
|
+
const line = raw.trimEnd()
|
|
403
|
+
if (/^#{1,3}\s/.test(line)) {
|
|
404
|
+
const lvl = line.match(/^#+/)![0].length
|
|
405
|
+
const size = [22, 16, 13][lvl - 1]
|
|
406
|
+
const before = prevType === 'start' ? 0 : [14, 12, 10][lvl - 1]
|
|
407
|
+
const wrapped = wrap(line.slice(lvl + 1), size, textW)
|
|
408
|
+
wrapped.forEach((l, i) => items.push({ text: l, size, indent: 0, spaceBefore: i === 0 ? before : 0, spaceAfter: 4, color: '#111111' }))
|
|
409
|
+
prevType = 'header'
|
|
410
|
+
} else if (/^[-*]\s/.test(line)) {
|
|
411
|
+
const wrapped = wrap(line.slice(2), bodySize, textW - 18)
|
|
412
|
+
wrapped.forEach((l, i) => items.push({ text: (i === 0 ? '- ' : ' ') + l, size: bodySize, indent: 12, spaceBefore: 0, spaceAfter: 2 }))
|
|
413
|
+
prevType = 'list'
|
|
414
|
+
} else if (/^\d+\.\s/.test(line)) {
|
|
415
|
+
const num = line.match(/^\d+/)![0]
|
|
416
|
+
const text = line.slice(num.length + 2)
|
|
417
|
+
const wrapped = wrap(text, bodySize, textW - 18)
|
|
418
|
+
wrapped.forEach((l, i) => items.push({ text: (i === 0 ? num + '. ' : ' ') + l, size: bodySize, indent: 12, spaceBefore: 0, spaceAfter: 2 }))
|
|
419
|
+
prevType = 'list'
|
|
420
|
+
} else if (/^(-{3,}|\*{3,}|_{3,})$/.test(line)) {
|
|
421
|
+
items.push({ text: '', size: bodySize, indent: 0, spaceBefore: 8, spaceAfter: 8, rule: true })
|
|
422
|
+
prevType = 'rule'
|
|
423
|
+
} else if (line.trim() === '') {
|
|
424
|
+
if (prevType !== 'start' && prevType !== 'blank') items.push({ text: '', size: bodySize, indent: 0, spaceBefore: 0, spaceAfter: 4 })
|
|
425
|
+
prevType = 'blank'
|
|
426
|
+
} else {
|
|
427
|
+
const wrapped = wrap(line, bodySize, textW)
|
|
428
|
+
wrapped.forEach((l, i) => items.push({ text: l, size: bodySize, indent: 0, spaceBefore: 0, spaceAfter: 4, color: '#111111' }))
|
|
429
|
+
prevType = 'para'
|
|
430
|
+
}
|
|
431
|
+
}
|
|
432
|
+
|
|
433
|
+
const pages: { items: Item[]; ys: number[] }[] = []
|
|
434
|
+
let y = H - M, pg: Item[] = [], ys: number[] = []
|
|
435
|
+
for (const item of items) {
|
|
436
|
+
const needed = item.spaceBefore + item.size + item.spaceAfter
|
|
437
|
+
if (y - needed < M) { pages.push({ items: pg, ys }); pg = []; ys = []; y = H - M }
|
|
438
|
+
y -= item.spaceBefore
|
|
439
|
+
ys.push(y); pg.push(item)
|
|
440
|
+
y -= item.size + item.spaceAfter
|
|
441
|
+
}
|
|
442
|
+
if (pg.length) pages.push({ items: pg, ys })
|
|
443
|
+
|
|
444
|
+
for (const { items: pi, ys: py } of pages) {
|
|
445
|
+
doc.page(W, H, ctx => {
|
|
446
|
+
pi.forEach((it, i) => {
|
|
447
|
+
if (it.rule) ctx.line(M, py[i], W - M, py[i], '#e0e0e0', 0.5)
|
|
448
|
+
else if (it.text) ctx.text(it.text, M + it.indent, py[i], it.size, { color: it.color })
|
|
449
|
+
})
|
|
450
|
+
})
|
|
451
|
+
}
|
|
452
|
+
return doc.build()
|
|
453
|
+
}
|
|
454
|
+
*/
|