diff options
author | patacongo <patacongo@42af7a65-404d-4744-a932-0658087f49c3> | 2011-07-23 13:53:59 +0000 |
---|---|---|
committer | patacongo <patacongo@42af7a65-404d-4744-a932-0658087f49c3> | 2011-07-23 13:53:59 +0000 |
commit | baf4af60ec1100c43dec884c046d55e9a57c6b20 (patch) | |
tree | 5533e72fbb341f9e278d73e037dad5edf4b8a523 /nuttx/tools | |
parent | e3ba485a852fc10a85533d0c5a28c6e01d66a0d4 (diff) | |
download | px4-nuttx-baf4af60ec1100c43dec884c046d55e9a57c6b20.tar.gz px4-nuttx-baf4af60ec1100c43dec884c046d55e9a57c6b20.tar.bz2 px4-nuttx-baf4af60ec1100c43dec884c046d55e9a57c6b20.zip |
Add JP's BDF font conversion program
git-svn-id: svn://svn.code.sf.net/p/nuttx/code/trunk@3813 42af7a65-404d-4744-a932-0658087f49c3
Diffstat (limited to 'nuttx/tools')
-rw-r--r-- | nuttx/tools/Makefile.host | 10 | ||||
-rwxr-xr-x | nuttx/tools/README.txt | 137 | ||||
-rw-r--r-- | nuttx/tools/bdf-converter.c | 407 |
3 files changed, 551 insertions, 3 deletions
diff --git a/nuttx/tools/Makefile.host b/nuttx/tools/Makefile.host index 163472462..fc8762480 100644 --- a/nuttx/tools/Makefile.host +++ b/nuttx/tools/Makefile.host @@ -33,7 +33,7 @@ # ############################################################################ -all: mkconfig mkversion mksyscall +all: mkconfig mkversion mksyscall bdf-converter default: mkconfig mksyscall .PHONY: clean @@ -56,6 +56,12 @@ mkversion: mkconfig.c cfgparser.c mksyscall: mksyscall.c @gcc $(CFLAGS) -o mksyscall mksyscall.c +# mksyscall - Convert a CSV file into syscall stubs and proxies + +bdf-converter: bdf-converter.c + @gcc $(CFLAGS) -o bdf-converter bdf-converter.c + clean: @rm -f *.o *.a *~ .*.swp - @rm -f mkconfig mksyscall mkversion mkconfig.exe mksyscall.exe mkversion.exe
\ No newline at end of file + @rm -f mkconfig mksyscall mkversion bdf-converter + @rm -f mkconfig.exe mksyscall.exe mkversion.exe bdf-converter.exe diff --git a/nuttx/tools/README.txt b/nuttx/tools/README.txt index 259822378..549b6e31a 100755 --- a/nuttx/tools/README.txt +++ b/nuttx/tools/README.txt @@ -54,7 +54,7 @@ mkversion.c, cfgparser.c, and cfgparser.h mksyscall.c - This is C file that is used to build mksyscall program. The mksyscall + This is a C file that is used to build mksyscall program. The mksyscall program is used during the initial NuttX build by the logic in the top- level syscall/ directory. @@ -74,6 +74,141 @@ mksyscall.c accept this CVS file as input and generate all of the required proxy or stub files as output. See syscall/README.txt for additonal information. +bdf-convert.c + + This C file is used to build the bdf-converter program. The bdf-converter + program be used to convert fonts in Bitmap Distribution Format (BDF) + into fonts that can be used in the NX graphics system. + + Below are general instructions for creating and installing a new font + in the NX graphic system: + + 1. Locate a font in BDF format, + 2. Use the bdf-converter program to convert the BDF font to the NuttX + font format. This will result in a C header file containing + defintions. That header file should be installed at, for example, + graphics/nxfonts/nxfonts_myfont.h. + + Create a new NuttX configuration variable. For example, suppose + you define the following variable: CONFIG_NXFONT_MYFONT. Then + you would need to: + + 3. Define CONFIG_NXFONT_MYFONT=y in your NuttX configuration file. + + A font ID number has to be assigned for each new font. The font ID + is defined in the file include/nuttx/nxfonts.h. Those definitions + have to be extended to support your new font. Look at how the font ID + enabled by CONFIG_NXFONT_SANS23X27 is defined and add an ID for your + new font in a similar fashion: + + 4. include/nuttx/nxfonts.h. Add you new font as a possible system + default font: + + #if defined(CONFIG_NXFONT_SANS23X27) + # define NXFONT_DEFAULT FONTID_SANS23X27 + #elif defined(CONFIG_NXFONT_MYFONT) + # define NXFONT_DEFAULT FONTID_MYFONT + #endif + + Then define the actual font ID. Make sure that the font ID value + is unique: + + enum nx_fontid_e + { + FONTID_DEFAULT = 0 /* The default font */ + #ifdef CONFIG_NXFONT_SANS23X27 + , FONTID_SANS23X27 = 1 /* The 23x27 sans serif font */ + #endif + #ifdef CONFIG_NXFONT_MYFONT + , FONTID_MYFONT = 2 /* My shiny, new font */ + #endif + ... + + New Add the font to the NX build system. There are several files that + you have to modify to to this. Look how the build system uses the + font CONFIG_NXFONT_SANS23X27 for examaples: + + 5. nuttx/graphics/Makefile. This file needs logic to auto-generate + a C source file from the header file that you generated with the + the bdf-converter program. Notice NXFONTS_FONTID=2; this must be + set to the same font ID value that you defined in the + include/nuttx/nxfonts.h file. + + genfontsources: + ifeq ($(CONFIG_NXFONT_SANS23X27),y) + @$(MAKE) -C nxfonts -f Makefile.sources TOPDIR=$(TOPDIR) NXFONTS_FONTID=1 EXTRADEFINES=$(EXTRADEFINES) + endif + ifeq ($(CONFIG_NXFONT_MYFONT),y) + @$(MAKE) -C nxfonts -f Makefile.sources TOPDIR=$(TOPDIR) NXFONTS_FONTID=2 EXTRADEFINES=$(EXTRADEFINES) + endif + + 6. nuttx/graphics/nxfonts/Make.defs. Set the make variable NXFSET_CSRCS. + NXFSET_CSRCS determines the name of the font C file to build when + NXFONTS_FONTID=2: + + ifeq ($(CONFIG_NXFONT_SANS23X27),y) + NXFSET_CSRCS += nxfonts_bitmaps_sans23x27.c + endif + ifeq ($(CONFIG_NXFONT_MYFONT),y) + NXFSET_CSRCS += nxfonts_bitmaps_myfont.c + endif + + 7. nuttx/graphics/nxfonts/Makefile.sources. This is the Makefile used + in step 5 that will actually generate the font C file. So, given + your NXFONTS_FONTID=2, it needs to determine a prefix to use for + auto-generated variable and function names and (again) the name of + the autogenerated file to create (this must be the same name that + was used in nuttx/graphics/nxfonts/Make.defs): + + ifeq ($(NXFONTS_FONTID),1) + NXFONTS_PREFIX := g_sans23x27_ + GEN_CSRC = nxfonts_bitmaps_sans23x27.c + endif + ifeq ($(NXFONTS_FONTID),2) + NXFONTS_PREFIX := g_myfont_ + GEN_CSRC = nxfonts_bitmaps_myfont.c + endif + + 8. graphics/nxfonts/nxfonts_bitmaps.c. This is the file that contains + the generic font structures. It is used as a "template" file by + nuttx/graphics/nxfonts/Makefile.sources to create your customized + font data set. + + #if NXFONTS_FONTID == 1 + # include "nxfonts_sans23x27.h" + #elif NXFONTS_FONTID == 2 + # include "nxfonts_myfont.h" + #else + # error "No font ID specified" + #endif + + Where nxfonts_myfont.h is the NuttX font file that we generated in + step 2 using the bdf-converter tool. + + 9. Finally, we need to extend the logic that does the run-time font + lookups so that can find our new font. The lookup function is + NXHANDLE nxf_getfonthandle(enum nx_fontid_e fontid). The new + font information needs to be added to data structures used by + that function: + + #ifdef CONFIG_NXFONT_SANS23X27 + extern const struct nx_fontpackage_s g_sans23x27_package; + #endif + #ifdef CONFIG_NXFONT_MYFONT + extern const struct nx_fontpackage_s g_myfont_package; + #endif + + static FAR const struct nx_fontpackage_s *g_fontpackages[] = + { + #ifdef CONFIG_NXFONT_SANS23X27 + &g_sans23x27_package, + #endif + #ifdef CONFIG_NXFONT_MYFONT + &g_myfont_package, + #endif + NULL + }; + Makefile.host This is the makefile that is used to make the mkconfig program from diff --git a/nuttx/tools/bdf-converter.c b/nuttx/tools/bdf-converter.c new file mode 100644 index 000000000..94d00b8ee --- /dev/null +++ b/nuttx/tools/bdf-converter.c @@ -0,0 +1,407 @@ +/**************************************************************************** + * tools/bdf-converter.c + * + * Copyright (C) 2011 NX Engineering, S.A., All rights reserved. + * Author: Jose Pablo Carballo Gomez <jcarballo@nx-engineering.com> + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. 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. + * 3. Neither the name NuttX nor the names of its contributors may be + * used to endorse or promote products derived from this software + * without specific prior written permission. + * + * 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 OWNER 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. + * + ****************************************************************************/ + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include <stdio.h> +#include <stdlib.h> +#include <stdbool.h> +#include <string.h> +#include <ctype.h> + +/**************************************************************************** + * Pre-Processor Definitions + ****************************************************************************/ + +// BDF Specification Version 2.2: +// This version lifts the restriction on line length. In this version, the new +// maximum length of a value of the type string is 65535 characters, and hence +// lines may now be at least this long. + +#define BDF_MAX_LINE_LENGTH 65535 + +/* Ranges of 7-bit and 8-bit fonts */ + +#define NXFONT_MIN7BIT 33 +#define NXFONT_MAX7BIT 126 + +#define NXFONT_MIN8BIT 161 +#define NXFONT_MAX8BIT 255 + +/**************************************************************************** + * Public Types + ****************************************************************************/ + +/* This structure holds information about a glyph */ + +typedef struct glyphinfo_s +{ + char *name; /* Name for they glyph */ + int encoding; /* The Adobe Standard Encoding value */ + int bb_w; /* The width of the black pixels in x */ + int bb_h; /* The height of the black pixels in y */ + int bb_x_off; /* X displacement of the lower left corner + * of the bitmap from origin 0 */ + int bb_y_off; /* Y displacement of the lower left corner + * of the bitmap from origin 0 */ + uint32_t *bitmap; /* Hexadecimal data for the */ +} glyphinfo_t; + +/* This structures provides the metrics for one glyph */ + +typedef struct nx_fontmetric_s +{ + uint32_t stride : 2; /* Width of one font row in bytes */ + uint32_t width : 6; /* Width of the font in bits */ + uint32_t height : 6; /* Height of the font in rows */ + uint32_t xoffset : 6; /* Top, left-hand corner X-offset in pixels */ + uint32_t yoffset : 6; /* Top, left-hand corner y-offset in pixels */ + uint32_t unused : 6; +} nx_fontmetric_t; + +/**************************************************************************** + * Private Functions + ****************************************************************************/ + +static void trimLine(char *line) +{ + char *str; + str = line; + char *strEnd; + for (strEnd = str + strlen(str) - 1; + strEnd >= str && isspace((int)(*strEnd)); + strEnd--); + *(strEnd + 1) = 0; +} + +/**************************************************************************** + * Name: bdf_parseIntLine + * + * Description: + * Parses a line containing a BDF property followed by integers. It will + * ignore the first token that corresponds to the property name. + * + * Input Parameters: + * line - A line with a BDF property followed by integers, i.e.: + * "FONTBOUNDINGBOX 8 13 0 -2" + * count - How many integers are specified by the BDF property. In the + * example above, count = 4. + * info - A pointer to memory provided by the caller in which to + * return the array of integers. For the example above: + * info[0] = 8 + * info[1] = 13 + * info[2] = 0 + * info[3] = -2 + * + ****************************************************************************/ +static void bdf_parseintline(char *line, unsigned int count, int *info) +{ + char *str, *token, *saveptr1; + str = line; + + /* Ignore the key */ + + token = (char *)strtok_r(str, " ", &saveptr1); + + while ((token = (char *)strtok_r(NULL, " ", &saveptr1)) && count--) + { + *(info++) = atoi(token); + } +} + +static void bdf_printglyphinfo(const glyphinfo_t *ginfo) +{ + printf("NAME = %s\n", ginfo->name); + printf("ENCODING = %d\n", ginfo->encoding); + printf("BB_W = %d\n", ginfo->bb_w); + printf("BB_H = %d\n", ginfo->bb_h); + printf("BB_X_OFF = %d\n", ginfo->bb_x_off); + printf("BB_Y_OFF = %d\n", ginfo->bb_y_off); + int i; + for (i = 0; i < ginfo->bb_h; i++) + { + printf("BITMAP[%d] = %x\n", i, ginfo->bitmap[i]); + } +} + +static void bdf_printnxmetricinfo(const nx_fontmetric_t *info) +{ + printf("STRIDE = %d\n", info->stride); + printf("WIDTH = %d\n", info->width); + printf("HEIGHT = %d\n", info->height); + printf("XOFFSET = %d\n", info->xoffset); + printf("YOFFSET = %d\n", info->yoffset); +} + +static void bdf_getglyphinfo(FILE *file, glyphinfo_t *ginfo) +{ + char line[BDF_MAX_LINE_LENGTH]; + char lineCopy[BDF_MAX_LINE_LENGTH]; + char *str, *token, *saveptr1; + bool done; + + done = false; + + while(fgets(line, BDF_MAX_LINE_LENGTH, file) != NULL && !done) + { + trimLine(line); + strcpy(lineCopy, line); + str = line; + + while ((token = (char *)strtok_r(str, " ", &saveptr1))) + { + + /* ENCODING information */ + + if(strcmp(token, "ENCODING") == 0) + { + token = (char *)strtok_r(NULL, " ", &saveptr1); + ginfo->encoding = atoi(token); + } + + /* BBX information */ + + else if(strcmp(token, "BBX") == 0) + { + int bbxinfo[4]; + bdf_parseintline(lineCopy, 4, bbxinfo); + ginfo->bb_w = bbxinfo[0]; + ginfo->bb_h = bbxinfo[1]; + ginfo->bb_x_off = bbxinfo[2]; + ginfo->bb_y_off = bbxinfo[3]; + + /* This is the last BDF property of interest*/ + + done = true; + } + + str = NULL; + } + + } +} + +static void bdf_getglyphbitmap(FILE *file, glyphinfo_t *ginfo) +{ + char line[BDF_MAX_LINE_LENGTH]; + uint32_t *bitmap; + bool readingbitmap; + + bitmap = ginfo->bitmap; + readingbitmap = true; + + while (readingbitmap) + { + if (fgets(line, BDF_MAX_LINE_LENGTH, file) != NULL) + { + trimLine(line); + + if(strcmp(line, "ENDCHAR") == 0) + { + readingbitmap = false; + } + else + { + char *endptr; + *bitmap = strtoul(line, &endptr, 16); + bitmap++; + } + + } + else + { + /* error condition */ + + readingbitmap = false; + } + + } +} + +static void bdf_getstride(glyphinfo_t *ginfo, uint32_t *stride) +{ + *stride = (ginfo->bb_w % 8 == 0) ? ginfo->bb_w / 8 : ginfo->bb_w / 8 + 1 ; +} + +static void bdf_printoutput(FILE *out, + glyphinfo_t *ginfo, + nx_fontmetric_t *nxmetric) +{ + + /* Only interested in the 7 and 8 bit ranges */ + + if ((ginfo->encoding >= NXFONT_MIN7BIT && + ginfo->encoding <= NXFONT_MAX7BIT) || + (ginfo->encoding >= NXFONT_MIN8BIT && + ginfo->encoding <= NXFONT_MAX8BIT)) + { + + /* Glyph general info */ + + fprintf(out, "/* %s (%d) */\n", ginfo->name, ginfo->encoding); + + /* Glyph metrics */ + + fprintf(out, + "#define NXFONT_METRICS_%d {%d, %d, %d, %d, %d, 0}\n", + ginfo->encoding, + nxmetric->stride, + nxmetric->width, + nxmetric->height, + nxmetric->xoffset, + nxmetric->yoffset); + + /* Glyph bitmap */ + + fprintf(out, "#define NXFONT_BITMAP_%d {", ginfo->encoding); + int i; + for (i = 0; i < ginfo->bb_h - 1; i++) + { + fprintf(out, "0x%x, ", ginfo->bitmap[i]); + } + fprintf(out, "0x%x}\n", ginfo->bitmap[i]); + + fprintf(out, "\n"); + } + +} + +/**************************************************************************** + * Main + ****************************************************************************/ + +int main(int argc, char **argv) +{ + FILE *file, *out; + char line[BDF_MAX_LINE_LENGTH]; + char lineCopy[BDF_MAX_LINE_LENGTH]; + char *str, *token, *saveptr1; + + /* FONTBOUNDINGBOX properties*/ + int fbb_x, fbb_y, fbb_x_off, fbb_y_off; + + file = fopen("8x13.bdf", "r"); + out = fopen("out.txt", "w"); + + if (file == NULL) + { + perror("Error opening file"); + } + else + { + while (fgets(line, BDF_MAX_LINE_LENGTH, file) != NULL) + { + printf("--\n"); + + // Save a copy of the line + + strcpy(lineCopy,line); + + // Clean it + + trimLine(line); + str = line; + + while ((token = (char *)strtok_r(str, " ", &saveptr1))) + { + + /* FONTBOUNDINGBOX - Global font information */ + + if (strcmp(token, "FONTBOUNDINGBOX") == 0) + { + int fbbinfo[4]; + bdf_parseintline(lineCopy, 4, fbbinfo); + fbb_x = fbbinfo[0]; + fbb_y = fbbinfo[1]; + fbb_x_off = fbbinfo[2]; + fbb_y_off = fbbinfo[3]; + } + + /* STARTCHAR - Individual glyph information */ + + if (strcmp(token, "STARTCHAR") == 0) + { + glyphinfo_t ginfo; + + /* Glyph name */ + + ginfo.name = (char *)strtok_r(NULL, " ", &saveptr1); + + /* Glyph information: + * ENCODING + * BBX + */ + + bdf_getglyphinfo(file, &ginfo); + + /* Glyph bitmap */ + + ginfo.bitmap = malloc(sizeof(uint32_t) * ginfo.bb_h); + bdf_getglyphbitmap(file, &ginfo); + + bdf_printglyphinfo(&ginfo); + + /* Convert to nxfonts */ + + nx_fontmetric_t nxmetric; + uint32_t stride; + bdf_getstride(&ginfo, &stride); + nxmetric.stride = stride; + nxmetric.width = ginfo.bb_w; + nxmetric.height = ginfo.bb_h; + nxmetric.xoffset = (-fbb_x_off) + ginfo.bb_x_off; + nxmetric.yoffset = fbb_y + fbb_y_off - ginfo.bb_y_off; + bdf_printnxmetricinfo(&nxmetric); + + bdf_printoutput(out, &ginfo, &nxmetric); + + /* Free memory */ + + free(ginfo.bitmap); + + } + + str = NULL; + } + + } + fclose(file); + fclose(out); + } + + return EXIT_SUCCESS; +} |