Skip to content

Commit

Permalink
Merge pull request #165 from Mrmaxmeier/cleanup-unicode
Browse files Browse the repository at this point in the history
Cleanup handling of unicode filenames
  • Loading branch information
pkgw committed Sep 25, 2018
2 parents 6d61258 + 4ea5dab commit b53f3b9
Show file tree
Hide file tree
Showing 20 changed files with 229 additions and 241 deletions.
52 changes: 25 additions & 27 deletions tectonic/XeTeX_ext.c
Expand Up @@ -133,40 +133,39 @@ get_encoding_mode_and_info(int32_t* info)
*/
UErrorCode err = U_ZERO_ERROR;
UConverter* cnv;
char* name = (char*)name_of_file + 1;
*info = 0;
if (strcasecmp(name, "auto") == 0) {
if (strcasecmp(name_of_file, "auto") == 0) {
return AUTO;
}
if (strcasecmp(name, "utf8") == 0) {
if (strcasecmp(name_of_file, "utf8") == 0) {
return UTF8;
}
if (strcasecmp(name, "utf16") == 0) { /* depends on host platform */
if (strcasecmp(name_of_file, "utf16") == 0) { /* depends on host platform */
return US_NATIVE_UTF16;
}
if (strcasecmp(name, "utf16be") == 0) {
if (strcasecmp(name_of_file, "utf16be") == 0) {
return UTF16BE;
}
if (strcasecmp(name, "utf16le") == 0) {
if (strcasecmp(name_of_file, "utf16le") == 0) {
return UTF16LE;
}
if (strcasecmp(name, "bytes") == 0) {
if (strcasecmp(name_of_file, "bytes") == 0) {
return RAW;
}

/* try for an ICU converter */
cnv = ucnv_open(name, &err);
cnv = ucnv_open(name_of_file, &err);
if (cnv == NULL) {
begin_diagnostic();
print_nl('U'); /* ensure message starts on a new line */
print_c_string("nknown encoding `");
print_c_string(name);
print_c_string(name_of_file);
print_c_string("'; reading as raw bytes");
end_diagnostic(1);
return RAW;
} else {
ucnv_close(cnv);
*info = maketexstring(name);
*info = maketexstring(name_of_file);
return ICUMAPPING;
}
}
Expand Down Expand Up @@ -235,7 +234,7 @@ static char *saved_mapping_name = NULL;
void
check_for_tfm_font_mapping(void)
{
char* cp = strstr((char*)name_of_file + 1, ":mapping=");
char* cp = strstr(name_of_file, ":mapping=");
saved_mapping_name = mfree(saved_mapping_name);
if (cp != NULL) {
*cp = 0;
Expand Down Expand Up @@ -711,7 +710,7 @@ splitFontName(char* name, char** var, char** feat, char** end, int* index)
}

void*
find_native_font(unsigned char* uname, int32_t scaled_size)
find_native_font(char* uname, int32_t scaled_size)
/* scaled_size here is in TeX points, or is a negative integer for 'scaled_t' */
{
void* rval = NULL;
Expand Down Expand Up @@ -798,9 +797,8 @@ find_native_font(unsigned char* uname, int32_t scaled_size)
if (varString != NULL)
name_length += strlen(varString) + 1;
free(name_of_file);
name_of_file = xmalloc(name_length + 4); /* +2 would be correct: initial space, final NUL */
name_of_file[0] = ' ';
strcpy((char*)name_of_file + 1, fullName);
name_of_file = xmalloc(name_length + 3); /* +1 would be correct here (trailing \0). +3 is kept for Pascal-like access patterns. */
strcpy(name_of_file, fullName);

if (scaled_size < 0) {
font = createFont(fontRef, scaled_size);
Expand Down Expand Up @@ -843,14 +841,14 @@ find_native_font(unsigned char* uname, int32_t scaled_size)

/* append the style and feature strings, so that \show\fontID will give a full result */
if (varString != NULL && *varString != 0) {
strcat((char*)name_of_file + 1, "/");
strcat((char*)name_of_file + 1, varString);
strcat(name_of_file, "/");
strcat(name_of_file, varString);
}
if (featString != NULL && *featString != 0) {
strcat((char*)name_of_file + 1, ":");
strcat((char*)name_of_file + 1, featString);
strcat(name_of_file, ":");
strcat(name_of_file, featString);
}
name_length = strlen((char*)name_of_file + 1);
name_length = strlen(name_of_file);
}
}

Expand Down Expand Up @@ -1035,7 +1033,7 @@ gr_font_get_named(int32_t what, void* pEngine)
XeTeXLayoutEngine engine = (XeTeXLayoutEngine)pEngine;
switch (what) {
case XeTeX_find_feature_by_name:
rval = findGraphiteFeatureNamed(engine, (const char*)name_of_file + 1, name_length);
rval = findGraphiteFeatureNamed(engine, name_of_file, name_length);
break;
}
return rval;
Expand All @@ -1048,7 +1046,7 @@ gr_font_get_named_1(int32_t what, void* pEngine, int32_t param)
XeTeXLayoutEngine engine = (XeTeXLayoutEngine)pEngine;
switch (what) {
case XeTeX_find_selector_by_name:
rval = findGraphiteFeatureSettingNamed(engine, param, (const char*)name_of_file + 1, name_length);
rval = findGraphiteFeatureSettingNamed(engine, param, name_of_file, name_length);
break;
}
return rval;
Expand Down Expand Up @@ -1828,15 +1826,15 @@ map_char_to_glyph(int32_t font, int32_t ch)

int32_t
map_glyph_to_index(int32_t font)
/* glyph name is at name_of_file+1 */
/* glyph name is at name_of_file */
{
#ifdef XETEX_MAC
if (font_area[font] == AAT_FONT_FLAG)
return MapGlyphToIndex_AAT((CFDictionaryRef)(font_layout_engine[font]), (const char*)name_of_file + 1);
return MapGlyphToIndex_AAT((CFDictionaryRef)(font_layout_engine[font]), name_of_file);
else
#endif
if (font_area[font] == OTGR_FONT_FLAG)
return mapGlyphToIndex((XeTeXLayoutEngine)(font_layout_engine[font]), (const char*)name_of_file + 1);
return mapGlyphToIndex((XeTeXLayoutEngine)(font_layout_engine[font]), name_of_file);
else
_tt_abort("bad native font flag in `map_glyph_to_index`");
}
Expand Down Expand Up @@ -2021,7 +2019,7 @@ aat_font_get_named(int what, CFDictionaryRef attributes)
CFArrayRef features = CTFontCopyFeatures(font);
if (features) {
CFDictionaryRef feature = findDictionaryInArray(features, kCTFontFeatureTypeNameKey,
(const char*)name_of_file + 1, name_length);
name_of_file, name_length);
if (feature) {
CFNumberRef identifier = CFDictionaryGetValue(feature, kCTFontFeatureTypeIdentifierKey);
CFNumberGetValue(identifier, kCFNumberIntType, &rval);
Expand All @@ -2047,7 +2045,7 @@ aat_font_get_named_1(int what, CFDictionaryRef attributes, int param)
if (features) {
CFDictionaryRef feature = findDictionaryInArrayWithIdentifier(features, kCTFontFeatureTypeIdentifierKey, param);
if (feature) {
CFNumberRef selector = findSelectorByName(feature, (const char*)name_of_file + 1, name_length);
CFNumberRef selector = findSelectorByName(feature, name_of_file, name_length);
if (selector)
CFNumberGetValue(selector, kCFNumberIntType, &rval);
}
Expand Down
2 changes: 1 addition & 1 deletion tectonic/XeTeX_ext.h
Expand Up @@ -108,7 +108,7 @@ int linebreak_next(void);
int get_encoding_mode_and_info(int32_t* info);
void print_utf8_str(const unsigned char* str, int len);
void print_chars(const unsigned short* str, int len);
void* find_native_font(unsigned char* name, int32_t scaled_size);
void* find_native_font(char* name, int32_t scaled_size);
void release_font_engine(void* engine, int type_flag);
int readCommonFeatures(const char* feat, const char* end, float* extend,
float* slant, float* embolden, float* letterspace, uint32_t* rgbValue);
Expand Down
11 changes: 5 additions & 6 deletions tectonic/XeTeX_pic.c
Expand Up @@ -57,11 +57,11 @@ count_pdf_file_pages (void)
rust_input_handle_t handle;
pdf_file *pf;

handle = ttstub_input_open ((const char *) name_of_file + 1, TTIF_PICT, 0);
handle = ttstub_input_open (name_of_file, TTIF_PICT, 0);
if (handle == NULL)
return 0;

if ((pf = pdf_open((const char *) name_of_file + 1, handle)) == NULL) {
if ((pf = pdf_open(name_of_file, handle)) == NULL) {
/* TODO: issue warning */
ttstub_input_close(handle);
return 0;
Expand Down Expand Up @@ -200,27 +200,26 @@ get_image_size_in_inches (rust_input_handle_t handle, float *width, float *heigh
int
find_pic_file (char **path, real_rect *bounds, int pdfBoxType, int page)
{
char *in_path = (char *) name_of_file + 1;
int err = -1;
rust_input_handle_t handle;

handle = ttstub_input_open (in_path, TTIF_PICT, 0);
handle = ttstub_input_open (name_of_file, TTIF_PICT, 0);
bounds->x = bounds->y = bounds->wd = bounds->ht = 0.0;

if (handle == NULL)
return 1;

if (pdfBoxType != 0) {
/* if cmd was \XeTeXpdffile, use xpdflib to read it */
err = pdf_get_rect (in_path, handle, page, pdfBoxType, bounds);
err = pdf_get_rect (name_of_file, handle, page, pdfBoxType, bounds);
} else {
err = get_image_size_in_inches (handle, &bounds->wd, &bounds->ht);
bounds->wd *= 72.27;
bounds->ht *= 72.27;
}

if (err == 0)
*path = xstrdup(in_path);
*path = xstrdup(name_of_file);

ttstub_input_close (handle);

Expand Down
11 changes: 6 additions & 5 deletions tectonic/io.c
Expand Up @@ -28,7 +28,7 @@ tt_open_input (int filefmt)

fullnameoffile = mfree(fullnameoffile);

fname = (char *) name_of_file + 1;
fname = name_of_file;

if (filefmt == TTIF_TECTONIC_PRIMARY)
handle = ttstub_input_open_primary ();
Expand All @@ -40,8 +40,8 @@ tt_open_input (int filefmt)

fullnameoffile = xstrdup(fname);
name_length = strlen(fname);
name_of_file = xmalloc(name_length + 2);
strcpy((char *) name_of_file + 1, fname);
name_of_file = xmalloc(name_length + 1);
strcpy(name_of_file, fname);
return handle;
}

Expand Down Expand Up @@ -475,7 +475,7 @@ get_uni_c(UFILE* f)
void
make_utf16_name(void)
{
unsigned char* s = name_of_file + 1;
unsigned char* s = (unsigned char *) name_of_file;
uint32_t rval;
uint16_t* t;
static int name16len = 0;
Expand All @@ -485,7 +485,8 @@ make_utf16_name(void)
name_of_file16 = xcalloc(name16len, sizeof(uint16_t));
}
t = name_of_file16;
while (s <= name_of_file + name_length) {

while (s < name_of_file + name_length) {
uint16_t extraBytes;
rval = *(s++);
extraBytes = bytesFromUTF8[rval];
Expand Down
7 changes: 4 additions & 3 deletions tectonic/texmfmp.c
Expand Up @@ -99,14 +99,15 @@ gettexstring (str_number s)
name = xmalloc(len * 3 + 1); /* max UTF16->UTF8 expansion
(code units, not bytes) */
for (i = 0, j = 0; i < len; i++) {
unsigned int c = str_pool[i + str_start[s - 65536L]];
uint32_t c = str_pool[i + str_start[s - 65536L]];
if (c >= 0xD800 && c <= 0xDBFF) {
unsigned int lo = str_pool[++i + str_start[s - 65536L]];
uint32_t lo = str_pool[++i + str_start[s - 65536L]];
if (lo >= 0xDC00 && lo <= 0xDFFF)
c = (c - 0xD800) * 0x0400 + lo - 0xDC00;
c = (c - 0xD800) * 0x0400 + lo - 0xDC00 + 0x10000;
else
c = 0xFFFD;
}

if (c < 0x80)
bytesToWrite = 1;
else if (c < 0x800)
Expand Down
8 changes: 4 additions & 4 deletions tectonic/xetex-shipout.c
Expand Up @@ -164,9 +164,9 @@ ship_out(int32_t p)
if (job_name == 0)
open_log_file();
pack_job_name(output_file_extension);
dvi_file = ttstub_output_open ((const char *) name_of_file + 1, 0);
dvi_file = ttstub_output_open (name_of_file, 0);
if (dvi_file == NULL)
_tt_abort ("cannot open output file \"%s\"", name_of_file + 1);
_tt_abort ("cannot open output file \"%s\"", name_of_file);
output_file_name = make_name_string();
}

Expand Down Expand Up @@ -1697,9 +1697,9 @@ out_what(int32_t p)

pack_file_name(cur_name, cur_area, cur_ext);

write_file[j] = ttstub_output_open((const char *) name_of_file + 1, 0);
write_file[j] = ttstub_output_open(name_of_file, 0);
if (write_file[j] == NULL)
_tt_abort("cannot open output file \"%s\"", name_of_file + 1);
_tt_abort("cannot open output file \"%s\"", name_of_file);

write_open[j] = true;

Expand Down

0 comments on commit b53f3b9

Please sign in to comment.