File: textline.c

package info (click to toggle)
dia 0.98%2Bgit20260221-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 52,136 kB
  • sloc: ansic: 156,533; xml: 14,063; python: 6,250; cpp: 3,647; sh: 447; perl: 137; makefile: 33
file content (388 lines) | stat: -rw-r--r-- 10,257 bytes parent folder | download
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
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
/* 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 <glib/gi18n-lib.h>

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <glib.h>
#include <math.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);


/**
 * text_line_set_string:
 * @text_line: The object to change.
 * @string: The string to display.  This string will be copied.
 *
 * Sets this object to display a particular string.
 */
void
text_line_set_string (TextLine *text_line, const char *string)
{
  if (g_set_str (&text_line->chars, string)) {
    text_line_dirty_cache (text_line);
  }
}


/**
 * text_line_set_font:
 * @text_line: The object to change.
 * @font: The font to use for displaying this object.
 *
 * Sets the font used by this object.
 */
void
text_line_set_font (TextLine *text_line, DiaFont *font)
{
  if (g_set_object (&text_line->font, font)) {
    text_line_dirty_cache (text_line);
  }
}


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


/**
 * text_line_new:
 * @string: the string to display
 * @font: the font to display the string with.
 * @height: the height of the font, in cm from baseline to baseline.
 *
 * Creates a new TextLine object from its components.
 */
TextLine *
text_line_new (const char *string, DiaFont *font, double 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;
}


/**
 * text_line_copy:
 * @text_line: The object to copy.
 *
 * Make a deep copy of the given TextLine
 */
TextLine *
text_line_copy (TextLine *text_line)
{
  return text_line_new (text_line->chars,
                        text_line->font,
                        text_line->height);
}


/**
 * text_line_destroy:
 * @text_line: the object to kill.
 *
 * Destroy a text_line object, this is deallocating all memory used and
 * unreffing reffed objects.
 */
void
text_line_destroy (TextLine *text_line)
{
  g_clear_pointer (&text_line->chars, g_free);
  g_clear_object (&text_line->font);
  clear_layout_offset (text_line);
  g_clear_pointer (&text_line->offsets, g_free);
  g_free (text_line);
}


/**
 * text_line_calc_boundingbox_size:
 * @text_line:
 * @size: A place to store the width and height of the text.
 *
 * TextLine bounding box caclulation
 *
 * Calculate the bounding box size of this object. Since a text object has no
 * position or alignment, this collapses to just a size.
 */
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;
}


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


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


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


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


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


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


/**
 * text_line_get_alignment_adjustment:
 * @text_line: a line of text
 * @alignment: how to align it.
 *
 * Calculate #TextLine adjustment for #DiaAlignment
 *
 * Return the amount this text line would need to be shifted in order to
 * implement the given alignment.
 *
 * 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.
 *
 * Since: dawn-of-time
 */
double
text_line_get_alignment_adjustment (TextLine *text_line, DiaAlignment alignment)
{
  text_line_cache_values (text_line);

  switch (alignment) {
    case DIA_ALIGN_CENTRE:
     return text_line->width / 2;
    case DIA_ALIGN_RIGHT:
       return text_line->width;
    case DIA_ALIGN_LEFT:
    default:
     return 0.0;
  }
}

/* **** Private functions **** */

/**
 * text_line_dirty_cache:
 * @text_line: the object that has changed.
 *
 * Mark this object as needing update before usage.
 *
 * Since: dawn-of-time
 */
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_clear_pointer (&run->glyphs->glyphs, g_free);
      g_clear_pointer (&run->glyphs, g_free);
    }
    g_slist_free(runs);
    g_clear_pointer (&text_line->layout_offsets, g_free);
  }
}

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;

    g_clear_pointer (&text_line->offsets, g_free);
    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_clear_pointer (&text_line->offsets, g_free);
      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;
  }
}

/*!
 * \brief Move glyphs to approximate a desired total width
 *
 * 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++) {
/*
    printf("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);
  }
}


/**
 * text_line_adjust_layout_line:
 * @line: The #TextLine object that corresponds to the glyphs.
 * @layoutline: The one set of glyphs contained in the #TextLine's layout.
 * @scale: The relative height of the font in glyphs.
 *
 * 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.
 */
void
text_line_adjust_layout_line (TextLine        *line,
                              PangoLayoutLine *layoutline,
                              double           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)) {
    g_printerr ("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) {
      g_printerr ("Glyph length error: %d != %d\n",
                  glyphs->num_glyphs,
                  layoutglyphs->num_glyphs);
    }
  }
}