Remove info in `name' table from previous run of ttfautohint.
[ttfautohint.git] / frontend / info.cpp
blob06baaf6394829211c835c90f5123ab3fabd0453d
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[1] = '\0';
47 strong[2] = '\0';
48 strong[3] = '\0';
49 if (idata->gray_strong_stem_width)
50 strong[count++] = 'g';
51 if (idata->gdi_cleartype_strong_stem_width)
52 strong[count++] = 'G';
53 if (idata->dw_cleartype_strong_stem_width)
54 strong[count++] = 'D';
55 d+= sprintf(d, " -w \"%s\"", strong);
57 if (idata->pre_hinting)
58 d += sprintf(d, " -p");
59 if (idata->hint_with_components)
60 d += sprintf(d, " -c");
61 if (idata->latin_fallback)
62 d += sprintf(d, " -f");
63 if (idata->symbol)
64 d += sprintf(d, " -s");
66 idata->data_len = d - (char*)idata->data;
68 // prepare UTF16-BE version data
69 d = (char*)idata->data;
70 dw = (char*)idata->data_wide;
71 for (unsigned short i = 0; i < idata->data_len; i++)
73 *(dw++) = '\0';
74 *(dw++) = *(d++);
76 idata->data_wide_len = idata->data_len << 1;
80 int
81 info(unsigned short platform_id,
82 unsigned short encoding_id,
83 unsigned short /* language_id */,
84 unsigned short name_id,
85 unsigned short* len,
86 unsigned char** str,
87 void* user)
89 Info_Data* idata = (Info_Data*)user;
90 unsigned char ttfautohint_string[] = TTFAUTOHINT_STRING;
91 unsigned char ttfautohint_string_wide[] = TTFAUTOHINT_STRING_WIDE;
93 // we use memmem, so don't count the trailing \0 character
94 size_t ttfautohint_string_len = sizeof (TTFAUTOHINT_STRING) - 1;
95 size_t ttfautohint_string_wide_len = sizeof (TTFAUTOHINT_STRING_WIDE) - 1;
97 unsigned char* v;
98 unsigned short v_len;
99 unsigned char* s;
100 size_t s_len;
101 size_t offset;
103 // if it is a version string, append our data
104 if (name_id != 5)
105 return 0;
107 if (platform_id == 1
108 || (platform_id == 3 && !(encoding_id == 1
109 || encoding_id == 10)))
111 // one-byte or multi-byte encodings
112 v = idata->data;
113 v_len = idata->data_len;
114 s = ttfautohint_string;
115 s_len = ttfautohint_string_len;
116 offset = 2;
118 else
120 // (two-byte) UTF-16BE for everything else
121 v = idata->data_wide;
122 v_len = idata->data_wide_len;
123 s = ttfautohint_string_wide;
124 s_len = ttfautohint_string_wide_len;
125 offset = 4;
128 // if we already have an ttfautohint info string,
129 // remove it up to a following `;' character (or end of string)
130 unsigned char* s_start = (unsigned char*)memmem(*str, *len, s, s_len);
131 if (s_start)
133 unsigned char* s_end = s_start + offset;
134 unsigned char* limit = *str + *len;
136 while (s_end < limit)
138 if (*s_end == ';')
140 if (offset == 2)
141 break;
142 else
144 if (*(s_end - 1) == '\0') // UTF-16BE
146 s_end--;
147 break;
152 s_end++;
155 while (s_end < limit)
156 *s_start++ = *s_end++;
158 *len -= s_end - s_start;
161 // do nothing if the string would become too long
162 if (*len > 0xFFFF - v_len)
163 return 0;
165 unsigned short len_new = *len + v_len;
166 unsigned char* str_new = (unsigned char*)realloc(*str, len_new);
167 if (!str_new)
168 return 1;
170 *str = str_new;
171 memcpy(*str + *len, v, v_len);
172 *len = len_new;
174 return 0;
177 } // extern "C"
179 // end of info.cpp