|
1 | 1 | using System;
|
| 2 | +using System.Collections.Generic; |
2 | 3 | using System.Runtime.InteropServices;
|
| 4 | +using System.Text; |
3 | 5 |
|
4 | 6 | namespace NanoVG
|
5 | 7 | {
|
6 |
| - public static unsafe partial class NVG |
| 8 | + public static partial class NVG |
7 | 9 | {
|
8 | 10 | static NVG()
|
9 | 11 | {
|
@@ -44,10 +46,186 @@ public struct NVGcolor
|
44 | 46 | public float r,g,b,a;
|
45 | 47 | }
|
46 | 48 |
|
| 49 | + public class TextRow |
| 50 | + { |
| 51 | + public string SourceText { get; } |
| 52 | + public string Text { get; } |
| 53 | + public int NextLinePosition { get; } |
| 54 | + public float MaxX { get; } |
| 55 | + public float MinX { get; } |
| 56 | + public float Width { get; } |
| 57 | + |
| 58 | + internal TextRow(string sourceText, string text, int nextLinePos, float minX, float maxX, float width) |
| 59 | + { |
| 60 | + SourceText = sourceText; |
| 61 | + Text = text; |
| 62 | + NextLinePosition = nextLinePos; |
| 63 | + MinX = minX; |
| 64 | + MaxX = maxX; |
| 65 | + Width = width; |
| 66 | + } |
| 67 | + } |
| 68 | + |
47 | 69 | public enum NVGcreateFlags
|
48 | 70 | {
|
49 | 71 | NVG_ANTIALIAS = 1<<0,
|
50 | 72 | NVG_STENCIL_STROKES = 1<<1,
|
51 | 73 | NVG_DEBUG = 1<<2,
|
52 | 74 | }
|
| 75 | + |
| 76 | + public static partial class NVG |
| 77 | + { |
| 78 | + private static T GetStringPointers<T>(string str, int length, Func<IntPtr, IntPtr, T> callback) |
| 79 | + { |
| 80 | + if(length > str.Length) |
| 81 | + throw new ArgumentOutOfRangeException(nameof(length)); |
| 82 | + |
| 83 | + byte[] utf8 = Encoding.UTF8.GetBytes(str); |
| 84 | + int byteLength = length > 0 ? Encoding.UTF8.GetByteCount(str, 0, length) : 0; |
| 85 | + |
| 86 | + GCHandle handle = default; |
| 87 | + try |
| 88 | + { |
| 89 | + handle = GCHandle.Alloc(utf8, GCHandleType.Pinned); |
| 90 | + |
| 91 | + IntPtr strPtr = handle.AddrOfPinnedObject(); |
| 92 | + IntPtr endPtr = length > 0 ? endPtr = IntPtr.Add(strPtr, byteLength) : IntPtr.Zero; |
| 93 | + |
| 94 | + return callback(strPtr, endPtr); |
| 95 | + } |
| 96 | + finally |
| 97 | + { |
| 98 | + if(handle.IsAllocated) |
| 99 | + handle.Free(); |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + /// <summary> |
| 104 | + /// Draws text string at specified location. |
| 105 | + /// </summary> |
| 106 | + public static float Text(this NVGcontext ctx, float x, float y, string @string, int length = -1) |
| 107 | + { |
| 108 | + return GetStringPointers(@string, length, (strPtr, endPtr) => Text(ctx, x, y, strPtr, endPtr)); |
| 109 | + } |
| 110 | + |
| 111 | + /// <summary> |
| 112 | + /// Draws multi-line text string at specified location wrapped at the specified width. <br/> |
| 113 | + /// White space is stripped at the beginning of the rows, the text is split at word boundaries or when new-line characters are encountered. <br/> |
| 114 | + /// Words longer than the max width are slit at nearest character (i.e. no hyphenation). |
| 115 | + /// </summary> |
| 116 | + public static void TextBox(this NVGcontext ctx, float x, float y, float breakRowWidth, string @string, int length = -1) |
| 117 | + { |
| 118 | + GetStringPointers(@string, length, (strPtr, endPtr) => { |
| 119 | + TextBox(ctx, x, y, breakRowWidth, strPtr, endPtr); |
| 120 | + return (object)null; |
| 121 | + }); |
| 122 | + } |
| 123 | + |
| 124 | + /// <summary> |
| 125 | + /// Measures the specified text string. Parameter bounds should be a pointer to float[4], <br/> |
| 126 | + /// if the bounding box of the text should be returned. The bounds value are [xmin,ymin, xmax,ymax] <br/> |
| 127 | + /// Returns the horizontal advance of the measured text (i.e. where the next character should drawn). <br/> |
| 128 | + /// Measured values are returned in local coordinate space. |
| 129 | + /// </summary> |
| 130 | + public static float TextBounds(this NVGcontext ctx, float x, float y, string @string, out float[] bounds, int strLength = -1) |
| 131 | + { |
| 132 | + float[] retBounds = new float[4]; |
| 133 | + float retVal = GetStringPointers(@string, strLength, (strPtr, endPtr) => TextBounds(ctx, x, y, strPtr, endPtr, retBounds)); |
| 134 | + |
| 135 | + bounds = retBounds; |
| 136 | + return retVal; |
| 137 | + } |
| 138 | + |
| 139 | + /// <summary> |
| 140 | + /// Measures the specified multi-text string. Parameter bounds should be a pointer to float[4], <br/> |
| 141 | + /// if the bounding box of the text should be returned. The bounds value are [xmin,ymin, xmax,ymax] <br/> |
| 142 | + /// Measured values are returned in local coordinate space. |
| 143 | + /// </summary> |
| 144 | + public static void TextBoxBounds(this NVGcontext ctx, float x, float y, float breakRowWidth, string @string, out float[] bounds, int strLength = -1) |
| 145 | + { |
| 146 | + float[] retBounds = new float[4]; |
| 147 | + |
| 148 | + GetStringPointers(@string, strLength, (strPtr, endPtr) => { |
| 149 | + TextBoxBounds(ctx, x, y, breakRowWidth, strPtr, endPtr, retBounds); |
| 150 | + return (object)null; |
| 151 | + }); |
| 152 | + |
| 153 | + bounds = retBounds; |
| 154 | + } |
| 155 | + |
| 156 | + /// <summary> |
| 157 | + /// Calculates the glyph x positions of the specified text. Measured values are returned in local coordinate space. |
| 158 | + /// </summary> |
| 159 | + public static NVGglyphPosition[] TextGlyphPositions(this NVGcontext ctx, float x, float y, string @string, int maxPositions, int strLength = -1) |
| 160 | + { |
| 161 | + if(maxPositions <= 0) |
| 162 | + return Array.Empty<NVGglyphPosition>(); |
| 163 | + |
| 164 | + NVGglyphPosition[] positions = new NVGglyphPosition[maxPositions]; |
| 165 | + int posCount = GetStringPointers(@string, strLength, (strPtr, endPtr) => { |
| 166 | + unsafe |
| 167 | + { |
| 168 | + fixed(NVGglyphPosition* ptr = &positions[0]) |
| 169 | + { |
| 170 | + return TextGlyphPositions(ctx, x, y, strPtr, endPtr, new IntPtr(ptr), maxPositions); |
| 171 | + } |
| 172 | + } |
| 173 | + }); |
| 174 | + |
| 175 | + if(posCount <= 0) |
| 176 | + return Array.Empty<NVGglyphPosition>(); |
| 177 | + |
| 178 | + if(posCount != positions.Length) |
| 179 | + Array.Resize(ref positions, posCount); |
| 180 | + |
| 181 | + return positions; |
| 182 | + } |
| 183 | + |
| 184 | + /// <summary> |
| 185 | + /// Breaks the specified text into lines. <br/> |
| 186 | + /// White space is stripped at the beginning of the rows, the text is split at word boundaries or when new-line characters are encountered. <br/> |
| 187 | + /// Words longer than the max width are slit at nearest character (i.e. no hyphenation). |
| 188 | + /// </summary> |
| 189 | + public static TextRow[] TextBreakLines(this NVGcontext ctx, string @string, float breakRowWidth, int maxRows, int strLength = -1) |
| 190 | + { |
| 191 | + if(@string.Length <= 0) |
| 192 | + return Array.Empty<TextRow>(); |
| 193 | + |
| 194 | + NVGtextRow[] _rows = new NVGtextRow[maxRows]; |
| 195 | + byte[] str = Encoding.UTF8.GetBytes(@string); |
| 196 | + long startOffset = 0; |
| 197 | + |
| 198 | + int rowCount = GetStringPointers(@string, strLength, (strPtr, endPtr) => { |
| 199 | + unsafe |
| 200 | + { |
| 201 | + fixed(NVGtextRow* ptr = &_rows[0]) |
| 202 | + { |
| 203 | + int rowCount = TextBreakLines(ctx, strPtr, endPtr, breakRowWidth, new IntPtr(ptr), maxRows); |
| 204 | + startOffset = strPtr.ToInt64(); |
| 205 | + return rowCount; |
| 206 | + } |
| 207 | + } |
| 208 | + }); |
| 209 | + |
| 210 | + if(rowCount <= 0) |
| 211 | + return Array.Empty<TextRow>(); |
| 212 | + |
| 213 | + List<TextRow> rows = new List<TextRow>(); |
| 214 | + |
| 215 | + for(int i = 0; i < rowCount; i++) |
| 216 | + { |
| 217 | + NVGtextRow row = _rows[i]; |
| 218 | + int start = (int)(row.start.ToInt64() - startOffset); |
| 219 | + int length = (int)(row.end.ToInt64() - row.start.ToInt64()); |
| 220 | + int nextLine = (int)(row.next.ToInt64() - startOffset); |
| 221 | + |
| 222 | + // Convert from byte pos to char pos |
| 223 | + nextLine = Encoding.UTF8.GetCharCount(str, 0, nextLine); |
| 224 | + |
| 225 | + rows.Add(new TextRow(@string, Encoding.UTF8.GetString(str, start, length), nextLine, row.minx, row.maxx, row.width)); |
| 226 | + } |
| 227 | + |
| 228 | + return rows.ToArray(); |
| 229 | + } |
| 230 | + } |
53 | 231 | }
|
0 commit comments