-/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 8 -*- */
-
/*
- * (GLABELS) Label and Business Card Creation program for GNOME
- *
- * label_text.c: GLabels label text object
+ * label-text.c
+ * Copyright (C) 2001-2009 Jim Evins <evins@snaught.com>.
*
- * Copyright (C) 2001-2007 Jim Evins <evins@snaught.com>.
+ * This file is part of gLabels.
*
- * This program is free software; you can redistribute it and/or modify
+ * gLabels is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
+ * the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
- * This program is distributed in the hope that it will be useful,
+ * gLabels is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+ * along with gLabels. If not, see <http://www.gnu.org/licenses/>.
*/
+#include <config.h>
+
#include "label-text.h"
#include <glib/gmem.h>
#include "debug.h"
+
/*========================================================*/
/* Private macros and constants. */
/*========================================================*/
#define FONT_SCALE (72.0/96.0)
+
/*========================================================*/
/* Private types. */
/*========================================================*/
gdouble h;
};
+
/*========================================================*/
/* Private globals. */
/*========================================================*/
+
/*========================================================*/
/* Private function prototypes. */
/*========================================================*/
gboolean screen_flag,
glMergeRecord *record);
+static void draw_text_real (glLabelObject *object,
+ cairo_t *cr,
+ gboolean screen_flag,
+ glMergeRecord *record,
+ guint color);
+
static gdouble auto_shrink_font_size (cairo_t *cr,
gchar *family,
gdouble size,
gdouble width);
-\f
/*****************************************************************************/
-/* Boilerplate object stuff. */
+/* Object infrastructure. */
/*****************************************************************************/
G_DEFINE_TYPE (glLabelText, gl_label_text, GL_TYPE_LABEL_OBJECT);
+
+/*****************************************************************************/
+/* Class Init Function. */
+/*****************************************************************************/
static void
gl_label_text_class_init (glLabelTextClass *class)
{
object_class->finalize = gl_label_text_finalize;
}
+
+/*****************************************************************************/
+/* Object Instance Init Function. */
+/*****************************************************************************/
static void
gl_label_text_init (glLabelText *ltext)
{
G_CALLBACK(buffer_changed_cb), ltext);
}
+
+/*****************************************************************************/
+/* Finalize Method. */
+/*****************************************************************************/
static void
gl_label_text_finalize (GObject *object)
{
G_OBJECT_CLASS (gl_label_text_parent_class)->finalize (object);
}
+
/*****************************************************************************/
-/* NEW label "text" object. */
+/** New Object Generator. */
/*****************************************************************************/
GObject *
gl_label_text_new (glLabel *label)
return G_OBJECT (ltext);
}
+
/*****************************************************************************/
/* Copy object contents. */
/*****************************************************************************/
return ltext->priv->buffer;
}
+
GList *
gl_label_text_get_lines (glLabelText *ltext)
{
return lines;
}
-/*---------------------------------------------------------------------------*/
-/* PRIVATE. text buffer "changed" callback. */
-/*---------------------------------------------------------------------------*/
-void buffer_changed_cb (GtkTextBuffer *textbuffer,
- glLabelText *ltext)
+
+/*****************************************************************************/
+/* Text buffer "changed" callback. */
+/*****************************************************************************/
+static void
+buffer_changed_cb (GtkTextBuffer *textbuffer,
+ glLabelText *ltext)
{
ltext->priv->size_changed = TRUE;
gl_label_object_emit_changed (GL_LABEL_OBJECT(ltext));
}
-/*---------------------------------------------------------------------------*/
-/* PRIVATE. get object size method. */
-/*---------------------------------------------------------------------------*/
+
+/*****************************************************************************/
+/* Get object size method. */
+/*****************************************************************************/
static void
get_size (glLabelObject *object,
gdouble *w,
gl_debug (DEBUG_LABEL, "END");
}
-/*---------------------------------------------------------------------------*/
-/* PRIVATE. set font family method. */
-/*---------------------------------------------------------------------------*/
+
+/*****************************************************************************/
+/* Set font family method. */
+/*****************************************************************************/
static void
set_font_family (glLabelObject *object,
const gchar *font_family)
gl_debug (DEBUG_LABEL, "END");
}
-/*---------------------------------------------------------------------------*/
-/* PRIVATE. set font size method. */
-/*---------------------------------------------------------------------------*/
+
+/*****************************************************************************/
+/* Set font size method. */
+/*****************************************************************************/
static void
set_font_size (glLabelObject *object,
gdouble font_size)
gl_debug (DEBUG_LABEL, "END");
}
-/*---------------------------------------------------------------------------*/
-/* PRIVATE. set font weight method. */
-/*---------------------------------------------------------------------------*/
+
+/*****************************************************************************/
+/* Set font weight method. */
+/*****************************************************************************/
static void
set_font_weight (glLabelObject *object,
PangoWeight font_weight)
gl_debug (DEBUG_LABEL, "END");
}
-/*---------------------------------------------------------------------------*/
-/* PRIVATE. set font italic flag method. */
-/*---------------------------------------------------------------------------*/
+
+/*****************************************************************************/
+/* Set font italic flag method. */
+/*****************************************************************************/
static void
set_font_italic_flag (glLabelObject *object,
gboolean font_italic_flag)
gl_debug (DEBUG_LABEL, "END");
}
-/*---------------------------------------------------------------------------*/
-/* PRIVATE. set text alignment method. */
-/*---------------------------------------------------------------------------*/
+
+/*****************************************************************************/
+/* Set text alignment method. */
+/*****************************************************************************/
static void
set_text_alignment (glLabelObject *object,
PangoAlignment text_alignment)
gl_debug (DEBUG_LABEL, "END");
}
-/*---------------------------------------------------------------------------*/
-/* PRIVATE. set text line spacing method. */
-/*---------------------------------------------------------------------------*/
+
+/*****************************************************************************/
+/* Set text line spacing method. */
+/*****************************************************************************/
static void
set_text_line_spacing (glLabelObject *object,
gdouble line_spacing)
gl_debug (DEBUG_LABEL, "END");
}
-/*---------------------------------------------------------------------------*/
-/* PRIVATE. set text color method. */
-/*---------------------------------------------------------------------------*/
+
+/*****************************************************************************/
+/* Set text color method. */
+/*****************************************************************************/
static void
set_text_color (glLabelObject *object,
glColorNode *text_color_node)
gl_debug (DEBUG_LABEL, "END");
}
-/*---------------------------------------------------------------------------*/
-/* PRIVATE. get font family method. */
-/*---------------------------------------------------------------------------*/
+
+/*****************************************************************************/
+/* Get font family method. */
+/*****************************************************************************/
static gchar *
get_font_family (glLabelObject *object)
{
return g_strdup (ltext->priv->font_family);
}
-/*---------------------------------------------------------------------------*/
-/* PRIVATE. get font size method. */
-/*---------------------------------------------------------------------------*/
+
+/*****************************************************************************/
+/* Get font size method. */
+/*****************************************************************************/
static gdouble
get_font_size (glLabelObject *object)
{
return ltext->priv->font_size;
}
-/*---------------------------------------------------------------------------*/
-/* PRIVATE. get font weight method. */
-/*---------------------------------------------------------------------------*/
+
+/*****************************************************************************/
+/* Get font weight method. */
+/*****************************************************************************/
static PangoWeight
get_font_weight (glLabelObject *object)
{
return ltext->priv->font_weight;
}
-/*---------------------------------------------------------------------------*/
-/* PRIVATE. get font italic flag method. */
-/*---------------------------------------------------------------------------*/
+
+/*****************************************************************************/
+/* Get font italic flag method. */
+/*****************************************************************************/
static gboolean
get_font_italic_flag (glLabelObject *object)
{
return ltext->priv->font_italic_flag;
}
-/*---------------------------------------------------------------------------*/
-/* PRIVATE. get text alignment method. */
-/*---------------------------------------------------------------------------*/
+
+/*****************************************************************************/
+/* Get text alignment method. */
+/*****************************************************************************/
static PangoAlignment
get_text_alignment (glLabelObject *object)
{
return ltext->priv->align;
}
-/*---------------------------------------------------------------------------*/
-/* PRIVATE. get text line spacing method. */
-/*---------------------------------------------------------------------------*/
+
+/*****************************************************************************/
+/* Get text line spacing method. */
+/*****************************************************************************/
static gdouble
get_text_line_spacing (glLabelObject *object)
{
return ltext->priv->line_spacing;
}
-/*---------------------------------------------------------------------------*/
-/* PRIVATE. get text color method. */
-/*---------------------------------------------------------------------------*/
+
+/*****************************************************************************/
+/* Get text color method. */
+/*****************************************************************************/
static glColorNode*
get_text_color (glLabelObject *object)
{
return gl_color_node_dup (ltext->priv->color_node);
}
+
/*****************************************************************************/
/* Set auto shrink flag. */
/*****************************************************************************/
gl_debug (DEBUG_LABEL, "END");
}
+
/*****************************************************************************/
/* Query auto shrink flag. */
/*****************************************************************************/
return ltext->priv->auto_shrink;
}
+
/*****************************************************************************/
/* Draw object method. */
/*****************************************************************************/
gboolean screen_flag,
glMergeRecord *record)
{
- gdouble x0, y0;
- cairo_matrix_t matrix;
- gdouble object_w, object_h;
- gdouble raw_w, raw_h;
- gchar *text;
- GList *lines;
- gchar *font_family;
- gdouble font_size;
- PangoWeight font_weight;
- gboolean font_italic_flag;
glColorNode *color_node;
- gboolean auto_shrink;
guint color;
- gdouble text_line_spacing;
- PangoAlignment alignment;
- PangoStyle style;
- PangoLayout *layout;
- PangoFontDescription *desc;
- gdouble scale_x, scale_y;
- cairo_font_options_t *font_options;
- PangoContext *context;
-
gl_debug (DEBUG_LABEL, "START");
- gl_label_object_get_position (object, &x0, &y0);
- gl_label_object_get_matrix (object, &matrix);
-
- gl_label_object_get_size (object, &object_w, &object_h);
- gl_label_object_get_raw_size (object, &raw_w, &raw_h);
- lines = gl_label_text_get_lines (GL_LABEL_TEXT (object));
- font_family = gl_label_object_get_font_family (object);
- font_size = gl_label_object_get_font_size (object) * FONT_SCALE;
- font_weight = gl_label_object_get_font_weight (object);
- font_italic_flag = gl_label_object_get_font_italic_flag (object);
-
color_node = gl_label_object_get_text_color (object);
color = gl_color_node_expand (color_node, record);
if (color_node->field_flag && screen_flag)
}
gl_color_node_free (&color_node);
- alignment = gl_label_object_get_text_alignment (object);
- text_line_spacing =
- gl_label_object_get_text_line_spacing (object);
- auto_shrink = gl_label_text_get_auto_shrink (GL_LABEL_TEXT (object));
-
- text = gl_text_node_lines_expand (lines, record);
-
- style = font_italic_flag ? PANGO_STYLE_ITALIC : PANGO_STYLE_NORMAL;
-
-
- if (!screen_flag && record && auto_shrink && (raw_w != 0.0))
- {
- font_size = auto_shrink_font_size (cr,
- font_family,
- font_size,
- font_weight,
- style,
- text,
- object_w);
- g_print ("Autosize new size = %g\n", font_size);
- }
-
+ draw_text_real (object, cr, screen_flag, record, color);
- /*
- * Workaround for pango Bug#341481.
- * Render font at device scale and scale font size accordingly.
- */
- scale_x = 1.0;
- scale_y = 1.0;
- cairo_device_to_user_distance (cr, &scale_x, &scale_y);
- scale_x = fabs (scale_x);
- scale_y = fabs (scale_y);
- cairo_save (cr);
- cairo_scale (cr, scale_x, scale_y);
+ gl_debug (DEBUG_LABEL, "END");
+}
- layout = pango_cairo_create_layout (cr);
- font_options = cairo_font_options_create ();
- cairo_font_options_set_hint_style (font_options, CAIRO_HINT_STYLE_NONE);
- context = pango_layout_get_context (layout);
- pango_cairo_context_set_font_options (context, font_options);
- cairo_font_options_destroy (font_options);
+/*****************************************************************************/
+/* Draw shadow method. */
+/*****************************************************************************/
+static void
+draw_shadow (glLabelObject *object,
+ cairo_t *cr,
+ gboolean screen_flag,
+ glMergeRecord *record)
+{
+ glColorNode *color_node;
+ guint color;
+ glColorNode *shadow_color_node;
+ gdouble shadow_opacity;
+ guint shadow_color;
- desc = pango_font_description_new ();
- pango_font_description_set_family (desc, font_family);
- pango_font_description_set_weight (desc, font_weight);
- pango_font_description_set_style (desc, style);
- pango_font_description_set_size (desc, font_size * PANGO_SCALE / scale_x);
- pango_layout_set_font_description (layout, desc);
- pango_font_description_free (desc);
+ gl_debug (DEBUG_LABEL, "START");
- pango_layout_set_text (layout, text, -1);
- pango_layout_set_spacing (layout, font_size * (text_line_spacing-1) * PANGO_SCALE / scale_x);
- if (raw_w == 0.0)
- {
- pango_layout_set_width (layout, -1);
- }
- else
+ color_node = gl_label_object_get_text_color (object);
+ color = gl_color_node_expand (color_node, record);
+ if (color_node->field_flag && screen_flag)
{
- pango_layout_set_width (layout, object_w * PANGO_SCALE / scale_x);
+ color = GL_COLOR_MERGE_DEFAULT;
}
- pango_layout_set_wrap (layout, PANGO_WRAP_CHAR);
- pango_layout_set_alignment (layout, alignment);
-
-
- cairo_set_source_rgba (cr, GL_COLOR_RGBA_ARGS (color));
- cairo_move_to (cr, GL_LABEL_TEXT_MARGIN/scale_x, 0);
- pango_cairo_show_layout (cr, layout);
-
- cairo_restore (cr);
-
- g_object_unref (layout);
-
+ gl_color_node_free (&color_node);
+
+ shadow_color_node = gl_label_object_get_shadow_color (object);
+ if (shadow_color_node->field_flag)
+ {
+ shadow_color_node->color = GL_COLOR_SHADOW_MERGE_DEFAULT;
+ }
+ shadow_opacity = gl_label_object_get_shadow_opacity (object);
+ shadow_color = gl_color_shadow (shadow_color_node->color, shadow_opacity, color);
+ gl_color_node_free (&shadow_color_node);
- gl_text_node_lines_free (&lines);
- g_free (font_family);
+ draw_text_real (object, cr, screen_flag, record, shadow_color);
gl_debug (DEBUG_LABEL, "END");
}
+
/*****************************************************************************/
-/* Draw shadow method. */
+/* Draw text. */
/*****************************************************************************/
static void
-draw_shadow (glLabelObject *object,
- cairo_t *cr,
- gboolean screen_flag,
- glMergeRecord *record)
+draw_text_real (glLabelObject *object,
+ cairo_t *cr,
+ gboolean screen_flag,
+ glMergeRecord *record,
+ guint color)
{
gdouble x0, y0;
cairo_matrix_t matrix;
PangoWeight font_weight;
gboolean font_italic_flag;
gboolean auto_shrink;
- guint color;
- glColorNode *color_node;
gdouble text_line_spacing;
- glColorNode *shadow_color_node;
- gdouble shadow_opacity;
- guint shadow_color;
PangoAlignment alignment;
PangoStyle style;
PangoLayout *layout;
font_weight = gl_label_object_get_font_weight (object);
font_italic_flag = gl_label_object_get_font_italic_flag (object);
- color_node = gl_label_object_get_text_color (object);
- color = gl_color_node_expand (color_node, record);
- gl_color_node_free (&color_node);
-
alignment = gl_label_object_get_text_alignment (object);
text_line_spacing =
gl_label_object_get_text_line_spacing (object);
auto_shrink = gl_label_text_get_auto_shrink (GL_LABEL_TEXT (object));
- shadow_color_node = gl_label_object_get_shadow_color (object);
- if (shadow_color_node->field_flag)
- {
- shadow_color_node->color = GL_COLOR_SHADOW_MERGE_DEFAULT;
- }
- shadow_opacity = gl_label_object_get_shadow_opacity (object);
- shadow_color = gl_color_shadow (shadow_color_node->color, shadow_opacity, color);
- gl_color_node_free (&shadow_color_node);
-
text = gl_text_node_lines_expand (lines, record);
style = font_italic_flag ? PANGO_STYLE_ITALIC : PANGO_STYLE_NORMAL;
pango_layout_set_wrap (layout, PANGO_WRAP_CHAR);
pango_layout_set_alignment (layout, alignment);
- cairo_set_source_rgba (cr, GL_COLOR_RGBA_ARGS (shadow_color));
+
+ cairo_set_source_rgba (cr, GL_COLOR_RGBA_ARGS (color));
cairo_move_to (cr, GL_LABEL_TEXT_MARGIN/scale_x, 0);
pango_cairo_show_layout (cr, layout);
-
cairo_restore (cr);
g_object_unref (layout);
+
gl_text_node_lines_free (&lines);
g_free (font_family);
gl_debug (DEBUG_LABEL, "END");
}
+
/*****************************************************************************/
/* Automatically shrink text size to fit within horizontal width. */
/*****************************************************************************/
return new_size;
}
+
+
+/*
+ * Local Variables: -- emacs
+ * mode: C -- emacs
+ * c-basic-offset: 8 -- emacs
+ * tab-width: 8 -- emacs
+ * indent-tabs-mode: nil -- emacs
+ * End: -- emacs
+ */