monopix 0.0.0-stage → 1.0.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/README.md +126 -2
- package/dist/index.cjs +421 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +117 -0
- package/dist/index.d.ts +117 -0
- package/dist/index.js +415 -0
- package/dist/index.js.map +1 -0
- package/package.json +44 -4
package/README.md
CHANGED
|
@@ -1,3 +1,127 @@
|
|
|
1
|
-
#
|
|
1
|
+
# monopix
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
> Lightweight, zero-dependency JavaScript/TypeScript avatar generator creating pixel-art avatars from initials with extensible multi-mode architecture.
|
|
4
|
+
|
|
5
|
+
## Features
|
|
6
|
+
|
|
7
|
+
- **Initial Extractor**: Unicode & Arabic-aware character extraction.
|
|
8
|
+
- **Pixel Mode Engine**:
|
|
9
|
+
- Handcrafted, crisp 5x5 and 7x7 bitmap fonts for Latin (A-Z, 0-9) and Arabic alphabet (أ-ي).
|
|
10
|
+
- Algorithmic scaling to any arbitrary grid size ($N \times N$, e.g. 10x10, 16x16).
|
|
11
|
+
- Deterministic procedural fallback for symbols, emojis, and rare glyphs.
|
|
12
|
+
- **Customizable Styling**:
|
|
13
|
+
- Foreground letter color & background color.
|
|
14
|
+
- Pixel shapes: `square`, `circle`, `rounded`.
|
|
15
|
+
- Avatar shapes: `square`, `circle`.
|
|
16
|
+
- Configurable padding, pixel radius, and gap.
|
|
17
|
+
- **Multiple Output Formats**:
|
|
18
|
+
- `.toSVG()`: Crisp standalone SVG markup.
|
|
19
|
+
- `.toDataURI()`: Data URI for `<img src="...">` (UTF-8 or base64).
|
|
20
|
+
- `.renderToCanvas()`: Direct rendering into HTML5 Canvas.
|
|
21
|
+
- `.getGrid()`: 2D boolean grid matrix.
|
|
22
|
+
- **Multi-Mode Extensibility**: Easily register custom avatar modes with `registerMode()`.
|
|
23
|
+
- **Zero Runtime Dependencies**: Ultra-fast, runs anywhere (Node.js, Browser, Deno, Bun, Cloudflare Workers).
|
|
24
|
+
|
|
25
|
+
---
|
|
26
|
+
|
|
27
|
+
## Installation
|
|
28
|
+
|
|
29
|
+
```bash
|
|
30
|
+
npm install monopix
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
---
|
|
34
|
+
|
|
35
|
+
## Quick Start
|
|
36
|
+
|
|
37
|
+
### ESM / TypeScript
|
|
38
|
+
```typescript
|
|
39
|
+
import { createAvatar } from 'monopix';
|
|
40
|
+
|
|
41
|
+
const avatar = createAvatar({
|
|
42
|
+
name: 'Ahmed',
|
|
43
|
+
size: 128,
|
|
44
|
+
options: {
|
|
45
|
+
gridSize: 7,
|
|
46
|
+
color: '#10B981',
|
|
47
|
+
background: '#0F172A',
|
|
48
|
+
},
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
// Output formats:
|
|
52
|
+
const svg = avatar.toSVG();
|
|
53
|
+
const dataUri = avatar.toDataURI();
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
### CommonJS
|
|
57
|
+
```javascript
|
|
58
|
+
const { createAvatar } = require('monopix');
|
|
59
|
+
|
|
60
|
+
const avatar = createAvatar({
|
|
61
|
+
name: 'سارة',
|
|
62
|
+
options: {
|
|
63
|
+
gridSize: 7,
|
|
64
|
+
shape: 'circle',
|
|
65
|
+
pixelShape: 'circle',
|
|
66
|
+
color: '#F43F5E',
|
|
67
|
+
background: '#1E1B4B',
|
|
68
|
+
},
|
|
69
|
+
});
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
---
|
|
73
|
+
|
|
74
|
+
## API Reference
|
|
75
|
+
|
|
76
|
+
### `createAvatar(options: MonopixOptions): MonopixAvatar`
|
|
77
|
+
Alias: `monopix(options)`.
|
|
78
|
+
|
|
79
|
+
#### `MonopixOptions`
|
|
80
|
+
| Property | Type | Default | Description |
|
|
81
|
+
|---|---|---|---|
|
|
82
|
+
| `name` | `string` | *(required)* | Name, username, or text to extract the initial from |
|
|
83
|
+
| `mode` | `string` | `'pixel'` | Avatar mode (`pixel` or custom registered mode) |
|
|
84
|
+
| `size` | `number` | `128` | Output width and height in pixels |
|
|
85
|
+
| `options`| `PixelOptions` | `{}` | Mode-specific options |
|
|
86
|
+
|
|
87
|
+
#### `PixelOptions`
|
|
88
|
+
| Option | Type | Default | Description |
|
|
89
|
+
|---|---|---|---|
|
|
90
|
+
| `gridSize` | `number` | `7` | Size of the pixel grid (e.g. 5 for 5x5, 7 for 7x7, 14 for 14x14) |
|
|
91
|
+
| `color` | `string` | `'#FFFFFF'` | CSS color string for letter pixels |
|
|
92
|
+
| `background` | `string` | `'#0F172A'` | CSS color string for the avatar background |
|
|
93
|
+
| `shape` | `'square' \| 'circle'` | `'square'` | Outer shape of the avatar |
|
|
94
|
+
| `pixelShape` | `'square' \| 'circle' \| 'rounded'` | `'square'` | Shape of individual pixels |
|
|
95
|
+
| `padding` | `number` | `1` | Padding (in grid cells) around the character |
|
|
96
|
+
| `pixelRadius`| `number` | `0.25` | Corner radius ratio when `pixelShape: 'rounded'` (0 to 0.5) |
|
|
97
|
+
| `gap` | `number` | `0` | Spacing between pixels (0 to 0.5) |
|
|
98
|
+
|
|
99
|
+
---
|
|
100
|
+
|
|
101
|
+
## Custom Modes
|
|
102
|
+
|
|
103
|
+
You can register custom avatar modes:
|
|
104
|
+
|
|
105
|
+
```typescript
|
|
106
|
+
import { registerMode, createAvatar } from 'monopix';
|
|
107
|
+
|
|
108
|
+
registerMode('initials-circle', (name, size, options) => {
|
|
109
|
+
const char = name.charAt(0).toUpperCase();
|
|
110
|
+
return {
|
|
111
|
+
char,
|
|
112
|
+
grid: [[true]],
|
|
113
|
+
svg: `<svg viewBox="0 0 ${size} ${size}"><circle cx="${size/2}" cy="${size/2}" r="${size/2}" fill="#3B82F6"/><text x="50%" y="55%" text-anchor="middle" fill="#FFF">${char}</text></svg>`,
|
|
114
|
+
};
|
|
115
|
+
});
|
|
116
|
+
|
|
117
|
+
const avatar = createAvatar({
|
|
118
|
+
name: 'John',
|
|
119
|
+
mode: 'initials-circle',
|
|
120
|
+
});
|
|
121
|
+
```
|
|
122
|
+
|
|
123
|
+
---
|
|
124
|
+
|
|
125
|
+
## License
|
|
126
|
+
|
|
127
|
+
MIT
|
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,421 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
// src/utils/char.ts
|
|
4
|
+
function extractFirstChar(input) {
|
|
5
|
+
if (!input || typeof input !== "string") {
|
|
6
|
+
return "?";
|
|
7
|
+
}
|
|
8
|
+
const trimmed = input.trim();
|
|
9
|
+
if (trimmed.length === 0) {
|
|
10
|
+
return "?";
|
|
11
|
+
}
|
|
12
|
+
if (typeof Intl !== "undefined" && typeof Intl.Segmenter === "function") {
|
|
13
|
+
const segmenter = new Intl.Segmenter(void 0, { granularity: "grapheme" });
|
|
14
|
+
const segments = Array.from(segmenter.segment(trimmed));
|
|
15
|
+
if (segments.length > 0) {
|
|
16
|
+
const first2 = segments[0].segment;
|
|
17
|
+
return normalizeChar(first2);
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
const codePoints = Array.from(trimmed);
|
|
21
|
+
const first = codePoints[0] || "?";
|
|
22
|
+
return normalizeChar(first);
|
|
23
|
+
}
|
|
24
|
+
function normalizeChar(char) {
|
|
25
|
+
return char.toLocaleUpperCase();
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
// src/modes/pixel/matrices.ts
|
|
29
|
+
var LATIN_5X5 = {
|
|
30
|
+
A: ["01110", "10001", "11111", "10001", "10001"],
|
|
31
|
+
B: ["11110", "10001", "11110", "10001", "11110"],
|
|
32
|
+
C: ["01111", "10000", "10000", "10000", "01111"],
|
|
33
|
+
D: ["11110", "10001", "10001", "10001", "11110"],
|
|
34
|
+
E: ["11111", "10000", "11110", "10000", "11111"],
|
|
35
|
+
F: ["11111", "10000", "11110", "10000", "10000"],
|
|
36
|
+
G: ["01111", "10000", "10111", "10001", "01110"],
|
|
37
|
+
H: ["10001", "10001", "11111", "10001", "10001"],
|
|
38
|
+
I: ["11111", "00100", "00100", "00100", "11111"],
|
|
39
|
+
J: ["00111", "00010", "00010", "10010", "01100"],
|
|
40
|
+
K: ["10001", "10010", "11100", "10010", "10001"],
|
|
41
|
+
L: ["10000", "10000", "10000", "10000", "11111"],
|
|
42
|
+
M: ["10001", "11011", "10101", "10001", "10001"],
|
|
43
|
+
N: ["10001", "11001", "10101", "10011", "10001"],
|
|
44
|
+
O: ["01110", "10001", "10001", "10001", "01110"],
|
|
45
|
+
P: ["11110", "10001", "11110", "10000", "10000"],
|
|
46
|
+
Q: ["01110", "10001", "10001", "10011", "01111"],
|
|
47
|
+
R: ["11110", "10001", "11110", "10010", "10001"],
|
|
48
|
+
S: ["01111", "10000", "01110", "00001", "11110"],
|
|
49
|
+
T: ["11111", "00100", "00100", "00100", "00100"],
|
|
50
|
+
U: ["10001", "10001", "10001", "10001", "01110"],
|
|
51
|
+
V: ["10001", "10001", "10001", "01010", "00100"],
|
|
52
|
+
W: ["10001", "10001", "10101", "11011", "10001"],
|
|
53
|
+
X: ["10001", "01010", "00100", "01010", "10001"],
|
|
54
|
+
Y: ["10001", "01010", "00100", "00100", "00100"],
|
|
55
|
+
Z: ["11111", "00010", "00100", "01000", "11111"],
|
|
56
|
+
"0": ["01110", "10011", "10101", "11001", "01110"],
|
|
57
|
+
"1": ["00100", "01100", "00100", "00100", "01110"],
|
|
58
|
+
"2": ["11110", "00001", "01110", "10000", "11111"],
|
|
59
|
+
"3": ["11110", "00001", "01110", "00001", "11110"],
|
|
60
|
+
"4": ["10001", "10001", "11111", "00001", "00001"],
|
|
61
|
+
"5": ["11111", "10000", "11110", "00001", "11110"],
|
|
62
|
+
"6": ["01111", "10000", "11110", "10001", "01110"],
|
|
63
|
+
"7": ["11111", "00001", "00010", "00100", "00100"],
|
|
64
|
+
"8": ["01110", "10001", "01110", "10001", "01110"],
|
|
65
|
+
"9": ["01110", "10001", "01111", "00001", "11110"],
|
|
66
|
+
"?": ["01110", "10001", "00110", "00000", "00100"]
|
|
67
|
+
};
|
|
68
|
+
var ARABIC_5X5 = {
|
|
69
|
+
\u0627: ["00100", "00100", "00100", "00100", "00100"],
|
|
70
|
+
\u0628: ["00000", "10001", "11111", "00000", "00100"],
|
|
71
|
+
\u062A: ["01010", "00000", "10001", "11111", "00000"],
|
|
72
|
+
\u062B: ["00100", "01010", "10001", "11111", "00000"],
|
|
73
|
+
\u062C: ["11110", "00010", "00110", "00010", "11100"],
|
|
74
|
+
\u062D: ["11110", "00010", "00100", "00010", "11100"],
|
|
75
|
+
\u062E: ["00100", "11110", "00010", "00010", "11100"],
|
|
76
|
+
\u062F: ["00000", "00010", "00010", "11110", "00000"],
|
|
77
|
+
\u0630: ["00010", "00000", "00010", "11110", "00000"],
|
|
78
|
+
\u0631: ["00010", "00010", "00100", "01000", "10000"],
|
|
79
|
+
\u0632: ["00010", "00000", "00010", "00100", "01000"],
|
|
80
|
+
\u0633: ["10101", "10101", "11111", "00001", "11110"],
|
|
81
|
+
\u0634: ["01110", "10101", "11111", "00001", "11110"],
|
|
82
|
+
\u0635: ["01100", "10010", "11111", "00001", "11110"],
|
|
83
|
+
\u0636: ["00100", "01100", "11111", "00001", "11110"],
|
|
84
|
+
\u0637: ["00100", "00100", "01110", "10010", "11111"],
|
|
85
|
+
\u0638: ["01100", "00100", "01110", "10010", "11111"],
|
|
86
|
+
\u0639: ["01110", "10000", "01110", "00001", "11110"],
|
|
87
|
+
\u063A: ["00100", "01110", "01110", "00001", "11110"],
|
|
88
|
+
\u0641: ["01100", "10010", "01100", "11111", "00000"],
|
|
89
|
+
\u0642: ["10100", "01100", "10010", "01101", "11110"],
|
|
90
|
+
\u0643: ["10000", "10100", "11000", "10100", "11111"],
|
|
91
|
+
\u0644: ["00010", "00010", "00010", "00010", "11100"],
|
|
92
|
+
\u0645: ["01100", "10010", "01100", "00010", "00010"],
|
|
93
|
+
\u0646: ["00100", "00000", "10001", "10001", "01110"],
|
|
94
|
+
\u0647: ["01110", "10101", "10101", "10001", "01110"],
|
|
95
|
+
\u0648: ["01100", "10010", "01100", "00100", "01000"],
|
|
96
|
+
\u064A: ["00000", "10001", "01110", "00000", "01010"]
|
|
97
|
+
};
|
|
98
|
+
var LATIN_7X7 = {
|
|
99
|
+
A: ["0011100", "0100010", "1000001", "1111111", "1000001", "1000001", "1000001"],
|
|
100
|
+
B: ["1111110", "1000001", "1000001", "1111110", "1000001", "1000001", "1111110"],
|
|
101
|
+
C: ["0011110", "0100001", "1000000", "1000000", "1000000", "0100001", "0011110"],
|
|
102
|
+
D: ["1111100", "1000010", "1000001", "1000001", "1000001", "1000010", "1111100"],
|
|
103
|
+
E: ["1111111", "1000000", "1000000", "1111110", "1000000", "1000000", "1111111"],
|
|
104
|
+
F: ["1111111", "1000000", "1000000", "1111110", "1000000", "1000000", "1000000"],
|
|
105
|
+
G: ["0011110", "0100001", "1000000", "1001111", "1000001", "0100001", "0011110"],
|
|
106
|
+
H: ["1000001", "1000001", "1000001", "1111111", "1000001", "1000001", "1000001"],
|
|
107
|
+
I: ["0111110", "0001000", "0001000", "0001000", "0001000", "0001000", "0111110"],
|
|
108
|
+
J: ["0000111", "0000010", "0000010", "0000010", "1000010", "1000010", "0111100"],
|
|
109
|
+
K: ["1000011", "1000110", "1001100", "1111000", "1001100", "1000110", "1000011"],
|
|
110
|
+
L: ["1000000", "1000000", "1000000", "1000000", "1000000", "1000000", "1111111"],
|
|
111
|
+
M: ["1000001", "1100011", "1010101", "1001001", "1000001", "1000001", "1000001"],
|
|
112
|
+
N: ["1000001", "1100001", "1010001", "1001001", "1000101", "1000011", "1000001"],
|
|
113
|
+
O: ["0011100", "0100010", "1000001", "1000001", "1000001", "0100010", "0011100"],
|
|
114
|
+
P: ["1111110", "1000001", "1000001", "1111110", "1000000", "1000000", "1000000"],
|
|
115
|
+
Q: ["0011100", "0100010", "1000001", "1000001", "1000101", "0100010", "0011101"],
|
|
116
|
+
R: ["1111110", "1000001", "1000001", "1111110", "1000110", "1000011", "1000001"],
|
|
117
|
+
S: ["0111110", "1000001", "1000000", "0111110", "0000001", "1000001", "0111110"],
|
|
118
|
+
T: ["1111111", "0001000", "0001000", "0001000", "0001000", "0001000", "0001000"],
|
|
119
|
+
U: ["1000001", "1000001", "1000001", "1000001", "1000001", "0100010", "0011100"],
|
|
120
|
+
V: ["1000001", "1000001", "1000001", "0100010", "0100010", "0010100", "0001000"],
|
|
121
|
+
W: ["1000001", "1000001", "1000001", "1001001", "1010101", "1100011", "1000001"],
|
|
122
|
+
X: ["1000001", "0100010", "0010100", "0001000", "0010100", "0100010", "1000001"],
|
|
123
|
+
Y: ["1000001", "0100010", "0010100", "0001000", "0001000", "0001000", "0001000"],
|
|
124
|
+
Z: ["1111111", "0000010", "0000100", "0001000", "0010000", "0100000", "1111111"],
|
|
125
|
+
"0": ["0011100", "0100010", "1000101", "1001001", "1010001", "0100010", "0011100"],
|
|
126
|
+
"1": ["0001000", "0011000", "0101000", "0001000", "0001000", "0001000", "0111110"],
|
|
127
|
+
"2": ["0111110", "1000001", "0000010", "0001100", "0010000", "0100000", "1111111"],
|
|
128
|
+
"3": ["0111110", "1000001", "0000010", "0011100", "0000010", "1000001", "0111110"],
|
|
129
|
+
"4": ["0000110", "0001010", "0010010", "0100010", "1111111", "0000010", "0000010"],
|
|
130
|
+
"5": ["1111111", "1000000", "1111110", "0000001", "0000001", "1000001", "0111110"],
|
|
131
|
+
"6": ["0011110", "0100000", "1000000", "1111110", "1000001", "0100010", "0011100"],
|
|
132
|
+
"7": ["1111111", "0000010", "0000100", "0001000", "0010000", "0010000", "0010000"],
|
|
133
|
+
"8": ["0111110", "1000001", "1000001", "0111110", "1000001", "1000001", "0111110"],
|
|
134
|
+
"9": ["0011100", "0100010", "1000001", "0111111", "0000001", "0000010", "0111100"],
|
|
135
|
+
"?": ["0011100", "0100010", "0000010", "0001100", "0001000", "0000000", "0001000"]
|
|
136
|
+
};
|
|
137
|
+
var ARABIC_7X7 = {
|
|
138
|
+
\u0627: ["0001000", "0001000", "0001000", "0001000", "0001000", "0001000", "0001000"],
|
|
139
|
+
\u0628: ["0000000", "0000000", "1000001", "1000001", "1111111", "0000000", "0001000"],
|
|
140
|
+
\u062A: ["0000000", "0010100", "0000000", "1000001", "1000001", "1111111", "0000000"],
|
|
141
|
+
\u062B: ["0001000", "0010100", "0000000", "1000001", "1000001", "1111111", "0000000"],
|
|
142
|
+
\u062C: ["0111110", "0000010", "0000100", "0001100", "0000010", "0000010", "0111110"],
|
|
143
|
+
\u062D: ["0111110", "0000010", "0000100", "0000100", "0000010", "0000010", "0111110"],
|
|
144
|
+
\u062E: ["0001000", "0111110", "0000010", "0000100", "0000010", "0000010", "0111110"],
|
|
145
|
+
\u062F: ["0000000", "0000010", "0000010", "0000010", "0000010", "1111110", "0000000"],
|
|
146
|
+
\u0630: ["0000010", "0000000", "0000010", "0000010", "0000010", "1111110", "0000000"],
|
|
147
|
+
\u0631: ["0000010", "0000010", "0000100", "0001000", "0010000", "0100000", "1000000"],
|
|
148
|
+
\u0632: ["0000010", "0000000", "0000010", "0000100", "0001000", "0010000", "0100000"],
|
|
149
|
+
\u0633: ["1001001", "1001001", "1001001", "1111111", "0000001", "0000001", "1111110"],
|
|
150
|
+
\u0634: ["0011100", "1001001", "1001001", "1111111", "0000001", "0000001", "1111110"],
|
|
151
|
+
\u0635: ["0011100", "0100010", "1000010", "1111111", "0000001", "0000001", "1111110"],
|
|
152
|
+
\u0636: ["0001000", "0011100", "0100010", "1111111", "0000001", "0000001", "1111110"],
|
|
153
|
+
\u0637: ["0001000", "0001000", "0001000", "0011110", "0100010", "1000010", "1111111"],
|
|
154
|
+
\u0638: ["0011000", "0001000", "0001000", "0011110", "0100010", "1000010", "1111111"],
|
|
155
|
+
\u0639: ["0011100", "0100010", "0000010", "0011110", "0000001", "0000001", "1111110"],
|
|
156
|
+
\u063A: ["0001000", "0011100", "0100010", "0011110", "0000001", "0000001", "1111110"],
|
|
157
|
+
\u0641: ["0001000", "0011100", "0100010", "0011100", "0000010", "1111111", "0000000"],
|
|
158
|
+
\u0642: ["0010100", "0011100", "0100010", "0011100", "0000001", "0000001", "1111110"],
|
|
159
|
+
\u0643: ["1000000", "1000100", "1001000", "1011000", "1001000", "1000100", "1111111"],
|
|
160
|
+
\u0644: ["0000100", "0000100", "0000100", "0000100", "0000100", "0000100", "1111100"],
|
|
161
|
+
\u0645: ["0011100", "0100010", "0011100", "0000010", "0000010", "0000010", "0000010"],
|
|
162
|
+
\u0646: ["0001000", "0000000", "1000001", "1000001", "1000001", "0100010", "0011100"],
|
|
163
|
+
\u0647: ["0011100", "0100010", "1001001", "1001001", "1000001", "0100010", "0011100"],
|
|
164
|
+
\u0648: ["0011100", "0100010", "0011100", "0000100", "0001000", "0010000", "0100000"],
|
|
165
|
+
\u064A: ["0000000", "0000000", "1000001", "0100010", "0011100", "0000000", "0010100"]
|
|
166
|
+
};
|
|
167
|
+
function normalizeArabicChar(char) {
|
|
168
|
+
const map = {
|
|
169
|
+
\u0623: "\u0627",
|
|
170
|
+
\u0625: "\u0627",
|
|
171
|
+
\u0622: "\u0627",
|
|
172
|
+
\u0671: "\u0627",
|
|
173
|
+
\u0629: "\u0647",
|
|
174
|
+
\u0649: "\u064A",
|
|
175
|
+
\u0624: "\u0648",
|
|
176
|
+
\u0626: "\u064A",
|
|
177
|
+
\u0621: "\u0627"
|
|
178
|
+
};
|
|
179
|
+
return map[char] || char;
|
|
180
|
+
}
|
|
181
|
+
function getGlyphMatrix(char, preferredSize = 7) {
|
|
182
|
+
const normChar = normalizeArabicChar(char.toLocaleUpperCase());
|
|
183
|
+
if (preferredSize <= 5) {
|
|
184
|
+
if (LATIN_5X5[normChar]) return { matrix: LATIN_5X5[normChar], baseSize: 5 };
|
|
185
|
+
if (ARABIC_5X5[normChar]) return { matrix: ARABIC_5X5[normChar], baseSize: 5 };
|
|
186
|
+
} else {
|
|
187
|
+
if (LATIN_7X7[normChar]) return { matrix: LATIN_7X7[normChar], baseSize: 7 };
|
|
188
|
+
if (ARABIC_7X7[normChar]) return { matrix: ARABIC_7X7[normChar], baseSize: 7 };
|
|
189
|
+
if (LATIN_5X5[normChar]) return { matrix: LATIN_5X5[normChar], baseSize: 5 };
|
|
190
|
+
if (ARABIC_5X5[normChar]) return { matrix: ARABIC_5X5[normChar], baseSize: 5 };
|
|
191
|
+
}
|
|
192
|
+
return null;
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
// src/modes/pixel/rasterizer.ts
|
|
196
|
+
function createEmptyGrid(size) {
|
|
197
|
+
return Array.from({ length: size }, () => Array(size).fill(false));
|
|
198
|
+
}
|
|
199
|
+
function matrixToBooleanGrid(matrix) {
|
|
200
|
+
return matrix.map((row) => Array.from(row).map((cell) => cell === "1"));
|
|
201
|
+
}
|
|
202
|
+
function scaleGrid(src, targetSize) {
|
|
203
|
+
const srcHeight = src.length;
|
|
204
|
+
const srcWidth = src[0].length;
|
|
205
|
+
const target = createEmptyGrid(targetSize);
|
|
206
|
+
for (let r = 0; r < targetSize; r++) {
|
|
207
|
+
const srcR = Math.min(Math.floor(r / targetSize * srcHeight), srcHeight - 1);
|
|
208
|
+
for (let c = 0; c < targetSize; c++) {
|
|
209
|
+
const srcC = Math.min(Math.floor(c / targetSize * srcWidth), srcWidth - 1);
|
|
210
|
+
target[r][c] = src[srcR][srcC];
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
return target;
|
|
214
|
+
}
|
|
215
|
+
function generateFallbackGrid(char, gridSize) {
|
|
216
|
+
const grid = createEmptyGrid(gridSize);
|
|
217
|
+
let hash = 0;
|
|
218
|
+
for (let i = 0; i < char.length; i++) {
|
|
219
|
+
hash = (hash << 5) - hash + char.charCodeAt(i);
|
|
220
|
+
hash |= 0;
|
|
221
|
+
}
|
|
222
|
+
const halfWidth = Math.ceil(gridSize / 2);
|
|
223
|
+
for (let r = 0; r < gridSize; r++) {
|
|
224
|
+
for (let c = 0; c < halfWidth; c++) {
|
|
225
|
+
const bitIndex = (r * halfWidth + c) % 31;
|
|
226
|
+
const isFilled = (Math.abs(hash) >> bitIndex & 1) === 1;
|
|
227
|
+
grid[r][c] = isFilled;
|
|
228
|
+
grid[r][gridSize - 1 - c] = isFilled;
|
|
229
|
+
}
|
|
230
|
+
}
|
|
231
|
+
const hasPixel = grid.some((row) => row.some((cell) => cell));
|
|
232
|
+
if (!hasPixel) {
|
|
233
|
+
const mid = Math.floor(gridSize / 2);
|
|
234
|
+
grid[mid][mid] = true;
|
|
235
|
+
}
|
|
236
|
+
return grid;
|
|
237
|
+
}
|
|
238
|
+
function rasterizeChar(char, gridSize = 7) {
|
|
239
|
+
const validSize = Math.max(3, Math.floor(gridSize));
|
|
240
|
+
const glyph = getGlyphMatrix(char, validSize);
|
|
241
|
+
if (!glyph) {
|
|
242
|
+
return generateFallbackGrid(char, validSize);
|
|
243
|
+
}
|
|
244
|
+
const baseGrid = matrixToBooleanGrid(glyph.matrix);
|
|
245
|
+
if (glyph.baseSize === validSize) {
|
|
246
|
+
return baseGrid;
|
|
247
|
+
}
|
|
248
|
+
return scaleGrid(baseGrid, validSize);
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
// src/utils/sanitize.ts
|
|
252
|
+
function sanitizeColor(color, fallback) {
|
|
253
|
+
if (!color || typeof color !== "string") return fallback;
|
|
254
|
+
const trimmed = color.trim();
|
|
255
|
+
const safeColorRegex = /^#([0-9a-fA-F]{3,8})$|^rgba?\([0-9\s,.\/%]+\)$|^hsla?\([0-9\s,.\/%degturnrad]+\)$|^[a-zA-Z]+$/;
|
|
256
|
+
if (safeColorRegex.test(trimmed)) {
|
|
257
|
+
return trimmed;
|
|
258
|
+
}
|
|
259
|
+
return fallback;
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
// src/modes/pixel/renderer.ts
|
|
263
|
+
function renderPixelSvg({ grid, size, options = {} }) {
|
|
264
|
+
const gridSize = grid.length;
|
|
265
|
+
const padding = typeof options.padding === "number" && options.padding >= 0 ? options.padding : 1;
|
|
266
|
+
const color = sanitizeColor(options.color, "#FFFFFF");
|
|
267
|
+
const background = sanitizeColor(options.background, "#0F172A");
|
|
268
|
+
const pixelShape = options.pixelShape || "square";
|
|
269
|
+
const shape = options.shape || "square";
|
|
270
|
+
const gapRatio = Math.min(Math.max(options.gap ?? 0, 0), 0.5);
|
|
271
|
+
const pixelRadius = Math.min(Math.max(options.pixelRadius ?? 0.25, 0), 0.5);
|
|
272
|
+
const totalCells = gridSize + padding * 2;
|
|
273
|
+
const cellSize = size / totalCells;
|
|
274
|
+
const gapOffset = cellSize * (gapRatio / 2);
|
|
275
|
+
const cellRenderSize = cellSize * (1 - gapRatio);
|
|
276
|
+
let pixelsSvg = "";
|
|
277
|
+
for (let r = 0; r < gridSize; r++) {
|
|
278
|
+
for (let c = 0; c < gridSize; c++) {
|
|
279
|
+
if (!grid[r][c]) continue;
|
|
280
|
+
const x = (c + padding) * cellSize + gapOffset;
|
|
281
|
+
const y = (r + padding) * cellSize + gapOffset;
|
|
282
|
+
if (pixelShape === "circle") {
|
|
283
|
+
const radius = cellRenderSize / 2;
|
|
284
|
+
const cx = x + radius;
|
|
285
|
+
const cy = y + radius;
|
|
286
|
+
pixelsSvg += `<circle cx="${cx.toFixed(2)}" cy="${cy.toFixed(2)}" r="${radius.toFixed(2)}" fill="${color}"/>`;
|
|
287
|
+
} else if (pixelShape === "rounded") {
|
|
288
|
+
const rx = (cellRenderSize * pixelRadius).toFixed(2);
|
|
289
|
+
pixelsSvg += `<rect x="${x.toFixed(2)}" y="${y.toFixed(2)}" width="${cellRenderSize.toFixed(2)}" height="${cellRenderSize.toFixed(2)}" rx="${rx}" fill="${color}"/>`;
|
|
290
|
+
} else {
|
|
291
|
+
pixelsSvg += `<rect x="${x.toFixed(2)}" y="${y.toFixed(2)}" width="${cellRenderSize.toFixed(2)}" height="${cellRenderSize.toFixed(2)}" fill="${color}"/>`;
|
|
292
|
+
}
|
|
293
|
+
}
|
|
294
|
+
}
|
|
295
|
+
let clipDefinition = "";
|
|
296
|
+
let clipAttribute = "";
|
|
297
|
+
let backgroundElement = "";
|
|
298
|
+
if (shape === "circle") {
|
|
299
|
+
const half = size / 2;
|
|
300
|
+
clipDefinition = `<clipPath id="monopix-clip"><circle cx="${half}" cy="${half}" r="${half}"/></clipPath>`;
|
|
301
|
+
clipAttribute = ' clip-path="url(#monopix-clip)"';
|
|
302
|
+
backgroundElement = `<circle cx="${half}" cy="${half}" r="${half}" fill="${background}"/>`;
|
|
303
|
+
} else {
|
|
304
|
+
backgroundElement = `<rect width="${size}" height="${size}" fill="${background}"/>`;
|
|
305
|
+
}
|
|
306
|
+
return [
|
|
307
|
+
`<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 ${size} ${size}" width="${size}" height="${size}">`,
|
|
308
|
+
clipDefinition ? ` <defs>${clipDefinition}</defs>` : "",
|
|
309
|
+
` <g${clipAttribute}>`,
|
|
310
|
+
` ${backgroundElement}`,
|
|
311
|
+
` ${pixelsSvg}`,
|
|
312
|
+
" </g>",
|
|
313
|
+
"</svg>"
|
|
314
|
+
].filter(Boolean).join("\n");
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
// src/modes/pixel/index.ts
|
|
318
|
+
function renderPixelMode(name, size = 128, options = {}) {
|
|
319
|
+
const char = extractFirstChar(name);
|
|
320
|
+
const gridSize = options.gridSize && options.gridSize >= 3 ? Math.floor(options.gridSize) : 7;
|
|
321
|
+
const grid = rasterizeChar(char, gridSize);
|
|
322
|
+
const svg = renderPixelSvg({ grid, size, options });
|
|
323
|
+
return {
|
|
324
|
+
char,
|
|
325
|
+
grid,
|
|
326
|
+
svg
|
|
327
|
+
};
|
|
328
|
+
}
|
|
329
|
+
|
|
330
|
+
// src/modes/index.ts
|
|
331
|
+
var modeRegistry = /* @__PURE__ */ new Map();
|
|
332
|
+
modeRegistry.set("pixel", (name, size, options) => renderPixelMode(name, size, options));
|
|
333
|
+
function registerMode(modeName, renderer) {
|
|
334
|
+
modeRegistry.set(modeName.toLowerCase(), renderer);
|
|
335
|
+
}
|
|
336
|
+
function executeMode(options) {
|
|
337
|
+
const modeName = (options.mode || "pixel").toLowerCase();
|
|
338
|
+
const renderer = modeRegistry.get(modeName);
|
|
339
|
+
if (!renderer) {
|
|
340
|
+
throw new Error(`[monopix] Unsupported mode: "${modeName}". Registered modes: ${Array.from(modeRegistry.keys()).join(", ")}`);
|
|
341
|
+
}
|
|
342
|
+
const size = options.size && options.size > 0 ? options.size : 128;
|
|
343
|
+
return renderer(options.name, size, options.options);
|
|
344
|
+
}
|
|
345
|
+
|
|
346
|
+
// src/avatar.ts
|
|
347
|
+
var MonopixAvatar = class {
|
|
348
|
+
char;
|
|
349
|
+
grid;
|
|
350
|
+
svgContent;
|
|
351
|
+
options;
|
|
352
|
+
constructor(options) {
|
|
353
|
+
this.options = options;
|
|
354
|
+
const result = executeMode(options);
|
|
355
|
+
this.char = result.char;
|
|
356
|
+
this.grid = result.grid;
|
|
357
|
+
this.svgContent = result.svg;
|
|
358
|
+
}
|
|
359
|
+
/**
|
|
360
|
+
* Returns the generated avatar as an SVG markup string.
|
|
361
|
+
*/
|
|
362
|
+
toSVG() {
|
|
363
|
+
return this.svgContent;
|
|
364
|
+
}
|
|
365
|
+
/**
|
|
366
|
+
* Returns the avatar encoded as a data URI string suitable for <img src="...">.
|
|
367
|
+
*/
|
|
368
|
+
toDataURI(base64 = false) {
|
|
369
|
+
if (base64) {
|
|
370
|
+
if (typeof btoa === "function") {
|
|
371
|
+
return `data:image/svg+xml;base64,${btoa(unescape(encodeURIComponent(this.svgContent)))}`;
|
|
372
|
+
}
|
|
373
|
+
const maybeBuffer = globalThis.Buffer;
|
|
374
|
+
if (maybeBuffer && typeof maybeBuffer.from === "function") {
|
|
375
|
+
return `data:image/svg+xml;base64,${maybeBuffer.from(this.svgContent).toString("base64")}`;
|
|
376
|
+
}
|
|
377
|
+
}
|
|
378
|
+
return `data:image/svg+xml;utf8,${encodeURIComponent(this.svgContent)}`;
|
|
379
|
+
}
|
|
380
|
+
/**
|
|
381
|
+
* Renders the avatar into an HTML5 Canvas element (browser environments).
|
|
382
|
+
*/
|
|
383
|
+
renderToCanvas(canvas) {
|
|
384
|
+
return new Promise((resolve, reject) => {
|
|
385
|
+
const ctx = canvas.getContext("2d");
|
|
386
|
+
if (!ctx) {
|
|
387
|
+
return reject(new Error("[monopix] Canvas 2D context not available"));
|
|
388
|
+
}
|
|
389
|
+
const img = new Image();
|
|
390
|
+
img.onload = () => {
|
|
391
|
+
canvas.width = this.options.size || 128;
|
|
392
|
+
canvas.height = this.options.size || 128;
|
|
393
|
+
ctx.clearRect(0, 0, canvas.width, canvas.height);
|
|
394
|
+
ctx.drawImage(img, 0, 0);
|
|
395
|
+
resolve();
|
|
396
|
+
};
|
|
397
|
+
img.onerror = (err) => reject(err);
|
|
398
|
+
img.src = this.toDataURI();
|
|
399
|
+
});
|
|
400
|
+
}
|
|
401
|
+
/**
|
|
402
|
+
* Returns the boolean grid representation (true = pixel filled).
|
|
403
|
+
*/
|
|
404
|
+
getGrid() {
|
|
405
|
+
return this.grid.map((row) => [...row]);
|
|
406
|
+
}
|
|
407
|
+
};
|
|
408
|
+
|
|
409
|
+
// src/index.ts
|
|
410
|
+
function createAvatar(options) {
|
|
411
|
+
return new MonopixAvatar(options);
|
|
412
|
+
}
|
|
413
|
+
var monopix = createAvatar;
|
|
414
|
+
|
|
415
|
+
exports.MonopixAvatar = MonopixAvatar;
|
|
416
|
+
exports.createAvatar = createAvatar;
|
|
417
|
+
exports.extractFirstChar = extractFirstChar;
|
|
418
|
+
exports.monopix = monopix;
|
|
419
|
+
exports.registerMode = registerMode;
|
|
420
|
+
//# sourceMappingURL=index.cjs.map
|
|
421
|
+
//# sourceMappingURL=index.cjs.map
|