├── CmapTable.js ├── LICENSE ├── README.md ├── SfntTables.js ├── SvgTable.js ├── i-demo.otf ├── index.html ├── main.css └── main.js /CmapTable.js: -------------------------------------------------------------------------------- 1 | /* This Source Code Form is subject to the terms of the Mozilla Public 2 | * License, v. 2.0. If a copy of the MPL was not distributed with this file, 3 | * You can obtain one at http://mozilla.org/MPL/2.0/. */ 4 | 5 | // XXX this doesn't work yet 6 | 7 | function CmapTable() { 8 | return this; 9 | } 10 | 11 | function cmapFormat4CharsToGlyph(string, dataView) { 12 | if (string.length > 1) { 13 | return -1; 14 | } 15 | 16 | var charCode = string.charCodeAt(0); 17 | var segCount = dataView.getUint16(6) >> 1; 18 | var offset = 14; 19 | for (var i = 0; i < segCount; ++i) { 20 | if (dataView.getUint16(offset) >= charCode && 21 | dataView.getUint16(14 + segCount*2 + 2 + i*2) <= charCode) { 22 | return -1; // TODO fix 23 | } 24 | offset += 2; 25 | } 26 | } 27 | 28 | function cmapFormat6CharsToGlyph(string, dataView) { 29 | if (string.length > 1) { 30 | return -1; 31 | } 32 | 33 | return -1; // TODO fix 34 | } 35 | 36 | CmapTable.prototype.charsToGlyph = function (string) { 37 | var encodings = this.dataView.getUint16(2); 38 | if (4 + encodings*8 > this.dataView.byteLength) { 39 | throw "cmap table too short"; 40 | } 41 | 42 | var offset = 4; 43 | for (var i = 0; i < encodings; ++i) { 44 | var subtableOffset = this.dataView.getUint32(4 + i*8 + 4); 45 | if (subtableOffset + 4 > this.dataView.byteLength) { 46 | throw "cmap table too short"; 47 | } 48 | var format = this.dataView.getUint16(subtableOffset); 49 | switch (format) { 50 | case 4: 51 | var length = this.dataView.getUint16(subtableOffset + 2); 52 | if (subtableOffset + length > this.dataView.byteLength) { 53 | throw "cmap table too short"; 54 | } 55 | var glyphId = cmapFormat4CharsToGlyph(string, 56 | new DataView(this.dataView.buffer, this.dataView.byteOffset + subtableOffset, length)); 57 | if (glyphId >= 0) { 58 | return glyphId; 59 | } 60 | break; 61 | case 6: 62 | var length = this.dataView.getUint16(subtableOffset + 2); 63 | if (subtableOffset + length > this.dataView.byteLength) { 64 | throw "cmap table too short"; 65 | } 66 | var glyphId = cmapFormat6CharsToGlyph(string, 67 | new DataView(this.dataView.buffer, this.dataView.byteOffset + subtableOffset, length)); 68 | if (glyphId >= 0) { 69 | return glyphId; 70 | } 71 | break; 72 | default: throw "cmap format " + format + " not supported"; 73 | } 74 | offset += 8; 75 | } 76 | 77 | return -1; 78 | }; 79 | 80 | CmapTable.fromTable = function (dataView) { 81 | if (dataView.byteLength < 4) { 82 | throw "cmap table too short"; 83 | } 84 | if (dataView.getUint16(0) != 0) { 85 | throw "Unknown cmap table version"; 86 | } 87 | 88 | var table = new CmapTable(); 89 | table.dataView = dataView; 90 | return table; 91 | }; 92 | 93 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Mozilla Public License, version 2.0 2 | 3 | 1. Definitions 4 | 5 | 1.1. “Contributor” 6 | 7 | means each individual or legal entity that creates, contributes to the 8 | creation of, or owns Covered Software. 9 | 10 | 1.2. “Contributor Version” 11 | 12 | means the combination of the Contributions of others (if any) used by a 13 | Contributor and that particular Contributor’s Contribution. 14 | 15 | 1.3. “Contribution” 16 | 17 | means Covered Software of a particular Contributor. 18 | 19 | 1.4. “Covered Software” 20 | 21 | means Source Code Form to which the initial Contributor has attached the 22 | notice in Exhibit A, the Executable Form of such Source Code Form, and 23 | Modifications of such Source Code Form, in each case including portions 24 | thereof. 25 | 26 | 1.5. “Incompatible With Secondary Licenses” 27 | means 28 | 29 | a. that the initial Contributor has attached the notice described in 30 | Exhibit B to the Covered Software; or 31 | 32 | b. that the Covered Software was made available under the terms of version 33 | 1.1 or earlier of the License, but not also under the terms of a 34 | Secondary License. 35 | 36 | 1.6. “Executable Form” 37 | 38 | means any form of the work other than Source Code Form. 39 | 40 | 1.7. “Larger Work” 41 | 42 | means a work that combines Covered Software with other material, in a separate 43 | file or files, that is not Covered Software. 44 | 45 | 1.8. “License” 46 | 47 | means this document. 48 | 49 | 1.9. “Licensable” 50 | 51 | means having the right to grant, to the maximum extent possible, whether at the 52 | time of the initial grant or subsequently, any and all of the rights conveyed by 53 | this License. 54 | 55 | 1.10. “Modifications” 56 | 57 | means any of the following: 58 | 59 | a. any file in Source Code Form that results from an addition to, deletion 60 | from, or modification of the contents of Covered Software; or 61 | 62 | b. any new file in Source Code Form that contains any Covered Software. 63 | 64 | 1.11. “Patent Claims” of a Contributor 65 | 66 | means any patent claim(s), including without limitation, method, process, 67 | and apparatus claims, in any patent Licensable by such Contributor that 68 | would be infringed, but for the grant of the License, by the making, 69 | using, selling, offering for sale, having made, import, or transfer of 70 | either its Contributions or its Contributor Version. 71 | 72 | 1.12. “Secondary License” 73 | 74 | means either the GNU General Public License, Version 2.0, the GNU Lesser 75 | General Public License, Version 2.1, the GNU Affero General Public 76 | License, Version 3.0, or any later versions of those licenses. 77 | 78 | 1.13. “Source Code Form” 79 | 80 | means the form of the work preferred for making modifications. 81 | 82 | 1.14. “You” (or “Your”) 83 | 84 | means an individual or a legal entity exercising rights under this 85 | License. For legal entities, “You” includes any entity that controls, is 86 | controlled by, or is under common control with You. For purposes of this 87 | definition, “control” means (a) the power, direct or indirect, to cause 88 | the direction or management of such entity, whether by contract or 89 | otherwise, or (b) ownership of more than fifty percent (50%) of the 90 | outstanding shares or beneficial ownership of such entity. 91 | 92 | 93 | 2. License Grants and Conditions 94 | 95 | 2.1. Grants 96 | 97 | Each Contributor hereby grants You a world-wide, royalty-free, 98 | non-exclusive license: 99 | 100 | a. under intellectual property rights (other than patent or trademark) 101 | Licensable by such Contributor to use, reproduce, make available, 102 | modify, display, perform, distribute, and otherwise exploit its 103 | Contributions, either on an unmodified basis, with Modifications, or as 104 | part of a Larger Work; and 105 | 106 | b. under Patent Claims of such Contributor to make, use, sell, offer for 107 | sale, have made, import, and otherwise transfer either its Contributions 108 | or its Contributor Version. 109 | 110 | 2.2. Effective Date 111 | 112 | The licenses granted in Section 2.1 with respect to any Contribution become 113 | effective for each Contribution on the date the Contributor first distributes 114 | such Contribution. 115 | 116 | 2.3. Limitations on Grant Scope 117 | 118 | The licenses granted in this Section 2 are the only rights granted under this 119 | License. No additional rights or licenses will be implied from the distribution 120 | or licensing of Covered Software under this License. Notwithstanding Section 121 | 2.1(b) above, no patent license is granted by a Contributor: 122 | 123 | a. for any code that a Contributor has removed from Covered Software; or 124 | 125 | b. for infringements caused by: (i) Your and any other third party’s 126 | modifications of Covered Software, or (ii) the combination of its 127 | Contributions with other software (except as part of its Contributor 128 | Version); or 129 | 130 | c. under Patent Claims infringed by Covered Software in the absence of its 131 | Contributions. 132 | 133 | This License does not grant any rights in the trademarks, service marks, or 134 | logos of any Contributor (except as may be necessary to comply with the 135 | notice requirements in Section 3.4). 136 | 137 | 2.4. Subsequent Licenses 138 | 139 | No Contributor makes additional grants as a result of Your choice to 140 | distribute the Covered Software under a subsequent version of this License 141 | (see Section 10.2) or under the terms of a Secondary License (if permitted 142 | under the terms of Section 3.3). 143 | 144 | 2.5. Representation 145 | 146 | Each Contributor represents that the Contributor believes its Contributions 147 | are its original creation(s) or it has sufficient rights to grant the 148 | rights to its Contributions conveyed by this License. 149 | 150 | 2.6. Fair Use 151 | 152 | This License is not intended to limit any rights You have under applicable 153 | copyright doctrines of fair use, fair dealing, or other equivalents. 154 | 155 | 2.7. Conditions 156 | 157 | Sections 3.1, 3.2, 3.3, and 3.4 are conditions of the licenses granted in 158 | Section 2.1. 159 | 160 | 161 | 3. Responsibilities 162 | 163 | 3.1. Distribution of Source Form 164 | 165 | All distribution of Covered Software in Source Code Form, including any 166 | Modifications that You create or to which You contribute, must be under the 167 | terms of this License. You must inform recipients that the Source Code Form 168 | of the Covered Software is governed by the terms of this License, and how 169 | they can obtain a copy of this License. You may not attempt to alter or 170 | restrict the recipients’ rights in the Source Code Form. 171 | 172 | 3.2. Distribution of Executable Form 173 | 174 | If You distribute Covered Software in Executable Form then: 175 | 176 | a. such Covered Software must also be made available in Source Code Form, 177 | as described in Section 3.1, and You must inform recipients of the 178 | Executable Form how they can obtain a copy of such Source Code Form by 179 | reasonable means in a timely manner, at a charge no more than the cost 180 | of distribution to the recipient; and 181 | 182 | b. You may distribute such Executable Form under the terms of this License, 183 | or sublicense it under different terms, provided that the license for 184 | the Executable Form does not attempt to limit or alter the recipients’ 185 | rights in the Source Code Form under this License. 186 | 187 | 3.3. Distribution of a Larger Work 188 | 189 | You may create and distribute a Larger Work under terms of Your choice, 190 | provided that You also comply with the requirements of this License for the 191 | Covered Software. If the Larger Work is a combination of Covered Software 192 | with a work governed by one or more Secondary Licenses, and the Covered 193 | Software is not Incompatible With Secondary Licenses, this License permits 194 | You to additionally distribute such Covered Software under the terms of 195 | such Secondary License(s), so that the recipient of the Larger Work may, at 196 | their option, further distribute the Covered Software under the terms of 197 | either this License or such Secondary License(s). 198 | 199 | 3.4. Notices 200 | 201 | You may not remove or alter the substance of any license notices (including 202 | copyright notices, patent notices, disclaimers of warranty, or limitations 203 | of liability) contained within the Source Code Form of the Covered 204 | Software, except that You may alter any license notices to the extent 205 | required to remedy known factual inaccuracies. 206 | 207 | 3.5. Application of Additional Terms 208 | 209 | You may choose to offer, and to charge a fee for, warranty, support, 210 | indemnity or liability obligations to one or more recipients of Covered 211 | Software. However, You may do so only on Your own behalf, and not on behalf 212 | of any Contributor. You must make it absolutely clear that any such 213 | warranty, support, indemnity, or liability obligation is offered by You 214 | alone, and You hereby agree to indemnify every Contributor for any 215 | liability incurred by such Contributor as a result of warranty, support, 216 | indemnity or liability terms You offer. You may include additional 217 | disclaimers of warranty and limitations of liability specific to any 218 | jurisdiction. 219 | 220 | 4. Inability to Comply Due to Statute or Regulation 221 | 222 | If it is impossible for You to comply with any of the terms of this License 223 | with respect to some or all of the Covered Software due to statute, judicial 224 | order, or regulation then You must: (a) comply with the terms of this License 225 | to the maximum extent possible; and (b) describe the limitations and the code 226 | they affect. Such description must be placed in a text file included with all 227 | distributions of the Covered Software under this License. Except to the 228 | extent prohibited by statute or regulation, such description must be 229 | sufficiently detailed for a recipient of ordinary skill to be able to 230 | understand it. 231 | 232 | 5. Termination 233 | 234 | 5.1. The rights granted under this License will terminate automatically if You 235 | fail to comply with any of its terms. However, if You become compliant, 236 | then the rights granted under this License from a particular Contributor 237 | are reinstated (a) provisionally, unless and until such Contributor 238 | explicitly and finally terminates Your grants, and (b) on an ongoing basis, 239 | if such Contributor fails to notify You of the non-compliance by some 240 | reasonable means prior to 60 days after You have come back into compliance. 241 | Moreover, Your grants from a particular Contributor are reinstated on an 242 | ongoing basis if such Contributor notifies You of the non-compliance by 243 | some reasonable means, this is the first time You have received notice of 244 | non-compliance with this License from such Contributor, and You become 245 | compliant prior to 30 days after Your receipt of the notice. 246 | 247 | 5.2. If You initiate litigation against any entity by asserting a patent 248 | infringement claim (excluding declaratory judgment actions, counter-claims, 249 | and cross-claims) alleging that a Contributor Version directly or 250 | indirectly infringes any patent, then the rights granted to You by any and 251 | all Contributors for the Covered Software under Section 2.1 of this License 252 | shall terminate. 253 | 254 | 5.3. In the event of termination under Sections 5.1 or 5.2 above, all end user 255 | license agreements (excluding distributors and resellers) which have been 256 | validly granted by You or Your distributors under this License prior to 257 | termination shall survive termination. 258 | 259 | 6. Disclaimer of Warranty 260 | 261 | Covered Software is provided under this License on an “as is” basis, without 262 | warranty of any kind, either expressed, implied, or statutory, including, 263 | without limitation, warranties that the Covered Software is free of defects, 264 | merchantable, fit for a particular purpose or non-infringing. The entire 265 | risk as to the quality and performance of the Covered Software is with You. 266 | Should any Covered Software prove defective in any respect, You (not any 267 | Contributor) assume the cost of any necessary servicing, repair, or 268 | correction. This disclaimer of warranty constitutes an essential part of this 269 | License. No use of any Covered Software is authorized under this License 270 | except under this disclaimer. 271 | 272 | 7. Limitation of Liability 273 | 274 | Under no circumstances and under no legal theory, whether tort (including 275 | negligence), contract, or otherwise, shall any Contributor, or anyone who 276 | distributes Covered Software as permitted above, be liable to You for any 277 | direct, indirect, special, incidental, or consequential damages of any 278 | character including, without limitation, damages for lost profits, loss of 279 | goodwill, work stoppage, computer failure or malfunction, or any and all 280 | other commercial damages or losses, even if such party shall have been 281 | informed of the possibility of such damages. This limitation of liability 282 | shall not apply to liability for death or personal injury resulting from such 283 | party’s negligence to the extent applicable law prohibits such limitation. 284 | Some jurisdictions do not allow the exclusion or limitation of incidental or 285 | consequential damages, so this exclusion and limitation may not apply to You. 286 | 287 | 8. Litigation 288 | 289 | Any litigation relating to this License may be brought only in the courts of 290 | a jurisdiction where the defendant maintains its principal place of business 291 | and such litigation shall be governed by laws of that jurisdiction, without 292 | reference to its conflict-of-law provisions. Nothing in this Section shall 293 | prevent a party’s ability to bring cross-claims or counter-claims. 294 | 295 | 9. Miscellaneous 296 | 297 | This License represents the complete agreement concerning the subject matter 298 | hereof. If any provision of this License is held to be unenforceable, such 299 | provision shall be reformed only to the extent necessary to make it 300 | enforceable. Any law or regulation which provides that the language of a 301 | contract shall be construed against the drafter shall not be used to construe 302 | this License against a Contributor. 303 | 304 | 305 | 10. Versions of the License 306 | 307 | 10.1. New Versions 308 | 309 | Mozilla Foundation is the license steward. Except as provided in Section 310 | 10.3, no one other than the license steward has the right to modify or 311 | publish new versions of this License. Each version will be given a 312 | distinguishing version number. 313 | 314 | 10.2. Effect of New Versions 315 | 316 | You may distribute the Covered Software under the terms of the version of 317 | the License under which You originally received the Covered Software, or 318 | under the terms of any subsequent version published by the license 319 | steward. 320 | 321 | 10.3. Modified Versions 322 | 323 | If you create software not governed by this License, and you want to 324 | create a new license for such software, you may create and use a modified 325 | version of this License if you rename the license and remove any 326 | references to the name of the license steward (except to note that such 327 | modified license differs from this License). 328 | 329 | 10.4. Distributing Source Code Form that is Incompatible With Secondary Licenses 330 | If You choose to distribute Source Code Form that is Incompatible With 331 | Secondary Licenses under the terms of this version of the License, the 332 | notice described in Exhibit B of this License must be attached. 333 | 334 | Exhibit A - Source Code Form License Notice 335 | 336 | This Source Code Form is subject to the 337 | terms of the Mozilla Public License, v. 338 | 2.0. If a copy of the MPL was not 339 | distributed with this file, You can 340 | obtain one at 341 | http://mozilla.org/MPL/2.0/. 342 | 343 | If it is not possible or desirable to put the notice in a particular file, then 344 | You may include the notice in a location (such as a LICENSE file in a relevant 345 | directory) where a recipient would be likely to look for such a notice. 346 | 347 | You may add additional accurate notices of copyright ownership. 348 | 349 | Exhibit B - “Incompatible With Secondary Licenses” Notice 350 | 351 | This Source Code Form is “Incompatible 352 | With Secondary Licenses”, as defined by 353 | the Mozilla Public License, v. 2.0. 354 | 355 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | SVG OpenType Workshop 2 | ===================== 3 | 4 | [This Web application](https://rocallahan.github.io/svg-opentype-workshop/) lets you live-edit SVG glyphs and embeds those glyphs in an OpenType font, for rendering in a browser such as [Firefox](https://getfirefox.com) that supports SVG-in-OpenType fonts. 5 | -------------------------------------------------------------------------------- /SfntTables.js: -------------------------------------------------------------------------------- 1 | /* This Source Code Form is subject to the terms of the Mozilla Public 2 | * License, v. 2.0. If a copy of the MPL was not distributed with this file, 3 | * You can obtain one at http://mozilla.org/MPL/2.0/. */ 4 | 5 | function SfntTables(arrayBuffer) { 6 | this.tables = []; 7 | 8 | var view = new DataView(arrayBuffer); 9 | if (view.byteLength < 12) { 10 | throw "Invalid file length"; 11 | } 12 | this.version = view.getUint32(0); 13 | 14 | var numTables = view.getUint16(4); 15 | if (view.byteLength < 12 + numTables*16) { 16 | throw "Invalid file length"; 17 | } 18 | var offset = 12; 19 | for (var i = 0; i < numTables; ++i) { 20 | var tableOffset = view.getUint32(offset + 8); 21 | var tableLength = view.getUint32(offset + 12); 22 | if (tableOffset + tableLength > arrayBuffer.byteLength) { 23 | throw "Invalid table end: " + (tableOffset + tableLength); 24 | } 25 | this.tables.push({ 26 | tag:view.getUint32(offset), 27 | view:new DataView(arrayBuffer, tableOffset, tableLength) 28 | }); 29 | offset += 16; 30 | } 31 | return this; 32 | } 33 | 34 | SfntTables.createFromBlob = function (blob, onsuccess, onerror) { 35 | var reader = new FileReader(); 36 | reader.readAsArrayBuffer(blob); 37 | reader.onerror = function() { 38 | onerror("Read failed: " + reader.error.toString()); 39 | }; 40 | reader.onload = function() { 41 | var tables; 42 | try { 43 | tables = new SfntTables(reader.result); 44 | } catch (ex) { 45 | onerror("Invalid font: " + ex); 46 | return; 47 | } 48 | onsuccess(tables); 49 | }; 50 | }; 51 | 52 | function stringToSfntTag(str) { 53 | if (str.length != 4) { 54 | throw "Tag name must be 4 characters"; 55 | } 56 | var result = 0; 57 | for (var i = 0; i < 4; ++i) { 58 | var ch = str.charCodeAt(i); 59 | if (ch < 32 || ch > 126) { 60 | throw "Invalid character in tag"; 61 | } 62 | result += ch << (24 - i*8); 63 | } 64 | return result; 65 | } 66 | 67 | SfntTables.prototype.getTable = function (tag) { 68 | var sfntTag = stringToSfntTag(tag); 69 | for (var i = 0; i < this.tables.length; ++i) { 70 | if (this.tables[i].tag == sfntTag) { 71 | return this.tables[i].view; 72 | } 73 | } 74 | return null; 75 | }; 76 | 77 | SfntTables.prototype.hasTable = function (tag) { 78 | return this.getTable(tag) != null; 79 | }; 80 | 81 | SfntTables.prototype.setTable = function (tag, dataView) { 82 | var sfntTag = stringToSfntTag(tag); 83 | for (var i = 0; i < this.tables.length; ++i) { 84 | if (this.tables[i].tag == sfntTag) { 85 | this.tables[i].view = dataView; 86 | return; 87 | } 88 | } 89 | this.tables.push({ 90 | tag:sfntTag, 91 | view:dataView 92 | }); 93 | }; 94 | 95 | function checksumCombine(a, b) { 96 | return (a + b)&0xFFFFFFFF; 97 | } 98 | 99 | function calculateChecksum(view) { 100 | var result = 0; 101 | for (var offset = 0; offset < view.byteLength; offset += 4) { 102 | result = checksumCombine(result, view.getUint32(offset)); 103 | } 104 | return result; 105 | } 106 | 107 | SfntTables.prototype.toBlob = function () { 108 | var numTables = this.tables.length; 109 | if (numTables > 0x0FFF) { 110 | throw "Too many tables"; 111 | } 112 | 113 | this.tables.sort(function (a, b) { 114 | return a.tag - b.tag; 115 | }); 116 | 117 | var header = new ArrayBuffer(12 + numTables*16); 118 | var headerView = new DataView(header); 119 | headerView.setUint32(0, this.version); 120 | headerView.setUint16(4, numTables); 121 | for (var entrySelector = 1; ; ++entrySelector) { 122 | if ((1 << (entrySelector + 1)) > numTables) { 123 | break; 124 | } 125 | } 126 | var searchRange = 16 << entrySelector; 127 | headerView.setUint16(6, searchRange); 128 | headerView.setUint16(8, entrySelector); 129 | headerView.setUint16(10, numTables*16 - searchRange); 130 | 131 | var parts = []; 132 | parts.push(header); 133 | var offset = 12; 134 | var tableOffset = header.byteLength; 135 | var headSfntTag = stringToSfntTag('head'); 136 | var headTableView; 137 | var fileChecksum = 0; 138 | for (var i = 0; i < numTables; ++i) { 139 | var tag = this.tables[i].tag; 140 | headerView.setUint32(offset, tag); 141 | var tableView = this.tables[i].view; 142 | var paddedTableView = tableView; 143 | if (tableView.byteLength & 3) { 144 | var buf = new ArrayBuffer((tableView.byteLength + 3) & ~3); 145 | (new Uint8Array(buf, 0, tableView.byteLength)).set( 146 | new Uint8Array(tableView.buffer, tableView.byteOffset, tableView.byteLength)); 147 | paddedTableView = new DataView(buf); 148 | } 149 | if (tag == headSfntTag) { 150 | headTableView = paddedTableView; 151 | // Temporarily clear checkSumAdjustment while computing table checksum 152 | headTableView.setUint32(8, 0); 153 | } 154 | var checksum = calculateChecksum(paddedTableView); 155 | headerView.setUint32(offset + 4, checksum); 156 | headerView.setUint32(offset + 8, tableOffset); 157 | headerView.setUint32(offset + 12, tableView.byteLength); 158 | offset += 16; 159 | parts.push(paddedTableView); 160 | fileChecksum = checksumCombine(fileChecksum, checksum); 161 | tableOffset += paddedTableView.byteLength; 162 | if (tableOffset > 0xFFFFFFFF) { 163 | throw "File size overflow"; 164 | } 165 | } 166 | fileChecksum = checksumCombine(fileChecksum, calculateChecksum(headerView)); 167 | 168 | if (headTableView) { 169 | headTableView.setUint32(8, (0xB1B0AFBA - fileChecksum)&0xFFFFFFFF); 170 | } 171 | 172 | return new Blob(parts); 173 | }; 174 | 175 | -------------------------------------------------------------------------------- /SvgTable.js: -------------------------------------------------------------------------------- 1 | /* This Source Code Form is subject to the terms of the Mozilla Public 2 | * License, v. 2.0. If a copy of the MPL was not distributed with this file, 3 | * You can obtain one at http://mozilla.org/MPL/2.0/. */ 4 | 5 | function SvgTable() { 6 | this.documents = []; 7 | return this; 8 | } 9 | 10 | SvgTable.prototype.getDocumentCount = function() { 11 | return this.documents.length; 12 | }; 13 | 14 | SvgTable.prototype.getDocumentText = function (index) { 15 | if (index >= this.documents.length) { 16 | throw "Invalid document index"; 17 | } 18 | return this.documents[index].text; 19 | }; 20 | 21 | SvgTable.prototype.toArrayBuffer = function () { 22 | var numDocuments = this.documents.length; 23 | if (numDocuments > 0xFFFF) { 24 | throw "Too many documents"; 25 | } 26 | var docIndexOffset = 10; 27 | var docOffset = 2 + 12*numDocuments; 28 | var encodedTexts = []; 29 | var encoder = new TextEncoder(); 30 | var docOffsets = []; 31 | var docLengths = []; 32 | for (var i = 0; i < numDocuments; ++i) { 33 | for (var j = 0; j < i; ++j) { 34 | if (this.documents[j].text == this.documents[i].text) { 35 | break; 36 | } 37 | } 38 | if (j < i) { 39 | // Reuse existing document text 40 | docOffsets.push(docOffsets[j]); 41 | docLengths.push(docLengths[j]); 42 | encodedTexts.push(null); 43 | } else { 44 | docOffsets.push(docOffset); 45 | encodedTexts.push(encoder.encode(this.documents[i].text)); 46 | docOffset += encodedTexts[i].byteLength; 47 | docLengths.push(encodedTexts[i].byteLength); 48 | } 49 | } 50 | if (length > 0xFFFFFFFF) { 51 | throw "Table size overflow"; 52 | } 53 | var buf = new ArrayBuffer(docIndexOffset + docOffset); 54 | var headerView = new DataView(buf); 55 | headerView.setUint16(0, 0); 56 | headerView.setUint32(2, docIndexOffset); 57 | headerView.setUint32(6, 0); 58 | headerView.setUint16(docIndexOffset, numDocuments); 59 | var offset = docIndexOffset + 2; 60 | for (var i = 0; i < numDocuments; ++i) { 61 | headerView.setUint16(offset, this.documents[i].startGlyphId); 62 | headerView.setUint16(offset + 2, this.documents[i].endGlyphId); 63 | headerView.setUint32(offset + 4, docOffsets[i]); 64 | var textLength = docLengths[i]; 65 | headerView.setUint32(offset + 8, textLength); 66 | if (encodedTexts[i]) { 67 | (new Uint8Array(buf, docIndexOffset + docOffsets[i], textLength)).set(encodedTexts[i]); 68 | } 69 | offset += 12; 70 | } 71 | return buf; 72 | }; 73 | 74 | SvgTable.fromDocuments = function (documents, options) { 75 | if (documents.length == 0) { 76 | throw "Must have at least one document"; 77 | } 78 | 79 | var glyphMapping = []; 80 | var parser = new DOMParser(); 81 | for (var i = 0; i < documents.length; ++i) { 82 | var doc = parser.parseFromString(documents[i], "image/svg+xml"); 83 | var parseErrs = doc.getElementsByTagName("parsererror"); 84 | if (parseErrs.length > 0) { 85 | throw "Parse error: " + parseErrs[0].textContent; 86 | } 87 | 88 | var elementsWithGlyphIds = doc.querySelectorAll("[id]"); 89 | var glyphIds = []; 90 | for (var j = 0; j < elementsWithGlyphIds.length; ++j) { 91 | var m = /^glyph([1-9][0-9]*)$/.exec(elementsWithGlyphIds[j].getAttribute("id")); 92 | if (m) { 93 | glyphMapping.push({id:m[1], doc:documents[i]}); 94 | } 95 | } 96 | } 97 | 98 | if (glyphMapping.length == 0) { 99 | throw "No elements with id 'glyphNNN' found"; 100 | } 101 | 102 | glyphMapping.sort(function (a, b) { 103 | return a.id - b.id; 104 | }); 105 | 106 | var table = new SvgTable(); 107 | var startOfRun = 0; 108 | for (var j = 0; j < glyphMapping.length; ++j) { 109 | if (j > 0 && glyphMapping[j].id == glyphMapping[j - 1].id) { 110 | if (options && options.ignoreDuplicateGlyphIds) { 111 | glyphMapping.splice(j, 1); 112 | continue; 113 | } 114 | throw "Duplicate glyphs found for glyph ID " + glyphMapping[j].id; 115 | } 116 | if (j == glyphMapping.length - 1 || 117 | glyphMapping[j + 1].id != glyphMapping[j].id + 1 || 118 | glyphMapping[j + 1].doc != glyphMapping[j].doc) { 119 | // End of a run 120 | table.documents.push({ 121 | startGlyphId:glyphMapping[startOfRun].id, 122 | endGlyphId:glyphMapping[j].id, 123 | text:glyphMapping[j].doc 124 | }); 125 | startOfRun = j + 1; 126 | } 127 | } 128 | 129 | return table; 130 | }; 131 | 132 | SvgTable.fromTable = function (dataView) { 133 | if (dataView.getUint16(0) != 0) { 134 | throw "Unknown table version"; 135 | } 136 | if (dataView.getUint32(6) != 0) { 137 | throw "Color palettes not supported yet"; 138 | } 139 | var docIndexOffset = dataView.getUint32(2); 140 | if (docIndexOffset + 2 > dataView.byteLength) { 141 | throw "Document index out of range"; 142 | } 143 | var numEntries = dataView.getUint16(docIndexOffset); 144 | if (docIndexOffset + 2 + numEntries*12 > dataView.byteLength) { 145 | throw "Document index out of range"; 146 | } 147 | if (numEntries == 0) { 148 | throw "Must have at least one document"; 149 | } 150 | 151 | var table = new SvgTable(); 152 | var offset = docIndexOffset + 2; 153 | var textDecoder = new TextDecoder(); 154 | for (var i = 0; i < numEntries; ++i) { 155 | var startGlyphId = dataView.getUint16(offset); 156 | var endGlyphId = dataView.getUint16(offset + 2); 157 | if (startGlyphId > endGlyphId) { 158 | throw "Misordered glyph ids"; 159 | } 160 | if (i > 0 && table.documents[i - 1].endGlyphId >= startGlyphId) { 161 | throw "Misordered glyph ids with previous document"; 162 | } 163 | var docOffset = dataView.getUint32(offset + 4); 164 | var docLength = dataView.getUint32(offset + 8); 165 | if (docOffset + docLength > dataView.byteLength) { 166 | throw "Document end out of range"; 167 | } 168 | var docText = textDecoder.decode( 169 | new DataView(dataView.buffer, dataView.byteOffset + docIndexOffset + docOffset, docLength)); 170 | table.documents.push({ 171 | startGlyphId:startGlyphId, 172 | endGlyphId:endGlyphId, 173 | text:docText 174 | }); 175 | offset += 12; 176 | } 177 | 178 | return table; 179 | }; 180 | 181 | -------------------------------------------------------------------------------- /i-demo.otf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rocallahan/svg-opentype-workshop/7655e8c2ff27bd7213a3c8fb6076a02d8878b0a3/i-demo.otf -------------------------------------------------------------------------------- /index.html: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 |
7 |Load file: 17 |
SVG document: 18 | 19 |
20 |
21 |22 |
Sample text: 23 |
24 |Save generated font 25 | 26 | 27 | -------------------------------------------------------------------------------- /main.css: -------------------------------------------------------------------------------- 1 | /* This Source Code Form is subject to the terms of the Mozilla Public 2 | * License, v. 2.0. If a copy of the MPL was not distributed with this file, 3 | * You can obtain one at http://mozilla.org/MPL/2.0/. */ 4 | 5 | #sample { 6 | font-family:SampleFont; 7 | font-size:50px; 8 | padding:20px; 9 | height:200px; 10 | } 11 | .editable { 12 | white-space:pre; 13 | margin:20px; 14 | border:2px solid black; 15 | display:block; 16 | } 17 | .svgDoc { 18 | font-family:monospace; 19 | height:200px; 20 | overflow:auto; 21 | width:80%; 22 | } 23 | #svgTextError { 24 | display:none; 25 | } 26 | #svgTextError.error { 27 | display:block; 28 | color:red; 29 | } 30 | 31 | -------------------------------------------------------------------------------- /main.js: -------------------------------------------------------------------------------- 1 | /* This Source Code Form is subject to the terms of the Mozilla Public 2 | * License, v. 2.0. If a copy of the MPL was not distributed with this file, 3 | * You can obtain one at http://mozilla.org/MPL/2.0/. */ 4 | 5 | // An SfntTables object representing the current font data, or null/undefined if 6 | // nothing is loaded yet. 7 | var currentTables; 8 | // A Blob URL referencing the Blob form of the current font data, or null/undefined 9 | // if nothing is loaded yet. 10 | var blobURL; 11 | 12 | // Update the current font to include an "SVG " table with the contents of the 13 | // SVG documents, reporting errors if necessary. 14 | function updateFont() { 15 | svgTextError.classList.remove("error"); 16 | inlineStyle.textContent = ""; 17 | download.removeAttribute("href"); 18 | 19 | if (blobURL) { 20 | URL.revokeObjectURL(blobURL); 21 | blobURL = null; 22 | } 23 | 24 | if (!currentTables) { 25 | return; 26 | } 27 | 28 | var svgTable; 29 | try { 30 | var docs = []; 31 | for (var c = docContainer.firstChild; c; c = c.nextSibling) { 32 | docs.push(c.value); 33 | } 34 | var options = {ignoreDuplicateGlyphIds:ignoreDuplicateGlyphsCheckbox.checked}; 35 | svgTable = SvgTable.fromDocuments(docs, options); 36 | } catch (ex) { 37 | svgTextError.classList.add("error"); 38 | svgTextError.textContent = "Invalid SVG: " + ex; 39 | } 40 | 41 | if (svgTable) { 42 | currentTables.setTable("SVG ", new DataView(svgTable.toArrayBuffer())); 43 | } 44 | blobURL = URL.createObjectURL(currentTables.toBlob()); 45 | inlineStyle.textContent = 46 | "@font-face { font-family:SampleFont; src:url('" + blobURL + "'); }"; 47 | download.href = blobURL; 48 | } 49 | 50 | // Set up the current font by reading the contents of the currently selected file 51 | function readFile() { 52 | currentTables = null; 53 | docContainer.textContent = ""; 54 | 55 | var files = inputFile.files; 56 | if (files.length == 0) { 57 | updateFont(); 58 | return; 59 | } 60 | 61 | SfntTables.createFromBlob(files[0], function (tables) { 62 | currentTables = tables; 63 | if (currentTables.hasTable("SVG ")) { 64 | var svgTable; 65 | try { 66 | svgTable = SvgTable.fromTable(tables.getTable("SVG ")); 67 | } catch (ex) { 68 | alert("Malformed SVG table: " + ex); 69 | } 70 | if (svgTable) { 71 | for (var i = 0; i < svgTable.getDocumentCount(); ++i) { 72 | addDocument(svgTable.getDocumentText(i)); 73 | } 74 | } else { 75 | addDocument(""); 76 | } 77 | } 78 | updateFont(); 79 | }, function (error) { 80 | alert("Cannot parse font: " + error); 81 | }); 82 | } 83 | 84 | // Fill in first SVG document with a useful initial template 85 | function createTemplate() { 86 | if (docContainer.firstChild) { 87 | docContainer.firstChild.value = 88 | ""; 91 | } 92 | updateFont(); 93 | } 94 | 95 | function addDocument(value) { 96 | var textArea = document.createElement("textarea"); 97 | textArea.setAttribute("spellcheck", "false"); 98 | textArea.setAttribute("class", "editable svgDoc"); 99 | textArea.value = value; 100 | docContainer.appendChild(textArea); 101 | updateFont(); 102 | } 103 | 104 | function init() { 105 | inputFile.addEventListener("change", readFile); 106 | docContainer.addEventListener("input", updateFont); 107 | createTemplateButton.addEventListener("click", createTemplate); 108 | addDocumentButton.addEventListener("click", function() { addDocument(""); }); 109 | ignoreDuplicateGlyphsCheckbox.addEventListener("change", updateFont); 110 | 111 | var sampleText = []; 112 | for (var i = 32; i <= 127; ++i) { 113 | sampleText.push(String.fromCharCode(i)); 114 | if (i%32 == 31) { 115 | sampleText.push('\n'); 116 | } 117 | } 118 | sample.textContent = sampleText.join(''); 119 | 120 | readFile(); 121 | } 122 | 123 | window.addEventListener("DOMContentLoaded", init); 124 | --------------------------------------------------------------------------------