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, 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 var i, sid;37 if (format === 0) {38 charset['.notdef'] = readCharstringEncoding(aCharstrings[0]);39 for (i = 1; i < count + 1; i++) {40 sid = aStream.getByte() << 8 | aStream.getByte();41 charset[CFFStrings[sid]] = readCharstringEncoding(aCharstrings[i]);42 }43 } else if (format === 1) {44 for (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 sid = first++;50 charset[CFFStrings[sid]] = readCharstringEncoding(aCharstrings[j]);51 }52 }53 } else {54 error('Invalid charset format');55 }56 return charset;57}58/*59 * Take a Type2 binary charstring as input and transform it to a human60 * readable representation as specified by the 'The Type 2 Charstring Format',61 * chapter 3.1.62 */63function readCharstringEncoding(aString) {64 if (!aString) {65 return '';66 }67 var charstringTokens = [];68 var count = aString.length;69 for (var i = 0; i < count; ) {70 var value = aString[i++] | 0;71 var token = null;72 if (value < 0) {73 continue;74 } else if (value <= 11) {75 token = CFFEncodingMap[value];76 } else if (value === 12) {77 token = CFFEncodingMap[value][aString[i++]];78 } else if (value <= 18) {79 token = CFFEncodingMap[value];80 } else if (value <= 20) {81 ++i; // var mask = aString[i++];82 token = CFFEncodingMap[value];83 } else if (value <= 27) {84 token = CFFEncodingMap[value];85 } else if (value === 28) {86 token = aString[i++] << 8 | aString[i++];87 } else if (value <= 31) {88 token = CFFEncodingMap[value];89 } else if (value < 247) {90 token = parseInt(value, 10) - 139;91 } else if (value < 251) {92 token = (value - 247) * 256 + aString[i++] + 108;93 } else if (value < 255) {94 token = -(value - 251) * 256 - aString[i++] - 108;95 } else { // value === 25596 token = aString[i++] << 24 | aString[i++] << 16 |97 aString[i++] << 8 | aString[i];98 }99 charstringTokens.push(token);100 }101 return charstringTokens;102}103/*104 * Take a binary DICT Data as input and transform it into a human readable105 * form as specified by 'The Compact Font Format Specification', chapter 5.106 */107function readFontDictData(aString, aMap) {108 var fontDictDataTokens = [];109 var count = aString.length;110 for (var i = 0; i < count; i) {111 var value = aString[i++] | 0;112 var token = null;113 if (value === 12) {114 token = aMap[value][aString[i++]];115 } else if (value === 28) {116 token = aString[i++] << 8 | aString[i++];117 } else if (value === 29) {118 token = aString[i++] << 24 |119 aString[i++] << 16 |120 aString[i++] << 8 |121 aString[i++];122 } else if (value === 30) {123 token = '';124 var parsed = false;125 while (!parsed) {126 var octet = aString[i++];127 var nibbles = [parseInt(octet / 16, 10), parseInt(octet % 16, 10)];128 for (var j = 0; j < nibbles.length; j++) {129 var nibble = nibbles[j];130 switch (nibble) {131 case 0xA:132 token += '.';133 break;134 case 0xB:135 token += 'E';136 break;137 case 0xC:138 token += 'E-';139 break;140 case 0xD:141 break;142 case 0xE:143 token += '-';144 break;145 case 0xF:146 parsed = true;147 break;148 default:149 token += nibble;150 break;151 }152 }153 }154 token = parseFloat(token);155 } else if (value <= 31) {156 token = aMap[value];157 } else if (value <= 246) {158 token = parseInt(value, 10) - 139;159 } else if (value <= 250) {160 token = (value - 247) * 256 + aString[i++] + 108;161 } else if (value <= 254) {162 token = -(value - 251) * 256 - aString[i++] - 108;163 } else if (value === 255) {164 error('255 is not a valid DICT command');165 }166 fontDictDataTokens.push(token);167 }168 return fontDictDataTokens;169}170/*171 * Take a stream as input and return an array of objects.172 * In CFF an INDEX is a structure with the following format:173 * {174 * count: 2 bytes (Number of objects stored in INDEX),175 * offsize: 1 byte (Offset array element size),176 * offset: [count + 1] bytes (Offsets array),177 * data: - (Objects data)178 * }179 *180 * More explanation are given in the 'CFF Font Format Specification',181 * chapter 5.182 */183function readFontIndexData(aStream, aIsByte) {184 var count = aStream.getByte() << 8 | aStream.getByte();185 var offsize = aStream.getByte();186 function getNextOffset() {187 switch (offsize) {188 case 0:189 return 0;190 case 1:191 return aStream.getByte();192 case 2:193 return aStream.getByte() << 8 | aStream.getByte();194 case 3:195 return aStream.getByte() << 16 | aStream.getByte() << 8 |196 aStream.getByte();197 case 4:198 return aStream.getByte() << 24 | aStream.getByte() << 16 |199 aStream.getByte() << 8 | aStream.getByte();200 }201 error(offsize + ' is not a valid offset size');202 return null;203 }204 var offsets = [];205 var i;206 for (i = 0; i < count + 1; i++) {207 offsets.push(getNextOffset());208 }209 dump('Found ' + count + ' objects at offsets :' +210 offsets + ' (offsize: ' + offsize + ')');211 // Now extract the objects212 var relativeOffset = aStream.pos;213 var objects = [];214 for (i = 0; i < count; i++) {215 var offset = offsets[i];216 aStream.pos = relativeOffset + offset - 1;217 var data = [];218 var length = offsets[i + 1] - 1;219 for (var j = offset - 1; j < length; j++) {220 data.push(aIsByte ? aStream.getByte() : aStream.getChar());221 }222 objects.push(data);223 }224 return objects;225}226var Type2Parser = function type2Parser(aFilePath) {227 var font = new Dict(null);228 var xhr = new XMLHttpRequest();229 xhr.open('GET', aFilePath, false);230 xhr.responseType = 'arraybuffer';231 xhr.expected = (document.URL.indexOf('file:') === 0) ? 0 : 200;232 xhr.send(null);233 this.data = new Stream(xhr.response);234 // Turn on this flag for additional debugging logs235 var debug = false;236 function dump(aStr) {237 if (debug) {238 console.log(aStr);239 }240 }241 function parseAsToken(aString, aMap) {242 var decoded = readFontDictData(aString, aMap);243 var stack = [];244 var count = decoded.length;245 for (var i = 0; i < count; i++) {246 var token = decoded[i];247 if (isNum(token)) {248 stack.push(token);249 } else {250 switch (token.operand) {251 case 'SID':252 font.set(token.name, CFFStrings[stack.pop()]);253 break;254 case 'number number':255 font.set(token.name, {256 offset: stack.pop(),257 size: stack.pop()258 });259 break;260 case 'boolean':261 font.set(token.name, stack.pop());262 break;263 case 'delta':264 font.set(token.name, stack.pop());265 break;266 default:267 if (token.operand && token.operand.length) {268 var array = [];269 for (var j = 0; j < token.operand.length; j++) {270 array.push(stack.pop());271 }272 font.set(token.name, array);273 } else {274 font.set(token.name, stack.pop());275 }276 break;277 }278 }279 }280 }281 this.parse = function type2ParserParse(aStream) {282 font.set('major', aStream.getByte());283 font.set('minor', aStream.getByte());284 font.set('hdrSize', aStream.getByte());285 font.set('offsize', aStream.getByte());286 // Read the NAME Index287 dump('Reading Index: Names');288 font.set('Names', readFontIndexData(aStream));289 dump('Names: ' + font.get('Names'));290 // Read the Top Dict Index291 dump('Reading Index: TopDict');292 var topDict = readFontIndexData(aStream, true);293 dump('TopDict: ' + topDict);294 // Read the String Index295 dump('Reading Index: Strings');296 var strings = readFontIndexData(aStream);297 dump('strings: ' + strings);298 // Fill up the Strings dictionary with the new unique strings299 var i;300 for (i = 0; i < strings.length; i++) {301 CFFStrings.push(strings[i].join(''));302 }303 // Parse the TopDict operator304 var count = topDict.length;305 for (i = 0; i < count; i++) {306 parseAsToken(topDict[i], CFFDictDataMap);307 }308 // Read the Global Subr Index that comes just after the Strings Index309 // (cf. "The Compact Font Format Specification" Chapter 16)310 dump('Reading Global Subr Index');311 var subrs = readFontIndexData(aStream, true);312 dump(subrs);313 // Reading Private Dict314 var priv = font.get('Private');315 dump('Reading Private Dict (offset: ' + priv.offset +316 ' size: ' + priv.size + ')');317 aStream.pos = priv.offset;318 var privateDict = [];319 for (i = 0; i < priv.size; i++) {320 privateDict.push(aStream.getByte());321 }322 dump('privateData:' + privateDict);323 parseAsToken(privateDict, CFFDictPrivateDataMap);324 for (var p in font.map) {325 dump(p + '::' + font.get(p));326 }327 // Read CharStrings Index328 var charStringsOffset = font.get('CharStrings');329 dump('Read CharStrings Index (offset: ' + charStringsOffset + ')');330 aStream.pos = charStringsOffset;331 var charStrings = readFontIndexData(aStream, true);332 // Read Charset333 dump('Read Charset for ' + charStrings.length + ' glyphs');334 var charsetEntry = font.get('charset');335 if (charsetEntry === 0) {336 error('Need to support CFFISOAdobeCharset');337 } else if (charsetEntry === 1) {338 error('Need to support CFFExpert');339 } else if (charsetEntry === 2) {340 error('Need to support CFFExpertSubsetCharset');341 } else {342 aStream.pos = charsetEntry;343 readCharset(aStream, charStrings);344 }345 };346};347/*348 * To try the Type2 decoder on a local file in the current directory:349 *350 * var cff = new Type2Parser("file.cff");351 * cff.parse(this.data);352 *353 * To try the Type2 decoder on a custom built CFF array:354 *355 * var file = new Uint8Array(cffFileArray, 0, cffFileSize);356 * var parser = new Type2Parser();357 * parser.parse(new Stream(file));358 *359 */360/*361 * Write to a file to the disk (works only on Firefox in privilege mode)362 * but this is useful for dumping a font file to the disk and check with363 * fontforge or the ots program what's wrong with the file.364 *365 * writeToFile(fontData, "/tmp/pdf.js." + fontCount + ".cff");366 */367function writeToFile(aBytes, aFilePath) {368 if (!('netscape' in window)) {369 return;370 }371 netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect');372 var Cc = Components.classes,373 Ci = Components.interfaces;374 var file = Cc['@mozilla.org/file/local;1'].createInstance(Ci.nsILocalFile);375 file.initWithPath(aFilePath);376 var stream = Cc['@mozilla.org/network/file-output-stream;1']377 .createInstance(Ci.nsIFileOutputStream);378 stream.init(file, 0x04 | 0x08 | 0x20, 0x180, 0);379 var bos = Cc['@mozilla.org/binaryoutputstream;1']380 .createInstance(Ci.nsIBinaryOutputStream);381 bos.setOutputStream(stream);382 bos.writeByteArray(aBytes, aBytes.length);383 stream.close();...
Using AI Code Generation
1var text = '“”‘’…—–';2var text2 = '“”‘’…—–';3var text3 = '“”‘’…—–';4var text4 = '“”‘’…—–';5var text5 = '“”‘’…—–';6var text6 = '“”‘’…—–';7var text7 = '“”‘’…—–';8var text8 = '“”‘’…—–';9var text9 = '“”‘’…—–';10var text10 = '“”‘’…—–';11var text11 = '“”‘’…—–';12var text12 = '“”‘’…—–';13var text13 = '“”‘’…—–';14var text14 = '“”‘’…—–';15var text15 = '“”‘’…—–';16var text16 = '“”‘’…—–';17var text17 = '“”‘’…—–';18var text18 = '“”‘’…—–';19var text19 = '“”‘’…—–';20var text20 = '“”‘’…—–';21var text21 = '“”‘’…—–';22var text22 = '“”‘’…—–';23var text23 = '“”‘’…—–';24var text24 = '“”‘’…—–';25var text25 = '“”‘’…—–';26var text26 = '“”‘’…—–';27var text27 = '“”‘’…—–';28var text28 = '“”‘’…—–';29var text29 = '“”‘’…—–';30var text30 = '“”‘’…—–';31var text31 = '“”‘’…—–';32var text32 = '“”‘’…—–';33var text33 = '“”‘’…—–';34var text34 = '“”‘’…—–';35var text35 = '“”‘’…—–';36var text36 = '“”‘’…—–';37var text37 = '“”‘’…—–';38var text38 = '“”‘’…—–';
Using AI Code Generation
1var wptools = require('wptools');2var cff = wptools.cffencoding();3cff.encode('hello world');4cff.decode('68656c6c6f20776f726c64');5var wptools = require('wptools');6var cff = wptools.cffencoding();7cff.encode('hello world');8cff.decode('68656c6c6f20776f726c64');9var wptools = require('wptools');10var cff = wptools.cffencoding();11cff.encode('hello world');12cff.decode('68656c6c6f20776f726c64');13var wptools = require('wptools');14var cff = wptools.cffencoding();15cff.encode('hello world');16cff.decode('68656c6c6f20776f726c64');17var wptools = require('wptools');18var cff = wptools.cffencoding();19cff.encode('hello world');20cff.decode('68656c6c6f20776f726c64');21var wptools = require('wptools');22var cff = wptools.cffencoding();23cff.encode('hello world');24cff.decode('68656c6c6f20776f726c64');25var wptools = require('wptools');
Using AI Code Generation
1var pdf = require('wptext2pdf');2var fs = require('fs');3var pdf = new pdf();4var text = 'Hello World';5var options = {6};7pdf.addText(text, options);8pdf.writePDF('test.pdf', function(err) {9 if (err) {10 return console.log(err);11 }12 console.log('PDF saved!');13});14### new PDF()15### PDF.addText(text, options)16### PDF.writePDF(filename, callback)17MIT © [Michael Chen](
Using AI Code Generation
1var cff = require('wptexturize');2var text = 'This is a test. It is only a test.';3console.log(cff(text));4var cff = require('wptexturize');5var text = 'This is a test. It is only a test.';6console.log(cff(text, true));7var cff = require('wptexturize');8var text = 'This is a test. It is only a test.';9console.log(cff(text, false));10var cff = require('wptexturize');11var text = 'This is a test. It is only a test.';12console.log(cff(text, false, false));13var cff = require('wptexturize');14var text = 'This is a test. It is only a test.';15console.log(cff(text, false, false, true));16var cff = require('wptexturize');17var text = 'This is a test. It is only a test.';18console.log(cff(text, false, false, false, true));19var cff = require('wptexturize');20var text = 'This is a test. It is only a test.';21console.log(cff(text, false, false, false, false, true));
Using AI Code Generation
1var pdf = require('wptext2pdf');2var fs = require('fs');3var path = require('path');4var text = "Hello World";5var options = {6 pdf: {7 }8};9pdf.create(text, options).toFile('./test.pdf', function(err, res) {10 if (err) return console.log(err);11});12var pdf = require('wptext2pdf');13var fs = require('fs');14var path = require('path');15var text = "Hello World";16var options = {17 pdf: {18 }19};20pdf.create(text, options).toFile('./test.pdf', function(err, res) {21 if (err) return console.log(err);22});23var pdf = require('wptext2pdf');24var fs = require('fs');25var path = require('path');26var text = "Hello World";27var options = {28 pdf: {29 }30};31pdf.create(text, options).toFile('./test.pdf', function(err, res) {32 if (err) return console.log(err);33});34var pdf = require('wptext2pdf');35var fs = require('fs');36var path = require('path');37var text = "Hello World";38var options = {39 pdf: {40 }41};42pdf.create(text, options).toFile('./test.pdf', function(err, res) {43 if (err) return console.log(err);
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!!