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
This commit is contained in:
patacongo 2011-07-23 13:53:59 +00:00
parent 56de8b9f01
commit 2ee238e14b
7 changed files with 895 additions and 23 deletions

View File

@ -1939,4 +1939,7 @@
* configs/stm3210e-eval/nsh2: Extended to support two new commands:
'msconn' will connect the USB mass storage device; 'msdis' will
disconnect the USB storage device.
* tools/bdf-converter.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.

View File

@ -12,7 +12,7 @@
<h1><big><font color="#3c34ec">
<i>NX Graphics Subsystem</i>
</font></big></h1>
<p>Last Updated: July 19, 2011</p>
<p>Last Updated: July 23, 2011</p>
</td>
</tr>
</table>
@ -178,14 +178,17 @@
</ul>
</p>
<p>
<big><b>Appendix C</b> <a href="#testcoverage">NX Test Coverage</a></big>
<big><b>Appendix C</b> <a href="#installnewfonts">Installing New Fonts</a></big>
</p>
<p>
<big><b>Appendix D</b> <a href="#testcoverage">NX Test Coverage</a></big>
</p>
<ul>
<i><b>Table C.1:</b> <a href="#nxglibcoverage">NXGLIB API Test Coverage</a></i><br>
<i><b>Table C.2:</b> <a href="#nxcbcoverage">NX Server Callbacks Test Coverage</a></i><br>
<i><b>Table C.3:</b> <a href="#nxcoverage">NX API Test Coverage</a></i><br>
<i><b>Table C.4:</b> <a href="#nxtkcoverage">NXTK API Test Coverage</a></i><br>
<i><b>Table C.5:</b> <a href="#nxfontscoverage">NXFONTS API Test Coverage</a></i><br>
<i><b>Table D.1:</b> <a href="#nxglibcoverage">NXGLIB API Test Coverage</a></i><br>
<i><b>Table D.2:</b> <a href="#nxcbcoverage">NX Server Callbacks Test Coverage</a></i><br>
<i><b>Table D.3:</b> <a href="#nxcoverage">NX API Test Coverage</a></i><br>
<i><b>Table D.4:</b> <a href="#nxtkcoverage">NXTK API Test Coverage</a></i><br>
<i><b>Table D.5:</b> <a href="#nxfontscoverage">NXFONTS API Test Coverage</a></i><br>
</ul>
</td>
</tr>
@ -2729,7 +2732,185 @@ int nxf_convert_32bpp(FAR uint32_t *dest, uint16_t height,
<table width ="100%">
<tr bgcolor="#e4e4e4">
<td>
<h1>Appendix C <a name="testcoverage">NX Test Coverage</a></h1>
<h1>Appendix C <a name="installnewfonts">Installing New Fonts</a></h1>
</td>
</tr>
</table>
<p>
There is a tool called <i>bdf-converter</i> in the directory <code>tools/.</code>.
The <i>bdf-converter</i> program be used to convert fonts in Bitmap Distribution Format (BDF) into fonts that can be used in the NX graphics system.
</p>
<p>
Below are general instructions for creating and installing a new font in the NX graphic system:
</p>
<ol start="1">
<li>
<p>
Locate a font in BDF format,
</p>
</li>
<li>
<p>
Use the <i>bdf-converter</i> 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, <code>graphics/nxfonts/nxfonts_myfont.h</code>.
</p>
</li>
</ol>
<p>
Create a new NuttX configuration variable.
For example, suppose you define the following variable: <code>CONFIG_NXFONT_MYFONT</code>.
Then you would need to:
</p>
<ol start="3">
<li>
<p>
Define <code>CONFIG_NXFONT_MYFONT=y</code> in your NuttX configuration file.
</p>
</li>
</ol>
<p>
A font ID number has to be assigned for each new font.
The font ID is defined in the file <code>include/nuttx/nxfonts.h</code>.
Those definitions have to be extended to support your new font.
Look at how the font ID enabled by <code>CONFIG_NXFONT_SANS23X27</code> is defined and add an ID for yournew font in a similar fashion:
</p>
<ol start="4">
<li>
<p>
<code>include/nuttx/nxfonts.h</code>. Add you new font as a possible system default font:
</p>
<ul></pre>
#if defined(CONFIG_NXFONT_SANS23X27)
# define NXFONT_DEFAULT FONTID_SANS23X27
#elif defined(CONFIG_NXFONT_MYFONT)
# define NXFONT_DEFAULT FONTID_MYFONT
#endif
</pre><ul>
<p>
Then define the actual font ID.
Make sure that the font ID value is unique:
</p>
<ul><pre>
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
...
</pre></ul>
</li>
</ol>
<p>
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:
</p>
<ol start="5">
<li>
<p>
<code>nuttx/graphics/Makefile</code>.
This file needs logic to auto-generate a C source file from the header file that you generated with the the <i>bdf-converter</i> program.
Notice <code>NXFONTS_FONTID=2</code>; this must be set to the same font ID value that you defined in the <code>include/nuttx/nxfonts.h</code> file.
</p>
<ul><pre>
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
</pre></ul>
</li>
<li>
<p>
<code>nuttx/graphics/nxfonts/Make.defs</code>.
Set the make variable <code>NXFSET_CSRCS</code>.
<code>NXFSET_CSRCS</code> determines the name of the font C file to build when <code>NXFONTS_FONTID=2</code>:
</p>
<ul><pre>
ifeq ($(CONFIG_NXFONT_SANS23X27),y)
NXFSET_CSRCS += nxfonts_bitmaps_sans23x27.c
endif
ifeq ($(CONFIG_NXFONT_MYFONT),y)
NXFSET_CSRCS += nxfonts_bitmaps_myfont.c
endif
</pre></ul>
</li>
<li>
<p>
<code>nuttx/graphics/nxfonts/Makefile.sources</code>.
This is the Makefile used in step 5 that will actually generate the font C file.
So, given your </code>NXFONTS_FONTID=2</code>, 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 <code>nuttx/graphics/nxfonts/Make.defs</code>):
</p>
<ul><pre>
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
</pre></ul>
</li>
<li>
<p>
<code>graphics/nxfonts/nxfonts_bitmaps.c</code>.
This is the file that contains the generic font structures.
It is used as a &quot;template&qout; file by <code>nuttx/graphics/nxfonts/Makefile.sources </code>to create your customized font data set at build time.
</p>
<ul><pre>
#if NXFONTS_FONTID == 1
# include "nxfonts_sans23x27.h"
#elif NXFONTS_FONTID == 2
# include "nxfonts_myfont.h"
#else
# error "No font ID specified"
#endif
</pre></ul>
<p>
Where <code>nxfonts_myfont.h<code> is the NuttX font file that we generated in
step 2 using the <i>bdf-converter</i> tool.
</p>
<li>
<p>
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 <code>NXHANDLE nxf_getfonthandle(enum nx_fontid_e fontid)</code>.
The new font information needs to be added to data structures used by that function:
</p>
<ul><pre>
#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
};
</pre></ul>
</li>
</ol>
<table width ="100%">
<tr bgcolor="#e4e4e4">
<td>
<h1>Appendix D <a name="testcoverage">NX Test Coverage</a></h1>
</td>
</tr>
</table>
@ -2810,7 +2991,7 @@ make
The following table describes the testing performed on each NX API:
</p>
<center><h2>Table C.1: <a name="nxglibcoverage">NXGLIB API Test Coverage</a></h2></center>
<center><h2>Table D.1: <a name="nxglibcoverage">NXGLIB API Test Coverage</a></h2></center>
<center><table border="1" width="80%">
<tr>
<th width="40%">Function</th>
@ -2910,7 +3091,7 @@ make
</table></center>
<center><h2>Table C.2: <a name="nxcbcoverage">NX Server Callbacks Test Coverage</a></h2></center>
<center><h2>Table D.2: <a name="nxcbcoverage">NX Server Callbacks Test Coverage</a></h2></center>
<center><table border="1" width="80%">
<tr>
<th width="40%">Function</th>
@ -2939,7 +3120,7 @@ make
</tr>
</table></center>
<center><h2>Table C.3: <a name="nxcoverage">NX API Test Coverage</a></h2></center>
<center><h2>Table D.3: <a name="nxcoverage">NX API Test Coverage</a></h2></center>
<center><table border="1" width="80%">
<tr>
<th width="40%">Function</th>
@ -3083,7 +3264,7 @@ make
</table></center>
<center><h2>Table C.4: <a name="nxtkcoverage">NXTK API Test Coverage</a></h2></center>
<center><h2>Table D.4: <a name="nxtkcoverage">NXTK API Test Coverage</a></h2></center>
<center><table border="1" width="80%">
<tr>
<th width="40%">Function</th>
@ -3177,7 +3358,7 @@ make
</tr>
</table></center>
<center><h2>Table C.5: <a name="nxfontscoverage">NXFONTS API Test Coverage</a></h2></center>
<center><h2>Table D.5: <a name="nxfontscoverage">NXFONTS API Test Coverage</a></h2></center>
<center><table border="1" width="80%">
<tr>
<th width="40%">Function</th>

View File

@ -4,6 +4,14 @@ README
This directory contains tiny graphics support for NuttX. The contents of this directory
are only build if CONFIG_NX is defined in the NuttX configuration file.
Contents
^^^^^^^^
Roadmap
Related Header Files
Directories
Installing New Fonts
Configuration Settings
Roadmap
^^^^^^^
@ -37,8 +45,8 @@ include/nutt/nxtk.h -- Describe the NXTOOLKIT C interfaces
include/nutt/nxfont.h -- Describe sthe NXFONT C interfaces
include/nuttx/nxwidgets.h -- Will describe the NXWIDGETS classes (no longer planned)
Directories:
^^^^^^^^^^^^
Directories
^^^^^^^^^^^
graphics/nxglib
The NuttX tiny graphics library. The directory contains generic utilities
@ -80,6 +88,142 @@ graphics/nxtk
graphics/nxwidgets
At one time, I planned to put NXWIDGETS implementation here, but not anymore.
Installing New Fonts
^^^^^^^^^^^^^^^^^^^^
There is a tool called bdf-converter in the directory tools/. 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
};
Configuration Settings
^^^^^^^^^^^^^^^^^^^^^^

View File

@ -54,10 +54,6 @@
# define CONFIG_NXFONTS_CHARBITS 7
#endif
/* Only font supported for now */
#define CONFIG_NXFONT_SANS23X27 1
/****************************************************************************
* Public Types
****************************************************************************/

View File

@ -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
@rm -f mkconfig mksyscall mkversion bdf-converter
@rm -f mkconfig.exe mksyscall.exe mkversion.exe bdf-converter.exe

View File

@ -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

407
tools/bdf-converter.c Normal file
View File

@ -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;
}