From b6cde86fa2f259821593d13be7dc5d78e032a4f7 Mon Sep 17 00:00:00 2001 From: Werner Lemberg Date: Sat, 24 Nov 2012 13:51:43 +0100 Subject: [PATCH] Add x height snapping exceptions data to version string info. --- frontend/info.cpp | 86 ++++++++++++++++++++++++++++++++++++++++++++++++++-- frontend/info.h | 4 ++- frontend/main.cpp | 23 ++++++++++---- frontend/maingui.cpp | 35 +++++++++++++++++---- lib/ttfautohint.c | 2 +- 5 files changed, 134 insertions(+), 16 deletions(-) diff --git a/frontend/info.cpp b/frontend/info.cpp index fb7f68f..3f28961 100644 --- a/frontend/info.cpp +++ b/frontend/info.cpp @@ -26,13 +26,29 @@ extern "C" { -void +// return value 1 means allocation error, value 2 too long a string + +int build_version_string(Info_Data* idata) { char* d; char* dw; + char* s = NULL; + size_t s_len; + unsigned char* data_new; + unsigned short data_new_len; char strong[4]; int count; + int ret = 0; + + // 128 bytes certainly hold the following options except -X + data_new = (unsigned char*)realloc(idata->data, 128); + if (!data_new) + { + ret = 1; + goto Fail; + } + idata->data = data_new; d = (char*)idata->data; d += sprintf(d, TTFAUTOHINT_STRING " (v%s)", VERSION); @@ -65,10 +81,61 @@ build_version_string(Info_Data* idata) d += sprintf(d, " -f"); if (idata->symbol) d += sprintf(d, " -s"); + if (idata->x_height_snapping_exceptions) + d += sprintf(d, " -X \"\""); // fill in data later idata->data_len = d - (char*)idata->data; + if (idata->x_height_snapping_exceptions) + { + s = number_set_show(idata->x_height_snapping_exceptions, 6, 0x7FFF); + if (!s) + { + ret = 1; + goto Fail; + } + + // ensure UTF16-BE version doesn't get too long + s_len = strlen(s); + if (s_len > 0xFFFF / 2 - 128) + { + ret = 2; + goto Fail; + } + } + else + s_len = 0; + + // we now reallocate to the real size + data_new_len = idata->data_len + s_len; + data_new = (unsigned char*)realloc(idata->data, data_new_len); + if (!data_new) + { + ret = 1; + goto Fail; + } + + if (idata->x_height_snapping_exceptions) + { + // overwrite second doublequote and append it instead + d = (char*)(data_new + idata->data_len - 1); + sprintf(d, "%s\"", s); + } + + idata->data = data_new; + idata->data_len = data_new_len; + // prepare UTF16-BE version data + idata->data_wide_len = 2 * idata->data_len; + data_new = (unsigned char*)realloc(idata->data_wide, + idata->data_wide_len); + if (!data_new) + { + ret = 1; + goto Fail; + } + idata->data_wide = data_new; + d = (char*)idata->data; dw = (char*)idata->data_wide; for (unsigned short i = 0; i < idata->data_len; i++) @@ -76,7 +143,22 @@ build_version_string(Info_Data* idata) *(dw++) = '\0'; *(dw++) = *(d++); } - idata->data_wide_len = idata->data_len << 1; + +Exit: + free(s); + + return ret; + +Fail: + free(idata->data); + free(idata->data_wide); + + idata->data = NULL; + idata->data_wide = NULL; + idata->data_len = 0; + idata->data_wide_len = 0; + + goto Exit; } diff --git a/frontend/info.h b/frontend/info.h index 4851cd8..297e72b 100644 --- a/frontend/info.h +++ b/frontend/info.h @@ -15,6 +15,7 @@ #define __INFO_H__ #include +#include extern "C" { @@ -34,6 +35,7 @@ typedef struct Info_Data_ bool dw_cleartype_strong_stem_width; int increase_x_height; + number_range* x_height_snapping_exceptions; bool windows_compatibility; bool pre_hinting; @@ -43,7 +45,7 @@ typedef struct Info_Data_ } Info_Data; -void +int build_version_string(Info_Data* idata); TA_Error diff --git a/frontend/main.cpp b/frontend/main.cpp index 98edd97..b730656 100644 --- a/frontend/main.cpp +++ b/frontend/main.cpp @@ -610,9 +610,6 @@ main(int argc, in = stdin; } - unsigned char version_data[128]; - unsigned char version_data_wide[256]; - const unsigned char* error_string; Progress_Data progress_data = {-1, 1, 0}; Info_Data info_data; @@ -621,8 +618,10 @@ main(int argc, info_func = NULL; else { - info_data.data = version_data; - info_data.data_wide = version_data_wide; + info_data.data = NULL; // must be deallocated after use + info_data.data_wide = NULL; // must be deallocated after use + info_data.data_len = 0; + info_data.data_wide_len = 0; info_data.hinting_range_min = hinting_range_min; info_data.hinting_range_max = hinting_range_max; @@ -639,7 +638,13 @@ main(int argc, info_data.latin_fallback = latin_fallback; info_data.symbol = symbol; - build_version_string(&info_data); + int ret = build_version_string(&info_data); + if (ret == 1) + fprintf(stderr, "Warning: Can't allocate memory" + " for ttfautohint options string in `name' table\n"); + else if (ret == 2) + fprintf(stderr, "Warning: ttfautohint options string" + " in `name' table too long\n"); } if (in == stdin) @@ -671,6 +676,12 @@ main(int argc, increase_x_height, latin_fallback, symbol, debug); + if (!no_info) + { + free(info_data.data); + free(info_data.data_wide); + } + if (error) { if (error == TA_Err_Invalid_FreeType_Version) diff --git a/frontend/maingui.cpp b/frontend/maingui.cpp index 4f14d5a..db3befd 100644 --- a/frontend/maingui.cpp +++ b/frontend/maingui.cpp @@ -536,9 +536,6 @@ again: if (!open_files(input_name, &input, output_name, &output)) return; - unsigned char version_data[128]; - unsigned char version_data_wide[256]; - QProgressDialog dialog; dialog.setCancelButtonText(tr("Cancel")); dialog.setMinimumDuration(1000); @@ -549,8 +546,10 @@ again: GUI_Progress_Data gui_progress_data = {-1, true, &dialog}; Info_Data info_data; - info_data.data = version_data; - info_data.data_wide = version_data_wide; + info_data.data = NULL; // must be deallocated after use + info_data.data_wide = NULL; // must be deallocated after use + info_data.data_len = 0; + info_data.data_wide_len = 0; info_data.hinting_range_min = min_box->value(); info_data.hinting_range_max = max_box->value(); @@ -573,7 +572,25 @@ again: info_data.symbol = symbol_box->isChecked(); if (info_box->isChecked()) - build_version_string(&info_data); + { + int ret = build_version_string(&info_data); + if (ret == 1) + QMessageBox::information( + this, + "TTFautohint", + tr("Can't allocate memory for TTFautohint options string" + " in name table."), + QMessageBox::Ok, + QMessageBox::Ok); + else if (ret == 2) + QMessageBox::information( + this, + "TTFautohint", + tr("TTFautohint options string" + " in name table too long."), + QMessageBox::Ok, + QMessageBox::Ok); + } else info_func = NULL; @@ -609,6 +626,12 @@ again: info_data.increase_x_height, info_data.latin_fallback, info_data.symbol); + if (info_box->isChecked()) + { + free(info_data.data); + free(info_data.data_wide); + } + fclose(input); fclose(output); diff --git a/lib/ttfautohint.c b/lib/ttfautohint.c index 6d14abf..1691797 100644 --- a/lib/ttfautohint.c +++ b/lib/ttfautohint.c @@ -351,7 +351,7 @@ TTF_autohint(const char* options, font->windows_compatibility); s = number_set_show(font->x_height_snapping_exceptions, - TA_PROP_INCREASE_X_HEIGHT_MIN, -1); + TA_PROP_INCREASE_X_HEIGHT_MIN, 0x7FFF); DUMPSTR("x-height-snapping-exceptions", s); free(s); -- 2.11.4.GIT