2016-05-25 00:52:53 -05:00
|
|
|
// 25 may 2016
|
2016-05-25 20:17:34 -05:00
|
|
|
#include "uipriv_unix.h"
|
2018-03-11 15:15:28 -05:00
|
|
|
#include "attrstr.h"
|
2016-05-25 00:52:53 -05:00
|
|
|
|
2018-03-11 15:15:28 -05:00
|
|
|
int uiprivGraphemesTakesUTF16(void)
|
2016-05-25 00:52:53 -05:00
|
|
|
{
|
2016-12-03 17:53:46 -06:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-04-15 21:43:14 -05:00
|
|
|
uiprivGraphemes *uiprivNewGraphemes(void *s, size_t len)
|
2016-12-03 17:53:46 -06:00
|
|
|
{
|
2018-04-15 21:43:14 -05:00
|
|
|
uiprivGraphemes *g;
|
2016-12-03 17:53:46 -06:00
|
|
|
char *text = (char *) s;
|
|
|
|
size_t lenchars;
|
2016-05-25 14:07:32 -05:00
|
|
|
PangoLogAttr *logattrs;
|
|
|
|
size_t i;
|
2016-12-03 17:53:46 -06:00
|
|
|
size_t *op;
|
2016-05-25 00:52:53 -05:00
|
|
|
|
2018-04-15 21:43:14 -05:00
|
|
|
g = uiprivNew(uiprivGraphemes);
|
2016-12-03 17:53:46 -06:00
|
|
|
|
|
|
|
// TODO see if we can use the utf routines
|
2016-05-25 14:07:32 -05:00
|
|
|
lenchars = g_utf8_strlen(text, -1);
|
2018-04-15 15:36:03 -05:00
|
|
|
logattrs = (PangoLogAttr *) uiprivAlloc((lenchars + 1) * sizeof (PangoLogAttr), "PangoLogAttr[] (graphemes)");
|
2016-05-25 14:07:32 -05:00
|
|
|
pango_get_log_attrs(text, len,
|
|
|
|
-1, NULL,
|
|
|
|
logattrs, lenchars + 1);
|
|
|
|
|
2016-12-03 17:53:46 -06:00
|
|
|
// first figure out how many graphemes there are
|
|
|
|
g->len = 0;
|
|
|
|
for (i = 0; i < lenchars; i++)
|
|
|
|
if (logattrs[i].is_cursor_position != 0)
|
|
|
|
g->len++;
|
|
|
|
|
2018-04-15 15:36:03 -05:00
|
|
|
g->pointsToGraphemes = (size_t *) uiprivAlloc((len + 1) * sizeof (size_t), "size_t[] (graphemes)");
|
|
|
|
g->graphemesToPoints = (size_t *) uiprivAlloc((g->len + 1) * sizeof (size_t), "size_t[] (graphemes)");
|
2016-12-03 17:53:46 -06:00
|
|
|
|
|
|
|
// compute the graphemesToPoints array
|
|
|
|
// TODO merge with the next for loop somehow?
|
|
|
|
op = g->graphemesToPoints;
|
2016-05-25 14:07:32 -05:00
|
|
|
for (i = 0; i < lenchars; i++)
|
|
|
|
if (logattrs[i].is_cursor_position != 0)
|
|
|
|
// TODO optimize this
|
|
|
|
*op++ = g_utf8_offset_to_pointer(text, i) - text;
|
|
|
|
// and do the last one
|
|
|
|
*op++ = len;
|
|
|
|
|
2016-12-03 17:53:46 -06:00
|
|
|
// and finally build the pointsToGraphemes array
|
|
|
|
op = g->pointsToGraphemes;
|
|
|
|
for (i = 0; i < g->len; i++) {
|
|
|
|
size_t j;
|
|
|
|
size_t first, last;
|
|
|
|
|
|
|
|
first = g->graphemesToPoints[i];
|
|
|
|
last = g->graphemesToPoints[i + 1];
|
|
|
|
for (j = first; j < last; j++)
|
|
|
|
*op++ = i;
|
|
|
|
}
|
|
|
|
// and do the last one
|
|
|
|
*op++ = i;
|
|
|
|
|
2018-04-15 15:36:03 -05:00
|
|
|
uiprivFree(logattrs);
|
2016-12-03 17:53:46 -06:00
|
|
|
return g;
|
2016-05-25 00:52:53 -05:00
|
|
|
}
|