extract_ttc 0.3.6 → 0.3.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (40) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +0 -4
  3. data/.rubocop.yml +7 -9
  4. data/.rubocop_todo.yml +135 -0
  5. data/Gemfile +6 -6
  6. data/README.adoc +856 -55
  7. data/Rakefile +7 -101
  8. data/exe/extract_ttc +7 -0
  9. data/extract_ttc.gemspec +3 -4
  10. data/lib/extract_ttc/cli.rb +47 -0
  11. data/lib/extract_ttc/commands/extract.rb +88 -0
  12. data/lib/extract_ttc/commands/info.rb +112 -0
  13. data/lib/extract_ttc/commands/list.rb +60 -0
  14. data/lib/extract_ttc/configuration.rb +126 -0
  15. data/lib/extract_ttc/constants.rb +42 -0
  16. data/lib/extract_ttc/models/extraction_result.rb +56 -0
  17. data/lib/extract_ttc/models/validation_result.rb +53 -0
  18. data/lib/extract_ttc/true_type_collection.rb +79 -0
  19. data/lib/extract_ttc/true_type_font.rb +239 -0
  20. data/lib/extract_ttc/utilities/checksum_calculator.rb +89 -0
  21. data/lib/extract_ttc/utilities/output_path_generator.rb +100 -0
  22. data/lib/extract_ttc/version.rb +1 -1
  23. data/lib/extract_ttc.rb +83 -55
  24. data/sig/extract_ttc/configuration.rbs +19 -0
  25. data/sig/extract_ttc/constants.rbs +17 -0
  26. data/sig/extract_ttc/models/extraction_result.rbs +19 -0
  27. data/sig/extract_ttc/models/font_data.rbs +17 -0
  28. data/sig/extract_ttc/models/table_directory_entry.rbs +15 -0
  29. data/sig/extract_ttc/models/true_type_collection_header.rbs +15 -0
  30. data/sig/extract_ttc/models/true_type_font_offset_table.rbs +17 -0
  31. data/sig/extract_ttc/models/validation_result.rbs +17 -0
  32. data/sig/extract_ttc/utilities/checksum_calculator.rbs +13 -0
  33. data/sig/extract_ttc/utilities/output_path_generator.rbs +11 -0
  34. data/sig/extract_ttc/validators/true_type_collection_validator.rbs +9 -0
  35. data/sig/extract_ttc.rbs +20 -0
  36. metadata +44 -28
  37. data/ext/stripttc/LICENSE +0 -31
  38. data/ext/stripttc/dummy.c +0 -2
  39. data/ext/stripttc/extconf.rb +0 -5
  40. data/ext/stripttc/stripttc.c +0 -187
@@ -1,187 +0,0 @@
1
- #include <stdio.h>
2
- #include <stdlib.h>
3
- #include <string.h>
4
- #include <libgen.h>
5
-
6
- /* This program takes a ttc file and turns it into its component ttf files */
7
- /* This makes two changes to the data: */
8
- /* * The tables are placed at different offsets, therefore */
9
- /* the offset fields in the table header are also different. */
10
- /* * the 'head' table checksumAdjustment field is set correctly */
11
-
12
- #define CHR(ch1,ch2,ch3,ch4) (((ch1)<<24)|((ch2)<<16)|((ch3)<<8)|(ch4))
13
-
14
- #ifdef _WIN32
15
- #define EXPORT_API __declspec(dllexport)
16
- #else
17
- #define EXPORT_API
18
- #endif
19
-
20
- static void putshort(FILE *file,int sval) {
21
- putc((sval>>8)&0xff,file);
22
- putc(sval&0xff,file);
23
- }
24
-
25
- static void putlong(FILE *file,int val) {
26
- putc((val>>24)&0xff,file);
27
- putc((val>>16)&0xff,file);
28
- putc((val>>8)&0xff,file);
29
- putc(val&0xff,file);
30
- }
31
-
32
- static int getushort(FILE *ttf) {
33
- int ch1 = getc(ttf);
34
- int ch2 = getc(ttf);
35
- if ( ch2==EOF )
36
- return( EOF );
37
- return( (ch1<<8)|ch2 );
38
- }
39
-
40
- static int getlong(FILE *ttf) {
41
- int ch1 = getc(ttf);
42
- int ch2 = getc(ttf);
43
- int ch3 = getc(ttf);
44
- int ch4 = getc(ttf);
45
- if ( ch4==EOF )
46
- return( EOF );
47
- return( (ch1<<24)|(ch2<<16)|(ch3<<8)|ch4 );
48
- }
49
-
50
- static unsigned int filecheck(FILE *file) {
51
- unsigned int sum = 0, chunk;
52
-
53
- rewind(file);
54
- while ( 1 ) {
55
- chunk = getlong(file);
56
- if ( feof(file) || ferror(file))
57
- break;
58
- sum += chunk;
59
- }
60
- return( sum );
61
- }
62
-
63
- static void copytable(FILE *ttf,FILE *ttc,int offset,int length) {
64
- int i, ch;
65
-
66
- fseek(ttc,offset,SEEK_SET);
67
- for ( i=0; i<length && (ch=getc(ttc))!=EOF ; ++i )
68
- putc(ch,ttf);
69
- if ( ch==EOF )
70
- fprintf( stderr, "File ended before table\n" );
71
- if ( length&1 ) {
72
- putc('\0',ttf);
73
- ++length;
74
- }
75
- if ( length & 2 ) {
76
- putshort(ttf,0);
77
- }
78
- }
79
-
80
- static int handlefont(char *filename,int which,FILE *ttc,int offset) {
81
- char outfile[2000], *pt;
82
- FILE *ttf;
83
- int i, cnt, *offsets, *lengths, head, tag, pos, headpos;
84
- strcpy(outfile,basename(filename));
85
- pt = strrchr(outfile,'.');
86
- if ( pt==NULL )
87
- pt = outfile + strlen(outfile);
88
- sprintf( pt, "_%02d.ttf", which );
89
-
90
- ttf = fopen( outfile,"wb");
91
- if ( ttf==NULL ) {
92
- fprintf( stderr, "Failed to open %s for output.\n", outfile );
93
- return( -3 );
94
- }
95
- printf ( "%s ", outfile );
96
-
97
- fseek(ttc,offset,SEEK_SET);
98
- putlong(ttf,getlong(ttc)); /* sfnt version */
99
- putshort(ttf,cnt = getushort(ttc)); /* table cnt */
100
- putshort(ttf,getushort(ttc)); /* binary search header */
101
- putshort(ttf,getushort(ttc));
102
- putshort(ttf,getushort(ttc));
103
-
104
- offsets = malloc(cnt*sizeof(int));
105
- lengths = malloc(cnt*sizeof(int));
106
- head = -1;
107
- for ( i=0; i<cnt; ++i ) {
108
- tag = getlong(ttc);
109
- if ( tag==CHR('h','e','a','d'))
110
- head = i;
111
- putlong(ttf,tag);
112
- putlong(ttf,getlong(ttc)); /* checksum */
113
- putlong(ttf,offsets[i] = getlong(ttc)); /* Reserve space for offset, will fix later */
114
- putlong(ttf,lengths[i] = getlong(ttc));
115
- }
116
- headpos = -1;
117
- for ( i=0; i<cnt; ++i ) {
118
- pos = ftell(ttf);
119
- copytable(ttf,ttc,offsets[i],lengths[i]);
120
- if ( i==head ) {
121
- fseek(ttf,pos+8,SEEK_SET);
122
- putlong(ttf,0);
123
- headpos = pos;
124
- }
125
- fseek(ttf,12+i*16+8,SEEK_SET);
126
- putlong(ttf,pos); /* Fix offset here */
127
- fseek(ttf,0,SEEK_END);
128
- }
129
- if ( headpos!=-1 ) {
130
- unsigned int checksum;
131
- checksum = filecheck(ttf);
132
- checksum = 0xb1b0afba-checksum;
133
- fseek(ttf,headpos+2*sizeof(int),SEEK_SET);
134
- putlong(ttf,checksum);
135
- }
136
- fclose(ttf);
137
- free(offsets); free(lengths);
138
- return( 0 );
139
- }
140
-
141
- EXPORT_API int handlefile(char *filename) {
142
- FILE *ttc = fopen(filename,"rb");
143
- int version, cnt, e, i;
144
- int *offsets;
145
-
146
- if ( ttc==NULL ) {
147
- fprintf( stderr, "Could not open %s\n", filename );
148
- return( -1 );
149
- }
150
-
151
- version = getlong(ttc);
152
- if ( version!=CHR('t','t','c','f')) {
153
- fprintf( stderr, "%s does not look like a ttc file, bad version.\n", filename );
154
- fclose(ttc);
155
- return( -2 );
156
- }
157
-
158
- version = getlong(ttc);
159
- if ( version!=0x10000 && version != 0x20000 )
160
- fprintf( stderr, "Unexpected ttc version number: %08x\n", (unsigned int)(version) );
161
- cnt = getlong(ttc);
162
- offsets = malloc(cnt*sizeof(int));
163
- for ( i=0; i<cnt; ++i )
164
- offsets[i] = getlong(ttc);
165
- printf( "%s => ", filename );
166
- for ( i=0; i<cnt; ++i )
167
- if ( (e = handlefont(filename,i,ttc,offsets[i])) ) {
168
- fflush(stdout);
169
- fclose(ttc);
170
- free(offsets);
171
- return( e );
172
- };
173
- printf( "\n" );
174
- fflush(stdout);
175
- fclose(ttc);
176
- free(offsets);
177
- return( 0 );
178
- }
179
-
180
- int main(int argc, char *argv[]) {
181
- int e, i;
182
-
183
- for ( i=1; i<argc; ++i )
184
- if ( (e = handlefile(argv[i])) )
185
- return( e );
186
- return( 0 );
187
- }