From 86b8b0bccabdaf105c543466f1a40bf188ba8233 Mon Sep 17 00:00:00 2001 From: ands Date: Thu, 23 Oct 2014 10:34:27 +0200 Subject: [PATCH] bug fixes for special cases and a workaround for an annoying nanovg text bound measuring problem if the text is empty --- blendish.h | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/blendish.h b/blendish.h index 10cd0e9..9bafad1 100644 --- a/blendish.h +++ b/blendish.h @@ -2216,6 +2216,7 @@ int bndIconLabelTextPosition(NVGcontext *ctx, float x, float y, float w, float h static NVGtextRow rows[BND_MAX_ROWS]; int nrows = nvgTextBreakLines( ctx, label, NULL, w, rows, BND_MAX_ROWS); + if (nrows == 0) return 0; nvgTextBoxBounds(ctx, x, y, w, label, NULL, bounds); nvgTextMetrics(ctx, &asc, &desc, &lh); @@ -2234,15 +2235,17 @@ int bndIconLabelTextPosition(NVGcontext *ctx, float x, float y, float w, float h return p; } -static void bndCaretPosition(NVGcontext *ctx, float x, float y,float top, +static void bndCaretPosition(NVGcontext *ctx, float x, float y, float *bounds, float lineHeight, const char *caret, NVGtextRow *rows,int nrows, int *cr, float *cx, float *cy) { static NVGglyphPosition glyphs[BND_MAX_GLYPHS]; int r,nglyphs; for (r=0; r < nrows && rows[r].end < caret; ++r); *cr = r; + *cx = bounds[0]; + *cy = bounds[1] + r*lineHeight; + if (nrows == 0) return; *cx = rows[r].minx; - *cy = top+r*lineHeight; nglyphs = nvgTextGlyphPositions( ctx, x, y, rows[r].start, rows[r].end+1, glyphs, BND_MAX_GLYPHS); for (int i=0; i < nglyphs; ++i) { @@ -2283,9 +2286,13 @@ void bndIconLabelCaret(NVGcontext *ctx, float x, float y, float w, float h, nvgTextBoxBounds(ctx, x, y, w, label, NULL, bounds); nvgTextMetrics(ctx, &asc, &desc, &lh); - bndCaretPosition(ctx, x, y, bounds[1], lh, label+cbegin, + // hardcoded workaround for broken bounds[1] value if text is empty + if (bounds[1] == y) + bounds[1] -= 10.37f; // ?!?! + + bndCaretPosition(ctx, x, y, bounds, lh, label+cbegin, rows, nrows, &c0r, &c0x, &c0y); - bndCaretPosition(ctx, x, y, bounds[1], lh, label+cend, + bndCaretPosition(ctx, x, y, bounds, lh, label+cend, rows, nrows, &c1r, &c1x, &c1y); nvgBeginPath(ctx);