| 1 |
/* |
| 2 |
Compiler of CFF, subset and full conversion |
| 3 |
* note that there is no optimization in current implement |
| 4 |
*/ |
| 5 |
|
| 6 |
#include <math.h> |
| 7 |
#include <stdarg.h> |
| 8 |
#include <stdint.h> |
| 9 |
#include <stdio.h> |
| 10 |
#include <stdlib.h> |
| 11 |
#include <string.h> |
| 12 |
|
| 13 |
#include "libcff.h" |
| 14 |
|
| 15 |
caryll_Buffer *cff_buildHeader(void) { |
| 16 |
return bufninit(4, 1, 0, 4, 4); |
| 17 |
} |
| 18 |
|
| 19 |
void cff_mergeCS2Operator(caryll_Buffer *blob, int32_t val) { |
| 20 |
if (val >= 0x100) { |
| 21 |
bufnwrite8(blob, 2, val >> 8, val & 0xff); |
| 22 |
} else { |
| 23 |
bufnwrite8(blob, 1, val & 0xff); |
| 24 |
} |
| 25 |
} |
| 26 |
void cff_mergeCS2Int(caryll_Buffer *blob, int32_t val) { |
| 27 |
if (val >= -1131 && val <= -108) { |
| 28 |
bufnwrite8(blob, 2, (uint8_t)((-108 - val) / 256 + 251), (uint8_t)((-108 - val) % 256)); |
| 29 |
} else if (val >= -107 && val <= 107) { |
| 30 |
bufnwrite8(blob, 1, (uint8_t)(val + 139)); |
| 31 |
} else if (val >= 108 && val <= 1131) { |
| 32 |
bufnwrite8(blob, 2, (uint8_t)((val - 108) / 256 + 247), (uint8_t)((val - 108) % 256)); |
| 33 |
} else { |
| 34 |
if (val >= -32768 && val <= 32767) { |
| 35 |
bufnwrite8(blob, 3, 28, (uint8_t)(val >> 8), (uint8_t)((val << 8) >> 8)); |
| 36 |
} else { |
| 37 |
cff_mergeCS2Int(blob, 0); |
| 38 |
} |
| 39 |
} |
| 40 |
} |
| 41 |
static void mergeCS2Real(caryll_Buffer *blob, double val) { |
| 42 |
int16_t integerPart = floor(val); |
| 43 |
uint16_t fractionPart = (val - integerPart) * 65536.0; |
| 44 |
bufnwrite8(blob, 5, 0xFF, integerPart >> 8, integerPart & 0xFF, fractionPart >> 8, fractionPart & 0xFF); |
| 45 |
} |
| 46 |
void cff_mergeCS2Operand(caryll_Buffer *blob, double val) { |
| 47 |
double intpart; |
| 48 |
if (modf(val, &intpart) == 0.0) { |
| 49 |
cff_mergeCS2Int(blob, (int32_t)intpart); |
| 50 |
} else { |
| 51 |
mergeCS2Real(blob, val); |
| 52 |
} |
| 53 |
} |
| 54 |
void cff_mergeCS2Special(caryll_Buffer *blob, uint8_t val) { |
| 55 |
bufwrite8(blob, val); |
| 56 |
} |
| 57 |
|
| 58 |
caryll_Buffer *cff_buildOffset(int32_t val) { |
| 59 |
return bufninit(5, 29, (val >> 24) & 0xff, (val >> 16) & 0xff, (val >> 8) & 0xff, val & 0xff); |
| 60 |
} |