X-Git-Url: https://git.octo.it/?a=blobdiff_plain;f=src%2Frrd_gfx.c;h=f4c04efe925073637db6565b5cde07ce9697f3f5;hb=c966bf04b22f45e38ca9d1b318514154b33c367d;hp=ac33ba208ad9681afe4806d38b6e8cc37bc2ca17;hpb=d3061079f2399ecb1e8af5c132106157a03aa843;p=rrdtool.git diff --git a/src/rrd_gfx.c b/src/rrd_gfx.c index ac33ba2..f4c04ef 100644 --- a/src/rrd_gfx.c +++ b/src/rrd_gfx.c @@ -1,5 +1,5 @@ /**************************************************************************** - * RRDtool 1.2.8 Copyright by Tobi Oetiker, 1997-2005 + * RRDtool 1.2.9 Copyright by Tobi Oetiker, 1997-2005 **************************************************************************** * rrd_gfx.c graphics wrapper for rrdtool **************************************************************************/ @@ -7,9 +7,9 @@ /* #define DEBUG */ #ifdef DEBUG -# define DPRINT(x) (void)(printf x, printf("\n")) +# define DPRINTF(...) fprintf(stderr, __VA_ARGS__); #else -# define DPRINT(x) +# define DPRINTF(...) #endif #include "rrd_tool.h" #include @@ -38,7 +38,7 @@ typedef struct gfx_string_s *gfx_string; struct gfx_string_s { unsigned int width; unsigned int height; - size_t count; /* number of characters */ + int count; /* number of characters */ gfx_char glyphs; size_t num_glyphs; FT_BBox bbox; @@ -376,7 +376,7 @@ gfx_string gfx_string_create(gfx_canvas_t *canvas, FT_Face face,const char *text gfx_string string = (gfx_string) malloc (sizeof(struct gfx_string_s)); gfx_char glyph; /* current glyph in table */ - unsigned int n; + int n; int error; int gottab = 0; @@ -386,7 +386,11 @@ gfx_string gfx_string_create(gfx_canvas_t *canvas, FT_Face face,const char *text cstr = malloc(sizeof(wchar_t) * clen); /* yes we are allocating probably too much here, I know */ string->count=mbstowcs(cstr,text,clen); if ( string->count == -1){ - string->count=mbstowcs(cstr,"Enc-Err",6); + /* conversion did not work, so lets fall back to just use what we got */ + string->count=clen-1; + for(n=0;text[n] != '\0';n++){ + cstr[n]=(unsigned char)text[n]; + } } #else char *cstr = strdup(text); @@ -409,12 +413,13 @@ gfx_string gfx_string_create(gfx_canvas_t *canvas, FT_Face face,const char *text use_kerning = FT_HAS_KERNING(face); previous = 0; glyph = string->glyphs; - for (n=0; ncount;glyph++) { + for (n=0; ncount;glyph++,n++) { FT_Vector vec; /* handle the tabs ... have a witespace glyph inserted, but set its width such that the distance of the new right edge is x times tabwidth from 0,0 where x is an integer. */ unsigned int letter = cstr[n]; + letter = afm_fix_osx_charset(letter); /* unsafe macro */ gottab = 0; if (letter == '\\' && n+1 < string->count && cstr[n+1] == 't'){ @@ -451,12 +456,12 @@ gfx_string gfx_string_create(gfx_canvas_t *canvas, FT_Face face,const char *text canvas->aa_type == AA_LIGHT ? FT_LOAD_TARGET_LIGHT : FT_LOAD_TARGET_MONO : FT_LOAD_TARGET_MONO); if (error) { - fprintf (stderr, "couldn't load glyph: %c\n", letter); + DPRINTF("couldn't load glyph: %c\n", letter) continue; } error = FT_Get_Glyph (slot, &glyph->image); if (error) { - fprintf (stderr, "couldn't get glyph from slot: %c\n", letter); + DPRINTF("couldn't get glyph %c from slot %d\n", letter, (int)slot) continue; } /* if we are in tabbing mode, we replace the tab with a space and shift the position @@ -478,7 +483,7 @@ gfx_string gfx_string_create(gfx_canvas_t *canvas, FT_Face face,const char *text FT_Vector_Transform (&vec, &string->transform); error = FT_Glyph_Transform (glyph->image, &string->transform, &vec); if (error) { - fprintf (stderr, "couldn't transform glyph\n"); + DPRINTF("couldn't transform glyph id %d\n", letter) continue; } @@ -488,15 +493,13 @@ gfx_string gfx_string_create(gfx_canvas_t *canvas, FT_Face face,const char *text canvas->aa_type == AA_LIGHT ? FT_RENDER_MODE_LIGHT : FT_RENDER_MODE_MONO : FT_RENDER_MODE_MONO, 0, 1); if (error) { - fprintf (stderr, "couldn't convert glyph to bitmap\n"); + DPRINTF("couldn't convert glyph id %d to bitmap\n", letter) continue; } /* increment number of glyphs */ previous = glyph->index; string->num_glyphs++; - n++; - } free(cstr); /* printf ("number of glyphs = %d\n", string->num_glyphs);*/ @@ -637,19 +640,19 @@ int gfx_render_png (gfx_canvas_t *canvas, pen_x += vec.x/64; pen_y += vec.y/64; glyph = string->glyphs; - for(n=0; nnum_glyphs; ++n, ++glyph) { + for(n=0; nnum_glyphs; n++, glyph++) { int gr; FT_Glyph image; FT_BitmapGlyph bit; /* long buf_x,comp_n; */ /* make copy to transform */ if (! glyph->image) { - fprintf (stderr, "no image\n"); + DPRINTF("no image\n") continue; } error = FT_Glyph_Copy (glyph->image, &image); if (error) { - fprintf (stderr, "couldn't copy image\n"); + DPRINTF("couldn't copy image\n") continue; } @@ -1040,37 +1043,46 @@ static void svg_close_tag_empty_node(FILE *fp) static void svg_write_text(FILE *fp, const char *text) { - const unsigned char *p, *start, *last; - unsigned int ch; - p = (const unsigned char*)text; - if (!p) - return; - /* trim leading spaces */ - while (*p == ' ') - p++; - start = p; - /* trim trailing spaces */ - last = p - 1; - while ((ch = *p) != 0) { - if (ch != ' ') - last = p; - p++; - } - /* encode trimmed text */ - p = start; - while (p <= last) { +#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_host2unicode(ch); /* unsafe macro */ + 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 >= 127) - fprintf(fp, "&#%d;", ch); + 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(ch, fp); + putc((char)ch, fp); } } } @@ -1709,10 +1721,26 @@ static void eps_write_linearea(eps_state *state, gfx_node_t *node) static void eps_write_text(eps_state *state, gfx_node_t *node) { FILE *fp = state->fp; - const char *p; 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) { @@ -1725,11 +1753,11 @@ static void eps_write_text(eps_state *state, gfx_node_t *node) fputs("T1 ", fp); fputs("(", fp); lineLen = 20; - p = node->text; while (1) { - unsigned char ch = *(unsigned char*)p; + ch = *p; if (!ch) break; + ch = afm_fix_osx_charset(ch); /* unsafe macro */ if (++lineLen > 70) { fputs("\\\n", fp); /* backslash and \n */ lineLen = 0; @@ -1752,8 +1780,10 @@ static void eps_write_text(eps_state *state, gfx_node_t *node) fputs("\\t", fp); break; default: - if (ch >= 126 || ch < 32) { - fprintf(fp, "\\%03o", ch); + if (ch > 255) { + fputc('?', fp); + } else if (ch >= 126 || ch < 32) { + fprintf(fp, "\\%03o", (unsigned int)ch); lineLen += 3; } else { fputc(ch, fp); @@ -1761,6 +1791,9 @@ static void eps_write_text(eps_state *state, gfx_node_t *node) } p++; } +#ifdef HAVE_MBSTOWCS + free(cstr); +#endif if (node->angle) { /* can't use svg_write_number as 2 decimals is far from enough to avoid skewed text */ @@ -1808,6 +1841,7 @@ int gfx_render_eps (gfx_canvas_t *canvas, state.linecap = -1; state.linejoin = -1; state.has_dash = 0; + state.line_width = 1; if (eps_prologue(&state) == -1) return -1; eps_set_color(&state, background); @@ -1919,6 +1953,17 @@ static void pdf_put(pdf_buffer *buf, const char *text, int len) buf->current_size += len; } +static void pdf_put_char(pdf_buffer *buf, char c) +{ + if (buf->alloc_size >= buf->current_size + 1) { + buf->data[buf->current_size++] = c; + } else { + char tmp[1]; + tmp[0] = (char)c; + pdf_put(buf, tmp, 1); + } +} + static void pdf_puts(pdf_buffer *buf, const char *text) { pdf_put(buf, text, strlen(text)); @@ -1949,18 +1994,23 @@ static void pdf_putnumber(pdf_buffer *buf, double d) pdf_puts(buf, tmp); } -static void pdf_put_string_contents(pdf_buffer *buf, const char *text) +static void pdf_put_string_contents_wide(pdf_buffer *buf, const afm_char *text) { - const char *p = text; + const afm_char *p = text; while (1) { - unsigned char ch = *(unsigned char*)p; + afm_char ch = *p; + ch = afm_fix_osx_charset(ch); /* unsafe macro */ switch (ch) { - case 0: return; + case 0: + return; case '(': + pdf_puts(buf, "\\("); + break; case ')': + pdf_puts(buf, "\\)"); + break; case '\\': - pdf_puts(buf, "\\"); - pdf_put(buf, p, 1); + pdf_puts(buf, "\\\\"); break; case '\n': pdf_puts(buf, "\\n"); @@ -1972,18 +2022,48 @@ static void pdf_put_string_contents(pdf_buffer *buf, const char *text) pdf_puts(buf, "\\t"); break; default: - if (ch >= 126 || ch < 32) { + if (ch > 255) { + pdf_put_char(buf, '?'); + } else if (ch >= 126 || ch < 32) { + pdf_put_char(buf, ch); + } else if (ch >= 0 && ch <= 255) { char tmp[10]; - snprintf(tmp, sizeof(tmp), "\\%03o", ch); + snprintf(tmp, sizeof(tmp), "\\%03o", (int)ch); pdf_puts(buf, tmp); - } else { - pdf_put(buf, p, 1); } } p++; } } +static void pdf_put_string_contents(pdf_buffer *buf, const char *text) +{ +#ifdef HAVE_MBSTOWCS + size_t clen = strlen(text) + 1; + wchar_t *cstr = malloc(sizeof(wchar_t) * clen); + int text_count = mbstowcs(cstr, text, clen); + if (text_count == -1) + text_count = mbstowcs(cstr, "Enc-Err", 6); + pdf_put_string_contents_wide(buf, cstr); +#if 0 + if (*text == 'W') { + fprintf(stderr, "Decoding utf8 for '%s'\n", text); + wchar_t *p = cstr; + char *pp = text; + fprintf(stderr, "sz wc = %d\n", sizeof(wchar_t)); + while (*p) { + fprintf(stderr, " %d = %c versus %d = %c\n", *p, (char)*p, 255 & (int)*pp, *pp); + p++; + pp++; + } + } +#endif + free(cstr); +#else + pdf_put_string_contents_wide(buf, text); +#endif +} + static void pdf_init_object(pdf_state *state, pdf_buffer *buf) { pdf_init_buffer(state, buf);