File: textline.c

package info (click to toggle)
dia 0.97.3-1
  • links: PTS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 49,248 kB
  • ctags: 18,377
  • sloc: ansic: 150,303; xml: 55,218; cpp: 15,396; sh: 11,898; python: 3,782; makefile: 3,675
file content (341 lines) | stat: -rw-r--r-- 9,956 bytes parent folder | download | duplicates (2)
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
/* Dia -- an diagram creation/manipulation program
 * Copyright (C) 1998 Alexander Larsson
 *
 * 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 of the License, 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.
 */
#include <config.h>

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <glib.h>
#include <math.h>

#include <gdk/gdkkeysyms.h>

#include "propinternals.h"
#include "text.h"
#include "message.h"
#include "textline.h"

static void text_line_dirty_cache(TextLine *text_line);
static void text_line_cache_values(TextLine *text_line);
static void clear_layout_offset (TextLine *text_line);

/** Sets this object to display a particular string.
 * @param text_line The object to change.
 * @param string The string to display.  This string will be copied.
 */
void
text_line_set_string(TextLine *text_line, const gchar *string)
{
  if (text_line->chars == NULL ||
      strcmp(text_line->chars, string)) {
    if (text_line->chars != NULL) {
      g_free(text_line->chars);
    }
    
    text_line->chars = g_strdup(string);
    
    text_line_dirty_cache(text_line);
  }
}

/** Sets the font used by this object.
 * @param text_line The object to change.
 * @param font The font to use for displaying this object.
 */
void
text_line_set_font(TextLine *text_line, DiaFont *font)
{
  if (text_line->font != font) {
    DiaFont *old_font = text_line->font;
    dia_font_ref(font);
    text_line->font = font;
    if (old_font != NULL) {
      dia_font_unref(old_font);
    }
    text_line_dirty_cache(text_line);
  }
}

/** Sets the font height used by this object.
 * @param text_line The object to change.
 * @param height The font height to use for displaying this object 
 * (in cm, from baseline to baseline)
 */
void
text_line_set_height(TextLine *text_line, real height)
{
  if (fabs(text_line->height - height) > 0.00001) {
    text_line->height = height;
    text_line_dirty_cache(text_line);
  }
}

/** Creates a new TextLine object from its components.
 * @param string the string to display
 * @param font the font to display the string with.
 * @param height the height of the font, in cm from baseline to baseline.
 */
TextLine *
text_line_new(const gchar *string, DiaFont *font, real height)
{
  TextLine *text_line = g_new0(TextLine, 1);

  text_line_set_string(text_line, string);
  text_line_set_font(text_line, font);
  text_line_set_height(text_line, height);

  return text_line;
}

TextLine *
text_line_copy(const TextLine *text_line)
{
  return text_line_new(text_line->chars, text_line->font, text_line->height);
}

/** Destroy a text_line object, deallocating all memory used and unreffing
 * reffed objects.
 * @param text_line the object to kill.
 */
void
text_line_destroy(TextLine *text_line)
{
  if (text_line->chars != NULL) {
    g_free(text_line->chars);
  }
  if (text_line->font != NULL) {
    dia_font_unref(text_line->font);
  }
  clear_layout_offset (text_line);
  g_free (text_line->offsets);
  g_free(text_line);
}

/** Calculate the bounding box size of this object.  Since a text object has no
 * position or alignment, this collapses to just a size. 
 * @param text_line
 * @param size A place to store the width and height of the text.
 */
void
text_line_calc_boundingbox_size(TextLine *text_line, Point *size)
{
  text_line_cache_values(text_line);

  size->x = text_line->width;
  size->y = text_line->ascent + text_line->descent;
}

gchar *
text_line_get_string(const TextLine *text_line)
{
  return text_line->chars;
}

DiaFont *
text_line_get_font(const TextLine *text_line)
{
  return text_line->font;
}

real 
text_line_get_height(const TextLine *text_line)
{
  return text_line->height;
}

real
text_line_get_width(const TextLine *text_line)
{
  text_line_cache_values((TextLine *)text_line);
  return text_line->width;
}

real
text_line_get_ascent(const TextLine *text_line)
{
  text_line_cache_values((TextLine *)text_line);
  return text_line->ascent;
}

real
text_line_get_descent(const TextLine *text_line)
{
  text_line_cache_values((TextLine *)text_line);
  return text_line->descent;
}

/** Return the amount this text line would need to be shifted in order to
 * implement the given alignment.
 * @param text_line a line of text
 * @param alignment how to align it.
 * @returns The amount (in diagram lengths) to shift the x positiion of
 * rendering this such that it looks aligned when printed with x at the left.
 * Always a positive number.
 */
real
text_line_get_alignment_adjustment(TextLine *text_line, Alignment alignment)
{
  text_line_cache_values(text_line);
  switch (alignment) {
      case ALIGN_CENTER:
	return text_line->width / 2;
      case ALIGN_RIGHT:
	return text_line->width;
      default:
	return 0.0;
   }  
}

/* **** Private functions **** */
/** Mark this object as needing update before usage. 
 * @param text_line the object that has changed.
 */
static void
text_line_dirty_cache(TextLine *text_line)
{
  text_line->clean = FALSE;
}

static void
clear_layout_offset (TextLine *text_line)
{
  if (text_line->layout_offsets != NULL) {
    GSList *runs = text_line->layout_offsets->runs;

    for (; runs != NULL; runs = g_slist_next(runs)) {
      PangoGlyphItem *run = (PangoGlyphItem *) runs->data;

      g_free(run->glyphs->glyphs);
      g_free(run->glyphs);
    }
    g_slist_free(runs);
    g_free(text_line->layout_offsets);
    text_line->layout_offsets = NULL;
  }
}

static void
text_line_cache_values(TextLine *text_line)
{
  if (!text_line->clean ||
      text_line->chars != text_line->chars_cache ||
      text_line->font != text_line->font_cache ||
      text_line->height != text_line->height_cache) {
    int n_offsets;

    if (text_line->offsets != NULL) {
      g_free(text_line->offsets);
      text_line->offsets = NULL;
    }
    clear_layout_offset (text_line);

    if (text_line->chars == NULL ||
	text_line->chars[0] == '\0') {
      /* caclculate reasonable ascent/decent even for empty string */
      text_line->offsets = 
        dia_font_get_sizes("XjgM149", text_line->font, text_line->height,
			   &text_line->width, &text_line->ascent, 
			   &text_line->descent, &n_offsets,
			   &text_line->layout_offsets);
      clear_layout_offset (text_line);
      g_free (text_line->offsets);
      text_line->offsets = g_new (real,0); /* another way to assign NULL;) */
      text_line->width = 0;
    } else {
      text_line->offsets = 
	dia_font_get_sizes(text_line->chars, text_line->font, text_line->height,
			   &text_line->width, &text_line->ascent, 
			   &text_line->descent, &n_offsets, 
			   &text_line->layout_offsets);
    }
    text_line->clean = TRUE;
    text_line->chars_cache = text_line->chars;
    text_line->font_cache = text_line->font;
    text_line->height_cache = text_line->height;
  }
}

/** Adjust a line of glyphs to match the sizes stored in the TextLine
 * @param line The TextLine object that corresponds to the glyphs.
 * @param glyphs The one set of glyphs contained in layout created for
 * this TextLine during rendering.  The values in here will be changed.
 * @param scale The relative height of the font in glyphs.
 */
void
text_line_adjust_glyphs(TextLine *line, PangoGlyphString *glyphs, real scale)
{
  int i;

  for (i = 0; i < glyphs->num_glyphs; i++) {
/*
    fprintf(stderr, "Glyph %d: width %d, offset %f, textwidth %f\n",
	   i, new_glyphs->glyphs[i].geometry.width, line->offsets[i],
	   line->offsets[i] * scale * 20.0 * PANGO_SCALE);
*/
    glyphs->glyphs[i].geometry.width =
      (int)(line->offsets[i] * scale * 20.0 * PANGO_SCALE);
  }
}

/** Adjust a layout line to match the more fine-grained values stored in the
 * textline.  This circumvents the rounding errors in Pango and ensures a
 * linear scaling for zooming and export filters.
 * @param line The TextLine object that corresponds to the glyphs.
 * @param layoutline The one set of glyphs contained in the TextLine's layout.
 * @param scale The relative height of the font in glyphs.
 * @return An adjusted glyphstring, which should be freed by the caller.
 */
void
text_line_adjust_layout_line(TextLine *line, PangoLayoutLine *layoutline,
			     real scale)
{
  GSList *layoutruns = layoutline->runs;
  GSList *runs;

  if (line->layout_offsets == NULL) {
    return;
  }

  runs = line->layout_offsets->runs;

  if (g_slist_length(runs) != g_slist_length(layoutruns)) {
    fprintf(stderr, "Runs length error: %d != %d\n",
	   g_slist_length(line->layout_offsets->runs),
	   g_slist_length(layoutline->runs));
  }
  for (; runs != NULL && layoutruns != NULL; runs = g_slist_next(runs),
	 layoutruns = g_slist_next(layoutruns)) {
    PangoGlyphString *glyphs = ((PangoLayoutRun *) runs->data)->glyphs;
    PangoGlyphString *layoutglyphs =
      ((PangoLayoutRun *) layoutruns->data)->glyphs;
    int i;

    for (i = 0; i < glyphs->num_glyphs && i < layoutglyphs->num_glyphs; i++) {
      layoutglyphs->glyphs[i].geometry.width =
	(int)(glyphs->glyphs[i].geometry.width * scale / 20.0);
      layoutglyphs->glyphs[i].geometry.x_offset =
	(int)(glyphs->glyphs[i].geometry.x_offset * scale / 20.0);
      layoutglyphs->glyphs[i].geometry.y_offset =
	(int)(glyphs->glyphs[i].geometry.y_offset * scale / 20.0);
    }
    if (glyphs->num_glyphs != layoutglyphs->num_glyphs) {
      fprintf(stderr, "Glyph length error: %d != %d\n", 
	     glyphs->num_glyphs, layoutglyphs->num_glyphs);
    }
  }
}