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>
29 #include <goffice/goffice.h>
30 #include <glib/gi18n-lib.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 */
38 format_nonnumber (GnmValue
const *value
)
40 switch (value
->v_any
.type
) {
45 return go_locale_boolean_name (value
->v_bool
.val
);
49 return value_peek_string (value
);
52 return value_error_name (GNM_ERROR_VALUE
, TRUE
);
57 g_assert_not_reached ();
63 hash_fill (PangoLayout
*layout
, GString
*str
, const GOFontMetrics
*metrics
, int col_width
)
66 if (str
) g_string_truncate (str
, 0);
67 if (layout
) pango_layout_set_text (layout
, "", -1);
69 int l
= metrics
->hash_width
> 0
70 ? col_width
/ metrics
->hash_width
76 g_string_truncate (hashstr
, 0);
78 hashstr
= g_string_sized_new (l
);
80 go_string_append_c_n (hashstr
, '#', l
);
82 pango_layout_set_text (layout
, hashstr
->str
, -1);
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
,
95 GODateConventions
const *date_conv
,
96 gboolean unicode_minus
)
98 GOFormatNumberError err
;
101 char *sval_free
= NULL
;
104 g_return_val_if_fail (value
!= NULL
, GO_FORMAT_NUMBER_INVALID_FORMAT
);
107 format
= VALUE_FMT (value
);
108 if (format
&& go_format_is_markup (format
))
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
);
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
);
132 gboolean rtl
= FALSE
;
133 PangoLayoutLine
*line
;
135 pango_layout_set_text (layout
, sval
, -1);
136 line
= pango_layout_get_line (layout
, 0);
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
)
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
,
152 val
, type
, sval
, NULL
,
153 col_width
, date_conv
, unicode_minus
);
158 case GO_FORMAT_NUMBER_OK
:
160 case GO_FORMAT_NUMBER_INVALID_FORMAT
:
162 case GO_FORMAT_NUMBER_DATE_ERROR
:
163 hash_fill (layout
, str
, metrics
, col_width
);
166 g_assert_not_reached ();
174 gnm_format_layout (PangoLayout
*layout
,
175 GOFontMetrics
*metrics
,
176 GOFormat
const *format
,
177 GnmValue
const *value
,
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
,
190 col_width
, date_conv
, unicode_minus
);
192 g_string_free (tmp_str
, TRUE
);
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.
207 format_value_gstring (GString
*str
,
208 GOFormat
const *format
,
209 GnmValue
const *value
,
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
,
221 col_width
, date_conv
, FALSE
);
225 go_string_append_gstring (str
, tmp_str
);
226 g_string_free (tmp_str
, TRUE
);
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.
242 format_value_layout (PangoLayout
*layout
,
243 GOFormat
const *format
,
244 GnmValue
const *value
,
246 GODateConventions
const *date_conv
)
248 return format_value_common (layout
, NULL
,
249 go_format_measure_strlen
,
250 go_font_metrics_unit
,
252 col_width
, date_conv
, FALSE
);
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
);
268 gnm_format_specialize (GOFormat
const *fmt
, GnmValue
const *value
)
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
);
282 type
= VALUE_IS_ERROR (value
) ? 'E' : 'S';
285 #ifdef GNM_WITH_LONG_DOUBLE
286 return go_format_specializel (fmt
, val
, type
, NULL
);
288 return go_format_specialize (fmt
, val
, type
, NULL
);
293 gnm_format_is_date_for_value (GOFormat
const *fmt
,
294 GnmValue
const *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
)
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
)
319 fmt
= gnm_format_specialize (fmt
, value
);
321 mbd
= go_format_month_before_day (fmt
);
323 mbd
= go_locale_month_before_day ();
329 gnm_format_for_date_editing (GnmCell
const *cell
)
334 ? gnm_format_month_before_day (gnm_cell_get_format (cell
),
336 : go_locale_month_before_day ();
340 fmttxt
= gnm_format_frob_slashes ("d/m/yyyy");
344 fmttxt
= gnm_format_frob_slashes ("m/d/yyyy");
347 fmttxt
= gnm_format_frob_slashes ("yyyy-m-d");
351 fmt
= go_format_new_from_XL (fmttxt
);
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
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
= '/';
373 for (s
= df
->str
; *s
; s
++) {
375 case 'd': case 'm': case 'y':
376 while (g_ascii_isalpha (*s
))
378 while (g_unichar_isspace (g_utf8_get_char (s
)))
379 s
= g_utf8_next_char (s
);
381 g_unichar_ispunct (g_utf8_get_char (s
))) {
382 date_sep
= g_utf8_get_char (s
);
394 g_string_append_unichar (res
, date_sep
);
396 g_string_append_c (res
, *fmt
);
400 return g_string_free (res
, FALSE
);
405 gnm_format_has_hour (GOFormat
const *fmt
,
406 GnmValue
const *value
)
409 fmt
= gnm_format_specialize (fmt
, value
);
411 return go_format_has_hour (fmt
);
415 gnm_format_import (const char *fmt
,
416 GnmFormatImportFlags flags
)
418 GOFormat
*res
= go_format_new_from_XL (fmt
);
421 if (!go_format_is_invalid (res
))
425 if ((flags
& GNM_FORMAT_IMPORT_PATCHUP_INCOMPLETE
) &&
427 fmt
[len
- 1] == '_') {
428 GString
*fmt2
= g_string_new (fmt
);
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
);
441 if (flags
& GNM_FORMAT_IMPORT_NULL_INVALID
) {
442 go_format_unref (res
);