~ubuntu-branches/ubuntu/vivid/pango1.0/vivid-proposed

« back to all changes in this revision

Viewing changes to modules/basic/basic-coretext.c

  • Committer: Package Import Robot
  • Author(s): Michael Biebl
  • Date: 2012-03-31 22:00:53 UTC
  • mfrom: (1.8.6) (63.1.16 experimental)
  • Revision ID: package-import@ubuntu.com-20120331220053-7w1irtk74gutqgmi
Tags: 1.30.0-1
* New upstream release.
* debian/watch: Track stable releases again.
* Refreshed and updated patches.
* Bump Standards-Version to 3.9.3.

Show diffs side-by-side

added added

removed removed

Lines of Context:
3
3
 *
4
4
 * Copyright (C) 2005 Imendio AB
5
5
 * Copyright (C) 2010  Kristian Rietveld  <kris@gtk.org>
 
6
 * Copyright (C) 2012  Kristian Rietveld  <kris@lanedo.com>
6
7
 *
7
8
 * This library is free software; you can redistribute it and/or
8
9
 * modify it under the terms of the GNU Library General Public
68
69
  glyphs->glyphs[i].geometry.width = logical_rect.width;
69
70
}
70
71
 
71
 
static void
72
 
basic_engine_shape (PangoEngineShape    *engine,
73
 
                    PangoFont           *font,
74
 
                    const char          *text,
75
 
                    gint                 length,
76
 
                    const PangoAnalysis *analysis,
77
 
                    PangoGlyphString    *glyphs)
 
72
 
 
73
/* The "RunIterator" helps us to iterate over the array of runs that is obtained from
 
74
 * the CoreText type setter. Even though Pango considers the string that is passed to
 
75
 * the shaping engine a single run, CoreText might consider it to consist out of
 
76
 * multiple runs. Because of this, we have an interface around the CoreText array of
 
77
 * runs that works like iterating a single array, which makes our job in the shaping
 
78
 * engine function easier.
 
79
 */
 
80
 
 
81
struct RunIterator
78
82
{
79
 
  const char *p;
80
 
  char *copy;
81
83
  CTLineRef line;
82
84
  CFStringRef cstr;
 
85
  CFArrayRef runs;
 
86
  CFIndex glyph_count;
 
87
 
 
88
  CFIndex total_ct_i;
 
89
  CFIndex ct_i;
 
90
 
 
91
  int current_run_number;
 
92
  CTRunRef current_run;
 
93
  CFIndex *current_indices;
 
94
  const CGGlyph *current_cgglyphs;
 
95
  CTRunStatus current_run_status;
 
96
};
 
97
 
 
98
static void
 
99
run_iterator_free_current_run (struct RunIterator *iter)
 
100
{
 
101
  iter->current_run_number = -1;
 
102
  iter->current_run = NULL;
 
103
  iter->current_cgglyphs = NULL;
 
104
  if (iter->current_indices)
 
105
    free (iter->current_indices);
 
106
  iter->current_indices = NULL;
 
107
}
 
108
 
 
109
static void
 
110
run_iterator_set_current_run (struct RunIterator *iter,
 
111
                              const int           run_number)
 
112
{
 
113
  CFIndex ct_glyph_count;
 
114
 
 
115
  run_iterator_free_current_run (iter);
 
116
 
 
117
  iter->current_run_number = run_number;
 
118
  iter->current_run = CFArrayGetValueAtIndex (iter->runs, run_number);
 
119
  iter->current_run_status = CTRunGetStatus (iter->current_run);
 
120
  iter->current_cgglyphs = CTRunGetGlyphsPtr (iter->current_run);
 
121
 
 
122
  ct_glyph_count = CTRunGetGlyphCount (iter->current_run);
 
123
  iter->current_indices = malloc (sizeof (CFIndex *) * ct_glyph_count);
 
124
  CTRunGetStringIndices (iter->current_run, CFRangeMake (0, ct_glyph_count),
 
125
                         iter->current_indices);
 
126
 
 
127
  iter->ct_i = 0;
 
128
}
 
129
 
 
130
static CFIndex
 
131
run_iterator_get_glyph_count (struct RunIterator *iter)
 
132
{
 
133
  CFIndex accumulator = 0;
 
134
  CFIndex i;
 
135
 
 
136
  for (i = 0; i < CFArrayGetCount (iter->runs); i++)
 
137
    accumulator += CTRunGetGlyphCount (CFArrayGetValueAtIndex (iter->runs, i));
 
138
 
 
139
  return accumulator;
 
140
}
 
141
 
 
142
static gboolean
 
143
run_iterator_is_rtl (struct RunIterator *iter)
 
144
{
 
145
  /* Assume run status is equal for all runs? */
 
146
  CTRunStatus run_status = CTRunGetStatus (CFArrayGetValueAtIndex (iter->runs, 0));
 
147
 
 
148
  return run_status & kCTRunStatusRightToLeft;
 
149
}
 
150
 
 
151
static gboolean
 
152
run_iterator_run_is_non_monotonic (struct RunIterator *iter)
 
153
{
 
154
  CTRunStatus run_status = CTRunGetStatus (iter->current_run);
 
155
 
 
156
  return run_status & kCTRunStatusNonMonotonic;
 
157
}
 
158
 
 
159
static gunichar
 
160
run_iterator_get_character (struct RunIterator *iter)
 
161
{
 
162
  return CFStringGetCharacterAtIndex (iter->cstr, iter->current_indices[iter->ct_i]);
 
163
}
 
164
 
 
165
static CGGlyph
 
166
run_iterator_get_cgglyph (struct RunIterator *iter)
 
167
{
 
168
  return iter->current_cgglyphs[iter->ct_i];
 
169
}
 
170
 
 
171
static CFIndex
 
172
run_iterator_get_index (struct RunIterator *iter)
 
173
{
 
174
  return iter->current_indices[iter->ct_i];
 
175
}
 
176
 
 
177
static void
 
178
run_iterator_create (struct RunIterator *iter,
 
179
                     const char         *text,
 
180
                     const gint          length,
 
181
                     CTFontRef           ctfont)
 
182
{
 
183
  char *copy;
83
184
  CFDictionaryRef attributes;
84
185
  CFAttributedStringRef attstr;
85
 
  PangoCoreTextFont *cfont = PANGO_CORE_TEXT_FONT (font);
86
 
  PangoCoverage *coverage;
87
 
  CFArrayRef runs;
88
 
  CTRunRef run;
89
 
  CTRunStatus run_status;
90
 
  CFIndex i, glyph_count;
91
 
  const CGGlyph *cgglyphs;
92
186
 
93
187
  CFTypeRef keys[] = {
94
188
      (CFTypeRef) kCTFontAttributeName
95
189
  };
96
190
 
97
191
  CFTypeRef values[] = {
98
 
      pango_core_text_font_get_ctfont (cfont)
 
192
      ctfont
99
193
  };
100
194
 
 
195
  /* Initialize RunIterator structure */
 
196
  iter->current_run_number = -1;
 
197
  iter->current_run = NULL;
 
198
  iter->current_indices = NULL;
 
199
  iter->current_cgglyphs = NULL;
 
200
 
 
201
  /* Create CTLine */
101
202
  attributes = CFDictionaryCreate (kCFAllocatorDefault,
102
203
                                   (const void **)keys,
103
204
                                   (const void **)values,
108
209
  copy = g_strndup (text, length + 1);
109
210
  copy[length] = 0;
110
211
 
111
 
  cstr = CFStringCreateWithCString (kCFAllocatorDefault, copy,
112
 
                                    kCFStringEncodingUTF8);
 
212
  iter->cstr = CFStringCreateWithCString (kCFAllocatorDefault, copy,
 
213
                                          kCFStringEncodingUTF8);
113
214
  g_free (copy);
114
215
 
115
216
  attstr = CFAttributedStringCreate (kCFAllocatorDefault,
116
 
                                     cstr,
 
217
                                     iter->cstr,
117
218
                                     attributes);
118
219
 
119
 
  line = CTLineCreateWithAttributedString (attstr);
120
 
 
121
 
  runs = CTLineGetGlyphRuns (line);
122
 
 
123
 
  /* Since Pango divides things into runs already, we assume there is
124
 
   * only a single run in this line.
125
 
   */
126
 
  run = CFArrayGetValueAtIndex (runs, 0);
127
 
  run_status = CTRunGetStatus (run);
128
 
  glyph_count = CTRunGetGlyphCount (run);
129
 
  cgglyphs = CTRunGetGlyphsPtr (run);
130
 
 
131
 
  p = text;
132
 
  pango_glyph_string_set_size (glyphs, glyph_count);
 
220
  iter->line = CTLineCreateWithAttributedString (attstr);
 
221
  iter->runs = CTLineGetGlyphRuns (iter->line);
 
222
 
 
223
  CFRelease (attstr);
 
224
  CFRelease (attributes);
 
225
 
 
226
  iter->total_ct_i = 0;
 
227
  iter->glyph_count = run_iterator_get_glyph_count (iter);
 
228
 
 
229
  /* If CoreText did not render any glyphs for this string (can happen,
 
230
   * e.g. a run solely consisting of a BOM), glyph_count will be zero and
 
231
   * we immediately set the iterator variable to indicate end of glyph list.
 
232
   */
 
233
  if (iter->glyph_count > 0)
 
234
    run_iterator_set_current_run (iter, 0);
 
235
  else
 
236
    iter->total_ct_i = -1;
 
237
}
 
238
 
 
239
static void
 
240
run_iterator_free (struct RunIterator *iter)
 
241
{
 
242
  run_iterator_free_current_run (iter);
 
243
 
 
244
  CFRelease (iter->line);
 
245
  CFRelease (iter->cstr);
 
246
}
 
247
 
 
248
static gboolean
 
249
run_iterator_at_end (struct RunIterator *iter)
 
250
{
 
251
  if (iter->total_ct_i == -1)
 
252
    return TRUE;
 
253
 
 
254
  return FALSE;
 
255
}
 
256
 
 
257
static void
 
258
run_iterator_advance (struct RunIterator *iter)
 
259
{
 
260
  if (iter->total_ct_i >= iter->glyph_count - 1)
 
261
    {
 
262
      run_iterator_free_current_run (iter);
 
263
      iter->ct_i = iter->total_ct_i = -1;
 
264
    }
 
265
  else
 
266
    {
 
267
      iter->total_ct_i++;
 
268
      iter->ct_i++;
 
269
 
 
270
      if (iter->total_ct_i < iter->glyph_count &&
 
271
          iter->ct_i >= CTRunGetGlyphCount (iter->current_run))
 
272
        {
 
273
          iter->current_run_number++;
 
274
          run_iterator_set_current_run (iter, iter->current_run_number);
 
275
        }
 
276
    }
 
277
}
 
278
 
 
279
 
 
280
 
 
281
struct GlyphInfo
 
282
{
 
283
  CFIndex index;
 
284
  CGGlyph cgglyph;
 
285
  gunichar wc;
 
286
};
 
287
 
 
288
static gint
 
289
glyph_info_compare_func (gconstpointer a, gconstpointer b)
 
290
{
 
291
  const struct GlyphInfo *gi_a = a;
 
292
  const struct GlyphInfo *gi_b = b;
 
293
 
 
294
  if (gi_a->index < gi_b->index)
 
295
    return -1;
 
296
  else if (gi_a->index > gi_b->index)
 
297
    return 1;
 
298
  /* else */
 
299
  return 0;
 
300
}
 
301
 
 
302
static void
 
303
glyph_info_free (gpointer data, gpointer user_data)
 
304
{
 
305
  g_slice_free (struct GlyphInfo, data);
 
306
}
 
307
 
 
308
static GSList *
 
309
create_core_text_glyph_list (const char *text,
 
310
                             gint        length,
 
311
                             CTFontRef   ctfont)
 
312
{
 
313
  GSList *glyph_list = NULL;
 
314
  struct RunIterator riter;
 
315
 
 
316
  run_iterator_create (&riter, text, length, ctfont);
 
317
 
 
318
  while (!run_iterator_at_end (&riter))
 
319
    {
 
320
      struct GlyphInfo *gi;
 
321
 
 
322
      gi = g_slice_new (struct GlyphInfo);
 
323
      gi->index = run_iterator_get_index (&riter);
 
324
      gi->cgglyph = run_iterator_get_cgglyph (&riter);
 
325
      gi->wc = run_iterator_get_character (&riter);
 
326
 
 
327
      glyph_list = g_slist_prepend (glyph_list, gi);
 
328
 
 
329
      run_iterator_advance (&riter);
 
330
    }
 
331
 
 
332
  glyph_list = g_slist_sort (glyph_list, glyph_info_compare_func);
 
333
 
 
334
  run_iterator_free (&riter);
 
335
 
 
336
  return glyph_list;
 
337
}
 
338
 
 
339
 
 
340
static void
 
341
basic_engine_shape (PangoEngineShape    *engine,
 
342
                    PangoFont           *font,
 
343
                    const char          *text,
 
344
                    gint                 length,
 
345
                    const PangoAnalysis *analysis,
 
346
                    PangoGlyphString    *glyphs)
 
347
{
 
348
  const char *p;
 
349
  gulong n_chars, gs_i, gs_prev_i;
 
350
  PangoCoreTextFont *cfont = PANGO_CORE_TEXT_FONT (font);
 
351
  PangoCoverage *coverage;
 
352
  GSList *glyph_list;
 
353
  GSList *glyph_iter;
 
354
 
 
355
  /* We first fully iterate over the glyph sequence generated by CoreText and
 
356
   * store this into a list, which is sorted after the iteration. We make a pass
 
357
   * over the sorted linked list to build up the PangoGlyphString.
 
358
   *
 
359
   * We have to do this in order to properly handle a bunch of characteristics of the
 
360
   * glyph sequence generated by the CoreText typesetter:
 
361
   *   # E.g. zero-width spaces do not end up in the CoreText glyph sequence. We have
 
362
   *     to manually account for the gap in the character indices.
 
363
   *   # Sometimes, CoreText generates two glyph for the same character index. We
 
364
   *     currently handle this "properly" as in we do not crash or corrupt memory,
 
365
   *     but that's about it.
 
366
   *   # Due to mismatches in size, the CoreText glyph sequence can either be longer or
 
367
   *     shorter than the PangoGlyphString. Note that the size of the PangoGlyphString
 
368
   *     should match the number of characters in "text".
 
369
   *
 
370
   * If performance becomes a problem, it is certainly possible to use a faster code
 
371
   * that only does a single iteration over the string for "simple cases". Simple cases
 
372
   * could include these that only consist out of one run (simple Latin text), which
 
373
   * don't have gaps in the glyph sequence and which are monotonically
 
374
   * increasing/decreasing.
 
375
   *
 
376
   * FIXME items for future fixing:
 
377
   *   # CoreText strings are UTF16, and the indices *often* refer to characters,
 
378
   *     but not *always*. Notable exception is when a character is encoded using
 
379
   *     two UTF16 code points. This are two characters in a CFString. At this point
 
380
   *     advancing a single character in the CFString and advancing a single character
 
381
   *     using g_utf8_next_char in the const char string goes out of sync.
 
382
   *   # We currently don't bother about LTR, Pango core appears to fix this up for us.
 
383
   *     (Even when we cared warnings were generated that strings were in the wrong
 
384
   *     order, this should be investigated).
 
385
   *   # When CoreText generates two glyphs for one character, only one is stored.
 
386
   *     This breaks the example strings for e.g. Georgian and Gothic.
 
387
   */
 
388
 
 
389
  glyph_list = create_core_text_glyph_list (text, length,
 
390
                                            pango_core_text_font_get_ctfont (cfont));
 
391
 
 
392
  /* Translate the glyph list to a PangoGlyphString */
 
393
  n_chars = g_utf8_strlen (text, length);
 
394
  pango_glyph_string_set_size (glyphs, n_chars);
 
395
 
 
396
  glyph_iter = glyph_list;
 
397
 
133
398
  coverage = pango_font_get_coverage (PANGO_FONT (cfont),
134
399
                                      analysis->language);
135
400
 
136
 
  for (i = 0; i < glyph_count; i++)
 
401
  for (gs_prev_i = -1, gs_i = 0, p = text; gs_i < n_chars;
 
402
       gs_prev_i = gs_i, gs_i++, p = g_utf8_next_char (p))
137
403
    {
138
 
      CFIndex real_i, prev_i;
139
 
      gunichar wc;
140
 
      gunichar mirrored_ch;
141
 
 
142
 
      wc = g_utf8_get_char (p);
143
 
 
144
 
      if (analysis->level % 2)
145
 
        if (pango_get_mirror_char (wc, &mirrored_ch))
146
 
          wc = mirrored_ch;
147
 
 
148
 
      if (run_status & kCTRunStatusRightToLeft)
149
 
        {
150
 
          real_i = glyph_count - i - 1;
151
 
          prev_i = real_i + 1;
152
 
        }
153
 
      else
154
 
        {
155
 
          real_i = i;
156
 
          prev_i = real_i - 1;
157
 
        }
158
 
 
159
 
      if (wc == 0xa0)   /* non-break-space */
160
 
        wc = 0x20;
161
 
 
162
 
      if (pango_is_zero_width (wc))
163
 
        {
164
 
          set_glyph (font, glyphs, real_i, p - text, PANGO_GLYPH_EMPTY);
165
 
        }
166
 
      else
167
 
        {
 
404
      struct GlyphInfo *gi = glyph_iter != NULL ? glyph_iter->data : NULL;
 
405
 
 
406
      if (gi == NULL || gi->index > gs_i)
 
407
        {
 
408
          /* gs_i is behind, insert empty glyph */
 
409
          set_glyph (font, glyphs, gs_i, p - text, PANGO_GLYPH_EMPTY);
 
410
          continue;
 
411
        }
 
412
      else if (gi->index < gs_i)
 
413
        {
 
414
          while (gi && gi->index < gs_i)
 
415
            {
 
416
              glyph_iter = g_slist_next (glyph_iter);
 
417
              if (glyph_iter)
 
418
                gi = glyph_iter->data;
 
419
              else
 
420
                gi = NULL;
 
421
            }
 
422
        }
 
423
 
 
424
      if (gi != NULL && gi->index == gs_i)
 
425
        {
 
426
          gunichar mirrored_ch;
168
427
          PangoCoverageLevel result;
169
428
 
170
 
          result = pango_coverage_get (coverage, wc);
 
429
          if (analysis->level % 2)
 
430
            if (g_unichar_get_mirror_char (gi->wc, &mirrored_ch))
 
431
              gi->wc = mirrored_ch;
 
432
 
 
433
          if (gi->wc == 0xa0)   /* non-break-space */
 
434
            gi->wc = 0x20;
 
435
 
 
436
          result = pango_coverage_get (coverage, gi->wc);
171
437
 
172
438
          if (result != PANGO_COVERAGE_NONE)
173
439
            {
174
 
              set_glyph (font, glyphs, real_i, p - text, cgglyphs[real_i]);
 
440
              set_glyph (font, glyphs, gs_i, p - text, gi->cgglyph);
175
441
 
176
 
              if (g_unichar_type (wc) == G_UNICODE_NON_SPACING_MARK)
 
442
              if (g_unichar_type (gi->wc) == G_UNICODE_NON_SPACING_MARK)
177
443
                {
178
 
                  if (i > 0)
 
444
                  if (gi->index > 0)
179
445
                    {
180
446
                      PangoRectangle logical_rect, ink_rect;
181
447
 
182
 
                      glyphs->glyphs[real_i].geometry.width = MAX (glyphs->glyphs[prev_i].geometry.width,
183
 
                                                                   glyphs->glyphs[prev_i].geometry.width);
184
 
                      glyphs->glyphs[prev_i].geometry.width = 0;
185
 
                      glyphs->log_clusters[real_i] = glyphs->log_clusters[prev_i];
 
448
                      glyphs->glyphs[gs_i].geometry.width = MAX (glyphs->glyphs[gs_prev_i].geometry.width,
 
449
                                                                 glyphs->glyphs[gs_i].geometry.width);
 
450
                      glyphs->glyphs[gs_prev_i].geometry.width = 0;
 
451
                      glyphs->log_clusters[gs_i] = glyphs->log_clusters[gs_prev_i];
186
452
 
187
453
                      /* Some heuristics to try to guess how overstrike glyphs are
188
454
                       * done and compensate
189
455
                       */
190
 
                      pango_font_get_glyph_extents (font, glyphs->glyphs[real_i].glyph, &ink_rect, &logical_rect);
 
456
                      pango_font_get_glyph_extents (font, glyphs->glyphs[gs_i].glyph, &ink_rect, &logical_rect);
191
457
                      if (logical_rect.width == 0 && ink_rect.x == 0)
192
 
                        glyphs->glyphs[real_i].geometry.x_offset = (glyphs->glyphs[real_i].geometry.width - ink_rect.width) / 2;
 
458
                        glyphs->glyphs[gs_i].geometry.x_offset = (glyphs->glyphs[gs_i].geometry.width - ink_rect.width) / 2;
193
459
                    }
194
460
                }
195
461
            }
196
462
          else
197
 
            {
198
 
              set_glyph (font, glyphs, real_i, p - text,
199
 
                         PANGO_GET_UNKNOWN_GLYPH (wc));
200
 
            }
 
463
            set_glyph (font, glyphs, gs_i, p - text, PANGO_GET_UNKNOWN_GLYPH (gi->wc));
 
464
 
 
465
          glyph_iter = g_slist_next (glyph_iter);
201
466
        }
202
 
 
203
 
      p = g_utf8_next_char (p);
204
467
    }
205
468
 
206
 
  CFRelease (line);
207
 
  CFRelease (attstr);
208
 
  CFRelease (cstr);
209
 
  CFRelease (attributes);
210
469
  pango_coverage_unref (coverage);
 
470
  g_slist_foreach (glyph_list, glyph_info_free, NULL);
 
471
  g_slist_free (glyph_list);
211
472
}
212
473
 
213
474
static void