Skip to content

Commit a1ba2eb

Browse files
authored
font-edit: Improve the type descriptions (#29)
Currently, there is a lack of description for geometric types and functions. pt_t is used to describe a geometric object—a point, while pts_t is used to describe a dynamically configurable array. n is used to describe the current number of configured points, and s is used to describe the maximum configurable memory limit, measured in bytes. spline_t is used to describe cubic spline interpolation based on four points. new_pts, dispose_pts, and add_pt are methods for the pt_t and pts_t types. distance_to_point, distance_to_line, and distance_to_segment are used to describe the distance relationships between geometric objects. lerp is a function for computing linear interpolation points. fit is used to compute a spline with a specific error tolerance.
1 parent 1504dfd commit a1ba2eb

File tree

2 files changed

+114
-38
lines changed

2 files changed

+114
-38
lines changed

tools/font-edit/twin-fedit.c

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ static cmd_t *insert_cmd(cmd_t **prev)
105105
{
106106
cmd_t *n = malloc(sizeof(cmd_t));
107107

108-
n->op = OpNoop;
108+
n->op = op_noop;
109109
n->next = *prev;
110110
*prev = n;
111111
return n;
@@ -187,17 +187,17 @@ static char_t *read_char(void)
187187
switch (line[5]) {
188188
case 'm':
189189
cmd = append_cmd(c);
190-
cmd->op = OpMove;
190+
cmd->op = op_move;
191191
sscanf(line + 8, "%lf, %lf", &cmd->pt[0].x, &cmd->pt[0].y);
192192
break;
193193
case 'l':
194194
cmd = append_cmd(c);
195-
cmd->op = OpLine;
195+
cmd->op = op_line;
196196
sscanf(line + 8, "%lf, %lf", &cmd->pt[0].x, &cmd->pt[0].y);
197197
break;
198198
case 'c':
199199
cmd = append_cmd(c);
200-
cmd->op = OpCurve;
200+
cmd->op = op_curve;
201201
sscanf(line + 8, "%lf, %lf, %lf, %lf, %lf, %lf", &cmd->pt[0].x,
202202
&cmd->pt[0].y, &cmd->pt[1].x, &cmd->pt[1].y, &cmd->pt[2].x,
203203
&cmd->pt[2].y);
@@ -257,13 +257,13 @@ static void draw_char(char_t *c)
257257
alpha = 0.5;
258258

259259
switch (cmd->op) {
260-
case OpMove:
260+
case op_move:
261261
dot(cr, cmd->pt[0].x, cmd->pt[0].y, 1, 1, 0, alpha);
262262
break;
263-
case OpLine:
263+
case op_line:
264264
dot(cr, cmd->pt[0].x, cmd->pt[0].y, 1, 0, 0, alpha);
265265
break;
266-
case OpCurve:
266+
case op_curve:
267267
dot(cr, cmd->pt[0].x, cmd->pt[0].y, 0, 0, 1, alpha);
268268
dot(cr, cmd->pt[1].x, cmd->pt[1].y, 0, 0, 1, alpha);
269269
dot(cr, cmd->pt[2].x, cmd->pt[2].y, 0, 1, 0, alpha);
@@ -280,13 +280,13 @@ static void draw_char(char_t *c)
280280
double alpha = 1;
281281

282282
switch (cmd->op) {
283-
case OpMove:
283+
case op_move:
284284
spot(cr, cmd->pt[0].x, cmd->pt[0].y, 1, 1, 0, alpha);
285285
break;
286-
case OpLine:
286+
case op_line:
287287
spot(cr, cmd->pt[0].x, cmd->pt[0].y, 1, 0, 0, alpha);
288288
break;
289-
case OpCurve:
289+
case op_curve:
290290
spot(cr, cmd->pt[0].x, cmd->pt[0].y, 0, 0, 1, alpha);
291291
spot(cr, cmd->pt[1].x, cmd->pt[1].y, 0, 0, 1, alpha);
292292
spot(cr, cmd->pt[2].x, cmd->pt[2].y, 0, 1, 0, alpha);
@@ -301,13 +301,13 @@ static void draw_char(char_t *c)
301301

302302
for (cmd = c->cmd; cmd; cmd = cmd->next) {
303303
switch (cmd->op) {
304-
case OpMove:
304+
case op_move:
305305
cairo_move_to(cr, cmd->pt[0].x, cmd->pt[0].y);
306306
break;
307-
case OpLine:
307+
case op_line:
308308
cairo_line_to(cr, cmd->pt[0].x, cmd->pt[0].y);
309309
break;
310-
case OpCurve:
310+
case op_curve:
311311
cairo_curve_to(cr, cmd->pt[0].x, cmd->pt[0].y, cmd->pt[1].x,
312312
cmd->pt[1].y, cmd->pt[2].x, cmd->pt[2].y);
313313
break;
@@ -321,7 +321,7 @@ static void draw_char(char_t *c)
321321
double tx, ty;
322322
char buf[10];
323323

324-
if (cmd->op == OpCurve) {
324+
if (cmd->op == op_curve) {
325325
tx = cmd->pt[2].x;
326326
ty = cmd->pt[2].y;
327327
} else {
@@ -359,7 +359,7 @@ static cmd_t *pos_to_cmd(char_t *c, cmd_t *start, int ix, int iy)
359359

360360
cmd = start;
361361
while (cmd) {
362-
int i = cmd->op == OpCurve ? 2 : 0;
362+
int i = cmd->op == op_curve ? 2 : 0;
363363
double dx = cmd->pt[i].x - x;
364364
double dy = cmd->pt[i].y - y;
365365
double err = sqrt(dx * dx + dy * dy);
@@ -404,7 +404,7 @@ static void replace_with_spline(char_t *c, cmd_t *first, cmd_t *last)
404404

405405
order(&first, &last);
406406
for (cmd = first; cmd != last->next; cmd = cmd->next) {
407-
int i = cmd->op == OpCurve ? 2 : 0;
407+
int i = cmd->op == op_curve ? 2 : 0;
408408
add_pt(pts, &cmd->pt[i]);
409409
}
410410

@@ -421,7 +421,7 @@ static void replace_with_spline(char_t *c, cmd_t *first, cmd_t *last)
421421

422422
cmd = insert_cmd(&first->next);
423423

424-
cmd->op = OpCurve;
424+
cmd->op = op_curve;
425425
cmd->pt[0] = s.b;
426426
cmd->pt[1] = s.c;
427427
cmd->pt[2] = s.d;
@@ -436,12 +436,12 @@ static void split(char_t *c, cmd_t *first, cmd_t *last)
436436

437437
push(c);
438438
cmd = insert_cmd(&first->next);
439-
cmd->op = OpLine;
439+
cmd->op = op_line;
440440
cmd->pt[0] = lerp(&first->pt[0], &last->pt[0]);
441-
if (last->op == OpMove) {
441+
if (last->op == op_move) {
442442
cmd_t *extra = insert_cmd(&last->next);
443443

444-
extra->op = OpLine;
444+
extra->op = op_line;
445445
extra->pt[0] = last->pt[0];
446446
last->pt[0] = cmd->pt[0];
447447
}
@@ -533,9 +533,9 @@ static void play(char_t *c)
533533
}
534534
} else {
535535
cmd_t *spline;
536-
if (c->first && c->first->op == OpCurve)
536+
if (c->first && c->first->op == op_curve)
537537
spline = c->first;
538-
else if (c->last && c->last->op == OpCurve)
538+
else if (c->last && c->last->op == op_curve)
539539
spline = c->last;
540540
else
541541
spline = 0;
@@ -586,15 +586,15 @@ static void write_char(char_t *c)
586586

587587
for (cmd = c->cmd; cmd; cmd = cmd->next) {
588588
switch (cmd->op) {
589-
case OpMove:
589+
case op_move:
590590
printf(" 'm', %g, %g,\n", cmd->pt[0].x, cmd->pt[0].y);
591591
offset += 3;
592592
break;
593-
case OpLine:
593+
case op_line:
594594
printf(" 'l', %g, %g,\n", cmd->pt[0].x, cmd->pt[0].y);
595595
offset += 3;
596596
break;
597-
case OpCurve:
597+
case op_curve:
598598
printf(" 'c', %g, %g, %g, %g, %g, %g,\n", cmd->pt[0].x,
599599
cmd->pt[0].y, cmd->pt[1].x, cmd->pt[1].y, cmd->pt[2].x,
600600
cmd->pt[2].y);

tools/font-edit/twin-fedit.h

Lines changed: 89 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,44 @@
3535
#include <string.h>
3636
#include <unistd.h>
3737

38-
typedef enum _op { OpMove, OpLine, OpCurve, OpNoop } op_t;
38+
/* Geometric types */
3939

40-
typedef struct {
40+
/*
41+
* pt_t - Point in a 2-D coordinate system
42+
* @x: x-component
43+
* @y: y-component
44+
*/
45+
typedef struct _pt {
4146
double x, y;
4247
} pt_t;
4348

49+
/*
50+
* pts_t - Points in a 2-D coordinate system
51+
* @n: numeber of the stored points
52+
* @s: size of the storage
53+
* @pt: pointer of pt_ts
54+
*/
55+
typedef struct _pts {
56+
int n;
57+
int s;
58+
pt_t *pt;
59+
} pts_t;
60+
61+
/*
62+
* spline_t - Cubic spline type
63+
* @a: starting point.
64+
* @b: first control point.
65+
* @c: last control point.
66+
* @d: ending point.
67+
*/
68+
typedef struct _spline {
69+
pt_t a, b, c, d;
70+
} spline_t;
71+
72+
/* Command line tpyes */
73+
74+
typedef enum { op_move, op_line, op_curve, op_noop } op_t;
75+
4476
typedef struct _cmd {
4577
struct _cmd *next;
4678
op_t op;
@@ -59,30 +91,74 @@ typedef struct _char {
5991
cmd_t *last;
6092
} char_t;
6193

62-
typedef struct _pts_t {
63-
int n;
64-
int s;
65-
pt_t *pt;
66-
} pts_t;
67-
68-
typedef struct _spline {
69-
pt_t a, b, c, d;
70-
} spline_t;
71-
72-
spline_t fit(pt_t *p, int n);
94+
/* Geometric functions */
7395

96+
/*
97+
* new_pts() - Allocate and initialize a new point
98+
*
99+
* Return: pts_t type, allocated point.
100+
*/
74101
pts_t *new_pts(void);
75102

103+
/*
104+
* dispose_pts() - Free all storage used by pts_t
105+
* @pts: the points to be free
106+
*/
76107
void dispose_pts(pts_t *pts);
77108

109+
/*
110+
* add_pt() - Add a point to pts_t
111+
* @pts: the object that receives the added points
112+
* @pt: the point to be added
113+
*/
78114
void add_pt(pts_t *pts, pt_t *pt);
79115

116+
/*
117+
* distance_to_point() - Calculate distance between two points
118+
* @a: point in 2-D coordinate
119+
* @b: point in 2-D coordinate
120+
*
121+
* Return: double type, the distance between point a and point b.
122+
*/
80123
double distance_to_point(pt_t *a, pt_t *b);
81124

125+
/*
126+
* distance_to_line() - Calculate distance from a point to a line
127+
* @p: point outside the line
128+
* @p1: one of the points used to calculate the line
129+
* @p2: one of the points used to calculate the line
130+
*
131+
* Return: double type, the distance from point p to the line.
132+
*/
82133
double distance_to_line(pt_t *p, pt_t *p1, pt_t *p2);
83134

135+
/*
136+
* distance_to_segment() - Calculate shortest distance from a point
137+
* to a line segment
138+
* @p: point outside the line segment
139+
* @p1: one of the points used to calculate the line segment
140+
* @p2: one of the points used to calculate the line segment
141+
*
142+
* Return: double type, the distance from point p to the line segment.
143+
*/
84144
double distance_to_segment(pt_t *p, pt_t *p1, pt_t *p2);
85145

146+
/*
147+
* lerp() - Interpolate linearly a point between two points
148+
* @a: one of the point to be interpolated
149+
* @b: one of the point to be interpolated
150+
*
151+
* Return: pt_t type, a interpolation point by two points.
152+
*/
86153
pt_t lerp(pt_t *a, pt_t *b);
87154

155+
/*
156+
* fit() - Fit a spline within a specified tolerance
157+
* @a: points
158+
* @n: number of points
159+
*
160+
* Return: spline_t type, a cubic spline of points.
161+
*/
162+
spline_t fit(pt_t *p, int n);
163+
88164
#endif /* _TWIN_FEDIT_H_ */

0 commit comments

Comments
 (0)