/* BSD License Copyright (c) 2014 Andrey Chilikin https://github.com/achilikin Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ /** Basic BDF Exporter - converts BDF files to C structures. Only 8 bits wide fonts are supported. */ #include #include #include #include #include #include "bdf.h" // check if 'buf' starts with 'key' and store pointer to the argument static char *key_arg(char *buf, const char *key, char **arg) { char *end; size_t len = strlen(key); if (strncmp(buf, key, len) == 0) { end = buf + len; if (*end > ' ') return NULL; if (arg == NULL) return buf; for(; *end <= ' ' && *end != '\0'; end++); *arg = end; return buf; } return NULL; } // rotate glyph bitmap CCW static uint32_t rotate_glyph(const uint8_t *glyph, unsigned gw, int gh, uint8_t *grot) { uint32_t dy = 0; do { for(uint8_t n = 0; n < gw; n++) { uint8_t out = 0; uint8_t mask = 0x80 >> n; for(uint8_t b = 0; b < 8; b++) { if (glyph[b] & mask) out |= 1 << b; } grot[dy++] = out; } glyph += 8; } while((gh -= 8) > 0); return dy; } bdfe_t *bdf_convert(const char *name, unsigned gmin, unsigned gmax, unsigned ascender, int flags) { FILE *fp; char *arg; bdfe_t *font = NULL; char buf[BUFSIZ]; char startchar[BUFSIZ]; if (name == NULL || (fp = fopen(name, "r")) == NULL) return NULL; memset(buf, 0, sizeof(buf)); // parse file header up to 'CHARS' keyword unsigned nchars = 0, dx = 0, dy = 0, descent = 0; while(fgets(buf, sizeof(buf) - 2, fp) != NULL) { arg = strchr(buf, '\0'); while(*arg < ' ' && arg != buf) *arg-- = '\0'; if (key_arg(buf, "FONTBOUNDINGBOX", &arg)) { dx = strtoul(arg, &arg, 10); dy = strtoul(arg, &arg, 10); strtoul(arg, &arg, 10); descent = -strtoul(arg, &arg, 10); } if (key_arg(buf, "FONT_DESCENT", &arg)) { descent = strtoul(arg, &arg, 10); } if (key_arg(buf, "CHARS", &arg)) { nchars = atoi(arg); break; } } if (dy == 0) { fclose(fp); return NULL; } // recalculate glyphs x size as we cannot use FONTBOUNDINGBOX // as it is for our gmin/gmax range dx = 0; while(fgets(buf, sizeof(buf) - 2, fp) != NULL) { if (key_arg(buf, "STARTCHAR", &arg)) { unsigned idx = 0, dw; while(fgets(buf, sizeof(buf) - 2, fp) != NULL) { if (key_arg(buf, "ENCODING", &arg)) { idx = atoi(arg); if (idx < gmin || idx > gmax) break; } if (key_arg(buf, "DWIDTH", &arg)) { dw = atoi(arg); if (dw > dx) dx = dw; break; } } } } // limit vertical size to 16 pixels or 2 lines on SSD1306 if (dy > 16) dy = 16; unsigned gh = dy, gw = dx; // round y size to 8 pixels for SSD1306 text mode if ((flags & BDF_ROTATE) || !(flags & BDF_NATIVE)) { dy = (dy+7) & ~0x07; descent += dy - gh; gh = dy; } if (ascender > dy/2) ascender = dy/2; // rewind the file pointer ans start parsing glyphs fseek(fp, 0, SEEK_SET); fwrite(&dx, 1, 4, stdout); fwrite(&dy, 1, 4, stdout); unsigned gsize = dy * ((dx + 7) / 8); font = (bdfe_t *)malloc(sizeof(bdfe_t) + gsize*nchars); font->chars = 0; // glyphs output buffer uint8_t *gout = font->font; // glyph storage buffer, big enough to allow displacement manipulations uint8_t *gbuf = (uint8_t *)malloc(gsize*2); memset(gbuf, 0, gsize*2); while(fgets(buf, sizeof(buf) - 2, fp) != NULL) { if (key_arg(buf, "STARTCHAR", &arg)) { unsigned displacement = 0; unsigned bitmap = 0, i = 0, idx = 0; unsigned bbw = 0; int bbox = 0, bboy = 0; uint8_t *gin = gbuf + gsize; memset(gin, 0, gsize); // store a copy in case if verbose output is on arg = strchr(buf, '\0'); while(*arg < ' ' && arg != buf) *arg-- = '\0'; strcpy(startchar, buf); while(fgets(buf, sizeof(buf) - 2, fp) != NULL) { arg = strchr(buf, '\0'); while(*arg < ' ' && arg != buf) *arg-- = '\0'; if (key_arg(buf, "ENCODING", &arg)) { idx = atoi(arg); if (idx < gmin || idx > gmax) break; } if (key_arg(buf, "DWIDTH", &arg)) { gw = atoi(arg); if (gw > 8) break; } if (key_arg(buf, "BBX", &arg)) { bbw = strtol(arg, &arg, 10); strtol(arg, &arg, 10); // skip bbh, we'll calculate it (i) bbox = strtol(arg, &arg, 10); bboy = strtol(arg, &arg, 10); } if (key_arg(buf, "ENDCHAR", &arg)) { font->chars++; // check if baseline alignment is needed if (bboy > 0) i += bboy; if ((bboy < 0) && (i < dy)) { displacement = dy - descent - i; // move BBX to origin displacement -= bboy; i += displacement; } if (i < (dy - descent)) displacement += dy - descent - i; gin -= displacement + ascender; if (flags & BDF_ROTATE) { gh = rotate_glyph(gin, dx, dy, gout); gw = 8; } else memcpy(gout, gin, dy); for(i = 0; i < gh; i++) { fwrite(&gout[i], 1, 1, stdout); } gout += gh; break; } if (key_arg(buf, "BITMAP", &arg)) { bitmap = 1; continue; } if (bitmap && (i < dy)) { gin[i] = (uint8_t)strtoul(buf, NULL, 16); if ((bbw < 8) && (bbox > 0) && (bbox < 8)) gin[i] = gin[i] >> bbox; i++; } } } } font->gw = dx; font->bpg = gh; free(gbuf); fclose(fp); return font; }