3 * Copyright (C) 2001-2009 Jim Evins <evins@snaught.com>.
5 * This file is part of gLabels.
7 * gLabels is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
12 * gLabels is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with gLabels. If not, see <http://www.gnu.org/licenses/>.
23 #include "label-text.h"
25 #include <glib/gi18n.h>
27 #include <pango/pango.h>
31 #include "font-util.h"
32 #include "font-history.h"
37 /*========================================================*/
38 /* Private macros and constants. */
39 /*========================================================*/
41 #define FONT_SCALE (72.0/96.0)
44 /*========================================================*/
46 /*========================================================*/
48 struct _glLabelTextPrivate {
49 GtkTextTagTable *tag_table;
50 GtkTextBuffer *buffer;
54 PangoWeight font_weight;
55 gboolean font_italic_flag;
57 glColorNode *color_node;
61 gboolean size_changed;
65 gboolean checkpoint_flag;
69 /*========================================================*/
70 /* Private globals. */
71 /*========================================================*/
74 /*========================================================*/
75 /* Private function prototypes. */
76 /*========================================================*/
78 static void gl_label_text_finalize (GObject *object);
80 static void copy (glLabelObject *dst_object,
81 glLabelObject *src_object);
83 static void buffer_begin_user_action_cb (GtkTextBuffer *textbuffer,
86 static void buffer_changed_cb (GtkTextBuffer *textbuffer,
89 static void get_size (glLabelObject *object,
93 static void set_font_family (glLabelObject *object,
94 const gchar *font_family,
97 static void set_font_size (glLabelObject *object,
101 static void set_font_weight (glLabelObject *object,
102 PangoWeight font_weight,
103 gboolean checkpoint);
105 static void set_font_italic_flag (glLabelObject *object,
106 gboolean font_italic_flag,
107 gboolean checkpoint);
109 static void set_text_alignment (glLabelObject *object,
110 PangoAlignment text_alignment,
111 gboolean checkpoint);
113 static void set_text_line_spacing (glLabelObject *object,
114 gdouble text_line_spacing,
115 gboolean checkpoint);
117 static void set_text_color (glLabelObject *object,
118 glColorNode *text_color_node,
119 gboolean checkpoint);
121 static gchar *get_font_family (glLabelObject *object);
123 static gdouble get_font_size (glLabelObject *object);
125 static PangoWeight get_font_weight (glLabelObject *object);
127 static gboolean get_font_italic_flag (glLabelObject *object);
129 static PangoAlignment get_text_alignment (glLabelObject *object);
131 static gdouble get_text_line_spacing (glLabelObject *object);
133 static glColorNode* get_text_color (glLabelObject *object);
135 static void draw_object (glLabelObject *object,
137 gboolean screen_flag,
138 glMergeRecord *record);
140 static void draw_shadow (glLabelObject *object,
142 gboolean screen_flag,
143 glMergeRecord *record);
145 static void draw_text_real (glLabelObject *object,
147 gboolean screen_flag,
148 glMergeRecord *record,
151 static gdouble auto_shrink_font_size (cairo_t *cr,
159 static gboolean object_at (glLabelObject *object,
165 /*****************************************************************************/
166 /* Object infrastructure. */
167 /*****************************************************************************/
168 G_DEFINE_TYPE (glLabelText, gl_label_text, GL_TYPE_LABEL_OBJECT);
171 /*****************************************************************************/
172 /* Class Init Function. */
173 /*****************************************************************************/
175 gl_label_text_class_init (glLabelTextClass *class)
177 GObjectClass *object_class = G_OBJECT_CLASS (class);
178 glLabelObjectClass *label_object_class = GL_LABEL_OBJECT_CLASS (class);
180 gl_label_text_parent_class = g_type_class_peek_parent (class);
182 label_object_class->copy = copy;
184 label_object_class->get_size = get_size;
186 label_object_class->set_font_family = set_font_family;
187 label_object_class->set_font_size = set_font_size;
188 label_object_class->set_font_weight = set_font_weight;
189 label_object_class->set_font_italic_flag = set_font_italic_flag;
190 label_object_class->set_text_alignment = set_text_alignment;
191 label_object_class->set_text_line_spacing = set_text_line_spacing;
192 label_object_class->set_text_color = set_text_color;
193 label_object_class->get_font_family = get_font_family;
194 label_object_class->get_font_size = get_font_size;
195 label_object_class->get_font_weight = get_font_weight;
196 label_object_class->get_font_italic_flag = get_font_italic_flag;
197 label_object_class->get_text_alignment = get_text_alignment;
198 label_object_class->get_text_line_spacing = get_text_line_spacing;
199 label_object_class->get_text_color = get_text_color;
200 label_object_class->draw_object = draw_object;
201 label_object_class->draw_shadow = draw_shadow;
202 label_object_class->object_at = object_at;
204 object_class->finalize = gl_label_text_finalize;
208 /*****************************************************************************/
209 /* Object Instance Init Function. */
210 /*****************************************************************************/
212 gl_label_text_init (glLabelText *ltext)
214 ltext->priv = g_new0 (glLabelTextPrivate, 1);
216 ltext->priv->tag_table = gtk_text_tag_table_new ();
217 ltext->priv->buffer = gtk_text_buffer_new (ltext->priv->tag_table);
219 ltext->priv->size_changed = TRUE;
221 ltext->priv->checkpoint_flag = TRUE;
223 g_signal_connect (G_OBJECT(ltext->priv->buffer), "begin-user-action",
224 G_CALLBACK(buffer_begin_user_action_cb), ltext);
225 g_signal_connect (G_OBJECT(ltext->priv->buffer), "changed",
226 G_CALLBACK(buffer_changed_cb), ltext);
230 /*****************************************************************************/
231 /* Finalize Method. */
232 /*****************************************************************************/
234 gl_label_text_finalize (GObject *object)
236 glLabelText *ltext = GL_LABEL_TEXT (object);
238 g_return_if_fail (object && GL_IS_LABEL_TEXT (object));
240 g_object_unref (ltext->priv->tag_table);
241 g_object_unref (ltext->priv->buffer);
242 g_free (ltext->priv->font_family);
243 gl_color_node_free (&(ltext->priv->color_node));
244 g_free (ltext->priv);
246 G_OBJECT_CLASS (gl_label_text_parent_class)->finalize (object);
250 /*****************************************************************************/
251 /** New Object Generator. */
252 /*****************************************************************************/
254 gl_label_text_new (glLabel *label,
258 glColorNode *color_node;
260 ltext = g_object_new (gl_label_text_get_type(), NULL);
266 gl_label_checkpoint (label, _("Create text object"));
269 color_node = gl_color_node_new_default ();
271 color_node->color = gl_label_get_default_text_color (label);
273 ltext->priv->font_family = gl_label_get_default_font_family (label);
274 ltext->priv->font_size = gl_label_get_default_font_size (label);
275 ltext->priv->font_weight = gl_label_get_default_font_weight (label);
276 ltext->priv->font_italic_flag = gl_label_get_default_font_italic_flag (label);
277 ltext->priv->align = gl_label_get_default_text_alignment (label);
278 ltext->priv->color_node = color_node;
279 ltext->priv->line_spacing = gl_label_get_default_text_line_spacing (label);
281 gl_label_add_object (label, GL_LABEL_OBJECT (ltext));
282 gl_label_object_set_parent (GL_LABEL_OBJECT (ltext), label);
285 return G_OBJECT (ltext);
289 /*****************************************************************************/
290 /* Copy object contents. */
291 /*****************************************************************************/
293 copy (glLabelObject *dst_object,
294 glLabelObject *src_object)
296 glLabelText *ltext = (glLabelText *)src_object;
297 glLabelText *new_ltext = (glLabelText *)dst_object;
299 glColorNode *text_color_node;
301 gl_debug (DEBUG_LABEL, "START");
303 g_return_if_fail (ltext && GL_IS_LABEL_TEXT (ltext));
304 g_return_if_fail (new_ltext && GL_IS_LABEL_TEXT (new_ltext));
306 lines = gl_label_text_get_lines (ltext);
307 text_color_node = get_text_color (src_object);
308 gl_label_text_set_lines (new_ltext, lines, FALSE);
310 new_ltext->priv->font_family = g_strdup (ltext->priv->font_family);
311 new_ltext->priv->font_size = ltext->priv->font_size;
312 new_ltext->priv->font_weight = ltext->priv->font_weight;
313 new_ltext->priv->font_italic_flag = ltext->priv->font_italic_flag;
314 set_text_color (dst_object, text_color_node, FALSE);
315 new_ltext->priv->align = ltext->priv->align;
316 new_ltext->priv->line_spacing = ltext->priv->line_spacing;
317 new_ltext->priv->auto_shrink = ltext->priv->auto_shrink;
319 new_ltext->priv->size_changed = ltext->priv->size_changed;
320 new_ltext->priv->w = ltext->priv->w;
321 new_ltext->priv->h = ltext->priv->h;
323 gl_color_node_free (&text_color_node);
324 gl_text_node_lines_free (&lines);
326 gl_debug (DEBUG_LABEL, "END");
330 /*****************************************************************************/
331 /* Set object params. */
332 /*****************************************************************************/
334 gl_label_text_set_lines (glLabelText *ltext,
340 gl_debug (DEBUG_LABEL, "START");
342 g_return_if_fail (ltext && GL_IS_LABEL_TEXT (ltext));
344 ltext->priv->checkpoint_flag = checkpoint;
346 text = gl_text_node_lines_expand (lines, NULL);
347 gtk_text_buffer_set_text (ltext->priv->buffer, text, -1);
350 ltext->priv->size_changed = TRUE;
352 ltext->priv->checkpoint_flag = TRUE;
354 gl_debug (DEBUG_LABEL, "END");
359 gl_label_text_set_text (glLabelText *ltext,
363 gl_debug (DEBUG_LABEL, "START");
365 g_return_if_fail (ltext && GL_IS_LABEL_TEXT (ltext));
367 ltext->priv->checkpoint_flag = checkpoint;
369 gtk_text_buffer_set_text (ltext->priv->buffer, text, -1);
371 ltext->priv->size_changed = TRUE;
373 ltext->priv->checkpoint_flag = TRUE;
375 gl_debug (DEBUG_LABEL, "END");
379 /*****************************************************************************/
380 /* Get object params. */
381 /*****************************************************************************/
383 gl_label_text_get_buffer (glLabelText *ltext)
385 g_return_val_if_fail (ltext && GL_IS_LABEL_TEXT (ltext), NULL);
387 return ltext->priv->buffer;
392 gl_label_text_get_lines (glLabelText *ltext)
394 GtkTextIter start, end;
398 g_return_val_if_fail (ltext && GL_IS_LABEL_TEXT (ltext), NULL);
400 gtk_text_buffer_get_bounds (ltext->priv->buffer, &start, &end);
401 text = gtk_text_buffer_get_text (ltext->priv->buffer,
402 &start, &end, FALSE);
403 lines = gl_text_node_lines_new_from_text (text);
411 gl_label_text_get_text (glLabelText *ltext)
413 GtkTextIter start, end;
416 g_return_val_if_fail (ltext && GL_IS_LABEL_TEXT (ltext), NULL);
418 gtk_text_buffer_get_bounds (ltext->priv->buffer, &start, &end);
419 text = gtk_text_buffer_get_text (ltext->priv->buffer,
420 &start, &end, FALSE);
426 /*****************************************************************************/
427 /* Text buffer "changed" callback. */
428 /*****************************************************************************/
430 buffer_begin_user_action_cb (GtkTextBuffer *textbuffer,
435 if ( ltext->priv->checkpoint_flag )
437 label = gl_label_object_get_parent (GL_LABEL_OBJECT (ltext));
438 gl_label_checkpoint (label, _("Typing"));
443 /*****************************************************************************/
444 /* Text buffer "changed" callback. */
445 /*****************************************************************************/
447 buffer_changed_cb (GtkTextBuffer *textbuffer,
450 ltext->priv->size_changed = TRUE;
452 gl_label_object_emit_changed (GL_LABEL_OBJECT(ltext));
456 /*****************************************************************************/
457 /* Get object size method. */
458 /*****************************************************************************/
460 get_size (glLabelObject *object,
464 glLabelText *ltext = (glLabelText *)object;
465 PangoFontMap *fontmap;
466 PangoContext *context;
467 cairo_font_options_t *options;
470 PangoFontDescription *desc;
472 gdouble line_spacing;
473 GtkTextIter start, end;
475 gdouble w_parent, h_parent;
478 gl_debug (DEBUG_LABEL, "START");
480 g_return_if_fail (ltext && GL_IS_LABEL_TEXT (ltext));
482 gl_label_object_get_raw_size (object, &w_parent, &h_parent);
484 if ( (w_parent != 0.0) || (h_parent != 0.0) ) {
490 if (!ltext->priv->size_changed)
497 font_size = GL_LABEL_TEXT (object)->priv->font_size * FONT_SCALE;
498 line_spacing = GL_LABEL_TEXT (object)->priv->line_spacing;
500 gtk_text_buffer_get_bounds (ltext->priv->buffer, &start, &end);
501 text = gtk_text_buffer_get_text (ltext->priv->buffer,
502 &start, &end, FALSE);
505 fontmap = pango_cairo_font_map_new ();
506 context = pango_cairo_font_map_create_context (PANGO_CAIRO_FONT_MAP (fontmap));
507 options = cairo_font_options_create ();
508 cairo_font_options_set_hint_style (options, CAIRO_HINT_STYLE_NONE);
509 pango_cairo_context_set_font_options (context, options);
510 cairo_font_options_destroy (options);
512 layout = pango_layout_new (context);
514 style = GL_LABEL_TEXT (object)->priv->font_italic_flag ? PANGO_STYLE_ITALIC : PANGO_STYLE_NORMAL;
516 desc = pango_font_description_new ();
517 pango_font_description_set_family (desc, GL_LABEL_TEXT (object)->priv->font_family);
518 pango_font_description_set_weight (desc, GL_LABEL_TEXT (object)->priv->font_weight);
519 pango_font_description_set_style (desc, style);
520 pango_font_description_set_size (desc, font_size * PANGO_SCALE);
521 pango_layout_set_font_description (layout, desc);
522 pango_font_description_free (desc);
524 pango_layout_set_spacing (layout, font_size * (line_spacing-1) * PANGO_SCALE);
525 pango_layout_set_text (layout, text, -1);
526 pango_layout_get_size (layout, &iw, &ih);
527 *w = ltext->priv->w = iw / PANGO_SCALE + 2*GL_LABEL_TEXT_MARGIN;
528 *h = ltext->priv->h = ih / PANGO_SCALE;
529 ltext->priv->size_changed = FALSE;
531 g_object_unref (layout);
532 g_object_unref (context);
533 g_object_unref (fontmap);
536 gl_debug (DEBUG_LABEL, "END");
540 /*****************************************************************************/
541 /* Set font family method. */
542 /*****************************************************************************/
544 set_font_family (glLabelObject *object,
545 const gchar *font_family,
548 glLabelText *ltext = (glLabelText *)object;
549 gchar *good_font_family;
552 gl_debug (DEBUG_LABEL, "START");
554 g_return_if_fail (ltext && GL_IS_LABEL_TEXT (ltext));
555 g_return_if_fail (font_family);
557 good_font_family = gl_font_util_validate_family (font_family);
559 if (ltext->priv->font_family) {
560 if (strcmp (ltext->priv->font_family, good_font_family) == 0) {
561 g_free (good_font_family);
562 gl_debug (DEBUG_LABEL, "END (no change)");
565 g_free (ltext->priv->font_family);
570 label = gl_label_object_get_parent (GL_LABEL_OBJECT (ltext));
571 gl_label_checkpoint (label, _("Font family"));
574 ltext->priv->font_family = g_strdup (good_font_family);
575 g_free (good_font_family);
577 gl_debug (DEBUG_LABEL, "new font family = %s", ltext->priv->font_family);
579 ltext->priv->size_changed = TRUE;
581 gl_font_history_model_add_family (gl_font_history, ltext->priv->font_family);
583 gl_label_object_emit_changed (GL_LABEL_OBJECT(ltext));
585 gl_debug (DEBUG_LABEL, "END");
589 /*****************************************************************************/
590 /* Set font size method. */
591 /*****************************************************************************/
593 set_font_size (glLabelObject *object,
597 glLabelText *ltext = (glLabelText *)object;
600 gl_debug (DEBUG_LABEL, "START");
602 g_return_if_fail (ltext && GL_IS_LABEL_TEXT (ltext));
604 if (ltext->priv->font_size != font_size)
608 label = gl_label_object_get_parent (GL_LABEL_OBJECT (ltext));
609 gl_label_checkpoint (label, _("Font size"));
612 ltext->priv->size_changed = TRUE;
614 ltext->priv->font_size = font_size;
615 gl_label_object_emit_changed (GL_LABEL_OBJECT(ltext));
618 gl_debug (DEBUG_LABEL, "END");
622 /*****************************************************************************/
623 /* Set font weight method. */
624 /*****************************************************************************/
626 set_font_weight (glLabelObject *object,
627 PangoWeight font_weight,
630 glLabelText *ltext = (glLabelText *)object;
633 gl_debug (DEBUG_LABEL, "START");
635 g_return_if_fail (ltext && GL_IS_LABEL_TEXT (ltext));
637 if (ltext->priv->font_weight != font_weight)
641 label = gl_label_object_get_parent (GL_LABEL_OBJECT (ltext));
642 gl_label_checkpoint (label, _("Font weight"));
645 ltext->priv->size_changed = TRUE;
647 ltext->priv->font_weight = font_weight;
648 gl_label_object_emit_changed (GL_LABEL_OBJECT(ltext));
651 gl_debug (DEBUG_LABEL, "END");
655 /*****************************************************************************/
656 /* Set font italic flag method. */
657 /*****************************************************************************/
659 set_font_italic_flag (glLabelObject *object,
660 gboolean font_italic_flag,
663 glLabelText *ltext = (glLabelText *)object;
666 gl_debug (DEBUG_LABEL, "START");
668 g_return_if_fail (ltext && GL_IS_LABEL_TEXT (ltext));
670 if (ltext->priv->font_italic_flag != font_italic_flag)
674 label = gl_label_object_get_parent (GL_LABEL_OBJECT (ltext));
675 gl_label_checkpoint (label, _("Italic"));
678 ltext->priv->size_changed = TRUE;
680 ltext->priv->font_italic_flag = font_italic_flag;
681 gl_label_object_emit_changed (GL_LABEL_OBJECT(ltext));
684 gl_debug (DEBUG_LABEL, "END");
688 /*****************************************************************************/
689 /* Set text alignment method. */
690 /*****************************************************************************/
692 set_text_alignment (glLabelObject *object,
693 PangoAlignment text_alignment,
696 glLabelText *ltext = (glLabelText *)object;
699 gl_debug (DEBUG_LABEL, "START");
701 g_return_if_fail (ltext && GL_IS_LABEL_TEXT (ltext));
703 if (ltext->priv->align != text_alignment)
707 label = gl_label_object_get_parent (GL_LABEL_OBJECT (ltext));
708 gl_label_checkpoint (label, _("Align text"));
711 ltext->priv->size_changed = TRUE;
713 ltext->priv->align = text_alignment;
714 gl_label_object_emit_changed (GL_LABEL_OBJECT(ltext));
717 gl_debug (DEBUG_LABEL, "END");
721 /*****************************************************************************/
722 /* Set text line spacing method. */
723 /*****************************************************************************/
725 set_text_line_spacing (glLabelObject *object,
726 gdouble line_spacing,
729 glLabelText *ltext = (glLabelText *)object;
732 gl_debug (DEBUG_LABEL, "START");
734 g_return_if_fail (ltext && GL_IS_LABEL_TEXT (ltext));
736 if (ltext->priv->line_spacing != line_spacing)
740 label = gl_label_object_get_parent (GL_LABEL_OBJECT (ltext));
741 gl_label_checkpoint (label, _("Line spacing"));
744 ltext->priv->size_changed = TRUE;
746 ltext->priv->line_spacing = line_spacing;
747 gl_label_object_emit_changed (GL_LABEL_OBJECT(ltext));
750 gl_debug (DEBUG_LABEL, "END");
754 /*****************************************************************************/
755 /* Set text color method. */
756 /*****************************************************************************/
758 set_text_color (glLabelObject *object,
759 glColorNode *text_color_node,
762 glLabelText *ltext = (glLabelText *)object;
765 gl_debug (DEBUG_LABEL, "START");
767 g_return_if_fail (ltext && GL_IS_LABEL_TEXT (ltext));
769 if (!gl_color_node_equal (ltext->priv->color_node, text_color_node))
773 label = gl_label_object_get_parent (GL_LABEL_OBJECT (ltext));
774 gl_label_checkpoint (label, _("Text color"));
777 gl_color_node_free (&(ltext->priv->color_node));
778 ltext->priv->color_node = gl_color_node_dup (text_color_node);
780 gl_label_object_emit_changed (GL_LABEL_OBJECT(ltext));
783 gl_debug (DEBUG_LABEL, "END");
787 /*****************************************************************************/
788 /* Get font family method. */
789 /*****************************************************************************/
791 get_font_family (glLabelObject *object)
793 glLabelText *ltext = (glLabelText *)object;
795 gl_debug (DEBUG_LABEL, "");
797 g_return_val_if_fail (ltext && GL_IS_LABEL_TEXT (ltext), NULL);
799 return g_strdup (ltext->priv->font_family);
803 /*****************************************************************************/
804 /* Get font size method. */
805 /*****************************************************************************/
807 get_font_size (glLabelObject *object)
809 glLabelText *ltext = (glLabelText *)object;
811 gl_debug (DEBUG_LABEL, "");
813 g_return_val_if_fail (ltext && GL_IS_LABEL_TEXT (ltext), 0.0);
815 return ltext->priv->font_size;
819 /*****************************************************************************/
820 /* Get font weight method. */
821 /*****************************************************************************/
823 get_font_weight (glLabelObject *object)
825 glLabelText *ltext = (glLabelText *)object;
827 gl_debug (DEBUG_LABEL, "");
829 g_return_val_if_fail (ltext && GL_IS_LABEL_TEXT (ltext), PANGO_WEIGHT_NORMAL);
831 return ltext->priv->font_weight;
835 /*****************************************************************************/
836 /* Get font italic flag method. */
837 /*****************************************************************************/
839 get_font_italic_flag (glLabelObject *object)
841 glLabelText *ltext = (glLabelText *)object;
843 gl_debug (DEBUG_LABEL, "");
845 g_return_val_if_fail (ltext && GL_IS_LABEL_TEXT (ltext), FALSE);
847 return ltext->priv->font_italic_flag;
851 /*****************************************************************************/
852 /* Get text alignment method. */
853 /*****************************************************************************/
854 static PangoAlignment
855 get_text_alignment (glLabelObject *object)
857 glLabelText *ltext = (glLabelText *)object;
859 gl_debug (DEBUG_LABEL, "");
861 g_return_val_if_fail (ltext && GL_IS_LABEL_TEXT (ltext), GTK_JUSTIFY_LEFT);
863 return ltext->priv->align;
867 /*****************************************************************************/
868 /* Get text line spacing method. */
869 /*****************************************************************************/
871 get_text_line_spacing (glLabelObject *object)
873 glLabelText *ltext = (glLabelText *)object;
875 gl_debug (DEBUG_LABEL, "");
877 g_return_val_if_fail (ltext && GL_IS_LABEL_TEXT (ltext), 0.0);
879 return ltext->priv->line_spacing;
883 /*****************************************************************************/
884 /* Get text color method. */
885 /*****************************************************************************/
887 get_text_color (glLabelObject *object)
889 glLabelText *ltext = (glLabelText *)object;
891 gl_debug (DEBUG_LABEL, "");
893 g_return_val_if_fail (ltext && GL_IS_LABEL_TEXT (ltext), 0);
895 return gl_color_node_dup (ltext->priv->color_node);
899 /*****************************************************************************/
900 /* Set auto shrink flag. */
901 /*****************************************************************************/
903 gl_label_text_set_auto_shrink (glLabelText *ltext,
904 gboolean auto_shrink,
909 gl_debug (DEBUG_LABEL, "BEGIN");
911 g_return_if_fail (ltext && GL_IS_LABEL_TEXT (ltext));
913 if (ltext->priv->auto_shrink != auto_shrink)
917 label = gl_label_object_get_parent (GL_LABEL_OBJECT (ltext));
918 gl_label_checkpoint (label, _("Auto shrink"));
921 ltext->priv->auto_shrink = auto_shrink;
922 gl_label_object_emit_changed (GL_LABEL_OBJECT(ltext));
925 gl_debug (DEBUG_LABEL, "END");
929 /*****************************************************************************/
930 /* Query auto shrink flag. */
931 /*****************************************************************************/
933 gl_label_text_get_auto_shrink (glLabelText *ltext)
935 gl_debug (DEBUG_LABEL, "");
937 g_return_val_if_fail (ltext && GL_IS_LABEL_TEXT (ltext), 0);
939 return ltext->priv->auto_shrink;
943 /*****************************************************************************/
944 /* Draw object method. */
945 /*****************************************************************************/
947 draw_object (glLabelObject *object,
949 gboolean screen_flag,
950 glMergeRecord *record)
952 glColorNode *color_node;
955 gl_debug (DEBUG_LABEL, "START");
957 color_node = gl_label_object_get_text_color (object);
958 color = gl_color_node_expand (color_node, record);
959 if (color_node->field_flag && screen_flag)
961 color = GL_COLOR_MERGE_DEFAULT;
963 gl_color_node_free (&color_node);
965 draw_text_real (object, cr, screen_flag, record, color);
967 gl_debug (DEBUG_LABEL, "END");
971 /*****************************************************************************/
972 /* Draw shadow method. */
973 /*****************************************************************************/
975 draw_shadow (glLabelObject *object,
977 gboolean screen_flag,
978 glMergeRecord *record)
980 glColorNode *color_node;
982 glColorNode *shadow_color_node;
983 gdouble shadow_opacity;
986 gl_debug (DEBUG_LABEL, "START");
988 color_node = gl_label_object_get_text_color (object);
989 color = gl_color_node_expand (color_node, record);
990 if (color_node->field_flag && screen_flag)
992 color = GL_COLOR_MERGE_DEFAULT;
994 gl_color_node_free (&color_node);
996 shadow_color_node = gl_label_object_get_shadow_color (object);
997 if (shadow_color_node->field_flag)
999 shadow_color_node->color = GL_COLOR_SHADOW_MERGE_DEFAULT;
1001 shadow_opacity = gl_label_object_get_shadow_opacity (object);
1002 shadow_color = gl_color_shadow (shadow_color_node->color, shadow_opacity, color);
1003 gl_color_node_free (&shadow_color_node);
1005 draw_text_real (object, cr, screen_flag, record, shadow_color);
1007 gl_debug (DEBUG_LABEL, "END");
1011 /*****************************************************************************/
1013 /*****************************************************************************/
1015 draw_text_real (glLabelObject *object,
1017 gboolean screen_flag,
1018 glMergeRecord *record,
1021 gdouble object_w, object_h;
1022 gdouble raw_w, raw_h;
1027 PangoWeight font_weight;
1028 gboolean font_italic_flag;
1029 gboolean auto_shrink;
1030 gdouble text_line_spacing;
1031 PangoAlignment alignment;
1033 PangoLayout *layout;
1034 PangoFontDescription *desc;
1035 cairo_font_options_t *font_options;
1036 PangoContext *context;
1039 gl_debug (DEBUG_LABEL, "START");
1041 gl_label_object_get_size (object, &object_w, &object_h);
1042 gl_label_object_get_raw_size (object, &raw_w, &raw_h);
1043 lines = gl_label_text_get_lines (GL_LABEL_TEXT (object));
1044 font_family = gl_label_object_get_font_family (object);
1045 font_size = gl_label_object_get_font_size (object) * FONT_SCALE;
1046 font_weight = gl_label_object_get_font_weight (object);
1047 font_italic_flag = gl_label_object_get_font_italic_flag (object);
1049 alignment = gl_label_object_get_text_alignment (object);
1051 gl_label_object_get_text_line_spacing (object);
1052 auto_shrink = gl_label_text_get_auto_shrink (GL_LABEL_TEXT (object));
1054 text = gl_text_node_lines_expand (lines, record);
1056 style = font_italic_flag ? PANGO_STYLE_ITALIC : PANGO_STYLE_NORMAL;
1059 if (!screen_flag && record && auto_shrink && (raw_w != 0.0))
1061 font_size = auto_shrink_font_size (cr,
1073 layout = pango_cairo_create_layout (cr);
1075 font_options = cairo_font_options_create ();
1076 cairo_font_options_set_hint_style (font_options, CAIRO_HINT_STYLE_NONE);
1077 context = pango_layout_get_context (layout);
1078 pango_cairo_context_set_font_options (context, font_options);
1079 cairo_font_options_destroy (font_options);
1081 desc = pango_font_description_new ();
1082 pango_font_description_set_family (desc, font_family);
1083 pango_font_description_set_weight (desc, font_weight);
1084 pango_font_description_set_style (desc, style);
1085 pango_font_description_set_size (desc, font_size * PANGO_SCALE);
1086 pango_layout_set_font_description (layout, desc);
1087 pango_font_description_free (desc);
1089 pango_layout_set_text (layout, text, -1);
1090 pango_layout_set_spacing (layout, font_size * (text_line_spacing-1) * PANGO_SCALE);
1093 pango_layout_set_width (layout, -1);
1097 pango_layout_set_width (layout, object_w * PANGO_SCALE);
1099 pango_layout_set_wrap (layout, PANGO_WRAP_CHAR);
1100 pango_layout_set_alignment (layout, alignment);
1103 cairo_set_source_rgba (cr, GL_COLOR_RGBA_ARGS (color));
1104 cairo_move_to (cr, GL_LABEL_TEXT_MARGIN, 0);
1105 pango_cairo_show_layout (cr, layout);
1109 g_object_unref (layout);
1112 gl_text_node_lines_free (&lines);
1113 g_free (font_family);
1115 gl_debug (DEBUG_LABEL, "END");
1119 /*****************************************************************************/
1120 /* Automatically shrink text size to fit within horizontal width. */
1121 /*****************************************************************************/
1123 auto_shrink_font_size (cairo_t *cr,
1131 PangoLayout *layout;
1132 PangoFontDescription *desc;
1134 gdouble layout_width;
1137 layout = pango_cairo_create_layout (cr);
1139 desc = pango_font_description_new ();
1140 pango_font_description_set_family (desc, family);
1141 pango_font_description_set_weight (desc, weight);
1142 pango_font_description_set_style (desc, style);
1143 pango_font_description_set_size (desc, size * PANGO_SCALE);
1145 pango_layout_set_font_description (layout, desc);
1146 pango_font_description_free (desc);
1148 pango_layout_set_text (layout, text, -1);
1149 pango_layout_set_width (layout, -1);
1150 pango_layout_get_size (layout, &iw, &ih);
1151 layout_width = (gdouble)iw / (gdouble)PANGO_SCALE;
1153 g_object_unref (layout);
1155 g_print ("Object w = %g, layout w = %g\n", width, layout_width);
1157 if ( layout_width > width )
1160 new_size = size * (width-2*GL_LABEL_TEXT_MARGIN)/layout_width;
1162 /* Round down to nearest 1/2 point */
1163 new_size = (int)(new_size*2.0) / 2.0;
1165 /* don't get ridiculously small. */
1180 /*****************************************************************************/
1181 /* Is object at coordinates? */
1182 /*****************************************************************************/
1184 object_at (glLabelObject *object,
1191 gl_label_object_get_size (object, &w, &h);
1193 cairo_rectangle (cr, 0.0, 0.0, w, h);
1195 if (cairo_in_fill (cr, x, y))
1206 * Local Variables: -- emacs
1208 * c-basic-offset: 8 -- emacs
1209 * tab-width: 8 -- emacs
1210 * indent-tabs-mode: nil -- emacs