Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[sfntdiff] Support diff'ing different font formats
Browse files Browse the repository at this point in the history
Fixes #626
miguelsousa committed Mar 15, 2019
1 parent 112b6aa commit e3f5b15
Showing 9 changed files with 73 additions and 80 deletions.
25 changes: 23 additions & 2 deletions c/sfntdiff/source/Dfile.c
Original file line number Diff line number Diff line change
@@ -5,16 +5,17 @@
/*
* Machine independant file operations. The ANSI standard buffered i/o library
* can't be used here because the Macintosh version doesn't support resource
* reading.
* reading.
*
* This code assumes that 32 bits is large enough to hold a file offset. This
* seems a safe assumtion since other code would break long before
* fonts/resource files reached this size.
* fonts/resource files reached this size.
*/

#include <stdio.h>
#include <stdarg.h>
#include <string.h>
#include <stdbool.h>

#include "Dglobal.h"
#include "Dfile.h"
@@ -250,3 +251,23 @@ Card32 fileSniff(Card8 which) {
fileSeekAbsNotBuffered(which, 0);
return value;
}

bool isSupportedFontFormat(Card32 value, Byte8 *fname) {
switch (value) {
case bits_:
case typ1_:
case true_:
case OTTO_:
case VERSION(1, 0):
return true;
break;
case 256:
case ttcf_:
warning("unsupported file [%s] (ignored)\n", fname);
return false;
break;
default:
warning("unsupported/bad file [%s] (ignored)\n", fname);
return false;
}
}
2 changes: 2 additions & 0 deletions c/sfntdiff/source/Dfile.h
Original file line number Diff line number Diff line change
@@ -6,6 +6,7 @@
* File input support.
*/

#include <stdbool.h>
#ifndef FILE_H
#define FILE_H

@@ -20,6 +21,7 @@ extern void fileReadBytes(Card8 which, Int32 count, Card8 *buf);
extern void fileReadObject(Card8 which, IntX size, ...);
extern Byte8 *fileName(Card8 which);
extern Card32 fileSniff(Card8 which);
extern bool isSupportedFontFormat(Card32 value, Byte8 *fname);

/* Convienience macros */
#define TELL(which) fileTell((which))
97 changes: 21 additions & 76 deletions c/sfntdiff/source/Dmain.c
Original file line number Diff line number Diff line change
@@ -10,8 +10,9 @@
#include "Dsys.h"
#include <ctype.h>
#include <string.h>
#include <stdbool.h>

Byte8 *version = "2.21217"; /* Program version */
Byte8 *version = "3.0.0"; /* Program version */

static char *sourcepath = "";
#include "setjmp.h"
@@ -100,7 +101,7 @@ IntN main(IntN argc, Byte8 *argv[]) {

volatile IntX i = 0;
IntN argi;
Card32 value, value2;
bool supported, supported2;
Byte8 *filename1;
Byte8 *filename2;
IntN name1isDir, name2isDir;
@@ -151,34 +152,16 @@ IntN main(IntN argc, Byte8 *argv[]) {
}

/* See if we can recognize the file type */
value = fileSniff(1);
value2 = fileSniff(2);
if (value != value2) {
supported = isSupportedFontFormat(fileSniff(1), filename1);
supported2 = isSupportedFontFormat(fileSniff(2), filename2);

if (!supported || !supported2) {
fileClose(1);
fileClose(2);
fatal("file1 [%s] is not the same type as file2 [%s]\n", filename1, filename2);
quit(1);
}

switch (value) {
case bits_:
case typ1_:
case true_:
case OTTO_:
case VERSION(1, 0):
sfntRead(0, -1, 0, -1); /* Read plain sfnt file */
break;
case ttcf_:
warning("unsupported file [%s] (ignored)\n", filename1);
fileClose(1);
fileClose(2);
quit(1);
break;
default:
warning("unsupported/bad file [%s] (ignored)\n", filename1);
fileClose(1);
fileClose(2);
quit(1);
}
sfntRead(0, -1, 0, -1); /* Read plain sfnt file */
sfntDump();
sfntFree();
fileClose(1);
@@ -211,35 +194,16 @@ IntN main(IntN argc, Byte8 *argv[]) {
}

/* See if we can recognize the file type */
value = fileSniff(1);
value2 = fileSniff(2);
if (value != value2) {
supported = isSupportedFontFormat(fileSniff(1), fil1);
supported2 = isSupportedFontFormat(fileSniff(2), fil2);

if (!supported || !supported2) {
fileClose(1);
fileClose(2);
fatal("file1 [%s] is not the same type as file2 [%s]\n", fil1, fil2);
continue;
}

switch (value) {
case bits_:
case typ1_:
case true_:
case OTTO_:
case VERSION(1, 0):
sfntRead(0, -1, 0, -1); /* Read plain sfnt file */
break;
case 256:
case ttcf_:
warning("unsupported file [%s] (ignored)\n", fil1);
fileClose(1);
fileClose(2);
quit(1);
break;
default:
warning("unsupported/bad file [%s] (ignored)\n", fil1);
fileClose(1);
fileClose(2);
continue;
}
sfntRead(0, -1, 0, -1); /* Read plain sfnt file */
sfntDump();
sfntFree();
fileClose(1);
@@ -269,35 +233,16 @@ IntN main(IntN argc, Byte8 *argv[]) {
}

/* See if we can recognize the file type */
value = fileSniff(1);
value2 = fileSniff(2);
if (value != value2) {
supported = isSupportedFontFormat(fileSniff(1), filename1);
supported2 = isSupportedFontFormat(fileSniff(2), fil2);

if (!supported || !supported2) {
fileClose(1);
fileClose(2);
fatal("file1 [%s] is not the same type as file2 [%s]\n", filename1, fil2);
quit(1);
}

switch (value) {
case bits_:
case typ1_:
case true_:
case OTTO_:
case VERSION(1, 0):
sfntRead(0, -1, 0, -1); /* Read plain sfnt file */
break;
case 256:
case ttcf_:
warning("unsupported file [%s] (ignored)\n", filename1);
fileClose(1);
fileClose(2);
quit(1);
break;
default:
warning("unsupported/bad file [%s] (ignored)\n", filename1);
fileClose(1);
fileClose(2);
quit(1);
}
sfntRead(0, -1, 0, -1); /* Read plain sfnt file */
sfntDump();
sfntFree();
fileClose(1);
8 changes: 8 additions & 0 deletions tests/sfntdiff_data/expected_output/bug626.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
Thu Mar 14 02:59:39 2019
sfntdiff (2.21217) (-d 0) files:
< SourceSerifPro-It.otf
> SourceSerifPro-It.ttf
'sfnt' differs.
'head' differs.
'maxp' differs.
'post' differs.
Binary file added tests/sfntdiff_data/input/SourceSerifPro-It.otf
Binary file not shown.
Binary file added tests/sfntdiff_data/input/SourceSerifPro-It.ttf
Binary file not shown.
Empty file.
Empty file.
21 changes: 19 additions & 2 deletions tests/sfntdiff_test.py
Original file line number Diff line number Diff line change
@@ -8,6 +8,7 @@
from test_utils import get_expected_path

TOOL = 'sfntdiff'
CMD = ['-t', TOOL]


# -----
@@ -41,7 +42,23 @@ def test_exit_unknown_option(arg):
def test_diff(args, txt_filename):
if args:
args.insert(0, '-o')
actual_path = runner(
['-t', TOOL, '-s', '-f', 'regular.otf', 'bold.otf'] + args)
actual_path = runner(CMD + ['-s', '-f', 'regular.otf', 'bold.otf'] + args)
expected_path = get_expected_path(txt_filename)
assert differ([expected_path, actual_path, '-l', '1-4'])


def test_diff_otf_vs_ttf_bug626():
actual_path = runner(CMD + ['-s', '-f', 'SourceSerifPro-It.otf',
'SourceSerifPro-It.ttf'])
expected_path = get_expected_path('bug626.txt')
assert differ([expected_path, actual_path, '-l', '1-4'])


def test_not_font_files():
stderr_path = runner(CMD + ['-s', '-e', '-f', 'not_a_font_1.otf',
'not_a_font_2.otf'])
with open(stderr_path, 'rb') as f:
output = f.read()
assert b'[WARNING]: unsupported/bad file' in output
assert b'not_a_font_1.otf] (ignored)' in output
assert b'not_a_font_2.otf] (ignored)' in output

0 comments on commit e3f5b15

Please sign in to comment.