From 1fd361a1ea06e44286c213ca1f814f49306fdc43 Mon Sep 17 00:00:00 2001 From: giraffedata Date: Sat, 19 Aug 2006 03:12:28 +0000 Subject: Create Subversion repository git-svn-id: http://svn.code.sf.net/p/netpbm/code/trunk@1 9d0c8265-081b-0410-96cb-a4ca84ce46f8 --- lib/ppmdfont.c | 133 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 133 insertions(+) create mode 100644 lib/ppmdfont.c (limited to 'lib/ppmdfont.c') diff --git a/lib/ppmdfont.c b/lib/ppmdfont.c new file mode 100644 index 00000000..a378f79c --- /dev/null +++ b/lib/ppmdfont.c @@ -0,0 +1,133 @@ +#include +#include + +#include "pm.h" +#include "mallocvar.h" +#include "ppmdfont.h" + + +extern struct ppmd_font ppmd_standardfont; + +static const struct ppmd_font * currentFontP = &ppmd_standardfont; + +static void +readGlyphHeader(FILE * const ifP, + struct ppmd_glyphHeader * const glyphHeaderP) { + + glyphHeaderP->commandCount = fgetc(ifP); + glyphHeaderP->skipBefore = fgetc(ifP); + glyphHeaderP->skipAfter = fgetc(ifP); +} + + + +static void +readGlyphCommand(FILE * const ifP, + struct ppmd_glyphCommand * const glyphCommandP) { + + glyphCommandP->verb = fgetc(ifP); + glyphCommandP->x = fgetc(ifP); + glyphCommandP->y = fgetc(ifP); +} + + + +static void +readCharacter(FILE * const ifP, + struct ppmd_glyph * const glyphP) { + + unsigned int commandNum; + struct ppmd_glyphCommand * commandList; + + readGlyphHeader(ifP, &glyphP->header); + + MALLOCARRAY(commandList, glyphP->header.commandCount); + + if (commandList == NULL) + pm_error("Insufficient memory to create a %u-command " + "command list.", glyphP->header.commandCount); + + for (commandNum = 0; + commandNum < glyphP->header.commandCount; + ++commandNum) { + + readGlyphCommand(ifP, &commandList[commandNum]); + } + glyphP->commandList = commandList; +} + + + +static void +readFontHeader(FILE * const ifP, + struct ppmd_fontHeader * const fontHeaderP) { + + fread(&fontHeaderP->signature, 1, sizeof(fontHeaderP->signature), ifP); + fontHeaderP->format = fgetc(ifP); + fontHeaderP->characterCount = fgetc(ifP); + fontHeaderP->firstCodePoint = fgetc(ifP); +} + + + +void +ppmd_set_font(const struct ppmd_font * const newFontP) { + + currentFontP = newFontP; +} + + + +const struct ppmd_font * +ppmd_get_font(void) { + + return currentFontP; +} + + + +void +ppmd_read_font(FILE * const ifP, + const struct ppmd_font ** const fontPP) { + + unsigned int relativeCodePoint; + struct ppmd_glyph * glyphTable; + struct ppmd_font * fontP; + + MALLOCVAR(fontP); + if (fontP == NULL) + pm_error("Insufficient memory for font header"); + + readFontHeader(ifP, &fontP->header); + + MALLOCARRAY(glyphTable, fontP->header.characterCount); + if (glyphTable == NULL) + pm_error("Insufficient memory to store %u characters", + fontP->header.characterCount); + + for (relativeCodePoint = 0; + relativeCodePoint < fontP->header.characterCount; + ++relativeCodePoint) { + + readCharacter(ifP, &glyphTable[relativeCodePoint]); + } + fontP->glyphTable = glyphTable; + *fontPP = fontP; +} + + + +void +ppmd_free_font(const struct ppmd_font * const fontP) { + + unsigned int relativeCodePoint; + + for (relativeCodePoint = 0; + relativeCodePoint < fontP->header.characterCount; + ++relativeCodePoint) { + + free((void*)fontP->glyphTable[relativeCodePoint].commandList); + } + free((void*)fontP->glyphTable); + free((void*)fontP); +} -- cgit 1.4.1