[goffice] GOFormat: improve general rendering; add more tests.
- From: Morten Welinder <mortenw src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [goffice] GOFormat: improve general rendering; add more tests.
- Date: Wed, 2 Apr 2014 20:51:23 +0000 (UTC)
commit ffd71bca70abeea628e5388ef810221e67de2a54
Author: Morten Welinder <terra gnome org>
Date: Wed Apr 2 16:50:55 2014 -0400
GOFormat: improve general rendering; add more tests.
Note, that tests still fail.
goffice/utils/go-format.c | 45 +++++++++++++++++++++++++++++++--------------
tests/test-format.c | 29 +++++++++++++++++++++++++----
2 files changed, 56 insertions(+), 18 deletions(-)
---
diff --git a/goffice/utils/go-format.c b/goffice/utils/go-format.c
index c176253..fe23330 100644
--- a/goffice/utils/go-format.c
+++ b/goffice/utils/go-format.c
@@ -4809,6 +4809,18 @@ drop_zeroes (GString *str, int *prec)
}
#endif
+static int
+SUFFIX(ilog10) (DOUBLE x)
+{
+ if (x >= 1000)
+ return (int)log10 (x);
+ if (x >= 100)
+ return 2;
+ if (x >= 10)
+ return 1;
+ return 0;
+}
+
/**
* go_render_general:
@@ -4854,8 +4866,8 @@ SUFFIX(go_render_general) (PangoLayout *layout, GString *str,
guint num_shape,
guint shape_flags)
{
- DOUBLE aval, l10;
- int prec, safety, digs, maxdigits = PREFIX(DIG);
+ DOUBLE aval;
+ int prec, safety, digs, digs_as_int, maxdigits = PREFIX(DIG);
size_t epos;
gboolean rounds_to_0;
int sign_width;
@@ -4871,15 +4883,18 @@ SUFFIX(go_render_general) (PangoLayout *layout, GString *str,
col_width = INT_MAX;
sign_width = 0;
} else {
- int w = col_width / min_digit_width;
+ int w;
+
+ sign_width = unicode_minus
+ ? metrics->minus_width
+ : metrics->hyphen_width;
+
+ w = (col_width - (val <= -0.5 ? sign_width : 0)) / min_digit_width;
if (w <= maxdigits) {
/* We're limited by width. */
maxdigits = w;
check_val = FALSE;
}
- sign_width = unicode_minus
- ? metrics->minus_width
- : metrics->hyphen_width;
}
#ifdef DEBUG_GENERAL
@@ -4892,25 +4907,24 @@ SUFFIX(go_render_general) (PangoLayout *layout, GString *str,
aval = SUFFIX(fabs) (val);
if (aval >= SUFFIX(1e15) || aval < SUFFIX(1e-4))
goto e_notation;
- l10 = SUFFIX(log10) (aval);
- /* Number of digits in [aval]. */
- digs = (aval >= 1 ? 1 + (int)l10 : 1);
+ /* Number of digits in round(aval). */
+ digs_as_int = (aval >= 9.5 ? 1 + SUFFIX(ilog10) (aval + 0.5) : 1);
/* Check if there is room for the whole part, including sign. */
safety = metrics->avg_digit_width / 2;
- if (digs * min_digit_width > col_width) {
+ if (digs_as_int * min_digit_width > col_width) {
#ifdef DEBUG_GENERAL
g_print ("No room for whole part.\n");
#endif
goto e_notation;
- } else if (digs * metrics->max_digit_width + safety <
+ } else if (digs_as_int * metrics->max_digit_width + safety <
col_width - (val > 0 ? 0 : sign_width)) {
#ifdef DEBUG_GENERAL
g_print ("Room for whole part.\n");
#endif
- if (val == SUFFIX(floor) (val) || digs == maxdigits) {
+ if (val == SUFFIX(floor) (val) || digs_as_int == maxdigits) {
g_string_printf (str, "%.0" FORMAT_f, val);
HANDLE_NUMERAL_SHAPE;
HANDLE_SIGN (0);
@@ -4931,10 +4945,13 @@ SUFFIX(go_render_general) (PangoLayout *layout, GString *str,
if (w > col_width)
goto e_notation;
- if (val == SUFFIX(floor) (val) || digs == maxdigits)
+ if (val == SUFFIX(floor) (val) || digs_as_int == maxdigits)
return;
}
+ /* Number of digits in [aval]. */
+ digs = (aval >= 1 ? 1 + SUFFIX(ilog10) (aval) : 1);
+
prec = maxdigits - digs;
g_string_printf (str, "%.*" FORMAT_f, prec, val);
if (check_val) {
@@ -4972,7 +4989,7 @@ SUFFIX(go_render_general) (PangoLayout *layout, GString *str,
e_notation:
rounds_to_0 = (aval < 0.5);
prec = (col_width -
- (val > 0 ? 0 : sign_width) -
+ (val >= 0 ? 0 : sign_width) -
(aval < 1 ? sign_width : metrics->plus_width) -
metrics->E_width) / min_digit_width - 3;
if (prec <= 0) {
diff --git a/tests/test-format.c b/tests/test-format.c
index 0d34245..3ce2cd5 100644
--- a/tests/test-format.c
+++ b/tests/test-format.c
@@ -17,7 +17,10 @@ test_general_format_1 (double val, int width, const char *expected)
g_printerr ("go_render_general: %.17g %d -> \"%s\"\n",
val, width, str->str);
- g_assert (strcmp (str->str, expected) == 0);
+ if (expected && strcmp (str->str, expected) != 0) {
+ g_printerr ("Expected \"%s\"\n", expected);
+ g_assert (0);
+ }
g_string_free (str, TRUE);
}
@@ -25,13 +28,31 @@ test_general_format_1 (double val, int width, const char *expected)
static void
test_general_format (void)
{
- test_general_format_1 (-9.5, 5, "-9.5");
- test_general_format_1 (-9.5, 4, "-9.5");
- test_general_format_1 (-9.5, 3, "-10");
+ test_general_format_1 (0, 5, "0");
+ test_general_format_1 (0, 3, "0");
+ test_general_format_1 (0, 2, "0");
+ test_general_format_1 (0, 1, "0");
+
+ test_general_format_1 (9.25, 5, "9.25");
+ test_general_format_1 (9.25, 3, "9.3");
+ test_general_format_1 (9.25, 2, "9");
+ test_general_format_1 (9.25, 1, "9");
test_general_format_1 (9.5, 5, "9.5");
test_general_format_1 (9.5, 3, "9.5");
test_general_format_1 (9.5, 2, "10");
+ test_general_format_1 (9.5, 1, NULL);
+
+ test_general_format_1 (-9.5, 5, "-9.5");
+ test_general_format_1 (-9.5, 4, "-9.5");
+ test_general_format_1 (-9.5, 3, "-10");
+ test_general_format_1 (-9.5, 2, NULL);
+
+ test_general_format_1 (-9.25, 5, "-9.25");
+ test_general_format_1 (-9.25, 4, "-9.3");
+ test_general_format_1 (-9.25, 2, "-9");
+ test_general_format_1 (-9.25, 1, NULL);
+
}
/* ------------------------------------------------------------------------- */
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]