+static void grow_unicode(ttf_t*ttf, int index)
+{
+ int size = index+1;
+ if(!ttf->unicode) {
+ ttf->unicode = rfx_calloc(sizeof(ttf->unicode[0])*size);
+ } else if(ttf->unicode_size<size) {
+ ttf->unicode = rfx_realloc(ttf->unicode, sizeof(ttf->unicode[0])*size);
+ memset(ttf->unicode+ttf->unicode_size, 0, sizeof(ttf->unicode[0])*(size - ttf->unicode_size));
+ }
+ ttf->unicode_size = size;
+}
+void cmap_parse(memreader_t*r, ttf_t*ttf)
+{
+ readU16(r); // version (0)
+ int num_subtables = readU16(r);
+ int t;
+ char warn=1;
+ if(r->pos+num_subtables*8 > r->size) {
+ msg("<warning> CMap overflow");
+ num_subtables = (r->size-r->pos)/8;
+ }
+ unicode_t*data = 0;
+ for(t=0;t<num_subtables;t++) {
+ U16 platform = readU16(r);
+ U16 encoding = readU16(r);
+ U32 offset = readU32(r);
+ if(offset>r->size) {
+ msg("<warning> CMAP table %d %d is out of bounds (%d)", platform, encoding, offset);
+ continue;
+ }
+
+ int is_unicode = platform==0 ||
+ platform==3 && encoding == 1 ||
+ platform==3 && encoding == 10;
+
+ if(!is_unicode)
+ continue;
+
+ INIT_READ(t, r->mem, r->size, offset);
+ U16 format = readU16(&t);
+ int length = readU16(&t);
+ U16 language = readU16(&t);
+
+ if(language)
+ msg("<warning> Language code %02x in unicode mapping", language);
+
+ int num = 0;
+ if(format == 0) {
+ num = length-6;
+ if(t.pos+length > t.size) {
+ msg("<warning> overflow in format 0 cmap table");
+ num = t.size-t.pos;
+ }
+ data = malloc(num*sizeof(unicode_t));
+ int s;
+ grow_unicode(ttf, num);
+ for(s=0;s<num;s++) {
+ ttf->unicode[s] = readU8(&t);
+ }
+ } else if(format == 4) {
+ U16 segment_count = readU16(&t);
+ if(segment_count&1) {
+ msg("<error> Bad segmentx2 count %d", segment_count);
+ continue;
+ }
+ segment_count>>=1;
+ readU16(&t); //searchrange
+ readU16(&t); //entry selector
+ readU16(&t); //range shift
+ INIT_READ(r_end, t.mem, t.size, t.pos);
+ INIT_READ(r_start, t.mem, t.size, t.pos+2+segment_count*2);
+ INIT_READ(r_delta, t.mem, t.size, t.pos+2+segment_count*4);
+ INIT_READ(r_range, t.mem, t.size, t.pos+2+segment_count*6);
+ int glyphmap_start = t.pos+2+segment_count*8;
+ int glyphmap_size = t.size - glyphmap_start;
+ int s;
+ for(s=0;s<segment_count;s++) {
+ U16 start = readU16(&r_start);
+ U16 end = readU16(&r_end);
+ U16 delta = readU16(&r_delta);
+ U16 range = readU16(&r_range);
+ if(start==0xffff && end==0xffff && delta==1) {
+ /* this is a common (maybe even required) occurence in fonts
+ which explicitly map "unicode undefined" (0xffff) to
+ "glyph undefined" (0).
+ We don't want to blow our unicode table up to 65536 just
+ because of this, so ignore this entry.
+ */
+ continue;
+ }
+ grow_unicode(ttf, end);
+ int u;
+ if(!range) {
+ for(u=start;u<=end;u++) {
+ ttf->unicode[u] = (u + delta) & 0xffff;
+ }
+ } else {
+ int pos = r_range.pos-2+range;
+ if(warn && pos+end-start+1 > t.size) {
+ msg("<warning> glyphmap index out of bounds (%d-%d/%d)", pos, pos+end-start, t.size);
+ warn=0;
+ }
+ INIT_READ(g, t.mem, t.size, pos);
+ for(u=start;u<=end;u++) {
+ ttf->unicode[u] = readU16(&g);
+ }
+ }
+ }
+ }
+ }
+}
+static int segment_size(unicode_t*unicode, int pos, int size)
+{
+ int s;
+ int count=0;
+ for(s=pos;s<size;s++) {
+ if(!unicode[s])
+ count++;
+ if(count>4) {
+ /* a segment costs us 8 bytes, so for more than 4 consecutive
+ zero entries (16 bit each) in the glyph index array,
+ it pays off to start a new segment */
+ break;
+ }
+ }
+ s -= count; // go to the last filled in entry
+ if(s==size)
+ return size-1;
+ return s;
+}
+void cmap_write(ttf_t* ttf, ttf_table_t*w)
+{
+ writeU16(w, 0); //version
+ writeU16(w, 2); //two tables
+
+ writeU16(w, 0); //platform (unicode)
+ writeU16(w, 3); //encoding (unicode 2.0)
+ writeU32(w, 20); //offset
+
+ writeU16(w, 3); //platform (windows)
+ writeU16(w, 1); //encoding (unicode basic multilingual plane UCS-2)
+ writeU32(w, 20); //offset
+
+ writeU16(w, 4); // format=4
+ int length_pos = w->len;
+ writeU16(w, 0); // length: we don't know yet
+ writeU16(w, 0); // language (n/a for unicode)
+ int num_segments_pos = w->len;
+ writeU16(w, 0); //number of segments: we don't know yet either
+ writeU16(w, 0); //searchrange
+ writeU16(w, 0); //entry selector
+ writeU16(w, 0); //range shift
+
+ int pos=0;
+ int num_segments=0;
+ while(pos < ttf->unicode_size) {
+ if(!ttf->unicode[pos]) {
+ pos++;
+ continue;
+ }
+ int s = segment_size(ttf->unicode, pos, ttf->unicode_size);
+ pos = s+1;
+ num_segments++;
+ }
+
+ num_segments++; // account for 0xffff mapping
+
+ int glyphmap_start = w->len+2+num_segments*8;
+
+ int t;
+ int end_pos = w->len;
+ for(t=0;t<num_segments;t++) {writeU16(w, 0);} //end array
+ writeU16(w, 0); //reserved byte
+ int start_pos = w->len;
+ for(t=0;t<num_segments;t++) {writeU16(w, 0);} //start array
+ int delta_pos = w->len;
+ for(t=0;t<num_segments;t++) {writeU16(w, 0);} //delta array
+ int range_pos = w->len;
+ for(t=0;t<num_segments;t++) {writeU16(w, 0);} //range array
+
+ w->data[num_segments_pos]=(num_segments*2)>>8;
+ w->data[num_segments_pos+1]=(num_segments*2);
+
+ pos=0;
+ num_segments = 0;
+ while(pos < ttf->unicode_size) {
+ if(!ttf->unicode[pos]) {
+ pos++;
+ continue;
+ }
+ U16 end = segment_size(ttf->unicode, pos, ttf->unicode_size);
+ w->data[end_pos++]=end>>8;
+ w->data[end_pos++]=end;
+ w->data[start_pos++]=pos>>8;
+ w->data[start_pos++]=pos;
+ int s;
+ U16 delta = ttf->unicode[pos]-pos;
+ char do_delta=1;
+ for(s=pos+1;s<=end;s++) {
+ U16 delta2 = ttf->unicode[s]-s;
+ if(delta2!=delta) {
+ do_delta=0;
+ break;
+ }
+ }
+ U16 range;
+ if(do_delta) {
+ range = 0;
+ } else {
+ delta = 0;
+ range = w->len - range_pos;
+ for(s=pos;s<=end;s++) {
+ writeU16(w, ttf->unicode[s]);
+ }
+ }
+ w->data[delta_pos++]=delta>>8;
+ w->data[delta_pos++]=delta;
+ w->data[range_pos++]=range>>8;
+ w->data[range_pos++]=range;
+ num_segments++;
+ pos = end+1;
+ }
+
+ /* write out a mapping from 0xffff to 0- seems to be required
+ by some libraries (e.g. fonttools) */
+ w->data[end_pos++]=0xff;
+ w->data[end_pos++]=0xff;
+ w->data[start_pos++]=0xff;
+ w->data[start_pos++]=0xff;
+ w->data[delta_pos++]=0;
+ w->data[delta_pos++]=1;
+ w->data[range_pos++]=0;
+ w->data[range_pos++]=0;
+
+ w->data[length_pos]=(w->len-20)>>8;
+ w->data[length_pos+1]=w->len-20;
+}
+void cmap_delete(ttf_t*ttf)
+{
+ if(ttf->unicode) {
+ free(ttf->unicode);
+ ttf->unicode=0;
+ }
+ ttf->unicode_size=0;
+}
+void name_parse(memreader_t*r, ttf_t*ttf)
+{
+ U16 format = readU16(r);
+ U16 count = readU16(r);
+ U16 offset = readU16(r);
+
+ int t;
+ for(t=0;t<count;t++) {
+ U16 platform = readU16(r);
+ U16 encoding = readU16(r);
+ U16 language = readU16(r);
+ U16 name_id = readU16(r);
+ U16 len = readU16(r);
+ U16 offset_2 = readU16(r);
+ if(name_id==4) {
+ if(ttf->name)
+ free(ttf->name);
+ ttf->name = strdup_n(&r->mem[offset+offset_2], len);
+ }
+ }
+}
+void name_write(ttf_t*ttf, ttf_table_t*table)
+{
+ writeU16(table, 0); //format
+ writeU16(table, 1); //count
+ int offset = 18;
+ writeU16(table, offset); //offset
+
+ writeU16(table, 1); //platform id
+ writeU16(table, 0); //encoding id
+ writeU16(table, 0); //language
+ writeU16(table, 4); //4: full name
+ int len = strlen(ttf->name);
+ writeU16(table, len);
+ writeU16(table, table->len+2 - offset);
+ int t;
+ for(t=0;t<len;t++) {
+ writeU8(table, ttf->name[t]);
+ }
+}
+void name_delete(ttf_t*ttf)
+{
+ if(ttf->name) {
+ free(ttf->name);
+ ttf->name=0;
+ }
+}
+
+static table_post_t*post_new(ttf_t*ttf)
+{
+ table_post_t*post = rfx_calloc(sizeof(table_post_t));
+ return post;
+}
+void post_parse(memreader_t*r, ttf_t*ttf)
+{
+ table_post_t*post = ttf->post = rfx_calloc(sizeof(table_post_t));
+ U16 format = readU16(r);
+ post->italic_angle = readU16(r);
+ post->underline_position = readU16(r);
+ post->underline_thickness = readU16(r);
+ U16 is_monospaced = readU16(r);
+ readU16(r); // min mem 42
+ readU16(r);
+ readU16(r); // min mem 1
+ readU16(r);
+}
+void post_write(ttf_t*ttf, ttf_table_t*table)
+{
+ table_post_t*post = ttf->post;
+ writeU32(table, 0x00030000);
+ writeU32(table, post->italic_angle);
+ writeU16(table, post->underline_position);
+ writeU16(table, post->underline_thickness);
+ writeU32(table, 0); //is monospaced TODO
+ writeU32(table, 0); //min mem 42
+ writeU32(table, 0);
+ writeU32(table, 0); //min mem 1
+ writeU32(table, 0);
+}
+void post_delete(ttf_t*ttf)
+{
+ if(ttf->post) {
+ free(ttf->post);
+ ttf->post = 0;
+ }
+}
+