Update Spanish translation
[gnumeric.git] / src / gnm-format.c
blob17e81ce75c30f9cbba715c39279c6d71cb48fa69
1 /* format.c - attempts to emulate excel's number formatting ability.
3 * Copyright (C) 1998 Chris Lahey, Miguel de Icaza
4 * Copyright (C) 2006-2007 Morten Welinder (terra@gnome.org)
6 * Redid the format parsing routine to make it accept more of the Excel
7 * formats. The number rendeing code from Chris has not been touched,
8 * that routine is pretty good.
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, see <https://www.gnu.org/licenses/>.
24 #include <gnumeric-config.h>
25 #include <gnm-format.h>
26 #include <value.h>
27 #include <cell.h>
29 #include <goffice/goffice.h>
30 #include <glib/gi18n-lib.h>
31 #include <string.h>
32 #include <style-font.h>
34 #define UTF8_NEWLINE "\xe2\x86\xa9" /* unicode U+21A9 */
35 #define UTF8_NEWLINE_RTL "\xe2\x86\xaa" /* unicode U+21AA */
37 static char const *
38 format_nonnumber (GnmValue const *value)
40 switch (value->v_any.type) {
41 case VALUE_EMPTY:
42 return "";
44 case VALUE_BOOLEAN:
45 return go_locale_boolean_name (value->v_bool.val);
47 case VALUE_ERROR:
48 case VALUE_STRING:
49 return value_peek_string (value);
51 case VALUE_CELLRANGE:
52 return value_error_name (GNM_ERROR_VALUE, TRUE);
54 case VALUE_ARRAY:
55 case VALUE_FLOAT:
56 default:
57 g_assert_not_reached ();
59 return "";
62 static void
63 hash_fill (PangoLayout *layout, GString *str, const GOFontMetrics *metrics, int col_width)
65 if (col_width <= 0) {
66 if (str) g_string_truncate (str, 0);
67 if (layout) pango_layout_set_text (layout, "", -1);
68 } else {
69 int l = metrics->hash_width > 0
70 ? col_width / metrics->hash_width
71 : 1;
72 GString *hashstr;
74 if (str) {
75 hashstr = str;
76 g_string_truncate (hashstr, 0);
77 } else {
78 hashstr = g_string_sized_new (l);
80 go_string_append_c_n (hashstr, '#', l);
81 if (layout)
82 pango_layout_set_text (layout, hashstr->str, -1);
83 if (str != hashstr)
84 g_string_free (hashstr, TRUE);
88 static GOFormatNumberError
89 format_value_common (PangoLayout *layout, GString *str,
90 const GOFormatMeasure measure,
91 const GOFontMetrics *metrics,
92 GOFormat const *format,
93 GnmValue const *value,
94 int col_width,
95 GODateConventions const *date_conv,
96 gboolean unicode_minus)
98 GOFormatNumberError err;
99 gnm_float val;
100 const char *sval;
101 char *sval_free = NULL;
102 char type;
104 g_return_val_if_fail (value != NULL, GO_FORMAT_NUMBER_INVALID_FORMAT);
106 if (format == NULL)
107 format = VALUE_FMT (value);
108 if (format && go_format_is_markup (format))
109 format = NULL;
111 /* Use top left corner of an array result. This will not work for
112 * ranges because we dont't have a location */
113 if (value->v_any.type == VALUE_ARRAY)
114 value = value_area_fetch_x_y (value, 0, 0, NULL);
116 if (VALUE_IS_FLOAT (value)) {
117 val = value_get_as_float (value);
118 type = 'F';
119 sval = NULL;
120 } else {
121 val = 0;
122 /* Close enough: */
123 type = VALUE_IS_ERROR (value) ? 'E' : 'S';
124 sval = format_nonnumber (value);
125 if (sval != NULL && layout != NULL &&
126 pango_layout_get_single_paragraph_mode (layout)
127 && strchr (sval, '\n') != NULL) {
128 /* We are in single paragraph mode. This happens in HALIGN_FILL */
129 GString *str = g_string_new (sval);
130 gchar *ptr;
131 PangoDirection dir;
132 gboolean rtl = FALSE;
133 PangoLayoutLine *line;
135 pango_layout_set_text (layout, sval, -1);
136 line = pango_layout_get_line (layout, 0);
137 if (line) {
138 dir = line->resolved_dir;
139 rtl = (dir == PANGO_DIRECTION_RTL || dir == PANGO_DIRECTION_TTB_RTL
140 || dir == PANGO_DIRECTION_WEAK_RTL);
143 while ((ptr = strchr (str->str, '\n')) != NULL)
144 go_string_replace
145 (str, ptr - str->str, 1, rtl ? UTF8_NEWLINE_RTL : UTF8_NEWLINE, -1);
147 sval = sval_free = g_string_free (str, FALSE);
150 err = gnm_format_value_gstring (layout, str, measure, metrics,
151 format,
152 val, type, sval, NULL,
153 col_width, date_conv, unicode_minus);
155 g_free (sval_free);
157 switch (err) {
158 case GO_FORMAT_NUMBER_OK:
159 break;
160 case GO_FORMAT_NUMBER_INVALID_FORMAT:
161 break;
162 case GO_FORMAT_NUMBER_DATE_ERROR:
163 hash_fill (layout, str, metrics, col_width);
164 break;
165 default:
166 g_assert_not_reached ();
169 return err;
173 GOFormatNumberError
174 gnm_format_layout (PangoLayout *layout,
175 GOFontMetrics *metrics,
176 GOFormat const *format,
177 GnmValue const *value,
178 int col_width,
179 GODateConventions const *date_conv,
180 gboolean unicode_minus)
182 GString *tmp_str = g_string_sized_new (100);
183 GOFormatNumberError err;
185 err = format_value_common (layout, tmp_str,
186 go_format_measure_pango,
187 metrics,
188 format,
189 value,
190 col_width, date_conv, unicode_minus);
192 g_string_free (tmp_str, TRUE);
194 return err;
198 * format_value_gstring:
199 * @str: append the result here.
200 * @format: (nullable): #GOFormat.
201 * @value: #GnmValue to convert
202 * @col_width: maximum width in characters, -1 for unlimited
203 * @date_conv: #GODateConventions.
206 GOFormatNumberError
207 format_value_gstring (GString *str,
208 GOFormat const *format,
209 GnmValue const *value,
210 int col_width,
211 GODateConventions const *date_conv)
213 GString *tmp_str = str->len ? g_string_sized_new (100) : NULL;
214 GOFormatNumberError err;
216 err = format_value_common (NULL, tmp_str ? tmp_str : str,
217 go_format_measure_strlen,
218 go_font_metrics_unit,
219 format,
220 value,
221 col_width, date_conv, FALSE);
223 if (tmp_str) {
224 if (!err)
225 go_string_append_gstring (str, tmp_str);
226 g_string_free (tmp_str, TRUE);
229 return err;
233 * format_value_layout:
234 * @layout: A PangoLayout
235 * @format: (nullable): #GOFormat.
236 * @value: #GnmValue to convert
237 * @col_width: optional limit on width, -1 for unlimited
238 * @date_conv: #GODateConventions.
241 GOFormatNumberError
242 format_value_layout (PangoLayout *layout,
243 GOFormat const *format,
244 GnmValue const *value,
245 int col_width,
246 GODateConventions const *date_conv)
248 return format_value_common (layout, NULL,
249 go_format_measure_strlen,
250 go_font_metrics_unit,
251 format, value,
252 col_width, date_conv, FALSE);
256 gchar *
257 format_value (GOFormat const *format,
258 GnmValue const *value,
259 int col_width, GODateConventions const *date_conv)
261 GString *result = g_string_sized_new (20);
262 format_value_gstring (result, format, value,
263 col_width, date_conv);
264 return g_string_free (result, FALSE);
267 GOFormat const *
268 gnm_format_specialize (GOFormat const *fmt, GnmValue const *value)
270 char type;
271 gnm_float val;
273 g_return_val_if_fail (fmt != NULL, go_format_general ());
274 g_return_val_if_fail (value != NULL, fmt);
276 if (VALUE_IS_FLOAT (value)) {
277 val = value_get_as_float (value);
278 type = 'F';
279 } else {
280 val = 0;
281 /* Close enough: */
282 type = VALUE_IS_ERROR (value) ? 'E' : 'S';
285 #ifdef GNM_WITH_LONG_DOUBLE
286 return go_format_specializel (fmt, val, type, NULL);
287 #else
288 return go_format_specialize (fmt, val, type, NULL);
289 #endif
293 gnm_format_is_date_for_value (GOFormat const *fmt,
294 GnmValue const *value)
296 if (value)
297 fmt = gnm_format_specialize (fmt, value);
299 return go_format_is_date (fmt);
303 gnm_format_is_time_for_value (GOFormat const *fmt,
304 GnmValue const *value)
306 if (value)
307 fmt = gnm_format_specialize (fmt, value);
309 return go_format_is_time (fmt);
313 gnm_format_month_before_day (GOFormat const *fmt,
314 GnmValue const *value)
316 int mbd;
318 if (value)
319 fmt = gnm_format_specialize (fmt, value);
321 mbd = go_format_month_before_day (fmt);
322 if (mbd < 0)
323 mbd = go_locale_month_before_day ();
325 return mbd;
328 GOFormat *
329 gnm_format_for_date_editing (GnmCell const *cell)
331 char *fmttxt;
332 GOFormat *fmt;
333 int mbd = cell
334 ? gnm_format_month_before_day (gnm_cell_get_format (cell),
335 cell->value)
336 : go_locale_month_before_day ();
338 switch (mbd) {
339 case 0:
340 fmttxt = gnm_format_frob_slashes ("d/m/yyyy");
341 break;
342 default:
343 case 1:
344 fmttxt = gnm_format_frob_slashes ("m/d/yyyy");
345 break;
346 case 2:
347 fmttxt = gnm_format_frob_slashes ("yyyy-m-d");
348 break;
351 fmt = go_format_new_from_XL (fmttxt);
352 g_free (fmttxt);
353 return fmt;
357 * Change slashes to whatever the locale uses for date separation.
358 * Note: this operates on strings, not GOFormats.
360 * We aren't doing this completely right: a locale might use 24/12-1999 and
361 * we'll just use the slash.
363 * If it wasn't so hacky, this should go to go-locale.c
365 char *
366 gnm_format_frob_slashes (const char *fmt)
368 const GString *df = go_locale_get_date_format();
369 GString *res = g_string_new (NULL);
370 gunichar date_sep = '/';
371 const char *s;
373 for (s = df->str; *s; s++) {
374 switch (*s) {
375 case 'd': case 'm': case 'y':
376 while (g_ascii_isalpha (*s))
377 s++;
378 while (g_unichar_isspace (g_utf8_get_char (s)))
379 s = g_utf8_next_char (s);
380 if (*s != ',' &&
381 g_unichar_ispunct (g_utf8_get_char (s))) {
382 date_sep = g_utf8_get_char (s);
383 goto got_date_sep;
385 break;
386 default:
387 ; /* Nothing */
390 got_date_sep:
392 while (*fmt) {
393 if (*fmt == '/') {
394 g_string_append_unichar (res, date_sep);
395 } else
396 g_string_append_c (res, *fmt);
397 fmt++;
400 return g_string_free (res, FALSE);
404 gboolean
405 gnm_format_has_hour (GOFormat const *fmt,
406 GnmValue const *value)
408 if (value)
409 fmt = gnm_format_specialize (fmt, value);
411 return go_format_has_hour (fmt);
414 GOFormat *
415 gnm_format_import (const char *fmt,
416 GnmFormatImportFlags flags)
418 GOFormat *res = go_format_new_from_XL (fmt);
419 size_t len;
421 if (!go_format_is_invalid (res))
422 return res;
424 len = strlen (fmt);
425 if ((flags & GNM_FORMAT_IMPORT_PATCHUP_INCOMPLETE) &&
426 len > 0 &&
427 fmt[len - 1] == '_') {
428 GString *fmt2 = g_string_new (fmt);
429 GOFormat *res2;
431 g_string_append_c (fmt2, ')');
432 res2 = go_format_new_from_XL (fmt2->str);
433 g_string_free (fmt2, TRUE);
435 if (!go_format_is_invalid (res2)) {
436 go_format_unref (res);
437 return res2;
441 if (flags & GNM_FORMAT_IMPORT_NULL_INVALID) {
442 go_format_unref (res);
443 res = NULL;
446 return res;