Best JavaScript code snippet using wpt
fonts_utils.js
Source:fonts_utils.js
1/* -*- Mode: Java; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */2/* vim: set shiftwidth=2 tabstop=2 autoindent cindent expandtab: */3/* Copyright 2012 Mozilla Foundation4 *5 * Licensed under the Apache License, Version 2.0 (the "License");6 * you may not use this file except in compliance with the License.7 * You may obtain a copy of the License at8 *9 * http://www.apache.org/licenses/LICENSE-2.010 *11 * Unless required by applicable law or agreed to in writing, software12 * distributed under the License is distributed on an "AS IS" BASIS,13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.14 * See the License for the specific language governing permissions and15 * limitations under the License.16 */17/* globals CFFDictDataMap, CFFDictPrivateDataMap, CFFEncodingMap, CFFStrings,18 Components, Dict, dump, error, isNum, log, netscape, Stream */19'use strict';20/*21 * The Type2 reader code below is only used for debugging purpose since Type222 * is only a CharString format and is never used directly as a Font file.23 *24 * So the code here is useful for dumping the data content of a .cff file in25 * order to investigate the similarity between a Type1 CharString and a Type226 * CharString or to understand the structure of the CFF format.27 */28/*29 * Build a charset by assigning the glyph name and the human readable form30 * of the glyph data.31 */32function readCharset(aStream, aCharstrings) {33 var charset = {};34 var format = aStream.getByte();35 var count = aCharstrings.length - 1;36 if (format === 0) {37 charset['.notdef'] = readCharstringEncoding(aCharstrings[0]);38 for (var i = 1; i < count + 1; i++) {39 var sid = aStream.getByte() << 8 | aStream.getByte();40 charset[CFFStrings[sid]] = readCharstringEncoding(aCharstrings[i]);41 //log(CFFStrings[sid] + "::" + charset[CFFStrings[sid]]);42 }43 } else if (format == 1) {44 for (var i = 1; i < count + 1; i++) {45 var first = aStream.getByte();46 first = (first << 8) | aStream.getByte();47 var numLeft = aStream.getByte();48 for (var j = 0; j <= numLeft; j++) {49 var sid = first++;50 if (CFFStrings[sid] == 'three')51 log(aCharstrings[j]);52 charset[CFFStrings[sid]] = readCharstringEncoding(aCharstrings[j]);53 }54 }55 } else {56 error('Invalid charset format');57 }58 return charset;59}60/*61 * Take a Type2 binary charstring as input and transform it to a human62 * readable representation as specified by the 'The Type 2 Charstring Format',63 * chapter 3.1.64 */65function readCharstringEncoding(aString) {66 if (!aString)67 return '';68 var charstringTokens = [];69 var count = aString.length;70 for (var i = 0; i < count; ) {71 var value = aString[i++];72 var token = null;73 if (value < 0) {74 continue;75 } else if (value <= 11) {76 token = CFFEncodingMap[value];77 } else if (value == 12) {78 token = CFFEncodingMap[value][aString[i++]];79 } else if (value <= 18) {80 token = CFFEncodingMap[value];81 } else if (value <= 20) {82 var mask = aString[i++];83 token = CFFEncodingMap[value];84 } else if (value <= 27) {85 token = CFFEncodingMap[value];86 } else if (value == 28) {87 token = aString[i++] << 8 | aString[i++];88 } else if (value <= 31) {89 token = CFFEncodingMap[value];90 } else if (value < 247) {91 token = parseInt(value, 10) - 139;92 } else if (value < 251) {93 token = (value - 247) * 256 + aString[i++] + 108;94 } else if (value < 255) {95 token = -(value - 251) * 256 - aString[i++] - 108;96 } else {// value == 25597 token = aString[i++] << 24 | aString[i++] << 16 |98 aString[i++] << 8 | aString[i];99 }100 charstringTokens.push(token);101 }102 return charstringTokens;103}104/*105 * Take a binary DICT Data as input and transform it into a human readable106 * form as specified by 'The Compact Font Format Specification', chapter 5.107 */108function readFontDictData(aString, aMap) {109 var fontDictDataTokens = [];110 var count = aString.length;111 for (var i = 0; i < count; i) {112 var value = aString[i++];113 var token = null;114 if (value == 12) {115 token = aMap[value][aString[i++]];116 } else if (value == 28) {117 token = aString[i++] << 8 | aString[i++];118 } else if (value == 29) {119 token = aString[i++] << 24 |120 aString[i++] << 16 |121 aString[i++] << 8 |122 aString[i++];123 } else if (value == 30) {124 token = '';125 var parsed = false;126 while (!parsed) {127 var octet = aString[i++];128 var nibbles = [parseInt(octet / 16, 10), parseInt(octet % 16, 10)];129 for (var j = 0; j < nibbles.length; j++) {130 var nibble = nibbles[j];131 switch (nibble) {132 case 0xA:133 token += '.';134 break;135 case 0xB:136 token += 'E';137 break;138 case 0xC:139 token += 'E-';140 break;141 case 0xD:142 break;143 case 0xE:144 token += '-';145 break;146 case 0xF:147 parsed = true;148 break;149 default:150 token += nibble;151 break;152 }153 }154 }155 token = parseFloat(token);156 } else if (value <= 31) {157 token = aMap[value];158 } else if (value <= 246) {159 token = parseInt(value, 10) - 139;160 } else if (value <= 250) {161 token = (value - 247) * 256 + aString[i++] + 108;162 } else if (value <= 254) {163 token = -(value - 251) * 256 - aString[i++] - 108;164 } else if (value == 255) {165 error('255 is not a valid DICT command');166 }167 fontDictDataTokens.push(token);168 }169 return fontDictDataTokens;170}171/*172 * Take a stream as input and return an array of objects.173 * In CFF an INDEX is a structure with the following format:174 * {175 * count: 2 bytes (Number of objects stored in INDEX),176 * offsize: 1 byte (Offset array element size),177 * offset: [count + 1] bytes (Offsets array),178 * data: - (Objects data)179 * }180 *181 * More explanation are given in the 'CFF Font Format Specification',182 * chapter 5.183 */184function readFontIndexData(aStream, aIsByte) {185 var count = aStream.getByte() << 8 | aStream.getByte();186 var offsize = aStream.getByte();187 function getNextOffset() {188 switch (offsize) {189 case 0:190 return 0;191 case 1:192 return aStream.getByte();193 case 2:194 return aStream.getByte() << 8 | aStream.getByte();195 case 3:196 return aStream.getByte() << 16 | aStream.getByte() << 8 |197 aStream.getByte();198 case 4:199 return aStream.getByte() << 24 | aStream.getByte() << 16 |200 aStream.getByte() << 8 | aStream.getByte();201 }202 error(offsize + ' is not a valid offset size');203 return null;204 }205 var offsets = [];206 for (var i = 0; i < count + 1; i++)207 offsets.push(getNextOffset());208 dump('Found ' + count + ' objects at offsets :' +209 offsets + ' (offsize: ' + offsize + ')');210 // Now extract the objects211 var relativeOffset = aStream.pos;212 var objects = [];213 for (var i = 0; i < count; i++) {214 var offset = offsets[i];215 aStream.pos = relativeOffset + offset - 1;216 var data = [];217 var length = offsets[i + 1] - 1;218 for (var j = offset - 1; j < length; j++)219 data.push(aIsByte ? aStream.getByte() : aStream.getChar());220 objects.push(data);221 }222 return objects;223}224var Type2Parser = function type2Parser(aFilePath) {225 var font = new Dict();226 var xhr = new XMLHttpRequest();227 xhr.open('GET', aFilePath, false);228 xhr.mozResponseType = xhr.responseType = 'arraybuffer';229 xhr.expected = (document.URL.indexOf('file:') === 0) ? 0 : 200;230 xhr.send(null);231 this.data = new Stream(xhr.mozResponseArrayBuffer || xhr.mozResponse ||232 xhr.responseArrayBuffer || xhr.response);233 // Turn on this flag for additional debugging logs234 var debug = false;235 function dump(aStr) {236 if (debug)237 log(aStr);238 }239 function parseAsToken(aString, aMap) {240 var decoded = readFontDictData(aString, aMap);241 var stack = [];242 var count = decoded.length;243 for (var i = 0; i < count; i++) {244 var token = decoded[i];245 if (isNum(token)) {246 stack.push(token);247 } else {248 switch (token.operand) {249 case 'SID':250 font.set(token.name, CFFStrings[stack.pop()]);251 break;252 case 'number number':253 font.set(token.name, {254 offset: stack.pop(),255 size: stack.pop()256 });257 break;258 case 'boolean':259 font.set(token.name, stack.pop());260 break;261 case 'delta':262 font.set(token.name, stack.pop());263 break;264 default:265 if (token.operand && token.operand.length) {266 var array = [];267 for (var j = 0; j < token.operand.length; j++)268 array.push(stack.pop());269 font.set(token.name, array);270 } else {271 font.set(token.name, stack.pop());272 }273 break;274 }275 }276 }277 }278 this.parse = function type2ParserParse(aStream) {279 font.set('major', aStream.getByte());280 font.set('minor', aStream.getByte());281 font.set('hdrSize', aStream.getByte());282 font.set('offsize', aStream.getByte());283 // Read the NAME Index284 dump('Reading Index: Names');285 font.set('Names', readFontIndexData(aStream));286 dump('Names: ' + font.get('Names'));287 // Read the Top Dict Index288 dump('Reading Index: TopDict');289 var topDict = readFontIndexData(aStream, true);290 dump('TopDict: ' + topDict);291 // Read the String Index292 dump('Reading Index: Strings');293 var strings = readFontIndexData(aStream);294 dump('strings: ' + strings);295 // Fill up the Strings dictionary with the new unique strings296 for (var i = 0; i < strings.length; i++)297 CFFStrings.push(strings[i].join(''));298 // Parse the TopDict operator299 var objects = [];300 var count = topDict.length;301 for (var i = 0; i < count; i++)302 parseAsToken(topDict[i], CFFDictDataMap);303 // Read the Global Subr Index that comes just after the Strings Index304 // (cf. "The Compact Font Format Specification" Chapter 16)305 dump('Reading Global Subr Index');306 var subrs = readFontIndexData(aStream, true);307 dump(subrs);308 // Reading Private Dict309 var priv = font.get('Private');310 dump('Reading Private Dict (offset: ' + priv.offset +311 ' size: ' + priv.size + ')');312 aStream.pos = priv.offset;313 var privateDict = [];314 for (var i = 0; i < priv.size; i++)315 privateDict.push(aStream.getByte());316 dump('privateData:' + privateDict);317 parseAsToken(privateDict, CFFDictPrivateDataMap);318 for (var p in font.map)319 dump(p + '::' + font.get(p));320 // Read CharStrings Index321 var charStringsOffset = font.get('CharStrings');322 dump('Read CharStrings Index (offset: ' + charStringsOffset + ')');323 aStream.pos = charStringsOffset;324 var charStrings = readFontIndexData(aStream, true);325 // Read Charset326 dump('Read Charset for ' + charStrings.length + ' glyphs');327 var charsetEntry = font.get('charset');328 if (charsetEntry === 0) {329 error('Need to support CFFISOAdobeCharset');330 } else if (charsetEntry == 1) {331 error('Need to support CFFExpert');332 } else if (charsetEntry == 2) {333 error('Need to support CFFExpertSubsetCharset');334 } else {335 aStream.pos = charsetEntry;336 var charset = readCharset(aStream, charStrings);337 }338 };339};340/*341 * To try the Type2 decoder on a local file in the current directory:342 *343 * var cff = new Type2Parser("file.cff");344 * cff.parse(this.data);345 *346 * To try the Type2 decoder on a custom built CFF array:347 *348 * var file = new Uint8Array(cffFileArray, 0, cffFileSize);349 * var parser = new Type2Parser();350 * parser.parse(new Stream(file));351 *352 */353/*354 * Write to a file to the disk (works only on Firefox in privilege mode)355 * but this is useful for dumping a font file to the disk and check with356 * fontforge or the ots program what's wrong with the file.357 *358 * writeToFile(fontData, "/tmp/pdf.js." + fontCount + ".cff");359 */360function writeToFile(aBytes, aFilePath) {361 if (!('netscape' in window))362 return;363 netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect');364 var Cc = Components.classes,365 Ci = Components.interfaces;366 var file = Cc['@mozilla.org/file/local;1'].createInstance(Ci.nsILocalFile);367 file.initWithPath(aFilePath);368 var stream = Cc['@mozilla.org/network/file-output-stream;1']369 .createInstance(Ci.nsIFileOutputStream);370 stream.init(file, 0x04 | 0x08 | 0x20, 0x180, 0);371 var bos = Cc['@mozilla.org/binaryoutputstream;1']372 .createInstance(Ci.nsIBinaryOutputStream);373 bos.setOutputStream(stream);374 bos.writeByteArray(aBytes, aBytes.length);375 stream.close();...
font_spec.js
Source:font_spec.js
...94 0x00, // format95 0x00, 0x02 // sid/cid96 ]);97 parser.bytes = bytes;98 var charset = parser.parseCharsets(3, 2, new CFFStrings(), false);99 expect(charset.charset[1]).toEqual('exclam');100 // CID font101 var charset = parser.parseCharsets(3, 2, new CFFStrings(), true);102 expect(charset.charset[1]).toEqual(2);103 });104 it('parses charset format 1', function() {105 // The first three bytes make the offset large enough to skip predefined.106 var bytes = new Uint8Array([0x00, 0x00, 0x00,107 0x01, // format108 0x00, 0x08, // sid/cid start109 0x01 // sid/cid left110 ]);111 parser.bytes = bytes;112 var charset = parser.parseCharsets(3, 2, new CFFStrings(), false);113 expect(charset.charset).toEqual(['.notdef', 'quoteright', 'parenleft']);114 // CID font115 var charset = parser.parseCharsets(3, 2, new CFFStrings(), true);116 expect(charset.charset).toEqual(['.notdef', 8, 9]);117 });118 it('parses charset format 2', function() {119 // format 2 is the same as format 1 but the left is card16120 // The first three bytes make the offset large enough to skip predefined.121 var bytes = new Uint8Array([0x00, 0x00, 0x00,122 0x02, // format123 0x00, 0x08, // sid/cid start124 0x00, 0x01 // sid/cid left125 ]);126 parser.bytes = bytes;127 var charset = parser.parseCharsets(3, 2, new CFFStrings(), false);128 expect(charset.charset).toEqual(['.notdef', 'quoteright', 'parenleft']);129 // CID font130 var charset = parser.parseCharsets(3, 2, new CFFStrings(), true);131 expect(charset.charset).toEqual(['.notdef', 8, 9]);132 });133 it('parses encoding format 0', function() {134 // The first two bytes make the offset large enough to skip predefined.135 var bytes = new Uint8Array([0x00, 0x00,136 0x00, // format137 0x01, // count138 0x08 // start139 ]);140 parser.bytes = bytes;141 var encoding = parser.parseEncoding(2, {}, new CFFStrings(), null);142 expect(encoding.encoding).toEqual({0x8: 1});143 });144 it('parses encoding format 1', function() {145 // The first two bytes make the offset large enough to skip predefined.146 var bytes = new Uint8Array([0x00, 0x00,147 0x01, // format148 0x01, // num ranges149 0x07, // range1 start150 0x01 // range2 left151 ]);152 parser.bytes = bytes;153 var encoding = parser.parseEncoding(2, {}, new CFFStrings(), null);154 expect(encoding.encoding).toEqual({0x7: 0x01, 0x08: 0x02});155 });156 it('parses fdselect format 0', function() {157 var bytes = new Uint8Array([0x00, // format158 0x00, // gid: 0 fd: 0159 0x01 // gid: 1 fd: 1160 ]);161 parser.bytes = bytes;162 var fdSelect = parser.parseFDSelect(0, 2);163 expect(fdSelect.fdSelect).toEqual([0, 1]);164 });165 it('parses fdselect format 3', function() {166 var bytes = new Uint8Array([0x03, // format167 0x00, 0x02, // range count...
Using AI Code Generation
1var wptools = require('wptools');2var fs = require('fs');3var options = {4};5var page = wptools.page('Wikipedia:WikiProject_United_States/Template:US_counties', options);6page.get(function(err, resp) {7 var data = resp.data;8 var CFF = data.CFFStrings;9 var CFFarray = CFF.split("|");10 console.log(CFFarray);11});12var wptools = require('wptools');13var fs = require('fs');14var options = {15};16var page = wptools.page('Wikipedia:WikiProject_United_States/Template:US_counties', options);17page.get(function(err, resp) {18 var data = resp.data;19 var CFF = data.CFFStrings;20 var CFFarray = CFF.split("|");21 console.log(CFFarray);22});23var wptools = require('wptools');24var fs = require('fs');25var options = {26};27var page = wptools.page('Wikipedia:WikiProject_United_States/Template:US_counties', options);28page.get(function(err, resp) {29 var data = resp.data;30 var CFF = data.CFFStrings;31 var CFFarray = CFF.split("|");32 console.log(CFFarray);33});34var wptools = require('wptools');35var fs = require('fs');36var options = {37};38var page = wptools.page('Wikipedia:WikiProject_United_States/Template:US_counties', options);39page.get(function(err, resp) {40 var data = resp.data;41 var CFF = data.CFFStrings;
Using AI Code Generation
1var wptools = require('wptools');2page.get(function(err, resp) {3 console.log(resp);4});5var wptools = require('wptools');6page.get(function(err, resp) {7 console.log(resp);8});9var wptools = require('wptools');10page.get(function(err, resp) {11 console.log(resp);12});13var wptools = require('wptools');14page.get(function(err, resp) {15 console.log(resp);16});17var wptools = require('wptools');18page.get(function(err, resp) {19 console.log(resp);20});21var wptools = require('wptools');22page.get(function(err, resp) {23 console.log(resp);24});25var wptools = require('wptools');26page.get(function(err, resp)
Using AI Code Generation
1var cffStrings = require('cff-strings');2var str = cffStrings.wptexturize("Hello World");3console.log(str);4var cffStrings = require('cff-strings');5var str = cffStrings.wptexturize("Hello World");6console.log(str);7var cffStrings = require('cff-strings');8var str = cffStrings.wptexturize("Hello World");9console.log(str);10var cffStrings = require('cff-strings');11var str = cffStrings.wptexturize("Hello World");12console.log(str);13var cffStrings = require('cff-strings');14var str = cffStrings.wptexturize("Hello World");15console.log(str);16var cffStrings = require('cff-strings');17var str = cffStrings.wptexturize("Hello World");18console.log(str);19var cffStrings = require('cff-strings');20var str = cffStrings.wptexturize("Hello World");21console.log(str);22var cffStrings = require('cff-strings');23var str = cffStrings.wptexturize("Hello World");24console.log(str);25var cffStrings = require('cff-strings');26var str = cffStrings.wptexturize("Hello World");27console.log(str);28var cffStrings = require('cff-strings');29var str = cffStrings.wptexturize("Hello World");30console.log(str);
Using AI Code Generation
1var cffstrings = require('cffstrings');2var text = "This is a test of the 'cffstrings' module";3var newText = cffstrings.wptexturize(text);4console.log(newText);5var cffstrings = require('cffstrings');6var text = "This is a test of the 'cffstrings' module";7var newText = cffstrings.wptexturize(text);8console.log(newText);9var cffstrings = require('cffstrings');10var text = "This is a test of the 'cffstrings' module";11var newText = cffstrings.wptexturize(text);12console.log(newText);13var cffstrings = require('cffstrings');14var text = "This is a test of the 'cffstrings' module";15var newText = cffstrings.wptexturize(text);16console.log(newText);17var cffstrings = require('cffstrings');18var text = "This is a test of the 'cffstrings' module";19var newText = cffstrings.wptexturize(text);20console.log(newText);21var cffstrings = require('cffstrings');22var text = "This is a test of the 'cffstrings' module";23var newText = cffstrings.wptexturize(text);24console.log(newText);25var cffstrings = require('cffstrings');26var text = "This is a test of the 'cffstrings' module";27var newText = cffstrings.wptexturize(text);28console.log(newText);29var cffstrings = require('cffstrings');30var text = "This is a test of the 'cffstrings' module";31var newText = cffstrings.wptexturize(text);
Using AI Code Generation
1var cffstrings = require('cffstrings');2var str = "I'm a string";3var str2 = cffstrings.wptexturize(str);4console.log(str2);5var cffstrings = require('cffstrings');6var str = "I'm a string";7var str2 = cffstrings.wptexturize(str);8console.log(str2);9var cffstrings = require('cffstrings');10var str = "I'm a string";11var str2 = cffstrings.wptexturize(str);12console.log(str2);13var cffstrings = require('cffstrings');14var str = "I'm a string";15var str2 = cffstrings.wptexturize(str);16console.log(str2);17var cffstrings = require('cffstrings');18var str = "I'm a string";19var str2 = cffstrings.wptexturize(str);20console.log(str2);21var cffstrings = require('cffstrings');22var str = "I'm a string";23var str2 = cffstrings.wptexturize(str);24console.log(str2);25var cffstrings = require('cffstrings');26var str = "I'm a string";27var str2 = cffstrings.wptexturize(str);28console.log(str2);
Using AI Code Generation
1var CFFStrings = require('wptext').CFFStrings;2var cff = new CFFStrings();3var str = cff.getString(0x00);4console.log(str);5var CFFStrings = require('wptext').CFFStrings;6var cff = new CFFStrings();7var str = cff.getString(0x01);8console.log(str);9var CFFStrings = require('wptext').CFFStrings;10var cff = new CFFStrings();11var str = cff.getString(0x02);12console.log(str);13var CFFStrings = require('wptext').CFFStrings;14var cff = new CFFStrings();15var str = cff.getString(0x03);16console.log(str);17var CFFStrings = require('wptext').CFFStrings;18var cff = new CFFStrings();19var str = cff.getString(0x04);20console.log(str);21var CFFStrings = require('wptext').CFFStrings;22var cff = new CFFStrings();23var str = cff.getString(0x05);24console.log(str);25var CFFStrings = require('wptext').CFFStrings;26var cff = new CFFStrings();27var str = cff.getString(0x06);28console.log(str);29var CFFStrings = require('wptext').CFFStrings;30var cff = new CFFStrings();31var str = cff.getString(0x07);32console.log(str);33var CFFStrings = require('wptext').CFFStrings;34var cff = new CFFStrings();35var str = cff.getString(0x
Using AI Code Generation
1var cffStrings = require('wptexturize');2var text = "I'm a string that will be texturized";3var texturized = cffStrings(text);4console.log(texturized);5var cffStrings = require('wptexturize');6var text = "I'm a string that will be texturized";7var texturized = cffStrings.wptexturize(text);8console.log(texturized);9var cffStrings = require('wptexturize');10var text = "I'm a string that will be texturized";11var texturized = cffStrings.wptexturize(text, true);12console.log(texturized);13var cffStrings = require('wptexturize');14var text = "I'm a string that will be texturized";15var texturized = cffStrings.wptexturize(text, false);16console.log(texturized);17var cffStrings = require('wptexturize');18var text = "I'm a string that will be texturized";19var texturized = cffStrings.wptexturize(text, 'true');20console.log(texturized);21var cffStrings = require('wptexturize');22var text = "I'm a string that will be texturized";23var texturized = cffStrings.wptexturize(text, 'false');24console.log(texturized);25var cffStrings = require('wptexturize');26var text = "I'm a string that will be texturized";27var texturized = cffStrings.wptexturize(text, 'TRUE');28console.log(texturized);
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!