commit d01659aacdcc639bf40f7d4dc741d9480d0232a3
parent b3509a3c96665faca579c09a0c6840d31ddb371a
Author: Frederic Cambus <fred@statdns.com>
Date: Thu, 6 Feb 2020 14:32:16 +0100
Introduce a parseLine() function to parse keys, makes things cleaner.
Diffstat:
4 files changed, 194 insertions(+), 113 deletions(-)
diff --git a/CMakeLists.txt b/CMakeLists.txt
@@ -4,7 +4,7 @@
# https://github.com/fcambus/bdftosfd
#
# Created: 2019-11-21
-# Last Updated: 2020-01-16
+# Last Updated: 2020-02-06
#
# bdftosfd is released under the BSD 2-Clause license
# See LICENSE file for details
@@ -26,7 +26,7 @@ check_function_exists(strtonum HAVE_STRTONUM)
include_directories("compat")
set(CMAKE_BUILD_TYPE Release)
-set(SRC src/bdftosfd.c src/header.c)
+set(SRC src/bdftosfd.c src/header.c src/parse.c)
if(NOT HAVE_PLEDGE)
set (SRC ${SRC} compat/pledge.c)
diff --git a/src/bdftosfd.c b/src/bdftosfd.c
@@ -30,6 +30,7 @@
#include "compat.h"
#include "config.h"
#include "header.h"
+#include "parse.h"
struct timespec begin, end, elapsed;
@@ -67,6 +68,7 @@ main(int argc, char *argv[])
int32_t height = 0, width = 0;
int32_t ascent = 0, descent = 0;
+ int key;
int32_t x = 0, y = 0;
uint32_t mask = 0;
int32_t xlength = 64, ylength = 64; /* Default values for 8x16 fonts */
@@ -125,10 +127,15 @@ main(int argc, char *argv[])
return EXIT_FAILURE;
}
-
while (fgets(lineBuffer, LINE_LENGTH_MAX, bdfFile)) {
- if (*lineBuffer) {
- if (!font.name && !strncmp(lineBuffer, "FAMILY_NAME ", 12)) {
+ if (!*lineBuffer)
+ continue;
+
+ key = parseLine(lineBuffer);
+
+ switch(key) {
+ case FAMILY_NAME:
+ if (!font.name) {
token = strtok(lineBuffer, " \t");
if (token)
@@ -137,22 +144,22 @@ main(int argc, char *argv[])
if (name)
font.name = strdup(name);
- continue;
}
- if (!strncmp(lineBuffer, "COPYRIGHT ", 10)) {
- token = strtok(lineBuffer, " \t");
+ continue;
- if (token)
- copyright = strtok(NULL, "\n");
+ case COPYRIGHT:
+ token = strtok(lineBuffer, " \t");
- if (copyright)
- font.copyright = strdup(copyright);
+ if (token)
+ copyright = strtok(NULL, "\n");
- continue;
- }
+ if (copyright)
+ font.copyright = strdup(copyright);
+
+ continue;
- if (!strncmp(lineBuffer, "FONTBOUNDINGBOX ", 16)) {
+ case FONTBOUNDINGBOX:
token = strtok(lineBuffer, " \t");
if (token)
@@ -181,140 +188,130 @@ main(int argc, char *argv[])
mask = 1 << (stride[width] * 8 - 1);
continue;
- }
- if (!strncmp(lineBuffer, "FONT_ASCENT ", 12)) {
- token = strtok(lineBuffer, " \t");
+ case FONT_ASCENT:
+ token = strtok(lineBuffer, " \t");
- if (token)
- value = strtok(NULL, "\n");
+ if (token)
+ value = strtok(NULL, "\n");
- if (value)
- ascent = strtonum(value, 0, 64, &errstr);
+ if (value)
+ ascent = strtonum(value, 0, 64, &errstr);
- if (!errstr)
- font.ascent = ascent * ylength;
- else
- errx(EXIT_FAILURE, "Invalid value for FONT_ASCENT.");
+ if (!errstr)
+ font.ascent = ascent * ylength;
+ else
+ errx(EXIT_FAILURE, "Invalid value for FONT_ASCENT.");
- continue;
- }
+ continue;
- if (!strncmp(lineBuffer, "FONT_DESCENT ", 12)) {
- token = strtok(lineBuffer, " \t");
+ case FONT_DESCENT:
+ token = strtok(lineBuffer, " \t");
- if (token)
- value = strtok(NULL, "\n");
+ if (token)
+ value = strtok(NULL, "\n");
- if (value)
- descent = strtonum(value, 0, 64, &errstr);
+ if (value)
+ descent = strtonum(value, 0, 64, &errstr);
- if (!errstr)
- font.descent = descent * ylength;
- else
- errx(EXIT_FAILURE, "Invalid value for FONT_DESCENT.");
+ if (!errstr)
+ font.descent = descent * ylength;
+ else
+ errx(EXIT_FAILURE, "Invalid value for FONT_DESCENT.");
- continue;
- }
+ continue;
- if (!strncmp(lineBuffer, "FONT_VERSION ", 13)) {
- token = strtok(lineBuffer, " \t");
+ case FONT_VERSION:
+ token = strtok(lineBuffer, " \t");
- if (token)
- version = strtok(NULL, "\n");
+ if (token)
+ version = strtok(NULL, "\n");
- if (version)
- font.version = strdup(version);
+ if (version)
+ font.version = strdup(version);
- continue;
- }
+ continue;
- if (!strncmp(lineBuffer, "CHARS ", 6)) {
- token = strtok(lineBuffer, " \t");
+ case CHARS:
+ token = strtok(lineBuffer, " \t");
- if (token)
- font.chars = strtok(NULL, " \n");
+ if (token)
+ font.chars = strtok(NULL, " \n");
- if (font.chars)
- header(stdout, &font);
- else
- errx(EXIT_FAILURE, "Invalid value for CHARS.");
+ if (font.chars)
+ header(stdout, &font);
+ else
+ errx(EXIT_FAILURE, "Invalid value for CHARS.");
- continue;
- }
+ continue;
- if (!strncmp(lineBuffer, "STARTCHAR", 9)) {
- fprintf(stdout, "StartChar:");
- token = strtok(lineBuffer, " \t");
+ case STARTCHAR:
+ fprintf(stdout, "StartChar:");
+ token = strtok(lineBuffer, " \t");
- if (token) {
- charname = strtok(NULL, " \n");
+ if (token) {
+ charname = strtok(NULL, " \n");
- while (charname) {
- fprintf(stdout, " %s", charname);
- charname = strtok(NULL, " \n");
- }
+ while (charname) {
+ fprintf(stdout, " %s", charname);
+ charname = strtok(NULL, " \n");
}
-
- continue;
}
- if (!strncmp(lineBuffer, "ENCODING", 8)) {
- token = strtok(lineBuffer, " \t");
+ continue;
- if (token)
- encoding = strtok(NULL, " \n");
+ case ENCODING:
+ token = strtok(lineBuffer, " \t");
- if (encoding)
- fprintf(stdout, "\nEncoding: %s %s %s\n", encoding, encoding, encoding);
+ if (token)
+ encoding = strtok(NULL, " \n");
- continue;
- }
+ if (encoding)
+ fprintf(stdout, "\nEncoding: %s %s %s\n", encoding, encoding, encoding);
- if (!strncmp(lineBuffer, "BITMAP", 6)) {
- fprintf(stdout, "Width: 512\n"
- "Flags: HW\n"
- "LayerCount: 2\n"
- "Fore\n"
- "SplineSet\n");
+ continue;
- y = font.ascent;
- readglyph = true;
- glyphes++;
+ case BITMAP:
+ fprintf(stdout, "Width: 512\n"
+ "Flags: HW\n"
+ "LayerCount: 2\n"
+ "Fore\n"
+ "SplineSet\n");
- continue;
- }
+ y = font.ascent;
+ readglyph = true;
+ glyphes++;
- if (!strncmp(lineBuffer, "ENDCHAR", 7)) {
- fprintf(stdout, "EndSplineSet\n"
- "EndChar\n\n");
+ continue;
- readglyph = false;
+ case ENDCHAR:
+ fprintf(stdout, "EndSplineSet\n"
+ "EndChar\n\n");
- continue;
- }
+ readglyph = false;
+ continue;
+ }
- if (readglyph) {
- uint32_t row = strtoul(lineBuffer, NULL, 16);
-
- for (size_t column = 0; column < width; column++) {
- if ((row & (mask >> column)) != 0) {
- x = column * xlength;
- fprintf(stdout, "%d %d m 1\n"
- " %d %d l 1\n"
- " %d %d l 1\n"
- " %d %d l 1\n"
- " %d %d l 1\n",
- x, y,
- x, y - ylength,
- x + xlength, y - ylength,
- x + xlength, y,
- x, y);
- }
+ if (readglyph) {
+ uint32_t row = strtoul(lineBuffer, NULL, 16);
+
+ for (size_t column = 0; column < width; column++) {
+ if ((row & (mask >> column)) != 0) {
+ x = column * xlength;
+ fprintf(stdout, "%d %d m 1\n"
+ " %d %d l 1\n"
+ " %d %d l 1\n"
+ " %d %d l 1\n"
+ " %d %d l 1\n",
+ x, y,
+ x, y - ylength,
+ x + xlength, y - ylength,
+ x + xlength, y,
+ x, y);
}
-
- y -= ylength;
}
+
+ y -= ylength;
}
}
diff --git a/src/parse.c b/src/parse.c
@@ -0,0 +1,54 @@
+/*
+ * bdftosfd
+ * Copyright (c) 2019-2020, Frederic Cambus
+ * https://github.com/fcambus/bdftosfd
+ *
+ * Created: 2019-11-21
+ * Last Updated: 2020-02-06
+ *
+ * bdftosfd is released under the BSD 2-Clause license
+ * See LICENSE file for details
+ */
+
+#include <string.h>
+#include "parse.h"
+
+int
+parseLine(char *lineBuffer) {
+ if (*lineBuffer) {
+ if (!strncmp(lineBuffer, "FAMILY_NAME ", 12))
+ return FAMILY_NAME;
+
+ if (!strncmp(lineBuffer, "COPYRIGHT ", 10))
+ return COPYRIGHT;
+
+ if (!strncmp(lineBuffer, "FONTBOUNDINGBOX ", 16))
+ return FONTBOUNDINGBOX;
+
+ if (!strncmp(lineBuffer, "FONT_ASCENT ", 12))
+ return FONT_ASCENT;
+
+ if (!strncmp(lineBuffer, "FONT_DESCENT ", 12))
+ return FONT_DESCENT;
+
+ if (!strncmp(lineBuffer, "FONT_VERSION ", 13))
+ return FONT_VERSION;
+
+ if (!strncmp(lineBuffer, "CHARS ", 6))
+ return CHARS;
+
+ if (!strncmp(lineBuffer, "STARTCHAR", 9))
+ return STARTCHAR;
+
+ if (!strncmp(lineBuffer, "ENCODING", 8))
+ return ENCODING;
+
+ if (!strncmp(lineBuffer, "BITMAP", 6))
+ return BITMAP;
+
+ if (!strncmp(lineBuffer, "ENDCHAR", 7))
+ return ENDCHAR;
+ }
+
+ return 0;
+}
diff --git a/src/parse.h b/src/parse.h
@@ -0,0 +1,30 @@
+/*
+ * bdftosfd
+ * Copyright (c) 2019-2020, Frederic Cambus
+ * https://github.com/fcambus/bdftosfd
+ *
+ * Created: 2019-11-21
+ * Last Updated: 2020-02-06
+ *
+ * bdftosfd is released under the BSD 2-Clause license
+ * See LICENSE file for details
+ */
+
+#ifndef PARSE_H
+#define PARSE_H
+
+#define FAMILY_NAME 1
+#define COPYRIGHT 2
+#define FONTBOUNDINGBOX 3
+#define FONT_ASCENT 4
+#define FONT_DESCENT 5
+#define FONT_VERSION 6
+#define CHARS 7
+#define STARTCHAR 8
+#define ENCODING 9
+#define BITMAP 10
+#define ENDCHAR 11
+
+int parseLine(char *);
+
+#endif /* PARSE_H */