# Make printing working correctly under any locale, not only under latin1 ones # - patch from Vlad Harchev # diff -ru src-orig/print-cell.c src/print-cell.c --- src-orig/print-cell.c Sat Feb 24 08:24:53 2001 +++ src/print-cell.c Sat Mar 10 13:20:44 2001 @@ -5,6 +5,7 @@ * Miguel de Icaza 1999 (miguel@kernel.org) * * g_unichar_to_utf8: Copyright Red Hat, Inc + * i18n of printing: Copyright 2001 by Vlad Harchev */ #include #include @@ -28,6 +29,18 @@ #include "rendered-value.h" #include "portability.h" +/* + Define this to enable i18n-wise printing and string measuring - it requires + mbstowcs to be available. Most probably printing work fine for ANY locale + (though gnome-print doesn't support CJK yet - but when it will be ready, no + changes will be needed in the code used when _PROPER_I18N is defined. + + If this macro is undefined, printing will work only for iso-8859-1, so please + try hard to avoid undefining it. + - Vlad Harchev +*/ +#define _PROPER_I18N + #if 0 #define MERGE_DEBUG(range, str) do { range_dump (range, str); } while (0) #else @@ -145,6 +158,92 @@ return ret; } +int +print_show (GnomePrintContext *pc, char const *text) +{ +#ifdef _PROPER_I18N + wchar_t* wcs,wcbuf[4096]; + char* utf8,utf8buf[4096]; + + int conv_status; + int n = strlen(text); + int retval; + + g_return_val_if_fail (pc && text, -1); + + if ( n > (sizeof(wcbuf)/sizeof(wcbuf[0]))) + wcs = g_new(wchar_t,n); + else + wcs = wcbuf; + + conv_status = mbstowcs(wcs, text, n); + + if (conv_status == (size_t)(-1)){ + if (wcs != wcbuf) + g_free (wcs); + return 0; + }; + if (conv_status * 6 > sizeof(utf8buf)) + utf8 = g_new(gchar, conv_status * 6); + else + utf8 = utf8buf; + { + int i; + char* p = utf8; + for(i = 0; i < conv_status; ++i) + p += g_unichar_to_utf8 ( (gint) wcs[i], p); + if (wcs != wcbuf) + g_free(wcs); + retval = gnome_print_show_sized (pc, utf8, p - utf8); + } + + if (utf8 != utf8buf) + g_free(utf8); + return retval; +#else + return print_show_iso8859_1 (pc, text); +#endif +}; + +double +get_width_string_n (GnomeFont *font,char const* text,guint n) +{ +#ifdef _PROPER_I18N + wchar_t* wcs,wcbuf[4000]; + int conv_status,i; + double total = 0; + + if ( n > (sizeof(wcbuf)/sizeof(wcbuf[0]))) + wcs = g_new(wchar_t,n); + else + wcs = wcbuf; + + conv_status = mbstowcs(wcs, text, n); + + if (conv_status == (size_t)(-1)){ + if (wcs != wcbuf) + g_free (wcs); + return 0; + }; + for (i = 0; i < conv_status; ++i) + total += gnome_font_get_glyph_width(font, + gnome_font_lookup_default(font, wcs[i])); + + if (wcs != wcbuf) + g_free(wcs); + return total; +#else + return gnome_font_get_width_string_n (font, text, n); +#endif +}; + + +double +get_width_string (GnomeFont *font,char const* text) +{ + return get_width_string_n (font, text, strlen(text)); +}; + /***********************************************************/ /* @@ -159,9 +258,7 @@ double const * const line_offset, int num_lines) { gnome_print_moveto (context, x, text_base); - /* FIXME: - * Switch this back to gnome_print_show once we use UTF-8 internally */ - print_show_iso8859_1 (context, text); + print_show (context, text); /* FIXME how to handle small fonts ? * the text_base should be at least 2 pixels above the bottom */ @@ -178,7 +275,7 @@ double x1, double text_base, double width, double const * const line_offset, int num_lines) { - double const len = gnome_font_get_width_string_n (font, "#", 1); + double const len = get_width_string_n (font, "#", 1); int count = 0; if (len != 0) { @@ -205,7 +302,7 @@ for (line_begin = p = text; *p; p++) { double const len_current = - gnome_font_get_width_string_n (font, p, 1); + get_width_string_n (font, p, 1); /* Wrap if there is an embeded newline, or we have overflowed */ if (*p == '\n' || used + len_current > width) { @@ -412,7 +509,7 @@ line_offset[num_lines++] = font_ascent/-2; /* FIXME : This will be wrong for JUSTIFIED halignments */ - cell_width_pts = gnome_font_get_width_string (print_font, text); + cell_width_pts = get_width_string (print_font, text); /* if a number overflows, do special drawing */ if ((cell_width_pts + indent) > width && cell_is_number (cell) && @@ -528,17 +625,17 @@ /* Be cheap, only calculate the width of the * string if we need to. */ if (num_lines > 0) - len = gnome_font_get_width_string (print_font, str); + len = get_width_string (print_font, str); break; case HALIGN_RIGHT: - len = gnome_font_get_width_string (print_font, str); + len = get_width_string (print_font, str); x = rect_x + rect_width - 1 - len - indent; break; case HALIGN_CENTER: case HALIGN_CENTER_ACROSS_SELECTION: - len = gnome_font_get_width_string (print_font, str); + len = get_width_string (print_font, str); x = rect_x + h_center - len / 2; } diff -ru src-orig/print-cell.h src/print-cell.h --- src-orig/print-cell.h Mon Feb 19 01:23:46 2001 +++ src/print-cell.h Sat Mar 10 13:21:38 2001 @@ -10,7 +10,11 @@ /* This function got introduced when gnome-print switched to UTF-8, and will * disappear again once Gnumeric makes the switch */ -int print_show_iso8859_1 (GnomePrintContext *pc, char const *text); +int print_show (GnomePrintContext *pc, char const *text); + +/* Use these instead of gnome_font_get_width_string[_n] ! */ +double get_width_string_n (GnomeFont *font,char const* text,guint n); +double get_width_string (GnomeFont *font,char const* text); void print_make_rectangle_path (GnomePrintContext *pc, double left, double bottom, diff -ru src-orig/print.c src/print.c --- src-orig/print.c Fri Feb 23 16:19:24 2001 +++ src/print.c Sat Mar 10 13:22:53 2001 @@ -271,7 +271,7 @@ return; } - len = gnome_font_get_width_string (pj->decoration_font, text); + len = get_width_string (pj->decoration_font, text); pm = &pj->pi->margins; switch (side){ @@ -291,9 +291,7 @@ x = 0; } gnome_print_moveto (pj->print_context, x, y); - /* FIXME: - * Switch this back to gnome_print_show once we use UTF-8 internally */ - print_show_iso8859_1 (pj->print_context, text); + print_show (pj->print_context, text); g_free (text); }