The Birdfont Source Code


All Repositories / birdfont.git / blob – RSS feed

Cmap.vala in libbirdfont/OpenFontFormat

This file is a part of the Birdfont project.

Contributing

Send patches or pull requests to johan.mattsson.m@gmail.com.
Clone this repository: git clone https://github.com/johanmattssonm/birdfont.git

Revisions

View the latest version of libbirdfont/OpenFontFormat/Cmap.vala.
Merge branch 'master' of gitorious.org:birdfont/birdfont
1 /* 2 Copyright (C) 2012, 2013, 2014 Johan Mattsson 3 4 This library is free software; you can redistribute it and/or modify 5 it under the terms of the GNU Lesser General Public License as 6 published by the Free Software Foundation; either version 3 of the 7 License, or (at your option) any later version. 8 9 This library is distributed in the hope that it will be useful, but 10 WITHOUT ANY WARRANTY; without even the implied warranty of 11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 12 Lesser General Public License for more details. 13 */ 14 15 namespace BirdFont { 16 17 public class CmapTable : Table { 18 19 GlyfTable glyf_table; 20 CmapSubtableFormat4 cmap_format4 = new CmapSubtableFormat4 (); 21 22 public CmapTable(GlyfTable gt) { 23 glyf_table = gt; 24 id = "cmap"; 25 } 26 27 public unichar get_char (uint32 i) { 28 return get_prefered_table ().get_char (i) ; 29 } 30 31 CmapSubtableFormat4 get_prefered_table () { 32 return cmap_format4; 33 } 34 35 public override string get_id () { 36 return "cmap"; 37 } 38 39 public override void parse (FontData dis) throws GLib.Error { 40 uint16 version; 41 uint16 nsubtables; 42 43 uint16 platform; 44 uint16 encoding; 45 uint32 sub_offset; 46 47 return_if_fail (offset > 0 && length > 0); 48 49 dis.seek (offset); 50 51 version = dis.read_ushort (); 52 nsubtables = dis.read_ushort (); 53 54 printd (@"cmap version: $version\n"); 55 printd (@"cmap subtables: $nsubtables\n"); 56 57 if (version != 0) { 58 warning (@"Bad version for cmap table: $version expecting 0. Number of subtables: $nsubtables"); 59 return; 60 } 61 62 for (uint i = 0; i < nsubtables; i++) { 63 platform = dis.read_ushort (); 64 encoding = dis.read_ushort (); 65 sub_offset = dis.read_ulong (); 66 67 if (platform == 3 && encoding == 1) { 68 printd (@"Parsing Unicode BMP (UCS-2) Platform: $platform Encoding: $encoding\n"); 69 cmap_format4.offset = offset + sub_offset; 70 } else { 71 stderr.printf (@"Unknown cmap format. Platform: $platform Encoding: $encoding.\n"); 72 } 73 74 if (encoding == 3) { 75 stderr.printf ("Font contains a cmap table with the obsolete encoding 3.\n"); 76 } 77 } 78 79 if (cmap_format4.offset > 0) { 80 cmap_format4.parse (dis); 81 } else { 82 warning ("No cmap subtable4 found."); 83 } 84 } 85 86 /** Character to glyph mapping */ 87 public void process (GlyfTable glyf_table) throws GLib.Error { 88 FontData fd = new FontData (); 89 FontData cmap0_data; 90 FontData cmap4_data; 91 FontData cmap12_data; 92 CmapSubtableFormat0 cmap0 = new CmapSubtableFormat0 (); 93 CmapSubtableFormat4 cmap4 = new CmapSubtableFormat4 (); 94 CmapSubtableFormat12 cmap12 = new CmapSubtableFormat12 (); 95 uint16 n_encoding_tables; 96 97 cmap0_data = cmap0.get_cmap_data (glyf_table); 98 cmap4_data = cmap4.get_cmap_data (glyf_table); 99 cmap12_data = cmap12.get_cmap_data (glyf_table); 100 101 n_encoding_tables = 3; 102 103 fd.add_u16 (0); // table version 104 fd.add_u16 (n_encoding_tables); 105 106 fd.add_u16 (1); // platform 107 fd.add_u16 (0); // encoding 108 fd.add_ulong (28); // subtable offseet 109 110 fd.add_u16 (3); // platform 111 fd.add_u16 (1); // encoding (Format Unicode UCS-4) 112 fd.add_ulong (28 + cmap0_data.length ()); // subtable offseet 113 114 fd.add_u16 (3); // platform 115 fd.add_u16 (10); // encoding 116 fd.add_ulong (28 + cmap0_data.length () + cmap4_data.length ()); // subtable offseet 117 118 fd.append (cmap0_data); 119 fd.append (cmap4_data); 120 fd.append (cmap12_data); 121 122 // padding 123 fd.pad (); 124 125 this.font_data = fd; 126 } 127 } 128 129 /** Largest power of two less than max. */ 130 internal static uint16 largest_pow2 (uint16 max) { 131 uint16 x = 1; 132 uint16 l = 0; 133 134 while (x <= max) { 135 l = x; 136 x = x << 1; 137 } 138 139 return l; 140 } 141 142 /** Largest exponent for a power of two less than max. */ 143 internal static uint16 largest_pow2_exponent (uint16 max) { 144 uint16 exp = 0; 145 uint16 l = 0; 146 uint16 x = 0; 147 148 while (x <= max) { 149 l = exp; 150 exp++; 151 x = 1 << exp; 152 } 153 154 return l; 155 } 156 157 } 158