summaryrefslogtreecommitdiff
path: root/libgame/game-text.c
blob: de798ac749512306e4ca78cdd29434a4b66f919e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
/*
 *  Copyright (C) 2005 Benjamin Otte <otte@gnome.org>
 *
 *  This program 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, or (at your option)
 *  any later version.
 *
 *  This program 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.
 *
 *  $Id$
 */

#include "game-private.h"
#include "game-text.h"
#include <math.h>

#define GAME_TEXT_IS_BOUND(text) ((text)->bound_to != NULL)

enum {
  PROP_0,
  PROP_TEXT,
};

static GameObjectClass *parent_class = NULL;

static void
game_text_get_property (GObject *object, guint param_id, GValue *value, 
    GParamSpec * pspec)
{
  GameText *text = GAME_TEXT (object);
  
  switch (param_id) {
    case PROP_TEXT:
      g_value_set_string (value, pango_layout_get_text (text->layout));
      break;
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (text, param_id, pspec);
      break;
  }
}

static void
game_text_set_property (GObject *object, guint param_id, const GValue *value,
    GParamSpec *pspec)
{
  GameText *text = GAME_TEXT (object);

  switch (param_id) {
    case PROP_TEXT:
      pango_layout_set_text (text->layout, g_value_get_string (value), -1);
      break;
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (text, param_id, pspec);
      break;
  }
  game_graphic_invalidate (GAME_GRAPHIC (text));
}

static void
game_text_finalize (GObject *object)
{
  GameText *text = GAME_TEXT (object);

  g_object_unref (text->layout);

  G_OBJECT_CLASS (parent_class)->finalize (object);
}

static void
game_text_draw (GameGraphic *graphic, cairo_t *cr)
{
  GameText *text = GAME_TEXT (graphic);
  int width, height, size, new_size = 0, max = G_MAXINT;
  double pw, ph, gw, gh, scale;
  const PangoFontDescription *desc;
  PangoFontDescription *copy;

  cairo_set_source_rgb (cr, 1.0, 1.0, 1.0);
  cairo_scale (cr, 0.01, 0.01);
  pango_cairo_update_layout (cr, text->layout);
  gw = (graphic->rect.x2 - graphic->rect.x1) * 100;
  gh = (graphic->rect.y2 - graphic->rect.y1) * 100;
  while (TRUE) {
    desc = pango_layout_get_font_description (text->layout);
    if (!desc)
      desc = pango_context_get_font_description (
	  pango_layout_get_context (text->layout));
    size = pango_font_description_get_size (desc) / PANGO_SCALE;
    pango_layout_get_size (text->layout, &width, &height);
    pw = (double) width / PANGO_SCALE;
    ph = (double) height / PANGO_SCALE;
    scale = MIN (gw / pw, gh / ph);
    if (scale < 1.0)
      max = size - 1;
    new_size = MIN (floor (scale * size), max);
    if (new_size == size)
      break;
    copy = pango_font_description_copy (desc);
    if (pango_font_description_get_size_is_absolute (desc)) {
      pango_font_description_set_absolute_size (copy, new_size * PANGO_SCALE);
    } else {
      pango_font_description_set_size (copy, new_size * PANGO_SCALE);
    }
    size = new_size;
    pango_layout_set_font_description (text->layout, copy);
    pango_font_description_free (copy);
  }
  pango_layout_get_size (text->layout, &width, &height);
  pw = (double) width / PANGO_SCALE;
  ph = (double) height / PANGO_SCALE;
  cairo_move_to (cr, 
      100 * graphic->rect.x1 + (gw - pw) / 2, 
      100 * graphic->rect.y1 + (gh - ph) / 2);
  pango_cairo_show_layout (cr, text->layout);
}

static void
game_text_class_init (gpointer g_class, gpointer class_data)
{
  GObjectClass *object_class = G_OBJECT_CLASS (g_class);
  GameGraphicClass *graphic_class = GAME_GRAPHIC_CLASS (g_class);
  
  parent_class = g_type_class_peek_parent (g_class);

  object_class->finalize = game_text_finalize;
  object_class->set_property = game_text_set_property;
  object_class->get_property = game_text_get_property;
  
  g_object_class_install_property (object_class, PROP_TEXT,
      g_param_spec_string ("text", _("text"), _("text to show"),
	  NULL, G_PARAM_READWRITE));

  graphic_class->draw = game_text_draw;
}

static void
game_text_init (GTypeInstance *instance, gpointer g_class)
{
  GameText *text = GAME_TEXT (instance);
  cairo_surface_t *dummy;
  cairo_t *cr;

  dummy = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 1, 1);
  cr = cairo_create (dummy);
  cairo_surface_destroy (dummy);
  text->layout = pango_cairo_create_layout (cr);
  cairo_destroy (cr);
}

GType
game_text_get_type ()
{
  static GType text_type = 0;
  
  if (!text_type) {
    static const GTypeInfo text_info = {
      sizeof (GameTextClass),
      NULL,
      NULL,
      game_text_class_init,
      NULL,
      NULL,
      sizeof (GameText),
      0,
      game_text_init,
    };
    
    text_type = g_type_register_static (GAME_TYPE_GRAPHIC, "GameText", &text_info, 0);
  }
  
  return text_type;
}