- afm_cunicode *p = fontinfo->ligatures;
- int num = fontinfo->ligatures_count;
- int i;
- if (!num)
- return 0;
- DLOG((stderr, " find-lig, num = %d\n", num));
- for (i = 0; i < num; i++, p += 3) {
- DLOG((stderr, " lig: %d + %d -> %d (%c %c %c)\n",
- p[0], p[1], p[2], p[0], p[1], p[2]));
- if (ch1 == *p && ch2 == p[1]) {
- DLOG((stderr, " matches.\n"));
- return p[2];
+ afm_cunicode *p = fontinfo->ligatures;
+ int num = fontinfo->ligatures_count;
+ int i;
+
+ if (!num)
+ return 0;
+ DLOG((stderr, " find-lig, num = %d\n", num));
+ for (i = 0; i < num; i++, p += 3) {
+ DLOG((stderr, " lig: %d + %d -> %d (%c %c %c)\n",
+ p[0], p[1], p[2], p[0], p[1], p[2]));
+ if (ch1 == *p && ch2 == p[1]) {
+ DLOG((stderr, " matches.\n"));
+ return p[2];
+ }
- const afm_fontinfo *fontinfo = afm_findfont(font);
- long width = 0;
- double widthf;
- const unsigned char *up = (const unsigned char*)text;
- DLOG((stderr, "================= %s\n", text));
- if (fontinfo == NULL)
- return size * strlen(text);
- while (1) {
- afm_unicode ch1, ch2;
- int idx1, kern_idx;
- if ((ch1 = *up) == 0)
- break;
- ch1 = afm_host2unicode(ch1); /* unsafe macro */
- ch2 = *++up;
- ch2 = afm_host2unicode(ch2); /* unsafe macro */
- DLOG((stderr, "------------- Loop: %d + %d (%c%c) at %d\n",
- ch1, ch2, ch1, ch2 ? ch2 : ' ',
- (up - (const unsigned char*)text) - 1));
- idx1 = afm_find_char_index(fontinfo, ch1);
- DLOG((stderr, " idx1 = %d\n", idx1));
-#if ENABLE_LIGATURES
- if (ch2) {
- int ch1_new = afm_find_combined_ligature(fontinfo, ch1, ch2);
- DLOG((stderr, " lig-ch = %d\n", ch1_new));
- if (ch1_new) {
- ch1 = ch1_new;
- idx1 = afm_find_char_index(fontinfo, ch1);
- ch2 = afm_host2unicode(*++up);
- DLOG((stderr, " -> idx1 = %d, ch2 = %d (%c)\n",
- idx1, ch2, ch2 ? ch2 : ' '));
- }
+#ifdef HAVE_MBSTOWCS
+ size_t clen = strlen(text) + 1;
+ wchar_t *cstr = malloc(sizeof(wchar_t) * clen); /* yes we are allocating probably too much here, I know */
+ int text_count = mbstowcs(cstr, text, clen);
+ double w;
+
+ if (text_count == -1)
+ text_count = mbstowcs(cstr, "Enc-Err", 6);
+#ifdef __APPLE__
+ while (text_count > 0) {
+ text_count--;
+ cstr[text_count] = afm_fix_osx_charset(cstr[text_count]); /* unsafe macro */
- }
- widthf = (width * 6 / 1000.0) * size;
- DLOG((stderr, "Returns %ld (%ld) -> %f\n", width, width * 6, widthf));
- return widthf;
+ while (1) {
+ afm_unicode ch1, ch2;
+ int idx1, kern_idx;
+
+ if ((ch1 = *up) == 0)
+ break;
+ ch2 = *++up;
+ DLOG((stderr, "------------- Loop: %d + %d (%c%c) at %d\n",
+ ch1, ch2, ch1, ch2 ? ch2 : ' ',
+ (up - (const unsigned char *) text) - 1));
+ idx1 = afm_find_char_index(fontinfo, ch1);
+ DLOG((stderr, " idx1 = %d\n", idx1));
+#if ENABLE_LIGATURES
+ if (ch2) {
+ int ch1_new =
+ afm_find_combined_ligature(fontinfo, ch1, ch2);
+
+ DLOG((stderr, " lig-ch = %d\n", ch1_new));
+ if (ch1_new) {
+ ch1 = ch1_new;
+ idx1 = afm_find_char_index(fontinfo, ch1);
+ ch2 = *++up;
+ DLOG((stderr, " -> idx1 = %d, ch2 = %d (%c)\n",
+ idx1, ch2, ch2 ? ch2 : ' '));
+ }
+ }
+#endif
+ width += fontinfo->widths[idx1];
+ DLOG((stderr, "Plain width of %d = %d\n", ch1,
+ fontinfo->widths[idx1]));
+ if (fontinfo->kerning_index && ch2) {
+ kern_idx = fontinfo->kerning_index[idx1];
+ DLOG((stderr, " kern_idx = %d\n", kern_idx));
+ if (kern_idx > 0)
+ width += afm_find_kern(fontinfo, kern_idx, ch2);
+ }
+ }
+ widthf = (width * 6 / 1000.0) * size;
+ DLOG((stderr, "Returns %ld (%ld) -> %f\n", width, width * 6, widthf));
+ return widthf;
- '\xC4', '\xC5', '\xC7', '\xC9', '\xD1', '\xD6', '\xDC', '\xE1', /* 80 */
- '\xE0', '\xE2', '\xE4', '\xE3', '\xE5', '\xE7', '\xE9', '\xE8', /* 88 */
- '\xEA', '\xEB', '\xED', '\xEC', '\xEE', '\xEF', '\xF1', '\xF3', /* 90 */
- '\xF2', '\xF4', '\xF6', '\xF5', '\xFA', '\xF9', '\xFB', '\xFC', /* 98 */
- '\xDD', '\xB0', '\xA2', '\xA3', '\xA7', ' ', '\xB6', '\xDF', /* A0 */
- '\xAE', '\xA9', ' ', '\xB4', '\xA8', ' ', '\xC6', '\xD8', /* A8 */
- ' ', '\xB1', '\xBE', ' ', '\xA5', '\xB5', ' ', ' ', /* B0 */
- '\xBD', '\xBC', ' ', '\xAA', '\xBA', ' ', '\xE6', '\xF8', /* B8 */
- '\xBF', '\xA1', '\xAC', ' ', ' ', ' ', ' ', '\xAB', /* C0 */
- '\xBB', ' ', '\xA0', '\xC0', '\xC3', '\xD5', ' ', '\xA6', /* C8 */
- '\xAD', ' ', '"', '"', '\'', '\'', '\xF7', '\xD7', /* D0 */
- '\xFF', ' ', ' ', '\xA4', '\xD0', '\xF0', '\xDE', '\xFE', /* D8 */
- '\xFD', '\xB7', ' ', ' ', ' ', '\xC2', '\xCA', '\xC1', /* E0 */
- '\xCB', '\xC8', '\xCD', '\xCE', '\xCF', '\xCC', '\xD3', '\xD4', /* E8 */
- ' ', '\xD2', '\xDA', '\xDB', '\xD9', ' ', ' ', ' ', /* F0 */
- '\xAF', ' ', ' ', ' ', '\xB8', ' ', ' ', ' ', /* F8 */
+ '\xC4', '\xC5', '\xC7', '\xC9', '\xD1', '\xD6', '\xDC', '\xE1', /* 80 */
+ '\xE0', '\xE2', '\xE4', '\xE3', '\xE5', '\xE7', '\xE9', '\xE8', /* 88 */
+ '\xEA', '\xEB', '\xED', '\xEC', '\xEE', '\xEF', '\xF1', '\xF3', /* 90 */
+ '\xF2', '\xF4', '\xF6', '\xF5', '\xFA', '\xF9', '\xFB', '\xFC', /* 98 */
+ '\xDD', '\xB0', '\xA2', '\xA3', '\xA7', ' ', '\xB6', '\xDF', /* A0 */
+ '\xAE', '\xA9', ' ', '\xB4', '\xA8', ' ', '\xC6', '\xD8', /* A8 */
+ ' ', '\xB1', '\xBE', ' ', '\xA5', '\xB5', ' ', ' ', /* B0 */
+ '\xBD', '\xBC', ' ', '\xAA', '\xBA', ' ', '\xE6', '\xF8', /* B8 */
+ '\xBF', '\xA1', '\xAC', ' ', ' ', ' ', ' ', '\xAB', /* C0 */
+ '\xBB', ' ', '\xA0', '\xC0', '\xC3', '\xD5', ' ', '\xA6', /* C8 */
+ '\xAD', ' ', '"', '"', '\'', '\'', '\xF7', '\xD7', /* D0 */
+ '\xFF', ' ', ' ', '\xA4', '\xD0', '\xF0', '\xDE', '\xFE', /* D8 */
+ '\xFD', '\xB7', ' ', ' ', ' ', '\xC2', '\xCA', '\xC1', /* E0 */
+ '\xCB', '\xC8', '\xCD', '\xCE', '\xCF', '\xCC', '\xD3', '\xD4', /* E8 */
+ ' ', '\xD2', '\xDA', '\xDB', '\xD9', ' ', ' ', ' ', /* F0 */
+ '\xAF', ' ', ' ', ' ', '\xB8', ' ', ' ', ' ', /* F8 */