ref: 9bfaf452c97c5bcbedd275e9f5a111639e54c3a1
parent: 0d17a04bbcbb6ee26415d356f40e75d891c5f016
author: Sigrid Solveig Haflínudóttir <sigrid@ftrv.se>
date: Mon Jul 15 22:11:03 EDT 2024
combine more testing logic together into one file
--- a/plan9/test.c
+++ b/plan9/test.c
@@ -32,9 +32,7 @@
void
main(int argc, char **argv)
{
- int i;
Otfile in, out;
- Otf *o;
in.seek = otfseek;
in.read = otfread;
@@ -43,62 +41,8 @@
out.aux = Bfdopen(1, OWRITE);
parseoptions();
- for(i = 0; i < argc; i++){
- if((in.aux = Bopen(argv[i], OREAD)) == nil || (o = otfopen(&in)) == nil){
- fprint(2, "%r\n");
- continue;
- }
- if(ppem <= 0)
- Bprint(out.aux, "%s\n", argv[i]);
- if(map > 0 && gind < 0){
- int i, n = otfglyfnum(o);
- GlyfImage *im = ppem > 0 ? calloc(n, sizeof(*im)) : nil;
- for(i = 0; i < n; i++){
- Glyf *g = otfglyf(o, i);
- if(g == nil)
- sysfatal("glyf %d: %r", i);
- if(ppem > 0 && g->numberOfContours != 0){
- if(otfdrawglyf(o, g, ppem, gap, &im[i]) != 0)
- sysfatal("glyf %d: %r", i);
- }else if(ppem <= 0){
- Bprint(out.aux, "%d (%s):\n", i,
- g->simple ? "simple" : (g->component ? "component" : "empty"));
- print_Glyf(&out, indentΔ, o, g);
- }
- free(g);
- }
- if(ppem > 0){
- dumpmap(&out, im, n);
- for(i = 0; i < n; i++)
- free(im[i].b);
- free(im);
- }else{
- fprint(2, "\n");
- }
- }else if(gind < 0){
- otfprint(o, &out, indentΔ);
- }else{
- int n = otfglyfnum(o);
- if(gind >= n)
- sysfatal("glyph %d out of range, max %d", gind, n-1);
- Glyf *g = otfglyf(o, gind);
- if(g == nil){
- fprint(2, "%d: %r\n", gind);
- }else if(ppem > 0){
- GlyfImage im;
- if(otfdrawglyf(o, g, ppem, gap, &im) != 0)
- sysfatal("%r");
- fprint(1, "%11s %11d %11d %11d %11d ", "k8", 0, 0, im.w, im.h);
- write(1, im.b, im.w*im.h);
- free(im.b);
- }else{
- Bprint(out.aux, "\n%d:\n", gind);
- print_Glyf(&out, indentΔ, o, g);
- }
- }
- otfclose(o);
- Bterm(in.aux);
- }
+ if((in.aux = Bopen(*argv, OREAD)) == nil || process(&in, &out) != 0)
+ sysfatal("%r");
Bterm(out.aux);
exits(nil);
--- a/test.h
+++ b/test.h
@@ -1,7 +1,7 @@
static void
printusage(Otfile *f)
{
- f->print(f->aux, "usage: %s [-i GLYPH_ID] [-p PPEM [-g PIXELS] [-m ... -[-H GLYPH_ID]]] font.otf ...\n", argv0);
+ f->print(f->aux, "usage: %s [-i GLYPH_ID] [-p PPEM [-g PIXELS] [-m ... -[-H GLYPH_ID]]] font.otf\n", argv0);
f->print(f->aux, " -i: specifies a single glyph id\n");
f->print(f->aux, " -p: draw (of size in pixels per em) and write the image to stdout\n");
f->print(f->aux, " -g: gap (in pixels) to add to every border of a glyph\n");
@@ -99,6 +99,68 @@
return 0;
}
+static int
+process(Otfile *in, Otfile *out)
+{
+ GlyfImage *im;
+ int i, n;
+ Glyf *g;
+ Otf *o;
+
+ if((o = otfopen(in)) == nil)
+ return -1;
+ n = otfglyfnum(o);
+ if(gind >= n){
+ werrstr("out of range (max %d)", n-1);
+ goto glypherr;
+ }
+ if(map && gind < 0){
+ im = ppem > 0 ? calloc(n, sizeof(*im)) : nil;
+ for(i = 0; i < n; i++){
+ if((g = otfglyf(o, i)) == nil){
+ gind = i;
+glypherr:
+ werrstr("glyph %d: %r", gind);
+ return -1;
+ }
+ if(ppem > 0 && g->numberOfContours != 0){
+ if(otfdrawglyf(o, g, ppem, gap, im+i) != 0)
+ goto glypherr;
+ }else if(ppem <= 0){
+ out->print(out->aux, "%d (%s):\n", i,
+ g->simple ? "simple" : (g->component ? "component" : "empty"));
+ print_Glyf(out, indentΔ, o, g);
+ }
+ free(g);
+ }
+ if(ppem > 0){
+ if(dumpmap(out, im, n) != 0)
+ return -1;
+ for(i = 0; i < n; i++)
+ free(im[i].b);
+ free(im);
+ }
+ }else if(gind < 0){
+ otfprint(o, out, indentΔ);
+ }else{
+ if((g = otfglyf(o, gind)) == nil){
+ goto glypherr;
+ }else if(ppem > 0){
+ GlyfImage im;
+ if(otfdrawglyf(o, g, ppem, gap, &im) != 0)
+ goto glypherr;
+ out->print(out->aux, "%11s %11d %11d %11d %11d ", "k8", 0, 0, im.w, im.h);
+ out->write(out->aux, im.b, im.w*im.h);
+ free(im.b);
+ }else{
+ out->print(out->aux, "\n%d:\n", gind);
+ print_Glyf(out, indentΔ, o, g);
+ }
+ }
+ otfclose(o);
+ return 0;
+}
+
#define parseoptions() \
ARGBEGIN{ \
case 'g': \
@@ -118,4 +180,6 @@
break; \
default: \
usage(&out); \
- }ARGEND
+ }ARGEND \
+ if(argc != 1) \
+ usage(&out);
--- a/unix/test.c
+++ b/unix/test.c
@@ -5,9 +5,8 @@
#include <stdlib.h>
#include <string.h>
#include "otf.h"
+#include "otfsys.h"
-#define nil NULL
-
static char *argv0;
#define ARGBEGIN for((argv0? 0: (argv0=*argv)),argv++,argc--;\
@@ -63,8 +62,6 @@
main(int argc, char **argv)
{
Otfile in, out;
- int i;
- Otf *o;
out.print = (void*)fprintf;
out.write = otfwrite;
@@ -73,61 +70,10 @@
in.read = otfread;
parseoptions();
- for(i = 0; i < argc; i++){
- if((in.aux = fopen(argv[i], "rb")) == NULL)
- err(1, "%s", argv[i]);
- if((o = otfopen(&in)) == NULL)
- errx(1, "%s: %s", argv[i], otferrstr());
- if(ppem <= 0)
- out.print(out.aux, "%s\n", argv[i]);
- if(map && gind < 0){
- int i, n = otfglyfnum(o);
- GlyfImage *im = ppem > 0 ? calloc(n, sizeof(*im)) : NULL;
- for(i = 0; i < n; i++){
- Glyf *g = otfglyf(o, i);
- if(g == NULL)
- errx(1, "glyph %d: %s", i, otferrstr());
- if(ppem > 0 && g->numberOfContours != 0){
- if(otfdrawglyf(o, g, ppem, gap, im+i) != 0)
- errx(1, "glyph %d: %s", i, otferrstr());
- }else if(ppem <= 0){
- out.print(out.aux, "%d (%s):\n", i,
- g->simple ? "simple" : (g->component ? "component" : "empty"));
- print_Glyf(&out, indentΔ, o, g);
- }
- free(g);
- }
- if(ppem > 0){
- if(dumpmap(&out, im, n) != 0)
- err(1, nil);
- for(i = 0; i < n; i++)
- free(im[i].b);
- free(im);
- }
- }else if(gind < 0){
- otfprint(o, &out, indentΔ);
- }else{
- int n = otfglyfnum(o);
- if(gind >= n)
- errx(1, "glyph %d out of range, max %d", gind, n-1);
- Glyf *g = otfglyf(o, gind);
- if(g == NULL){
- errx(1, "glyph %d: %s", gind, otferrstr());
- }else if(ppem > 0){
- GlyfImage im;
- if(otfdrawglyf(o, g, ppem, gap, &im) != 0)
- errx(1, "glyph %d: %s", gind, otferrstr());
- out.print(out.aux, "%11s %11d %11d %11d %11d ", "k8", 0, 0, im.w, im.h);
- out.write(out.aux, im.b, im.w*im.h);
- free(im.b);
- }else{
- out.print(out.aux, "\n%d:\n", gind);
- print_Glyf(&out, indentΔ, o, g);
- }
- }
- otfclose(o);
- fclose(in.aux);
- }
+ if((in.aux = fopen(*argv, "rb")) == NULL)
+ err(1, nil);
+ if(process(&in, &out) != 0)
+ errx(1, "%s", otferrstr());
fclose(out.aux);
return 0;