Commit c84d5109 authored by Michael Meeks's avatar Michael Meeks

Jody Goldberg's colour mapping fixes.

parent 00e78795
......@@ -44,6 +44,7 @@ Nick Lamb
Jody Goldberg
* Supplied many new error and error checking functions.
* Vastly improved Excel color loading.
Several translations updated: thanks to our translators
......
1999-06-13 Michael Meeks <michael@edenproject.org>
* ms-excel-read.c (ms_excel_set_cell_colors): Remove identical color
warning. This is a useful bug at the moment.
1999-06-13 Jody Goldberg <jgoldberg@home.com>
* ms-excel-read.c (biff_get_error_text) : Use new global error strings.
(ms_excel_default_palette) : New function.
(ms_excel_palette_new) : Colors are stored
<DontCare><Blue><Green><Red>
rather than
<DontCare><Red><Green><Blue>
(ms_excel_palette_get) : Clone bytes to scale colors, begin to
handle more of the 'special' color indices and guess that indices
are offset by 8.
(ms_excel_palette_destroy) : Don't delete the default palette.
(ms_excel_set_cell_colors) : Don't filter too many bits, support
initial contrast methods.
(ms_excel_set_cell_font) : Ditto.
(ms_excel_set_cell_xf) : Ditto.
(biff_xf_data_new) : Default palette to internal palette.
1999-06-12 Michael Meeks <michael@edenproject.org>
* ms-excel-read.c (ms_excel_read_supporting_wb):
Renamed from _WB.
(ms_excel_read_name): Remove buggy names assert.
1999-06-11 Jody Goldberg <jgoldberg@home.com>
* biff-types.h: Add some missing types as defined in
the Excel 97 Developers kit. Begin coverage of Chart specific records.
Improve documentation of 'odd-ball' entries and collsions in the low
order numbering scheme.
* ms-obj.c (ms_obj_read_obj) : Remove unused sheet param
to facilitate support for Workbook level objects. Extend known object
types.
* ms-obj.h (ms_obj_read_obj) : Ditto.
* ms-obj.c (ms_chart_biff_read) : Initial experimental
harness for excel charts.
* ms-obj.h (ms_chart_biff_read) : Ditto.
* ms-excel-read.c (biff_name_data_new) : Take Workbook instead of sheet.
(ms_biff_unknown_code) : New function split out from.
(ms_excel_read_workbook) : Here.
(ms_excel_read_name) : New function split out from.
(ms_excel_read_cell) : Here.
(ms_excel_externname): New function split out from
(ms_excel_read_cell) : Here.
(ms_excel_read_supporting_WB) : New function. Will probably
split up in the future when the filename encoding is clarified.
(ms_excel_read_workbook) : Precheck for odd-balls and chart types.
Add hooks for other fields.
1999-06-09 Michael Meeks <michael@edenproject.org>
* ms-excel-read.c (ms_excel_read_workbook): Added version
......
......@@ -33,7 +33,8 @@
#include "ms-obj.h"
#include "ms-escher.h"
#define EXCEL_DEBUG 0
#define EXCEL_DEBUG 0
#define EXCEL_DEBUG_COLOR 0
/* This many styles are reserved */
#define XF_MAGIC_OFFSET (16 + 4)
......@@ -194,15 +195,15 @@ biff_get_error_text (const guint8 err)
char *buf ;
switch (err)
{
case 0: buf = "#NULL!" ; break ;
case 7: buf = "#DIV/0!" ; break ;
case 15: buf = "#VALUE!" ; break ;
case 23: buf = "#REF!" ; break ;
case 29: buf = "#NAME?" ; break ;
case 36: buf = "#NUM!" ; break ;
case 42: buf = "#N/A" ; break ;
case 0: buf = gnumeric_err_NULL; break ;
case 7: buf = gnumeric_err_DIV0; break ;
case 15: buf = gnumeric_err_VALUE; break ;
case 23: buf = gnumeric_err_REF; break ;
case 29: buf = gnumeric_err_NAME; break ;
case 36: buf = gnumeric_err_NUM; break ;
case 42: buf = gnumeric_err_NA; break ;
default:
buf = "#UNKNOWN!"; break ;
buf = _("#UNKNOWN!"); break ;
}
return buf ;
}
......@@ -698,9 +699,70 @@ biff_name_data_destroy (gpointer key, BIFF_NAME_DATA *bnd, gpointer userdata)
return 1 ;
}
/* NOTE: There is a built in palette, and it seems this may be appended
to it, anyway colours are seriously broken ATM, it is a non-trivial
fix: Michael. */
static MS_EXCEL_PALETTE *
ms_excel_default_palette ()
{
static MS_EXCEL_PALETTE * pal = NULL;
if (!pal)
{
static struct default_color
{
/* These were generated by creating a sheet and
* modifying the 1st color cell and saving. This
* created a custom palette. I then loaded the sheet
* into gnumeric and dumped the results. Unfortunately
* there was a bug in the extraction that swapped the
* red and blue. It is too much effort to retype this.
* So I'll leave it in this odd format for now.
*/
int b, g, r;
} default_colors[56] = {
{ 0,0,0 }, { 255,255,255 }, { 0,0,255 }, { 0,255,0 },
{ 255,0,0 }, { 0,255,255 }, { 255,0,255 }, { 255,255,0},
{ 0,0,128 }, { 0,128,0 }, { 128,0,0 }, { 0,128,128 },
{ 128,0,128 }, { 128,128,0 }, { 192,192,192}, {128,128,128},
{ 255,153,153}, {102,51,153}, {204,255,255}, {255,255,204 },
{ 102,0,102 }, { 128,128,255 }, {204,102,0}, {255,204,204 },
{ 128,0,0 }, { 255,0,255 }, { 0,255,255 }, { 255,255,0 },
{ 128,0,128 }, { 0,0,128 }, { 128,128,0 }, { 255,0,0 },
{255,204,0}, {255,255,204}, {204,255,204}, { 153,255,255 },
{255,204,153}, {204,153,255}, {255,153,204}, {153,204,255},
{255,102,51}, {204,204,51}, {0,204,153}, {0,204,255},
{0,153,255}, {0,102,255}, {153,102,102}, {150,150,150},
{102,51,0}, {102,153,51}, {0,51,0}, {0,51,51},
{0,51,153}, {102,51,153}, {153,51,51}, {51,51,51}
};
int entries = sizeof(default_colors)/
sizeof(struct default_color);
#if EXCEL_DEBUG_COLOR > 1
printf ("Creating default pallete\n");
#endif
pal = (MS_EXCEL_PALETTE *) g_malloc (sizeof (MS_EXCEL_PALETTE));
pal->length = entries;
pal->red = g_new (int, entries) ;
pal->green = g_new (int, entries) ;
pal->blue = g_new (int, entries) ;
pal->gnum_cols = g_new (StyleColor *, entries) ;
while (--entries >= 0) {
pal->red[entries] = default_colors[entries].r;
pal->green[entries] = default_colors[entries].g;
pal->blue[entries] = default_colors[entries].b;
pal->gnum_cols[entries] = NULL ;
}
}
return pal;
}
/* See: S59DC9.HTM */
static MS_EXCEL_PALETTE *
ms_excel_palette_new (BIFF_QUERY * q)
......@@ -716,15 +778,20 @@ ms_excel_palette_new (BIFF_QUERY * q)
pal->blue = g_new (int, len) ;
pal->gnum_cols = g_new (StyleColor *, len) ;
#if EXCEL_DEBUG_COLOR > 3
printf ("New palette with %d entries\n", len);
#endif
for (lp = 0; lp < len; lp++){
LONG num = BIFF_GETLONG (q->data + 2 + lp * 4);
pal->red[lp] = (num & 0x00ff0000) >> 16;
/* NOTE the order of bytes is different from what one would
* expect */
pal->blue[lp] = (num & 0x00ff0000) >> 16;
pal->green[lp] = (num & 0x0000ff00) >> 8;
pal->blue[lp] = (num & 0x000000ff) >> 0;
#if EXCEL_DEBUG > 0
printf ("Colour %d : 0x%8x (%d,%d,%d)\n", lp, num, pal->red[lp], pal->green[lp], pal->blue[lp]);
pal->red[lp] = (num & 0x000000ff) >> 0;
#if EXCEL_DEBUG_COLOR > 2
printf ("Colour %d : 0x%8x (%d,%d,%d)\n", lp,
num, pal->red[lp], pal->green[lp], pal->blue[lp]);
#endif
pal->gnum_cols[lp] = NULL ;
}
......@@ -732,20 +799,89 @@ ms_excel_palette_new (BIFF_QUERY * q)
}
static StyleColor *
ms_excel_palette_get (MS_EXCEL_PALETTE *pal, guint idx)
ms_excel_palette_get (MS_EXCEL_PALETTE *pal, guint idx, StyleColor * contrast)
{
#if EXCEL_DEBUG_COLOR > 4
printf ("Color Index %d\n", idx);
#endif
/* NOTE : not documented but seems close
* If you find a normative reference please forward it.
*
* The color index field seems to use
* 0 = Black
* 8-63 = Palette index 0-55
*
* 127, 64 = contrast ??
* 65 = White ??
*
* Standard combos are
* - fore=64, back=65
* - fore=0-63, back=64
*
* Rethink this when we understand to relationships between
* automatic colors.
*/
/* FIXME FIXME FIXME : this should now be an assert */
if (!pal)
return NULL ;
if (idx == 0)
{
/* Just a guess but maybe this is constant black */
static StyleColor * black = NULL;
if (!black)
black = style_color_new (0, 0, 0);
return black;
}
else if (idx == 64 || idx ==127)
{
/* These seem to be some sort of automatic contract colors */
if (contrast)
{
/* FIXME FIXME FIXME : This is a BIG guess */
/* If the contrast colour closer to black or white based
* on this VERY loose metric. There is more to do.
*/
int const guess =
contrast->color.red +
contrast->color.green +
contrast->color.blue;
#if EXCEL_DEBUG_COLOR > 1
printf ("Contrast : %d", guess);
#endif
if (guess <= (0x7fff + 0x8000 + 0x7fff))
{
#if EXCEL_DEBUG_COLOR > 1
puts("White");
#endif
return style_color_new (0xffff, 0xffff, 0xffff);
}
#if EXCEL_DEBUG_COLOR > 1
puts("Black");
#endif
}
return style_color_new (0, 0, 0);
} else if (idx == 65)
{
/* FIXME FIXME FIXME */
/* These seem to be some sort of automatic contract colors */
return style_color_new (0xffff, 0xffff, 0xffff);
}
idx -= 8;
if (idx < pal->length && idx >= 0)
{
if (pal->gnum_cols[idx] == NULL) {
gushort r, g, b;
r = pal->red[idx]*(65536/128-1);
g = pal->green[idx]*(65536/128-1);
b = pal->blue[idx]*(65536/128-1);
#if EXCEL_DEBUG > 0
printf ("New color in slot %d : RGB= %d,%d,%d -> %d,%d,%d\n",
idx, pal->red[idx], pal->green[idx], pal->blue[idx],
/* scale 8 bit/color -> 16 bit/color by cloning */
r = (pal->red[idx] << 8) | pal->red[idx];
g = (pal->green[idx] << 8) | pal->green[idx];
b = (pal->blue[idx] << 8) | pal->blue[idx];
#if EXCEL_DEBUG_COLOR > 1
printf ("New color in slot %d : RGB= %d,%d,%d\n",
r, g, b);
#endif
pal->gnum_cols[idx] = style_color_new (r, g, b);
......@@ -753,11 +889,10 @@ ms_excel_palette_get (MS_EXCEL_PALETTE *pal, guint idx)
}
return pal->gnum_cols[idx];
}
else if (idx == 64)
return style_color_new (0, 0, 0);
else
{
return NULL;
}
}
static void
ms_excel_palette_destroy (MS_EXCEL_PALETTE * pal)
......@@ -803,16 +938,15 @@ typedef struct _BIFF_XF_DATA {
} BIFF_XF_DATA;
static void
ms_excel_set_cell_colors (MS_EXCEL_SHEET * sheet, Cell * cell, BIFF_XF_DATA * xf, StyleColor *basefore)
ms_excel_set_cell_colors (MS_EXCEL_SHEET * sheet, Cell * cell,
BIFF_XF_DATA * xf, StyleColor *basefore)
{
MS_EXCEL_PALETTE *p = sheet->wb->palette;
StyleColor *fore, *back;
int col;
if (!p || !xf) {
#if EXCEL_DEBUG > 0
printf ("Internal Error: No palette !\n");
#endif
return;
}
......@@ -821,32 +955,36 @@ ms_excel_set_cell_colors (MS_EXCEL_SHEET * sheet, Cell * cell, BIFF_XF_DATA * xf
style->back_color->color.green, style->back_color->color.blue) ; */
if (!basefore) {
#if EXCEL_DEBUG > 0
printf ("Cell : '%s' : (%d, %d)\n",
#if EXCEL_DEBUG_COLOR > 2
printf ("Cell Color : '%s' : (%d, %d)\n",
cell_name (cell->col->pos, cell->row->pos),
xf->pat_foregnd_col&0x3f, xf->pat_backgnd_col&0x3f);
xf->pat_foregnd_col, xf->pat_backgnd_col);
#endif
fore = ms_excel_palette_get (sheet->wb->palette, xf->pat_foregnd_col&0x3f);
back = ms_excel_palette_get (sheet->wb->palette, xf->pat_backgnd_col&0x3f);
fore = ms_excel_palette_get (sheet->wb->palette,
xf->pat_foregnd_col, NULL);
back = ms_excel_palette_get (sheet->wb->palette,
xf->pat_backgnd_col, fore);
} else {
fore = basefore;
back = ms_excel_palette_get (sheet->wb->palette, xf->pat_foregnd_col&0x3f);
#if EXCEL_DEBUG > 0
printf ("Cell : '%s' : (Fontcol, %d)\n",
back = ms_excel_palette_get (sheet->wb->palette,
xf->pat_foregnd_col, NULL);
#if EXCEL_DEBUG_COLOR > 2
printf ("Cell Color : '%s' : (Fontcol, %d)\n",
cell_name (cell->col->pos, cell->row->pos),
xf->pat_foregnd_col);
#endif
}
if (fore && back) {
if (fore == back)
printf ("FIXME: patterns need work\n");
#if EXCEL_DEBUG > 0
printf ("FIXME: patterns need work\n")
#endif
;
else
cell_set_color_from_style (cell, fore, back);
}
else
printf ("Missing color\n");
#if EXCEL_DEBUG > 0
#endif
}
/**
......@@ -861,7 +999,7 @@ ms_excel_set_cell_font (MS_EXCEL_SHEET * sheet, Cell * cell, BIFF_XF_DATA * xf)
StyleColor *col;
if (!fd) {
printf ("Unknown fount idx %d\n", xf->font_idx);
printf ("Unknown font idx %d\n", xf->font_idx);
return NULL ;
}
g_assert (fd->index != 4);
......@@ -873,7 +1011,7 @@ ms_excel_set_cell_font (MS_EXCEL_SHEET * sheet, Cell * cell, BIFF_XF_DATA * xf)
cell_set_font_from_style (cell, fd->style_font);
else
printf ("Duff StyleFont\n") ;
return ms_excel_palette_get (sheet->wb->palette, fd->color_idx);
return ms_excel_palette_get (sheet->wb->palette, fd->color_idx, NULL);
}
static void
......@@ -923,7 +1061,9 @@ ms_excel_set_cell_xf (MS_EXCEL_SHEET * sheet, Cell * cell, guint16 xfidx)
int lp;
StyleColor *tmp[4];
for (lp=0;lp<4;lp++)
tmp[lp] = ms_excel_palette_get (sheet->wb->palette, xf->border_color[lp]) ;
tmp[lp] = ms_excel_palette_get (sheet->wb->palette,
xf->border_color[lp],
NULL);
cell_set_border (cell, xf->border_type, tmp);
}
......@@ -1146,6 +1286,11 @@ biff_xf_data_new (MS_EXCEL_WORKBOOK *wb, BIFF_QUERY * q, eBiff_version ver)
data = BIFF_GETWORD (q->data + 18);
xf->pat_foregnd_col = (data & 0x007f);
xf->pat_backgnd_col = (data & 0x3f80) >> 7;
#if EXCEL_DEBUG_COLOR > 2
printf("Color f=%x b=%x\n",
xf->pat_foregnd_col,
xf->pat_backgnd_col);
#endif
} else { /* Biff 7 */
data = BIFF_GETWORD (q->data + 8);
xf->pat_foregnd_col = (data & 0x007f);
......@@ -1351,7 +1496,7 @@ ms_excel_workbook_new (void)
(GCompareFunc)biff_guint16_equal) ;
ans->name_data = g_hash_table_new ((GHashFunc)biff_guint16_hash,
(GCompareFunc)biff_guint16_equal) ;
ans->palette = NULL;
ans->palette = ms_excel_default_palette ();
ans->global_strings = NULL;
ans->global_string_max = 0;
ans->read_drawing_group = 0;
......@@ -1429,7 +1574,7 @@ ms_excel_workbook_destroy (MS_EXCEL_WORKBOOK * wb)
wb) ;
g_hash_table_destroy (wb->name_data) ;
if (wb->palette)
if (wb->palette && wb->palette != ms_excel_default_palette ())
ms_excel_palette_destroy (wb->palette);
if (wb->extern_sheets)
......@@ -2330,7 +2475,6 @@ ms_excel_read_workbook (MS_OLE * file)
biff_boundsheet_data_new (wb, q, ver->version);
break;
case BIFF_PALETTE:
printf ("READ PALETTE\n");
wb->palette = ms_excel_palette_new (q);
break;
case BIFF_FONT: /* see S59D8C.HTM */
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment