Index: src/print-cell.c
===================================================================
RCS file: /cvs/gnome/gnumeric/src/print-cell.c,v
retrieving revision 1.118
diff -u -r1.118 print-cell.c
--- src/print-cell.c	23 Jun 2004 02:42:04 -0000	1.118
+++ src/print-cell.c	19 Sep 2004 17:18:44 -0000
@@ -35,153 +35,9 @@
 #define MERGE_DEBUG(range, str)
 #endif
 
-static inline void
-print_hline (GnomePrintContext *context,
-	     float x1, float x2, float y)
-{
-	gnome_print_moveto (context, x1, y);
-	gnome_print_lineto (context, x2, y);
-	gnome_print_stroke (context);
-}
-
 /***********************************************************/
 
 /*
- * WARNING : This code is an almost exact duplicate of
- *          cell-draw.c
- * Try to keep it that way.
- */
-
-static inline void
-print_text (GnomePrintContext *context,
-	    double x, double text_base, char const *text, double len_pts,
-	    double const * const line_offset, int num_lines)
-{
-	gnome_print_moveto (context, x, text_base);
-	gnome_print_show (context, text);
-
-	/* FIXME how to handle small fonts ?
-	 * the text_base should be at least 2 pixels above the bottom */
-	while (--num_lines >= 0) {
-		double y = text_base - line_offset[num_lines];
-		gnome_print_newpath (context);
-		gnome_print_setlinewidth (context, 0);
-		print_hline (context, x, x+len_pts, y);
-	}
-}
-
-static char const hashes[] =
-"################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################";
-
-static void
-print_overflow (GnomePrintContext *context, GnomeFont *font,
-		double x1, double text_base, double width,
-		double const * const line_offset, int num_lines)
-{
-	double const len = gnome_font_get_width_utf8_sized (font, "##########", 10) / 10;
-	unsigned count = 1;
-
-	if (len != 0)
-		count = width / len;
-	if (count == 0)
-		count = 1;
-	else if (count >= sizeof (hashes))
-		count = sizeof (hashes) - 1;
-	x1 += (width - count*len) / 2; /* Center */
-	print_text (context, x1, text_base, hashes + sizeof (hashes) - count - 1,
-		    count*len, line_offset, num_lines);
-}
-
-static GList *
-cell_split_text_no_wrap (char const *text)
-{
-	GList *lines = NULL;
-	gchar const *next = text;
-
-	while (next != NULL) {
-		gchar const *this = next;
-		gchar *str;
-
-		next = g_utf8_strchr (next, -1, '\n');
-		if (next == NULL)
-			str = g_strdup (this);
-		else {
-			str = g_strndup (this, next - this);
-			next++;
-		}
-		lines = g_list_append (lines, str);
-	}
-	return lines;
-}
-
-static GList *
-cell_split_text (GnomeFont *font, char const *text, int const width)
-{
-	char const *p, *next, *line_begin;
-	char const *first_whitespace = NULL;
-	char const *last_whitespace = NULL;
-	gboolean prev_was_space = FALSE;
-	GList *list = NULL;
-	double used = 0., used_last_space = 0.;
-	double len_current;
-
-	for (line_begin = p = text; *p; p = next) {
-		next = g_utf8_next_char (p);
-		len_current = gnome_font_get_width_utf8_sized (font, p, next - p);
-
-		/* Wrap if there is an embeded newline, or we have overflowed */
-		if (*p == '\n' || used + len_current > width) {
-			char const *begin = line_begin;
-			int len;
-
-			if (*p == '\n') {
-				/* start after newline, preserve whitespace */
-				line_begin = p+1;
-				len = p - begin;
-				used = 0.;
-			} else if (last_whitespace != NULL) {
-				/* Split at the run of whitespace */
-				line_begin = last_whitespace + 1;
-				len = first_whitespace - begin;
-				used = len_current + used - used_last_space;
-			} else {
-				/* Split before the current character */
-				line_begin = p; /* next line starts here */
-				len = p - begin;
-				used = len_current;
-			}
-
-			list = g_list_append (list, g_strndup (begin, len));
-			first_whitespace = last_whitespace = NULL;
-			prev_was_space = FALSE;
-			continue;
-		}
-
-		used += len_current;
-		if (*p == '-') {
-			used_last_space = used;
-			last_whitespace = p;
-			first_whitespace = p+1;
-			prev_was_space = TRUE;
-		} else if (g_unichar_isspace (g_utf8_get_char (p))) {
-			used_last_space = used;
-			last_whitespace = p;
-			if (!prev_was_space)
-				first_whitespace = p;
-			prev_was_space = TRUE;
-		} else
-			prev_was_space = FALSE;
-	}
-
-	/* Catch the final bit that did not wrap */
-	if (*line_begin)
-		list = g_list_append (list,
-				      g_strndup (line_begin, p - line_begin));
-
-	return list;
-}
-
-/*
  * print_make_rectangle_path
  * @pc      print context
  * @left    left side x coordinate
@@ -220,279 +76,30 @@
 print_cell (GnmCell const *cell, GnmStyle const *mstyle, GnomePrintContext *context,
 	    double x1, double y1, double width, double height, double h_center)
 {
-	Sheet const * const sheet = cell->base.sheet;
-	GnmFont *style_font = mstyle_get_font (mstyle, sheet->context, 1.0);
-	GnomeFont *print_font = style_font->gnome_print_font;
-	double const font_descent = gnome_font_get_descender (print_font);
-	double const font_ascent = gnome_font_get_ascender (print_font);
-	double rect_x, rect_width, rect_y, rect_height;
-
-	ColRowInfo const * const ci = cell->col_info; /* DEPRECATED */
-	ColRowInfo const * const ri = cell->row_info; /* DEPRECATED */
-	double text_base;
-	double font_height;
-	StyleHAlignFlags halign;
-	StyleVAlignFlags valign;
-	int num_lines = 0;
-	double line_offset [3]; /* There are up to 3 lines, double underlined strikethroughs */
-	char const *text;
-	const PangoColor *fore;
-	double cell_width_pts, indent = 0.;
-
-	gboolean is_multi_line_text;
-
-	/* Don't print zeros if they should be ignored. */
-	if (sheet && sheet->hide_zero && cell_is_zero (cell) &&
-	    (!sheet->display_formulas || !cell_has_expr (cell)))
-		return;
-
-	if (cell->rendered_value == NULL) {
-		g_warning ("Serious cell error at '%s'", cell_name (cell));
-		/* This can occur when eg. a plugin function fires up a dialog */
-		return;
-	}
-
-	text = rendered_value_get_text (cell->rendered_value);
-	fore = cell_get_render_color (cell);
-	g_return_if_fail (fore != NULL); /* Be extra careful */
-
-	/* Get the sizes exclusive of margins and grids */
-	/* FIXME : all callers will eventually pass in their cell size */
-	if (width < 0) /* DEPRECATED */
-		width  = ci->size_pts - (ci->margin_b + ci->margin_a + 1.);
-	if (height < 0) /* DEPRECATED */
-		height = ri->size_pts - (ri->margin_b + ri->margin_a + 1.);
-
-	/* This rectangle has the whole area used by this cell
-	 * excluding the surrounding grid lines and margins */
-	if (width <= 0 || height <= 0)
-		return;
-
-	/* This rectangle has the whole area used by this cell
-	 * excluding the surrounding grid lines and margins */
-	rect_x = x1 + 1 + ci->margin_a;
-	rect_y = y1 - 1 - ri->margin_a;
-	rect_width = width + 1;
-	rect_height = height + 1;
-
-	font_height = style_font->size_pts;
-	valign = mstyle_get_align_v (mstyle);
-
-	switch (valign) {
-	default:
-		g_warning ("Unhandled cell vertical alignment.");
-
-	case VALIGN_JUSTIFY:
-	case VALIGN_TOP:
-		/*
-		 * rect.y == first pixel past margin
-		 * add font ascent
-		 */
-		text_base = rect_y - font_ascent;
-		break;
-
-	case VALIGN_CENTER:
-		text_base = rect_y - font_ascent -
-		    (height - font_height) / 2;
-		break;
-
-	case VALIGN_BOTTOM:
-		/*
-		 * rect.y == first pixel past margin
-		 * add height == first pixel in lower margin
-		 * subtract font descent (sign of descent seems reversed for
-		 *	 gnome-print-2.0)
-		 */
-		text_base = rect_y - height - font_descent;
-		break;
-	}
-
-	/* Do not allow text to impinge upon the grid lines or margins
-	 * FIXME : Should use margins from spaninfo->left and spaninfo->right
-	 *
-	 * NOTE : postscript clip paths exclude near the border, gdk includes it.
-	 */
-	gnome_print_gsave (context);
-	print_make_rectangle_path (context,
-				   rect_x - 1.,
-				   rect_y - rect_height - 1.,
-				   rect_x + rect_width + 1.,
-				   rect_y + 1.);
-	gnome_print_clip (context);
+	/* For use with se cell_calc_layout from cell-draw */
+	GdkRectangle  rect;
+	GdkColor *res_color; /* FIXME a pointer ? */
+	gint res_x;
+	gint res_y;
+	PangoLayout *res_layout;
+	
+	/* For use with se cell_calc_layout from cell-draw */
+	cell_calc_layout (cell, x1,y1, width, height, h_center,
+		  &rect, &res_color, &res_x, &res_y, &res_layout);
 
 	/* Set the font colour */
 	gnome_print_setrgbcolor (context,
-				 fore->red   / (double) 0xffff,
-				 fore->green / (double) 0xffff,
-				 fore->blue  / (double) 0xffff);
-
-	/* Handle underlining and strikethrough */
-	switch (mstyle_get_font_uline (mstyle)) {
-	case UNDERLINE_SINGLE : num_lines = 1;
-				line_offset[0] = 1.;
-				break;
-
-	case UNDERLINE_DOUBLE : num_lines = 2;
-				line_offset[0] = 0.;
-				line_offset[1] = 2.;
-
-	default :
-				break;
-	};
-	if (mstyle_get_font_strike (mstyle))
-		line_offset[num_lines++] = font_ascent/-2;
-
-	/* FIXME : This will be wrong for JUSTIFIED halignments */
-	halign = style_default_halign (mstyle, cell);
-	cell_width_pts = gnome_font_get_width_utf8 (print_font, text);
-	if (halign == HALIGN_LEFT || halign == HALIGN_RIGHT) {
-		/* 2*width seems to be pretty close to XL's notion */
-		/* FIXME: Why use digit?  */
-		indent = mstyle_get_indent (mstyle) *
-			2. * style_font->approx_width.pts.digit;
-	}
-
-	/* if a number overflows, do special drawing */
-	if ((cell_width_pts + indent) > width && cell_is_number (cell) &&
-	    sheet && !sheet->display_formulas) {
-		print_overflow (context, print_font, rect_x,
-				text_base, width, line_offset, num_lines);
-		style_font_unref (style_font);
-		gnome_print_grestore (context);
-		return;
-	}
-
-	if (halign == HALIGN_CENTER_ACROSS_SELECTION || h_center <= 0.)
-		h_center = width / 2.;
-
-	is_multi_line_text = mstyle_get_wrap_text (mstyle) 
-		|| (NULL != g_utf8_strchr (text, -1, '\n')); 
+				 res_color->red   / (double) 0xffff,
+				 res_color->green / (double) 0xffff,
+				 res_color->blue  / (double) 0xffff);
+	
+	/* print the text */
+    gnome_print_moveto (context, res_x, res_y);
+    gnome_print_pango_layout (context, res_layout);
+	
+	/* unref layout */
+    g_object_unref (res_layout);
 	
-	if (halign != HALIGN_JUSTIFY && valign != VALIGN_JUSTIFY &&
-	    !is_multi_line_text) {
-		double x, total, len = cell_width_pts;
-
-		switch (halign) {
-		case HALIGN_FILL: /* fall through */
-		case HALIGN_LEFT:
-			x = rect_x + indent;
-			break;
-
-		case HALIGN_RIGHT:
-			x = rect_x + rect_width - 1 - cell_width_pts -indent;
-			break;
-
-		case HALIGN_CENTER:
-		case HALIGN_CENTER_ACROSS_SELECTION:
-			x = rect_x + h_center - cell_width_pts / 2;
-			break;
-
-		default:
-			g_warning ("Single-line justification style not supported.");
-			x = rect_x;
-			break;
-		}
-
-		gnome_print_setfont (context, print_font);
-		total = len; /* don't include partial copies after the first */
-		do {
-			print_text (context, x, text_base, text, len,
-				    line_offset, num_lines);
-
-			x += len;
-			total += len;
-		} while (halign == HALIGN_FILL && total < rect_width && len > 0);
-	} else {
-		GList *lines, *l;
-		int line_count;
-		double x, y_offset, inter_space;
-
-		lines = mstyle_get_wrap_text (mstyle) ? 
-			cell_split_text (print_font, text, width) :
-			cell_split_text_no_wrap (text);
-		line_count = g_list_length (lines);
-
-		switch (valign) {
-		case VALIGN_TOP:
-			y_offset = 0.;
-			inter_space = font_height;
-			break;
-
-		case VALIGN_CENTER:
-			y_offset = ((height -
-				       (line_count * font_height)) / 2);
-			inter_space = font_height;
-			break;
-
-		case VALIGN_JUSTIFY:
-			if (line_count > 1) {
-				y_offset = 0;
-				inter_space = font_height +
-					(height - (line_count * font_height))
-					/ (line_count - 1);
-
-				/* lines should not overlap */
-				if (inter_space < font_height)
-					inter_space = font_height;
-				break;
-			}
-			/* Else, we become a VALIGN_BOTTOM line */
-
-		case VALIGN_BOTTOM:
-			y_offset = (height - (line_count * font_height));
-			inter_space = font_height;
-			break;
-
-		default:
-			g_warning ("Unhandled cell vertical alignment.");
-			y_offset = 0;
-			inter_space = font_height;
-		}
-
-		gnome_print_setfont (context, print_font);
-
-		y_offset += font_height - 1;
-		for (l = lines; l; l = l->next) {
-			char const * const str = l->data;
-			double len = 0.;
-
-			switch (halign) {
-			default:
-				g_warning ("Multi-line justification style not supported.");
-			case HALIGN_JUSTIFY:
-				/* fall through */
-			case HALIGN_LEFT:
-				x = rect_x + indent;
-
-				/* Be cheap, only calculate the width of the
-				 * string if we need to. */
-				if (num_lines > 0)
-					len = gnome_font_get_width_utf8 (print_font, str);
-				break;
-
-			case HALIGN_RIGHT:
-				len = gnome_font_get_width_utf8 (print_font, str);
-				x = rect_x + rect_width - 1 - len - indent;
-				break;
-
-			case HALIGN_CENTER:
-			case HALIGN_CENTER_ACROSS_SELECTION:
-				len = gnome_font_get_width_utf8 (print_font, str);
-				x = rect_x + h_center - len / 2;
-			}
-
-			print_text (context,
-				    x, y1 - y_offset, str,
-				    len, line_offset, num_lines);
-
-			y_offset += inter_space;
-
-			g_free (l->data);
-		}
-		g_list_free (lines);
-	}
-	style_font_unref (style_font);
-
 	gnome_print_grestore (context);
 }
 
