- /* We need to discover two things: firstly whether the string is
- * longer than @p max glyphs and secondly if it is not, where to cut
- * the string.
- *
- * Combining characters follow their base character (unicode
- * standard 5.0 s2.11), so after each base character we must
- */
- while(*t) {
- PARSE_UTF8(t, c, return s);
- if(combining(c))
- /* This must be an initial combining character. We just skip it. */
- continue;
- /* So c must be a base character. It may be followed by any
- * number of combining characters. We advance past them. */
- do {
- r = t;
- PARSE_UTF8(t, c, return s);
- } while(combining(c));
- /* Last character wasn't a combining character so back up */
- t = r;
- ++n;
- /* So now there are N glyphs before position T. We might
- * therefore have reached the cut position. */
- if(n == max - 3)
- cut = t;
+ /* Convert to UTF-32 for processing */
+ if(!(s32 = utf8_to_utf32(s, strlen(s), &l32)))
+ return 0;
+ it = utf32_iterator_new(s32, l32);
+ cut = l32;
+ graphemes = 0; /* # of graphemes left of it */
+ while(graphemes <= max && utf32_iterator_where(it) < l32) {
+ if(graphemes == max - 1)
+ cut = utf32_iterator_where(it);
+ utf32_iterator_advance(it, 1);
+ if(utf32_iterator_grapheme_boundary(it))
+ ++graphemes;
+ }
+ if(graphemes > max) { /* we need to cut */
+ s32[cut] = 0x2026; /* HORIZONTAL ELLIPSIS */
+ l32 = cut + 1;
+ s = utf32_to_utf8(s32, l32, 0);