]> git.sur5r.net Git - glabels/blob - src/label-text.c
Removed obsolete workaround.
[glabels] / src / label-text.c
1 /*
2  *  label-text.c
3  *  Copyright (C) 2001-2009  Jim Evins <evins@snaught.com>.
4  *
5  *  This file is part of gLabels.
6  *
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.
11  *
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.
16  *
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/>.
19  */
20
21 #include <config.h>
22
23 #include "label-text.h"
24
25 #include <glib/gi18n.h>
26 #include <glib.h>
27 #include <pango/pango.h>
28 #include <math.h>
29 #include <string.h>
30
31 #include "font-util.h"
32 #include "font-history.h"
33
34 #include "debug.h"
35
36
37 /*========================================================*/
38 /* Private macros and constants.                          */
39 /*========================================================*/
40
41 #define FONT_SCALE (72.0/96.0)
42
43
44 /*========================================================*/
45 /* Private types.                                         */
46 /*========================================================*/
47
48 struct _glLabelTextPrivate {
49         GtkTextTagTable *tag_table;
50         GtkTextBuffer   *buffer;
51
52         gchar           *font_family;
53         gdouble          font_size;
54         PangoWeight      font_weight;
55         gboolean         font_italic_flag;
56         PangoAlignment   align;
57         glColorNode     *color_node;
58         gdouble          line_spacing;
59         gboolean         auto_shrink;
60
61         gboolean         size_changed;
62         gdouble          w;
63         gdouble          h;
64
65         gboolean         checkpoint_flag;
66 };
67
68
69 /*========================================================*/
70 /* Private globals.                                       */
71 /*========================================================*/
72
73
74 /*========================================================*/
75 /* Private function prototypes.                           */
76 /*========================================================*/
77
78 static void gl_label_text_finalize      (GObject          *object);
79
80 static void copy                        (glLabelObject    *dst_object,
81                                          glLabelObject    *src_object);
82
83 static void buffer_begin_user_action_cb (GtkTextBuffer    *textbuffer,
84                                          glLabelText      *ltext);
85
86 static void buffer_changed_cb           (GtkTextBuffer    *textbuffer,
87                                          glLabelText      *ltext);
88
89 static void get_size                    (glLabelObject    *object,
90                                          gdouble          *w,
91                                          gdouble          *h);
92
93 static void set_font_family             (glLabelObject    *object,
94                                          const gchar      *font_family,
95                                          gboolean          checkpoint);
96
97 static void set_font_size               (glLabelObject    *object,
98                                          gdouble           font_size,
99                                          gboolean          checkpoint);
100
101 static void set_font_weight             (glLabelObject    *object,
102                                          PangoWeight       font_weight,
103                                          gboolean          checkpoint);
104
105 static void set_font_italic_flag        (glLabelObject    *object,
106                                          gboolean          font_italic_flag,
107                                          gboolean          checkpoint);
108
109 static void set_text_alignment          (glLabelObject    *object,
110                                          PangoAlignment    text_alignment,
111                                          gboolean          checkpoint);
112
113 static void set_text_line_spacing       (glLabelObject    *object,
114                                          gdouble           text_line_spacing,
115                                          gboolean          checkpoint);
116
117 static void set_text_color              (glLabelObject    *object,
118                                          glColorNode      *text_color_node,
119                                          gboolean          checkpoint);
120
121 static gchar          *get_font_family             (glLabelObject    *object);
122
123 static gdouble         get_font_size               (glLabelObject    *object);
124
125 static PangoWeight     get_font_weight             (glLabelObject    *object);
126
127 static gboolean        get_font_italic_flag        (glLabelObject    *object);
128
129 static PangoAlignment  get_text_alignment          (glLabelObject    *object);
130
131 static gdouble         get_text_line_spacing       (glLabelObject    *object);
132
133 static glColorNode*    get_text_color              (glLabelObject    *object);
134
135 static void            draw_object                 (glLabelObject    *object,
136                                                     cairo_t          *cr,
137                                                     gboolean          screen_flag,
138                                                     glMergeRecord    *record);
139
140 static void            draw_shadow                 (glLabelObject    *object,
141                                                     cairo_t          *cr,
142                                                     gboolean          screen_flag,
143                                                     glMergeRecord    *record);
144
145 static void            draw_text_real              (glLabelObject    *object,
146                                                     cairo_t          *cr,
147                                                     gboolean          screen_flag,
148                                                     glMergeRecord    *record,
149                                                     guint             color);
150
151 static gdouble         auto_shrink_font_size       (cairo_t          *cr,
152                                                     gchar            *family,
153                                                     gdouble           size,
154                                                     PangoWeight       weight,
155                                                     PangoStyle        style,
156                                                     gchar            *text,
157                                                     gdouble           width);
158
159 static gboolean        object_at                   (glLabelObject    *object,
160                                                     cairo_t          *cr,
161                                                     gdouble           x_pixels,
162                                                     gdouble           y_pixels);
163
164
165 /*****************************************************************************/
166 /* Object infrastructure.                                                    */
167 /*****************************************************************************/
168 G_DEFINE_TYPE (glLabelText, gl_label_text, GL_TYPE_LABEL_OBJECT);
169
170
171 /*****************************************************************************/
172 /* Class Init Function.                                                      */
173 /*****************************************************************************/
174 static void
175 gl_label_text_class_init (glLabelTextClass *class)
176 {
177         GObjectClass       *object_class       = G_OBJECT_CLASS (class);
178         glLabelObjectClass *label_object_class = GL_LABEL_OBJECT_CLASS (class);
179
180         gl_label_text_parent_class = g_type_class_peek_parent (class);
181
182         label_object_class->copy                  = copy;
183
184         label_object_class->get_size              = get_size;
185
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;
203
204         object_class->finalize = gl_label_text_finalize;
205 }
206
207
208 /*****************************************************************************/
209 /* Object Instance Init Function.                                            */
210 /*****************************************************************************/
211 static void
212 gl_label_text_init (glLabelText *ltext)
213 {
214         ltext->priv = g_new0 (glLabelTextPrivate, 1);
215
216         ltext->priv->tag_table         = gtk_text_tag_table_new ();
217         ltext->priv->buffer            = gtk_text_buffer_new (ltext->priv->tag_table);
218
219         ltext->priv->size_changed      = TRUE;
220
221         ltext->priv->checkpoint_flag   = TRUE;
222
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);
227 }
228
229
230 /*****************************************************************************/
231 /* Finalize Method.                                                          */
232 /*****************************************************************************/
233 static void
234 gl_label_text_finalize (GObject *object)
235 {
236         glLabelText *ltext = GL_LABEL_TEXT (object);
237
238         g_return_if_fail (object && GL_IS_LABEL_TEXT (object));
239
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);
245
246         G_OBJECT_CLASS (gl_label_text_parent_class)->finalize (object);
247 }
248
249
250 /*****************************************************************************/
251 /** New Object Generator.                                                    */
252 /*****************************************************************************/
253 GObject *
254 gl_label_text_new (glLabel *label,
255                    gboolean checkpoint)
256 {
257         glLabelText   *ltext;
258         glColorNode   *color_node;
259
260         ltext = g_object_new (gl_label_text_get_type(), NULL);
261
262         if (label != NULL)
263         {
264                 if ( checkpoint )
265                 {
266                         gl_label_checkpoint (label, _("Create text object"));
267                 }
268
269                 color_node = gl_color_node_new_default ();
270
271                 color_node->color = gl_label_get_default_text_color (label);
272
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);
280
281                 gl_label_add_object (label, GL_LABEL_OBJECT (ltext));
282                 gl_label_object_set_parent (GL_LABEL_OBJECT (ltext), label);
283         }
284
285         return G_OBJECT (ltext);
286 }
287
288
289 /*****************************************************************************/
290 /* Copy object contents.                                                     */
291 /*****************************************************************************/
292 static void
293 copy (glLabelObject *dst_object,
294       glLabelObject *src_object)
295 {
296         glLabelText      *ltext     = (glLabelText *)src_object;
297         glLabelText      *new_ltext = (glLabelText *)dst_object;
298         GList            *lines;
299         glColorNode      *text_color_node;
300
301         gl_debug (DEBUG_LABEL, "START");
302
303         g_return_if_fail (ltext && GL_IS_LABEL_TEXT (ltext));
304         g_return_if_fail (new_ltext && GL_IS_LABEL_TEXT (new_ltext));
305
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);
309
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;
318
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;
322
323         gl_color_node_free (&text_color_node);
324         gl_text_node_lines_free (&lines);
325
326         gl_debug (DEBUG_LABEL, "END");
327 }
328
329
330 /*****************************************************************************/
331 /* Set object params.                                                        */
332 /*****************************************************************************/
333 void
334 gl_label_text_set_lines (glLabelText *ltext,
335                          GList       *lines,
336                          gboolean     checkpoint)
337 {
338         gchar *text;
339
340         gl_debug (DEBUG_LABEL, "START");
341
342         g_return_if_fail (ltext && GL_IS_LABEL_TEXT (ltext));
343
344         ltext->priv->checkpoint_flag = checkpoint;
345
346         text = gl_text_node_lines_expand (lines, NULL);
347         gtk_text_buffer_set_text (ltext->priv->buffer, text, -1);
348         g_free (text);
349
350         ltext->priv->size_changed = TRUE;
351
352         ltext->priv->checkpoint_flag = TRUE;
353
354         gl_debug (DEBUG_LABEL, "END");
355 }
356
357
358 void
359 gl_label_text_set_text (glLabelText *ltext,
360                         const gchar *text,
361                         gboolean     checkpoint)
362 {
363         gl_debug (DEBUG_LABEL, "START");
364
365         g_return_if_fail (ltext && GL_IS_LABEL_TEXT (ltext));
366
367         ltext->priv->checkpoint_flag = checkpoint;
368
369         gtk_text_buffer_set_text (ltext->priv->buffer, text, -1);
370
371         ltext->priv->size_changed = TRUE;
372
373         ltext->priv->checkpoint_flag = TRUE;
374
375         gl_debug (DEBUG_LABEL, "END");
376 }
377
378
379 /*****************************************************************************/
380 /* Get object params.                                                        */
381 /*****************************************************************************/
382 GtkTextBuffer *
383 gl_label_text_get_buffer (glLabelText *ltext)
384 {
385         g_return_val_if_fail (ltext && GL_IS_LABEL_TEXT (ltext), NULL);
386
387         return ltext->priv->buffer;
388 }
389
390
391 GList *
392 gl_label_text_get_lines (glLabelText *ltext)
393 {
394         GtkTextIter  start, end;
395         gchar       *text;
396         GList       *lines;
397
398         g_return_val_if_fail (ltext && GL_IS_LABEL_TEXT (ltext), NULL);
399
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);
404         g_free (text);
405
406         return lines;
407 }
408
409
410 gchar *
411 gl_label_text_get_text (glLabelText      *ltext)
412 {
413         GtkTextIter  start, end;
414         gchar       *text;
415
416         g_return_val_if_fail (ltext && GL_IS_LABEL_TEXT (ltext), NULL);
417
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);
421
422         return text;
423 }
424
425
426 /*****************************************************************************/
427 /* Text buffer "changed" callback.                                           */
428 /*****************************************************************************/
429 static void
430 buffer_begin_user_action_cb (GtkTextBuffer *textbuffer,
431                              glLabelText   *ltext)
432 {
433         glLabel *label;
434
435         if ( ltext->priv->checkpoint_flag )
436         {
437                 label = gl_label_object_get_parent (GL_LABEL_OBJECT (ltext));
438                 gl_label_checkpoint (label, _("Typing"));
439         }
440 }
441
442
443 /*****************************************************************************/
444 /* Text buffer "changed" callback.                                           */
445 /*****************************************************************************/
446 static void
447 buffer_changed_cb (GtkTextBuffer *textbuffer,
448                    glLabelText   *ltext)
449 {
450         ltext->priv->size_changed = TRUE;
451
452         gl_label_object_emit_changed (GL_LABEL_OBJECT(ltext));
453 }
454
455
456 /*****************************************************************************/
457 /* Get object size method.                                                   */
458 /*****************************************************************************/
459 static void
460 get_size (glLabelObject *object,
461           gdouble       *w,
462           gdouble       *h)
463 {
464         glLabelText          *ltext = (glLabelText *)object;
465         PangoFontMap         *fontmap;
466         PangoContext         *context;
467         cairo_font_options_t *options;
468         PangoStyle            style;
469         PangoLayout          *layout;
470         PangoFontDescription *desc;
471         gdouble               font_size;
472         gdouble               line_spacing;
473         GtkTextIter           start, end;
474         gchar                *text;
475         gdouble               w_parent, h_parent;
476         gint                  iw, ih;
477
478         gl_debug (DEBUG_LABEL, "START");
479
480         g_return_if_fail (ltext && GL_IS_LABEL_TEXT (ltext));
481
482         gl_label_object_get_raw_size (object, &w_parent, &h_parent);
483
484         if ( (w_parent != 0.0) || (h_parent != 0.0) ) {
485                 *w = w_parent;
486                 *h = h_parent;
487                 return;
488         }
489
490         if (!ltext->priv->size_changed)
491         {
492                 *w = ltext->priv->w;
493                 *h = ltext->priv->h;
494                 return;
495         }
496
497         font_size = GL_LABEL_TEXT (object)->priv->font_size * FONT_SCALE;
498         line_spacing = GL_LABEL_TEXT (object)->priv->line_spacing;
499
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);
503
504         
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);
511
512         layout = pango_layout_new (context);
513
514         style = GL_LABEL_TEXT (object)->priv->font_italic_flag ? PANGO_STYLE_ITALIC : PANGO_STYLE_NORMAL;
515
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);
523
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;
530
531         g_object_unref (layout);
532         g_object_unref (context);
533         g_object_unref (fontmap);
534         g_free (text);
535
536         gl_debug (DEBUG_LABEL, "END");
537 }
538
539
540 /*****************************************************************************/
541 /* Set font family method.                                                   */
542 /*****************************************************************************/
543 static void
544 set_font_family (glLabelObject *object,
545                  const gchar   *font_family,
546                  gboolean       checkpoint)
547 {
548         glLabelText    *ltext = (glLabelText *)object;
549         gchar          *good_font_family;
550         glLabel        *label;
551
552         gl_debug (DEBUG_LABEL, "START");
553
554         g_return_if_fail (ltext && GL_IS_LABEL_TEXT (ltext));
555         g_return_if_fail (font_family);
556
557         good_font_family = gl_font_util_validate_family (font_family);
558
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)");
563                         return;
564                 }
565                 g_free (ltext->priv->font_family);
566         }
567
568         if ( checkpoint )
569         {
570                 label = gl_label_object_get_parent (GL_LABEL_OBJECT (ltext));
571                 gl_label_checkpoint (label, _("Font family"));
572         }
573
574         ltext->priv->font_family = g_strdup (good_font_family);
575         g_free (good_font_family);
576
577         gl_debug (DEBUG_LABEL, "new font family = %s", ltext->priv->font_family);
578
579         ltext->priv->size_changed = TRUE;
580
581         gl_font_history_model_add_family (gl_font_history, ltext->priv->font_family);
582
583         gl_label_object_emit_changed (GL_LABEL_OBJECT(ltext));
584
585         gl_debug (DEBUG_LABEL, "END");
586 }
587
588
589 /*****************************************************************************/
590 /* Set font size method.                                                     */
591 /*****************************************************************************/
592 static void
593 set_font_size (glLabelObject *object,
594                gdouble        font_size,
595                gboolean       checkpoint)
596 {
597         glLabelText    *ltext = (glLabelText *)object;
598         glLabel        *label;
599
600         gl_debug (DEBUG_LABEL, "START");
601
602         g_return_if_fail (ltext && GL_IS_LABEL_TEXT (ltext));
603
604         if (ltext->priv->font_size != font_size)
605         {
606                 if ( checkpoint )
607                 {
608                         label = gl_label_object_get_parent (GL_LABEL_OBJECT (ltext));
609                         gl_label_checkpoint (label, _("Font size"));
610                 }
611
612                 ltext->priv->size_changed = TRUE;
613
614                 ltext->priv->font_size = font_size;
615                 gl_label_object_emit_changed (GL_LABEL_OBJECT(ltext));
616         }
617
618         gl_debug (DEBUG_LABEL, "END");
619 }
620
621
622 /*****************************************************************************/
623 /* Set font weight method.                                                   */
624 /*****************************************************************************/
625 static void
626 set_font_weight (glLabelObject   *object,
627                  PangoWeight      font_weight,
628                  gboolean         checkpoint)
629 {
630         glLabelText    *ltext = (glLabelText *)object;
631         glLabel        *label;
632
633         gl_debug (DEBUG_LABEL, "START");
634
635         g_return_if_fail (ltext && GL_IS_LABEL_TEXT (ltext));
636
637         if (ltext->priv->font_weight != font_weight)
638         {
639                 if ( checkpoint )
640                 {
641                         label = gl_label_object_get_parent (GL_LABEL_OBJECT (ltext));
642                         gl_label_checkpoint (label, _("Font weight"));
643                 }
644
645                 ltext->priv->size_changed = TRUE;
646
647                 ltext->priv->font_weight = font_weight;
648                 gl_label_object_emit_changed (GL_LABEL_OBJECT(ltext));
649         }
650
651         gl_debug (DEBUG_LABEL, "END");
652 }
653
654
655 /*****************************************************************************/
656 /* Set font italic flag method.                                              */
657 /*****************************************************************************/
658 static void
659 set_font_italic_flag (glLabelObject *object,
660                       gboolean       font_italic_flag,
661                       gboolean       checkpoint)
662 {
663         glLabelText    *ltext = (glLabelText *)object;
664         glLabel        *label;
665
666         gl_debug (DEBUG_LABEL, "START");
667
668         g_return_if_fail (ltext && GL_IS_LABEL_TEXT (ltext));
669
670         if (ltext->priv->font_italic_flag != font_italic_flag)
671         {
672                 if ( checkpoint )
673                 {
674                         label = gl_label_object_get_parent (GL_LABEL_OBJECT (ltext));
675                         gl_label_checkpoint (label, _("Italic"));
676                 }
677
678                 ltext->priv->size_changed = TRUE;
679
680                 ltext->priv->font_italic_flag = font_italic_flag;
681                 gl_label_object_emit_changed (GL_LABEL_OBJECT(ltext));
682         }
683
684         gl_debug (DEBUG_LABEL, "END");
685 }
686
687
688 /*****************************************************************************/
689 /* Set text alignment method.                                                */
690 /*****************************************************************************/
691 static void
692 set_text_alignment (glLabelObject    *object,
693                     PangoAlignment    text_alignment,
694                     gboolean          checkpoint)
695 {
696         glLabelText    *ltext = (glLabelText *)object;
697         glLabel        *label;
698
699         gl_debug (DEBUG_LABEL, "START");
700
701         g_return_if_fail (ltext && GL_IS_LABEL_TEXT (ltext));
702
703         if (ltext->priv->align != text_alignment)
704         {
705                 if ( checkpoint )
706                 {
707                         label = gl_label_object_get_parent (GL_LABEL_OBJECT (ltext));
708                         gl_label_checkpoint (label, _("Align text"));
709                 }
710
711                 ltext->priv->size_changed = TRUE;
712
713                 ltext->priv->align = text_alignment;
714                 gl_label_object_emit_changed (GL_LABEL_OBJECT(ltext));
715         }
716
717         gl_debug (DEBUG_LABEL, "END");
718 }
719
720
721 /*****************************************************************************/
722 /* Set text line spacing method.                                             */
723 /*****************************************************************************/
724 static void
725 set_text_line_spacing (glLabelObject *object,
726                        gdouble        line_spacing,
727                        gboolean       checkpoint)
728 {
729         glLabelText    *ltext = (glLabelText *)object;
730         glLabel        *label;
731
732         gl_debug (DEBUG_LABEL, "START");
733
734         g_return_if_fail (ltext && GL_IS_LABEL_TEXT (ltext));
735
736         if (ltext->priv->line_spacing != line_spacing)
737         {
738                 if ( checkpoint )
739                 {
740                         label = gl_label_object_get_parent (GL_LABEL_OBJECT (ltext));
741                         gl_label_checkpoint (label, _("Line spacing"));
742                 }
743
744                 ltext->priv->size_changed = TRUE;
745
746                 ltext->priv->line_spacing = line_spacing;
747                 gl_label_object_emit_changed (GL_LABEL_OBJECT(ltext));
748         }
749
750         gl_debug (DEBUG_LABEL, "END");
751 }
752
753
754 /*****************************************************************************/
755 /* Set text color method.                                                    */
756 /*****************************************************************************/
757 static void
758 set_text_color (glLabelObject *object,
759                 glColorNode   *text_color_node,
760                 gboolean       checkpoint)
761 {
762         glLabelText    *ltext = (glLabelText *)object;
763         glLabel        *label;
764
765         gl_debug (DEBUG_LABEL, "START");
766
767         g_return_if_fail (ltext && GL_IS_LABEL_TEXT (ltext));
768
769         if (!gl_color_node_equal (ltext->priv->color_node, text_color_node))
770         {
771                 if ( checkpoint )
772                 {
773                         label = gl_label_object_get_parent (GL_LABEL_OBJECT (ltext));
774                         gl_label_checkpoint (label, _("Text color"));
775                 }
776
777                 gl_color_node_free (&(ltext->priv->color_node));
778                 ltext->priv->color_node = gl_color_node_dup (text_color_node);
779                 
780                 gl_label_object_emit_changed (GL_LABEL_OBJECT(ltext));
781         }
782
783         gl_debug (DEBUG_LABEL, "END");
784 }
785
786
787 /*****************************************************************************/
788 /* Get font family method.                                                   */
789 /*****************************************************************************/
790 static gchar *
791 get_font_family (glLabelObject *object)
792 {
793         glLabelText    *ltext = (glLabelText *)object;
794
795         gl_debug (DEBUG_LABEL, "");
796
797         g_return_val_if_fail (ltext && GL_IS_LABEL_TEXT (ltext), NULL);
798
799         return g_strdup (ltext->priv->font_family);
800 }
801
802
803 /*****************************************************************************/
804 /* Get font size method.                                                     */
805 /*****************************************************************************/
806 static gdouble
807 get_font_size (glLabelObject *object)
808 {
809         glLabelText    *ltext = (glLabelText *)object;
810
811         gl_debug (DEBUG_LABEL, "");
812
813         g_return_val_if_fail (ltext && GL_IS_LABEL_TEXT (ltext), 0.0);
814
815         return ltext->priv->font_size;
816 }
817
818
819 /*****************************************************************************/
820 /* Get font weight method.                                                   */
821 /*****************************************************************************/
822 static PangoWeight
823 get_font_weight (glLabelObject   *object)
824 {
825         glLabelText    *ltext = (glLabelText *)object;
826
827         gl_debug (DEBUG_LABEL, "");
828
829         g_return_val_if_fail (ltext && GL_IS_LABEL_TEXT (ltext), PANGO_WEIGHT_NORMAL);
830
831         return ltext->priv->font_weight;
832 }
833
834
835 /*****************************************************************************/
836 /* Get font italic flag method.                                              */
837 /*****************************************************************************/
838 static gboolean
839 get_font_italic_flag (glLabelObject *object)
840 {
841         glLabelText    *ltext = (glLabelText *)object;
842
843         gl_debug (DEBUG_LABEL, "");
844
845         g_return_val_if_fail (ltext && GL_IS_LABEL_TEXT (ltext), FALSE);
846
847         return ltext->priv->font_italic_flag;
848 }
849
850
851 /*****************************************************************************/
852 /* Get text alignment method.                                                */
853 /*****************************************************************************/
854 static PangoAlignment
855 get_text_alignment (glLabelObject    *object)
856 {
857         glLabelText    *ltext = (glLabelText *)object;
858
859         gl_debug (DEBUG_LABEL, "");
860
861         g_return_val_if_fail (ltext && GL_IS_LABEL_TEXT (ltext), GTK_JUSTIFY_LEFT);
862
863         return ltext->priv->align;
864 }
865
866
867 /*****************************************************************************/
868 /* Get text line spacing method.                                             */
869 /*****************************************************************************/
870 static gdouble
871 get_text_line_spacing (glLabelObject *object)
872 {
873         glLabelText    *ltext = (glLabelText *)object;
874
875         gl_debug (DEBUG_LABEL, "");
876
877         g_return_val_if_fail (ltext && GL_IS_LABEL_TEXT (ltext), 0.0);
878
879         return ltext->priv->line_spacing;
880 }
881
882
883 /*****************************************************************************/
884 /* Get text color method.                                                    */
885 /*****************************************************************************/
886 static glColorNode*
887 get_text_color (glLabelObject *object)
888 {
889         glLabelText    *ltext = (glLabelText *)object;
890
891         gl_debug (DEBUG_LABEL, "");
892
893         g_return_val_if_fail (ltext && GL_IS_LABEL_TEXT (ltext), 0);
894
895         return gl_color_node_dup (ltext->priv->color_node);
896 }
897
898
899 /*****************************************************************************/
900 /* Set auto shrink flag.                                                     */
901 /*****************************************************************************/
902 void
903 gl_label_text_set_auto_shrink (glLabelText      *ltext,
904                                gboolean          auto_shrink,
905                                gboolean          checkpoint)
906 {
907         glLabel *label;
908
909         gl_debug (DEBUG_LABEL, "BEGIN");
910
911         g_return_if_fail (ltext && GL_IS_LABEL_TEXT (ltext));
912
913         if (ltext->priv->auto_shrink != auto_shrink)
914         {
915                 if ( checkpoint )
916                 {
917                         label = gl_label_object_get_parent (GL_LABEL_OBJECT (ltext));
918                         gl_label_checkpoint (label, _("Auto shrink"));
919                 }
920
921                 ltext->priv->auto_shrink = auto_shrink;
922                 gl_label_object_emit_changed (GL_LABEL_OBJECT(ltext));
923         }
924
925         gl_debug (DEBUG_LABEL, "END");
926 }
927
928
929 /*****************************************************************************/
930 /* Query auto shrink flag.                                                   */
931 /*****************************************************************************/
932 gboolean
933 gl_label_text_get_auto_shrink (glLabelText      *ltext)
934 {
935         gl_debug (DEBUG_LABEL, "");
936
937         g_return_val_if_fail (ltext && GL_IS_LABEL_TEXT (ltext), 0);
938
939         return ltext->priv->auto_shrink;
940 }
941
942
943 /*****************************************************************************/
944 /* Draw object method.                                                       */
945 /*****************************************************************************/
946 static void
947 draw_object (glLabelObject *object,
948              cairo_t       *cr,
949              gboolean       screen_flag,
950              glMergeRecord *record)
951 {
952         glColorNode     *color_node;
953         guint            color;
954
955         gl_debug (DEBUG_LABEL, "START");
956
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)
960         {
961                 color = GL_COLOR_MERGE_DEFAULT;
962         }
963         gl_color_node_free (&color_node);
964         
965         draw_text_real (object, cr, screen_flag, record, color);
966
967         gl_debug (DEBUG_LABEL, "END");
968 }
969
970
971 /*****************************************************************************/
972 /* Draw shadow method.                                                       */
973 /*****************************************************************************/
974 static void
975 draw_shadow (glLabelObject *object,
976              cairo_t       *cr,
977              gboolean       screen_flag,
978              glMergeRecord *record)
979 {
980         glColorNode     *color_node;
981         guint            color;
982         glColorNode     *shadow_color_node;
983         gdouble          shadow_opacity;
984         guint            shadow_color;
985
986         gl_debug (DEBUG_LABEL, "START");
987
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)
991         {
992                 color = GL_COLOR_MERGE_DEFAULT;
993         }
994         gl_color_node_free (&color_node);
995         
996         shadow_color_node = gl_label_object_get_shadow_color (object);
997         if (shadow_color_node->field_flag)
998         {
999                 shadow_color_node->color = GL_COLOR_SHADOW_MERGE_DEFAULT;
1000         }
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);
1004
1005         draw_text_real (object, cr, screen_flag, record, shadow_color);
1006
1007         gl_debug (DEBUG_LABEL, "END");
1008 }
1009
1010
1011 /*****************************************************************************/
1012 /* Draw text.                                                                */
1013 /*****************************************************************************/
1014 static void
1015 draw_text_real (glLabelObject *object,
1016                 cairo_t       *cr,
1017                 gboolean       screen_flag,
1018                 glMergeRecord *record,
1019                 guint          color)
1020 {
1021         gdouble          object_w, object_h;
1022         gdouble          raw_w, raw_h;
1023         gchar           *text;
1024         GList           *lines;
1025         gchar           *font_family;
1026         gdouble          font_size;
1027         PangoWeight      font_weight;
1028         gboolean         font_italic_flag;
1029         gboolean         auto_shrink;
1030         gdouble          text_line_spacing;
1031         PangoAlignment   alignment;
1032         PangoStyle       style;
1033         PangoLayout     *layout;
1034         PangoFontDescription *desc;
1035         cairo_font_options_t *font_options;
1036         PangoContext         *context;
1037
1038
1039         gl_debug (DEBUG_LABEL, "START");
1040
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);
1048
1049         alignment = gl_label_object_get_text_alignment (object);
1050         text_line_spacing =
1051                 gl_label_object_get_text_line_spacing (object);
1052         auto_shrink = gl_label_text_get_auto_shrink (GL_LABEL_TEXT (object));
1053
1054         text = gl_text_node_lines_expand (lines, record);
1055
1056         style = font_italic_flag ? PANGO_STYLE_ITALIC : PANGO_STYLE_NORMAL;
1057
1058
1059         if (!screen_flag && record && auto_shrink && (raw_w != 0.0))
1060         {
1061                 font_size = auto_shrink_font_size (cr,
1062                                                    font_family,
1063                                                    font_size,
1064                                                    font_weight,
1065                                                    style,
1066                                                    text,
1067                                                    object_w);
1068         }
1069
1070
1071         cairo_save (cr);
1072
1073         layout = pango_cairo_create_layout (cr);
1074
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);
1080
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);
1088
1089         pango_layout_set_text (layout, text, -1);
1090         pango_layout_set_spacing (layout, font_size * (text_line_spacing-1) * PANGO_SCALE);
1091         if (raw_w == 0.0)
1092         {
1093                 pango_layout_set_width (layout, -1);
1094         }
1095         else
1096         {
1097                 pango_layout_set_width (layout, object_w * PANGO_SCALE);
1098         }
1099         pango_layout_set_wrap (layout, PANGO_WRAP_CHAR);
1100         pango_layout_set_alignment (layout, alignment);
1101
1102
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);
1106
1107         cairo_restore (cr);
1108
1109         g_object_unref (layout);
1110
1111
1112         gl_text_node_lines_free (&lines);
1113         g_free (font_family);
1114
1115         gl_debug (DEBUG_LABEL, "END");
1116 }
1117
1118
1119 /*****************************************************************************/
1120 /* Automatically shrink text size to fit within horizontal width.            */
1121 /*****************************************************************************/
1122 static gdouble
1123 auto_shrink_font_size (cairo_t     *cr,
1124                        gchar       *family,
1125                        gdouble      size,
1126                        PangoWeight  weight,
1127                        PangoStyle   style,
1128                        gchar       *text,
1129                        gdouble      width)
1130 {
1131         PangoLayout          *layout;
1132         PangoFontDescription *desc;
1133         gint                  iw, ih;
1134         gdouble               layout_width;
1135         gdouble               new_size;
1136
1137         layout = pango_cairo_create_layout (cr);
1138
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);
1144         
1145         pango_layout_set_font_description (layout, desc);
1146         pango_font_description_free       (desc);
1147
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;
1152
1153         g_object_unref (layout);
1154
1155         g_print ("Object w = %g, layout w = %g\n", width, layout_width);
1156
1157         if ( layout_width > width )
1158         {
1159                 /* Scale down. */
1160                 new_size = size * (width-2*GL_LABEL_TEXT_MARGIN)/layout_width;
1161
1162                 /* Round down to nearest 1/2 point */
1163                 new_size = (int)(new_size*2.0) / 2.0;
1164
1165                 /* don't get ridiculously small. */
1166                 if (new_size < 1.0)
1167                 {
1168                         new_size = 1.0;
1169                 }
1170         }
1171         else
1172         {
1173                 new_size = size;
1174         }
1175
1176         return new_size;
1177 }
1178
1179
1180 /*****************************************************************************/
1181 /* Is object at coordinates?                                                 */
1182 /*****************************************************************************/
1183 static gboolean
1184 object_at (glLabelObject *object,
1185            cairo_t       *cr,
1186            gdouble        x,
1187            gdouble        y)
1188 {
1189         gdouble           w, h;
1190
1191         gl_label_object_get_size (object, &w, &h);
1192
1193         cairo_rectangle (cr, 0.0, 0.0, w, h);
1194
1195         if (cairo_in_fill (cr, x, y))
1196         {
1197                 return TRUE;
1198         }
1199
1200         return FALSE;
1201 }
1202
1203
1204
1205 /*
1206  * Local Variables:       -- emacs
1207  * mode: C                -- emacs
1208  * c-basic-offset: 8      -- emacs
1209  * tab-width: 8           -- emacs
1210  * indent-tabs-mode: nil  -- emacs
1211  * End:                   -- emacs
1212  */