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"
30 #include <goffice/goffice.h>
31 #include <glib/gi18n-lib.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 */
39 format_nonnumber (GnmValue
const *value
)
41 switch (value
->v_any
.type
) {
46 return go_locale_boolean_name (value
->v_bool
.val
);
50 return value_peek_string (value
);
53 return value_error_name (GNM_ERROR_VALUE
, TRUE
);
58 g_assert_not_reached ();
64 hash_fill (PangoLayout
*layout
, GString
*str
, const GOFontMetrics
*metrics
, int col_width
)
67 if (str
) g_string_truncate (str
, 0);
68 if (layout
) pango_layout_set_text (layout
, "", -1);
70 int l
= metrics
->hash_width
> 0
71 ? col_width
/ metrics
->hash_width
77 g_string_truncate (hashstr
, 0);
79 hashstr
= g_string_sized_new (l
);
81 go_string_append_c_n (hashstr
, '#', l
);
83 pango_layout_set_text (layout
, hashstr
->str
, -1);
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
,
96 GODateConventions
const *date_conv
,
97 gboolean unicode_minus
)
99 GOFormatNumberError err
;
102 char *sval_free
= NULL
;
105 g_return_val_if_fail (value
!= NULL
, GO_FORMAT_NUMBER_INVALID_FORMAT
);
108 format
= VALUE_FMT (value
);
109 if (format
&& go_format_is_markup (format
))
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
);
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
);
133 gboolean rtl
= FALSE
;
134 PangoLayoutLine
*line
;
136 pango_layout_set_text (layout
, sval
, -1);
137 line
= pango_layout_get_line (layout
, 0);
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
)
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
,
153 val
, type
, sval
, NULL
,
154 col_width
, date_conv
, unicode_minus
);
159 case GO_FORMAT_NUMBER_OK
:
161 case GO_FORMAT_NUMBER_INVALID_FORMAT
:
163 case GO_FORMAT_NUMBER_DATE_ERROR
:
164 hash_fill (layout
, str
, metrics
, col_width
);
167 g_assert_not_reached ();
175 gnm_format_layout (PangoLayout
*layout
,
176 GOFontMetrics
*metrics
,
177 GOFormat
const *format
,
178 GnmValue
const *value
,
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
,
191 col_width
, date_conv
, unicode_minus
);
193 g_string_free (tmp_str
, TRUE
);
199 * format_value_gstring:
200 * @str: append the result here.
201 * @format: (nullable): #GOFormat.
202 * @value: #GnmValue to convert
203 * @col_width: maximum width in characters, -1 for unlimited
204 * @date_conv: #GODateConventions.
208 format_value_gstring (GString
*str
,
209 GOFormat
const *format
,
210 GnmValue
const *value
,
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
,
222 col_width
, date_conv
, FALSE
);
226 go_string_append_gstring (str
, tmp_str
);
227 g_string_free (tmp_str
, TRUE
);
234 * format_value_layout:
235 * @layout: A PangoLayout
236 * @format: (nullable): #GOFormat.
237 * @value: #GnmValue to convert
238 * @col_width: optional limit on width, -1 for unlimited
239 * @date_conv: #GODateConventions.
243 format_value_layout (PangoLayout
*layout
,
244 GOFormat
const *format
,
245 GnmValue
const *value
,
247 GODateConventions
const *date_conv
)
249 return format_value_common (layout
, NULL
,
250 go_format_measure_strlen
,
251 go_font_metrics_unit
,
253 col_width
, date_conv
, FALSE
);
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
);
269 gnm_format_specialize (GOFormat
const *fmt
, GnmValue
const *value
)
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
);
283 type
= VALUE_IS_ERROR (value
) ? 'E' : 'S';
286 #ifdef GNM_WITH_LONG_DOUBLE
287 return go_format_specializel (fmt
, val
, type
, NULL
);
289 return go_format_specialize (fmt
, val
, type
, NULL
);
294 gnm_format_is_date_for_value (GOFormat
const *fmt
,
295 GnmValue
const *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
)
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
)
320 fmt
= gnm_format_specialize (fmt
, value
);
322 mbd
= go_format_month_before_day (fmt
);
324 mbd
= go_locale_month_before_day ();
330 gnm_format_for_date_editing (GnmCell
const *cell
)
335 ? gnm_format_month_before_day (gnm_cell_get_format (cell
),
337 : go_locale_month_before_day ();
341 fmttxt
= gnm_format_frob_slashes ("d/m/yyyy");
345 fmttxt
= gnm_format_frob_slashes ("m/d/yyyy");
348 fmttxt
= gnm_format_frob_slashes ("yyyy-m-d");
352 fmt
= go_format_new_from_XL (fmttxt
);
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
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
= '/';
374 for (s
= df
->str
; *s
; s
++) {
376 case 'd': case 'm': case 'y':
377 while (g_ascii_isalpha (*s
))
379 while (g_unichar_isspace (g_utf8_get_char (s
)))
380 s
= g_utf8_next_char (s
);
382 g_unichar_ispunct (g_utf8_get_char (s
))) {
383 date_sep
= g_utf8_get_char (s
);
395 g_string_append_unichar (res
, date_sep
);
397 g_string_append_c (res
, *fmt
);
401 return g_string_free (res
, FALSE
);
406 gnm_format_has_hour (GOFormat
const *fmt
,
407 GnmValue
const *value
)
410 fmt
= gnm_format_specialize (fmt
, value
);
412 return go_format_has_hour (fmt
);
416 gnm_format_import (const char *fmt
,
417 GnmFormatImportFlags flags
)
419 GOFormat
*res
= go_format_new_from_XL (fmt
);
422 if (!go_format_is_invalid (res
))
426 if ((flags
& GNM_FORMAT_IMPORT_PATCHUP_INCOMPLETE
) &&
428 fmt
[len
- 1] == '_') {
429 GString
*fmt2
= g_string_new (fmt
);
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
);
442 if (flags
& GNM_FORMAT_IMPORT_NULL_INVALID
) {
443 go_format_unref (res
);