Whitespace.
[gnumeric.git] / src / gnm-format.c
blob7e70cd9ccd106188a730c79a92c4069bd2363a8f
1 /* vim: set sw=8: -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
2 /* format.c - attempts to emulate excel's number formatting ability.
4 * Copyright (C) 1998 Chris Lahey, Miguel de Icaza
5 * Copyright (C) 2006-2007 Morten Welinder (terra@gnome.org)
7 * Redid the format parsing routine to make it accept more of the Excel
8 * formats. The number rendeing code from Chris has not been touched,
9 * that routine is pretty good.
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, see <https://www.gnu.org/licenses/>.
25 #include <gnumeric-config.h>
26 #include "gnm-format.h"
27 #include "value.h"
28 #include "cell.h"
30 #include <goffice/goffice.h>
31 #include <glib/gi18n-lib.h>
32 #include <string.h>
33 #include <style-font.h>
35 #define UTF8_NEWLINE "\xe2\x86\xa9" /* unicode U+21A9 */
36 #define UTF8_NEWLINE_RTL "\xe2\x86\xaa" /* unicode U+21AA */
38 static char const *
39 format_nonnumber (GnmValue const *value)
41 switch (value->v_any.type) {
42 case VALUE_EMPTY:
43 return "";
45 case VALUE_BOOLEAN:
46 return go_locale_boolean_name (value->v_bool.val);
48 case VALUE_ERROR:
49 case VALUE_STRING:
50 return value_peek_string (value);
52 case VALUE_CELLRANGE:
53 return value_error_name (GNM_ERROR_VALUE, TRUE);
55 case VALUE_ARRAY:
56 case VALUE_FLOAT:
57 default:
58 g_assert_not_reached ();
60 return "";
63 static void
64 hash_fill (PangoLayout *layout, GString *str, const GOFontMetrics *metrics, int col_width)
66 if (col_width <= 0) {
67 if (str) g_string_truncate (str, 0);
68 if (layout) pango_layout_set_text (layout, "", -1);
69 } else {
70 int l = metrics->hash_width > 0
71 ? col_width / metrics->hash_width
72 : 1;
73 GString *hashstr;
75 if (str) {
76 hashstr = str;
77 g_string_truncate (hashstr, 0);
78 } else {
79 hashstr = g_string_sized_new (l);
81 go_string_append_c_n (hashstr, '#', l);
82 if (layout)
83 pango_layout_set_text (layout, hashstr->str, -1);
84 if (str != hashstr)
85 g_string_free (hashstr, TRUE);
89 static GOFormatNumberError
90 format_value_common (PangoLayout *layout, GString *str,
91 const GOFormatMeasure measure,
92 const GOFontMetrics *metrics,
93 GOFormat const *format,
94 GnmValue const *value,
95 int col_width,
96 GODateConventions const *date_conv,
97 gboolean unicode_minus)
99 GOFormatNumberError err;
100 gnm_float val;
101 const char *sval;
102 char *sval_free = NULL;
103 char type;
105 g_return_val_if_fail (value != NULL, GO_FORMAT_NUMBER_INVALID_FORMAT);
107 if (format == NULL)
108 format = VALUE_FMT (value);
109 if (format && go_format_is_markup (format))
110 format = NULL;
112 /* Use top left corner of an array result. This will not work for
113 * ranges because we dont't have a location */
114 if (value->v_any.type == VALUE_ARRAY)
115 value = value_area_fetch_x_y (value, 0, 0, NULL);
117 if (VALUE_IS_FLOAT (value)) {
118 val = value_get_as_float (value);
119 type = 'F';
120 sval = NULL;
121 } else {
122 val = 0;
123 /* Close enough: */
124 type = VALUE_IS_ERROR (value) ? 'E' : 'S';
125 sval = format_nonnumber (value);
126 if (sval != NULL && layout != NULL &&
127 pango_layout_get_single_paragraph_mode (layout)
128 && strchr (sval, '\n') != NULL) {
129 /* We are in single paragraph mode. This happens in HALIGN_FILL */
130 GString *str = g_string_new (sval);
131 gchar *ptr;
132 PangoDirection dir;
133 gboolean rtl = FALSE;
134 PangoLayoutLine *line;
136 pango_layout_set_text (layout, sval, -1);
137 line = pango_layout_get_line (layout, 0);
138 if (line) {
139 dir = line->resolved_dir;
140 rtl = (dir == PANGO_DIRECTION_RTL || dir == PANGO_DIRECTION_TTB_RTL
141 || dir == PANGO_DIRECTION_WEAK_RTL);
144 while ((ptr = strchr (str->str, '\n')) != NULL)
145 go_string_replace
146 (str, ptr - str->str, 1, rtl ? UTF8_NEWLINE_RTL : UTF8_NEWLINE, -1);
148 sval = sval_free = g_string_free (str, FALSE);
151 err = gnm_format_value_gstring (layout, str, measure, metrics,
152 format,
153 val, type, sval, NULL,
154 col_width, date_conv, unicode_minus);
156 g_free (sval_free);
158 switch (err) {
159 case GO_FORMAT_NUMBER_OK:
160 break;
161 case GO_FORMAT_NUMBER_INVALID_FORMAT:
162 break;
163 case GO_FORMAT_NUMBER_DATE_ERROR:
164 hash_fill (layout, str, metrics, col_width);
165 break;
166 default:
167 g_assert_not_reached ();
170 return err;
174 GOFormatNumberError
175 gnm_format_layout (PangoLayout *layout,
176 GOFontMetrics *metrics,
177 GOFormat const *format,
178 GnmValue const *value,
179 int col_width,
180 GODateConventions const *date_conv,
181 gboolean unicode_minus)
183 GString *tmp_str = g_string_sized_new (100);
184 GOFormatNumberError err;
186 err = format_value_common (layout, tmp_str,
187 go_format_measure_pango,
188 metrics,
189 format,
190 value,
191 col_width, date_conv, unicode_minus);
193 g_string_free (tmp_str, TRUE);
195 return err;
199 * format_value_gstring:
200 * @str: append the result here.
201 * @format: #GOFormat.
202 * @value: #GnmValue to convert
203 * @col_width: optional
204 * @date_conv: #GODateConventions.
207 GOFormatNumberError
208 format_value_gstring (GString *str,
209 GOFormat const *format,
210 GnmValue const *value,
211 int col_width,
212 GODateConventions const *date_conv)
214 GString *tmp_str = str->len ? g_string_sized_new (100) : NULL;
215 GOFormatNumberError err;
217 err = format_value_common (NULL, tmp_str ? tmp_str : str,
218 go_format_measure_strlen,
219 go_font_metrics_unit,
220 format,
221 value,
222 col_width, date_conv, FALSE);
224 if (tmp_str) {
225 if (!err)
226 go_string_append_gstring (str, tmp_str);
227 g_string_free (tmp_str, TRUE);
230 return err;
234 * format_value_layout:
235 * @layout:
236 * @format: #GOFormat.
237 * @value: #GnmValue to convert
238 * @col_width: optional limit on width, -1 for unlimited
239 * @date_conv: #GODateConventions.
242 GOFormatNumberError
243 format_value_layout (PangoLayout *layout,
244 GOFormat const *format,
245 GnmValue const *value,
246 int col_width,
247 GODateConventions const *date_conv)
249 return format_value_common (layout, NULL,
250 go_format_measure_strlen,
251 go_font_metrics_unit,
252 format, value,
253 col_width, date_conv, FALSE);
257 gchar *
258 format_value (GOFormat const *format,
259 GnmValue const *value,
260 int col_width, GODateConventions const *date_conv)
262 GString *result = g_string_sized_new (20);
263 format_value_gstring (result, format, value,
264 col_width, date_conv);
265 return g_string_free (result, FALSE);
268 GOFormat const *
269 gnm_format_specialize (GOFormat const *fmt, GnmValue const *value)
271 char type;
272 gnm_float val;
274 g_return_val_if_fail (fmt != NULL, go_format_general ());
275 g_return_val_if_fail (value != NULL, fmt);
277 if (VALUE_IS_FLOAT (value)) {
278 val = value_get_as_float (value);
279 type = 'F';
280 } else {
281 val = 0;
282 /* Close enough: */
283 type = VALUE_IS_ERROR (value) ? 'E' : 'S';
286 #ifdef GNM_WITH_LONG_DOUBLE
287 return go_format_specializel (fmt, val, type, NULL);
288 #else
289 return go_format_specialize (fmt, val, type, NULL);
290 #endif
294 gnm_format_is_date_for_value (GOFormat const *fmt,
295 GnmValue const *value)
297 if (value)
298 fmt = gnm_format_specialize (fmt, value);
300 return go_format_is_date (fmt);
304 gnm_format_is_time_for_value (GOFormat const *fmt,
305 GnmValue const *value)
307 if (value)
308 fmt = gnm_format_specialize (fmt, value);
310 return go_format_is_time (fmt);
314 gnm_format_month_before_day (GOFormat const *fmt,
315 GnmValue const *value)
317 int mbd;
319 if (value)
320 fmt = gnm_format_specialize (fmt, value);
322 mbd = go_format_month_before_day (fmt);
323 if (mbd < 0)
324 mbd = go_locale_month_before_day ();
326 return mbd;
329 GOFormat *
330 gnm_format_for_date_editing (GnmCell const *cell)
332 char *fmttxt;
333 GOFormat *fmt;
334 int mbd = cell
335 ? gnm_format_month_before_day (gnm_cell_get_format (cell),
336 cell->value)
337 : go_locale_month_before_day ();
339 switch (mbd) {
340 case 0:
341 fmttxt = gnm_format_frob_slashes ("d/m/yyyy");
342 break;
343 default:
344 case 1:
345 fmttxt = gnm_format_frob_slashes ("m/d/yyyy");
346 break;
347 case 2:
348 fmttxt = gnm_format_frob_slashes ("yyyy-m-d");
349 break;
352 fmt = go_format_new_from_XL (fmttxt);
353 g_free (fmttxt);
354 return fmt;
358 * Change slashes to whatever the locale uses for date separation.
359 * Note: this operates on strings, not GOFormats.
361 * We aren't doing this completely right: a locale might use 24/12-1999 and
362 * we'll just use the slash.
364 * If it wasn't so hacky, this should go to go-locale.c
366 char *
367 gnm_format_frob_slashes (const char *fmt)
369 const GString *df = go_locale_get_date_format();
370 GString *res = g_string_new (NULL);
371 gunichar date_sep = '/';
372 const char *s;
374 for (s = df->str; *s; s++) {
375 switch (*s) {
376 case 'd': case 'm': case 'y':
377 while (g_ascii_isalpha (*s))
378 s++;
379 while (g_unichar_isspace (g_utf8_get_char (s)))
380 s = g_utf8_next_char (s);
381 if (*s != ',' &&
382 g_unichar_ispunct (g_utf8_get_char (s))) {
383 date_sep = g_utf8_get_char (s);
384 goto got_date_sep;
386 break;
387 default:
388 ; /* Nothing */
391 got_date_sep:
393 while (*fmt) {
394 if (*fmt == '/') {
395 g_string_append_unichar (res, date_sep);
396 } else
397 g_string_append_c (res, *fmt);
398 fmt++;
401 return g_string_free (res, FALSE);
405 gboolean
406 gnm_format_has_hour (GOFormat const *fmt,
407 GnmValue const *value)
409 if (value)
410 fmt = gnm_format_specialize (fmt, value);
412 return go_format_has_hour (fmt);
415 GOFormat *
416 gnm_format_import (const char *fmt,
417 GnmFormatImportFlags flags)
419 GOFormat *res = go_format_new_from_XL (fmt);
420 size_t len;
422 if (!go_format_is_invalid (res))
423 return res;
425 len = strlen (fmt);
426 if ((flags & GNM_FORMAT_IMPORT_PATCHUP_INCOMPLETE) &&
427 len > 0 &&
428 fmt[len - 1] == '_') {
429 GString *fmt2 = g_string_new (fmt);
430 GOFormat *res2;
432 g_string_append_c (fmt2, ')');
433 res2 = go_format_new_from_XL (fmt2->str);
434 g_string_free (fmt2, TRUE);
436 if (!go_format_is_invalid (res2)) {
437 go_format_unref (res);
438 return res2;
442 if (flags & GNM_FORMAT_IMPORT_NULL_INVALID) {
443 go_format_unref (res);
444 res = NULL;
447 return res;