-/* ------- SVG -------
- SVG reference:
- http://www.w3.org/TR/SVG/
-*/
-static int svg_indent = 0;
-static int svg_single_line = 0;
-static const char *svg_default_font = "-dummy-";
-typedef struct svg_dash
-{
- int dash_enable;
- double dash_adjust, dash_len, dash_offset;
- double adjusted_on, adjusted_off;
-} svg_dash;
-
-
-static void svg_print_indent(FILE *fp)
-{
- int i;
- for (i = svg_indent - svg_single_line; i > 0; i--) {
- putc(' ', fp);
- putc(' ', fp);
- }
-}
-
-static void svg_start_tag(FILE *fp, const char *name)
-{
- svg_print_indent(fp);
- putc('<', fp);
- fputs(name, fp);
- svg_indent++;
-}
-
-static void svg_close_tag_single_line(FILE *fp)
-{
- svg_single_line++;
- putc('>', fp);
-}
-
-static void svg_close_tag(FILE *fp)
-{
- putc('>', fp);
- if (!svg_single_line)
- putc('\n', fp);
-}
-
-static void svg_end_tag(FILE *fp, const char *name)
-{
- /* name is NULL if closing empty-node tag */
- svg_indent--;
- if (svg_single_line)
- svg_single_line--;
- else if (name)
- svg_print_indent(fp);
- if (name != NULL) {
- fputs("</", fp);
- fputs(name, fp);
- } else {
- putc('/', fp);
- }
- svg_close_tag(fp);
-}
-
-static void svg_close_tag_empty_node(FILE *fp)
-{
- svg_end_tag(fp, NULL);
-}
-
-static void svg_write_text(FILE *fp, const char *text)
-{
-#ifdef HAVE_MBSTOWCS
- size_t clen;
- wchar_t *p, *cstr, ch;
- int text_count;
- if (!text)
- return;
- clen = strlen(text) + 1;
- cstr = malloc(sizeof(wchar_t) * clen);
- text_count = mbstowcs(cstr, text, clen);
- if (text_count == -1)
- text_count = mbstowcs(cstr, "Enc-Err", 6);
- p = cstr;
-#else
- const unsigned char *p = text, ch;
- if (!p)
- return;
-#endif
- while (1) {
- ch = *p++;
- ch = afm_fix_osx_charset(ch); /* unsafe macro */
- switch (ch) {
- case 0:
-#ifdef HAVE_MBSTOWCS
- free(cstr);
-#endif
- return;
- case '&': fputs("&", fp); break;
- case '<': fputs("<", fp); break;
- case '>': fputs(">", fp); break;
- case '"': fputs(""", fp); break;
- default:
- if (ch == 32) {
- if (p <= cstr + 1 || !*p || *p == 32)
- fputs(" ", fp); /* non-breaking space in unicode */
- else
- fputc(32, fp);
- } else if (ch < 32 || ch >= 127)
- fprintf(fp, "&#%d;", (int)ch);
- else
- putc((char)ch, fp);
- }
- }
-}
-
-static void svg_format_number(char *buf, int bufsize, double d)
-{
- /* omit decimals if integer to reduce filesize */
- char *p;
- snprintf(buf, bufsize, "%.2f", d);
- p = buf; /* doesn't trust snprintf return value */
- while (*p)
- p++;
- while (--p > buf) {
- char ch = *p;
- if (ch == '0') {
- *p = '\0'; /* zap trailing zeros */
- continue;
- }
- if (ch == '.')
- *p = '\0'; /* zap trailing dot */
- break;
- }
-}
-
-static void svg_write_number(FILE *fp, double d)
-{
- char buf[60];
- svg_format_number(buf, sizeof(buf), d);
- fputs(buf, fp);
-}
-
-static int svg_color_is_black(int c)
-{
- /* gfx_color_t is RRGGBBAA */
- return c == 0x000000FF;
-}
-
-static void svg_write_color(FILE *fp, gfx_color_t c, const char *attr)
-{
- /* gfx_color_t is RRGGBBAA, svg can use #RRGGBB and #RGB like html */
- gfx_color_t rrggbb = (int)((c >> 8) & 0xFFFFFF);
- gfx_color_t opacity = c & 0xFF;
- fprintf(fp, " %s=\"", attr);
- if ((rrggbb & 0x0F0F0F) == ((rrggbb >> 4) & 0x0F0F0F)) {
- /* css2 short form, #rgb is #rrggbb, not #r0g0b0 */
- fprintf(fp, "#%03lX",
- ( ((rrggbb >> 8) & 0xF00)
- | ((rrggbb >> 4) & 0x0F0)
- | ( rrggbb & 0x00F)));
- } else {
- fprintf(fp, "#%06lX", rrggbb);
- }
- fputs("\"", fp);
- if (opacity != 0xFF) {
- fprintf(fp, " opacity=\"");
- svg_write_number(fp, opacity / 255.0);
- fputs("\"", fp);
- }
-}
-
-static void svg_get_dash(gfx_node_t *node, svg_dash *d)
-{
- double offset;
- int mult;
- if (node->dash_on <= 0 || node->dash_off <= 0) {
- d->dash_enable = 0;
- return;
- }
- d->dash_enable = 1;
- d->dash_len = node->dash_on + node->dash_off;
- /* dash on/off adjustment due to round caps */
- d->dash_adjust = 0.8 * node->size;
- d->adjusted_on = node->dash_on - d->dash_adjust;
- if (d->adjusted_on < 0.01)
- d->adjusted_on = 0.01;
- d->adjusted_off = d->dash_len - d->adjusted_on;
- /* dash offset calc */
- if (node->path[0].x == node->path[1].x) /* only good for horz/vert lines */
- offset = node->path[0].y;
- else
- offset = node->path[0].x;
- mult = (int)fabs(offset / d->dash_len);
- d->dash_offset = offset - mult * d->dash_len;
- if (node->path[0].x < node->path[1].x || node->path[0].y < node->path[1].y)
- d->dash_offset = d->dash_len - d->dash_offset;
-}
-
-static int svg_dash_equal(svg_dash *a, svg_dash *b)
-{
- if (a->dash_enable != b->dash_enable)
- return 0;
- if (a->adjusted_on != b->adjusted_on)
- return 0;
- if (a->adjusted_off != b->adjusted_off)
- return 0;
- /* rest of properties will be the same when on+off are */
- return 1;
-}
-
-static void svg_common_path_attributes(FILE *fp, gfx_node_t *node)
-{
- svg_dash dash_info;
- svg_get_dash(node, &dash_info);
- fputs(" stroke-width=\"", fp);
- svg_write_number(fp, node->size);
- fputs("\"", fp);
- svg_write_color(fp, node->color, "stroke");
- fputs(" fill=\"none\"", fp);
- if (dash_info.dash_enable) {
- if (dash_info.dash_offset != 0) {
- fputs(" stroke-dashoffset=\"", fp);
- svg_write_number(fp, dash_info.dash_offset);
- fputs("\"", fp);
- }
- fputs(" stroke-dasharray=\"", fp);
- svg_write_number(fp, dash_info.adjusted_on);
- fputs(",", fp);
- svg_write_number(fp, dash_info.adjusted_off);
- fputs("\"", fp);
- }
-}
-
-static int svg_is_int_step(double a, double b)
-{
- double diff = fabs(a - b);
- return floor(diff) == diff;
-}
-
-static int svg_path_straight_segment(FILE *fp,
- double lastA, double currentA, double currentB,
- gfx_node_t *node,
- int segment_idx, int isx, char absChar, char relChar)
-{
- if (!svg_is_int_step(lastA, currentA)) {
- putc(absChar, fp);
- svg_write_number(fp, currentA);
- return 0;
- }
- if (segment_idx < node->points - 1) {
- ArtVpath *vec = node->path + segment_idx + 1;
- if (vec->code == ART_LINETO) {
- double nextA = (isx ? vec->x : vec->y) - LINEOFFSET;
- double nextB = (isx ? vec->y : vec->x) - LINEOFFSET;
- if (nextB == currentB
- && ((currentA >= lastA) == (nextA >= currentA))
- && svg_is_int_step(currentA, nextA)) {
- return 1; /* skip to next as it is a straight line */
- }
- }
- }
- putc(relChar, fp);
- svg_write_number(fp, currentA - lastA);
- return 0;
-}
-
-static void svg_path(FILE *fp, gfx_node_t *node, int multi)
-{
- int i;
- double lastX = 0, lastY = 0;
- /* for straight lines <path..> tags take less space than
- <line..> tags because of the efficient packing
- in the 'd' attribute */
- svg_start_tag(fp, "path");
- if (!multi)
- svg_common_path_attributes(fp, node);
- fputs(" d=\"", fp);
- /* specification of the 'd' attribute: */
- /* http://www.w3.org/TR/SVG/paths.html#PathDataGeneralInformation */
- for (i = 0; i < node->points; i++) {
- ArtVpath *vec = node->path + i;
- double x = vec->x - LINEOFFSET;
- double y = vec->y - LINEOFFSET;
- switch (vec->code) {
- case ART_MOVETO_OPEN: /* fall-through */
- case ART_MOVETO:
- putc('M', fp);
- svg_write_number(fp, x);
- putc(',', fp);
- svg_write_number(fp, y);
- break;
- case ART_LINETO:
- /* try optimize filesize by using minimal lineto commands */
- /* without introducing rounding errors. */
- if (x == lastX) {
- if (svg_path_straight_segment(fp, lastY, y, x, node, i, 0, 'V', 'v'))
- continue;
- } else if (y == lastY) {
- if (svg_path_straight_segment(fp, lastX, x, y, node, i, 1, 'H', 'h'))
- continue;
- } else {
- putc('L', fp);
- svg_write_number(fp, x);
- putc(',', fp);
- svg_write_number(fp, y);
- }
- break;
- case ART_CURVETO: break; /* unsupported */
- case ART_END: break; /* nop */
- }
- lastX = x;
- lastY = y;
- }
- if (node->closed_path)
- fputs(" Z", fp);
- fputs("\"", fp);
- svg_close_tag_empty_node(fp);
-}
-
-static void svg_multi_path(FILE *fp, gfx_node_t **nodeR)
-{
- /* optimize for multiple paths with the same color, penwidth, etc. */
- int num = 1;
- gfx_node_t *node = *nodeR;
- gfx_node_t *next = node->next;
- while (next) {
- if (next->type != node->type
- || next->size != node->size
- || next->color != node->color
- || next->dash_on != node->dash_on
- || next->dash_off != node->dash_off)
- break;
- next = next->next;
- num++;
- }
- if (num == 1) {
- svg_path(fp, node, 0);
- return;
- }
- svg_start_tag(fp, "g");
- svg_common_path_attributes(fp, node);
- svg_close_tag(fp);
- while (num && node) {
- svg_path(fp, node, 1);
- if (!--num)
- break;
- node = node->next;
- *nodeR = node;
- }
- svg_end_tag(fp, "g");
-}
-
-static void svg_area(FILE *fp, gfx_node_t *node)
-{
- int i;
- double startX = 0, startY = 0;
- svg_start_tag(fp, "polygon");
- fputs(" ", fp);
- svg_write_color(fp, node->color, "fill");
- fputs(" points=\"", fp);
- for (i = 0; i < node->points; i++) {
- ArtVpath *vec = node->path + i;
- double x = vec->x - LINEOFFSET;
- double y = vec->y - LINEOFFSET;
- switch (vec->code) {
- case ART_MOVETO_OPEN: /* fall-through */
- case ART_MOVETO:
- svg_write_number(fp, x);
- putc(',', fp);
- svg_write_number(fp, y);
- startX = x;
- startY = y;
- break;
- case ART_LINETO:
- if (i == node->points - 2
- && node->path[i + 1].code == ART_END
- && fabs(x - startX) < 0.001 && fabs(y - startY) < 0.001) {
- break; /* poly area always closed, no need for last point */
- }
- putc(' ', fp);
- svg_write_number(fp, x);
- putc(',', fp);
- svg_write_number(fp, y);
- break;
- case ART_CURVETO: break; /* unsupported */
- case ART_END: break; /* nop */
- }
- }
- fputs("\"", fp);
- svg_close_tag_empty_node(fp);
-}
-
-static void svg_text(FILE *fp, gfx_node_t *node)
-{
- pdf_coords g;
- const char *fontname;
- /* as svg has 0,0 in top-left corner (like most screens) instead of
- bottom-left corner like pdf and eps, we have to fake the coords
- using offset and inverse sin(r) value */
- int page_height = 1000;
- pdf_calc(page_height, node, &g);
- if (node->angle != 0) {
- svg_start_tag(fp, "g");
- /* can't use svg_write_number as 2 decimals is far from enough to avoid
- skewed text */
- fprintf(fp, " transform=\"matrix(%f,%f,%f,%f,%f,%f)\"",
- g.ma, -g.mb, -g.mc, g.md, g.tmx, page_height - g.tmy);
- svg_close_tag(fp);
- }
- svg_start_tag(fp, "text");
- if (!node->angle) {
- fputs(" x=\"", fp);
- svg_write_number(fp, g.tmx);
- fputs("\" y=\"", fp);
- svg_write_number(fp, page_height - g.tmy);
- fputs("\"", fp);
- }
- fontname = afm_get_font_name(node->filename);
- if (strcmp(fontname, svg_default_font))
- fprintf(fp, " font-family=\"%s\"", fontname);
- fputs(" font-size=\"", fp);
- svg_write_number(fp, node->size);
- fputs("\"", fp);
- if (!svg_color_is_black(node->color))
- svg_write_color(fp, node->color, "fill");
- svg_close_tag_single_line(fp);
- /* support for node->tabwidth missing */
- svg_write_text(fp, node->text);
- svg_end_tag(fp, "text");
- if (node->angle != 0)
- svg_end_tag(fp, "g");
-}
-
-int gfx_render_svg (gfx_canvas_t *canvas,
- art_u32 width, art_u32 height,
- gfx_color_t background, FILE *fp){
- gfx_node_t *node = canvas->firstnode;
- /* Find the first font used, and assume it is the mostly used
- one. It reduces the number of font-familty attributes. */
- while (node) {
- if (node->type == GFX_TEXT && node->filename) {
- svg_default_font = afm_get_font_name(node->filename);
- break;
- }
- node = node->next;
- }
- fputs(
-"<?xml version=\"1.0\" standalone=\"no\"?>\n"
-"<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.0//EN\"\n"
-" \"http://www.w3.org/TR/2001/REC-SVG-20010904/DTD/svg10.dtd\">\n"
-"<!--\n"
-" SVG file created by\n"
-" RRDtool " PACKAGE_VERSION " Tobias Oetiker, http://tobi.oetiker.ch\n"
-"\n"
-" The width/height attributes in the outhermost svg node\n"
-" are just default sizes for the browser which is used\n"
-" if the svg file is openened directly without being\n"
-" embedded in an html file.\n"
-" The viewBox is the local coord system for rrdtool.\n"
-"-->\n", fp);
- svg_start_tag(fp, "svg");
- fputs(" width=\"", fp);
- svg_write_number(fp, width * canvas->zoom);
- fputs("\" height=\"", fp);
- svg_write_number(fp, height * canvas->zoom);
- fputs("\" x=\"0\" y=\"0\" viewBox=\"", fp);
- svg_write_number(fp, -LINEOFFSET);
- fputs(" ", fp);
- svg_write_number(fp, -LINEOFFSET);
- fputs(" ", fp);
- svg_write_number(fp, width - LINEOFFSET);
- fputs(" ", fp);
- svg_write_number(fp, height - LINEOFFSET);
- fputs("\" preserveAspectRatio=\"xMidYMid\"", fp);
- fprintf(fp, " font-family=\"%s\"", svg_default_font); /* default font */
- fputs(" stroke-linecap=\"round\" stroke-linejoin=\"round\"", fp);
- svg_close_tag(fp);
- svg_start_tag(fp, "rect");
- fprintf(fp, " x=\"0\" y=\"0\" width=\"%d\" height=\"%d\"", width, height);
- svg_write_color(fp, background, "fill");
- svg_close_tag_empty_node(fp);
- node = canvas->firstnode;
- while (node) {
- switch (node->type) {
- case GFX_LINE:
- svg_multi_path(fp, &node);
- break;
- case GFX_AREA:
- svg_area(fp, node);
- break;
- case GFX_TEXT:
- svg_text(fp, node);
- }
- node = node->next;
- }
- svg_end_tag(fp, "svg");
- return 0;
-}
-
-/* ------- EPS -------
- EPS and Postscript references:
- http://partners.adobe.com/asn/developer/technotes/postscript.html
-*/
-
-typedef struct eps_font
-{
- const char *ps_font;
- int id;
- struct eps_font *next;
-} eps_font;
-
-typedef struct eps_state
-{
- FILE *fp;
- gfx_canvas_t *canvas;
- art_u32 page_width, page_height;
- eps_font *font_list;
- /*--*/
- gfx_color_t color;
- const char *font;
- double font_size;
- double line_width;
- int linecap, linejoin;
- int has_dash;
-} eps_state;
-
-static void eps_set_color(eps_state *state, gfx_color_t color)
-{
-#if USE_EPS_FAKE_ALPHA
- double a1, a2;
-#endif
- /* gfx_color_t is RRGGBBAA */
- if (state->color == color)
- return;
-#if USE_EPS_FAKE_ALPHA
- a1 = (color & 255) / 255.0;
- a2 = 255 * (1 - a1);
-#define eps_color_calc(x) (int)( ((x) & 255) * a1 + a2)
-#else
-#define eps_color_calc(x) (int)( (x) & 255)
-#endif
- /* gfx_color_t is RRGGBBAA */
- if (state->color == color)
- return;
- fprintf(state->fp, "%d %d %d Rgb\n",
- eps_color_calc(color >> 24),
- eps_color_calc(color >> 16),
- eps_color_calc(color >> 8));
- state->color = color;
-}
-
-static int eps_add_font(eps_state *state, gfx_node_t *node)
-{
- /* The fonts list could be postponed to the end using
- (atend), but let's be nice and have them in the header. */
- const char *ps_font = afm_get_font_postscript_name(node->filename);
- eps_font *ef;
- for (ef = state->font_list; ef; ef = ef->next) {
- if (!strcmp(ps_font, ef->ps_font))
- return 0;
- }
- ef = malloc(sizeof(eps_font));
- if (ef == NULL) {
- rrd_set_error("malloc for eps_font");
- return -1;
- }
- ef->next = state->font_list;
- ef->ps_font = ps_font;
- state->font_list = ef;
- return 0;
-}
-
-static void eps_list_fonts(eps_state *state, const char *dscName)
-{
- eps_font *ef;
- int lineLen = strlen(dscName);
- if (!state->font_list)
- return;
- fputs(dscName, state->fp);
- for (ef = state->font_list; ef; ef = ef->next) {
- int nameLen = strlen(ef->ps_font);
- if (lineLen + nameLen > 100 && lineLen) {
- fputs("\n", state->fp);
- fputs("%%- \n", state->fp);
- lineLen = 5;
- } else {
- fputs(" ", state->fp);
- lineLen++;
- }
- fputs(ef->ps_font, state->fp);
- lineLen += nameLen;
- }
- fputs("\n", state->fp);
-}
-
-static void eps_define_fonts(eps_state *state)
-{
- eps_font *ef;
- if (!state->font_list)
- return;
- for (ef = state->font_list; ef; ef = ef->next) {
- /* PostScript¨ LANGUAGE REFERENCE third edition
- page 349 */
- fprintf(state->fp,
- "%%\n"
- "/%s findfont dup length dict begin\n"
- "{ 1 index /FID ne {def} {pop pop} ifelse } forall\n"
- "/Encoding ISOLatin1Encoding def\n"
- "currentdict end\n"
- "/%s-ISOLatin1 exch definefont pop\n"
- "/SetFont-%s { /%s-ISOLatin1 findfont exch scalefont setfont } bd\n",
- ef->ps_font, ef->ps_font, ef->ps_font, ef->ps_font);
- }
-}
-
-static int eps_prologue(eps_state *state)
-{
- gfx_node_t *node;
- fputs(
- "%!PS-Adobe-3.0 EPSF-3.0\n"
- "%%Creator: RRDtool " PACKAGE_VERSION " Tobias Oetiker, http://tobi.oetiker.ch\n"
- /* can't like weird chars here */
- "%%Title: (RRDtool output)\n"
- "%%DocumentData: Clean7Bit\n"
- "", state->fp);
- fprintf(state->fp, "%%%%BoundingBox: 0 0 %d %d\n",
- state->page_width, state->page_height);
- for (node = state->canvas->firstnode; node; node = node->next) {
- if (node->type == GFX_TEXT && eps_add_font(state, node) == -1)
- return -1;
- }
- eps_list_fonts(state, "%%DocumentFonts:");
- eps_list_fonts(state, "%%DocumentNeededFonts:");
- fputs(
- "%%EndComments\n"
- "%%BeginProlog\n"
- "%%EndProlog\n" /* must have, or BoundingBox is ignored */
- "/bd { bind def } bind def\n"
- "", state->fp);
- fprintf(state->fp, "/X { %.2f add } bd\n", LINEOFFSET);
- fputs(
- "/X2 {X exch X exch} bd\n"
- "/M {X2 moveto} bd\n"
- "/L {X2 lineto} bd\n"
- "/m {moveto} bd\n"
- "/l {lineto} bd\n"
- "/S {stroke} bd\n"
- "/CP {closepath} bd\n"
- "/WS {setlinewidth stroke} bd\n"
- "/F {fill} bd\n"
- "/T1 {gsave} bd\n"
- "/T2 {concat 0 0 moveto show grestore} bd\n"
- "/T {moveto show} bd\n"
- "/Rgb { 255.0 div 3 1 roll\n"
- " 255.0 div 3 1 roll \n"
- " 255.0 div 3 1 roll setrgbcolor } bd\n"
- "", state->fp);
- eps_define_fonts(state);
- return 0;
-}
-
-static void eps_clear_dash(eps_state *state)
-{
- if (!state->has_dash)
- return;
- state->has_dash = 0;
- fputs("[1 0] 0 setdash\n", state->fp);
-}
-
-static void eps_write_linearea(eps_state *state, gfx_node_t *node)
-{
- int i;
- FILE *fp = state->fp;
- int useOffset = 0;
- int clearDashIfAny = 1;
- eps_set_color(state, node->color);
- if (node->type == GFX_LINE) {
- svg_dash dash_info;
- if (state->linecap != 1) {
- fputs("1 setlinecap\n", fp);
- state->linecap = 1;
- }
- if (state->linejoin != 1) {
- fputs("1 setlinejoin\n", fp);
- state->linejoin = 1;
- }
- svg_get_dash(node, &dash_info);
- if (dash_info.dash_enable) {
- clearDashIfAny = 0;
- state->has_dash = 1;
- fputs("[", fp);
- svg_write_number(fp, dash_info.adjusted_on);
- fputs(" ", fp);
- svg_write_number(fp, dash_info.adjusted_off);
- fputs("] ", fp);
- svg_write_number(fp, dash_info.dash_offset);
- fputs(" setdash\n", fp);
- }
- }
- if (clearDashIfAny)
- eps_clear_dash(state);
- for (i = 0; i < node->points; i++) {
- ArtVpath *vec = node->path + i;
- double x = vec->x;
- double y = state->page_height - vec->y;
- if (vec->code == ART_MOVETO_OPEN || vec->code == ART_MOVETO)
- useOffset = (fabs(x - floor(x) - 0.5) < 0.01 && fabs(y - floor(y) - 0.5) < 0.01);
- if (useOffset) {
- x -= LINEOFFSET;
- y -= LINEOFFSET;
- }
- switch (vec->code) {
- case ART_MOVETO_OPEN: /* fall-through */
- case ART_MOVETO:
- svg_write_number(fp, x);
- fputc(' ', fp);
- svg_write_number(fp, y);
- fputc(' ', fp);
- fputs(useOffset ? "M\n" : "m\n", fp);
- break;
- case ART_LINETO:
- svg_write_number(fp, x);
- fputc(' ', fp);
- svg_write_number(fp, y);
- fputc(' ', fp);
- fputs(useOffset ? "L\n" : "l\n", fp);
- break;
- case ART_CURVETO: break; /* unsupported */
- case ART_END: break; /* nop */
- }
- }
- if (node->type == GFX_LINE) {
- if (node->closed_path)
- fputs("CP ", fp);
- if (node->size != state->line_width) {
- state->line_width = node->size;
- svg_write_number(fp, state->line_width);
- fputs(" WS\n", fp);
- } else {
- fputs("S\n", fp);
- }
- } else {
- fputs("F\n", fp);
- }
-}
-
-static void eps_write_text(eps_state *state, gfx_node_t *node)
-{
- FILE *fp = state->fp;
- const char *ps_font = afm_get_font_postscript_name(node->filename);
- int lineLen = 0;
- pdf_coords g;
-#ifdef HAVE_MBSTOWCS
- size_t clen;
- wchar_t *p, *cstr, ch;
- int text_count;
- if (!node->text)
- return;
- clen = strlen(node->text) + 1;
- cstr = malloc(sizeof(wchar_t) * clen);
- text_count = mbstowcs(cstr, node->text, clen);
- if (text_count == -1)
- text_count = mbstowcs(cstr, "Enc-Err", 6);
- p = cstr;
-#else
- const unsigned char *p = node->text, ch;
- if (!p)
- return;
-#endif
- pdf_calc(state->page_height, node, &g);
- eps_set_color(state, node->color);
- if (strcmp(ps_font, state->font) || node->size != state->font_size) {
- state->font = ps_font;
- state->font_size = node->size;
- svg_write_number(fp, state->font_size);
- fprintf(fp, " SetFont-%s\n", state->font);
- }
- if (node->angle)
- fputs("T1 ", fp);
- fputs("(", fp);
- lineLen = 20;
- while (1) {
- ch = *p;
- if (!ch)
- break;
- ch = afm_fix_osx_charset(ch); /* unsafe macro */
- if (++lineLen > 70) {
- fputs("\\\n", fp); /* backslash and \n */
- lineLen = 0;
- }
- switch (ch) {
- case '%':
- case '(':
- case ')':
- case '\\':
- fputc('\\', fp);
- fputc(ch, fp);