static unsafe void DrawParagraph(NanoVGContext vg, float x, float y, float width, float height, float mx, float my) { NVGtextRow[] rows = new NVGtextRow[3]; NVGglyphPosition[] glyphs = new NVGglyphPosition[100]; string text = "This is longer chunk of text.\n \n Would have used lorem ipsum but she was busy jumping over the lazy dog with the fox and all the men who came to the aid of the party.🎉"; string start; string end; int nrows, i, nglyphs, j, lnum = 0; float lineh; float caretx, px; float[] bounds = new float[4]; float a; float gx = 0, gy = 0; int gutter = 0; //NVG_NOTUSED(height); NVG.Save(vg); NVG.FontSize(vg, 18.0f); NVG.FontFace(vg, "sans"); NVG.TextAlign(vg, NVGAlign.NVG_ALIGN_LEFT | NVGAlign.NVG_ALIGN_TOP); NVG.TextMetrics(vg, null, null, &lineh); // The text break API can be used to fill a large buffer of rows, // or to iterate over the text just few lines (or just one) at a time. // The "next" variable of the last returned item tells where to continue. start = text; end = text + text.Length; IntPtr rowPtr = NVG.GetIntPtrFromStructArray(rows); //GCHandle gch = GCHandle.Alloc(rows, GCHandleType.Normal); //IntPtr rowPtr = GCHandle.ToIntPtr(gch); nrows = NVG.TextBreakLines(vg, Encoding.UTF8.GetBytes(start), Encoding.UTF8.GetBytes(end), width, ref rowPtr, 3); var cRows = NVG.GetStructArrayFromIntPtr <NVGtextRow>(rowPtr, nrows); while (nrows > 0) { for (i = 0; i < nrows; i++) { NVGtextRow row = rows[i]; bool hit = mx > x && mx < (x + width) && my >= y && my < (y + lineh); NVG.BeginPath(vg); NVG.FillColor(vg, NVG.RGBA(255, 255, 255, hit ? (byte)64 : (byte)16)); NVG.Rect(vg, x, y, row.width, lineh); NVG.Fill(vg); NVG.FillColor(vg, NVG.RGBA(255, 255, 255, 255)); NVG.Text(vg, x, y, row.start, row.end); if (hit) { caretx = (mx < x + row.width / 2) ? x : x + row.width; px = x; IntPtr positions = NVG.MarshalArrayToPointer(glyphs); nglyphs = NVG.TextGlyphPositions(vg, x, y, NVG.StrToByte(row.start), NVG.StrToByte(row.end), positions, 100); for (j = 0; j < nglyphs; j++) { float x0 = glyphs[j].x; float x1 = (j + 1 < nglyphs) ? glyphs[j + 1].x : x + row.width; float gx2 = x0 * 0.3f + x1 * 0.7f; if (mx >= px && mx < gx2) { caretx = glyphs[j].x; } px = gx2; } NVG.BeginPath(vg); NVG.FillColor(vg, NVG.RGBA(255, 192, 0, 255)); NVG.Rect(vg, caretx, y, 1, lineh); NVG.Fill(vg); gutter = lnum + 1; gx = x - 10; gy = y + lineh / 2; } lnum++; y += lineh; } // Keep going... start = rows[nrows - 1].next; //nrows = NVG.TextBreakLines(vg, Encoding.UTF8.GetBytes(start), Encoding.UTF8.GetBytes(end), width, rowPtr, 3); } if (gutter > 0) { string txt; txt = "16" + gutter; NVG.FontSize(vg, 13.0f); NVG.TextAlign(vg, NVGAlign.NVG_ALIGN_RIGHT | NVGAlign.NVG_ALIGN_MIDDLE); NVG.TextBounds(vg, gx, gy, Encoding.UTF8.GetBytes(txt), null, NVG.MarshalToPointer(bounds)); NVG.BeginPath(vg); NVG.FillColor(vg, NVG.RGBA(255, 192, 0, 255)); NVG.RoundedRect(vg, (int)bounds[0] - 4, (int)bounds[1] - 2, (int)(bounds[2] - bounds[0]) + 8, (int)(bounds[3] - bounds[1]) + 4, ((int)(bounds[3] - bounds[1]) + 4) / 2 - 1); NVG.Fill(vg); NVG.FillColor(vg, NVG.RGBA(32, 32, 32, 255)); NVG.Text(vg, gx, gy, txt, null); } y += 20.0f; NVG.FontSize(vg, 13.0f); NVG.TextAlign(vg, NVGAlign.NVG_ALIGN_LEFT | NVGAlign.NVG_ALIGN_TOP); NVG.TextLineHeight(vg, 1.2f); IntPtr boundPtr = NVG.MarshalArrayToPointer(glyphs); NVG.TextBoxBounds(vg, x, y, 150, Encoding.UTF8.GetBytes("Hover your mouse over the text to see calculated caret position."), null, boundPtr); // Fade the tooltip out when close to it. gx = Math.Abs((mx - (bounds[0] + bounds[2]) * 0.5f) / (bounds[0] - bounds[2])); gy = Math.Abs((my - (bounds[1] + bounds[3]) * 0.5f) / (bounds[1] - bounds[3])); a = Math.Max(gx, gy) - 0.5f; a = Math.Clamp(a, 0, 1); NVG.GlobalAlpha(vg, a); NVG.BeginPath(vg); NVG.FillColor(vg, NVG.RGBA(220, 220, 220, 255)); NVG.RoundedRect(vg, bounds[0] - 2, bounds[1] - 2, (int)(bounds[2] - bounds[0]) + 4, (int)(bounds[3] - bounds[1]) + 4, 3); px = (int)((bounds[2] + bounds[0]) / 2); NVG.MoveTo(vg, px, bounds[1] - 10); NVG.LineTo(vg, px + 7, bounds[1] + 1); NVG.LineTo(vg, px - 7, bounds[1] + 1); NVG.Fill(vg); NVG.FillColor(vg, NVG.RGBA(0, 0, 0, 220)); NVG.TextBox(vg, x, y, 150, Encoding.UTF8.GetBytes("Hover your mouse over the text to see calculated caret position."), null); NVG.Restore(vg); }
public static NVGglyphPosition[] TextGlyphPositions(float X, float Y, string Txt) { NVGglyphPosition[] GlyphPositions = new NVGglyphPosition[Txt.Length]; NVG.TextGlyphPositions(Engine.NVGCtx, X, Y, Txt, IntPtr.Zero, GlyphPositions, GlyphPositions.Length); return(GlyphPositions); }