mkicons.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463
  1. /* mkicons: rasterize Adwaita symbolic SVGs into an NBICO1 icon bundle.
  2. *
  3. * ./mkicons <theme-dir> <size> <out.icn> [-v]
  4. *
  5. * Every ICON_LIST entry (include/icon_abi.h) names <theme-dir>/<file>.svg.
  6. * The SVG is drawn with the browser's svg_raster.c (the same antialiased
  7. * rasterizer the browser uses for inline SVG) into a size x size coverage
  8. * mask. Symbolic icons are single-colour, so only coverage is kept; ngui
  9. * tints it at draw time. -v prints an ASCII preview of every mask.
  10. *
  11. * The XML reader is deliberately tiny: it handles what the symbolic theme
  12. * uses (<svg viewBox>, <g>, <path>, <rect>, <circle>, <ellipse>, fill,
  13. * fill-rule, opacity, fill-opacity) and fails loudly on anything else
  14. * (transforms, strokes, style attributes, unknown elements) rather than
  15. * producing a wrong icon. */
  16. #include <stdio.h>
  17. #include <stdlib.h>
  18. #include <string.h>
  19. #include "icon_abi.h"
  20. #include "../user/apps/browser/svg_raster.h"
  21. #define ICON_MAX_SIZE 64
  22. #define PT_CAP 16384
  23. #define ATTR_MAX 16384
  24. #define GROUP_MAX 16
  25. struct icon_def {
  26. const char *name;
  27. const char *file;
  28. };
  29. #define ICON_DEF_(e, name, file) { name, file },
  30. static const struct icon_def icons[] = { ICON_LIST(ICON_DEF_) };
  31. #undef ICON_DEF_
  32. #define NICONS ((int)(sizeof(icons) / sizeof(icons[0])))
  33. static const char *cur_file;
  34. static void die(const char *msg, const char *detail)
  35. {
  36. fprintf(stderr, "mkicons: %s: %s%s%s\n", cur_file ? cur_file : "-",
  37. msg, detail ? ": " : "", detail ? detail : "");
  38. exit(1);
  39. }
  40. static char *read_file(const char *path)
  41. {
  42. FILE *f = fopen(path, "rb");
  43. long n;
  44. char *buf;
  45. if (!f)
  46. return NULL;
  47. fseek(f, 0, SEEK_END);
  48. n = ftell(f);
  49. fseek(f, 0, SEEK_SET);
  50. buf = malloc((size_t)n + 1);
  51. if (!buf || fread(buf, 1, (size_t)n, f) != (size_t)n) {
  52. fclose(f);
  53. free(buf);
  54. return NULL;
  55. }
  56. buf[n] = 0;
  57. fclose(f);
  58. return buf;
  59. }
  60. /* One start tag: name and the raw attribute text up to '>' or '/>'. */
  61. struct tag {
  62. char name[32];
  63. const char *attrs, *attrs_end;
  64. int closing; /* </name> */
  65. int self_closing; /* <name ... /> */
  66. };
  67. static int is_space(char c)
  68. {
  69. return c == ' ' || c == '\t' || c == '\n' || c == '\r';
  70. }
  71. /* Advance to the next tag; returns 0 at the end of the document. */
  72. static int next_tag(const char **pp, struct tag *t)
  73. {
  74. const char *p = *pp, *e;
  75. unsigned int n = 0;
  76. for (;;) {
  77. p = strchr(p, '<');
  78. if (!p)
  79. return 0;
  80. if (!strncmp(p, "<!--", 4)) {
  81. e = strstr(p, "-->");
  82. if (!e)
  83. die("unterminated comment", NULL);
  84. p = e + 3;
  85. continue;
  86. }
  87. if (p[1] == '?' || p[1] == '!') {
  88. e = strchr(p, '>');
  89. if (!e)
  90. die("unterminated declaration", NULL);
  91. p = e + 1;
  92. continue;
  93. }
  94. break;
  95. }
  96. p++;
  97. memset(t, 0, sizeof(*t));
  98. if (*p == '/') {
  99. t->closing = 1;
  100. p++;
  101. }
  102. while (*p && !is_space(*p) && *p != '>' && *p != '/') {
  103. if (n + 1 < sizeof(t->name))
  104. t->name[n++] = *p;
  105. p++;
  106. }
  107. t->name[n] = 0;
  108. t->attrs = p;
  109. e = p;
  110. while (*e && *e != '>') {
  111. if (*e == '"' || *e == '\'') {
  112. char q = *e++;
  113. while (*e && *e != q)
  114. e++;
  115. if (!*e)
  116. die("unterminated attribute", t->name);
  117. }
  118. e++;
  119. }
  120. if (!*e)
  121. die("unterminated tag", t->name);
  122. t->attrs_end = e;
  123. if (e > p && e[-1] == '/') {
  124. t->self_closing = 1;
  125. t->attrs_end = e - 1;
  126. }
  127. *pp = e + 1;
  128. return 1;
  129. }
  130. /* Copy attribute `name` into out; returns 1 when present. */
  131. static int attr(const struct tag *t, const char *name, char *out,
  132. unsigned int outsz)
  133. {
  134. const char *p = t->attrs;
  135. size_t nl = strlen(name);
  136. while (p < t->attrs_end) {
  137. const char *ns, *ne, *vs;
  138. char q;
  139. unsigned int n = 0;
  140. while (p < t->attrs_end && is_space(*p))
  141. p++;
  142. ns = p;
  143. while (p < t->attrs_end && *p != '=' && !is_space(*p))
  144. p++;
  145. if (p >= t->attrs_end)
  146. break;
  147. ne = p;
  148. vs = p;
  149. while (vs < t->attrs_end && (is_space(*vs) || *vs == '='))
  150. vs++;
  151. if (vs >= t->attrs_end || (*vs != '"' && *vs != '\''))
  152. die("malformed attribute", t->name);
  153. q = *vs++;
  154. p = vs;
  155. while (p < t->attrs_end && *p != q)
  156. p++;
  157. if ((size_t)(ne - ns) == nl && !strncmp(ns, name, nl)) {
  158. if ((size_t)(p - vs) + 1 > outsz)
  159. die("attribute too long", name);
  160. while (vs + n < p) {
  161. out[n] = vs[n];
  162. n++;
  163. }
  164. out[n] = 0;
  165. return 1;
  166. }
  167. p++;
  168. }
  169. return 0;
  170. }
  171. /* Reject attributes the renderer would silently ignore. */
  172. static void check_attrs(const struct tag *t)
  173. {
  174. static const char *const known[] = {
  175. "xmlns", "xmlns:xlink", "width", "height", "viewBox", "version",
  176. "id", "class", "fill", "fill-rule", "clip-rule", "opacity",
  177. "fill-opacity", "d", "x", "y", "rx", "ry", "cx", "cy", "r",
  178. "color", NULL
  179. };
  180. const char *p = t->attrs;
  181. while (p < t->attrs_end) {
  182. const char *ns;
  183. char q;
  184. int i, ok = 0;
  185. while (p < t->attrs_end && is_space(*p))
  186. p++;
  187. ns = p;
  188. while (p < t->attrs_end && *p != '=' && !is_space(*p))
  189. p++;
  190. if (p >= t->attrs_end)
  191. break;
  192. for (i = 0; known[i]; i++)
  193. if ((size_t)(p - ns) == strlen(known[i]) &&
  194. !strncmp(ns, known[i], (size_t)(p - ns)))
  195. ok = 1;
  196. if (!ok) {
  197. char nm[64];
  198. size_t l = (size_t)(p - ns);
  199. if (l >= sizeof(nm))
  200. l = sizeof(nm) - 1;
  201. memcpy(nm, ns, l);
  202. nm[l] = 0;
  203. /* stroke="none" is harmless; any real stroke is not. */
  204. if (!strcmp(nm, "stroke")) {
  205. char v[32];
  206. if (attr(t, "stroke", v, sizeof(v)) &&
  207. !strcmp(v, "none"))
  208. ok = 1;
  209. }
  210. if (!ok)
  211. die("unsupported attribute", nm);
  212. }
  213. while (p < t->attrs_end && *p != '"' && *p != '\'')
  214. p++;
  215. q = *p++;
  216. while (p < t->attrs_end && *p != q)
  217. p++;
  218. p++;
  219. }
  220. }
  221. static double num_attr(const struct tag *t, const char *name, double def)
  222. {
  223. char v[64];
  224. if (!attr(t, name, v, sizeof(v)))
  225. return def;
  226. return strtod(v, NULL);
  227. }
  228. struct paint {
  229. double opacity; /* opacity * fill-opacity, inherited */
  230. int none; /* fill="none" */
  231. int evenodd;
  232. };
  233. static void apply_paint(const struct tag *t, struct paint *p)
  234. {
  235. char v[64];
  236. if (attr(t, "fill", v, sizeof(v)))
  237. p->none = !strcmp(v, "none");
  238. if (attr(t, "fill-rule", v, sizeof(v)))
  239. p->evenodd = !strcmp(v, "evenodd");
  240. p->opacity *= num_attr(t, "opacity", 1.0);
  241. p->opacity *= num_attr(t, "fill-opacity", 1.0);
  242. }
  243. static void render(const char *svg, int size, unsigned char *mask)
  244. {
  245. static unsigned char ptbuf[PT_CAP * (2 * sizeof(float) + 1)];
  246. static unsigned short px[ICON_MAX_SIZE * ICON_MAX_SIZE];
  247. static char d[ATTR_MAX];
  248. struct paint stack[GROUP_MAX];
  249. struct svgr r;
  250. struct svgr_layer L = { px, mask, size, size };
  251. struct tag t;
  252. const char *p = svg;
  253. double m[6] = { 1, 0, 0, 1, 0, 0 };
  254. int depth = 0, seen_svg = 0, shapes = 0;
  255. memset(mask, 0, (size_t)size * (size_t)size);
  256. memset(px, 0, sizeof(px));
  257. svgr_init(&r, ptbuf, PT_CAP);
  258. stack[0].opacity = 1.0;
  259. stack[0].none = 0;
  260. stack[0].evenodd = 0;
  261. while (next_tag(&p, &t)) {
  262. struct paint pt;
  263. if (t.closing) {
  264. if (!strcmp(t.name, "g") || !strcmp(t.name, "svg")) {
  265. if (depth == 0)
  266. die("unbalanced </g>", NULL);
  267. depth--;
  268. }
  269. continue;
  270. }
  271. if (!strcmp(t.name, "title") || !strcmp(t.name, "desc") ||
  272. !strcmp(t.name, "metadata")) {
  273. /* Skip the element's text; nested tags are not used. */
  274. continue;
  275. }
  276. check_attrs(&t);
  277. pt = stack[depth];
  278. apply_paint(&t, &pt);
  279. if (!strcmp(t.name, "svg")) {
  280. char vb[128];
  281. double vx = 0, vy = 0, vw = 16, vh = 16;
  282. if (seen_svg++)
  283. die("nested <svg>", NULL);
  284. if (attr(&t, "viewBox", vb, sizeof(vb))) {
  285. char *q = vb;
  286. vx = strtod(q, &q);
  287. while (*q == ',' || is_space(*q))
  288. q++;
  289. vy = strtod(q, &q);
  290. while (*q == ',' || is_space(*q))
  291. q++;
  292. vw = strtod(q, &q);
  293. while (*q == ',' || is_space(*q))
  294. q++;
  295. vh = strtod(q, &q);
  296. }
  297. if (vw <= 0 || vh <= 0 || vw != vh)
  298. die("viewBox must be square", vb);
  299. m[0] = m[3] = size / vw;
  300. m[4] = -vx * m[0];
  301. m[5] = -vy * m[3];
  302. } else if (!strcmp(t.name, "g")) {
  303. /* group: paint state only */
  304. } else if (!strcmp(t.name, "path") || !strcmp(t.name, "rect") ||
  305. !strcmp(t.name, "circle") ||
  306. !strcmp(t.name, "ellipse")) {
  307. if (!seen_svg)
  308. die("shape outside <svg>", t.name);
  309. svgr_reset(&r, m);
  310. if (!strcmp(t.name, "path")) {
  311. if (!attr(&t, "d", d, sizeof(d)))
  312. die("<path> without d", NULL);
  313. if (svgr_path(&r, d) != 0)
  314. die("bad path data", NULL);
  315. } else if (!strcmp(t.name, "rect")) {
  316. double rx = num_attr(&t, "rx", -1);
  317. double ry = num_attr(&t, "ry", -1);
  318. if (rx < 0)
  319. rx = ry < 0 ? 0 : ry;
  320. if (ry < 0)
  321. ry = rx;
  322. svgr_rect(&r, num_attr(&t, "x", 0),
  323. num_attr(&t, "y", 0),
  324. num_attr(&t, "width", 0),
  325. num_attr(&t, "height", 0), rx, ry);
  326. } else if (!strcmp(t.name, "circle")) {
  327. double rr = num_attr(&t, "r", 0);
  328. svgr_ellipse(&r, num_attr(&t, "cx", 0),
  329. num_attr(&t, "cy", 0), rr, rr);
  330. } else {
  331. svgr_ellipse(&r, num_attr(&t, "cx", 0),
  332. num_attr(&t, "cy", 0),
  333. num_attr(&t, "rx", 0),
  334. num_attr(&t, "ry", 0));
  335. }
  336. if (r.overflow)
  337. die("path too complex", NULL);
  338. if (!pt.none && pt.opacity > 0) {
  339. unsigned int a = (unsigned int)(pt.opacity * 255.0 + 0.5);
  340. svgr_fill(&r, &L, 0xFFFF, a > 255 ? 255 : a,
  341. pt.evenodd);
  342. shapes++;
  343. }
  344. } else {
  345. die("unsupported element", t.name);
  346. }
  347. if (!t.self_closing &&
  348. (!strcmp(t.name, "g") || !strcmp(t.name, "svg"))) {
  349. if (++depth >= GROUP_MAX)
  350. die("groups nested too deep", NULL);
  351. stack[depth] = pt;
  352. }
  353. }
  354. if (!shapes)
  355. die("no filled shape", NULL);
  356. }
  357. static void preview(const char *name, const unsigned char *mask, int size)
  358. {
  359. static const char ramp[] = " .:-=+*#%@";
  360. int x, y;
  361. fprintf(stderr, "%s\n", name);
  362. for (y = 0; y < size; y++) {
  363. for (x = 0; x < size; x++) {
  364. int v = mask[y * size + x] * 9 / 255;
  365. fputc(ramp[v], stderr);
  366. fputc(ramp[v], stderr);
  367. }
  368. fputc('\n', stderr);
  369. }
  370. }
  371. int main(int argc, char **argv)
  372. {
  373. static unsigned char mask[ICON_MAX_SIZE * ICON_MAX_SIZE];
  374. struct icon_file_hdr h;
  375. char path[1024];
  376. int size, verbose, i;
  377. FILE *out;
  378. if (argc < 4) {
  379. fprintf(stderr, "usage: %s <theme-dir> <size> <out.icn> [-v]\n",
  380. argv[0]);
  381. return 2;
  382. }
  383. size = atoi(argv[2]);
  384. verbose = argc > 4 && !strcmp(argv[4], "-v");
  385. if (size < 8 || size > ICON_MAX_SIZE) {
  386. fprintf(stderr, "mkicons: size must be 8..%d\n", ICON_MAX_SIZE);
  387. return 2;
  388. }
  389. out = fopen(argv[3], "wb");
  390. if (!out) {
  391. perror(argv[3]);
  392. return 1;
  393. }
  394. memset(&h, 0, sizeof(h));
  395. memcpy(h.magic, ICON_FILE_MAGIC, 8);
  396. h.count = (unsigned short)NICONS;
  397. h.size = (unsigned short)size;
  398. fwrite(&h, sizeof(h), 1, out);
  399. for (i = 0; i < NICONS; i++) {
  400. char *svg;
  401. snprintf(path, sizeof(path), "%s/%s.svg", argv[1],
  402. icons[i].file);
  403. cur_file = path;
  404. svg = read_file(path);
  405. if (!svg)
  406. die("cannot read", NULL);
  407. render(svg, size, mask);
  408. free(svg);
  409. if (verbose)
  410. preview(icons[i].name, mask, size);
  411. fwrite(mask, 1, (size_t)size * (size_t)size, out);
  412. }
  413. if (fclose(out) != 0) {
  414. perror(argv[3]);
  415. remove(argv[3]);
  416. return 1;
  417. }
  418. return 0;
  419. }