about summary refs log tree commit diff
path: root/lib/ppmdfont.c
diff options
context:
space:
mode:
authorgiraffedata <giraffedata@9d0c8265-081b-0410-96cb-a4ca84ce46f8>2006-08-19 03:12:28 +0000
committergiraffedata <giraffedata@9d0c8265-081b-0410-96cb-a4ca84ce46f8>2006-08-19 03:12:28 +0000
commit1fd361a1ea06e44286c213ca1f814f49306fdc43 (patch)
tree64c8c96cf54d8718847339a403e5e67b922e8c3f /lib/ppmdfont.c
downloadnetpbm-mirror-1fd361a1ea06e44286c213ca1f814f49306fdc43.tar.gz
netpbm-mirror-1fd361a1ea06e44286c213ca1f814f49306fdc43.tar.xz
netpbm-mirror-1fd361a1ea06e44286c213ca1f814f49306fdc43.zip
Create Subversion repository
git-svn-id: http://svn.code.sf.net/p/netpbm/code/trunk@1 9d0c8265-081b-0410-96cb-a4ca84ce46f8
Diffstat (limited to 'lib/ppmdfont.c')
-rw-r--r--lib/ppmdfont.c133
1 files changed, 133 insertions, 0 deletions
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 <stdio.h>
+#include <assert.h>
+
+#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);
+}