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;
94 G_DEFINE_BOXED_TYPE (PangoGlyphString, pango_glyph_string,
95 pango_glyph_string_copy,
96 pango_glyph_string_free);
99 * pango_glyph_string_copy:
100 * @string: a #PangoGlyphString, may be %NULL
102 * Copy a glyph string and associated storage.
104 * Return value: the newly allocated #PangoGlyphString, which
105 * should be freed with pango_glyph_string_free(),
106 * or %NULL if @string was %NULL.
109 pango_glyph_string_copy (PangoGlyphString *string)
111 PangoGlyphString *new_string;
116 new_string = g_slice_new (PangoGlyphString);
118 *new_string = *string;
120 new_string->glyphs = g_memdup (string->glyphs,
121 string->space * sizeof (PangoGlyphInfo));
122 new_string->log_clusters = g_memdup (string->log_clusters,
123 string->space * sizeof (gint));
129 * pango_glyph_string_free:
130 * @string: a #PangoGlyphString, may be %NULL
132 * Free a glyph string and associated storage.
135 pango_glyph_string_free (PangoGlyphString *string)
140 g_free (string->glyphs);
141 g_free (string->log_clusters);
142 g_slice_free (PangoGlyphString, string);
146 * pango_glyph_string_extents_range:
147 * @glyphs: a #PangoGlyphString
148 * @start: start index
149 * @end: end index (the range is the set of bytes with
150 indices such that start <= index < end)
151 * @font: a #PangoFont
152 * @ink_rect: rectangle used to store the extents of the glyph string range as drawn
153 * or %NULL to indicate that the result is not needed.
154 * @logical_rect: rectangle used to store the logical extents of the glyph string range
155 * or %NULL to indicate that the result is not needed.
157 * Computes the extents of a sub-portion of a glyph string. The extents are
158 * relative to the start of the glyph string range (the origin of their
159 * coordinate system is at the start of the range, not at the start of the entire
163 pango_glyph_string_extents_range (PangoGlyphString *glyphs,
167 PangoRectangle *ink_rect,
168 PangoRectangle *logical_rect)
173 /* Note that the handling of empty rectangles for ink
174 * and logical rectangles is different. A zero-height ink
175 * rectangle makes no contribution to the overall ink rect,
176 * while a zero-height logical rect still reserves horizontal
177 * width. Also, we may return zero-width, positive height
178 * logical rectangles, while we'll never do that for the
181 g_return_if_fail (start <= end);
183 if (G_UNLIKELY (!ink_rect && !logical_rect))
191 ink_rect->height = 0;
198 logical_rect->width = 0;
199 logical_rect->height = 0;
202 for (i = start; i < end; i++)
204 PangoRectangle glyph_ink;
205 PangoRectangle glyph_logical;
207 PangoGlyphGeometry *geometry = &glyphs->glyphs[i].geometry;
209 pango_font_get_glyph_extents (font, glyphs->glyphs[i].glyph,
210 ink_rect ? &glyph_ink : NULL,
211 logical_rect ? &glyph_logical : NULL);
213 if (ink_rect && glyph_ink.width != 0 && glyph_ink.height != 0)
215 if (ink_rect->width == 0 || ink_rect->height == 0)
217 ink_rect->x = x_pos + glyph_ink.x + geometry->x_offset;
218 ink_rect->width = glyph_ink.width;
219 ink_rect->y = glyph_ink.y + geometry->y_offset;
220 ink_rect->height = glyph_ink.height;
226 new_x = MIN (ink_rect->x, x_pos + glyph_ink.x + geometry->x_offset);
227 ink_rect->width = MAX (ink_rect->x + ink_rect->width,
228 x_pos + glyph_ink.x + glyph_ink.width + geometry->x_offset) - new_x;
231 new_y = MIN (ink_rect->y, glyph_ink.y + geometry->y_offset);
232 ink_rect->height = MAX (ink_rect->y + ink_rect->height,
233 glyph_ink.y + glyph_ink.height + geometry->y_offset) - new_y;
240 logical_rect->width += geometry->width;
244 logical_rect->y = glyph_logical.y;
245 logical_rect->height = glyph_logical.height;
249 int new_y = MIN (logical_rect->y, glyph_logical.y);
250 logical_rect->height = MAX (logical_rect->y + logical_rect->height,
251 glyph_logical.y + glyph_logical.height) - new_y;
252 logical_rect->y = new_y;
256 x_pos += geometry->width;
261 * pango_glyph_string_extents:
262 * @glyphs: a #PangoGlyphString
263 * @font: a #PangoFont
264 * @ink_rect: (out) (allow-none): rectangle used to store the extents of the glyph string
265 * as drawn or %NULL to indicate that the result is not needed.
266 * @logical_rect: (out) (allow-none): rectangle used to store the logical extents of the
267 * glyph string or %NULL to indicate that the result is not needed.
269 * Compute the logical and ink extents of a glyph string. See the documentation
270 * for pango_font_get_glyph_extents() for details about the interpretation
274 pango_glyph_string_extents (PangoGlyphString *glyphs,
276 PangoRectangle *ink_rect,
277 PangoRectangle *logical_rect)
279 pango_glyph_string_extents_range (glyphs, 0, glyphs->num_glyphs,
280 font, ink_rect, logical_rect);
284 * pango_glyph_string_get_width:
285 * @glyphs: a #PangoGlyphString
287 * Computes the logical width of the glyph string as can also be computed
288 * using pango_glyph_string_extents(). However, since this only computes the
289 * width, it's much faster. This is in fact only a convenience function that
290 * computes the sum of geometry.width for each glyph in the @glyphs.
292 * Return value: the logical width of the glyph string.
297 pango_glyph_string_get_width (PangoGlyphString *glyphs)
302 for (i = 0; i < glyphs->num_glyphs; i++)
303 width += glyphs->glyphs[i].geometry.width;
309 * pango_glyph_string_get_logical_widths:
310 * @glyphs: a #PangoGlyphString
311 * @text: the text corresponding to the glyphs
312 * @length: the length of @text, in bytes
313 * @embedding_level: the embedding level of the string
314 * @logical_widths: an array whose length is the number of characters in
315 * text (equal to g_utf8_strlen (text, length) unless
316 * text has NUL bytes)
317 * to be filled in with the resulting character widths.
319 * Given a #PangoGlyphString resulting from pango_shape() and the corresponding
320 * text, determine the screen width corresponding to each character. When
321 * multiple characters compose a single cluster, the width of the entire
322 * cluster is divided equally among the characters.
324 * See also pango_glyph_item_get_logical_widths().
327 pango_glyph_string_get_logical_widths (PangoGlyphString *glyphs,
333 /* Build a PangoGlyphItem and call the other API */
334 PangoItem item = {0, length, pango_utf8_strlen (text, length),
336 embedding_level, PANGO_GRAVITY_AUTO, 0,
337 PANGO_SCRIPT_UNKNOWN, NULL,
339 PangoGlyphItem glyph_item = {&item, glyphs};
341 pango_glyph_item_get_logical_widths (&glyph_item, text, logical_widths);
344 /* The initial implementation here is script independent,
345 * but it might actually need to be virtualized into the
346 * rendering modules. Otherwise, we probably will end up
347 * enforcing unnatural cursor behavior for some languages.
349 * The only distinction that Uniscript makes is whether
350 * cursor positioning is allowed within clusters or not.
354 * pango_glyph_string_index_to_x:
355 * @glyphs: the glyphs return from pango_shape()
356 * @text: the text for the run
357 * @length: the number of bytes (not characters) in @text.
358 * @analysis: the analysis information return from pango_itemize()
359 * @index_: the byte index within @text
360 * @trailing: whether we should compute the result for the beginning (%FALSE)
361 * or end (%TRUE) of the character.
362 * @x_pos: location to store result
364 * Converts from character position to x position. (X position
365 * is measured from the left edge of the run). Character positions
366 * are computed by dividing up each cluster into equal portions.
370 pango_glyph_string_index_to_x (PangoGlyphString *glyphs,
373 PangoAnalysis *analysis,
383 int start_index = -1;
386 int cluster_chars = 0;
387 int cluster_offset = 0;
391 g_return_if_fail (glyphs != NULL);
392 g_return_if_fail (length >= 0);
393 g_return_if_fail (length == 0 || text != NULL);
395 if (!x_pos) /* Allow the user to do the useless */
398 if (glyphs->num_glyphs == 0)
404 /* Calculate the starting and ending character positions
405 * and x positions for the cluster
407 if (analysis->level % 2) /* Right to left */
409 for (i = glyphs->num_glyphs - 1; i >= 0; i--)
410 width += glyphs->glyphs[i].geometry.width;
412 for (i = glyphs->num_glyphs - 1; i >= 0; i--)
414 if (glyphs->log_clusters[i] > index)
416 end_index = glyphs->log_clusters[i];
421 if (glyphs->log_clusters[i] != start_index)
423 start_index = glyphs->log_clusters[i];
427 width -= glyphs->glyphs[i].geometry.width;
430 else /* Left to right */
432 for (i = 0; i < glyphs->num_glyphs; i++)
434 if (glyphs->log_clusters[i] > index)
436 end_index = glyphs->log_clusters[i];
441 if (glyphs->log_clusters[i] != start_index)
443 start_index = glyphs->log_clusters[i];
447 width += glyphs->glyphs[i].geometry.width;
454 end_xpos = (analysis->level % 2) ? 0 : width;
457 /* Calculate offset of character within cluster */
459 p = text + start_index;
460 while (p < text + end_index)
462 if (p < text + index)
465 p = g_utf8_next_char (p);
471 if (G_UNLIKELY (!cluster_chars)) /* pedantic */
477 *x_pos = ((cluster_chars - cluster_offset) * start_xpos +
478 cluster_offset * end_xpos) / cluster_chars;
482 * pango_glyph_string_x_to_index:
483 * @glyphs: the glyphs returned from pango_shape()
484 * @text: the text for the run
485 * @length: the number of bytes (not characters) in text.
486 * @analysis: the analysis information return from pango_itemize()
487 * @x_pos: the x offset (in Pango units)
488 * @index_: location to store calculated byte index within @text
489 * @trailing: location to store a boolean indicating
490 * whether the user clicked on the leading or trailing
491 * edge of the character.
493 * Convert from x offset to character position. Character positions
494 * are computed by dividing up each cluster into equal portions.
495 * In scripts where positioning within a cluster is not allowed
496 * (such as Thai), the returned value may not be a valid cursor
497 * position; the caller must combine the result with the logical
498 * attributes for the text to compute the valid cursor position.
501 pango_glyph_string_x_to_index (PangoGlyphString *glyphs,
504 PangoAnalysis *analysis,
514 int start_index = -1;
517 int cluster_chars = 0;
520 gboolean found = FALSE;
522 /* Find the cluster containing the position */
526 if (analysis->level % 2) /* Right to left */
528 for (i = glyphs->num_glyphs - 1; i >= 0; i--)
529 width += glyphs->glyphs[i].geometry.width;
531 for (i = glyphs->num_glyphs - 1; i >= 0; i--)
533 if (glyphs->log_clusters[i] != start_index)
537 end_index = glyphs->log_clusters[i];
543 start_index = glyphs->log_clusters[i];
548 width -= glyphs->glyphs[i].geometry.width;
550 if (width <= x_pos && x_pos < width + glyphs->glyphs[i].geometry.width)
554 else /* Left to right */
556 for (i = 0; i < glyphs->num_glyphs; i++)
558 if (glyphs->log_clusters[i] != start_index)
562 end_index = glyphs->log_clusters[i];
568 start_index = glyphs->log_clusters[i];
573 if (width <= x_pos && x_pos < width + glyphs->glyphs[i].geometry.width)
576 width += glyphs->glyphs[i].geometry.width;
583 end_xpos = (analysis->level % 2) ? 0 : width;
586 /* Calculate number of chars within cluster */
587 p = text + start_index;
588 while (p < text + end_index)
590 p = g_utf8_next_char (p);
594 if (start_xpos == end_xpos)
597 *index = start_index;
603 double cp = ((double)(x_pos - start_xpos) * cluster_chars) / (end_xpos - start_xpos);
605 /* LTR and right-to-left have to be handled separately
606 * here because of the edge condition when we are exactly
607 * at a pixel boundary; end_xpos goes with the next
608 * character for LTR, with the previous character for RTL.
610 if (start_xpos < end_xpos) /* Left-to-right */
614 char *p = text + start_index;
619 p = g_utf8_next_char (p);
627 *trailing = (cp - (int)cp >= 0.5) ? TRUE : FALSE;
629 else /* Right-to-left */
633 char *p = text + start_index;
638 p = g_utf8_next_char (p);
647 double cp_flip = cluster_chars - cp;
648 *trailing = (cp_flip - (int)cp_flip >= 0.5) ? FALSE : TRUE;