Browse Source

Fixed nvgTextBreakLines returning bad minx

- nvgTextBreakLines: wordMinX was relative to rowStartX, which caused
problems when rowStartX change on line break, but wordMinX was still
relative to the previous rowStartX
- fixed font size on perf graphs
- made demo to use row minx/maxx instead of row->width
- fixed demo hover distance
shared-context
Mikko Mononen 2 years ago
parent
commit
e281ffcabd
3 changed files with 24 additions and 22 deletions
  1. +9
    -7
      example/demo.c
  2. +11
    -11
      example/perf.c
  3. +4
    -4
      src/nanovg.c

+ 9
- 7
example/demo.c View File

@@ -25,7 +25,7 @@
#define ICON_TRASH 0xE729

//static float minf(float a, float b) { return a < b ? a : b; }
static float maxf(float a, float b) { return a > b ? a : b; }
//static float maxf(float a, float b) { return a > b ? a : b; }
//static float absf(float a) { return a >= 0.0f ? a : -a; }
static float clampf(float a, float mn, float mx) { return a < mn ? mn : (a > mx ? mx : a); }

@@ -868,8 +868,10 @@ void drawParagraph(NVGcontext* vg, float x, float y, float width, float height,
float caretx, px;
float bounds[4];
float a;
const char* hoverText = "Hover your mouse over the text to see calculated caret position.";
float gx,gy;
int gutter = 0;
const char* boxText = "Testing\nsome multiline\ntext.";
NVG_NOTUSED(height);

nvgSave(vg);
@@ -891,7 +893,7 @@ void drawParagraph(NVGcontext* vg, float x, float y, float width, float height,

nvgBeginPath(vg);
nvgFillColor(vg, nvgRGBA(255,255,255,hit?64:16));
nvgRect(vg, x, y, row->width, lineh);
nvgRect(vg, x + row->minx, y, row->maxx - row->minx, lineh);
nvgFill(vg);

nvgFillColor(vg, nvgRGBA(255,255,255,255));
@@ -948,12 +950,12 @@ void drawParagraph(NVGcontext* vg, float x, float y, float width, float height,
nvgTextAlign(vg, NVG_ALIGN_LEFT|NVG_ALIGN_TOP);
nvgTextLineHeight(vg, 1.2f);

nvgTextBoxBounds(vg, x,y, 150, "Hover your mouse over the text to see calculated caret position.", NULL, bounds);
nvgTextBoxBounds(vg, x,y, 150, hoverText, NULL, bounds);

// Fade the tooltip out when close to it.
gx = fabsf((mx - (bounds[0]+bounds[2])*0.5f) / (bounds[0] - bounds[2]));
gy = fabsf((my - (bounds[1]+bounds[3])*0.5f) / (bounds[1] - bounds[3]));
a = maxf(gx, gy) - 0.5f;
gx = clampf(mx, bounds[0], bounds[2]) - mx;
gy = clampf(my, bounds[1], bounds[3]) - my;
a = sqrtf(gx*gx + gy*gy) / 30.0f;
a = clampf(a, 0, 1);
nvgGlobalAlpha(vg, a);

@@ -967,7 +969,7 @@ void drawParagraph(NVGcontext* vg, float x, float y, float width, float height,
nvgFill(vg);

nvgFillColor(vg, nvgRGBA(0,0,0,220));
nvgTextBox(vg, x,y, 150, "Hover your mouse over the text to see calculated caret position.", NULL);
nvgTextBox(vg, x,y, 150, hoverText, NULL);

nvgRestore(vg);
}


+ 11
- 11
example/perf.c View File

@@ -151,36 +151,36 @@ void renderGraph(NVGcontext* vg, float x, float y, PerfGraph* fps)
nvgFontFace(vg, "sans");

if (fps->name[0] != '\0') {
nvgFontSize(vg, 14.0f);
nvgFontSize(vg, 12.0f);
nvgTextAlign(vg, NVG_ALIGN_LEFT|NVG_ALIGN_TOP);
nvgFillColor(vg, nvgRGBA(240,240,240,192));
nvgText(vg, x+3,y+1, fps->name, NULL);
nvgText(vg, x+3,y+3, fps->name, NULL);
}

if (fps->style == GRAPH_RENDER_FPS) {
nvgFontSize(vg, 18.0f);
nvgFontSize(vg, 15.0f);
nvgTextAlign(vg,NVG_ALIGN_RIGHT|NVG_ALIGN_TOP);
nvgFillColor(vg, nvgRGBA(240,240,240,255));
sprintf(str, "%.2f FPS", 1.0f / avg);
nvgText(vg, x+w-3,y+1, str, NULL);
nvgText(vg, x+w-3,y+3, str, NULL);

nvgFontSize(vg, 15.0f);
nvgTextAlign(vg,NVG_ALIGN_RIGHT|NVG_ALIGN_BOTTOM);
nvgFontSize(vg, 13.0f);
nvgTextAlign(vg,NVG_ALIGN_RIGHT|NVG_ALIGN_BASELINE);
nvgFillColor(vg, nvgRGBA(240,240,240,160));
sprintf(str, "%.2f ms", avg * 1000.0f);
nvgText(vg, x+w-3,y+h-1, str, NULL);
nvgText(vg, x+w-3,y+h-3, str, NULL);
}
else if (fps->style == GRAPH_RENDER_PERCENT) {
nvgFontSize(vg, 18.0f);
nvgFontSize(vg, 15.0f);
nvgTextAlign(vg,NVG_ALIGN_RIGHT|NVG_ALIGN_TOP);
nvgFillColor(vg, nvgRGBA(240,240,240,255));
sprintf(str, "%.1f %%", avg * 1.0f);
nvgText(vg, x+w-3,y+1, str, NULL);
nvgText(vg, x+w-3,y+3, str, NULL);
} else {
nvgFontSize(vg, 18.0f);
nvgFontSize(vg, 15.0f);
nvgTextAlign(vg,NVG_ALIGN_RIGHT|NVG_ALIGN_TOP);
nvgFillColor(vg, nvgRGBA(240,240,240,255));
sprintf(str, "%.2f ms", avg * 1000.0f);
nvgText(vg, x+w-3,y+1, str, NULL);
nvgText(vg, x+w-3,y+3, str, NULL);
}
}

+ 4
- 4
src/nanovg.c View File

@@ -2704,7 +2704,7 @@ int nvgTextBreakLines(NVGcontext* ctx, const char* string, const char* end, floa
rowStartX = iter.x;
rowStart = iter.str;
rowEnd = iter.next;
rowWidth = iter.nextx - rowStartX; // q.x1 - rowStartX;
rowWidth = iter.nextx - rowStartX;
rowMinX = q.x0 - rowStartX;
rowMaxX = q.x1 - rowStartX;
wordStart = iter.str;
@@ -2734,7 +2734,7 @@ int nvgTextBreakLines(NVGcontext* ctx, const char* string, const char* end, floa
if ((ptype == NVG_SPACE && (type == NVG_CHAR || type == NVG_CJK_CHAR)) || type == NVG_CJK_CHAR) {
wordStart = iter.str;
wordStartX = iter.x;
wordMinX = q.x0 - rowStartX;
wordMinX = q.x0;
}

// Break to new line when a character is beyond break width.
@@ -2771,13 +2771,13 @@ int nvgTextBreakLines(NVGcontext* ctx, const char* string, const char* end, floa
nrows++;
if (nrows >= maxRows)
return nrows;
// Update row
rowStartX = wordStartX;
rowStart = wordStart;
rowEnd = iter.next;
rowWidth = iter.nextx - rowStartX;
rowMinX = wordMinX;
rowMinX = wordMinX - rowStartX;
rowMaxX = q.x1 - rowStartX;
// No change to the word start
}
// Set null break point
breakEnd = rowStart;


Loading…
Cancel
Save