From 3218ba2a439120ca1cfa674290ba8b21e4246f2a Mon Sep 17 00:00:00 2001 From: Pietro Gagliardi Date: Sat, 3 Dec 2016 18:53:46 -0500 Subject: [PATCH] And migrated the Unix grapheme code for the new attributed string system. --- unix/graphemes.c | 55 +++++++++++++++++++++++++++++--------- unix/uipriv_unix.h | 3 --- windows/uipriv_windows.hpp | 3 --- 3 files changed, 43 insertions(+), 18 deletions(-) diff --git a/unix/graphemes.c b/unix/graphemes.c index a2c47b72..e489ebff 100644 --- a/unix/graphemes.c +++ b/unix/graphemes.c @@ -1,24 +1,41 @@ // 25 may 2016 #include "uipriv_unix.h" -ptrdiff_t *graphemes(const char *text, PangoContext *context) +int graphemesTakesUTF16(void) { - size_t len, lenchars; - PangoLogAttr *logattrs; - ptrdiff_t *out; - ptrdiff_t *op; - size_t i; + return 0; +} - 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); - logattrs = (PangoLogAttr *) uiAlloc((lenchars + 1) * sizeof (PangoLogAttr), "PangoLogAttr[]"); + logattrs = (PangoLogAttr *) uiAlloc((lenchars + 1) * sizeof (PangoLogAttr), "PangoLogAttr[] (graphemes)"); pango_get_log_attrs(text, len, -1, NULL, logattrs, lenchars + 1); - // should be more than enough - out = (ptrdiff_t *) uiAlloc((lenchars + 2) * sizeof (ptrdiff_t), "ptrdiff_t[]"); - op = out; + // 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++; + + 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++) if (logattrs[i].is_cursor_position != 0) // TODO optimize this @@ -26,6 +43,20 @@ ptrdiff_t *graphemes(const char *text, PangoContext *context) // and do the last one *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); - return out; + return g; } diff --git a/unix/uipriv_unix.h b/unix/uipriv_unix.h index 33ff1e35..de2c1d3d 100644 --- a/unix/uipriv_unix.h +++ b/unix/uipriv_unix.h @@ -49,9 +49,6 @@ extern void freeContext(uiDrawContext *); extern uiDrawTextFont *mkTextFont(PangoFont *f, gboolean add); extern PangoFont *pangoDescToPangoFont(PangoFontDescription *pdesc); -// graphemes.c -extern ptrdiff_t *graphemes(const char *text, PangoContext *context); - // image.c /*TODO remove this*/typedef struct uiImage uiImage; extern cairo_surface_t *imageAppropriateSurface(uiImage *i, GtkWidget *w); diff --git a/windows/uipriv_windows.hpp b/windows/uipriv_windows.hpp index 6ffe09f1..2354bfd7 100644 --- a/windows/uipriv_windows.hpp +++ b/windows/uipriv_windows.hpp @@ -148,9 +148,6 @@ extern BOOL showColorDialog(HWND parent, struct colorDialogRGBA *c); // sizing.cpp 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 extern D2D1_SIZE_F realGetSize(ID2D1RenderTarget *rt);