3 // Copyright (C) 2012-2014 by Werner Lemberg.
5 // This file is part of the ttfautohint library, and may only be used,
6 // modified, and distributed under the terms given in `COPYING'. By
7 // continuing to use, modify, or distribute this file you indicate that you
8 // have read `COPYING' and understand and accept it fully.
10 // The file `COPYING' mentioned in the previous paragraph is distributed
11 // with the ttfautohint library.
20 #include <numberset.h>
23 #define TTFAUTOHINT_STRING "; ttfautohint"
24 #define TTFAUTOHINT_STRING_WIDE "\0;\0 \0t\0t\0f\0a\0u\0t\0o\0h\0i\0n\0t"
26 // build string that gets appended to the `Version' field(s)
30 // return value 1 means allocation error, value 2 too long a string
33 build_version_string(Info_Data
* idata
)
39 unsigned char* data_new
;
40 unsigned short data_new_len
;
45 // 128 bytes certainly hold the following options except -X
46 data_new
= (unsigned char*)realloc(idata
->data
, 128);
52 idata
->data
= data_new
;
54 d
= (char*)idata
->data
;
55 d
+= sprintf(d
, TTFAUTOHINT_STRING
" (v%s)", VERSION
);
59 d
+= sprintf(d
, " -d");
63 d
+= sprintf(d
, " -l %d", idata
->hinting_range_min
);
64 d
+= sprintf(d
, " -r %d", idata
->hinting_range_max
);
65 d
+= sprintf(d
, " -G %d", idata
->hinting_limit
);
66 d
+= sprintf(d
, " -x %d", idata
->increase_x_height
);
67 if (idata
->fallback_stem_width
)
68 d
+= sprintf(d
, " -H %d", idata
->fallback_stem_width
);
69 d
+= sprintf(d
, " -D %s", idata
->default_script
);
70 d
+= sprintf(d
, " -f %s", idata
->fallback_script
);
77 if (idata
->gray_strong_stem_width
)
78 strong
[count
++] = 'g';
79 if (idata
->gdi_cleartype_strong_stem_width
)
80 strong
[count
++] = 'G';
81 if (idata
->dw_cleartype_strong_stem_width
)
82 strong
[count
++] = 'D';
84 d
+= sprintf(d
, " -w %s", strong
);
86 d
+= sprintf(d
, " -w \"\"");
88 if (idata
->windows_compatibility
)
89 d
+= sprintf(d
, " -W");
90 if (idata
->adjust_subglyphs
)
91 d
+= sprintf(d
, " -p");
92 if (idata
->hint_composites
)
93 d
+= sprintf(d
, " -c");
95 d
+= sprintf(d
, " -s");
96 if (idata
->x_height_snapping_exceptions_string
)
97 d
+= sprintf(d
, " -X \"\""); // fill in data later
100 idata
->data_len
= d
- (char*)idata
->data
;
102 if (idata
->x_height_snapping_exceptions_string
)
104 number_range
* x_height_snapping_exceptions
;
107 // only set specific value of `ret' for an allocation error,
108 // since syntax errors are handled in TTF_autohint
109 pos
= number_set_parse(idata
->x_height_snapping_exceptions_string
,
110 &x_height_snapping_exceptions
,
114 if (x_height_snapping_exceptions
== NUMBERSET_ALLOCATION_ERROR
)
119 s
= number_set_show(x_height_snapping_exceptions
, 6, 0x7FFF);
120 number_set_free(x_height_snapping_exceptions
);
128 // ensure UTF16-BE version doesn't get too long
130 if (s_len
> 0xFFFF / 2 - 128)
139 // we now reallocate to the real size
140 // (plus one byte so that `sprintf' works)
141 data_new_len
= idata
->data_len
+ s_len
;
142 data_new
= (unsigned char*)realloc(idata
->data
, data_new_len
+ 1);
149 if (idata
->x_height_snapping_exceptions_string
)
151 // overwrite second doublequote and append it instead
152 d
= (char*)(data_new
+ idata
->data_len
- 1);
153 sprintf(d
, "%s\"", s
);
156 idata
->data
= data_new
;
157 idata
->data_len
= data_new_len
;
159 // prepare UTF16-BE version data
160 idata
->data_wide_len
= 2 * idata
->data_len
;
161 data_new
= (unsigned char*)realloc(idata
->data_wide
,
162 idata
->data_wide_len
);
168 idata
->data_wide
= data_new
;
170 d
= (char*)idata
->data
;
171 dw
= (char*)idata
->data_wide
;
172 for (unsigned short i
= 0; i
< idata
->data_len
; i
++)
185 free(idata
->data_wide
);
188 idata
->data_wide
= NULL
;
190 idata
->data_wide_len
= 0;
197 info(unsigned short platform_id
,
198 unsigned short encoding_id
,
199 unsigned short /* language_id */,
200 unsigned short name_id
,
205 Info_Data
* idata
= (Info_Data
*)user
;
206 unsigned char ttfautohint_string
[] = TTFAUTOHINT_STRING
;
207 unsigned char ttfautohint_string_wide
[] = TTFAUTOHINT_STRING_WIDE
;
209 // we use memmem, so don't count the trailing \0 character
210 size_t ttfautohint_string_len
= sizeof (TTFAUTOHINT_STRING
) - 1;
211 size_t ttfautohint_string_wide_len
= sizeof (TTFAUTOHINT_STRING_WIDE
) - 1;
214 unsigned short v_len
;
219 // if it is a version string, append our data
224 || (platform_id
== 3 && !(encoding_id
== 1
225 || encoding_id
== 10)))
227 // one-byte or multi-byte encodings
229 v_len
= idata
->data_len
;
230 s
= ttfautohint_string
;
231 s_len
= ttfautohint_string_len
;
236 // (two-byte) UTF-16BE for everything else
237 v
= idata
->data_wide
;
238 v_len
= idata
->data_wide_len
;
239 s
= ttfautohint_string_wide
;
240 s_len
= ttfautohint_string_wide_len
;
244 // if we already have an ttfautohint info string,
245 // remove it up to a following `;' character (or end of string)
246 unsigned char* s_start
= (unsigned char*)memmem(*str
, *len
, s
, s_len
);
249 unsigned char* s_end
= s_start
+ offset
;
250 unsigned char* limit
= *str
+ *len
;
252 while (s_end
< limit
)
260 if (*(s_end
- 1) == '\0') // UTF-16BE
271 while (s_end
< limit
)
272 *s_start
++ = *s_end
++;
274 *len
-= s_end
- s_start
;
277 // do nothing if the string would become too long
278 if (*len
> 0xFFFF - v_len
)
281 unsigned short len_new
= *len
+ v_len
;
282 unsigned char* str_new
= (unsigned char*)realloc(*str
, len_new
);
287 memcpy(*str
+ *len
, v
, v_len
);