Skip to content

Commit

Permalink
Ver. 1.2 initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
doio committed Oct 28, 2017
1 parent 2dea5fb commit 291a1cd
Show file tree
Hide file tree
Showing 37 changed files with 6,574 additions and 0 deletions.
84 changes: 84 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
# gmake

NAME = ttf2woff
VERSION = 1.2
BINDIR = /usr/local/bin
PKG=$(NAME)-$(VERSION)
FILES_TTF2WOFF := Makefile ttf2woff.c ttf2woff.h genwoff.c genttf.c readttf.c readttc.c readwoff.c readwoff2.c \
optimize.c comp-zlib.c comp-zopfli.c compat.c ttf2woff.rc zopfli.diff
FILES_ZOPFLI := zopfli.h symbols.h \
$(patsubst %,%.h,zlib_container deflate lz77 blocksplitter squeeze hash cache tree util katajainen) \
$(patsubst %,%.c,zlib_container deflate lz77 blocksplitter squeeze hash cache tree util katajainen)
FILES += $(FILES_TTF2WOFF) $(addprefix zopfli/,$(FILES_ZOPFLI))

#WOFF2 = 1
ZOPFLI = 1

OBJ := ttf2woff.o readttf.o readttc.o readwoff.o genwoff.o genttf.o optimize.o
ifeq ($(ZOPFLI),)
OBJ += comp-zlib.o
else
OBJ += comp-zopfli.o
LDFLAGS += -lm
endif

CFLAGS ?= -O2 -g
LDFLAGS += -lz

ifneq ($(WOFF2),)
OBJ += readwoff2.o
LDFLAGS += -lbrotlidec
CFLAGS += -DREAD_WOFF2
endif

# eg. make WIN32=1 CC=mingw32-gcc RC=mingw32-windres
ifdef WIN32
EXE = .exe
CFLAGS += -DNO_ERRWARN
OBJ += compat.o rc.o
endif

OBJDIR =
OBJ := $(addprefix $(OBJDIR),$(OBJ))
ifneq ($(OBJDIR),)
CFLAGS += -o $@
endif

ttf2woff$(EXE): $(OBJ)
$(CC) -o $@ $(OBJ) $(LDFLAGS)

$(OBJDIR)ttf2woff.o: ttf2woff.c ttf2woff.h Makefile
$(CC) $(CFLAGS) -DVERSION=$(VERSION) -c ttf2woff.c

$(OBJDIR)comp-zopfli.o: comp-zopfli.c ttf2woff.h $(addprefix zopfli/,$(FILES_ZOPFLI))
$(CC) $(CFLAGS) -c comp-zopfli.c

$(OBJDIR)rc.o: ttf2woff.rc Makefile
$(RC) $(DEF) -DVERNUMS=`echo $(VERSION) | sed 's/\\./,/g; s/[^0-9,]//g'` -DVERSION=$(VERSION) -o $@ ttf2woff.rc

$(OBJDIR)%.o : %.c
$(CC) $(CPPFLAGS) $(CFLAGS) -c $^

install: ttf2woff
install -s $< $(BINDIR)

clean:
rm -f ttf2woff $(addsuffix .o,$(basename $(filter %.c,$(FILES_TTF2WOFF))))

dist:
ln -s . $(PKG)
tar czf $(PKG).tar.gz --group=root --owner=root $(addprefix $(PKG)/, $(FILES)); \
rm $(PKG)

.PHONY: install clean dist zopfli zopfli.diff


# git://github.com/google/zopfli.git
ZOPFLI_SRC = zopfli-src
zopfli: $(addprefix $(ZOPFLI_SRC)/src/zopfli/,$(FILES_ZOPFLI))
@install -d zopfli
cp -pf $^ zopfli
patch -p3 -dzopfli <zopfli.diff

zopfli.diff:
diff -u --minimal $(ZOPFLI_SRC)/src/zopfli zopfli >$@; true
34 changes: 34 additions & 0 deletions comp-zlib.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
* Copyright (C) 2013 Jan Bobrowski <jb@wizard.ae.krakow.pl>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* version 2 as published by the Free Software Foundation.
*/

#include <stdlib.h>
#include <zlib.h>
#include "ttf2woff.h"

char *copression_by = "zlib";

int zlib_compress(struct buf *out, struct buf *inp)
{
u8 *b;
int v;
uLongf len;

len = inp->len;
b = my_alloc(inp->len);

v = compress2(b,&len, inp->ptr,inp->len, 9);

if(v==Z_OK && REALLY_SMALLER(len, inp->len)) {
out->ptr = b;
out->len = len;
return 1;
} else {
my_free(b);
return 0;
}
}
56 changes: 56 additions & 0 deletions comp-zopfli.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
* Copyright (C) 2013-2017 Jan Bobrowski <jb@wizard.ae.krakow.pl>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* version 2 as published by the Free Software Foundation.
*/

#include <stdlib.h>
#include "ttf2woff.h"

#include "zopfli/zlib_container.c"
#include "zopfli/deflate.c"
#include "zopfli/lz77.c"
#include "zopfli/blocksplitter.c"
#include "zopfli/squeeze.c"
#include "zopfli/hash.c"
#include "zopfli/cache.c"
#include "zopfli/tree.c"
#include "zopfli/util.c"
#include "zopfli/katajainen.c"

#define adler32 zlib_adler32
#include <zlib.h>

char *copression_by = "zopfli";

int zlib_compress(struct buf *out, struct buf *inp)
{
ZopfliOptions opt = {0};
u8 *b = 0;
size_t sz = 0;

opt.numiterations = 15;
ZopfliZlibCompress(&opt, inp->ptr, inp->len, &b, &sz);

if(REALLY_SMALLER(sz, inp->len)) {

#if 1
/* Trust, but verify */
uLong tmpl = inp->len;
Bytef *tmpb = my_alloc(inp->len);
int v = uncompress(tmpb, &tmpl, b, sz);
if(v!=Z_OK || tmpl!=inp->len)
errx(3,"Zopfli error");
my_free(tmpb);
#endif

out->ptr = b;
out->len = sz;
return 1;
} else {
free(b);
return 0;
}
}
43 changes: 43 additions & 0 deletions compat.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <stdarg.h>

static void er(int s, int e, char *f, va_list *va)
{
// fprintf(stderr, "%s: ", getexecname());
if(f) vfprintf(stderr, f, *va);
va_end(*va);
if(e >= 0) fprintf(stderr, ": %s", strerror(e));
putc('\n', stderr);
if(s >= 0) exit(s);
}

void err(int s, char *f, ...)
{
va_list va;
va_start(va, f);
er(s, errno, f, &va);
}

void errx(int s, char *f, ...)
{
va_list va;
va_start(va, f);
er(s, -1, f, &va);
}

void warn(char *f, ...)
{
va_list va;
va_start(va, f);
er(-1, errno, f, &va);
}

void warnx(char *f, ...)
{
va_list va;
va_start(va, f);
er(-1, -1, f, &va);
}
63 changes: 63 additions & 0 deletions genttf.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/*
* Copyright (C) 2013 Jan Bobrowski <jb@wizard.ae.krakow.pl>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* version 2 as published by the Free Software Foundation.
*/

#include <stdlib.h>
#include <string.h>
#include <strings.h>
#include <assert.h>
#include "ttf2woff.h"

u8 *put_ttf_header(u8 buf[12], struct ttf *ttf)
{
u8 *p = buf;
int n = ttf->ntables;
p = p32(p, ttf->flavor);
p = p16(p, n);
while(n & n-1) n &= n-1;
p = p16(p, n<<4);
p = p16(p, ffs(n)-1);
p = p16(p, ttf->ntables-n << 4);
return p;
}

void gen_ttf(struct buf *out, struct ttf *ttf)
{
unsigned sfnt_size;
u8 *buf, *p;
int i;

sfnt_size = 12 + 16*ttf->ntables;
for(i=0; i<ttf->ntables; i++) {
struct table *t = ttf->tab_pos[i];
t->pos = sfnt_size; // remember offset in output file
sfnt_size += t->buf.len+3 & ~3;
}

buf = my_alloc(sfnt_size);
p = put_ttf_header(buf, ttf);

for(i=0; i<ttf->ntables; i++) {
struct table *t = &ttf->tables[i];
p = p32(p, t->tag);
p = p32(p, t->csum);
p = p32(p, t->pos);
p = p32(p, t->buf.len);
}

for(i=0; i<ttf->ntables; i++) {
struct table *t = ttf->tab_pos[i];
unsigned sz = t->buf.len;
p = append(p, t->buf.ptr, sz);
while(sz&3) *p++=0, sz++;
}

assert(p == buf+sfnt_size);

out->ptr = buf;
out->len = sfnt_size;
}
95 changes: 95 additions & 0 deletions genwoff.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
/*
* Copyright (C) 2013 Jan Bobrowski <jb@wizard.ae.krakow.pl>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* version 2 as published by the Free Software Foundation.
*/

#include <string.h>
#include <stdlib.h>
#include <assert.h>
#include "ttf2woff.h"

#define MIN_COMPR 16

void gen_woff(struct buf *out, struct ttf *ttf)
{
unsigned woff_size, sfnt_size;
struct buf meta_comp={0};
u32 meta_off, priv_off;
u8 *buf, *p;
int i;

woff_size = 44 + 20*ttf->ntables;
sfnt_size = 12 + 16*ttf->ntables;
for(i=0; i<ttf->ntables; i++) {
struct table *t = ttf->tab_pos[i];
t->pos = woff_size; // remember offset in output file
t->zbuf = t->buf;
if(t->buf.len >= MIN_COMPR)
zlib_compress(&t->zbuf, &t->buf);
sfnt_size += t->buf.len+3 & ~3;
woff_size += t->zbuf.len+3 & ~3;
}

meta_off = 0;
if(ttf->woff_meta.len >= MIN_COMPR) {
meta_comp = ttf->woff_meta;
zlib_compress(&meta_comp, &ttf->woff_meta);
meta_off = woff_size;
woff_size += meta_comp.len;
}

priv_off = 0;
if(ttf->woff_priv.len) {
priv_off = woff_size;
woff_size += ttf->woff_priv.len;
}

buf = my_alloc(woff_size);

p32(buf, 0x774F4646);
p32(buf+4, ttf->flavor);
p32(buf+8, woff_size);
p16(buf+12, ttf->ntables);
p16(buf+14, 0);
p32(buf+16, sfnt_size);
p32(buf+20, 0); // version ?
p32(buf+24, meta_off);
p32(buf+28, meta_comp.len); // meta len
p32(buf+32, ttf->woff_meta.len); // meta orig len
p32(buf+36, priv_off);
p32(buf+40, ttf->woff_priv.len);

p = buf + 44;
for(i=0; i<ttf->ntables; i++) {
struct table *t = &ttf->tables[i];
p32(p, t->tag);
p32(p+4, t->pos);
p32(p+8, t->zbuf.len);
p32(p+12, t->buf.len);
p32(p+16, t->csum);
p += 20;
}

for(i=0; i<ttf->ntables; i++) {
struct table *t = ttf->tab_pos[i];
u32 sz = t->zbuf.len;
p = append(p, t->zbuf.ptr, sz);
while(sz&3) *p++=0, sz++;
// if(t->zbuf.ptr != t->buf.ptr)
// my_free(t->zbuf.ptr);
}

if(meta_comp.len)
p = append(p, meta_comp.ptr, meta_comp.len);

if(ttf->woff_priv.len)
p = append(p, ttf->woff_priv.ptr, ttf->woff_priv.len);

assert(p == buf+woff_size);

out->ptr = buf;
out->len = woff_size;
}

0 comments on commit 291a1cd

Please sign in to comment.