9. Macfont Textbox
Based on btprnt_draw_textbox
, only with macfont.
<<funcdefs>>=
void macfont_textbox(btprnt_region *reg,
macfont_info *fnt,
int offx, int offy,
const char *str,
int color,
int *nx, int *ny);
<<funcs>>=
<<textbox_helpers>>
void macfont_textbox(btprnt_region *reg,
macfont_info *fnt,
int offx, int offy,
const char *str,
int color,
int *nx, int *ny)
{
int len;
int n;
int start;
int nchars;
int xpos;
int ypos;
len = strlen(str);
start = 0;
nchars = 0;
xpos = offx;
ypos = offy;
for (n = 0; n < len; n++) {
nchars++;
if (str[n] == ' ' || str[n] == '\n') {
write_word(reg, fnt,
&str[start], nchars,
&xpos, &ypos,
color);
start = n + 1;
nchars = 0;
}
}
if (nchars >= 0) {
write_word(reg, fnt,
&str[start], nchars,
&xpos, &ypos,
color);
}
if (nx != NULL) *nx = xpos;
if (ny != NULL) *ny = ypos;
}
<<textbox_helpers>>=
static int get_charwidth(macfont_info *fnt, char c)
{
uint16_t pos;
uint16_t width;
uint16_t ow;
pos = c - fnt->firstchar;
ow = fnt->owtable[pos];
ow = ((ow & 0xFF) << 8) | ow >> 8;
width = ow & 0x00FF;
return (int)width;
}
static int get_wordlen(macfont_info *fnt, const char *str, int sz)
{
int wordlen;
int n;
wordlen = 0;
for (n = 0; n < sz; n++) {
wordlen += get_charwidth(fnt, str[n]);
}
return wordlen;
}
static void write_word(btprnt_region *reg,
macfont_info *fnt,
const char *str,
int nchars,
int *xp,
int *yp,
int color)
{
int wordlen;
int i;
int xpos, ypos;
xpos = *xp;
ypos = *yp;
wordlen = get_wordlen(fnt, str, nchars);
if ((xpos + wordlen) > reg->w) {
if (wordlen < reg->w) {
xpos = 0;
ypos += fnt->fontheight * 1.5;
}
}
for (i = 0; i < nchars; i++) {
char c;
int cw;
c = str[i];
cw = get_charwidth(fnt, c);
if ((xpos + cw) > reg->w || c == '\n') {
xpos = 0;
ypos += fnt->fontheight * 1.5;
}
if (c != '\n') {
xpos +=
macfont_glyph(reg, fnt,
xpos,
ypos,
c,
color);
}
}
*xp = xpos;
*yp = ypos;
}
The janet function for this is called macfont-textbox
.
prev | home | next