summaryrefslogtreecommitdiff
path: root/Build/source/libs/t1lib/t1lib-5.1.2/examples/subset.c
blob: a39c838a13ec44955074c17972454f1689d60f8a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
/*--------------------------------------------------------------------------
  ----- File:        subset.c 
  ----- Author:      Rainer Menzner (Rainer.Menzner@web.de)
  ----- Date:        2007-12-22
  ----- Description: This file is part of t1lib. The program subset reads
                     a font file name and a string from the commandline
		     and creates a subset of the font which is sufficient
		     to display the string. It is an example of how to use
		     font subsetting.
  ----- Copyright:   t1lib is copyrighted (c) Rainer Menzner, 1996-2007.
                     As of version 0.5, t1lib is distributed under the
		     GNU General Public Library Lincense. The
		     conditions can be found in the files LICENSE and
		     LGPL, which should reside in the toplevel
		     directory of the distribution.  Please note that 
		     there are parts of t1lib that are subject to
		     other licenses:
		     The parseAFM-package is copyrighted by Adobe Systems
		     Inc.
		     The type1 rasterizer is copyrighted by IBM and the
		     X11-consortium.
  ----- Warranties:  Of course, there's NO WARRANTY OF ANY KIND :-)
  ----- Credits:     I want to thank IBM and the X11-consortium for making
                     their rasterizer freely available.
		     Also thanks to Piet Tutelaers for his ps2pk, from
		     which I took the rasterizer sources in a format
		     independent from X11.
                     Thanks to all people who make free software living!
--------------------------------------------------------------------------*/


#include <stdio.h>
#include <string.h>
#include <stdlib.h>
/* Note: We include t1lib.h from lib/t1lib. That way the objectfile does only
   need to be rebuild when the header itself changes and not each time the
   library has been recompiled */
#include "../lib/t1lib/t1lib.h"


void printusage( void);


int main(int argc, char *argv[])
{

  int i, j, result;
  int realargs=0;
  int logfile=0;
  char mask[256];
  int flags=T1_SUBSET_DEFAULT;
  char *subset=NULL;
  unsigned long ofsize=0;
  FILE *ofp=stdout;
  
  char** encoding     = 0;
  char*  encodingfile = 0;
  int    doreencode   = 0;
  int    reresult     = 0;

  
  if (argc==1){
    printusage();
    return(0);
  }

  /* If we want to log anything, then log all */
  T1_SetLogLevel(T1LOG_DEBUG);

  realargs=argc-1;

  for (j=1; j<argc; j++) {
    if (argv[j][0]=='-') {
      if (strcmp( argv[j], "-l")==0) {
	realargs -=1;
	logfile=1;
      }
      else if (strcmp( argv[j], "-a")==0) {
	/* default case */
	realargs -=1;
      }
      else if (strcmp( argv[j], "-b")==0) {
	realargs -=1;
	flags |=T1_SUBSET_ENCRYPT_BINARY;
      }
      else if (strcmp( argv[j], "-f")==0) {
	realargs -=1;
	flags |=T1_SUBSET_FORCE_REENCODE;
      }
      else if (strcmp( argv[j], "-s")==0) {
	realargs -=1;
	flags |=T1_SUBSET_SKIP_REENCODE;
      }
      else if (strcmp( argv[j], "-e")==0) {
	--realargs;
	if ( j == argc-1 ) {
	  fprintf(stderr, "subset: Option \"-e\": Missing filename argument.\n");
	  printusage();
	  return 1;
	}
	++j;
	encodingfile=argv[j];
	doreencode=1;
	--realargs;
      }
      else {
	fprintf(stderr, "subset: Unknown option: %s\n", argv[j]);
	printusage();
	return( 1);
      }
    }
    else {
      i=j;  /* this is assumed to be the first font file name */
      break;
    }
  }
  
  
  if (realargs<2) {
    fprintf( stderr, "subset: Need at least one font file and one string\n");
    printusage();
    return( 1);
  }
  
  
  if (logfile!=0) {
    if ((T1_InitLib( LOGFILE | 
		     IGNORE_CONFIGFILE |
		     IGNORE_FONTDATABASE)==NULL)) {
      fprintf(stderr, "subset: Initialization of t1lib failed (T1_errno=%d)\n",
	      T1_errno);
      return(1);
    }
  }
  else {
    if ((T1_InitLib( NO_LOGFILE | 
		     IGNORE_CONFIGFILE |
		     IGNORE_FONTDATABASE)==NULL)){
      fprintf(stderr, "subset: Initialization of t1lib failed (T1_errno=%d)\n", T1_errno);
      return(1);
    }
  }

  /* Load encoding file if specified */
  if ( doreencode != 0 ) {
    if ( (encoding = T1_LoadEncoding( encodingfile)) == NULL ) {
      fprintf( stderr, "subset: Could not load Encoding File %s (T1_errno=%d, %s).\n",
	       encodingfile, T1_errno, T1_StrError(T1_errno));
      exit( 0);
    }
  }
  

  /* First, build font data base */
  for (i=j; i<argc-1; i++){
    if ((result=T1_AddFont( argv[i]))<0)
      fprintf( stderr, "subset: Could not load font file %s (T1_errno=%d)\n",
	       argv[i], T1_errno);
  }
  if (T1_GetNoFonts()<1){
    fprintf( stderr, "subset: Nothing to do\n");
    T1_CloseLib();
    return( 2);
  }
  
  /* second, setup subsetting mask from commandline string */
  for ( i=0; i<256; i++) {
    mask[i]=0;
  }
  for (i=0; i<strlen(argv[argc-1]); i++) {
    mask[(unsigned char)argv[argc-1][i]]=1;
  }
  
  /* third, load fonts, generate subset and write it stdout. */
  for( i=0; i<T1_GetNoFonts(); i++){
    fprintf( stderr, "Loading %s ... ", T1_GetFontFileName(i));
    fflush(stdout);
    if ((T1_LoadFont(i))){
      fprintf(stderr, "failed\n");
      continue;
    }
    else {
      fprintf(stderr, "finished\n");
    }

    /* Reencode font */
    if ( doreencode != 0 ) {
      if ( (reresult = T1_ReencodeFont( i, encoding)) != 0 ) {
	fprintf( stderr, "Warning Reencoding font %d failed (%d)!\n", i, reresult);
      }
    }
  
    fprintf( stderr, "Processing ... ");
    fflush(stderr);
    if ((subset=T1_SubsetFont(i, mask, flags, 64, 16384, &ofsize))==NULL) {
      fprintf(stderr, "failed (T1_errno: %d)\n", T1_errno);
    }
    else {
      fwrite(subset, 1, ofsize, ofp);
      fprintf(stderr, "finished, wrote %lu bytes\n", ofsize);
    }
    fprintf( stderr, "Removing font %d ... ", i);
    fflush(stderr);
    if ((T1_DeleteFont( i))){
      fprintf(stderr, "failed\n");
    }
    else {
      fprintf(stderr, "finished\n");
    }
  }
  
  T1_CloseLib();
  return( 0);
}


void printusage( void)
{
  fprintf(stdout, "Usage: subset [-l|-a|-b|-f|-s|-e <encfile>] <fontfile1> [<fontfile2> ...] string\n");
  fprintf(stdout, "\n");
  fprintf(stdout, "Subset source fontfile(s) according to `string' and write result\nto stdout (T1Lib-%s)!\n\n",
	  T1_GetLibIdent());
  fprintf(stdout, "Options: -l                  Write a log-file t1lib.log.\n");
  fprintf(stdout, "         -a                  Create ASCII-encrypted file (default).\n");
  fprintf(stdout, "         -b                  Create Binary-enrypted file.\n");
  fprintf(stdout, "         -f                  Force reencoding of the font subset, even if\n");
  fprintf(stdout, "                             the source font uses internal StandardEncoding.\n");
  fprintf(stdout, "         -s                  Skip reencoding of the font subset, even if the\n");
  fprintf(stdout, "                             source font defines a font-specific encoding.\n");
  fprintf(stdout, "         -e <encodingfile>   Load an encoding from specified file and reencode\n");
  fprintf(stdout, "                             source font before starting to create the subset.\n");
  fprintf(stdout, "                             This option also implies [-f].\n");

  return;
}