@@ -16,6 +16,8 @@ module ttf
16
16
import encoding.utf8
17
17
import math
18
18
19
+ // BitMap represents a bitmap image of text rendered with the font supplied via
20
+ // the `tf` field.
19
21
pub struct BitMap {
20
22
pub mut :
21
23
tf & TTF_File = unsafe { nil }
@@ -50,33 +52,33 @@ pub mut:
50
52
* Utility
51
53
*
52
54
******************************************************************************/
53
- // clear clear the bitmap with 0 bytes
55
+ // clear clears the bitmap with 0 bytes.
54
56
pub fn (mut bmp BitMap) clear () {
55
57
mut sz := bmp.width * bmp.height * bmp.bp
56
58
unsafe {
57
59
vmemset (bmp.buf, 0x00 , sz)
58
60
}
59
61
}
60
62
61
- // transform matrix applied to the text
63
+ // trf_txt returns the transform matrix applied to the text.
62
64
pub fn (bmp &BitMap) trf_txt (p & Point) (int , int ) {
63
65
return int (p.x * bmp.tr_matrix[0 ] + p.y * bmp.tr_matrix[3 ] + bmp.tr_matrix[6 ]), int (
64
66
p.x * bmp.tr_matrix[1 ] + p.y * bmp.tr_matrix[4 ] + bmp.tr_matrix[7 ])
65
67
}
66
68
67
- // transform matrix applied to the char
69
+ // trf_ch returns the transform matrix applied to the char.
68
70
pub fn (bmp &BitMap) trf_ch (p & Point) (int , int ) {
69
71
return int (p.x * bmp.ch_matrix[0 ] + p.y * bmp.ch_matrix[3 ] + bmp.ch_matrix[6 ]), int (
70
72
p.x * bmp.ch_matrix[1 ] + p.y * bmp.ch_matrix[4 ] + bmp.ch_matrix[7 ])
71
73
}
72
74
73
- // set draw position in the buffer
75
+ // set_pos sets the draw position in the buffer
74
76
pub fn (mut bmp BitMap) set_pos (x f32 , y f32 ) {
75
77
bmp.tr_matrix[6 ] = x
76
78
bmp.tr_matrix[7 ] = y
77
79
}
78
80
79
- // set the rotation angle in radiants
81
+ // set_rotation sets the rotation angle in radians `a`
80
82
pub fn (mut bmp BitMap) set_rotation (a f32 ) {
81
83
bmp.tr_matrix[0 ] = f32 (math.cos (a)) // 1
82
84
bmp.tr_matrix[1 ] = f32 (- math.sin (a)) // 0
@@ -89,6 +91,7 @@ pub fn (mut bmp BitMap) set_rotation(a f32) {
89
91
* Filler functions
90
92
*
91
93
******************************************************************************/
94
+ // init_filler initializes the internal `filler` buffer.
92
95
pub fn (mut bmp BitMap) init_filler () {
93
96
h := bmp.height - bmp.filler.len
94
97
if h < 1 {
@@ -100,12 +103,14 @@ pub fn (mut bmp BitMap) init_filler() {
100
103
// dprintln("Init filler: ${bmp.filler.len} rows")
101
104
}
102
105
106
+ // clear_filler clears the internal `filler` buffer
103
107
pub fn (mut bmp BitMap) clear_filler () {
104
108
for i in 0 .. bmp.height {
105
109
bmp.filler[i].clear ()
106
110
}
107
111
}
108
112
113
+ // exec_filler plots the pixels of the `BitMap` to the internal buffer.
109
114
pub fn (mut bmp BitMap) exec_filler () {
110
115
for y in 0 .. bmp.height {
111
116
if bmp.filler[y].len > 0 {
@@ -131,6 +136,7 @@ pub fn (mut bmp BitMap) exec_filler() {
131
136
}
132
137
}
133
138
139
+ // fline populates the internal `filler` buffer with a line segment from `in_x0`,`in_y0` to `in_x1`,`in_y1`.
134
140
pub fn (mut bmp BitMap) fline (in_x0 int , in_y0 int , in_x1 int , in_y1 int , c u32 ) {
135
141
mut x0 := f32 (in_x0 )
136
142
mut x1 := f32 (in_x1 )
@@ -187,6 +193,8 @@ pub fn (mut bmp BitMap) fline(in_x0 int, in_y0 int, in_x1 int, in_y1 int, c u32)
187
193
* Draw functions
188
194
*
189
195
******************************************************************************/
196
+
197
+ // plot plots a pixel at `x`,`y` in color `c` in the internal bitmap buffer.
190
198
@[inline]
191
199
pub fn (mut bmp BitMap) plot (x int , y int , c u32 ) bool {
192
200
if x < 0 || x > = bmp.width || y < 0 || y > = bmp.height {
@@ -212,7 +220,7 @@ pub fn (mut bmp BitMap) plot(x int, y int, c u32) bool {
212
220
* smooth draw functions
213
221
*
214
222
******************************************************************************/
215
- // aline draw an aliased line on the bitmap
223
+ // aline draws an aliased line on the bitmap
216
224
pub fn (mut bmp BitMap) aline (in_x0 int , in_y0 int , in_x1 int , in_y1 int , c u32 ) {
217
225
// mut c1 := c
218
226
mut x0 := f32 (in_x0 )
@@ -307,6 +315,7 @@ pub fn (mut bmp BitMap) aline(in_x0 int, in_y0 int, in_x1 int, in_y1 int, c u32)
307
315
* draw functions
308
316
*
309
317
******************************************************************************/
318
+ // line plots a line segment to the internal buffer from `in_x0`,`in_y0` to `in_x1`,`in_y1` in the color `c`.
310
319
pub fn (mut bmp BitMap) line (in_x0 int , in_y0 int , in_x1 int , in_y1 int , c u32 ) {
311
320
// outline with aliased borders
312
321
if bmp.style == .outline_aliased {
@@ -387,13 +396,15 @@ pub fn (mut bmp BitMap) line(in_x0 int, in_y0 int, in_x1 int, in_y1 int, c u32)
387
396
}
388
397
}
389
398
399
+ // box plots a (hollow) box to the internal buffer from top-left `in_x0`, `in_y0` to bottom right `in_x1`, `in_y1` in color `c`.
390
400
pub fn (mut bmp BitMap) box (in_x0 int , in_y0 int , in_x1 int , in_y1 int , c u32 ) {
391
401
bmp.line (in_x0 , in_y0 , in_x1 , in_y0 , c)
392
402
bmp.line (in_x1 , in_y0 , in_x1 , in_y1 , c)
393
403
bmp.line (in_x0 , in_y1 , in_x1 , in_y1 , c)
394
404
bmp.line (in_x0 , in_y0 , in_x0 , in_y1 , c)
395
405
}
396
406
407
+ // quadratic plots a quadratic Bezier curve in color `c`.
397
408
pub fn (mut bmp BitMap) quadratic (in_x0 int , in_y0 int , in_x1 int , in_y1 int , in_cx int , in_cy int , c u32 ) {
398
409
/*
399
410
x0 := int(in_x0 * bmp.scale)
@@ -460,6 +471,7 @@ pub fn (mut bmp BitMap) quadratic(in_x0 int, in_y0 int, in_x1 int, in_y1 int, in
460
471
* TTF Query functions
461
472
*
462
473
******************************************************************************/
474
+ // get_chars_bbox returns all characters found in bounding box of string `in_string`.
463
475
pub fn (mut bmp BitMap) get_chars_bbox (in_string string ) []int {
464
476
mut res := []int {}
465
477
mut w := 0
@@ -532,6 +544,7 @@ pub fn (mut bmp BitMap) get_chars_bbox(in_string string) []int {
532
544
return res
533
545
}
534
546
547
+ // get_bbox returns the bounding box (width and height) of text `in_string`.
535
548
pub fn (mut bmp BitMap) get_bbox (in_string string ) (int , int ) {
536
549
mut w := 0
537
550
@@ -627,6 +640,8 @@ fn (mut bmp BitMap) draw_notdef_glyph(in_x int, in_w int) {
627
640
bmp.line (int (x - in_w), int (y), int (x), int (y - y_h), bmp.color)
628
641
}
629
642
643
+ // draw_text plots the pixels of the text `in_string` to the internal buffer and
644
+ // returns the text bounding box.
630
645
pub fn (mut bmp BitMap) draw_text (in_string string ) (int , int ) {
631
646
mut w := 0
632
647
@@ -702,6 +717,8 @@ pub fn (mut bmp BitMap) draw_text(in_string string) (int, int) {
702
717
return w, int (math.abs (int (bmp.tf.y_max - bmp.tf.y_min)) * bmp.scale)
703
718
}
704
719
720
+ // draw_glyph plots the pixels of the glyph at `index` to the internal buffer and
721
+ // returns the `x_max` and `x_min` values.
705
722
pub fn (mut bmp BitMap) draw_glyph (index u16 ) (int , int ) {
706
723
glyph := bmp.tf.read_glyph (index)
707
724
0 commit comments