summaryrefslogtreecommitdiff
path: root/pango/shape.c
blob: 3020007a5a46723e2827d7c36699ecc7bc0ae1ec (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
/* Pango
 * shape.c: Convert characters into glyphs.
 *
 * Copyright (C) 1999 Red Hat Software
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library 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
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */

#include <config.h>
#include <pango/pango-glyph.h>
#include <pango/pango-engine-private.h>

/**
 * pango_shape:
 * @text:      the text to process
 * @length:    the length (in bytes) of @text
 * @analysis:  #PangoAnalysis structure from pango_itemize()
 * @glyphs:    glyph string in which to store results
 *
 * Given a segment of text and the corresponding 
 * #PangoAnalysis structure returned from pango_itemize(),
 * convert the characters into glyphs. You may also pass
 * in only a substring of the item from pango_itemize().
 */
void
pango_shape (const gchar      *text, 
             gint              length, 
             PangoAnalysis    *analysis,
             PangoGlyphString *glyphs)
{
  int i;
  int last_cluster = -1;

  if (G_LIKELY (analysis->shape_engine && analysis->font))
    {
      _pango_engine_shape_shape (analysis->shape_engine, analysis->font,
				 text, length, analysis, glyphs);

      if (G_UNLIKELY (glyphs->num_glyphs == 0))
        {
	  /* If a font has been correctly chosen, but no glyphs are output,
	   * there's probably something wrong with the shaper.  Trying to be
	   * informative, we print out the font description, but to not 
	   * flood the terminal with zillions of the message, we set a flag
	   * on the font to only err once per font.
	   */
	  static GQuark warned_quark = 0;

	  if (!warned_quark)
	    warned_quark = g_quark_from_static_string ("pango-shaper-warned");
	  
	  if (!g_object_get_qdata (G_OBJECT (analysis->font), warned_quark))
	    {
	      PangoFontDescription *desc;
	      char *s;

	      desc = pango_font_describe (analysis->font);
	      s = pango_font_description_to_string (desc);
	      pango_font_description_free (desc);

	      g_warning ("shape engine failure, expect ugly output. the offending font is `%s'", s);

	      g_free (s);

	      g_object_set_qdata_full (G_OBJECT (analysis->font), warned_quark,
				       GINT_TO_POINTER (1), NULL);
	    }
	}
    }
  else
    {
      static struct {
        guint font : 1; 
	guint shape_engine : 1;
      } warned = { FALSE, FALSE };
      
      if (!analysis->shape_engine && !warned.shape_engine)
        {
	  g_critical ("pango_shape called with analysis->shape_engine == NULL");
	  warned.font = TRUE;
	}
      if (!analysis->font && !warned.font)
        {
	  g_critical ("pango_shape called with analysis->font == NULL");
	  warned.font = TRUE;
	}

      glyphs->num_glyphs = 0;
    }

  if (!glyphs->num_glyphs)
    {
      /* If failed to get glyphs, put a whitespace glyph per character
       */
      pango_glyph_string_set_size (glyphs, g_utf8_strlen (text, length));

      for (i = 0; i < glyphs->num_glyphs; i++)
        {
	  glyphs->glyphs[i].glyph = 0;
	  
	  glyphs->glyphs[i].geometry.x_offset = 0;
	  glyphs->glyphs[i].geometry.y_offset = 0;
	  glyphs->glyphs[i].geometry.width = 10 * PANGO_SCALE;
	  
	  glyphs->log_clusters[i] = i;
	}
    }

  /* Set glyphs[i].attr.is_cluster_start based on log_clusters[]
   */
  for (i = 0; i < glyphs->num_glyphs; i++)
    {
      if (glyphs->log_clusters[i] != last_cluster)
	{
	  glyphs->glyphs[i].attr.is_cluster_start = TRUE;
	  last_cluster = glyphs->log_clusters[i];
	}
      else
	glyphs->glyphs[i].attr.is_cluster_start = FALSE;
    }
}