1 /* format.c - attempts to emulate excel's number formatting ability.
2 *
3 * Copyright (C) 1998 Chris Lahey, Miguel de Icaza
4 * Copyright (C) 2006-2007 Morten Welinder (terra@gnome.org)
5 *
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.
9 *
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.
14 *
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.
19 *
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/>.
22 */
23
24 #include <gnumeric-config.h>
25 #include <gnm-format.h>
26 #include <value.h>
27 #include <cell.h>
28
29 #include <goffice/goffice.h>
30 #include <glib/gi18n-lib.h>
31 #include <string.h>
32 #include <style-font.h>
33
34 #define UTF8_NEWLINE "\xe2\x86\xa9" /* unicode U+21A9 */
35 #define UTF8_NEWLINE_RTL "\xe2\x86\xaa" /* unicode U+21AA */
36
37 static char const *
format_nonnumber(GnmValue const * value)38 format_nonnumber (GnmValue const *value)
39 {
40 switch (value->v_any.type) {
41 case VALUE_EMPTY:
42 return "";
43
44 case VALUE_BOOLEAN:
45 return go_locale_boolean_name (value->v_bool.val);
46
47 case VALUE_ERROR:
48 case VALUE_STRING:
49 return value_peek_string (value);
50
51 case VALUE_CELLRANGE:
52 return value_error_name (GNM_ERROR_VALUE, TRUE);
53
54 case VALUE_ARRAY:
55 case VALUE_FLOAT:
56 default:
57 g_assert_not_reached ();
58 }
59 return "";
60 }
61
62 static void
hash_fill(PangoLayout * layout,GString * str,const GOFontMetrics * metrics,int col_width)63 hash_fill (PangoLayout *layout, GString *str, const GOFontMetrics *metrics, int col_width)
64 {
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;
73
74 if (str) {
75 hashstr = str;
76 g_string_truncate (hashstr, 0);
77 } else {
78 hashstr = g_string_sized_new (l);
79 }
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);
85 }
86 }
87
88 static GOFormatNumberError
format_value_common(PangoLayout * layout,GString * str,const GOFormatMeasure measure,const GOFontMetrics * metrics,GOFormat const * format,GnmValue const * value,int col_width,GODateConventions const * date_conv,gboolean unicode_minus)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)
97 {
98 GOFormatNumberError err;
99 gnm_float val;
100 const char *sval;
101 char *sval_free = NULL;
102 char type;
103
104 g_return_val_if_fail (value != NULL, GO_FORMAT_NUMBER_INVALID_FORMAT);
105
106 if (format == NULL)
107 format = VALUE_FMT (value);
108 if (format && go_format_is_markup (format))
109 format = NULL;
110
111 /* Use top left corner of an array result. This will not work for
112 * ranges because we don't have a location */
113 if (value->v_any.type == VALUE_ARRAY)
114 value = value_area_fetch_x_y (value, 0, 0, NULL);
115
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;
134
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);
141 }
142
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);
146
147 sval = sval_free = g_string_free (str, FALSE);
148 }
149 }
150 err = gnm_format_value_gstring (layout, str, measure, metrics,
151 format,
152 val, type, sval, NULL,
153 col_width, date_conv, unicode_minus);
154
155 g_free (sval_free);
156
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 ();
167 }
168
169 return err;
170 }
171
172
173 GOFormatNumberError
gnm_format_layout(PangoLayout * layout,GOFontMetrics * metrics,GOFormat const * format,GnmValue const * value,int col_width,GODateConventions const * date_conv,gboolean unicode_minus)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)
181 {
182 GString *tmp_str = g_string_sized_new (100);
183 GOFormatNumberError err;
184
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);
191
192 g_string_free (tmp_str, TRUE);
193
194 return err;
195 }
196
197 /**
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.
204 *
205 **/
206 GOFormatNumberError
format_value_gstring(GString * str,GOFormat const * format,GnmValue const * value,int col_width,GODateConventions const * date_conv)207 format_value_gstring (GString *str,
208 GOFormat const *format,
209 GnmValue const *value,
210 int col_width,
211 GODateConventions const *date_conv)
212 {
213 GString *tmp_str = str->len ? g_string_sized_new (100) : NULL;
214 GOFormatNumberError err;
215
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);
222
223 if (tmp_str) {
224 if (!err)
225 go_string_append_gstring (str, tmp_str);
226 g_string_free (tmp_str, TRUE);
227 }
228
229 return err;
230 }
231
232 /**
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.
239 *
240 **/
241 GOFormatNumberError
format_value_layout(PangoLayout * layout,GOFormat const * format,GnmValue const * value,int col_width,GODateConventions const * date_conv)242 format_value_layout (PangoLayout *layout,
243 GOFormat const *format,
244 GnmValue const *value,
245 int col_width,
246 GODateConventions const *date_conv)
247 {
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);
253 }
254
255
256 gchar *
format_value(GOFormat const * format,GnmValue const * value,int col_width,GODateConventions const * date_conv)257 format_value (GOFormat const *format,
258 GnmValue const *value,
259 int col_width, GODateConventions const *date_conv)
260 {
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);
265 }
266
267 GOFormat const *
gnm_format_specialize(GOFormat const * fmt,GnmValue const * value)268 gnm_format_specialize (GOFormat const *fmt, GnmValue const *value)
269 {
270 char type;
271 gnm_float val;
272
273 g_return_val_if_fail (fmt != NULL, go_format_general ());
274 g_return_val_if_fail (value != NULL, fmt);
275
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';
283 }
284
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
290 }
291
292 int
gnm_format_is_date_for_value(GOFormat const * fmt,GnmValue const * value)293 gnm_format_is_date_for_value (GOFormat const *fmt,
294 GnmValue const *value)
295 {
296 if (value)
297 fmt = gnm_format_specialize (fmt, value);
298
299 return go_format_is_date (fmt);
300 }
301
302 int
gnm_format_is_time_for_value(GOFormat const * fmt,GnmValue const * value)303 gnm_format_is_time_for_value (GOFormat const *fmt,
304 GnmValue const *value)
305 {
306 if (value)
307 fmt = gnm_format_specialize (fmt, value);
308
309 return go_format_is_time (fmt);
310 }
311
312 int
gnm_format_month_before_day(GOFormat const * fmt,GnmValue const * value)313 gnm_format_month_before_day (GOFormat const *fmt,
314 GnmValue const *value)
315 {
316 int mbd;
317
318 if (value)
319 fmt = gnm_format_specialize (fmt, value);
320
321 mbd = go_format_month_before_day (fmt);
322 if (mbd < 0)
323 mbd = go_locale_month_before_day ();
324
325 return mbd;
326 }
327
328 GOFormat *
gnm_format_for_date_editing(GnmCell const * cell)329 gnm_format_for_date_editing (GnmCell const *cell)
330 {
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 ();
337
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;
349 }
350
351 fmt = go_format_new_from_XL (fmttxt);
352 g_free (fmttxt);
353 return fmt;
354 }
355
356 /*
357 * Change slashes to whatever the locale uses for date separation.
358 * Note: this operates on strings, not GOFormats.
359 *
360 * We aren't doing this completely right: a locale might use 24/12-1999 and
361 * we'll just use the slash.
362 *
363 * If it wasn't so hacky, this should go to go-locale.c
364 */
365 char *
gnm_format_frob_slashes(const char * fmt)366 gnm_format_frob_slashes (const char *fmt)
367 {
368 const GString *df = go_locale_get_date_format();
369 GString *res = g_string_new (NULL);
370 gunichar date_sep = '/';
371 const char *s;
372
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;
384 }
385 break;
386 default:
387 ; /* Nothing */
388 }
389 }
390 got_date_sep:
391
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++;
398 }
399
400 return g_string_free (res, FALSE);
401 }
402
403
404 gboolean
gnm_format_has_hour(GOFormat const * fmt,GnmValue const * value)405 gnm_format_has_hour (GOFormat const *fmt,
406 GnmValue const *value)
407 {
408 if (value)
409 fmt = gnm_format_specialize (fmt, value);
410
411 return go_format_has_hour (fmt);
412 }
413
414 GOFormat *
gnm_format_import(const char * fmt,GnmFormatImportFlags flags)415 gnm_format_import (const char *fmt,
416 GnmFormatImportFlags flags)
417 {
418 GOFormat *res = go_format_new_from_XL (fmt);
419 size_t len;
420
421 if (!go_format_is_invalid (res))
422 return res;
423
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;
430
431 g_string_append_c (fmt2, ')');
432 res2 = go_format_new_from_XL (fmt2->str);
433 g_string_free (fmt2, TRUE);
434
435 if (!go_format_is_invalid (res2)) {
436 go_format_unref (res);
437 return res2;
438 }
439 }
440
441 if (flags & GNM_FORMAT_IMPORT_NULL_INVALID) {
442 go_format_unref (res);
443 res = NULL;
444 }
445
446 return res;
447 }
448