void svg_parse_color_from_style(fz_context *ctx, svg_document *doc, const char *str, int *fill_is_set, float fill[3], int *stroke_is_set, float stroke[3]) { const char *p; p = strstr(str, "fill:"); if (p) { p += 5; while (*p && svg_is_whitespace(*p)) ++p; if (memcmp(p, "none", 4) != 0) { svg_parse_color(ctx, doc, p, fill); *fill_is_set = 1; } } p = strstr(str, "stroke:"); if (p) { p += 7; while (*p && svg_is_whitespace(*p)) ++p; if (memcmp(p, "none", 4) != 0) { svg_parse_color(ctx, doc, p, stroke); *stroke_is_set = 1; } } }
/* Coordinate transformations */ fz_matrix svg_parse_transform(fz_context *ctx, svg_document *doc, const char *str, fz_matrix transform) { char keyword[20]; int keywordlen; float args[6]; int nargs; nargs = 0; keywordlen = 0; while (*str) { while (svg_is_whitespace_or_comma(*str)) str ++; if (*str == 0) break; /* * Parse keyword and opening parenthesis. */ keywordlen = 0; while (svg_is_alpha(*str) && keywordlen < sizeof(keyword) - 1) keyword[keywordlen++] = *str++; keyword[keywordlen] = 0; if (keywordlen == 0) fz_throw(ctx, FZ_ERROR_SYNTAX, "expected keyword in transform attribute"); while (svg_is_whitespace(*str)) str ++; if (*str != '(') fz_throw(ctx, FZ_ERROR_SYNTAX, "expected opening parenthesis in transform attribute"); str ++; /* * Parse list of numbers until closing parenthesis */ nargs = 0; while (*str && *str != ')' && nargs < 6) { while (svg_is_whitespace_or_comma(*str)) str ++; if (svg_is_digit(*str)) str = svg_lex_number(&args[nargs++], str); } if (*str != ')') fz_throw(ctx, FZ_ERROR_SYNTAX, "expected closing parenthesis in transform attribute"); str ++; /* * Execute the transform. */ if (!strcmp(keyword, "matrix")) { fz_matrix m; if (nargs != 6) fz_throw(ctx, FZ_ERROR_SYNTAX, "wrong number of arguments to matrix(): %d", nargs); m.a = args[0]; m.b = args[1]; m.c = args[2]; m.d = args[3]; m.e = args[4]; m.f = args[5]; transform = fz_concat(transform, m); } else if (!strcmp(keyword, "translate")) { if (nargs != 2) fz_throw(ctx, FZ_ERROR_SYNTAX, "wrong number of arguments to translate(): %d", nargs); transform = fz_pre_translate(transform, args[0], args[1]); } else if (!strcmp(keyword, "scale")) { if (nargs == 1) transform = fz_pre_scale(transform, args[0], args[0]); else if (nargs == 2) transform = fz_pre_scale(transform, args[0], args[1]); else fz_throw(ctx, FZ_ERROR_SYNTAX, "wrong number of arguments to scale(): %d", nargs); } else if (!strcmp(keyword, "rotate")) { if (nargs != 1) fz_throw(ctx, FZ_ERROR_SYNTAX, "wrong number of arguments to rotate(): %d", nargs); transform = fz_pre_rotate(transform, args[0]); } else if (!strcmp(keyword, "skewX")) { fz_matrix m; if (nargs != 1) fz_throw(ctx, FZ_ERROR_SYNTAX, "wrong number of arguments to skewX(): %d", nargs); m.a = 1; m.b = 0; m.c = tanf(args[0] * FZ_DEGREE); m.d = 1; m.e = 0; m.f = 0; transform = fz_concat(transform, m); } else if (!strcmp(keyword, "skewY")) { fz_matrix m; if (nargs != 1) fz_throw(ctx, FZ_ERROR_SYNTAX, "wrong number of arguments to skewY(): %d", nargs); m.a = 1; m.b = tanf(args[0] * FZ_DEGREE); m.c = 0; m.d = 1; m.e = 0; m.f = 0; transform = fz_concat(transform, m); } else { fz_throw(ctx, FZ_ERROR_SYNTAX, "unknown transform function: %s", keyword); } } return transform; }