1 /*
2 * Copyright © 2011 Google, Inc.
3 *
4 * This is part of HarfBuzz, a text shaping library.
5 *
6 * Permission is hereby granted, without written agreement and without
7 * license or royalty fees, to use, copy, modify, and distribute this
8 * software and its documentation for any purpose, provided that the
9 * above copyright notice and the following two paragraphs appear in
10 * all copies of this software.
11 *
12 * IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR
13 * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
14 * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN
15 * IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
16 * DAMAGE.
17 *
18 * THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING,
19 * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
20 * FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS
21 * ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO
22 * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
23 *
24 * Google Author(s): Behdad Esfahbod
25 */
26
27 #include "helper-cairo.hh"
28
29 #include <cairo-ft.h>
30 #include <hb-ft.h>
31 #include FT_MULTIPLE_MASTERS_H
32
33 #include "helper-cairo-ansi.hh"
34 #ifdef CAIRO_HAS_SVG_SURFACE
35 # include <cairo-svg.h>
36 #endif
37 #ifdef CAIRO_HAS_PDF_SURFACE
38 # include <cairo-pdf.h>
39 #endif
40 #ifdef CAIRO_HAS_PS_SURFACE
41 # include <cairo-ps.h>
42 # if CAIRO_VERSION >= CAIRO_VERSION_ENCODE(1,6,0)
43 # define HAS_EPS 1
44
45 static cairo_surface_t *
_cairo_eps_surface_create_for_stream(cairo_write_func_t write_func,void * closure,double width,double height)46 _cairo_eps_surface_create_for_stream (cairo_write_func_t write_func,
47 void *closure,
48 double width,
49 double height)
50 {
51 cairo_surface_t *surface;
52
53 surface = cairo_ps_surface_create_for_stream (write_func, closure, width, height);
54 cairo_ps_surface_set_eps (surface, true);
55
56 return surface;
57 }
58
59 # else
60 # undef HAS_EPS
61 # endif
62 #endif
63
64
65 static FT_Library ft_library;
66
67 #ifdef HAVE_ATEXIT
68 static inline
free_ft_library()69 void free_ft_library ()
70 {
71 FT_Done_FreeType (ft_library);
72 }
73 #endif
74
75 cairo_scaled_font_t *
helper_cairo_create_scaled_font(const font_options_t * font_opts)76 helper_cairo_create_scaled_font (const font_options_t *font_opts)
77 {
78 hb_font_t *font = hb_font_reference (font_opts->get_font ());
79
80 cairo_font_face_t *cairo_face;
81 /* We cannot use the FT_Face from hb_font_t, as doing so will confuse hb_font_t because
82 * cairo will reset the face size. As such, create new face...
83 * TODO Perhaps add API to hb-ft to encapsulate this code. */
84 FT_Face ft_face = nullptr;//hb_ft_font_get_face (font);
85 if (!ft_face)
86 {
87 if (!ft_library)
88 {
89 FT_Init_FreeType (&ft_library);
90 #ifdef HAVE_ATEXIT
91 atexit (free_ft_library);
92 #endif
93 }
94
95 unsigned int blob_length;
96 const char *blob_data = hb_blob_get_data (font_opts->blob, &blob_length);
97
98 if (FT_New_Memory_Face (ft_library,
99 (const FT_Byte *) blob_data,
100 blob_length,
101 font_opts->face_index,
102 &ft_face))
103 fail (false, "FT_New_Memory_Face fail");
104 }
105 if (!ft_face)
106 {
107 /* This allows us to get some boxes at least... */
108 cairo_face = cairo_toy_font_face_create ("@cairo:sans",
109 CAIRO_FONT_SLANT_NORMAL,
110 CAIRO_FONT_WEIGHT_NORMAL);
111 }
112 else
113 {
114 #ifdef HAVE_FT_SET_VAR_BLEND_COORDINATES
115 unsigned int num_coords;
116 const int *coords = hb_font_get_var_coords_normalized (font, &num_coords);
117 if (num_coords)
118 {
119 FT_Fixed *ft_coords = (FT_Fixed *) calloc (num_coords, sizeof (FT_Fixed));
120 if (ft_coords)
121 {
122 for (unsigned int i = 0; i < num_coords; i++)
123 ft_coords[i] = coords[i] << 2;
124 FT_Set_Var_Blend_Coordinates (ft_face, num_coords, ft_coords);
125 free (ft_coords);
126 }
127 }
128 #endif
129
130 cairo_face = cairo_ft_font_face_create_for_ft_face (ft_face, font_opts->ft_load_flags);
131 }
132 cairo_matrix_t ctm, font_matrix;
133 cairo_font_options_t *font_options;
134
135 cairo_matrix_init_identity (&ctm);
136 cairo_matrix_init_scale (&font_matrix,
137 font_opts->font_size_x,
138 font_opts->font_size_y);
139 font_options = cairo_font_options_create ();
140 cairo_font_options_set_hint_style (font_options, CAIRO_HINT_STYLE_NONE);
141 cairo_font_options_set_hint_metrics (font_options, CAIRO_HINT_METRICS_OFF);
142
143 cairo_scaled_font_t *scaled_font = cairo_scaled_font_create (cairo_face,
144 &font_matrix,
145 &ctm,
146 font_options);
147
148 cairo_font_options_destroy (font_options);
149 cairo_font_face_destroy (cairo_face);
150
151 static cairo_user_data_key_t key;
152 if (cairo_scaled_font_set_user_data (scaled_font,
153 &key,
154 (void *) font,
155 (cairo_destroy_func_t) hb_font_destroy))
156 hb_font_destroy (font);
157
158 return scaled_font;
159 }
160
161 bool
helper_cairo_scaled_font_has_color(cairo_scaled_font_t * scaled_font)162 helper_cairo_scaled_font_has_color (cairo_scaled_font_t *scaled_font)
163 {
164 bool ret = false;
165 #ifdef FT_HAS_COLOR
166 FT_Face ft_face = cairo_ft_scaled_font_lock_face (scaled_font);
167 if (ft_face)
168 {
169 if (FT_HAS_COLOR (ft_face))
170 ret = true;
171 cairo_ft_scaled_font_unlock_face (scaled_font);
172 }
173 #endif
174 return ret;
175 }
176
177
178 enum class image_protocol_t {
179 NONE = 0,
180 ITERM2,
181 KITTY,
182 };
183
184 struct finalize_closure_t {
185 void (*callback)(finalize_closure_t *);
186 cairo_surface_t *surface;
187 cairo_write_func_t write_func;
188 void *closure;
189 image_protocol_t protocol;
190 };
191 static cairo_user_data_key_t finalize_closure_key;
192
193
194 static void
finalize_ansi(finalize_closure_t * closure)195 finalize_ansi (finalize_closure_t *closure)
196 {
197 cairo_status_t status;
198 status = helper_cairo_surface_write_to_ansi_stream (closure->surface,
199 closure->write_func,
200 closure->closure);
201 if (status != CAIRO_STATUS_SUCCESS)
202 fail (false, "Failed to write output: %s",
203 cairo_status_to_string (status));
204 }
205
206 static cairo_surface_t *
_cairo_ansi_surface_create_for_stream(cairo_write_func_t write_func,void * closure,double width,double height,cairo_content_t content,image_protocol_t protocol HB_UNUSED)207 _cairo_ansi_surface_create_for_stream (cairo_write_func_t write_func,
208 void *closure,
209 double width,
210 double height,
211 cairo_content_t content,
212 image_protocol_t protocol HB_UNUSED)
213 {
214 cairo_surface_t *surface;
215 int w = ceil (width);
216 int h = ceil (height);
217
218 switch (content) {
219 case CAIRO_CONTENT_ALPHA:
220 surface = cairo_image_surface_create (CAIRO_FORMAT_A8, w, h);
221 break;
222 default:
223 case CAIRO_CONTENT_COLOR:
224 surface = cairo_image_surface_create (CAIRO_FORMAT_RGB24, w, h);
225 break;
226 case CAIRO_CONTENT_COLOR_ALPHA:
227 surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, w, h);
228 break;
229 }
230 cairo_status_t status = cairo_surface_status (surface);
231 if (status != CAIRO_STATUS_SUCCESS)
232 fail (false, "Failed to create cairo surface: %s",
233 cairo_status_to_string (status));
234
235 finalize_closure_t *ansi_closure = g_new0 (finalize_closure_t, 1);
236 ansi_closure->callback = finalize_ansi;
237 ansi_closure->surface = surface;
238 ansi_closure->write_func = write_func;
239 ansi_closure->closure = closure;
240
241 if (cairo_surface_set_user_data (surface,
242 &finalize_closure_key,
243 (void *) ansi_closure,
244 (cairo_destroy_func_t) g_free))
245 g_free ((void *) closure);
246
247 return surface;
248 }
249
250
251 #ifdef CAIRO_HAS_PNG_FUNCTIONS
252
253 static cairo_status_t
byte_array_write_func(void * closure,const unsigned char * data,unsigned int size)254 byte_array_write_func (void *closure,
255 const unsigned char *data,
256 unsigned int size)
257 {
258 g_byte_array_append ((GByteArray *) closure, data, size);
259 return CAIRO_STATUS_SUCCESS;
260 }
261
262 static void
finalize_png(finalize_closure_t * closure)263 finalize_png (finalize_closure_t *closure)
264 {
265 cairo_status_t status;
266 GByteArray *bytes = nullptr;
267 GString *string;
268 gchar *base64;
269 size_t base64_len;
270
271 if (closure->protocol == image_protocol_t::NONE)
272 {
273 status = cairo_surface_write_to_png_stream (closure->surface,
274 closure->write_func,
275 closure->closure);
276 }
277 else
278 {
279 bytes = g_byte_array_new ();
280 status = cairo_surface_write_to_png_stream (closure->surface,
281 byte_array_write_func,
282 bytes);
283 }
284
285 if (status != CAIRO_STATUS_SUCCESS)
286 fail (false, "Failed to write output: %s",
287 cairo_status_to_string (status));
288
289 if (closure->protocol == image_protocol_t::NONE)
290 return;
291
292 base64 = g_base64_encode (bytes->data, bytes->len);
293 base64_len = strlen (base64);
294
295 string = g_string_new (NULL);
296 if (closure->protocol == image_protocol_t::ITERM2)
297 {
298 /* https://iterm2.com/documentation-images.html */
299 g_string_printf (string, "\033]1337;File=inline=1;size=%zu:%s\a\n",
300 base64_len, base64);
301 }
302 else if (closure->protocol == image_protocol_t::KITTY)
303 {
304 #define CHUNK_SIZE 4096
305 /* https://sw.kovidgoyal.net/kitty/graphics-protocol.html */
306 for (size_t pos = 0; pos < base64_len; pos += CHUNK_SIZE)
307 {
308 size_t len = base64_len - pos;
309
310 if (pos == 0)
311 g_string_append (string, "\033_Ga=T,f=100,m=");
312 else
313 g_string_append (string, "\033_Gm=");
314
315 if (len > CHUNK_SIZE)
316 {
317 g_string_append (string, "1;");
318 g_string_append_len (string, base64 + pos, CHUNK_SIZE);
319 }
320 else
321 {
322 g_string_append (string, "0;");
323 g_string_append_len (string, base64 + pos, len);
324 }
325
326 g_string_append (string, "\033\\");
327 }
328 g_string_append (string, "\n");
329 #undef CHUNK_SIZE
330 }
331
332 closure->write_func (closure->closure, (unsigned char *) string->str, string->len);
333
334 g_byte_array_unref (bytes);
335 g_free (base64);
336 g_string_free (string, TRUE);
337 }
338
339 static cairo_surface_t *
_cairo_png_surface_create_for_stream(cairo_write_func_t write_func,void * closure,double width,double height,cairo_content_t content,image_protocol_t protocol)340 _cairo_png_surface_create_for_stream (cairo_write_func_t write_func,
341 void *closure,
342 double width,
343 double height,
344 cairo_content_t content,
345 image_protocol_t protocol)
346 {
347 cairo_surface_t *surface;
348 int w = ceil (width);
349 int h = ceil (height);
350
351 switch (content) {
352 case CAIRO_CONTENT_ALPHA:
353 surface = cairo_image_surface_create (CAIRO_FORMAT_A8, w, h);
354 break;
355 default:
356 case CAIRO_CONTENT_COLOR:
357 surface = cairo_image_surface_create (CAIRO_FORMAT_RGB24, w, h);
358 break;
359 case CAIRO_CONTENT_COLOR_ALPHA:
360 surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, w, h);
361 break;
362 }
363 cairo_status_t status = cairo_surface_status (surface);
364 if (status != CAIRO_STATUS_SUCCESS)
365 fail (false, "Failed to create cairo surface: %s",
366 cairo_status_to_string (status));
367
368 finalize_closure_t *png_closure = g_new0 (finalize_closure_t, 1);
369 png_closure->callback = finalize_png;
370 png_closure->surface = surface;
371 png_closure->write_func = write_func;
372 png_closure->closure = closure;
373 png_closure->protocol = protocol;
374
375 if (cairo_surface_set_user_data (surface,
376 &finalize_closure_key,
377 (void *) png_closure,
378 (cairo_destroy_func_t) g_free))
379 g_free ((void *) closure);
380
381 return surface;
382 }
383
384 #endif
385
386 static cairo_status_t
stdio_write_func(void * closure,const unsigned char * data,unsigned int size)387 stdio_write_func (void *closure,
388 const unsigned char *data,
389 unsigned int size)
390 {
391 FILE *fp = (FILE *) closure;
392
393 while (size) {
394 size_t ret = fwrite (data, 1, size, fp);
395 size -= ret;
396 data += ret;
397 if (size && ferror (fp))
398 fail (false, "Failed to write output: %s", strerror (errno));
399 }
400
401 return CAIRO_STATUS_SUCCESS;
402 }
403
404 const char *helper_cairo_supported_formats[] =
405 {
406 "ansi",
407 #ifdef CAIRO_HAS_PNG_FUNCTIONS
408 "png",
409 #endif
410 #ifdef CAIRO_HAS_SVG_SURFACE
411 "svg",
412 #endif
413 #ifdef CAIRO_HAS_PDF_SURFACE
414 "pdf",
415 #endif
416 #ifdef CAIRO_HAS_PS_SURFACE
417 "ps",
418 #ifdef HAS_EPS
419 "eps",
420 #endif
421 #endif
422 nullptr
423 };
424
425 cairo_t *
helper_cairo_create_context(double w,double h,view_options_t * view_opts,output_options_t * out_opts,cairo_content_t content)426 helper_cairo_create_context (double w, double h,
427 view_options_t *view_opts,
428 output_options_t *out_opts,
429 cairo_content_t content)
430 {
431 cairo_surface_t *(*constructor) (cairo_write_func_t write_func,
432 void *closure,
433 double width,
434 double height) = nullptr;
435 cairo_surface_t *(*constructor2) (cairo_write_func_t write_func,
436 void *closure,
437 double width,
438 double height,
439 cairo_content_t content,
440 image_protocol_t protocol) = nullptr;
441
442 image_protocol_t protocol = image_protocol_t::NONE;
443 const char *extension = out_opts->output_format;
444 if (!extension) {
445 #if HAVE_ISATTY
446 if (isatty (fileno (out_opts->get_file_handle ())))
447 {
448 #ifdef CAIRO_HAS_PNG_FUNCTIONS
449 const char *name;
450 /* https://gitlab.com/gnachman/iterm2/-/issues/7154 */
451 if ((name = getenv ("LC_TERMINAL")) != nullptr &&
452 0 == g_ascii_strcasecmp (name, "iTerm2"))
453 {
454 extension = "png";
455 protocol = image_protocol_t::ITERM2;
456 }
457 else if ((name = getenv ("TERM")) != nullptr &&
458 0 == g_ascii_strcasecmp (name, "xterm-kitty"))
459 {
460 extension = "png";
461 protocol = image_protocol_t::KITTY;
462 }
463 else
464 extension = "ansi";
465 #else
466 extension = "ansi";
467 #endif
468 }
469 else
470 #endif
471 {
472 #ifdef CAIRO_HAS_PNG_FUNCTIONS
473 extension = "png";
474 #else
475 extension = "ansi";
476 #endif
477 }
478 }
479 if (0)
480 ;
481 else if (0 == g_ascii_strcasecmp (extension, "ansi"))
482 constructor2 = _cairo_ansi_surface_create_for_stream;
483 #ifdef CAIRO_HAS_PNG_FUNCTIONS
484 else if (0 == g_ascii_strcasecmp (extension, "png"))
485 constructor2 = _cairo_png_surface_create_for_stream;
486 #endif
487 #ifdef CAIRO_HAS_SVG_SURFACE
488 else if (0 == g_ascii_strcasecmp (extension, "svg"))
489 constructor = cairo_svg_surface_create_for_stream;
490 #endif
491 #ifdef CAIRO_HAS_PDF_SURFACE
492 else if (0 == g_ascii_strcasecmp (extension, "pdf"))
493 constructor = cairo_pdf_surface_create_for_stream;
494 #endif
495 #ifdef CAIRO_HAS_PS_SURFACE
496 else if (0 == g_ascii_strcasecmp (extension, "ps"))
497 constructor = cairo_ps_surface_create_for_stream;
498 #ifdef HAS_EPS
499 else if (0 == g_ascii_strcasecmp (extension, "eps"))
500 constructor = _cairo_eps_surface_create_for_stream;
501 #endif
502 #endif
503
504
505 unsigned int fr, fg, fb, fa, br, bg, bb, ba;
506 const char *color;
507 br = bg = bb = 0; ba = 255;
508 color = view_opts->back ? view_opts->back : DEFAULT_BACK;
509 sscanf (color + (*color=='#'), "%2x%2x%2x%2x", &br, &bg, &bb, &ba);
510 fr = fg = fb = 0; fa = 255;
511 color = view_opts->fore ? view_opts->fore : DEFAULT_FORE;
512 sscanf (color + (*color=='#'), "%2x%2x%2x%2x", &fr, &fg, &fb, &fa);
513
514 if (content == CAIRO_CONTENT_ALPHA)
515 {
516 if (view_opts->annotate ||
517 br != bg || bg != bb ||
518 fr != fg || fg != fb)
519 content = CAIRO_CONTENT_COLOR;
520 }
521 if (ba != 255)
522 content = CAIRO_CONTENT_COLOR_ALPHA;
523
524 cairo_surface_t *surface;
525 FILE *f = out_opts->get_file_handle ();
526 if (constructor)
527 surface = constructor (stdio_write_func, f, w, h);
528 else if (constructor2)
529 surface = constructor2 (stdio_write_func, f, w, h, content, protocol);
530 else
531 fail (false, "Unknown output format `%s'; supported formats are: %s%s",
532 extension,
533 g_strjoinv ("/", const_cast<char**> (helper_cairo_supported_formats)),
534 out_opts->explicit_output_format ? "" :
535 "\nTry setting format using --output-format");
536
537 cairo_t *cr = cairo_create (surface);
538 content = cairo_surface_get_content (surface);
539
540 switch (content) {
541 case CAIRO_CONTENT_ALPHA:
542 cairo_set_operator (cr, CAIRO_OPERATOR_SOURCE);
543 cairo_set_source_rgba (cr, 1., 1., 1., br / 255.);
544 cairo_paint (cr);
545 cairo_set_source_rgba (cr, 1., 1., 1.,
546 (fr / 255.) * (fa / 255.) + (br / 255) * (1 - (fa / 255.)));
547 break;
548 default:
549 case CAIRO_CONTENT_COLOR:
550 case CAIRO_CONTENT_COLOR_ALPHA:
551 cairo_set_operator (cr, CAIRO_OPERATOR_SOURCE);
552 cairo_set_source_rgba (cr, br / 255., bg / 255., bb / 255., ba / 255.);
553 cairo_paint (cr);
554 cairo_set_operator (cr, CAIRO_OPERATOR_OVER);
555 cairo_set_source_rgba (cr, fr / 255., fg / 255., fb / 255., fa / 255.);
556 break;
557 }
558
559 cairo_surface_destroy (surface);
560 return cr;
561 }
562
563 void
helper_cairo_destroy_context(cairo_t * cr)564 helper_cairo_destroy_context (cairo_t *cr)
565 {
566 finalize_closure_t *closure = (finalize_closure_t *)
567 cairo_surface_get_user_data (cairo_get_target (cr),
568 &finalize_closure_key);
569 if (closure)
570 closure->callback (closure);
571
572 cairo_status_t status = cairo_status (cr);
573 if (status != CAIRO_STATUS_SUCCESS)
574 fail (false, "Failed: %s",
575 cairo_status_to_string (status));
576 cairo_destroy (cr);
577 }
578
579
580 void
helper_cairo_line_from_buffer(helper_cairo_line_t * l,hb_buffer_t * buffer,const char * text,unsigned int text_len,int scale_bits,hb_bool_t utf8_clusters)581 helper_cairo_line_from_buffer (helper_cairo_line_t *l,
582 hb_buffer_t *buffer,
583 const char *text,
584 unsigned int text_len,
585 int scale_bits,
586 hb_bool_t utf8_clusters)
587 {
588 memset (l, 0, sizeof (*l));
589
590 l->num_glyphs = hb_buffer_get_length (buffer);
591 hb_glyph_info_t *hb_glyph = hb_buffer_get_glyph_infos (buffer, nullptr);
592 hb_glyph_position_t *hb_position = hb_buffer_get_glyph_positions (buffer, nullptr);
593 l->glyphs = cairo_glyph_allocate (l->num_glyphs + 1);
594
595 if (text) {
596 l->utf8 = g_strndup (text, text_len);
597 l->utf8_len = text_len;
598 l->num_clusters = l->num_glyphs ? 1 : 0;
599 for (unsigned int i = 1; i < l->num_glyphs; i++)
600 if (hb_glyph[i].cluster != hb_glyph[i-1].cluster)
601 l->num_clusters++;
602 l->clusters = cairo_text_cluster_allocate (l->num_clusters);
603 }
604
605 if ((l->num_glyphs && !l->glyphs) ||
606 (l->utf8_len && !l->utf8) ||
607 (l->num_clusters && !l->clusters))
608 {
609 l->finish ();
610 return;
611 }
612
613 hb_position_t x = 0, y = 0;
614 int i;
615 for (i = 0; i < (int) l->num_glyphs; i++)
616 {
617 l->glyphs[i].index = hb_glyph[i].codepoint;
618 l->glyphs[i].x = scalbn ((double) hb_position->x_offset + x, scale_bits);
619 l->glyphs[i].y = scalbn ((double) -hb_position->y_offset + y, scale_bits);
620 x += hb_position->x_advance;
621 y += -hb_position->y_advance;
622
623 hb_position++;
624 }
625 l->glyphs[i].index = -1;
626 l->glyphs[i].x = scalbn ((double) x, scale_bits);
627 l->glyphs[i].y = scalbn ((double) y, scale_bits);
628
629 if (l->num_clusters) {
630 memset ((void *) l->clusters, 0, l->num_clusters * sizeof (l->clusters[0]));
631 hb_bool_t backward = HB_DIRECTION_IS_BACKWARD (hb_buffer_get_direction (buffer));
632 l->cluster_flags = backward ? CAIRO_TEXT_CLUSTER_FLAG_BACKWARD : (cairo_text_cluster_flags_t) 0;
633 unsigned int cluster = 0;
634 const char *start = l->utf8, *end;
635 l->clusters[cluster].num_glyphs++;
636 if (backward) {
637 for (i = l->num_glyphs - 2; i >= 0; i--) {
638 if (hb_glyph[i].cluster != hb_glyph[i+1].cluster) {
639 g_assert (hb_glyph[i].cluster > hb_glyph[i+1].cluster);
640 if (utf8_clusters)
641 end = start + hb_glyph[i].cluster - hb_glyph[i+1].cluster;
642 else
643 end = g_utf8_offset_to_pointer (start, hb_glyph[i].cluster - hb_glyph[i+1].cluster);
644 l->clusters[cluster].num_bytes = end - start;
645 start = end;
646 cluster++;
647 }
648 l->clusters[cluster].num_glyphs++;
649 }
650 l->clusters[cluster].num_bytes = l->utf8 + text_len - start;
651 } else {
652 for (i = 1; i < (int) l->num_glyphs; i++) {
653 if (hb_glyph[i].cluster != hb_glyph[i-1].cluster) {
654 g_assert (hb_glyph[i].cluster > hb_glyph[i-1].cluster);
655 if (utf8_clusters)
656 end = start + hb_glyph[i].cluster - hb_glyph[i-1].cluster;
657 else
658 end = g_utf8_offset_to_pointer (start, hb_glyph[i].cluster - hb_glyph[i-1].cluster);
659 l->clusters[cluster].num_bytes = end - start;
660 start = end;
661 cluster++;
662 }
663 l->clusters[cluster].num_glyphs++;
664 }
665 l->clusters[cluster].num_bytes = l->utf8 + text_len - start;
666 }
667 }
668 }
669