MCPcopy
hub / github.com/Lulzx/tinypdf / page

Function page

src/index.ts:132–246  ·  view source on GitHub ↗
(widthOrFn: number | ((ctx: PageContext) => void), heightOrUndefined?: number, fnOrUndefined?: (ctx: PageContext) => void)

Source from the content-addressed store, hash-verified

130 }
131
132 function page(widthOrFn: number | ((ctx: PageContext) => void), heightOrUndefined?: number, fnOrUndefined?: (ctx: PageContext) => void): void {
133 let width: number
134 let height: number
135 let fn: (ctx: PageContext) => void
136
137 if (typeof widthOrFn === 'function') {
138 width = 612
139 height = 792
140 fn = widthOrFn
141 } else {
142 width = widthOrFn
143 height = heightOrUndefined!
144 fn = fnOrUndefined!
145 }
146
147 const ops: string[] = []
148 const images: { name: string; ref: Ref }[] = []
149 let imageCount = 0
150
151 const ctx: PageContext = {
152 text(str: string, x: number, y: number, size: number, opts: TextOptions = {}) {
153 const { align = 'left', width: boxWidth, color = '#000000' } = opts
154
155 let tx = x
156 if (align !== 'left' && boxWidth !== undefined) {
157 const textWidth = measureText(str, size)
158 if (align === 'center') tx = x + (boxWidth - textWidth) / 2
159 if (align === 'right') tx = x + boxWidth - textWidth
160 }
161
162 const rgb = parseColor(color)
163 if (rgb) ops.push(`${rgb[0].toFixed(3)} ${rgb[1].toFixed(3)} ${rgb[2].toFixed(3)} rg`)
164 ops.push('BT')
165 ops.push(`/F1 ${size} Tf`)
166 ops.push(`${tx.toFixed(2)} ${y.toFixed(2)} Td`)
167 ops.push(`${pdfString(str)} Tj`)
168 ops.push('ET')
169 },
170
171 rect(x: number, y: number, w: number, h: number, fill: string) {
172 const rgb = parseColor(fill)
173 if (rgb) {
174 ops.push(`${rgb[0].toFixed(3)} ${rgb[1].toFixed(3)} ${rgb[2].toFixed(3)} rg`)
175 ops.push(`${x.toFixed(2)} ${y.toFixed(2)} ${w.toFixed(2)} ${h.toFixed(2)} re`)
176 ops.push('f')
177 }
178 },
179
180 line(x1: number, y1: number, x2: number, y2: number, stroke: string, lineWidth: number = 1) {
181 const rgb = parseColor(stroke)
182 if (rgb) {
183 ops.push(`${lineWidth.toFixed(2)} w`)
184 ops.push(`${rgb[0].toFixed(3)} ${rgb[1].toFixed(3)} ${rgb[2].toFixed(3)} RG`)
185 ops.push(`${x1.toFixed(2)} ${y1.toFixed(2)} m`)
186 ops.push(`${x2.toFixed(2)} ${y2.toFixed(2)} l`)
187 ops.push('S')
188 }
189 },

Callers

nothing calls this directly

Calls 1

addObjectFunction · 0.85

Tested by

no test coverage detected