And migrated the Unix grapheme code for the new attributed string system.

This commit is contained in:
Pietro Gagliardi 2016-12-03 18:53:46 -05:00
parent 526173bf76
commit 3218ba2a43
3 changed files with 43 additions and 18 deletions

View File

@ -1,24 +1,41 @@
// 25 may 2016 // 25 may 2016
#include "uipriv_unix.h" #include "uipriv_unix.h"
ptrdiff_t *graphemes(const char *text, PangoContext *context) int graphemesTakesUTF16(void)
{ {
size_t len, lenchars; return 0;
PangoLogAttr *logattrs; }
ptrdiff_t *out;
ptrdiff_t *op;
size_t i;
len = strlen(text); struct graphemes *graphemes(void *s, size_t len);
{
struct graphemes *g;
char *text = (char *) s;
size_t lenchars;
PangoLogAttr *logattrs;
size_t i;
size_t *op;
g = uiNew(struct graphemes);
// TODO see if we can use the utf routines
lenchars = g_utf8_strlen(text, -1); lenchars = g_utf8_strlen(text, -1);
logattrs = (PangoLogAttr *) uiAlloc((lenchars + 1) * sizeof (PangoLogAttr), "PangoLogAttr[]"); logattrs = (PangoLogAttr *) uiAlloc((lenchars + 1) * sizeof (PangoLogAttr), "PangoLogAttr[] (graphemes)");
pango_get_log_attrs(text, len, pango_get_log_attrs(text, len,
-1, NULL, -1, NULL,
logattrs, lenchars + 1); logattrs, lenchars + 1);
// should be more than enough // first figure out how many graphemes there are
out = (ptrdiff_t *) uiAlloc((lenchars + 2) * sizeof (ptrdiff_t), "ptrdiff_t[]"); g->len = 0;
op = out; for (i = 0; i < lenchars; i++)
if (logattrs[i].is_cursor_position != 0)
g->len++;
g->pointsToGraphemes = (size_t *) uiAlloc((len + 1) * sizeof (size_t), "size_t[] (graphemes)");
g->graphemesToPoints = (size_t *) uiAlloc((g->len + 1) * sizeof (size_t), "size_t[] (graphemes)");
// compute the graphemesToPoints array
// TODO merge with the next for loop somehow?
op = g->graphemesToPoints;
for (i = 0; i < lenchars; i++) for (i = 0; i < lenchars; i++)
if (logattrs[i].is_cursor_position != 0) if (logattrs[i].is_cursor_position != 0)
// TODO optimize this // TODO optimize this
@ -26,6 +43,20 @@ ptrdiff_t *graphemes(const char *text, PangoContext *context)
// and do the last one // and do the last one
*op++ = len; *op++ = len;
// 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;
uiFree(logattrs); uiFree(logattrs);
return out; return g;
} }

View File

@ -49,9 +49,6 @@ extern void freeContext(uiDrawContext *);
extern uiDrawTextFont *mkTextFont(PangoFont *f, gboolean add); extern uiDrawTextFont *mkTextFont(PangoFont *f, gboolean add);
extern PangoFont *pangoDescToPangoFont(PangoFontDescription *pdesc); extern PangoFont *pangoDescToPangoFont(PangoFontDescription *pdesc);
// graphemes.c
extern ptrdiff_t *graphemes(const char *text, PangoContext *context);
// image.c // image.c
/*TODO remove this*/typedef struct uiImage uiImage; /*TODO remove this*/typedef struct uiImage uiImage;
extern cairo_surface_t *imageAppropriateSurface(uiImage *i, GtkWidget *w); extern cairo_surface_t *imageAppropriateSurface(uiImage *i, GtkWidget *w);

View File

@ -148,9 +148,6 @@ extern BOOL showColorDialog(HWND parent, struct colorDialogRGBA *c);
// sizing.cpp // sizing.cpp
extern void getSizing(HWND hwnd, uiWindowsSizing *sizing, HFONT font); extern void getSizing(HWND hwnd, uiWindowsSizing *sizing, HFONT font);
// graphemes.cpp
extern size_t *graphemes(WCHAR *msg);
// TODO move into a dedicated file abibugs.cpp when we rewrite the drawing code // TODO move into a dedicated file abibugs.cpp when we rewrite the drawing code
extern D2D1_SIZE_F realGetSize(ID2D1RenderTarget *rt); extern D2D1_SIZE_F realGetSize(ID2D1RenderTarget *rt);