-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtext.odin
More file actions
60 lines (53 loc) · 1.64 KB
/
Copy pathtext.odin
File metadata and controls
60 lines (53 loc) · 1.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
package gemreq
import "core:strings"
import "vendor:raylib"
Text_Line :: struct {
text: string,
size: [2]f32,
}
Text_Options :: struct {
font_name: string,
font_size: Font_Size,
spacing: f32,
color: raylib.Color,
}
text_wrap :: proc(browser: ^Browser, source: string, config: Text_Options, width: f64) -> []Text_Line {
font := browser.fonts[config.font_name][config.font_size]
font_size := font_size_float(config.font_size)
lines := make([dynamic]Text_Line)
reader := reader_make(source)
if len(source) == 0 do return lines[:]
is_whitespace :: proc(_: int, c: rune) -> bool {
return c == ' ' || c == '\t' || c == '\f' || c == '\v'
}
is_not_whitespace :: proc(_: int, c: rune) -> bool {
return c != ' ' && c != '\t' && c != '\f' && c != '\v'
}
for !reader_eof(&reader) {
text_best_fit: cstring
index_best_fit: int
measure_best_fit: [2]f32
reader_skip_whitespace(&reader)
for !reader_eof(&reader) {
word_length := reader_next_while(&reader, is_not_whitespace)
token := strings.trim_space(reader_peek_token(&reader))
text := strings.clone_to_cstring(token, context.temp_allocator)
measure := raylib.MeasureTextEx(font, text, font_size, config.spacing)
if f64(measure.x) >= width {
index_best_fit = reader.index_curr - word_length
break
}
text_best_fit = text
measure_best_fit = measure
reader_next_while(&reader, is_whitespace)
}
if text_best_fit == nil do break
if index_best_fit != 0 do reader.index_curr = index_best_fit
if len(text_best_fit) > 0 {
token := strings.trim_space(reader_consume(&reader))
line := Text_Line{ token, measure_best_fit }
append(&lines, line)
}
}
return lines[:]
}