ref: 5877598b3b65ae59ec9d1447e222af4b1e848911
dir: /unix/test.c/
#include <err.h>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "otf.h"
static void
dumpmap(FILE *out, GlyfImage *im, int n)
{
#define gap 1
int total, mid, npix, bw, bh, x, y, i, maxh, d;
u8int *b;
total = 0;
maxh = 0;
for(i = 0; i < n; i++){
if(maxh < im[i].h)
maxh = im[i].h;
total += im[i].w;
}
mid = total / n;
npix = (mid+gap)*(maxh+gap)*n;
bh = sqrt(npix);
bw = npix/bh;
bh *= 1.5;
npix *= 1.5;
npix += bw*gap;
if((b = malloc(npix)) == NULL)
err(1, NULL);
memset(b, 0xff, npix);
x = y = gap;
for(i = 0; i < n; i++){
if((bw-x) < im[i].w+gap){
y += maxh + gap;
x = gap;
}
if((bh-y) < maxh+gap)
continue;
for(d = 0; d < im[i].h; d++)
memcpy(b + (maxh - im[i].h + y + im[i].baseline + d)*bw + x, im[i].b + d*im[i].w, im[i].w);
x += im[i].w + gap;
}
fprintf(out, "%11s %11d %11d %11d %11d ", "k8", 0, 0, bw, y+maxh+gap);
fwrite(b, 1, bw*(y+maxh+gap), out);
free(b);
}
static char *argv0;
#define ARGBEGIN for((argv0? 0: (argv0=*argv)),argv++,argc--;\
argv[0] && argv[0][0]=='-' && argv[0][1];\
argc--, argv++) {\
char *_args, *_argt;\
int _argc;\
_args = &argv[0][1];\
if(_args[0]=='-' && _args[1]==0){\
argc--; argv++; break;\
}\
_argc = 0;\
while((_argc = *_args++) != 0)\
switch(_argc)
#define ARGEND (void)_argt; (void)_argc; (void)_args;}(void)argv; (void)argc;
#define ARGF() (_argt=_args, _args="",\
(*_argt? _argt: argv[1]? (argc--, *++argv): 0))
#define ARGC() _argc
#define EARGF(x) (_argt=_args, _args="",\
(*_argt? _argt: argv[1]? (argc--, *++argv): (x, (char*)0)))
static int
otfseek(void *aux, int off, int whence)
{
if(fseek(aux, off, whence) < 0)
return -1;
return ftell(aux);
}
static int
otfread(void *aux, void *dst, int sz)
{
return fread(dst, 1, sz, aux);
}
static void
usage(void)
{
fprintf(stderr, "usage: %s [-g GLYPH_ID] [-G] [-i N] [-s PX] font.otf ...\n", argv0);
fprintf(stderr, " -g: specifies a single glyph id\n");
fprintf(stderr, " -G: print out glyph ids, only ones that can be drawn atm (no compound yet)\n");
fprintf(stderr, " -p: draw (of size in pixels per em) and write the image to stdout\n");
exit(1);
}
int
main(int argc, char **argv)
{
int i, gi, G, ppem;
Otfile in, out;
Otf *o;
gi = -1;
G = 0;
ppem = 0;
ARGBEGIN{
case 'g':
gi = strtol(EARGF(usage()), NULL, 0);
break;
case 'G':
G++;
break;
case 'p':
ppem = strtol(EARGF(usage()), NULL, 0);
break;
default:
usage();
}ARGEND
in.seek = otfseek;
in.read = otfread;
out.print = (void*)fprintf;
out.aux = fdopen(1, "wb");
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)
fprintf(out.aux, "%s\n", argv[i]);
if(G && gi < 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, im+i) != 0)
errx(1, "glyph %d: %s", i, otferrstr());
}else if(ppem <= 0){
fprintf(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.aux, im, n);
for(i = 0; i < n; i++)
free(im[i].b);
free(im);
}
}else if(gi < 0){
otfprint(o, &out, indentΔ);
}else{
int n = otfglyfnum(o);
if(gi >= n)
errx(1, "glyph %d out of range, max %d", gi, n-1);
Glyf *g = otfglyf(o, gi);
if(g == NULL){
errx(1, "glyph %d: %s", gi, otferrstr());
}else if(ppem > 0){
if(g->component != NULL)
errx(1, "glyph %d: component", gi);
else{
GlyfImage im;
if(otfdrawglyf(o, g, ppem, &im) != 0)
errx(1, "glyph %d: %s", gi, otferrstr());
fprintf(out.aux, "%11s %11d %11d %11d %11d ", "k8", 0, 0, im.w, im.h);
fwrite(im.b, 1, im.w*im.h, out.aux);
free(im.b);
}
}else{
fprintf(out.aux, "\n%d:\n", gi);
print_Glyf(&out, indentΔ, o, g);
}
}
otfclose(o);
fclose(in.aux);
}
fclose(out.aux);
return 0;
}