4 * Copyright (C) 1999 Red Hat Software
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Library General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Library General Public License for more details.
16 * You should have received a copy of the GNU Library General Public
17 * License along with this library; if not, write to the
18 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 * Boston, MA 02111-1307, USA.
24 #include "pango-glyph.h"
25 #include "pango-font.h"
26 #include "pango-impl-utils.h"
29 * pango_glyph_string_new:
31 * Create a new #PangoGlyphString.
33 * Return value: the newly allocated #PangoGlyphString, which
34 * should be freed with pango_glyph_string_free().
37 pango_glyph_string_new (void)
39 PangoGlyphString *string = g_slice_new (PangoGlyphString);
41 string->num_glyphs = 0;
43 string->glyphs = NULL;
44 string->log_clusters = NULL;
50 * pango_glyph_string_set_size:
51 * @string: a #PangoGlyphString.
52 * @new_len: the new length of the string.
54 * Resize a glyph string to the given length.
57 pango_glyph_string_set_size (PangoGlyphString *string, gint new_len)
59 g_return_if_fail (new_len >= 0);
61 while (new_len > string->space)
63 if (string->space == 0)
69 const guint max_space =
70 MIN (G_MAXINT, G_MAXSIZE / MAX (sizeof(PangoGlyphInfo), sizeof(gint)));
72 guint more_space = (guint)string->space * 2;
74 if (more_space > max_space)
76 more_space = max_space;
78 if ((guint)new_len > max_space)
80 g_error ("%s: failed to allocate glyph string of length %i\n",
85 string->space = more_space;
89 string->glyphs = g_realloc (string->glyphs, string->space * sizeof (PangoGlyphInfo));
90 string->log_clusters = g_realloc (string->log_clusters, string->space * sizeof (gint));
91 string->num_glyphs = new_len;
95 pango_glyph_string_get_type (void)
97 static GType our_type = 0;
99 if (G_UNLIKELY (our_type == 0))
100 our_type = g_boxed_type_register_static (I_("PangoGlyphString"),
101 (GBoxedCopyFunc)pango_glyph_string_copy,
102 (GBoxedFreeFunc)pango_glyph_string_free);
108 * pango_glyph_string_copy:
109 * @string: a #PangoGlyphString, may be %NULL
111 * Copy a glyph string and associated storage.
113 * Return value: the newly allocated #PangoGlyphString, which
114 * should be freed with pango_glyph_string_free(),
115 * or %NULL if @string was %NULL.
118 pango_glyph_string_copy (PangoGlyphString *string)
120 PangoGlyphString *new_string;
125 new_string = g_slice_new (PangoGlyphString);
127 *new_string = *string;
129 new_string->glyphs = g_memdup (string->glyphs,
130 string->space * sizeof (PangoGlyphInfo));
131 new_string->log_clusters = g_memdup (string->log_clusters,
132 string->space * sizeof (gint));
138 * pango_glyph_string_free:
139 * @string: a #PangoGlyphString, may be %NULL
141 * Free a glyph string and associated storage.
144 pango_glyph_string_free (PangoGlyphString *string)
149 g_free (string->glyphs);
150 g_free (string->log_clusters);
151 g_slice_free (PangoGlyphString, string);
155 * pango_glyph_string_extents_range:
156 * @glyphs: a #PangoGlyphString
157 * @start: start index
158 * @end: end index (the range is the set of bytes with
159 indices such that start <= index < end)
160 * @font: a #PangoFont
161 * @ink_rect: rectangle used to store the extents of the glyph string range as drawn
162 * or %NULL to indicate that the result is not needed.
163 * @logical_rect: rectangle used to store the logical extents of the glyph string range
164 * or %NULL to indicate that the result is not needed.
166 * Computes the extents of a sub-portion of a glyph string. The extents are
167 * relative to the start of the glyph string range (the origin of their
168 * coordinate system is at the start of the range, not at the start of the entire
172 pango_glyph_string_extents_range (PangoGlyphString *glyphs,
176 PangoRectangle *ink_rect,
177 PangoRectangle *logical_rect)
182 /* Note that the handling of empty rectangles for ink
183 * and logical rectangles is different. A zero-height ink
184 * rectangle makes no contribution to the overall ink rect,
185 * while a zero-height logical rect still reserves horizontal
186 * width. Also, we may return zero-width, positive height
187 * logical rectangles, while we'll never do that for the
190 g_return_if_fail (start <= end);
192 if (G_UNLIKELY (!ink_rect && !logical_rect))
200 ink_rect->height = 0;
207 logical_rect->width = 0;
208 logical_rect->height = 0;
211 for (i = start; i < end; i++)
213 PangoRectangle glyph_ink;
214 PangoRectangle glyph_logical;
216 PangoGlyphGeometry *geometry = &glyphs->glyphs[i].geometry;
218 pango_font_get_glyph_extents (font, glyphs->glyphs[i].glyph,
219 ink_rect ? &glyph_ink : NULL,
220 logical_rect ? &glyph_logical : NULL);
222 if (ink_rect && glyph_ink.width != 0 && glyph_ink.height != 0)
224 if (ink_rect->width == 0 || ink_rect->height == 0)
226 ink_rect->x = x_pos + glyph_ink.x + geometry->x_offset;
227 ink_rect->width = glyph_ink.width;
228 ink_rect->y = glyph_ink.y + geometry->y_offset;
229 ink_rect->height = glyph_ink.height;
235 new_x = MIN (ink_rect->x, x_pos + glyph_ink.x + geometry->x_offset);
236 ink_rect->width = MAX (ink_rect->x + ink_rect->width,
237 x_pos + glyph_ink.x + glyph_ink.width + geometry->x_offset) - new_x;
240 new_y = MIN (ink_rect->y, glyph_ink.y + geometry->y_offset);
241 ink_rect->height = MAX (ink_rect->y + ink_rect->height,
242 glyph_ink.y + glyph_ink.height + geometry->y_offset) - new_y;
249 logical_rect->width += geometry->width;
253 logical_rect->y = glyph_logical.y;
254 logical_rect->height = glyph_logical.height;
258 int new_y = MIN (logical_rect->y, glyph_logical.y);
259 logical_rect->height = MAX (logical_rect->y + logical_rect->height,
260 glyph_logical.y + glyph_logical.height) - new_y;
261 logical_rect->y = new_y;
265 x_pos += geometry->width;
270 * pango_glyph_string_extents:
271 * @glyphs: a #PangoGlyphString
272 * @font: a #PangoFont
273 * @ink_rect: rectangle used to store the extents of the glyph string as drawn
274 * or %NULL to indicate that the result is not needed.
275 * @logical_rect: rectangle used to store the logical extents of the glyph string
276 * or %NULL to indicate that the result is not needed.
278 * Compute the logical and ink extents of a glyph string. See the documentation
279 * for pango_font_get_glyph_extents() for details about the interpretation
283 pango_glyph_string_extents (PangoGlyphString *glyphs,
285 PangoRectangle *ink_rect,
286 PangoRectangle *logical_rect)
288 pango_glyph_string_extents_range (glyphs, 0, glyphs->num_glyphs,
289 font, ink_rect, logical_rect);
293 * pango_glyph_string_get_width:
294 * @glyphs: a #PangoGlyphString
296 * Computes the logical width of the glyph string as can also be computed
297 * using pango_glyph_string_extents(). However, since this only computes the
298 * width, it's much faster. This is in fact only a convenience function that
299 * computes the sum of geometry.width for each glyph in the @glyphs.
301 * Return value: the logical width of the glyph string.
306 pango_glyph_string_get_width (PangoGlyphString *glyphs)
311 for (i = 0; i < glyphs->num_glyphs; i++)
312 width += glyphs->glyphs[i].geometry.width;
318 * pango_glyph_string_get_logical_widths:
319 * @glyphs: a #PangoGlyphString
320 * @text: the text corresponding to the glyphs
321 * @length: the length of @text, in bytes
322 * @embedding_level: the embedding level of the string
323 * @logical_widths: an array whose length is the number of characters in
324 * text (equal to g_utf8_strlen (text, length) unless
325 * text has NUL bytes)
326 * to be filled in with the resulting character widths.
328 * Given a #PangoGlyphString resulting from pango_shape() and the corresponding
329 * text, determine the screen width corresponding to each character. When
330 * multiple characters compose a single cluster, the width of the entire
331 * cluster is divided equally among the characters.
333 * See also pango_glyph_item_get_logical_widths().
336 pango_glyph_string_get_logical_widths (PangoGlyphString *glyphs,
342 /* Build a PangoGlyphItem and call the other API */
343 PangoItem item = {0, length, pango_utf8_strlen (text, length),
345 embedding_level, PANGO_GRAVITY_AUTO, 0,
346 PANGO_SCRIPT_UNKNOWN, NULL,
348 PangoGlyphItem glyph_item = {&item, glyphs};
350 return pango_glyph_item_get_logical_widths (&glyph_item, text, logical_widths);
353 /* The initial implementation here is script independent,
354 * but it might actually need to be virtualized into the
355 * rendering modules. Otherwise, we probably will end up
356 * enforcing unnatural cursor behavior for some languages.
358 * The only distinction that Uniscript makes is whether
359 * cursor positioning is allowed within clusters or not.
363 * pango_glyph_string_index_to_x:
364 * @glyphs: the glyphs return from pango_shape()
365 * @text: the text for the run
366 * @length: the number of bytes (not characters) in @text.
367 * @analysis: the analysis information return from pango_itemize()
368 * @index_: the byte index within @text
369 * @trailing: whether we should compute the result for the beginning (%FALSE)
370 * or end (%TRUE) of the character.
371 * @x_pos: location to store result
373 * Converts from character position to x position. (X position
374 * is measured from the left edge of the run). Character positions
375 * are computed by dividing up each cluster into equal portions.
379 pango_glyph_string_index_to_x (PangoGlyphString *glyphs,
382 PangoAnalysis *analysis,
392 int start_index = -1;
395 int cluster_chars = 0;
396 int cluster_offset = 0;
400 g_return_if_fail (glyphs != NULL);
401 g_return_if_fail (length >= 0);
402 g_return_if_fail (length == 0 || text != NULL);
404 if (!x_pos) /* Allow the user to do the useless */
407 if (glyphs->num_glyphs == 0)
413 /* Calculate the starting and ending character positions
414 * and x positions for the cluster
416 if (analysis->level % 2) /* Right to left */
418 for (i = glyphs->num_glyphs - 1; i >= 0; i--)
419 width += glyphs->glyphs[i].geometry.width;
421 for (i = glyphs->num_glyphs - 1; i >= 0; i--)
423 if (glyphs->log_clusters[i] > index)
425 end_index = glyphs->log_clusters[i];
430 if (glyphs->log_clusters[i] != start_index)
432 start_index = glyphs->log_clusters[i];
436 width -= glyphs->glyphs[i].geometry.width;
439 else /* Left to right */
441 for (i = 0; i < glyphs->num_glyphs; i++)
443 if (glyphs->log_clusters[i] > index)
445 end_index = glyphs->log_clusters[i];
450 if (glyphs->log_clusters[i] != start_index)
452 start_index = glyphs->log_clusters[i];
456 width += glyphs->glyphs[i].geometry.width;
463 end_xpos = (analysis->level % 2) ? 0 : width;
466 /* Calculate offset of character within cluster */
468 p = text + start_index;
469 while (p < text + end_index)
471 if (p < text + index)
474 p = g_utf8_next_char (p);
480 if (G_UNLIKELY (!cluster_chars)) /* pedantic */
486 *x_pos = ((cluster_chars - cluster_offset) * start_xpos +
487 cluster_offset * end_xpos) / cluster_chars;
491 * pango_glyph_string_x_to_index:
492 * @glyphs: the glyphs returned from pango_shape()
493 * @text: the text for the run
494 * @length: the number of bytes (not characters) in text.
495 * @analysis: the analysis information return from pango_itemize()
496 * @x_pos: the x offset (in Pango units)
497 * @index_: location to store calculated byte index within @text
498 * @trailing: location to store a boolean indicating
499 * whether the user clicked on the leading or trailing
500 * edge of the character.
502 * Convert from x offset to character position. Character positions
503 * are computed by dividing up each cluster into equal portions.
504 * In scripts where positioning within a cluster is not allowed
505 * (such as Thai), the returned value may not be a valid cursor
506 * position; the caller must combine the result with the logical
507 * attributes for the text to compute the valid cursor position.
510 pango_glyph_string_x_to_index (PangoGlyphString *glyphs,
513 PangoAnalysis *analysis,
523 int start_index = -1;
526 int cluster_chars = 0;
529 gboolean found = FALSE;
531 /* Find the cluster containing the position */
535 if (analysis->level % 2) /* Right to left */
537 for (i = glyphs->num_glyphs - 1; i >= 0; i--)
538 width += glyphs->glyphs[i].geometry.width;
540 for (i = glyphs->num_glyphs - 1; i >= 0; i--)
542 if (glyphs->log_clusters[i] != start_index)
546 end_index = glyphs->log_clusters[i];
552 start_index = glyphs->log_clusters[i];
557 width -= glyphs->glyphs[i].geometry.width;
559 if (width <= x_pos && x_pos < width + glyphs->glyphs[i].geometry.width)
563 else /* Left to right */
565 for (i = 0; i < glyphs->num_glyphs; i++)
567 if (glyphs->log_clusters[i] != start_index)
571 end_index = glyphs->log_clusters[i];
577 start_index = glyphs->log_clusters[i];
582 if (width <= x_pos && x_pos < width + glyphs->glyphs[i].geometry.width)
585 width += glyphs->glyphs[i].geometry.width;
592 end_xpos = (analysis->level % 2) ? 0 : width;
595 /* Calculate number of chars within cluster */
596 p = text + start_index;
597 while (p < text + end_index)
599 p = g_utf8_next_char (p);
603 if (start_xpos == end_xpos)
606 *index = start_index;
612 double cp = ((double)(x_pos - start_xpos) * cluster_chars) / (end_xpos - start_xpos);
614 /* LTR and right-to-left have to be handled separately
615 * here because of the edge condition when we are exactly
616 * at a pixel boundary; end_xpos goes with the next
617 * character for LTR, with the previous character for RTL.
619 if (start_xpos < end_xpos) /* Left-to-right */
623 char *p = text + start_index;
628 p = g_utf8_next_char (p);
636 *trailing = (cp - (int)cp >= 0.5) ? TRUE : FALSE;
638 else /* Right-to-left */
642 char *p = text + start_index;
647 p = g_utf8_next_char (p);
656 double cp_flip = cluster_chars - cp;
657 *trailing = (cp_flip - (int)cp_flip >= 0.5) ? FALSE : TRUE;