1 /* output text direction */
7 int dir_do
; /* enable text direction processing */
9 static char *dbuf
; /* text in n_td direction */
10 static int dbuf_sz
, dbuf_n
; /* dbuf[] size and length */
11 static char *rbuf
; /* text in (1 - n_td) direction */
12 static int rbuf_sz
, rbuf_n
; /* rbuf[] size and length */
13 static int dir_cd
; /* current direction */
15 /* append s to the start (dir == 0) or end (dir == 1) of d */
16 static void dir_copy(char **d
, int *d_n
, int *d_sz
, char *s
, int s_n
, int dir
)
18 while (*d_n
+ s_n
+ 1 > *d_sz
) {
19 int sz
= *d_sz
? *d_sz
* 2 : 512;
20 char *n
= malloc(sz
+ 1);
22 memcpy(dir
? n
+ *d_sz
: n
, *d
, *d_sz
);
28 memcpy(*d
+ *d_sz
- *d_n
- s_n
, s
, s_n
);
30 memcpy(*d
+ *d_n
, s
, s_n
);
34 /* copy rbuf (the text in reverse direction) to dbuf */
35 static void dir_flush(void)
37 char *s
= rbuf
+ (n_td
> 0 ? 0 : rbuf_sz
- rbuf_n
);
38 dir_copy(&dbuf
, &dbuf_n
, &dbuf_sz
, s
, rbuf_n
, n_td
);
42 /* append s to dbuf or rbuf based on the current text direction */
43 static void dir_append(char *s
)
46 if (dir
== n_td
&& rbuf_n
)
49 dir_copy(&dbuf
, &dbuf_n
, &dbuf_sz
, s
, strlen(s
), dir
);
51 dir_copy(&rbuf
, &rbuf_n
, &rbuf_sz
, s
, strlen(s
), dir
);
54 static void setfont(int f
)
57 sprintf(cmd
, "%cf(%02d", c_ec
, f
);
62 static void setsize(int s
)
65 sprintf(cmd
, s
<= 99 ? "%cs(%02d" : "%cs[%d]", c_ec
, s
);
70 static void setcolor(int m
)
73 sprintf(cmd
, "%cm[%s]", c_ec
, clr_str(m
));
78 void dir_fix(struct sbuf
*sbuf
, char *src
)
83 int f
= -1, s
= -1, m
= -1;
86 while ((t
= escread(&src
, &c
)) >= 0) {
94 memcpy(cmd
, prev_s
, src
- prev_s
);
95 cmd
[src
- prev_s
] = '\0';
123 sprintf(cmd
, "%c%c\x03%s\x03", c_ec
, t
, c
);
151 r
= n_td
> 0 ? dbuf
+ dbuf_sz
- dbuf_n
: dbuf
;
154 sbuf_append(sbuf
, r
);