[gnumeric] xls: fix import/export of rich text



commit f7e24002aedc65e0e8d39919f12623560bf53425
Author: Morten Welinder <terra gnome org>
Date:   Fri Mar 21 22:44:21 2014 -0400

    xls: fix import/export of rich text
    
    Export incorrectly left the last rich text item on for the remainder
    of the string.
    
    Import added every possible pango attribute.

 NEWS                           |    1 +
 plugins/excel/ChangeLog        |    7 +++++
 plugins/excel/ms-excel-read.c  |   49 ++++++++++++++++++++++-----------------
 plugins/excel/ms-excel-write.c |   22 +++++++++++++-----
 4 files changed, 52 insertions(+), 27 deletions(-)
---
diff --git a/NEWS b/NEWS
index f25a190..160ff42 100644
--- a/NEWS
+++ b/NEWS
@@ -5,6 +5,7 @@ Morten:
        * xlsx conditional format fixes.  [#726202]
        * Fix semantics of conditional format operators.  [#726806]
        * Fix xlsx quotes in formula strings.  [#726824]
+       * Fix xls export of rich text.
 
 --------------------------------------------------------------------------
 Gnumeric 1.12.13
diff --git a/plugins/excel/ChangeLog b/plugins/excel/ChangeLog
index c121651..4f50e78 100644
--- a/plugins/excel/ChangeLog
+++ b/plugins/excel/ChangeLog
@@ -1,5 +1,12 @@
 2014-03-21  Morten Welinder  <terra gnome org>
 
+       * ms-excel-write.c (txomarkup_new): Take also string argument.
+       Don't delete last run without attributes unless it is beyond the
+       string.
+
+       * ms-excel-read.c (ms_wb_get_font_markup): Defined a font's pango
+       attributes in terms of how it differs from font 0.
+
        * xlsx-write.c (xlsx_find_font): Use new gnm_style_equal_elem.
        (xlsx_find_border): Ditto.
        (xlsx_find_fill): Ditto.
diff --git a/plugins/excel/ms-excel-read.c b/plugins/excel/ms-excel-read.c
index 8a99cc7..aab0b4b 100644
--- a/plugins/excel/ms-excel-read.c
+++ b/plugins/excel/ms-excel-read.c
@@ -8,6 +8,7 @@
  *
  * (C) 1998-2001 Michael Meeks
  * (C) 2002-2008 Jody Goldberg
+ * (C) 2013-2013 Morten Welinder
  *
  * This program is free software; you can redistribute it and/or
  * modify it under the terms of the GNU General Public License as
@@ -1194,7 +1195,7 @@ excel_read_LABEL_markup (BiffQuery *q, ExcelReadSheet *esheet,
                        ms_biff_query_dump (q);
                });
 
-       txo_run.last = G_MAXINT;
+       txo_run.last = strlen (str);
 
        if (esheet_ver (esheet) >= MS_BIFF_V8) {
                XL_CHECK_CONDITION_VAL (ptr+2 <= end , NULL);
@@ -3244,26 +3245,30 @@ ms_wb_get_font_markup (MSContainer const *c, unsigned indx)
 {
        GnmXLImporter *importer = (GnmXLImporter *)c;
        ExcelFont const *fd = excel_font_get (importer, indx);
-       GnmColor *color;
 
-       if (fd == NULL) { /* random fallback */
-               fd = excel_font_get (importer, 0);
-               if (NULL == fd)
-                       return NULL;
-       }
+       if (fd == NULL || indx == 0)
+               return pango_attr_list_new ();
 
        if (fd->attrs == NULL) {
+               ExcelFont const *fd0 = excel_font_get (importer, 0);
                PangoAttrList *attrs;
-               PangoUnderline underline = gnm_translate_underline_to_pango
-                       (xls_uline_to_gnm_underline (fd->underline));
 
                attrs = pango_attr_list_new ();
-               add_attr (attrs, pango_attr_family_new (fd->fontname));
-               add_attr (attrs, pango_attr_size_new (fd->height * PANGO_SCALE / 20));
-               add_attr (attrs, pango_attr_weight_new (fd->boldness));
-               add_attr (attrs, pango_attr_style_new (fd->italic ? PANGO_STYLE_ITALIC : PANGO_STYLE_NORMAL));
-               add_attr (attrs, pango_attr_strikethrough_new (fd->struck_out));
-               add_attr (attrs, pango_attr_underline_new (underline));
+               if (strcmp (fd->fontname, fd0->fontname) != 0)
+                       add_attr (attrs, pango_attr_family_new (fd->fontname));
+               if (fd->height != fd0->height)
+                       add_attr (attrs, pango_attr_size_new (fd->height * PANGO_SCALE / 20));
+               if (fd->boldness != fd0->boldness)
+                       add_attr (attrs, pango_attr_weight_new (fd->boldness));
+               if (fd->italic != fd0->italic)
+                       add_attr (attrs, pango_attr_style_new (fd->italic ? PANGO_STYLE_ITALIC : 
PANGO_STYLE_NORMAL));
+               if (fd->struck_out != fd0->struck_out)
+                       add_attr (attrs, pango_attr_strikethrough_new (fd->struck_out));
+               if (fd->underline != fd0->underline) {
+                       PangoUnderline underline = gnm_translate_underline_to_pango
+                               (xls_uline_to_gnm_underline (fd->underline));
+                       add_attr (attrs, pango_attr_underline_new (underline));
+               }
 
                switch (fd->script) {
                case GO_FONT_SCRIPT_SUB:
@@ -3271,18 +3276,20 @@ ms_wb_get_font_markup (MSContainer const *c, unsigned indx)
                        break;
                default:
                case GO_FONT_SCRIPT_STANDARD:
-                       add_attr (attrs, go_pango_attr_subscript_new (FALSE));
-                       add_attr (attrs, go_pango_attr_superscript_new (FALSE));
+                       /* Just assume fd0 is standard.  */
                        break;
                case GO_FONT_SCRIPT_SUPER:
                        add_attr (attrs, go_pango_attr_superscript_new (TRUE));
                        break;
                }
 
-               color = (fd->color_idx == 127) ? style_color_black ()
-                       : excel_palette_get (importer, fd->color_idx);
-               add_attr (attrs, go_color_to_pango (color->go_color, TRUE));
-               style_color_unref (color);
+               if (fd->color_idx != fd0->color_idx) {
+                       GnmColor *color = (fd->color_idx == 127)
+                               ? style_color_black ()
+                               : excel_palette_get (importer, fd->color_idx);
+                       add_attr (attrs, go_color_to_pango (color->go_color, TRUE));
+                       style_color_unref (color);
+               }
 
                ((ExcelFont *)fd)->attrs = attrs;
        }
diff --git a/plugins/excel/ms-excel-write.c b/plugins/excel/ms-excel-write.c
index 0ffe399..77dea7e 100644
--- a/plugins/excel/ms-excel-write.c
+++ b/plugins/excel/ms-excel-write.c
@@ -8,7 +8,7 @@
  *    Jody Goldberg  (jody gnome org)
  *    Morten Welinder (terra gnome org)
  *
- * (C) 1998-2013 Michael Meeks, Jon K Hellan, Jody Goldberg, Morten Welinder
+ * (C) 1998-2014 Michael Meeks, Jon K Hellan, Jody Goldberg, Morten Welinder
  **/
 
 /*
@@ -2762,6 +2762,7 @@ xf_get_mstyle (XLExportBase *xle, gint idx)
 
 static GArray *
 txomarkup_new (ExcelWriteState *ewb,
+              const char *str,
               const PangoAttrList *markup,
               GnmStyle const *style)
 {
@@ -2769,6 +2770,7 @@ txomarkup_new (ExcelWriteState *ewb,
                pango_attr_list_get_iterator ((PangoAttrList*)markup);
        GArray *txo = g_array_sized_new (FALSE, FALSE, sizeof (int), 8);
        gboolean noattrs = TRUE;
+       gint slen = strlen (str);
 
        do {
                gint start, end;
@@ -2780,6 +2782,8 @@ txomarkup_new (ExcelWriteState *ewb,
                        /* Carry previous noattrs over.  */
                        break;
                }
+               if (start >= slen)
+                       break;
 
                attrs = pango_attr_iterator_get_attrs (iter);
                noattrs = (attrs == NULL);
@@ -2797,7 +2801,8 @@ txomarkup_new (ExcelWriteState *ewb,
                }
        } while (pango_attr_iterator_next (iter));
        /* trim end */
-       if (txo->len > 2 && noattrs)
+       if (txo->len > 2 && noattrs &&
+           g_array_index (txo, gint, txo->len - 2) >= slen)
                g_array_set_size (txo, txo->len - 2);
        pango_attr_iterator_destroy (iter);
 
@@ -2823,9 +2828,11 @@ cb_cell_pre_pass (GnmCell const *cell, ExcelWriteState *ewb)
                /* Collect unique fonts in rich text */
                if (VALUE_IS_STRING (cell->value) &&
                    go_format_is_markup (fmt)) {
-                       GArray *txo = txomarkup_new (ewb,
-                               go_format_get_markup (fmt),
-                               style);
+                       GArray *txo = txomarkup_new
+                               (ewb,
+                                value_peek_string (cell->value),
+                                go_format_get_markup (fmt),
+                                style);
 
                        g_hash_table_insert (ewb->cell_markup, (gpointer)cell, txo);
                        /* we use RSTRING, no need to add to SST */
@@ -6646,12 +6653,15 @@ extract_txomarkup (ExcelWriteState *ewb, SheetObject *so)
 {
        PangoAttrList *markup;
        GArray *txo;
+       char *text;
 
        g_object_get (G_OBJECT (so), "markup", &markup, NULL);
        if (!markup)
                return;
 
-       txo = txomarkup_new (ewb, markup, ewb->base.xf.default_style);
+       g_object_get (G_OBJECT (so), "text", &text, NULL);
+       txo = txomarkup_new (ewb, text, markup, ewb->base.xf.default_style);
+       g_free (text);
 
        /* It isn't a cell, but that doesn't matter here */
        g_hash_table_insert (ewb->cell_markup, (gpointer)so, txo);


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]