/*In Xcode, FLT_EPSILON is 1.192..x10-7, but the diff between value-roundf(value) can be 3.05..x10-5, when the input value was a 24.8 Fixed. */ static void writeReal(ufwCtx h, float value) { char buf[50]; /* if no decimal component, perform a faster to string conversion */ if ((fabs(value - roundf(value)) < TX_EPSILON) && value > LONG_MIN && value < LONG_MAX) ufw_ltoa(buf, (long)roundf(value)); else ctuDtostr(buf, sizeof(buf), value, 0, 2); writeBuf(h, strlen(buf), buf); }
/* Save real number arg in DICT. If not fractional save as integer. */ void cfwDictSaveReal(DICT *dict, float r) { char buf[50]; int value; /* Current nibble value */ int last = 0; /* Last nibble value */ int odd = 0; /* Flags odd nibble */ long i = (long)r; if (i == r) { /* Value was integer */ cfwDictSaveInt(dict, i); return; } /* Convert to string; 8 digits of precision is enough to accurately represent the matrix of a typical TrueType font using a 2048 unit em */ ctuDtostr(buf, r, 0, 8); *dnaNEXT(*dict) = cff_BCD; for (i = buf[0] == '0';; i++) { switch (buf[i]) { case '\0': /* Terminate number */ *dnaNEXT(*dict) = odd ? last << 4 | 0xf : 0xff; return; case '+': continue; case '-': value = 0xe; break; case '.': value = 0xa; break; case 'E': case 'e': value = (buf[++i] == '-') ? 0xc : 0xb; break; default: value = buf[i] - '0'; break; } if (odd) { *dnaNEXT(*dict) = last << 4 | value; } else { last = value; } odd = !odd; } }
/* Save real number arg in DICT. If not fractional save as integer. */ void cfwDictSaveReal(DICT *dict, float r) { char buf[50]; int value; /* Current nibble value */ int last = 0; /* Last nibble value */ int odd = 0; /* Flags odd nibble */ long i = (long)r; if (i == r) { /* Value was integer */ cfwDictSaveInt(dict, i); return; } ctuDtostr(buf, r, 0, 8); /* 8 places is as good as it gets when converting ASCII real numbers->float-> ASCII real numbers, as happens to all the PrivateDict values.*/ *dnaNEXT(*dict) = cff_BCD; for (i = buf[0] == '0';; i++) { switch (buf[i]) { case '\0': /* Terminate number */ *dnaNEXT(*dict) = odd ? last << 4 | 0xf : 0xff; return; case '+': continue; case '-': value = 0xe; break; case '.': value = 0xa; break; case 'E': case 'e': value = (buf[++i] == '-') ? 0xc : 0xb; break; default: value = buf[i] - '0'; break; } if (odd) { *dnaNEXT(*dict) = last << 4 | value; } else { last = value; } odd = !odd; } }