-
Notifications
You must be signed in to change notification settings - Fork 1
/
silicon.ts
100 lines (91 loc) · 2.03 KB
/
silicon.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
import {
font_list,
generate as generate_,
Options as Options_,
theme_list,
} from "./bindings/bindings.ts";
import { base64, is } from "./deps.ts";
const isError = is.ObjectOf({
Error: is.ObjectOf({
error: is.String,
}),
});
const isFontList = is.ObjectOf({
FontList: is.ObjectOf({
data: is.ArrayOf(is.String),
}),
});
const isThemeList = is.ObjectOf({
ThemeList: is.ObjectOf({
data: is.ArrayOf(is.String),
}),
});
const isImage = is.ObjectOf({
Image: is.ObjectOf({
data: is.String,
}),
});
/**
* Return available font list
*/
export function fontList(): string[] {
const result = font_list();
if (isError(result)) {
throw new Error(`cannot get font list: ${result.Error.error}`);
}
if (isFontList(result)) {
return result.FontList.data;
}
throw new Error(`unexpected result: ${JSON.stringify(result)}`);
}
/**
* Return available theme list
*/
export function themeList(): string[] {
const result = theme_list();
if (isThemeList(result)) {
return result.ThemeList.data;
}
throw new Error(`unexpected result: ${JSON.stringify(result)}`);
}
export type Options = Omit<Options_, "code" | "language">;
const defaultOptions: Options = {
font: "",
highlight_lines: "",
no_line_number: false,
no_round_corner: false,
no_window_controls: false,
background_color: "#aaaaff",
line_offset: 1,
line_pad: 2,
pad_horiz: 80,
pad_vert: 100,
shadow_blur_radius: 0,
shadow_color: "#555555",
shadow_offset_x: 0,
shadow_offset_y: 0,
tab_width: 4,
theme: "Solarized (dark)",
};
/*
* Generate an image from a code.
*/
export function generate(
code: string,
language: string,
opts: Partial<Options> = {},
): Uint8Array {
const result = generate_({
...defaultOptions,
...opts,
code: code,
language: language,
});
if (isError(result)) {
throw new Error(`cannot generate image: ${result.Error.error}`);
}
if (isImage(result)) {
return base64.decodeBase64(result.Image.data);
}
throw new Error(`unexpected result: ${JSON.stringify(result)}`);
}