/* mkicons: rasterize Adwaita symbolic SVGs into an NBICO1 icon bundle. * * ./mkicons [-v] * * Every ICON_LIST entry (include/icon_abi.h) names /.svg. * The SVG is drawn with the browser's svg_raster.c (the same antialiased * rasterizer the browser uses for inline SVG) into a size x size coverage * mask. Symbolic icons are single-colour, so only coverage is kept; ngui * tints it at draw time. -v prints an ASCII preview of every mask. * * The XML reader is deliberately tiny: it handles what the symbolic theme * uses (, , , , , , fill, * fill-rule, opacity, fill-opacity) and fails loudly on anything else * (transforms, strokes, style attributes, unknown elements) rather than * producing a wrong icon. */ #include #include #include #include "icon_abi.h" #include "../user/apps/browser/svg_raster.h" #define ICON_MAX_SIZE 64 #define PT_CAP 16384 #define ATTR_MAX 16384 #define GROUP_MAX 16 struct icon_def { const char *name; const char *file; }; #define ICON_DEF_(e, name, file) { name, file }, static const struct icon_def icons[] = { ICON_LIST(ICON_DEF_) }; #undef ICON_DEF_ #define NICONS ((int)(sizeof(icons) / sizeof(icons[0]))) static const char *cur_file; static void die(const char *msg, const char *detail) { fprintf(stderr, "mkicons: %s: %s%s%s\n", cur_file ? cur_file : "-", msg, detail ? ": " : "", detail ? detail : ""); exit(1); } static char *read_file(const char *path) { FILE *f = fopen(path, "rb"); long n; char *buf; if (!f) return NULL; fseek(f, 0, SEEK_END); n = ftell(f); fseek(f, 0, SEEK_SET); buf = malloc((size_t)n + 1); if (!buf || fread(buf, 1, (size_t)n, f) != (size_t)n) { fclose(f); free(buf); return NULL; } buf[n] = 0; fclose(f); return buf; } /* One start tag: name and the raw attribute text up to '>' or '/>'. */ struct tag { char name[32]; const char *attrs, *attrs_end; int closing; /* */ int self_closing; /* */ }; static int is_space(char c) { return c == ' ' || c == '\t' || c == '\n' || c == '\r'; } /* Advance to the next tag; returns 0 at the end of the document. */ static int next_tag(const char **pp, struct tag *t) { const char *p = *pp, *e; unsigned int n = 0; for (;;) { p = strchr(p, '<'); if (!p) return 0; if (!strncmp(p, ""); if (!e) die("unterminated comment", NULL); p = e + 3; continue; } if (p[1] == '?' || p[1] == '!') { e = strchr(p, '>'); if (!e) die("unterminated declaration", NULL); p = e + 1; continue; } break; } p++; memset(t, 0, sizeof(*t)); if (*p == '/') { t->closing = 1; p++; } while (*p && !is_space(*p) && *p != '>' && *p != '/') { if (n + 1 < sizeof(t->name)) t->name[n++] = *p; p++; } t->name[n] = 0; t->attrs = p; e = p; while (*e && *e != '>') { if (*e == '"' || *e == '\'') { char q = *e++; while (*e && *e != q) e++; if (!*e) die("unterminated attribute", t->name); } e++; } if (!*e) die("unterminated tag", t->name); t->attrs_end = e; if (e > p && e[-1] == '/') { t->self_closing = 1; t->attrs_end = e - 1; } *pp = e + 1; return 1; } /* Copy attribute `name` into out; returns 1 when present. */ static int attr(const struct tag *t, const char *name, char *out, unsigned int outsz) { const char *p = t->attrs; size_t nl = strlen(name); while (p < t->attrs_end) { const char *ns, *ne, *vs; char q; unsigned int n = 0; while (p < t->attrs_end && is_space(*p)) p++; ns = p; while (p < t->attrs_end && *p != '=' && !is_space(*p)) p++; if (p >= t->attrs_end) break; ne = p; vs = p; while (vs < t->attrs_end && (is_space(*vs) || *vs == '=')) vs++; if (vs >= t->attrs_end || (*vs != '"' && *vs != '\'')) die("malformed attribute", t->name); q = *vs++; p = vs; while (p < t->attrs_end && *p != q) p++; if ((size_t)(ne - ns) == nl && !strncmp(ns, name, nl)) { if ((size_t)(p - vs) + 1 > outsz) die("attribute too long", name); while (vs + n < p) { out[n] = vs[n]; n++; } out[n] = 0; return 1; } p++; } return 0; } /* Reject attributes the renderer would silently ignore. */ static void check_attrs(const struct tag *t) { static const char *const known[] = { "xmlns", "xmlns:xlink", "width", "height", "viewBox", "version", "id", "class", "fill", "fill-rule", "clip-rule", "opacity", "fill-opacity", "d", "x", "y", "rx", "ry", "cx", "cy", "r", "color", NULL }; const char *p = t->attrs; while (p < t->attrs_end) { const char *ns; char q; int i, ok = 0; while (p < t->attrs_end && is_space(*p)) p++; ns = p; while (p < t->attrs_end && *p != '=' && !is_space(*p)) p++; if (p >= t->attrs_end) break; for (i = 0; known[i]; i++) if ((size_t)(p - ns) == strlen(known[i]) && !strncmp(ns, known[i], (size_t)(p - ns))) ok = 1; if (!ok) { char nm[64]; size_t l = (size_t)(p - ns); if (l >= sizeof(nm)) l = sizeof(nm) - 1; memcpy(nm, ns, l); nm[l] = 0; /* stroke="none" is harmless; any real stroke is not. */ if (!strcmp(nm, "stroke")) { char v[32]; if (attr(t, "stroke", v, sizeof(v)) && !strcmp(v, "none")) ok = 1; } if (!ok) die("unsupported attribute", nm); } while (p < t->attrs_end && *p != '"' && *p != '\'') p++; q = *p++; while (p < t->attrs_end && *p != q) p++; p++; } } static double num_attr(const struct tag *t, const char *name, double def) { char v[64]; if (!attr(t, name, v, sizeof(v))) return def; return strtod(v, NULL); } struct paint { double opacity; /* opacity * fill-opacity, inherited */ int none; /* fill="none" */ int evenodd; }; static void apply_paint(const struct tag *t, struct paint *p) { char v[64]; if (attr(t, "fill", v, sizeof(v))) p->none = !strcmp(v, "none"); if (attr(t, "fill-rule", v, sizeof(v))) p->evenodd = !strcmp(v, "evenodd"); p->opacity *= num_attr(t, "opacity", 1.0); p->opacity *= num_attr(t, "fill-opacity", 1.0); } static void render(const char *svg, int size, unsigned char *mask) { static unsigned char ptbuf[PT_CAP * (2 * sizeof(float) + 1)]; static unsigned short px[ICON_MAX_SIZE * ICON_MAX_SIZE]; static char d[ATTR_MAX]; struct paint stack[GROUP_MAX]; struct svgr r; struct svgr_layer L = { px, mask, size, size }; struct tag t; const char *p = svg; double m[6] = { 1, 0, 0, 1, 0, 0 }; int depth = 0, seen_svg = 0, shapes = 0; memset(mask, 0, (size_t)size * (size_t)size); memset(px, 0, sizeof(px)); svgr_init(&r, ptbuf, PT_CAP); stack[0].opacity = 1.0; stack[0].none = 0; stack[0].evenodd = 0; while (next_tag(&p, &t)) { struct paint pt; if (t.closing) { if (!strcmp(t.name, "g") || !strcmp(t.name, "svg")) { if (depth == 0) die("unbalanced ", NULL); depth--; } continue; } if (!strcmp(t.name, "title") || !strcmp(t.name, "desc") || !strcmp(t.name, "metadata")) { /* Skip the element's text; nested tags are not used. */ continue; } check_attrs(&t); pt = stack[depth]; apply_paint(&t, &pt); if (!strcmp(t.name, "svg")) { char vb[128]; double vx = 0, vy = 0, vw = 16, vh = 16; if (seen_svg++) die("nested ", NULL); if (attr(&t, "viewBox", vb, sizeof(vb))) { char *q = vb; vx = strtod(q, &q); while (*q == ',' || is_space(*q)) q++; vy = strtod(q, &q); while (*q == ',' || is_space(*q)) q++; vw = strtod(q, &q); while (*q == ',' || is_space(*q)) q++; vh = strtod(q, &q); } if (vw <= 0 || vh <= 0 || vw != vh) die("viewBox must be square", vb); m[0] = m[3] = size / vw; m[4] = -vx * m[0]; m[5] = -vy * m[3]; } else if (!strcmp(t.name, "g")) { /* group: paint state only */ } else if (!strcmp(t.name, "path") || !strcmp(t.name, "rect") || !strcmp(t.name, "circle") || !strcmp(t.name, "ellipse")) { if (!seen_svg) die("shape outside ", t.name); svgr_reset(&r, m); if (!strcmp(t.name, "path")) { if (!attr(&t, "d", d, sizeof(d))) die(" without d", NULL); if (svgr_path(&r, d) != 0) die("bad path data", NULL); } else if (!strcmp(t.name, "rect")) { double rx = num_attr(&t, "rx", -1); double ry = num_attr(&t, "ry", -1); if (rx < 0) rx = ry < 0 ? 0 : ry; if (ry < 0) ry = rx; svgr_rect(&r, num_attr(&t, "x", 0), num_attr(&t, "y", 0), num_attr(&t, "width", 0), num_attr(&t, "height", 0), rx, ry); } else if (!strcmp(t.name, "circle")) { double rr = num_attr(&t, "r", 0); svgr_ellipse(&r, num_attr(&t, "cx", 0), num_attr(&t, "cy", 0), rr, rr); } else { svgr_ellipse(&r, num_attr(&t, "cx", 0), num_attr(&t, "cy", 0), num_attr(&t, "rx", 0), num_attr(&t, "ry", 0)); } if (r.overflow) die("path too complex", NULL); if (!pt.none && pt.opacity > 0) { unsigned int a = (unsigned int)(pt.opacity * 255.0 + 0.5); svgr_fill(&r, &L, 0xFFFF, a > 255 ? 255 : a, pt.evenodd); shapes++; } } else { die("unsupported element", t.name); } if (!t.self_closing && (!strcmp(t.name, "g") || !strcmp(t.name, "svg"))) { if (++depth >= GROUP_MAX) die("groups nested too deep", NULL); stack[depth] = pt; } } if (!shapes) die("no filled shape", NULL); } static void preview(const char *name, const unsigned char *mask, int size) { static const char ramp[] = " .:-=+*#%@"; int x, y; fprintf(stderr, "%s\n", name); for (y = 0; y < size; y++) { for (x = 0; x < size; x++) { int v = mask[y * size + x] * 9 / 255; fputc(ramp[v], stderr); fputc(ramp[v], stderr); } fputc('\n', stderr); } } int main(int argc, char **argv) { static unsigned char mask[ICON_MAX_SIZE * ICON_MAX_SIZE]; struct icon_file_hdr h; char path[1024]; int size, verbose, i; FILE *out; if (argc < 4) { fprintf(stderr, "usage: %s [-v]\n", argv[0]); return 2; } size = atoi(argv[2]); verbose = argc > 4 && !strcmp(argv[4], "-v"); if (size < 8 || size > ICON_MAX_SIZE) { fprintf(stderr, "mkicons: size must be 8..%d\n", ICON_MAX_SIZE); return 2; } out = fopen(argv[3], "wb"); if (!out) { perror(argv[3]); return 1; } memset(&h, 0, sizeof(h)); memcpy(h.magic, ICON_FILE_MAGIC, 8); h.count = (unsigned short)NICONS; h.size = (unsigned short)size; fwrite(&h, sizeof(h), 1, out); for (i = 0; i < NICONS; i++) { char *svg; snprintf(path, sizeof(path), "%s/%s.svg", argv[1], icons[i].file); cur_file = path; svg = read_file(path); if (!svg) die("cannot read", NULL); render(svg, size, mask); free(svg); if (verbose) preview(icons[i].name, mask, size); fwrite(mask, 1, (size_t)size * (size_t)size, out); } if (fclose(out) != 0) { perror(argv[3]); remove(argv[3]); return 1; } return 0; }