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"
26 #include <pango/pango.h>
30 #include "font-util.h"
31 #include "font-history.h"
36 /*========================================================*/
37 /* Private macros and constants. */
38 /*========================================================*/
40 #define DEFAULT_FONT_FAMILY "Sans"
41 #define DEFAULT_FONT_SIZE 14.0
42 #define DEFAULT_FONT_WEIGHT PANGO_WEIGHT_NORMAL
43 #define DEFAULT_FONT_ITALIC_FLAG FALSE
44 #define DEFAULT_ALIGN PANGO_ALIGN_LEFT
45 #define DEFAULT_COLOR GL_COLOR (0,0,0)
46 #define DEFAULT_TEXT_LINE_SPACING 1.0
47 #define DEFAULT_AUTO_SHRINK FALSE
49 #define FONT_SCALE (72.0/96.0)
52 /*========================================================*/
54 /*========================================================*/
56 struct _glLabelTextPrivate {
57 GtkTextTagTable *tag_table;
58 GtkTextBuffer *buffer;
62 PangoWeight font_weight;
63 gboolean font_italic_flag;
65 glColorNode *color_node;
69 gboolean size_changed;
75 /*========================================================*/
76 /* Private globals. */
77 /*========================================================*/
80 /*========================================================*/
81 /* Private function prototypes. */
82 /*========================================================*/
84 static void gl_label_text_finalize (GObject *object);
86 static void copy (glLabelObject *dst_object,
87 glLabelObject *src_object);
89 static void buffer_changed_cb (GtkTextBuffer *textbuffer,
92 static void get_size (glLabelObject *object,
96 static void set_font_family (glLabelObject *object,
97 const gchar *font_family);
99 static void set_font_size (glLabelObject *object,
102 static void set_font_weight (glLabelObject *object,
103 PangoWeight font_weight);
105 static void set_font_italic_flag (glLabelObject *object,
106 gboolean font_italic_flag);
108 static void set_text_alignment (glLabelObject *object,
109 PangoAlignment text_alignment);
111 static void set_text_line_spacing (glLabelObject *object,
112 gdouble text_line_spacing);
114 static void set_text_color (glLabelObject *object,
115 glColorNode *text_color_node);
117 static gchar *get_font_family (glLabelObject *object);
119 static gdouble get_font_size (glLabelObject *object);
121 static PangoWeight get_font_weight (glLabelObject *object);
123 static gboolean get_font_italic_flag (glLabelObject *object);
125 static PangoAlignment get_text_alignment (glLabelObject *object);
127 static gdouble get_text_line_spacing (glLabelObject *object);
129 static glColorNode* get_text_color (glLabelObject *object);
131 static void draw_object (glLabelObject *object,
133 gboolean screen_flag,
134 glMergeRecord *record);
136 static void draw_shadow (glLabelObject *object,
138 gboolean screen_flag,
139 glMergeRecord *record);
141 static void draw_text_real (glLabelObject *object,
143 gboolean screen_flag,
144 glMergeRecord *record,
147 static gdouble auto_shrink_font_size (cairo_t *cr,
156 /*****************************************************************************/
157 /* Object infrastructure. */
158 /*****************************************************************************/
159 G_DEFINE_TYPE (glLabelText, gl_label_text, GL_TYPE_LABEL_OBJECT);
162 /*****************************************************************************/
163 /* Class Init Function. */
164 /*****************************************************************************/
166 gl_label_text_class_init (glLabelTextClass *class)
168 GObjectClass *object_class = G_OBJECT_CLASS (class);
169 glLabelObjectClass *label_object_class = GL_LABEL_OBJECT_CLASS (class);
171 gl_label_text_parent_class = g_type_class_peek_parent (class);
173 label_object_class->copy = copy;
175 label_object_class->get_size = get_size;
177 label_object_class->set_font_family = set_font_family;
178 label_object_class->set_font_size = set_font_size;
179 label_object_class->set_font_weight = set_font_weight;
180 label_object_class->set_font_italic_flag = set_font_italic_flag;
181 label_object_class->set_text_alignment = set_text_alignment;
182 label_object_class->set_text_line_spacing = set_text_line_spacing;
183 label_object_class->set_text_color = set_text_color;
184 label_object_class->get_font_family = get_font_family;
185 label_object_class->get_font_size = get_font_size;
186 label_object_class->get_font_weight = get_font_weight;
187 label_object_class->get_font_italic_flag = get_font_italic_flag;
188 label_object_class->get_text_alignment = get_text_alignment;
189 label_object_class->get_text_line_spacing = get_text_line_spacing;
190 label_object_class->get_text_color = get_text_color;
191 label_object_class->draw_object = draw_object;
192 label_object_class->draw_shadow = draw_shadow;
194 object_class->finalize = gl_label_text_finalize;
198 /*****************************************************************************/
199 /* Object Instance Init Function. */
200 /*****************************************************************************/
202 gl_label_text_init (glLabelText *ltext)
204 ltext->priv = g_new0 (glLabelTextPrivate, 1);
206 ltext->priv->tag_table = gtk_text_tag_table_new ();
207 ltext->priv->buffer = gtk_text_buffer_new (ltext->priv->tag_table);
209 ltext->priv->font_family = g_strdup(DEFAULT_FONT_FAMILY);
210 ltext->priv->font_size = DEFAULT_FONT_SIZE;
211 ltext->priv->font_weight = DEFAULT_FONT_WEIGHT;
212 ltext->priv->font_italic_flag = DEFAULT_FONT_ITALIC_FLAG;
213 ltext->priv->align = DEFAULT_ALIGN;
214 ltext->priv->color_node = gl_color_node_new_default ();
215 ltext->priv->color_node->color = DEFAULT_COLOR;
216 ltext->priv->line_spacing = DEFAULT_TEXT_LINE_SPACING;
217 ltext->priv->auto_shrink = DEFAULT_AUTO_SHRINK;
219 ltext->priv->size_changed = TRUE;
221 g_signal_connect (G_OBJECT(ltext->priv->buffer), "changed",
222 G_CALLBACK(buffer_changed_cb), ltext);
226 /*****************************************************************************/
227 /* Finalize Method. */
228 /*****************************************************************************/
230 gl_label_text_finalize (GObject *object)
232 glLabelText *ltext = GL_LABEL_TEXT (object);
234 g_return_if_fail (object && GL_IS_LABEL_TEXT (object));
236 g_object_unref (ltext->priv->tag_table);
237 g_object_unref (ltext->priv->buffer);
238 g_free (ltext->priv->font_family);
239 gl_color_node_free (&(ltext->priv->color_node));
240 g_free (ltext->priv);
242 G_OBJECT_CLASS (gl_label_text_parent_class)->finalize (object);
246 /*****************************************************************************/
247 /** New Object Generator. */
248 /*****************************************************************************/
250 gl_label_text_new (glLabel *label)
254 ltext = g_object_new (gl_label_text_get_type(), NULL);
256 gl_label_object_set_parent (GL_LABEL_OBJECT(ltext), label);
258 return G_OBJECT (ltext);
262 /*****************************************************************************/
263 /* Copy object contents. */
264 /*****************************************************************************/
266 copy (glLabelObject *dst_object,
267 glLabelObject *src_object)
269 glLabelText *ltext = (glLabelText *)src_object;
270 glLabelText *new_ltext = (glLabelText *)dst_object;
272 glColorNode *text_color_node;
274 gl_debug (DEBUG_LABEL, "START");
276 g_return_if_fail (ltext && GL_IS_LABEL_TEXT (ltext));
277 g_return_if_fail (new_ltext && GL_IS_LABEL_TEXT (new_ltext));
279 lines = gl_label_text_get_lines (ltext);
280 text_color_node = get_text_color (src_object);
281 gl_label_text_set_lines (new_ltext, lines);
283 new_ltext->priv->font_family = g_strdup (ltext->priv->font_family);
284 new_ltext->priv->font_size = ltext->priv->font_size;
285 new_ltext->priv->font_weight = ltext->priv->font_weight;
286 new_ltext->priv->font_italic_flag = ltext->priv->font_italic_flag;
287 set_text_color (dst_object, text_color_node);
288 new_ltext->priv->align = ltext->priv->align;
289 new_ltext->priv->line_spacing = ltext->priv->line_spacing;
290 new_ltext->priv->auto_shrink = ltext->priv->auto_shrink;
292 new_ltext->priv->size_changed = ltext->priv->size_changed;
293 new_ltext->priv->w = ltext->priv->w;
294 new_ltext->priv->h = ltext->priv->h;
296 gl_color_node_free (&text_color_node);
297 gl_text_node_lines_free (&lines);
299 gl_debug (DEBUG_LABEL, "END");
303 /*****************************************************************************/
304 /* Set object params. */
305 /*****************************************************************************/
307 gl_label_text_set_lines (glLabelText *ltext,
312 gl_debug (DEBUG_LABEL, "START");
314 g_return_if_fail (ltext && GL_IS_LABEL_TEXT (ltext));
316 text = gl_text_node_lines_expand (lines, NULL);
317 gtk_text_buffer_set_text (ltext->priv->buffer, text, -1);
320 ltext->priv->size_changed = TRUE;
322 gl_debug (DEBUG_LABEL, "END");
325 /*****************************************************************************/
326 /* Get object params. */
327 /*****************************************************************************/
329 gl_label_text_get_buffer (glLabelText *ltext)
331 g_return_val_if_fail (ltext && GL_IS_LABEL_TEXT (ltext), NULL);
333 return ltext->priv->buffer;
338 gl_label_text_get_lines (glLabelText *ltext)
340 GtkTextIter start, end;
344 g_return_val_if_fail (ltext && GL_IS_LABEL_TEXT (ltext), NULL);
346 gtk_text_buffer_get_bounds (ltext->priv->buffer, &start, &end);
347 text = gtk_text_buffer_get_text (ltext->priv->buffer,
348 &start, &end, FALSE);
349 lines = gl_text_node_lines_new_from_text (text);
356 /*****************************************************************************/
357 /* Text buffer "changed" callback. */
358 /*****************************************************************************/
360 buffer_changed_cb (GtkTextBuffer *textbuffer,
363 ltext->priv->size_changed = TRUE;
365 gl_label_object_emit_changed (GL_LABEL_OBJECT(ltext));
369 /*****************************************************************************/
370 /* Get object size method. */
371 /*****************************************************************************/
373 get_size (glLabelObject *object,
377 glLabelText *ltext = (glLabelText *)object;
378 PangoFontMap *fontmap;
379 PangoContext *context;
380 cairo_font_options_t *options;
383 PangoFontDescription *desc;
385 gdouble line_spacing;
386 GtkTextIter start, end;
388 gdouble w_parent, h_parent;
391 gl_debug (DEBUG_LABEL, "START");
393 g_return_if_fail (ltext && GL_IS_LABEL_TEXT (ltext));
395 gl_label_object_get_raw_size (object, &w_parent, &h_parent);
397 if ( (w_parent != 0.0) || (h_parent != 0.0) ) {
403 if (!ltext->priv->size_changed)
410 font_size = GL_LABEL_TEXT (object)->priv->font_size * FONT_SCALE;
411 line_spacing = GL_LABEL_TEXT (object)->priv->line_spacing;
413 gtk_text_buffer_get_bounds (ltext->priv->buffer, &start, &end);
414 text = gtk_text_buffer_get_text (ltext->priv->buffer,
415 &start, &end, FALSE);
418 fontmap = pango_cairo_font_map_new ();
419 context = pango_cairo_font_map_create_context (PANGO_CAIRO_FONT_MAP (fontmap));
420 options = cairo_font_options_create ();
421 cairo_font_options_set_hint_style (options, CAIRO_HINT_STYLE_NONE);
422 pango_cairo_context_set_font_options (context, options);
423 cairo_font_options_destroy (options);
425 layout = pango_layout_new (context);
427 style = GL_LABEL_TEXT (object)->priv->font_italic_flag ? PANGO_STYLE_ITALIC : PANGO_STYLE_NORMAL;
429 desc = pango_font_description_new ();
430 pango_font_description_set_family (desc, GL_LABEL_TEXT (object)->priv->font_family);
431 pango_font_description_set_weight (desc, GL_LABEL_TEXT (object)->priv->font_weight);
432 pango_font_description_set_style (desc, style);
433 pango_font_description_set_size (desc, font_size * PANGO_SCALE);
434 pango_layout_set_font_description (layout, desc);
435 pango_font_description_free (desc);
437 pango_layout_set_spacing (layout, font_size * (line_spacing-1) * PANGO_SCALE);
438 pango_layout_set_text (layout, text, -1);
439 pango_layout_get_size (layout, &iw, &ih);
440 *w = ltext->priv->w = iw / PANGO_SCALE + 2*GL_LABEL_TEXT_MARGIN;
441 *h = ltext->priv->h = ih / PANGO_SCALE;
442 ltext->priv->size_changed = FALSE;
444 g_object_unref (layout);
445 g_object_unref (context);
446 g_object_unref (fontmap);
449 gl_debug (DEBUG_LABEL, "END");
453 /*****************************************************************************/
454 /* Set font family method. */
455 /*****************************************************************************/
457 set_font_family (glLabelObject *object,
458 const gchar *font_family)
460 glLabelText *ltext = (glLabelText *)object;
461 gchar *good_font_family;
463 gl_debug (DEBUG_LABEL, "START");
465 g_return_if_fail (ltext && GL_IS_LABEL_TEXT (ltext));
466 g_return_if_fail (font_family);
468 good_font_family = gl_font_util_validate_family (font_family);
470 if (ltext->priv->font_family) {
471 if (strcmp (ltext->priv->font_family, good_font_family) == 0) {
472 g_free (good_font_family);
473 gl_debug (DEBUG_LABEL, "END (no change)");
476 g_free (ltext->priv->font_family);
478 ltext->priv->font_family = g_strdup (good_font_family);
479 g_free (good_font_family);
481 gl_debug (DEBUG_LABEL, "new font family = %s", ltext->priv->font_family);
483 ltext->priv->size_changed = TRUE;
485 gl_font_history_model_add_family (gl_font_history, ltext->priv->font_family);
487 gl_label_object_emit_changed (GL_LABEL_OBJECT(ltext));
489 gl_debug (DEBUG_LABEL, "END");
493 /*****************************************************************************/
494 /* Set font size method. */
495 /*****************************************************************************/
497 set_font_size (glLabelObject *object,
500 glLabelText *ltext = (glLabelText *)object;
502 gl_debug (DEBUG_LABEL, "START");
504 g_return_if_fail (ltext && GL_IS_LABEL_TEXT (ltext));
506 if (ltext->priv->font_size != font_size) {
508 ltext->priv->size_changed = TRUE;
510 ltext->priv->font_size = font_size;
511 gl_label_object_emit_changed (GL_LABEL_OBJECT(ltext));
515 gl_debug (DEBUG_LABEL, "END");
519 /*****************************************************************************/
520 /* Set font weight method. */
521 /*****************************************************************************/
523 set_font_weight (glLabelObject *object,
524 PangoWeight font_weight)
526 glLabelText *ltext = (glLabelText *)object;
528 gl_debug (DEBUG_LABEL, "START");
530 g_return_if_fail (ltext && GL_IS_LABEL_TEXT (ltext));
532 if (ltext->priv->font_weight != font_weight) {
534 ltext->priv->size_changed = TRUE;
536 ltext->priv->font_weight = font_weight;
537 gl_label_object_emit_changed (GL_LABEL_OBJECT(ltext));
541 gl_debug (DEBUG_LABEL, "END");
545 /*****************************************************************************/
546 /* Set font italic flag method. */
547 /*****************************************************************************/
549 set_font_italic_flag (glLabelObject *object,
550 gboolean font_italic_flag)
552 glLabelText *ltext = (glLabelText *)object;
554 gl_debug (DEBUG_LABEL, "START");
556 g_return_if_fail (ltext && GL_IS_LABEL_TEXT (ltext));
558 if (ltext->priv->font_italic_flag != font_italic_flag) {
560 ltext->priv->size_changed = TRUE;
562 ltext->priv->font_italic_flag = font_italic_flag;
563 gl_label_object_emit_changed (GL_LABEL_OBJECT(ltext));
567 gl_debug (DEBUG_LABEL, "END");
571 /*****************************************************************************/
572 /* Set text alignment method. */
573 /*****************************************************************************/
575 set_text_alignment (glLabelObject *object,
576 PangoAlignment text_alignment)
578 glLabelText *ltext = (glLabelText *)object;
580 gl_debug (DEBUG_LABEL, "START");
582 g_return_if_fail (ltext && GL_IS_LABEL_TEXT (ltext));
584 if (ltext->priv->align != text_alignment) {
586 ltext->priv->size_changed = TRUE;
588 ltext->priv->align = text_alignment;
589 gl_label_object_emit_changed (GL_LABEL_OBJECT(ltext));
593 gl_debug (DEBUG_LABEL, "END");
597 /*****************************************************************************/
598 /* Set text line spacing method. */
599 /*****************************************************************************/
601 set_text_line_spacing (glLabelObject *object,
602 gdouble line_spacing)
604 glLabelText *ltext = (glLabelText *)object;
606 gl_debug (DEBUG_LABEL, "START");
608 g_return_if_fail (ltext && GL_IS_LABEL_TEXT (ltext));
610 if (ltext->priv->line_spacing != line_spacing) {
612 ltext->priv->size_changed = TRUE;
614 ltext->priv->line_spacing = line_spacing;
615 gl_label_object_emit_changed (GL_LABEL_OBJECT(ltext));
619 gl_debug (DEBUG_LABEL, "END");
623 /*****************************************************************************/
624 /* Set text color method. */
625 /*****************************************************************************/
627 set_text_color (glLabelObject *object,
628 glColorNode *text_color_node)
630 glLabelText *ltext = (glLabelText *)object;
632 gl_debug (DEBUG_LABEL, "START");
634 g_return_if_fail (ltext && GL_IS_LABEL_TEXT (ltext));
636 if (!gl_color_node_equal (ltext->priv->color_node, text_color_node)) {
638 gl_color_node_free (&(ltext->priv->color_node));
639 ltext->priv->color_node = gl_color_node_dup (text_color_node);
641 gl_label_object_emit_changed (GL_LABEL_OBJECT(ltext));
645 gl_debug (DEBUG_LABEL, "END");
649 /*****************************************************************************/
650 /* Get font family method. */
651 /*****************************************************************************/
653 get_font_family (glLabelObject *object)
655 glLabelText *ltext = (glLabelText *)object;
657 gl_debug (DEBUG_LABEL, "");
659 g_return_val_if_fail (ltext && GL_IS_LABEL_TEXT (ltext), NULL);
661 return g_strdup (ltext->priv->font_family);
665 /*****************************************************************************/
666 /* Get font size method. */
667 /*****************************************************************************/
669 get_font_size (glLabelObject *object)
671 glLabelText *ltext = (glLabelText *)object;
673 gl_debug (DEBUG_LABEL, "");
675 g_return_val_if_fail (ltext && GL_IS_LABEL_TEXT (ltext), 0.0);
677 return ltext->priv->font_size;
681 /*****************************************************************************/
682 /* Get font weight method. */
683 /*****************************************************************************/
685 get_font_weight (glLabelObject *object)
687 glLabelText *ltext = (glLabelText *)object;
689 gl_debug (DEBUG_LABEL, "");
691 g_return_val_if_fail (ltext && GL_IS_LABEL_TEXT (ltext), PANGO_WEIGHT_NORMAL);
693 return ltext->priv->font_weight;
697 /*****************************************************************************/
698 /* Get font italic flag method. */
699 /*****************************************************************************/
701 get_font_italic_flag (glLabelObject *object)
703 glLabelText *ltext = (glLabelText *)object;
705 gl_debug (DEBUG_LABEL, "");
707 g_return_val_if_fail (ltext && GL_IS_LABEL_TEXT (ltext), FALSE);
709 return ltext->priv->font_italic_flag;
713 /*****************************************************************************/
714 /* Get text alignment method. */
715 /*****************************************************************************/
716 static PangoAlignment
717 get_text_alignment (glLabelObject *object)
719 glLabelText *ltext = (glLabelText *)object;
721 gl_debug (DEBUG_LABEL, "");
723 g_return_val_if_fail (ltext && GL_IS_LABEL_TEXT (ltext), GTK_JUSTIFY_LEFT);
725 return ltext->priv->align;
729 /*****************************************************************************/
730 /* Get text line spacing method. */
731 /*****************************************************************************/
733 get_text_line_spacing (glLabelObject *object)
735 glLabelText *ltext = (glLabelText *)object;
737 gl_debug (DEBUG_LABEL, "");
739 g_return_val_if_fail (ltext && GL_IS_LABEL_TEXT (ltext), 0.0);
741 return ltext->priv->line_spacing;
745 /*****************************************************************************/
746 /* Get text color method. */
747 /*****************************************************************************/
749 get_text_color (glLabelObject *object)
751 glLabelText *ltext = (glLabelText *)object;
753 gl_debug (DEBUG_LABEL, "");
755 g_return_val_if_fail (ltext && GL_IS_LABEL_TEXT (ltext), 0);
757 return gl_color_node_dup (ltext->priv->color_node);
761 /*****************************************************************************/
762 /* Set auto shrink flag. */
763 /*****************************************************************************/
765 gl_label_text_set_auto_shrink (glLabelText *ltext,
766 gboolean auto_shrink)
768 gl_debug (DEBUG_LABEL, "BEGIN");
770 g_return_if_fail (ltext && GL_IS_LABEL_TEXT (ltext));
772 if (ltext->priv->auto_shrink != auto_shrink) {
774 ltext->priv->auto_shrink = auto_shrink;
775 gl_label_object_emit_changed (GL_LABEL_OBJECT(ltext));
779 gl_debug (DEBUG_LABEL, "END");
783 /*****************************************************************************/
784 /* Query auto shrink flag. */
785 /*****************************************************************************/
787 gl_label_text_get_auto_shrink (glLabelText *ltext)
789 gl_debug (DEBUG_LABEL, "");
791 g_return_val_if_fail (ltext && GL_IS_LABEL_TEXT (ltext), 0);
793 return ltext->priv->auto_shrink;
797 /*****************************************************************************/
798 /* Draw object method. */
799 /*****************************************************************************/
801 draw_object (glLabelObject *object,
803 gboolean screen_flag,
804 glMergeRecord *record)
806 glColorNode *color_node;
809 gl_debug (DEBUG_LABEL, "START");
811 color_node = gl_label_object_get_text_color (object);
812 color = gl_color_node_expand (color_node, record);
813 if (color_node->field_flag && screen_flag)
815 color = GL_COLOR_MERGE_DEFAULT;
817 gl_color_node_free (&color_node);
819 draw_text_real (object, cr, screen_flag, record, color);
821 gl_debug (DEBUG_LABEL, "END");
825 /*****************************************************************************/
826 /* Draw shadow method. */
827 /*****************************************************************************/
829 draw_shadow (glLabelObject *object,
831 gboolean screen_flag,
832 glMergeRecord *record)
834 glColorNode *color_node;
836 glColorNode *shadow_color_node;
837 gdouble shadow_opacity;
840 gl_debug (DEBUG_LABEL, "START");
842 color_node = gl_label_object_get_text_color (object);
843 color = gl_color_node_expand (color_node, record);
844 if (color_node->field_flag && screen_flag)
846 color = GL_COLOR_MERGE_DEFAULT;
848 gl_color_node_free (&color_node);
850 shadow_color_node = gl_label_object_get_shadow_color (object);
851 if (shadow_color_node->field_flag)
853 shadow_color_node->color = GL_COLOR_SHADOW_MERGE_DEFAULT;
855 shadow_opacity = gl_label_object_get_shadow_opacity (object);
856 shadow_color = gl_color_shadow (shadow_color_node->color, shadow_opacity, color);
857 gl_color_node_free (&shadow_color_node);
859 draw_text_real (object, cr, screen_flag, record, shadow_color);
861 gl_debug (DEBUG_LABEL, "END");
865 /*****************************************************************************/
867 /*****************************************************************************/
869 draw_text_real (glLabelObject *object,
871 gboolean screen_flag,
872 glMergeRecord *record,
875 gdouble object_w, object_h;
876 gdouble raw_w, raw_h;
881 PangoWeight font_weight;
882 gboolean font_italic_flag;
883 gboolean auto_shrink;
884 gdouble text_line_spacing;
885 PangoAlignment alignment;
888 PangoFontDescription *desc;
889 gdouble scale_x, scale_y;
890 cairo_font_options_t *font_options;
891 PangoContext *context;
894 gl_debug (DEBUG_LABEL, "START");
896 gl_label_object_get_size (object, &object_w, &object_h);
897 gl_label_object_get_raw_size (object, &raw_w, &raw_h);
898 lines = gl_label_text_get_lines (GL_LABEL_TEXT (object));
899 font_family = gl_label_object_get_font_family (object);
900 font_size = gl_label_object_get_font_size (object) * FONT_SCALE;
901 font_weight = gl_label_object_get_font_weight (object);
902 font_italic_flag = gl_label_object_get_font_italic_flag (object);
904 alignment = gl_label_object_get_text_alignment (object);
906 gl_label_object_get_text_line_spacing (object);
907 auto_shrink = gl_label_text_get_auto_shrink (GL_LABEL_TEXT (object));
909 text = gl_text_node_lines_expand (lines, record);
911 style = font_italic_flag ? PANGO_STYLE_ITALIC : PANGO_STYLE_NORMAL;
914 if (!screen_flag && record && auto_shrink && (raw_w != 0.0))
916 font_size = auto_shrink_font_size (cr,
927 * Workaround for pango Bug#341481.
928 * Render font at device scale and scale font size accordingly.
932 cairo_device_to_user_distance (cr, &scale_x, &scale_y);
933 scale_x = fabs (scale_x);
934 scale_y = fabs (scale_y);
936 cairo_scale (cr, scale_x, scale_y);
938 layout = pango_cairo_create_layout (cr);
940 font_options = cairo_font_options_create ();
941 cairo_font_options_set_hint_style (font_options, CAIRO_HINT_STYLE_NONE);
942 context = pango_layout_get_context (layout);
943 pango_cairo_context_set_font_options (context, font_options);
944 cairo_font_options_destroy (font_options);
946 desc = pango_font_description_new ();
947 pango_font_description_set_family (desc, font_family);
948 pango_font_description_set_weight (desc, font_weight);
949 pango_font_description_set_style (desc, style);
950 pango_font_description_set_size (desc, font_size * PANGO_SCALE / scale_x);
951 pango_layout_set_font_description (layout, desc);
952 pango_font_description_free (desc);
954 pango_layout_set_text (layout, text, -1);
955 pango_layout_set_spacing (layout, font_size * (text_line_spacing-1) * PANGO_SCALE / scale_x);
958 pango_layout_set_width (layout, -1);
962 pango_layout_set_width (layout, object_w * PANGO_SCALE / scale_x);
964 pango_layout_set_wrap (layout, PANGO_WRAP_CHAR);
965 pango_layout_set_alignment (layout, alignment);
968 cairo_set_source_rgba (cr, GL_COLOR_RGBA_ARGS (color));
969 cairo_move_to (cr, GL_LABEL_TEXT_MARGIN/scale_x, 0);
970 pango_cairo_show_layout (cr, layout);
974 g_object_unref (layout);
977 gl_text_node_lines_free (&lines);
978 g_free (font_family);
980 gl_debug (DEBUG_LABEL, "END");
984 /*****************************************************************************/
985 /* Automatically shrink text size to fit within horizontal width. */
986 /*****************************************************************************/
988 auto_shrink_font_size (cairo_t *cr,
997 PangoFontDescription *desc;
999 gdouble layout_width;
1002 layout = pango_cairo_create_layout (cr);
1004 desc = pango_font_description_new ();
1005 pango_font_description_set_family (desc, family);
1006 pango_font_description_set_weight (desc, weight);
1007 pango_font_description_set_style (desc, style);
1008 pango_font_description_set_size (desc, size * PANGO_SCALE);
1010 pango_layout_set_font_description (layout, desc);
1011 pango_font_description_free (desc);
1013 pango_layout_set_text (layout, text, -1);
1014 pango_layout_set_width (layout, -1);
1015 pango_layout_get_size (layout, &iw, &ih);
1016 layout_width = (gdouble)iw / (gdouble)PANGO_SCALE;
1018 g_object_unref (layout);
1020 g_print ("Object w = %g, layout w = %g\n", width, layout_width);
1022 if ( layout_width > width )
1025 new_size = size * (width-2*GL_LABEL_TEXT_MARGIN)/layout_width;
1027 /* Round down to nearest 1/2 point */
1028 new_size = (int)(new_size*2.0) / 2.0;
1030 /* don't get ridiculously small. */
1047 * Local Variables: -- emacs
1049 * c-basic-offset: 8 -- emacs
1050 * tab-width: 8 -- emacs
1051 * indent-tabs-mode: nil -- emacs