Skip to content

Commit 05cbbfb

Browse files
authored
x.ttf: add missing doc strings to the module's public symbols (#23205)
1 parent 570f849 commit 05cbbfb

File tree

5 files changed

+81
-29
lines changed

5 files changed

+81
-29
lines changed

vlib/x/ttf/common.v

Lines changed: 26 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ fn dprintln(txt string) {
4949
* Utility
5050
*
5151
******************************************************************************/
52-
// transform the bitmap from one layer to color layers
52+
// format_texture transforms the bitmap from one layer to color layers
5353
fn (mut bmp BitMap) format_texture() {
5454
r := u8(bmp.color >> 24)
5555
g := u8((bmp.color >> 16) & 0xFF)
@@ -84,7 +84,7 @@ fn (mut bmp BitMap) format_texture() {
8484
}
8585
}
8686

87-
// write out a .ppm file
87+
// save_as_ppm saves the `BitMap` data in .ppm file format to `file_name`.
8888
pub fn (mut bmp BitMap) save_as_ppm(file_name string) {
8989
tmp_buf := bmp.buf
9090
mut buf := unsafe { malloc_noscan(bmp.buf_size) }
@@ -114,6 +114,7 @@ pub fn (mut bmp BitMap) save_as_ppm(file_name string) {
114114
bmp.buf = tmp_buf
115115
}
116116

117+
// get_raw_bytes returns the raw bytes of the bitmap.
117118
pub fn (mut bmp BitMap) get_raw_bytes() []u8 {
118119
mut f_buf := []u8{len: bmp.buf_size / 4}
119120
mut i := 0
@@ -126,6 +127,7 @@ pub fn (mut bmp BitMap) get_raw_bytes() []u8 {
126127
return f_buf
127128
}
128129

130+
// save_raw_data saves the raw data to `file_name`.
129131
pub fn (mut bmp BitMap) save_raw_data(file_name string) {
130132
os.write_file_array(file_name, bmp.get_raw_bytes()) or { panic(err) }
131133
}
@@ -160,36 +162,39 @@ fn rfpart(x f32) f32 {
160162
* Colors
161163
*
162164
******************************************************************************/
163-
/*
164-
@[inline]
165-
pub fn (mut dev BitMap) get_color(x int, y int) (int, int, int, int){
166-
if x < 0 || x >= dev.width || y < 0 || y >= dev.height {
167-
return 0,0,0,0
168-
}
169-
mut i := (x + y * dev.width)*dev.bp
170-
unsafe{
171-
return dev.buf[i], dev.buf[i+1], dev.buf[i+2], dev.buf[i+3]
172-
}
173-
}
174165

175-
@[inline]
176-
pub fn (mut dev BitMap) get_color_u32(x int, y int) u32{
177-
r, g, b, a := dev.get_color(x, y)
178-
unsafe{
179-
return u32(r<<24) | u32(g<<16) | u32(b<<8) | u32(a)
180-
}
181-
}
182-
*/
166+
// @[inline]
167+
// pub fn (mut dev BitMap) get_color(x int, y int) (int, int, int, int){
168+
// if x < 0 || x >= dev.width || y < 0 || y >= dev.height {
169+
// return 0,0,0,0
170+
// }
171+
// mut i := (x + y * dev.width)*dev.bp
172+
// unsafe{
173+
// return dev.buf[i], dev.buf[i+1], dev.buf[i+2], dev.buf[i+3]
174+
// }
175+
// }
176+
177+
// @[inline]
178+
// pub fn (mut dev BitMap) get_color_u32(x int, y int) u32{
179+
// r, g, b, a := dev.get_color(x, y)
180+
// unsafe{
181+
// return u32(r<<24) | u32(g<<16) | u32(b<<8) | u32(a)
182+
// }
183+
// }
184+
183185
/******************************************************************************
184186
*
185187
* Drawing
186188
*
187189
******************************************************************************/
190+
191+
// color_multiply_alpha multiplies color `c`'s alpha channel with the `level` value.
188192
@[inline]
189193
pub fn color_multiply_alpha(c u32, level f32) u32 {
190194
return u32(f32(c & 0xFF) * level)
191195
}
192196

197+
// color_multiply multiplies R,G,B channels of color `c` with the `level` value.
193198
@[inline]
194199
pub fn color_multiply(c u32, level f32) u32 {
195200
mut r := (f32((c >> 24) & 0xFF) / 255.0) * level

vlib/x/ttf/render_bmp.v

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ module ttf
1616
import encoding.utf8
1717
import math
1818

19+
// BitMap represents a bitmap image of text rendered with the font supplied via
20+
// the `tf` field.
1921
pub struct BitMap {
2022
pub mut:
2123
tf &TTF_File = unsafe { nil }
@@ -50,33 +52,33 @@ pub mut:
5052
* Utility
5153
*
5254
******************************************************************************/
53-
// clear clear the bitmap with 0 bytes
55+
// clear clears the bitmap with 0 bytes.
5456
pub fn (mut bmp BitMap) clear() {
5557
mut sz := bmp.width * bmp.height * bmp.bp
5658
unsafe {
5759
vmemset(bmp.buf, 0x00, sz)
5860
}
5961
}
6062

61-
// transform matrix applied to the text
63+
// trf_txt returns the transform matrix applied to the text.
6264
pub fn (bmp &BitMap) trf_txt(p &Point) (int, int) {
6365
return int(p.x * bmp.tr_matrix[0] + p.y * bmp.tr_matrix[3] + bmp.tr_matrix[6]), int(
6466
p.x * bmp.tr_matrix[1] + p.y * bmp.tr_matrix[4] + bmp.tr_matrix[7])
6567
}
6668

67-
// transform matrix applied to the char
69+
// trf_ch returns the transform matrix applied to the char.
6870
pub fn (bmp &BitMap) trf_ch(p &Point) (int, int) {
6971
return int(p.x * bmp.ch_matrix[0] + p.y * bmp.ch_matrix[3] + bmp.ch_matrix[6]), int(
7072
p.x * bmp.ch_matrix[1] + p.y * bmp.ch_matrix[4] + bmp.ch_matrix[7])
7173
}
7274

73-
// set draw position in the buffer
75+
// set_pos sets the draw position in the buffer
7476
pub fn (mut bmp BitMap) set_pos(x f32, y f32) {
7577
bmp.tr_matrix[6] = x
7678
bmp.tr_matrix[7] = y
7779
}
7880

79-
// set the rotation angle in radiants
81+
// set_rotation sets the rotation angle in radians `a`
8082
pub fn (mut bmp BitMap) set_rotation(a f32) {
8183
bmp.tr_matrix[0] = f32(math.cos(a)) // 1
8284
bmp.tr_matrix[1] = f32(-math.sin(a)) // 0
@@ -89,6 +91,7 @@ pub fn (mut bmp BitMap) set_rotation(a f32) {
8991
* Filler functions
9092
*
9193
******************************************************************************/
94+
// init_filler initializes the internal `filler` buffer.
9295
pub fn (mut bmp BitMap) init_filler() {
9396
h := bmp.height - bmp.filler.len
9497
if h < 1 {
@@ -100,12 +103,14 @@ pub fn (mut bmp BitMap) init_filler() {
100103
// dprintln("Init filler: ${bmp.filler.len} rows")
101104
}
102105

106+
// clear_filler clears the internal `filler` buffer
103107
pub fn (mut bmp BitMap) clear_filler() {
104108
for i in 0 .. bmp.height {
105109
bmp.filler[i].clear()
106110
}
107111
}
108112

113+
// exec_filler plots the pixels of the `BitMap` to the internal buffer.
109114
pub fn (mut bmp BitMap) exec_filler() {
110115
for y in 0 .. bmp.height {
111116
if bmp.filler[y].len > 0 {
@@ -131,6 +136,7 @@ pub fn (mut bmp BitMap) exec_filler() {
131136
}
132137
}
133138

139+
// fline populates the internal `filler` buffer with a line segment from `in_x0`,`in_y0` to `in_x1`,`in_y1`.
134140
pub fn (mut bmp BitMap) fline(in_x0 int, in_y0 int, in_x1 int, in_y1 int, c u32) {
135141
mut x0 := f32(in_x0)
136142
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)
187193
* Draw functions
188194
*
189195
******************************************************************************/
196+
197+
// plot plots a pixel at `x`,`y` in color `c` in the internal bitmap buffer.
190198
@[inline]
191199
pub fn (mut bmp BitMap) plot(x int, y int, c u32) bool {
192200
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 {
212220
* smooth draw functions
213221
*
214222
******************************************************************************/
215-
// aline draw an aliased line on the bitmap
223+
// aline draws an aliased line on the bitmap
216224
pub fn (mut bmp BitMap) aline(in_x0 int, in_y0 int, in_x1 int, in_y1 int, c u32) {
217225
// mut c1 := c
218226
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)
307315
* draw functions
308316
*
309317
******************************************************************************/
318+
// line plots a line segment to the internal buffer from `in_x0`,`in_y0` to `in_x1`,`in_y1` in the color `c`.
310319
pub fn (mut bmp BitMap) line(in_x0 int, in_y0 int, in_x1 int, in_y1 int, c u32) {
311320
// outline with aliased borders
312321
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)
387396
}
388397
}
389398

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`.
390400
pub fn (mut bmp BitMap) box(in_x0 int, in_y0 int, in_x1 int, in_y1 int, c u32) {
391401
bmp.line(in_x0, in_y0, in_x1, in_y0, c)
392402
bmp.line(in_x1, in_y0, in_x1, in_y1, c)
393403
bmp.line(in_x0, in_y1, in_x1, in_y1, c)
394404
bmp.line(in_x0, in_y0, in_x0, in_y1, c)
395405
}
396406

407+
// quadratic plots a quadratic Bezier curve in color `c`.
397408
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) {
398409
/*
399410
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
460471
* TTF Query functions
461472
*
462473
******************************************************************************/
474+
// get_chars_bbox returns all characters found in bounding box of string `in_string`.
463475
pub fn (mut bmp BitMap) get_chars_bbox(in_string string) []int {
464476
mut res := []int{}
465477
mut w := 0
@@ -532,6 +544,7 @@ pub fn (mut bmp BitMap) get_chars_bbox(in_string string) []int {
532544
return res
533545
}
534546

547+
// get_bbox returns the bounding box (width and height) of text `in_string`.
535548
pub fn (mut bmp BitMap) get_bbox(in_string string) (int, int) {
536549
mut w := 0
537550

@@ -627,6 +640,8 @@ fn (mut bmp BitMap) draw_notdef_glyph(in_x int, in_w int) {
627640
bmp.line(int(x - in_w), int(y), int(x), int(y - y_h), bmp.color)
628641
}
629642

643+
// draw_text plots the pixels of the text `in_string` to the internal buffer and
644+
// returns the text bounding box.
630645
pub fn (mut bmp BitMap) draw_text(in_string string) (int, int) {
631646
mut w := 0
632647

@@ -702,6 +717,8 @@ pub fn (mut bmp BitMap) draw_text(in_string string) (int, int) {
702717
return w, int(math.abs(int(bmp.tf.y_max - bmp.tf.y_min)) * bmp.scale)
703718
}
704719

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.
705722
pub fn (mut bmp BitMap) draw_glyph(index u16) (int, int) {
706723
glyph := bmp.tf.read_glyph(index)
707724

vlib/x/ttf/render_sokol_cpu.v

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ import gg
1717
import sokol.sgl
1818
import sokol.gfx
1919

20+
// TTF_render_Sokol is a structure containing data for rendering a TTF font
21+
// as a sokol texture
2022
pub struct TTF_render_Sokol {
2123
pub mut:
2224
bmp &BitMap = unsafe { nil } // Base bitmap render
@@ -32,10 +34,12 @@ pub mut:
3234
* Render functions
3335
*
3436
******************************************************************************/
37+
// format_texture formats the BMP (bitmap).
3538
pub fn (mut tf_skl TTF_render_Sokol) format_texture() {
3639
tf_skl.bmp.format_texture()
3740
}
3841

42+
// create_text prepares the text `in_txt` in size `in_font_size` as a sokol texture.
3943
pub fn (mut tf_skl TTF_render_Sokol) create_text(in_txt string, in_font_size f32) {
4044
scale_reduct := tf_skl.scale_reduct
4145
device_dpi := tf_skl.device_dpi
@@ -72,6 +76,7 @@ pub fn (mut tf_skl TTF_render_Sokol) create_text(in_txt string, in_font_size f32
7276
tf_skl.format_texture()
7377
}
7478

79+
// create_text_block prepares a block of text as a sokol texture.
7580
pub fn (mut tf_skl TTF_render_Sokol) create_text_block(in_txt string, in_w int, in_h int, in_font_size f32) {
7681
scale_reduct := tf_skl.scale_reduct
7782
device_dpi := tf_skl.device_dpi
@@ -116,6 +121,7 @@ pub fn (mut tf_skl TTF_render_Sokol) create_text_block(in_txt string, in_w int,
116121
* Sokol Render functions
117122
*
118123
******************************************************************************/
124+
// create_texture creates the sokol texture from the internal buffer state.
119125
pub fn (mut tf_skl TTF_render_Sokol) create_texture() {
120126
w := tf_skl.bmp.width
121127
h := tf_skl.bmp.height
@@ -150,12 +156,14 @@ pub fn (mut tf_skl TTF_render_Sokol) create_texture() {
150156
tf_skl.sg_smp = ssmp
151157
}
152158

159+
// destroy_texture detroys the internal sokol texture.
153160
pub fn (tf_skl TTF_render_Sokol) destroy_texture() {
154161
gfx.destroy_image(tf_skl.sg_img)
155162
gfx.destroy_sampler(tf_skl.sg_smp)
156163
}
157164

158-
// Use only if usage: .dynamic
165+
// update_text_texture updates the sokol texture with current internal state.
166+
// NOTE: Only use if `.dynamic` is set.
159167
pub fn (mut tf_skl TTF_render_Sokol) update_text_texture() {
160168
sz := tf_skl.bmp.width * tf_skl.bmp.height * tf_skl.bmp.bp
161169
mut tmp_sbc := gfx.ImageData{}
@@ -166,6 +174,7 @@ pub fn (mut tf_skl TTF_render_Sokol) update_text_texture() {
166174
gfx.update_image(tf_skl.sg_img, &tmp_sbc)
167175
}
168176

177+
// draw_text_bmp renders the internal state to the current sokol pipeline.
169178
pub fn (tf_skl TTF_render_Sokol) draw_text_bmp(ctx &gg.Context, x f32, y f32) {
170179
// width := tf_skl.bmp.width >> 1
171180
// height := tf_skl.bmp.height >> 1

vlib/x/ttf/text_block.v

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ module ttf
1212
*
1313
* TODO:
1414
**********************************************************************/
15+
// Text_block represents a visual block of TTF text.
1516
pub struct Text_block {
1617
x int // x position of the left high corner
1718
y int // y position of the left high corner
@@ -20,6 +21,7 @@ pub struct Text_block {
2021
cut_lines bool = true // force to cut the line if the length is over the text block width
2122
}
2223

24+
// get_justify_space_cw returns the space needed to justify `txt`.
2325
pub fn (mut dev BitMap) get_justify_space_cw(txt string, w int, block_w int, space_cw int) f32 {
2426
num_spaces := txt.count(' ')
2527
if num_spaces < 1 {
@@ -33,7 +35,7 @@ pub fn (mut dev BitMap) get_justify_space_cw(txt string, w int, block_w int, spa
3335
return res
3436
}
3537

36-
// write out a text
38+
// draw_text_block renders out `text` in the `Text_block` `block`.
3739
pub fn (mut bmp BitMap) draw_text_block(text string, block Text_block) {
3840
mut x := block.x
3941
mut y := block.y

0 commit comments

Comments
 (0)