Better positioning of characters with smaller bounding box than font's bounding box, and using yoffset from both font and character

This commit is contained in:
Louis Beaudoin 2014-09-10 20:53:47 -04:00
parent 7f6c27e802
commit f68c2384bd

30
bdf2c.c
View File

@ -206,21 +206,25 @@ void Footer(FILE * out, const char *name, int width, int height, int chars)
/// ///
/// Dump character. /// Dump character.
/// ///
/// @param out file stream for output
/// @param bitmap input bitmap
/// @param width character width
/// @param height character height
/// ///
void DumpCharacter(FILE * out, unsigned char *bitmap, int width, int height) void DumpCharacter(FILE * out, unsigned char *bitmap, int fontwidth, int fontheight, int fontyoffset, int charheight, int charyoffset)
{ {
int x; int x;
int y; int y;
int c; int c;
for (y = 0; y < height; ++y) { // how many rows from the top of the font bounding box is the top of this character?
int yoffset = fontheight - charheight + (fontyoffset - charyoffset);
for (y = 0; y < fontheight; ++y) {
fputc('\t', out); fputc('\t', out);
for (x = 0; x < width; x += 8) { for (x = 0; x < fontwidth; x += 8) {
c = bitmap[y * ((width + 7) / 8) + x / 8]; // if current row is above or below the bitmap, output a blank row
if(y < yoffset || y > yoffset + charheight)
c = 0;
else
c = bitmap[(y - yoffset) * ((fontwidth + 7) / 8) + x / 8];
//printf("%d = %d\n", y * ((width+7)/8) + x/8, c); //printf("%d = %d\n", y * ((width+7)/8) + x/8, c);
if (c & 0x80) { if (c & 0x80) {
fputc('X', out); fputc('X', out);
@ -378,6 +382,8 @@ void ReadBdf(FILE * bdf, FILE * out, const char *name)
char *p; char *p;
int fontboundingbox_width; int fontboundingbox_width;
int fontboundingbox_height; int fontboundingbox_height;
int fontboundingbox_xoff;
int fontboundingbox_yoff;
int chars; int chars;
int i; int i;
int j; int j;
@ -396,6 +402,8 @@ void ReadBdf(FILE * bdf, FILE * out, const char *name)
fontboundingbox_width = 0; fontboundingbox_width = 0;
fontboundingbox_height = 0; fontboundingbox_height = 0;
fontboundingbox_xoff = 0;
fontboundingbox_yoff = 0;
chars = 0; chars = 0;
for (;;) { for (;;) {
if (!fgets(linebuf, sizeof(linebuf), bdf)) { // EOF if (!fgets(linebuf, sizeof(linebuf), bdf)) { // EOF
@ -410,6 +418,10 @@ void ReadBdf(FILE * bdf, FILE * out, const char *name)
fontboundingbox_width = atoi(p); fontboundingbox_width = atoi(p);
p = strtok(NULL, " \t\n\r"); p = strtok(NULL, " \t\n\r");
fontboundingbox_height = atoi(p); fontboundingbox_height = atoi(p);
p = strtok(NULL, " \t\n\r");
fontboundingbox_xoff = atoi(p);
p = strtok(NULL, " \t\n\r");
fontboundingbox_yoff = atoi(p);
} else if (!strcasecmp(s, "CHARS")) { } else if (!strcasecmp(s, "CHARS")) {
p = strtok(NULL, " \t\n\r"); p = strtok(NULL, " \t\n\r");
chars = atoi(p); chars = atoi(p);
@ -547,7 +559,7 @@ void ReadBdf(FILE * bdf, FILE * out, const char *name)
fontboundingbox_height); fontboundingbox_height);
} }
DumpCharacter(out, bitmap, fontboundingbox_width, DumpCharacter(out, bitmap, fontboundingbox_width,
fontboundingbox_height); fontboundingbox_height, fontboundingbox_yoff, bbh, bby);
scanline = -1; scanline = -1;
width = INT_MIN; width = INT_MIN;
} else { } else {