From b2cd5ef851a7101f13fe3dff438ddbbdbd4d009f Mon Sep 17 00:00:00 2001 From: Pietro Gagliardi Date: Mon, 13 Feb 2017 01:22:59 -0500 Subject: [PATCH] Wrote code to draw the background of text. --- common/drawtext.c | 38 ++++++++++++++++++++++++++++++++++++++ common/uipriv.h | 1 + 2 files changed, 39 insertions(+) diff --git a/common/drawtext.c b/common/drawtext.c index cbfe3ccb..9cc04966 100644 --- a/common/drawtext.c +++ b/common/drawtext.c @@ -49,3 +49,41 @@ void uiDrawCaret(uiDrawContext *c, double x, double y, uiDrawTextLayout *layout, uiDrawRestore(c); } + +void drawTextBackground(uiDrawContext *c, double x, double y, uiDrawTextLayout *layout, size_t start, size_t end, uiDrawBrush *brush, int isSelection) +{ + int line, nLines; + size_t lstart, lend; + double layoutwid, layoutht; + + uiDrawTextLayoutExtents(layout, &layoutwid, &layoutht); + nLines = uiDrawTextLayoutNumLines(layout); + for (line = 0; line < nLines; line++) { + uiDrawTextLayoutLineByteRange(layout, line, &lstart, &lend); + if (start >= lstart && start < lend) + break; + } + while (end - start > 0) { + uiDrawTextLayoutLineMetrics m; + double startx, endx; + uiDrawPath *path; + + if (lend > end) // don't cross lines + lend = end; + startx = uiDrawTextLayoutByteLocationInLine(layout, line, start); + // TODO explain this + endx = layoutwid; + if (!isSelection || end <= lend) + endx = uiDrawTextLayoutByteLocationInLine(layout, line, end); + uiDrawTextLayoutLineGetMetrics(layout, line, &m); + path = uiDrawNewPath(uiDrawFillModeWinding); + uiDrawPathAddRectangle(path, + x + startx, y + m.Y, + endx - startx, m.Height); + uiDrawPathEnd(path); + uiDrawFill(c, path, brush); + uiDrawFreePath(path); + line++; + start += lend - start; + } +} diff --git a/common/uipriv.h b/common/uipriv.h index 9d0dc8e6..553073f5 100644 --- a/common/uipriv.h +++ b/common/uipriv.h @@ -99,6 +99,7 @@ struct caretDrawParams { double width; }; extern void caretDrawParams(uiDrawContext *c, double height, struct caretDrawParams *p); +extern void drawTextBackground(uiDrawContext *c, double x, double y, uiDrawTextLayout *layout, size_t start, size_t end, uiDrawBrush *brush, int isSelection); #ifdef __cplusplus }