You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+18Lines changed: 18 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -63,6 +63,8 @@ const { lines } = layoutWithLines(prepared, 320, 26) // 320px max width, 26px li
63
63
for (let i =0; i<lines.length; i++) ctx.fillText(lines[i].text, 0, i*26)
64
64
```
65
65
66
+
Each rich line also carries `sourceOffset` / `sourceLength`, so callers can map a rendered line back to the original input without reconstructing whitespace normalization themselves.
67
+
66
68
-`walkLineRanges()` gives you line widths and cursors without building the text strings:
67
69
68
70
```ts
@@ -88,6 +90,16 @@ while (true) {
88
90
}
89
91
```
90
92
93
+
-`cursorToSourceOffset()` / `cursorRangeToSourceSpan()` convert rich cursors back into original-source offsets:
94
+
95
+
```ts
96
+
const line =layoutNextLine(prepared, cursor, width)
This usage allows rendering to canvas, SVG, WebGL and (eventually) server-side.
92
104
93
105
### API Glossary
@@ -109,11 +121,15 @@ type LayoutLine = {
109
121
width:number// Measured width of this line, e.g. 87.5
110
122
start:LayoutCursor// Inclusive start cursor in prepared segments/graphemes
111
123
end:LayoutCursor// Exclusive end cursor in prepared segments/graphemes
124
+
sourceOffset:number// Source offset in the original input covered by this line
125
+
sourceLength:number// Source span length in the original input covered by this line
112
126
}
113
127
typeLayoutLineRange= {
114
128
width:number// Measured width of this line, e.g. 87.5
115
129
start:LayoutCursor// Inclusive start cursor in prepared segments/graphemes
116
130
end:LayoutCursor// Exclusive end cursor in prepared segments/graphemes
131
+
sourceOffset:number// Source offset in the original input covered by this line
132
+
sourceLength:number// Source span length in the original input covered by this line
117
133
}
118
134
typeLayoutCursor= {
119
135
segmentIndex:number// Segment index in prepareWithSegments' prepared rich segment stream
@@ -125,6 +141,8 @@ Other helpers:
125
141
```ts
126
142
clearCache():void// clears Pretext's shared internal caches used by prepare() and prepareWithSegments(). Useful if your app cycles through many different fonts or text variants and you want to release the accumulated cache
127
143
setLocale(locale?:string):void// optional (by default we use the current locale). Sets locale for future prepare() and prepareWithSegments(). Internally, it also calls clearCache(). Setting a new locale doesn't affect existing prepare() and prepareWithSegments() states (no mutations to them)
144
+
cursorToSourceOffset(prepared:PreparedTextWithSegments, cursor:LayoutCursor):number// converts a rich cursor back into an offset in the original input text
145
+
cursorRangeToSourceSpan(prepared:PreparedTextWithSegments, start:LayoutCursor, end:LayoutCursor): { sourceOffset:number, sourceLength:number } // converts a rich cursor range back into the original input span
0 commit comments