bdf2sfd

BDF to SFD converter, allowing to vectorize bitmap fonts
Log | Files | Refs | README | LICENSE

commit bd0b152cc02fd42489ebab34549b52b07fd967f9
Author: Frederic Cambus <fred@statdns.com>
Date:   Tue,  3 Dec 2019 10:21:43 +0100

Initial commit.

Diffstat:
AAUTHORS | 5+++++
ACMakeLists.txt | 46++++++++++++++++++++++++++++++++++++++++++++++
ALICENSE | 24++++++++++++++++++++++++
Abdftosfd.1 | 58++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Acompat/compat.h | 24++++++++++++++++++++++++
Acompat/pledge.c | 6++++++
Acompat/pledge.h | 1+
Acompat/strtonum.c | 65+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Acompat/strtonum.h | 1+
Asrc/bdftosfd.c | 123+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Asrc/config.h | 22++++++++++++++++++++++
11 files changed, 375 insertions(+), 0 deletions(-)

diff --git a/AUTHORS b/AUTHORS @@ -0,0 +1,5 @@ +bdftosfd is developed by: + +Frederic Cambus <fred AT statdns DOT com> + +Site: https://www.cambus.net diff --git a/CMakeLists.txt b/CMakeLists.txt @@ -0,0 +1,46 @@ +# +# bdftosfd +# Copyright (c) 2019, Frederic Cambus +# https://github.com/fcambus/bdftosfd +# +# Created: 2019-11-21 +# Last Updated: 2019-12-03 +# +# bdftosfd is released under the BSD 2-Clause license +# See LICENSE file for details +# + +cmake_minimum_required (VERSION 2.6) + +project(bdftosfd C) + +include(CheckFunctionExists) +include(GNUInstallDirs) + +# Check if system has pledge and strtonum +list(APPEND CMAKE_REQUIRED_DEFINITIONS -D_OPENBSD_SOURCE) +check_function_exists(pledge HAVE_PLEDGE) +check_function_exists(strtonum HAVE_STRTONUM) + +# Additional include directories for compat functions + dependencies +include_directories("compat") + +set(CMAKE_BUILD_TYPE Release) +set(SRC src/bdftosfd.c) + +if(NOT HAVE_PLEDGE) + set (SRC ${SRC} compat/pledge.c) +endif() + +if(NOT HAVE_STRTONUM) + set (SRC ${SRC} compat/strtonum.c) +endif() + +add_definitions(-Wall -Wextra -std=c99 -pedantic) +add_executable(bdftosfd ${SRC}) + +install(TARGETS bdftosfd DESTINATION ${CMAKE_INSTALL_BINDIR}) +install(FILES bdftosfd.1 DESTINATION ${CMAKE_INSTALL_MANDIR}/man1/) + +enable_testing() +add_test(bdftosfd bdftosfd) diff --git a/LICENSE b/LICENSE @@ -0,0 +1,24 @@ +Copyright (c) 2019, Frederic Cambus +All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are met: + + * Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + + * 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. + +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 HOLDER 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. diff --git a/bdftosfd.1 b/bdftosfd.1 @@ -0,0 +1,58 @@ +.\" +.\" Copyright (c) 2019, Frederic Cambus +.\" All rights reserved. +.\" +.\" Redistribution and use in source and binary forms, with or without +.\" modification, are permitted provided that the following conditions are met: +.\" +.\" * Redistributions of source code must retain the above copyright +.\" notice, this list of conditions and the following disclaimer. +.\" +.\" * 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. +.\" +.\" 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 HOLDER 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. +.\" +.Dd $Mdocdate: December 3 2019 $ +.Dt BDFTOSFD 1 +.Os +.Sh NAME +.Nm bdftosfd +.Nd BDF to SFD converter +.Sh SYNOPSIS +.Nm +.Op Fl hv +.Ar file +.Sh DESCRIPTION +.Nm +is a BDF to SFD converter. +.Pp +If +.Ar file +is a single dash (`-'), +.Nm +reads from the standard input. +.Pp +The options are as follows: +.Bl -tag -width Ds +.It Fl h +Display usage. +.It Fl v +Display version. +.El + +.Sh AUTHORS +.Nm +was written by +.An Frederic Cambus . diff --git a/compat/compat.h b/compat/compat.h @@ -0,0 +1,24 @@ +#ifndef COMPAT_H +#define COMPAT_H + +#ifndef HAVE_PLEDGE +#include "pledge.h" +#endif + +#ifndef HAVE_STRTONUM +#include "strtonum.h" +#endif + +#ifndef timespecsub +#define timespecsub(tsp, usp, vsp) \ + do { \ + (vsp)->tv_sec = (tsp)->tv_sec - (usp)->tv_sec; \ + (vsp)->tv_nsec = (tsp)->tv_nsec - (usp)->tv_nsec; \ + if ((vsp)->tv_nsec < 0) { \ + (vsp)->tv_sec--; \ + (vsp)->tv_nsec += 1000000000L; \ + } \ + } while (0) +#endif + +#endif /* COMPAT_H */ diff --git a/compat/pledge.c b/compat/pledge.c @@ -0,0 +1,6 @@ +int +pledge(const char *promises, const char *execpromises) { + (void)promises; + (void)execpromises; + return 0; +} diff --git a/compat/pledge.h b/compat/pledge.h @@ -0,0 +1 @@ +int pledge(const char *, const char *); diff --git a/compat/strtonum.c b/compat/strtonum.c @@ -0,0 +1,65 @@ +/* $OpenBSD: strtonum.c,v 1.7 2013/04/17 18:40:58 tedu Exp $ */ + +/* + * Copyright (c) 2004 Ted Unangst and Todd Miller + * All rights reserved. + * + * Permission to use, copy, modify, and distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + */ + +#include <errno.h> +#include <limits.h> +#include <stdlib.h> + +#define INVALID 1 +#define TOOSMALL 2 +#define TOOLARGE 3 + +long long +strtonum(const char *numstr, long long minval, long long maxval, + const char **errstrp) +{ + long long ll = 0; + int error = 0; + char *ep; + struct errval { + const char *errstr; + int err; + } ev[4] = { + { NULL, 0 }, + { "invalid", EINVAL }, + { "too small", ERANGE }, + { "too large", ERANGE }, + }; + + ev[0].err = errno; + errno = 0; + if (minval > maxval) { + error = INVALID; + } else { + ll = strtoll(numstr, &ep, 10); + if (numstr == ep || *ep != '\0') + error = INVALID; + else if ((ll == LLONG_MIN && errno == ERANGE) || ll < minval) + error = TOOSMALL; + else if ((ll == LLONG_MAX && errno == ERANGE) || ll > maxval) + error = TOOLARGE; + } + if (errstrp != NULL) + *errstrp = ev[error].errstr; + errno = ev[error].err; + if (error) + ll = 0; + + return (ll); +} diff --git a/compat/strtonum.h b/compat/strtonum.h @@ -0,0 +1 @@ +long long strtonum(const char *, long long, long long, const char **); diff --git a/src/bdftosfd.c b/src/bdftosfd.c @@ -0,0 +1,123 @@ +/* + * bdftosfd + * Copyright (c) 2019, Frederic Cambus + * https://github.com/fcambus/bdftosfd + * + * Created: 2019-11-21 + * Last Updated: 2019-12-03 + * + * bdftosfd is released under the BSD 2-Clause license + * See LICENSE file for details + */ + +#define _XOPEN_SOURCE 600 +#define _POSIX_C_SOURCE 199309L +#define _POSIX_SOURCE + +#include <sys/stat.h> +#include <sys/time.h> +#include <sys/types.h> +#include <err.h> +#include <getopt.h> +#include <inttypes.h> +#include <stdbool.h> +#include <stdlib.h> +#include <stdint.h> +#include <stdio.h> +#include <string.h> +#include <time.h> + +#include "compat.h" +#include "config.h" + +struct timespec begin, end, elapsed; + +char lineBuffer[LINE_LENGTH_MAX]; + +FILE *bdfFile; +struct stat bdfFileStat; + +const char *errstr; + +int8_t getoptFlag; + +char *intputFile; + +uint64_t lines; +double runtime; + +void +displayUsage() { + printf("USAGE: bdftosfd [options] inputfile\n\n" \ + "Options are:\n\n" \ + " -h Display usage\n" \ + " -v Display version\n"); +} + +int +main(int argc, char *argv[]) { + if (pledge("stdio rpath", NULL) == -1) { + err(EXIT_FAILURE, "pledge"); + } + + while ((getoptFlag = getopt(argc, argv, "d:ghv")) != -1) { + switch (getoptFlag) { + case 'h': + displayUsage(); + return EXIT_SUCCESS; + + case 'v': + printf("%s\n", VERSION); + return EXIT_SUCCESS; + } + } + + if (optind < argc) { + intputFile = argv[optind]; + } else { + displayUsage(); + return EXIT_SUCCESS; + } + + argc -= optind; + argv += optind; + + /* Starting timer */ + clock_gettime(CLOCK_MONOTONIC, &begin); + + /* Open BDF file */ + if (!strcmp(intputFile, "-")) { + /* Read from standard input */ + bdfFile = stdin; + } else { + /* Attempt to read from file */ + if (!(bdfFile = fopen(intputFile, "r"))) { + perror("Can't open BDF file"); + return EXIT_FAILURE; + } + } + + /* Get BDF file size */ + if (fstat(fileno(bdfFile), &bdfFileStat)) { + perror("Can't stat BDF file"); + return EXIT_FAILURE; + } + + while (fgets(lineBuffer, LINE_LENGTH_MAX, bdfFile)) { + lines++; + } + + /* Stopping timer */ + clock_gettime(CLOCK_MONOTONIC, &end); + + timespecsub(&end, &begin, &elapsed); + runtime = elapsed.tv_sec + elapsed.tv_nsec / 1E9; + + /* Printing results */ + fprintf(stderr, "Processed %" PRIu64 " lines in %f seconds.\n", lines, runtime); + + /* Clean up */ + fclose(bdfFile); + + return EXIT_SUCCESS; +} diff --git a/src/config.h b/src/config.h @@ -0,0 +1,22 @@ +/* + * bdftosfd + * Copyright (c) 2019, Frederic Cambus + * https://github.com/fcambus/bdftosfd + * + * Created: 2019-11-21 + * Last Updated: 2019-12-03 + * + * bdftosfd is released under the BSD 2-Clause license + * See LICENSE file for details + */ + +#ifndef CONFIG_H +#define CONFIG_H + +#define VERSION "bdftosfd 1.0.0" + +enum { + LINE_LENGTH_MAX = 65536 +}; + +#endif /* CONFIG_H */