Skip to content

Commit 3a90aa9

Browse files
committed
feat(runner): color schema
1 parent 057e2da commit 3a90aa9

6 files changed

Lines changed: 102 additions & 18 deletions

File tree

README.md

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -124,24 +124,40 @@ pdfeasy.new({
124124
125125
## Runner Options
126126

127+
### Client-Side Setup
128+
127129
```ts
128130
pdfeasy.run({
129131
type: 'client',
130-
clientEmit: 'save'
131-
}).then(() => {}).catch((err: any) => {
132+
clientEmit: 'save',
133+
}).then(() => {}).catch((err) => {
132134
console.error(err)
133135
})
134136
```
135137

138+
### Server-Side Setup
139+
136140
```ts
137141
pdfeasy.run({
138142
type: 'server',
139-
serverPath: path.resolve(process.cwd() + '/examples')
140-
}).then(() => {}).catch((err: any) => {
143+
serverPath: path.resolve(process.cwd() + '/examples'),
144+
}).then(() => {}).catch((err) => {
141145
console.error(err)
142146
})
143147
```
144148

149+
### Color Schema
150+
151+
It is possible to define the color scheme used automatically:
152+
153+
```ts
154+
// converts all hex color to cmyk
155+
pdfeasy.run({ colorSchema: 'CMYK' })
156+
157+
// converts all hex color to rgb
158+
pdfeasy.run({ colorSchema: 'RBG' })
159+
```
160+
145161
## Custom Fonts
146162

147163
```ts
@@ -169,8 +185,6 @@ See [scripts](./scripts/generate/) for server-side runner.
169185

170186
## Bundles
171187

172-
> **Attention!** Its recommended use explicit imports (*import pdfeasy from 'pdfeasy/dist/client.esm.js'*) in your projects.
173-
174188
`pdfeasy/dist/client.cjs.js`
175189

176190
`pdfeasy/dist/client.esm.js`

demo/src/main.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ pdfeasy.addFonts([
4242
}
4343
])
4444

45-
pdfeasy.run().then((blob: string) => {
45+
pdfeasy.run({ colorSchema: 'CMYK' }).then((blob: string) => {
4646
const iframe = document.querySelector('#pdf') as HTMLIFrameElement
4747

4848
iframe.src = blob

src/pipe/factory.ts

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,19 @@
1-
import {
1+
import type {
22
Content,
33
ContentText,
44
ContentImage,
55
PDFEasyDefaults,
66
InternalGlobals,
7+
HexColor,
8+
RunOptionsBase,
79
} from '../types'
810
import { getCorrectFontFamily } from './transform'
911
import { getImageRaw, SvgToPNG } from '../content/image'
12+
import { HEXToCMYK } from 'src/schema/color'
13+
14+
export const resolveColor = (color: HexColor, run: RunOptionsBase) => {
15+
return run?.colorSchema === 'CMYK' ? HEXToCMYK(color) : color
16+
}
1017

1118
export const resolveCover = async (app: PDFKit.PDFDocument, based: string) => {
1219
const { raw } = await getImageRaw(based)
@@ -23,7 +30,8 @@ export const resolveContent = async (
2330
app: PDFKit.PDFDocument,
2431
defaults: PDFEasyDefaults,
2532
content: Content,
26-
globals: InternalGlobals
33+
globals: InternalGlobals,
34+
run: RunOptionsBase
2735
) => {
2836
const addStack = async () => {
2937
const stack = content.stack as Content[]
@@ -41,7 +49,7 @@ export const resolveContent = async (
4149
)
4250
)
4351
.fontSize(entity.text.fontSize || defaults.text.fontSize)
44-
.fillColor(entity.text.color || defaults.text.color)
52+
.fillColor(resolveColor(entity.text.color || defaults.text.color, run))
4553
.fillOpacity(entity.text.opacity || defaults.text.opacity)
4654
.text(entity.raw, {
4755
continued: !isLast,
@@ -66,7 +74,7 @@ export const resolveContent = async (
6674
await app
6775
.font(getCorrectFontFamily(style?.font || defaults.text.font, style))
6876
.fontSize(style?.fontSize || defaults.text.fontSize)
69-
.fillColor(style?.color || defaults.text.color)
77+
.fillColor(resolveColor(style?.color || defaults.text.color, run))
7078
.fillOpacity(style?.opacity || defaults.text.opacity)
7179
.text(data, {
7280
indent: style?.indent || defaults.text.indent,
@@ -84,7 +92,7 @@ export const resolveContent = async (
8492
await app
8593
.font(getCorrectFontFamily(defaults.text.font, {}))
8694
.fontSize(defaults.text.fontSize)
87-
.fillColor(defaults.text.color)
95+
.fillColor(resolveColor(defaults.text.color, run))
8896
.fillOpacity(defaults.text.opacity)
8997
.text(content.raw, {
9098
indent: defaults.text.indent,
@@ -143,10 +151,14 @@ export const resolveContent = async (
143151
}
144152

145153
const addCheckbox = async () => {
146-
const backgroundColor =
147-
content.checkbox?.backgroundColor ?? defaults.checkbox.backgroundColor
148-
const borderColor =
149-
content.checkbox?.borderColor ?? defaults.checkbox.borderColor
154+
const backgroundColor = resolveColor(
155+
content.checkbox?.backgroundColor ?? defaults.checkbox.backgroundColor,
156+
run
157+
)
158+
const borderColor = resolveColor(
159+
content.checkbox?.borderColor ?? defaults.checkbox.borderColor,
160+
run
161+
)
150162
const size = content.checkbox?.size ?? defaults.checkbox.size
151163

152164
app.initForm()
@@ -170,7 +182,7 @@ export const resolveContent = async (
170182
app
171183
.circle(app.x + 4, app.y + 6, 3)
172184
.lineWidth(1)
173-
.fill('#000')
185+
.fill(resolveColor('#000000', run))
174186

175187
await addText(false, ` ${content.raw}`)
176188
}

src/runner/pdfeasy.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,8 @@ export default class {
141141
this.pdfkit as typeof PDFDocument,
142142
this.def,
143143
content,
144-
this.globals
144+
this.globals,
145+
this.optionsRun
145146
)
146147
}
147148
}

src/schema/color.ts

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import { HexColor } from 'src/types'
2+
3+
export const HEXToCMYK = (
4+
color: HexColor
5+
): [number, number, number, number] | HexColor => {
6+
let C = 0
7+
let M = 0
8+
let Y = 0
9+
let K = 0
10+
11+
const hex = color.charAt(0) == '#' ? color.substring(1, 7) : color
12+
13+
if (hex.length != 6) {
14+
return color
15+
}
16+
17+
const r = parseInt(hex.substring(0, 2), 16)
18+
const g = parseInt(hex.substring(2, 4), 16)
19+
const b = parseInt(hex.substring(4, 6), 16)
20+
21+
if (r === 0 && g === 0 && b === 0) {
22+
return [0, 0, 0, 100]
23+
}
24+
25+
C = 1 - r / 255
26+
M = 1 - g / 255
27+
Y = 1 - b / 255
28+
29+
const minCMY = Math.min(C, Math.min(M, Y))
30+
31+
C = (C - minCMY) / (1 - minCMY)
32+
M = (M - minCMY) / (1 - minCMY)
33+
Y = (Y - minCMY) / (1 - minCMY)
34+
K = minCMY
35+
36+
C = Math.round(C * 100)
37+
M = Math.round(M * 100)
38+
Y = Math.round(Y * 100)
39+
K = Math.round(K * 100)
40+
41+
C = Number(C.toFixed(0)) ?? 0
42+
M = Number(M.toFixed(0)) ?? 0
43+
Y = Number(Y.toFixed(0)) ?? 0
44+
K = Number(K.toFixed(0)) ?? 0
45+
46+
return [C, M, Y, K]
47+
}

src/types.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@ export type PDFRunEmitOption = 'save' | 'blob' | 'none'
3030

3131
export type TextAlign = 'start' | 'center' | 'end' | 'justify'
3232

33+
export type ColorSchema = 'RGB' | 'CMYK'
34+
35+
export type HexColor = string | `#${string}`
36+
3337
export type EmitterType = {}
3438

3539
export interface InternalGlobals {
@@ -301,4 +305,10 @@ export interface RunOptions {
301305
* Required in {@link RunOptions} type: server
302306
*/
303307
serverPath?: string
308+
/**
309+
* Server file destination
310+
*
311+
* @default 'rgb'
312+
*/
313+
colorSchema?: ColorSchema
304314
}

0 commit comments

Comments
 (0)