Initialize properties of `globals' object.
[ttfautohint.git] / frontend / info.cpp
blobfb7f68f69aaccf52501f985cfe4b642550fb24f5
1 // info.cpp
3 // Copyright (C) 2012 by Werner Lemberg.
4 //
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.
9 //
10 // The file `COPYING' mentioned in the previous paragraph is distributed
11 // with the ttfautohint library.
14 #include <config.h>
15 #include <stdio.h>
16 #include <string.h>
17 #include <stdlib.h>
19 #include "info.h"
22 #define TTFAUTOHINT_STRING "; ttfautohint"
23 #define TTFAUTOHINT_STRING_WIDE "\0;\0 \0t\0t\0f\0a\0u\0t\0o\0h\0i\0n\0t"
25 // build string which gets appended to the `Version' field(s)
27 extern "C" {
29 void
30 build_version_string(Info_Data* idata)
32 char* d;
33 char* dw;
34 char strong[4];
35 int count;
37 d = (char*)idata->data;
38 d += sprintf(d, TTFAUTOHINT_STRING " (v%s)", VERSION);
40 d += sprintf(d, " -l %d", idata->hinting_range_min);
41 d += sprintf(d, " -r %d", idata->hinting_range_max);
42 d += sprintf(d, " -G %d", idata->hinting_limit);
43 d += sprintf(d, " -x %d", idata->increase_x_height);
45 count = 0;
46 strong[0] = '\0';
47 strong[1] = '\0';
48 strong[2] = '\0';
49 strong[3] = '\0';
50 if (idata->gray_strong_stem_width)
51 strong[count++] = 'g';
52 if (idata->gdi_cleartype_strong_stem_width)
53 strong[count++] = 'G';
54 if (idata->dw_cleartype_strong_stem_width)
55 strong[count++] = 'D';
56 d += sprintf(d, " -w \"%s\"", strong);
58 if (idata->windows_compatibility)
59 d += sprintf(d, " -W");
60 if (idata->pre_hinting)
61 d += sprintf(d, " -p");
62 if (!idata->hint_with_components)
63 d += sprintf(d, " -c");
64 if (idata->latin_fallback)
65 d += sprintf(d, " -f");
66 if (idata->symbol)
67 d += sprintf(d, " -s");
69 idata->data_len = d - (char*)idata->data;
71 // prepare UTF16-BE version data
72 d = (char*)idata->data;
73 dw = (char*)idata->data_wide;
74 for (unsigned short i = 0; i < idata->data_len; i++)
76 *(dw++) = '\0';
77 *(dw++) = *(d++);
79 idata->data_wide_len = idata->data_len << 1;
83 int
84 info(unsigned short platform_id,
85 unsigned short encoding_id,
86 unsigned short /* language_id */,
87 unsigned short name_id,
88 unsigned short* len,
89 unsigned char** str,
90 void* user)
92 Info_Data* idata = (Info_Data*)user;
93 unsigned char ttfautohint_string[] = TTFAUTOHINT_STRING;
94 unsigned char ttfautohint_string_wide[] = TTFAUTOHINT_STRING_WIDE;
96 // we use memmem, so don't count the trailing \0 character
97 size_t ttfautohint_string_len = sizeof (TTFAUTOHINT_STRING) - 1;
98 size_t ttfautohint_string_wide_len = sizeof (TTFAUTOHINT_STRING_WIDE) - 1;
100 unsigned char* v;
101 unsigned short v_len;
102 unsigned char* s;
103 size_t s_len;
104 size_t offset;
106 // if it is a version string, append our data
107 if (name_id != 5)
108 return 0;
110 if (platform_id == 1
111 || (platform_id == 3 && !(encoding_id == 1
112 || encoding_id == 10)))
114 // one-byte or multi-byte encodings
115 v = idata->data;
116 v_len = idata->data_len;
117 s = ttfautohint_string;
118 s_len = ttfautohint_string_len;
119 offset = 2;
121 else
123 // (two-byte) UTF-16BE for everything else
124 v = idata->data_wide;
125 v_len = idata->data_wide_len;
126 s = ttfautohint_string_wide;
127 s_len = ttfautohint_string_wide_len;
128 offset = 4;
131 // if we already have an ttfautohint info string,
132 // remove it up to a following `;' character (or end of string)
133 unsigned char* s_start = (unsigned char*)memmem(*str, *len, s, s_len);
134 if (s_start)
136 unsigned char* s_end = s_start + offset;
137 unsigned char* limit = *str + *len;
139 while (s_end < limit)
141 if (*s_end == ';')
143 if (offset == 2)
144 break;
145 else
147 if (*(s_end - 1) == '\0') // UTF-16BE
149 s_end--;
150 break;
155 s_end++;
158 while (s_end < limit)
159 *s_start++ = *s_end++;
161 *len -= s_end - s_start;
164 // do nothing if the string would become too long
165 if (*len > 0xFFFF - v_len)
166 return 0;
168 unsigned short len_new = *len + v_len;
169 unsigned char* str_new = (unsigned char*)realloc(*str, len_new);
170 if (!str_new)
171 return 1;
173 *str = str_new;
174 memcpy(*str + *len, v, v_len);
175 *len = len_new;
177 return 0;
180 } // extern "C"
182 // end of info.cpp