From: Jim Evins Date: Mon, 12 Jul 2004 01:09:00 +0000 (+0000) Subject: 2004-07-11 Jim Evins X-Git-Tag: glabels-2_3_0~465 X-Git-Url: https://git.sur5r.net/?a=commitdiff_plain;h=00ddb9df4fe9f2ae732a5561ea7b8867aa8433d4;p=glabels 2004-07-11 Jim Evins * data/glabels-2.0.dtd: * src/label-text.c: (gl_label_text_instance_init), (copy), (gl_label_text_set_auto_shrink), (gl_label_text_get_auto_shrink): * src/label-text.h: * src/object-editor-private.h: * src/object-editor-text-page.c: (gl_object_editor_prepare_text_page), (gl_object_editor_set_text_auto_shrink), (gl_object_editor_get_text_auto_shrink): * src/object-editor.c: (gl_object_editor_set_key_names): * src/object-editor.glade: * src/object-editor.h: * src/print.c: (draw_text_object): * src/view-text.c: (update_object_from_editor_cb), (update_editor_from_object_cb): * src/xml-label.c: (xml_parse_object_text), (xml_create_object_text): Added option to allow document merge to auto shrink text size to fit within the text box. The code in print.c that actually shrinks the text is loosely based on a patch submitted by Mark Harrison . This addresses feature request #661421. git-svn-id: https://glabels.svn.sourceforge.net/svnroot/glabels/trunk@432 f5e0f49d-192f-0410-a22d-a8d8700d0965 --- diff --git a/glabels2/ChangeLog b/glabels2/ChangeLog index 70e98169..fd29e957 100644 --- a/glabels2/ChangeLog +++ b/glabels2/ChangeLog @@ -1,3 +1,28 @@ +2004-07-11 Jim Evins + + * data/glabels-2.0.dtd: + * src/label-text.c: (gl_label_text_instance_init), (copy), + (gl_label_text_set_auto_shrink), (gl_label_text_get_auto_shrink): + * src/label-text.h: + * src/object-editor-private.h: + * src/object-editor-text-page.c: + (gl_object_editor_prepare_text_page), + (gl_object_editor_set_text_auto_shrink), + (gl_object_editor_get_text_auto_shrink): + * src/object-editor.c: (gl_object_editor_set_key_names): + * src/object-editor.glade: + * src/object-editor.h: + * src/print.c: (draw_text_object): + * src/view-text.c: (update_object_from_editor_cb), + (update_editor_from_object_cb): + * src/xml-label.c: (xml_parse_object_text), + (xml_create_object_text): + Added option to allow document merge to auto shrink text size to fit + within the text box. The code in print.c that actually shrinks the + text is loosely based on a patch submitted by Mark Harrison + . This addresses feature request + #661421. + 2004-07-11 Jim Evins * data/glabels-2.0.dtd: diff --git a/glabels2/data/glabels-2.0.dtd b/glabels2/data/glabels-2.0.dtd index dcd0b35d..556fb35e 100644 --- a/glabels2/data/glabels-2.0.dtd +++ b/glabels2/data/glabels-2.0.dtd @@ -217,6 +217,7 @@ %position_attrs; %size_attrs; justify %JUSTIFY_TYPE; #REQUIRED + auto_shrink %BOOLEAN_TYPE; #IMPLIED %affine_attrs; %shadow_attrs; > diff --git a/glabels2/src/label-text.c b/glabels2/src/label-text.c index 70ae32a7..82e3c1eb 100644 --- a/glabels2/src/label-text.c +++ b/glabels2/src/label-text.c @@ -33,13 +33,14 @@ /* Private macros and constants. */ /*========================================================*/ -#define DEFAULT_FONT_FAMILY "Sans" -#define DEFAULT_FONT_SIZE 14.0 -#define DEFAULT_FONT_WEIGHT GNOME_FONT_BOOK -#define DEFAULT_FONT_ITALIC_FLAG FALSE -#define DEFAULT_JUST GTK_JUSTIFY_LEFT -#define DEFAULT_COLOR GNOME_CANVAS_COLOR (0,0,0) +#define DEFAULT_FONT_FAMILY "Sans" +#define DEFAULT_FONT_SIZE 14.0 +#define DEFAULT_FONT_WEIGHT GNOME_FONT_BOOK +#define DEFAULT_FONT_ITALIC_FLAG FALSE +#define DEFAULT_JUST GTK_JUSTIFY_LEFT +#define DEFAULT_COLOR GNOME_CANVAS_COLOR (0,0,0) #define DEFAULT_TEXT_LINE_SPACING 1.0 +#define DEFAULT_AUTO_SHRINK FALSE /*========================================================*/ /* Private types. */ @@ -56,6 +57,7 @@ struct _glLabelTextPrivate { GtkJustification just; guint color; gdouble line_spacing; + gboolean auto_shrink; }; /*========================================================*/ @@ -194,6 +196,7 @@ gl_label_text_instance_init (glLabelText *ltext) ltext->private->just = DEFAULT_JUST; ltext->private->color = DEFAULT_COLOR; ltext->private->line_spacing = DEFAULT_TEXT_LINE_SPACING; + ltext->private->auto_shrink = DEFAULT_AUTO_SHRINK; g_signal_connect (G_OBJECT(ltext->private->buffer), "changed", G_CALLBACK(buffer_changed_cb), ltext); @@ -256,6 +259,7 @@ copy (glLabelObject *dst_object, new_ltext->private->color = ltext->private->color; new_ltext->private->just = ltext->private->just; new_ltext->private->line_spacing = ltext->private->line_spacing; + new_ltext->private->auto_shrink = ltext->private->auto_shrink; gl_text_node_lines_free (&lines); @@ -696,3 +700,37 @@ get_text_color (glLabelObject *object) return ltext->private->color; } +/*****************************************************************************/ +/* Set auto shrink flag. */ +/*****************************************************************************/ +void +gl_label_text_set_auto_shrink (glLabelText *ltext, + gboolean auto_shrink) +{ + gl_debug (DEBUG_LABEL, "BEGIN"); + + g_return_if_fail (ltext && GL_IS_LABEL_TEXT (ltext)); + + if (ltext->private->auto_shrink != auto_shrink) { + + ltext->private->auto_shrink = auto_shrink; + gl_label_object_emit_changed (GL_LABEL_OBJECT(ltext)); + + } + + gl_debug (DEBUG_LABEL, "END"); +} + +/*****************************************************************************/ +/* Query auto shrink flag. +/*****************************************************************************/ +gboolean +gl_label_text_get_auto_shrink (glLabelText *ltext) +{ + gl_debug (DEBUG_LABEL, ""); + + g_return_val_if_fail (ltext && GL_IS_LABEL_TEXT (ltext), 0); + + return ltext->private->auto_shrink; +} + diff --git a/glabels2/src/label-text.h b/glabels2/src/label-text.h index 99c60728..abf6fdba 100644 --- a/glabels2/src/label-text.h +++ b/glabels2/src/label-text.h @@ -66,6 +66,11 @@ void gl_label_text_get_box (glLabelText *ltext, gdouble *w, gdouble *h); +void gl_label_text_set_auto_shrink (glLabelText *ltext, + gboolean auto_shrink); +gboolean gl_label_text_get_auto_shrink (glLabelText *ltext); + + G_END_DECLS #endif /* __LABEL_TEXT_H__ */ diff --git a/glabels2/src/object-editor-private.h b/glabels2/src/object-editor-private.h index 2bc8c3e2..77d01bfa 100644 --- a/glabels2/src/object-editor-private.h +++ b/glabels2/src/object-editor-private.h @@ -97,6 +97,7 @@ struct _glObjectEditorPrivate { GtkWidget *text_center_toggle; GtkWidget *text_right_toggle; GtkWidget *text_line_spacing_spin; + GtkWidget *text_auto_shrink_check; GtkWidget *edit_page_vbox; GtkWidget *edit_text_view; diff --git a/glabels2/src/object-editor-text-page.c b/glabels2/src/object-editor-text-page.c index f22b1cd6..db642884 100644 --- a/glabels2/src/object-editor-text-page.c +++ b/glabels2/src/object-editor-text-page.c @@ -88,6 +88,8 @@ gl_object_editor_prepare_text_page (glObjectEditor *editor) glade_xml_get_widget (editor->priv->gui, "text_right_toggle"); editor->priv->text_line_spacing_spin = glade_xml_get_widget (editor->priv->gui, "text_line_spacing_spin"); + editor->priv->text_auto_shrink_check = + glade_xml_get_widget (editor->priv->gui, "text_auto_shrink_check"); /* Load family names */ family_names = gnome_font_family_list (); @@ -153,6 +155,11 @@ gl_object_editor_prepare_text_page (glObjectEditor *editor) G_CALLBACK (gl_object_editor_changed_cb), G_OBJECT (editor)); + g_signal_connect_swapped (G_OBJECT (editor->priv->text_auto_shrink_check), + "toggled", + G_CALLBACK (gl_object_editor_changed_cb), + G_OBJECT (editor)); + gl_debug (DEBUG_EDITOR, "END"); } @@ -546,3 +553,43 @@ gl_object_editor_get_text_line_spacing (glObjectEditor *editor) return text_line_spacing; } +/*****************************************************************************/ +/* Set auto shrink checkbox. */ +/*****************************************************************************/ +void +gl_object_editor_set_text_auto_shrink (glObjectEditor *editor, + gboolean auto_shrink) +{ + gl_debug (DEBUG_EDITOR, "START"); + + g_signal_handlers_block_by_func (G_OBJECT(editor->priv->text_auto_shrink_check), + gl_object_editor_changed_cb, + editor); + + gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (editor->priv->text_auto_shrink_check), + auto_shrink); + + g_signal_handlers_unblock_by_func (G_OBJECT(editor->priv->text_auto_shrink_check), + gl_object_editor_changed_cb, + editor); + + gl_debug (DEBUG_EDITOR, "END"); +} + +/*****************************************************************************/ +/* Query auto shrink checkbox. */ +/*****************************************************************************/ +gboolean gl_object_editor_get_text_auto_shrink (glObjectEditor *editor) +{ + gboolean auto_shrink; + + gl_debug (DEBUG_EDITOR, "START"); + + auto_shrink = + gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (editor->priv->text_auto_shrink_check)); + + gl_debug (DEBUG_EDITOR, "END"); + + return auto_shrink; +} + diff --git a/glabels2/src/object-editor.c b/glabels2/src/object-editor.c index 27412729..0de6051a 100644 --- a/glabels2/src/object-editor.c +++ b/glabels2/src/object-editor.c @@ -377,6 +377,11 @@ gl_object_editor_set_key_names (glObjectEditor *editor, gtk_widget_set_sensitive (editor->priv->edit_key_combo, merge != NULL); } + if (editor->priv->text_auto_shrink_check) { + gtk_widget_set_sensitive (editor->priv->text_auto_shrink_check, + merge != NULL); + } + if (editor->priv->edit_insert_field_button) { gtk_widget_set_sensitive (editor->priv->edit_insert_field_button, merge != NULL); diff --git a/glabels2/src/object-editor.glade b/glabels2/src/object-editor.glade index c85236c1..f9e019d7 100644 --- a/glabels2/src/object-editor.glade +++ b/glabels2/src/object-editor.glade @@ -289,7 +289,7 @@ True - 6 + 7 2 False 12 @@ -758,6 +758,27 @@ fill + + + + True + True + Allow merge to automatically shrink text + True + GTK_RELIEF_NORMAL + False + False + True + + + 0 + 2 + 6 + 7 + fill + + + 0 diff --git a/glabels2/src/object-editor.h b/glabels2/src/object-editor.h index ff47e423..f450c155 100644 --- a/glabels2/src/object-editor.h +++ b/glabels2/src/object-editor.h @@ -210,6 +210,11 @@ void gl_object_editor_set_text_color (glObjectEditor *editor, guint gl_object_editor_get_text_color (glObjectEditor *editor); +void gl_object_editor_set_text_auto_shrink (glObjectEditor *editor, + gboolean auto_shrink); + +gboolean gl_object_editor_get_text_auto_shrink (glObjectEditor *editor); + /* * Edit Text Page diff --git a/glabels2/src/print.c b/glabels2/src/print.c index 73bf4034..83b99711 100644 --- a/glabels2/src/print.c +++ b/glabels2/src/print.c @@ -721,22 +721,23 @@ draw_text_object (PrintInfo *pi, glLabelText *object, glMergeRecord *record) { - GnomeFont *font; - gchar **line; - gint i; - gdouble x_offset, y_offset, w, object_w, object_h; - gchar *text; - GList *lines; - gchar *font_family; - gdouble font_size; - GnomeFontWeight font_weight; - gboolean font_italic_flag; - guint color; + GnomeFont *font; + gchar **line; + gint i; + gdouble x_offset, y_offset, w, object_w, object_h; + gchar *text; + GList *lines; + gchar *font_family; + gdouble font_size; + GnomeFontWeight font_weight; + gboolean font_italic_flag; + guint color; GtkJustification just; - GnomeGlyphList *glyphlist; - ArtDRect bbox; - gdouble affine[6]; - gdouble text_line_spacing; + gboolean auto_shrink; + GnomeGlyphList *glyphlist; + ArtDRect bbox; + gdouble affine[6]; + gdouble text_line_spacing; gl_debug (DEBUG_PRINT, "START"); @@ -749,7 +750,53 @@ draw_text_object (PrintInfo *pi, font_italic_flag = gl_label_object_get_font_italic_flag (GL_LABEL_OBJECT(object)); color = gl_label_object_get_text_color (GL_LABEL_OBJECT(object)); just = gl_label_object_get_text_alignment (GL_LABEL_OBJECT(object)); - text_line_spacing = gl_label_object_get_text_line_spacing (GL_LABEL_OBJECT(object)); + text_line_spacing = + gl_label_object_get_text_line_spacing (GL_LABEL_OBJECT(object)); + auto_shrink = gl_label_text_get_auto_shrink (object); + + text = gl_text_node_lines_expand (lines, record); + line = g_strsplit (text, "\n", -1); + g_free (text); + + art_affine_identity (affine); + + if (record && auto_shrink) { + /* auto shrink text size to keep within text box limits. */ + for (i = 0; line[i] != NULL; i++) { + + font = gnome_font_find_closest_from_weight_slant (font_family, + font_weight, + font_italic_flag, + font_size); + glyphlist = gnome_glyphlist_from_text_dumb (font, + color, + 0.0, 0.0, + line[i]); + gnome_glyphlist_bbox (glyphlist, affine, 0, &bbox); + w = bbox.x1; + + /* If width is too large, iteratively shrink font_size until this + line fits the width, or until the font size is ridiculously + small. */ + while ( (w > object_w) && (font_size >= 1.0) ) { + + font_size -= 0.5; + + font = gnome_font_find_closest_from_weight_slant ( + font_family, + font_weight, + font_italic_flag, + font_size); + glyphlist = gnome_glyphlist_from_text_dumb (font, + color, + 0.0, 0.0, + line[i]); + gnome_glyphlist_bbox (glyphlist, affine, 0, &bbox); + w = bbox.x1; + } + } + + } font = gnome_font_find_closest_from_weight_slant ( font_family, @@ -764,12 +811,6 @@ draw_text_object (PrintInfo *pi, GL_COLOR_F_BLUE (color)); gnome_print_setopacity (pi->pc, GL_COLOR_F_ALPHA (color)); - text = gl_text_node_lines_expand (lines, record); - line = g_strsplit (text, "\n", -1); - g_free (text); - - art_affine_identity (affine); - for (i = 0; line[i] != NULL; i++) { glyphlist = gnome_glyphlist_from_text_dumb (font, color, diff --git a/glabels2/src/view-text.c b/glabels2/src/view-text.c index a4e49257..f129033e 100644 --- a/glabels2/src/view-text.c +++ b/glabels2/src/view-text.c @@ -312,6 +312,7 @@ update_object_from_editor_cb (glObjectEditor *editor, guint color; GtkJustification just; gdouble text_line_spacing; + gboolean auto_shrink; gl_debug (DEBUG_VIEW, "START"); @@ -330,6 +331,7 @@ update_object_from_editor_cb (glObjectEditor *editor, color = gl_object_editor_get_text_color (editor); just = gl_object_editor_get_text_alignment (editor); text_line_spacing = (gdouble) gl_object_editor_get_text_line_spacing (editor); + auto_shrink = gl_object_editor_get_text_auto_shrink (editor); gl_label_object_set_position (object, x, y); gl_label_object_set_font_family (object, font_family); @@ -339,6 +341,7 @@ update_object_from_editor_cb (glObjectEditor *editor, gl_label_object_set_text_color (object, color); gl_label_object_set_text_alignment (object, just); gl_label_object_set_text_line_spacing (object, text_line_spacing); + gl_label_text_set_auto_shrink (GL_LABEL_TEXT (object), auto_shrink); g_free (font_family); @@ -399,6 +402,7 @@ update_editor_from_object_cb (glLabelObject *object, guint color; GtkJustification just; gdouble text_line_spacing; + gboolean auto_shrink; gl_debug (DEBUG_VIEW, "START"); @@ -412,6 +416,7 @@ update_editor_from_object_cb (glLabelObject *object, color = gl_label_object_get_text_color (object); just = gl_label_object_get_text_alignment (object); text_line_spacing = gl_label_object_get_text_line_spacing (object); + auto_shrink = gl_label_text_get_auto_shrink (GL_LABEL_TEXT (object)); gl_object_editor_set_font_family (editor, font_family); gl_object_editor_set_font_size (editor, font_size); @@ -420,6 +425,7 @@ update_editor_from_object_cb (glLabelObject *object, gl_object_editor_set_text_color (editor, color); gl_object_editor_set_text_alignment (editor, just); gl_object_editor_set_text_line_spacing (editor, text_line_spacing); + gl_object_editor_set_text_auto_shrink (editor, auto_shrink); g_free (font_family); diff --git a/glabels2/src/xml-label.c b/glabels2/src/xml-label.c index 067fe41e..e18a4295 100644 --- a/glabels2/src/xml-label.c +++ b/glabels2/src/xml-label.c @@ -411,6 +411,7 @@ xml_parse_object_text (xmlNodePtr node, gdouble w, h; gchar *string; GtkJustification just; + gboolean auto_shrink; gdouble affine[6]; xmlNodePtr child; @@ -434,6 +435,10 @@ xml_parse_object_text (xmlNodePtr node, g_free (string); gl_label_object_set_text_alignment (GL_LABEL_OBJECT(object), just); + /* auto_shrink attr */ + auto_shrink = gl_xml_get_prop_boolean (node, "auto_shrink", FALSE); + gl_label_text_set_auto_shrink (GL_LABEL_TEXT(object), auto_shrink); + /* affine attrs */ affine[0] = gl_xml_get_prop_double (node, "a0", 0.0); affine[1] = gl_xml_get_prop_double (node, "a1", 0.0); @@ -1099,6 +1104,7 @@ xml_create_object_text (xmlNodePtr root, gdouble x, y; gdouble w, h; GtkJustification just; + gboolean auto_shrink; gdouble affine[6]; gl_debug (DEBUG_XML, "START"); @@ -1119,6 +1125,10 @@ xml_create_object_text (xmlNodePtr root, just = gl_label_object_get_text_alignment (object); xmlSetProp (node, "justify", gl_util_just_to_string (just)); + /* auto_shrink attr */ + auto_shrink = gl_label_text_get_auto_shrink (GL_LABEL_TEXT (object)); + gl_xml_set_prop_boolean (node, "auto_shrink", auto_shrink); + /* affine attrs */ gl_label_object_get_affine (object, affine); gl_xml_set_prop_double (node, "a0", affine[0]);