@ericminassian/platform-engine 0.4.0 → 0.5.0
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/dist/clients.d.ts +0 -4
- package/dist/clients.js +1 -14
- package/dist/clients.js.map +1 -1
- package/dist/compat.d.ts +9 -21
- package/dist/compat.js +6 -26
- package/dist/compat.js.map +1 -1
- package/dist/drivers/cognito.d.ts +27 -3
- package/dist/drivers/cognito.js +76 -24
- package/dist/drivers/cognito.js.map +1 -1
- package/dist/drivers/index.d.ts +0 -2
- package/dist/drivers/index.js +0 -2
- package/dist/drivers/index.js.map +1 -1
- package/dist/drivers/logs.d.ts +11 -0
- package/dist/drivers/logs.js +15 -1
- package/dist/drivers/logs.js.map +1 -1
- package/dist/drivers/scheduler.d.ts +0 -21
- package/dist/drivers/scheduler.js +0 -43
- package/dist/drivers/scheduler.js.map +1 -1
- package/dist/index.d.ts +2 -81
- package/dist/index.js +0 -5
- package/dist/index.js.map +1 -1
- package/dist/local.js +2 -140
- package/dist/local.js.map +1 -1
- package/dist/login-branding.d.ts +27 -0
- package/dist/login-branding.js +329 -0
- package/dist/login-branding.js.map +1 -0
- package/dist/login-glyphs.d.ts +12 -0
- package/dist/login-glyphs.js +903 -0
- package/dist/login-glyphs.js.map +1 -0
- package/dist/policy.d.ts +2 -26
- package/dist/policy.js +2 -107
- package/dist/policy.js.map +1 -1
- package/dist/stage.d.ts +1 -2
- package/dist/stage.js +16 -87
- package/dist/stage.js.map +1 -1
- package/dist/util.d.ts +0 -22
- package/dist/util.js +0 -16
- package/dist/util.js.map +1 -1
- package/package.json +10 -7
- package/src/clients.ts +1 -15
- package/src/compat.ts +9 -32
- package/src/drivers/cognito.ts +98 -23
- package/src/drivers/index.ts +0 -2
- package/src/drivers/logs.ts +21 -0
- package/src/drivers/scheduler.ts +0 -69
- package/src/index.ts +3 -78
- package/src/local.ts +2 -152
- package/src/login-branding.ts +407 -0
- package/src/login-glyphs.ts +911 -0
- package/src/policy.ts +2 -132
- package/src/stage.ts +16 -102
- package/src/util.ts +0 -34
- package/dist/drivers/ecr.d.ts +0 -109
- package/dist/drivers/ecr.js +0 -303
- package/dist/drivers/ecr.js.map +0 -1
- package/dist/drivers/ecs.d.ts +0 -163
- package/dist/drivers/ecs.js +0 -447
- package/dist/drivers/ecs.js.map +0 -1
- package/dist/images.d.ts +0 -104
- package/dist/images.js +0 -182
- package/dist/images.js.map +0 -1
- package/dist/services.d.ts +0 -155
- package/dist/services.js +0 -464
- package/dist/services.js.map +0 -1
- package/src/drivers/ecr.ts +0 -457
- package/src/drivers/ecs.ts +0 -661
- package/src/images.ts +0 -254
- package/src/services.ts +0 -663
|
@@ -0,0 +1,407 @@
|
|
|
1
|
+
import { crc32, deflateSync } from 'node:zlib';
|
|
2
|
+
import type {
|
|
3
|
+
AssetCategoryType,
|
|
4
|
+
AssetExtensionType,
|
|
5
|
+
ColorSchemeModeType,
|
|
6
|
+
CreateManagedLoginBrandingRequest,
|
|
7
|
+
} from '@aws-sdk/client-cognito-identity-provider';
|
|
8
|
+
import { GEIST_SEMIBOLD, type GlyphSet } from './login-glyphs.ts';
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* The managed login style every app client gets, after the console and
|
|
12
|
+
* Vercel's login page: a header with the platform's mark and domain (one
|
|
13
|
+
* account across every app), the app's name over a form without a card, the
|
|
14
|
+
* console's neutral greys, and the device's light or dark mode.
|
|
15
|
+
*
|
|
16
|
+
* Cognito takes colors and images, but no fonts or copy, so the names are
|
|
17
|
+
* drawn as Geist outlines. Its page font (Open Sans), its copy and its italic
|
|
18
|
+
* placeholders stay as they are.
|
|
19
|
+
*/
|
|
20
|
+
export interface LoginBranding {
|
|
21
|
+
/** The style's settings document, as Cognito stores and returns it. */
|
|
22
|
+
settings: Settings;
|
|
23
|
+
assets: Array<{
|
|
24
|
+
Category: AssetCategoryType;
|
|
25
|
+
ColorMode: ColorSchemeModeType;
|
|
26
|
+
Extension: AssetExtensionType;
|
|
27
|
+
Bytes: Uint8Array;
|
|
28
|
+
}>;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
type Settings = NonNullable<CreateManagedLoginBrandingRequest['Settings']>;
|
|
32
|
+
|
|
33
|
+
export function loginBranding(spec: { app: string; domain: string }): LoginBranding {
|
|
34
|
+
const assets: LoginBranding['assets'] = [];
|
|
35
|
+
for (const [mode, p] of [
|
|
36
|
+
['LIGHT', LIGHT],
|
|
37
|
+
['DARK', DARK],
|
|
38
|
+
] as const) {
|
|
39
|
+
const svg = (Category: 'FAVICON_SVG' | 'PAGE_HEADER_LOGO' | 'FORM_LOGO', body: string) =>
|
|
40
|
+
assets.push({ Category, ColorMode: mode, Extension: 'SVG', Bytes: Buffer.from(body) });
|
|
41
|
+
svg('FAVICON_SVG', markSvg(p));
|
|
42
|
+
assets.push({ Category: 'FAVICON_ICO', ColorMode: mode, Extension: 'ICO', Bytes: markIco(p) });
|
|
43
|
+
svg('PAGE_HEADER_LOGO', domainLockup(spec.domain.toLowerCase(), p));
|
|
44
|
+
svg('FORM_LOGO', appWordmark(spec.app, p));
|
|
45
|
+
}
|
|
46
|
+
return { settings: SETTINGS, assets };
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
// -------------------------------------------------------------------- colors
|
|
50
|
+
|
|
51
|
+
/** One color mode, after the console's tokens (services/control-plane/web/src/index.css). */
|
|
52
|
+
interface Palette {
|
|
53
|
+
/** Page and form background. */
|
|
54
|
+
page: string;
|
|
55
|
+
fg: string;
|
|
56
|
+
muted: string;
|
|
57
|
+
subtle: string;
|
|
58
|
+
/** Hovered secondary surfaces (`--muted`) and pressed ones (`--accent`). */
|
|
59
|
+
hover: string;
|
|
60
|
+
pressed: string;
|
|
61
|
+
border: string;
|
|
62
|
+
borderStrong: string;
|
|
63
|
+
input: string;
|
|
64
|
+
ring: string;
|
|
65
|
+
link: string;
|
|
66
|
+
linkHover: string;
|
|
67
|
+
/** The primary button: the foreground color, a step lighter (or darker) on hover and press. */
|
|
68
|
+
primaryHover: string;
|
|
69
|
+
primaryPressed: string;
|
|
70
|
+
onPrimary: string;
|
|
71
|
+
ok: string;
|
|
72
|
+
okBg: string;
|
|
73
|
+
warn: string;
|
|
74
|
+
warnBg: string;
|
|
75
|
+
bad: string;
|
|
76
|
+
badBg: string;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
const LIGHT: Palette = {
|
|
80
|
+
page: '#ffffff',
|
|
81
|
+
fg: '#171717',
|
|
82
|
+
muted: '#5f5f5f',
|
|
83
|
+
subtle: '#737373',
|
|
84
|
+
hover: '#f5f5f5',
|
|
85
|
+
pressed: '#ededed',
|
|
86
|
+
border: '#00000017',
|
|
87
|
+
borderStrong: '#00000029',
|
|
88
|
+
input: '#00000029',
|
|
89
|
+
ring: '#0068d6',
|
|
90
|
+
link: '#0062d1',
|
|
91
|
+
linkHover: '#004ea8',
|
|
92
|
+
primaryHover: '#383838',
|
|
93
|
+
primaryPressed: '#4d4d4d',
|
|
94
|
+
onPrimary: '#ffffff',
|
|
95
|
+
ok: '#297a3a',
|
|
96
|
+
okBg: '#f3faf4',
|
|
97
|
+
warn: '#a35200',
|
|
98
|
+
warnBg: '#fff8ef',
|
|
99
|
+
bad: '#cd2b31',
|
|
100
|
+
badBg: '#fff5f5',
|
|
101
|
+
};
|
|
102
|
+
|
|
103
|
+
const DARK: Palette = {
|
|
104
|
+
page: '#0a0a0a',
|
|
105
|
+
fg: '#ededed',
|
|
106
|
+
muted: '#a1a1a1',
|
|
107
|
+
subtle: '#8a8a8a',
|
|
108
|
+
hover: '#161616',
|
|
109
|
+
pressed: '#1f1f1f',
|
|
110
|
+
border: '#ffffff1a',
|
|
111
|
+
borderStrong: '#ffffff2b',
|
|
112
|
+
input: '#ffffff2e',
|
|
113
|
+
ring: '#52a8ff',
|
|
114
|
+
link: '#52a8ff',
|
|
115
|
+
linkHover: '#8cc4ff',
|
|
116
|
+
primaryHover: '#cccccc',
|
|
117
|
+
primaryPressed: '#b3b3b3',
|
|
118
|
+
onPrimary: '#0a0a0a',
|
|
119
|
+
ok: '#62c073',
|
|
120
|
+
okBg: '#0c1a0f',
|
|
121
|
+
warn: '#f2a20d',
|
|
122
|
+
warnBg: '#1c1405',
|
|
123
|
+
bad: '#ff6166',
|
|
124
|
+
badBg: '#1f0d0e',
|
|
125
|
+
};
|
|
126
|
+
|
|
127
|
+
/** Cognito's form: `rrggbbaa`, no `#`. */
|
|
128
|
+
const rgba = (hex: string) => (hex.length === 7 ? `${hex.slice(1)}ff` : hex.slice(1));
|
|
129
|
+
|
|
130
|
+
const modes = <T>(f: (p: Palette) => T) => ({ lightMode: f(LIGHT), darkMode: f(DARK) });
|
|
131
|
+
|
|
132
|
+
// ------------------------------------------------------------------ settings
|
|
133
|
+
|
|
134
|
+
const RADIUS = 6;
|
|
135
|
+
|
|
136
|
+
/** A bordered control (secondary and identity provider buttons). */
|
|
137
|
+
const outlined = (p: Palette) => ({
|
|
138
|
+
defaults: { backgroundColor: rgba(p.page), borderColor: rgba(p.borderStrong), textColor: rgba(p.fg) },
|
|
139
|
+
hover: { backgroundColor: rgba(p.hover), borderColor: rgba(p.borderStrong), textColor: rgba(p.fg) },
|
|
140
|
+
active: { backgroundColor: rgba(p.pressed), borderColor: rgba(p.borderStrong), textColor: rgba(p.fg) },
|
|
141
|
+
});
|
|
142
|
+
|
|
143
|
+
/**
|
|
144
|
+
* An update replaces the whole document, and Cognito fills what it leaves out
|
|
145
|
+
* with its own defaults, so this names everything the look depends on. Its
|
|
146
|
+
* keys come from `DescribeManagedLoginBrandingByClient --return-merged-resources`;
|
|
147
|
+
* Cognito refuses values it doesn't know.
|
|
148
|
+
*/
|
|
149
|
+
const SETTINGS: Settings = {
|
|
150
|
+
categories: {
|
|
151
|
+
global: {
|
|
152
|
+
colorSchemeMode: 'DYNAMIC',
|
|
153
|
+
pageHeader: { enabled: true },
|
|
154
|
+
pageFooter: { enabled: false },
|
|
155
|
+
spacingDensity: 'REGULAR',
|
|
156
|
+
},
|
|
157
|
+
form: {
|
|
158
|
+
displayGraphics: true,
|
|
159
|
+
location: { horizontal: 'CENTER', vertical: 'CENTER' },
|
|
160
|
+
},
|
|
161
|
+
},
|
|
162
|
+
components: {
|
|
163
|
+
pageBackground: { image: { enabled: false }, ...modes((p) => ({ color: rgba(p.page) })) },
|
|
164
|
+
pageHeader: {
|
|
165
|
+
backgroundImage: { enabled: false },
|
|
166
|
+
logo: { enabled: true, location: 'START' },
|
|
167
|
+
...modes((p) => ({ background: { color: rgba(p.page) }, borderColor: rgba(p.border) })),
|
|
168
|
+
},
|
|
169
|
+
// No card: the form sits on the page.
|
|
170
|
+
form: {
|
|
171
|
+
borderRadius: 0,
|
|
172
|
+
backgroundImage: { enabled: false },
|
|
173
|
+
logo: { enabled: true, location: 'CENTER', position: 'TOP', formInclusion: 'IN' },
|
|
174
|
+
...modes((p) => ({ backgroundColor: rgba(p.page), borderColor: `${rgba(p.page).slice(0, 6)}00` })),
|
|
175
|
+
},
|
|
176
|
+
pageText: modes((p) => ({
|
|
177
|
+
headingColor: rgba(p.fg),
|
|
178
|
+
bodyColor: rgba(p.muted),
|
|
179
|
+
descriptionColor: rgba(p.muted),
|
|
180
|
+
})),
|
|
181
|
+
primaryButton: modes((p) => ({
|
|
182
|
+
defaults: { backgroundColor: rgba(p.fg), textColor: rgba(p.onPrimary) },
|
|
183
|
+
hover: { backgroundColor: rgba(p.primaryHover), textColor: rgba(p.onPrimary) },
|
|
184
|
+
active: { backgroundColor: rgba(p.primaryPressed), textColor: rgba(p.onPrimary) },
|
|
185
|
+
disabled: { backgroundColor: rgba(p.pressed), borderColor: rgba(p.pressed) },
|
|
186
|
+
})),
|
|
187
|
+
secondaryButton: modes(outlined),
|
|
188
|
+
idpButton: { standard: modes(outlined) },
|
|
189
|
+
alert: {
|
|
190
|
+
borderRadius: 8,
|
|
191
|
+
...modes((p) => ({ error: { backgroundColor: rgba(p.badBg), borderColor: rgba(p.bad) } })),
|
|
192
|
+
},
|
|
193
|
+
favicon: { enabledTypes: ['ICO', 'SVG'] },
|
|
194
|
+
},
|
|
195
|
+
componentClasses: {
|
|
196
|
+
buttons: { borderRadius: RADIUS },
|
|
197
|
+
input: {
|
|
198
|
+
borderRadius: RADIUS,
|
|
199
|
+
...modes((p) => ({
|
|
200
|
+
defaults: { backgroundColor: rgba(p.page), borderColor: rgba(p.input) },
|
|
201
|
+
placeholderColor: rgba(p.subtle),
|
|
202
|
+
})),
|
|
203
|
+
},
|
|
204
|
+
inputLabel: modes((p) => ({ textColor: rgba(p.fg) })),
|
|
205
|
+
inputDescription: modes((p) => ({ textColor: rgba(p.muted) })),
|
|
206
|
+
link: modes((p) => ({ defaults: { textColor: rgba(p.link) }, hover: { textColor: rgba(p.linkHover) } })),
|
|
207
|
+
focusState: modes((p) => ({ borderColor: rgba(p.ring) })),
|
|
208
|
+
optionControls: modes((p) => ({
|
|
209
|
+
defaults: { backgroundColor: rgba(p.page), borderColor: rgba(p.input) },
|
|
210
|
+
selected: { backgroundColor: rgba(p.fg), foregroundColor: rgba(p.onPrimary) },
|
|
211
|
+
})),
|
|
212
|
+
statusIndicator: modes((p) => ({
|
|
213
|
+
success: { backgroundColor: rgba(p.okBg), borderColor: rgba(p.ok), indicatorColor: rgba(p.ok) },
|
|
214
|
+
warning: { backgroundColor: rgba(p.warnBg), borderColor: rgba(p.warn), indicatorColor: rgba(p.warn) },
|
|
215
|
+
error: { backgroundColor: rgba(p.badBg), borderColor: rgba(p.bad), indicatorColor: rgba(p.bad) },
|
|
216
|
+
pending: { indicatorColor: rgba(p.subtle) },
|
|
217
|
+
})),
|
|
218
|
+
divider: modes((p) => ({ borderColor: rgba(p.border) })),
|
|
219
|
+
dropDown: {
|
|
220
|
+
borderRadius: RADIUS,
|
|
221
|
+
...modes((p) => ({
|
|
222
|
+
defaults: { itemBackgroundColor: rgba(p.page) },
|
|
223
|
+
hover: {
|
|
224
|
+
itemBackgroundColor: rgba(p.hover),
|
|
225
|
+
itemBorderColor: rgba(p.borderStrong),
|
|
226
|
+
itemTextColor: rgba(p.fg),
|
|
227
|
+
},
|
|
228
|
+
match: { itemBackgroundColor: rgba(p.page), itemTextColor: rgba(p.link) },
|
|
229
|
+
})),
|
|
230
|
+
},
|
|
231
|
+
idpButtons: { icons: { enabled: true } },
|
|
232
|
+
},
|
|
233
|
+
};
|
|
234
|
+
|
|
235
|
+
// --------------------------------------------------------------------- logos
|
|
236
|
+
|
|
237
|
+
const num = (n: number) => String(Math.round(n * 100) / 100);
|
|
238
|
+
|
|
239
|
+
/** Cognito refuses a logo outside 1:1 to 4:1 (width:height), so pad it, content centered. */
|
|
240
|
+
function logo(width: number, height: number, body: string): string {
|
|
241
|
+
let w = width;
|
|
242
|
+
let h = height;
|
|
243
|
+
if (w > 4 * h) h = w / 4;
|
|
244
|
+
if (w < h) w = h;
|
|
245
|
+
const dx = (w - width) / 2;
|
|
246
|
+
const dy = (h - height) / 2;
|
|
247
|
+
const content = dx || dy ? `<g transform="translate(${num(dx)} ${num(dy)})">${body}</g>` : body;
|
|
248
|
+
return `<svg xmlns="http://www.w3.org/2000/svg" width="${num(w)}" height="${num(h)}" viewBox="0 0 ${num(w)} ${num(h)}">${content}</svg>`;
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
/**
|
|
252
|
+
* `text` as Geist outlines at `size` px, its baseline at `y` from `x`, with
|
|
253
|
+
* the font's kerning and `tracking` (in em) between letters.
|
|
254
|
+
*/
|
|
255
|
+
function outlineText(
|
|
256
|
+
text: string,
|
|
257
|
+
opts: { size: number; x: number; y: number; fill: string; tracking: number },
|
|
258
|
+
): { svg: string; width: number } {
|
|
259
|
+
const font: GlyphSet = GEIST_SEMIBOLD;
|
|
260
|
+
// App names and domains are ASCII; anything else has no outline and throws below.
|
|
261
|
+
const chars = text.split('');
|
|
262
|
+
let advance = 0;
|
|
263
|
+
const paths: string[] = [];
|
|
264
|
+
for (const [i, ch] of chars.entries()) {
|
|
265
|
+
const glyph = font.glyphs[ch];
|
|
266
|
+
if (!glyph) throw new Error(`no outline for ${JSON.stringify(ch)} in ${JSON.stringify(text)}`);
|
|
267
|
+
paths.push(`<path transform="translate(${advance} 0)" d="${glyph.d}"/>`);
|
|
268
|
+
advance += glyph.advance;
|
|
269
|
+
const next = chars[i + 1];
|
|
270
|
+
if (next) advance += (font.kerning[ch + next] ?? 0) + opts.tracking * font.unitsPerEm;
|
|
271
|
+
}
|
|
272
|
+
const scale = opts.size / font.unitsPerEm;
|
|
273
|
+
return {
|
|
274
|
+
svg: `<g fill="${opts.fill}" transform="translate(${num(opts.x)} ${num(opts.y)}) scale(${scale})">${paths.join('')}</g>`,
|
|
275
|
+
width: advance * scale,
|
|
276
|
+
};
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
/** The platform's mark (the console's favicon): a rounded square with a smaller one inside. */
|
|
280
|
+
const MARK = { size: 16, radius: 4, inner: { at: 4.5, size: 7, radius: 1.5 } };
|
|
281
|
+
|
|
282
|
+
function mark(x: number, size: number, p: Palette): string {
|
|
283
|
+
const k = size / MARK.size;
|
|
284
|
+
const i = MARK.inner;
|
|
285
|
+
return (
|
|
286
|
+
`<rect x="${num(x)}" width="${num(size)}" height="${num(size)}" rx="${num(MARK.radius * k)}" fill="${p.fg}"/>` +
|
|
287
|
+
`<rect x="${num(x + i.at * k)}" y="${num(i.at * k)}" width="${num(i.size * k)}" height="${num(i.size * k)}" rx="${num(i.radius * k)}" fill="${p.page}"/>`
|
|
288
|
+
);
|
|
289
|
+
}
|
|
290
|
+
|
|
291
|
+
const markSvg = (p: Palette) => logo(MARK.size, MARK.size, mark(0, MARK.size, p));
|
|
292
|
+
|
|
293
|
+
/** The header: the mark, then the platform's domain, centered on the mark. */
|
|
294
|
+
function domainLockup(domain: string, p: Palette): string {
|
|
295
|
+
const height = 22;
|
|
296
|
+
const size = 15;
|
|
297
|
+
const x = height + 9;
|
|
298
|
+
const capHeight = (GEIST_SEMIBOLD.capHeight * size) / GEIST_SEMIBOLD.unitsPerEm;
|
|
299
|
+
const text = outlineText(domain, { size, x, y: (height + capHeight) / 2, fill: p.fg, tracking: -0.01 });
|
|
300
|
+
return logo(x + text.width, height, mark(0, height, p) + text.svg);
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
/**
|
|
304
|
+
* The app's name over the form. Cognito scales a form logo to a fixed height
|
|
305
|
+
* (about 58px), so the room around the name sets its size: about 36px here,
|
|
306
|
+
* smaller for names long enough to need padding.
|
|
307
|
+
*/
|
|
308
|
+
function appWordmark(app: string, p: Palette): string {
|
|
309
|
+
const size = 30;
|
|
310
|
+
const height = size * 1.6;
|
|
311
|
+
const capHeight = (GEIST_SEMIBOLD.capHeight * size) / GEIST_SEMIBOLD.unitsPerEm;
|
|
312
|
+
const text = outlineText(app, { size, x: 0, y: (height + capHeight) / 2, fill: p.fg, tracking: -0.02 });
|
|
313
|
+
return logo(text.width, height, text.svg);
|
|
314
|
+
}
|
|
315
|
+
|
|
316
|
+
// ------------------------------------------------------------------- favicon
|
|
317
|
+
|
|
318
|
+
/**
|
|
319
|
+
* The mark as an ICO (16, 32 and 48px PNGs) for browsers without SVG
|
|
320
|
+
* favicons, rasterized here so the geometry has one source.
|
|
321
|
+
*/
|
|
322
|
+
function markIco(p: Palette): Uint8Array {
|
|
323
|
+
const images = [16, 32, 48].map((size) => ({ size, png: png(size, rasterizeMark(size, p)) }));
|
|
324
|
+
const header = Buffer.alloc(6 + 16 * images.length);
|
|
325
|
+
header.writeUInt16LE(1, 2);
|
|
326
|
+
header.writeUInt16LE(images.length, 4);
|
|
327
|
+
let offset = header.length;
|
|
328
|
+
for (const [i, { size, png: data }] of images.entries()) {
|
|
329
|
+
const entry = 6 + 16 * i;
|
|
330
|
+
header.writeUInt8(size, entry);
|
|
331
|
+
header.writeUInt8(size, entry + 1);
|
|
332
|
+
header.writeUInt16LE(1, entry + 4);
|
|
333
|
+
header.writeUInt16LE(32, entry + 6);
|
|
334
|
+
header.writeUInt32LE(data.length, entry + 8);
|
|
335
|
+
header.writeUInt32LE(offset, entry + 12);
|
|
336
|
+
offset += data.length;
|
|
337
|
+
}
|
|
338
|
+
return Buffer.concat([header, ...images.map((i) => i.png)]);
|
|
339
|
+
}
|
|
340
|
+
|
|
341
|
+
function insideRoundedRect(x: number, y: number, at: number, size: number, r: number): boolean {
|
|
342
|
+
if (x < at || y < at || x > at + size || y > at + size) return false;
|
|
343
|
+
const cx = Math.min(Math.max(x, at + r), at + size - r);
|
|
344
|
+
const cy = Math.min(Math.max(y, at + r), at + size - r);
|
|
345
|
+
return (x - cx) ** 2 + (y - cy) ** 2 <= r * r;
|
|
346
|
+
}
|
|
347
|
+
|
|
348
|
+
/** RGBA pixels, 4x4 samples a pixel. */
|
|
349
|
+
function rasterizeMark(size: number, p: Palette): Uint8Array {
|
|
350
|
+
const outer = [...Buffer.from(p.fg.slice(1), 'hex')];
|
|
351
|
+
const inner = [...Buffer.from(p.page.slice(1), 'hex')];
|
|
352
|
+
const out = new Uint8Array(size * size * 4);
|
|
353
|
+
const n = 4;
|
|
354
|
+
const unit = MARK.size / size;
|
|
355
|
+
for (let py = 0; py < size; py++) {
|
|
356
|
+
for (let px = 0; px < size; px++) {
|
|
357
|
+
const sum = [0, 0, 0];
|
|
358
|
+
let covered = 0;
|
|
359
|
+
for (let sy = 0; sy < n; sy++) {
|
|
360
|
+
for (let sx = 0; sx < n; sx++) {
|
|
361
|
+
const x = (px + (sx + 0.5) / n) * unit;
|
|
362
|
+
const y = (py + (sy + 0.5) / n) * unit;
|
|
363
|
+
const i = MARK.inner;
|
|
364
|
+
const color = insideRoundedRect(x, y, i.at, i.size, i.radius)
|
|
365
|
+
? inner
|
|
366
|
+
: insideRoundedRect(x, y, 0, MARK.size, MARK.radius)
|
|
367
|
+
? outer
|
|
368
|
+
: undefined;
|
|
369
|
+
if (!color) continue;
|
|
370
|
+
covered++;
|
|
371
|
+
for (let c = 0; c < 3; c++) sum[c]! += color[c]!;
|
|
372
|
+
}
|
|
373
|
+
}
|
|
374
|
+
const o = (py * size + px) * 4;
|
|
375
|
+
if (!covered) continue;
|
|
376
|
+
for (let c = 0; c < 3; c++) out[o + c] = Math.round(sum[c]! / covered);
|
|
377
|
+
out[o + 3] = Math.round((255 * covered) / (n * n));
|
|
378
|
+
}
|
|
379
|
+
}
|
|
380
|
+
return out;
|
|
381
|
+
}
|
|
382
|
+
|
|
383
|
+
function png(size: number, rgbaPixels: Uint8Array): Buffer {
|
|
384
|
+
const chunk = (type: string, data: Buffer) => {
|
|
385
|
+
const out = Buffer.alloc(12 + data.length);
|
|
386
|
+
out.writeUInt32BE(data.length, 0);
|
|
387
|
+
out.write(type, 4, 'latin1');
|
|
388
|
+
data.copy(out, 8);
|
|
389
|
+
out.writeUInt32BE(crc32(out.subarray(4, 8 + data.length)), 8 + data.length);
|
|
390
|
+
return out;
|
|
391
|
+
};
|
|
392
|
+
const ihdr = Buffer.alloc(13);
|
|
393
|
+
ihdr.writeUInt32BE(size, 0);
|
|
394
|
+
ihdr.writeUInt32BE(size, 4);
|
|
395
|
+
ihdr[8] = 8; // bits per channel
|
|
396
|
+
ihdr[9] = 6; // RGBA
|
|
397
|
+
const stride = size * 4;
|
|
398
|
+
const raw = Buffer.alloc(size * (stride + 1));
|
|
399
|
+
for (let y = 0; y < size; y++)
|
|
400
|
+
raw.set(rgbaPixels.subarray(y * stride, (y + 1) * stride), y * (stride + 1) + 1);
|
|
401
|
+
return Buffer.concat([
|
|
402
|
+
Buffer.from([0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a]),
|
|
403
|
+
chunk('IHDR', ihdr),
|
|
404
|
+
chunk('IDAT', deflateSync(raw)),
|
|
405
|
+
chunk('IEND', Buffer.alloc(0)),
|
|
406
|
+
]);
|
|
407
|
+
}
|