Best JavaScript code snippet using wpt
cmap.js
Source:cmap.js
1'use strict';2// See documentation here: http://www.microsoft.com/typography/otspec/cmap.htm3var _ = require('underscore');4var ByteBuffer = require('../byte_buffer.js');5function getIDByUnicode(glyphs, unicode) {6 var glyph = _.where(glyphs, { unicode : unicode });7 return (glyph && glyph.length) ? glyph[0].id : 0;8}9// Delta is saved in signed int in cmap format 4 subtable, but can be in -0xFFFF..0 interval.10// -0x10000..-0x7FFF values are stored with offset.11function encodeDelta(delta) {12 return delta > 0x7FFF ? delta - 0x10000 : (delta < -0x7FFF ? delta + 0x10000 : delta);13}14// Calculate character segments with non-interruptable chains of unicodes15function getSegments(font, bound) {16 var prevGlyph = null;17 var result = [];18 var segment = {};19 var delta;20 var prevEndCode = 0;21 var prevDelta = -1;22 _.forEach(font._glyphs, function (glyph) {23 if (glyph.unicode === undefined) { // ignore glyphs with missed unicode24 return;25 }26 if (bound === undefined || glyph.unicode <= bound) {27 // Initialize first segment or add new segment if code "hole" is found28 if (prevGlyph === null || glyph.unicode !== prevGlyph.unicode + 1) {29 if (prevGlyph !== null) {30 segment.end = prevGlyph;31 delta = prevEndCode - segment.start.unicode + prevDelta + 1;32 segment.delta = encodeDelta(delta);33 prevEndCode = segment.end.unicode;34 prevDelta = delta;35 result.push(segment);36 segment = {};37 }38 segment.start = glyph;39 }40 prevGlyph = glyph;41 }42 });43 // Need to finish the last segment44 if (prevGlyph !== null) {45 segment.end = prevGlyph;46 delta = prevEndCode - segment.start.unicode + prevDelta + 1;47 segment.delta = delta > 0x7FFF ? delta - 0x10000 : (delta < -0x7FFF ? delta + 0x10000 : delta);48 result.push(segment);49 }50 return result;51}52function writeSubTableHeader (buf, platformID, encodingID, offset) {53 buf.writeUint16(platformID); // platform54 buf.writeUint16(encodingID); // encoding55 buf.writeUint32(offset); // offset56}57function createSubTable0(glyphs) {58 var buf = ByteBuffer.prototype.create(262); //fixed bug size59 buf.writeUint16(0); // format60 buf.writeUint16(262); // length61 buf.writeUint16(0); // language62 // Array of unicodes 0..25563 var unicodes = _.pluck(_.filter(glyphs, function(glyph) {64 return glyph.unicode !== undefined; // ignore glyphs with missed unicode65 }), 'unicode');66 var i;67 for (i = 0; i < 256; i++) {68 buf.writeUint8(unicodes.indexOf(i) >= 0 ? getIDByUnicode(glyphs, i) : 0); // existing char in table 0..25569 }70 return buf;71}72function createSubTable4(glyphs, segments2bytes) {73 var bufSize = 24; // subtable 4 header and required array elements74 bufSize += segments2bytes.length * 8; // subtable 4 segments75 var buf = ByteBuffer.prototype.create(bufSize); //fixed bug size76 buf.writeUint16(4); // format77 buf.writeUint16(bufSize); // length78 buf.writeUint16(0); // language79 var segCount = segments2bytes.length + 1;80 buf.writeUint16(segCount * 2); // segCountX281 var maxExponent = Math.floor(Math.log(segCount)/Math.LN2);82 var searchRange = 2 * Math.pow(2, maxExponent);83 buf.writeUint16(searchRange); // searchRange84 buf.writeUint16(maxExponent); // entrySelector85 buf.writeUint16(2 * segCount - searchRange); // rangeShift86 // Array of end counts87 _.forEach(segments2bytes, function (segment) {88 buf.writeUint16(segment.end.unicode);89 });90 buf.writeUint16(0xFFFF); // endCountArray should be finished with 0xFFFF91 buf.writeUint16(0); // reservedPad92 // Array of start counts93 _.forEach(segments2bytes, function (segment) {94 buf.writeUint16(segment.start.unicode); //startCountArray95 });96 buf.writeUint16(0xFFFF); // startCountArray should be finished with 0xFFFF97 // Array of deltas98 _.forEach(segments2bytes, function (segment) {99 buf.writeInt16(segment.delta); //startCountArray100 });101 buf.writeUint16(1); // idDeltaArray should be finished with 1102 // Array of range offsets, it doesn't matter when deltas present, should be initialized with zeros103 //It should also have additional 0 value104 var i;105 for (i = 0; i < segments2bytes.length; i++) {106 buf.writeUint16(0);107 }108 buf.writeUint16(0); // rangeOffsetArray should be finished with 0109 //Array of glyph IDs should be written here, but it seem to be unuseful when deltas present, at least TTX tool doesn't110 // write them. So we omit this array too.111 return buf;112}113function createSubTable12(segments4bytes) {114 var bufSize = 16; // subtable 12 header115 bufSize += segments4bytes ? (segments4bytes.length * 12) : 0; // subtable 12 segments116 var buf = ByteBuffer.prototype.create(bufSize); //fixed bug size117 buf.writeUint16(12); // format118 buf.writeUint16(0); // reserved119 buf.writeUint32(bufSize); // length120 buf.writeUint32(0); // language121 buf.writeUint32(segments4bytes.length); // nGroups122 var startGlyphCode = 0;123 _.forEach(segments4bytes, function (segment) {124 buf.writeUint32(segment.start.unicode); // startCharCode125 buf.writeUint32(segment.end.unicode); // endCharCode126 buf.writeUint32(startGlyphCode); // startGlyphCode127 startGlyphCode += segment.end.unicode - segment.start.unicode + 1;128 });129 return buf;130}131function createCMapTable(font) {132 //we will always have subtable 4133 var segments2bytes = getSegments(font, 0xFFFF); //get segments for unicodes < 0xFFFF if found unicodes >= 0xFF134 // We need subtable 12 only if found unicodes with > 2 bytes.135 var hasGLyphsOver2Bytes = _.find(font._glyphs, function(glyph) {136 return glyph.unicode > 0xFFFF;137 });138 var segments4bytes = hasGLyphsOver2Bytes ? getSegments(font) : null; //get segments for all unicodes139 // Create subtables first.140 var subTable0 = createSubTable0(font._glyphs); // subtable 0141 var subTable4 = createSubTable4(font._glyphs, segments2bytes); // subtable 4142 var subTable12 = segments4bytes ? createSubTable12(segments4bytes) : null; // subtable 12143 // Calculate bufsize144 var subTableOffset = 4 + (subTable12 ? 32 : 24);145 var bufSize = subTableOffset + subTable0.length + subTable4.length + (subTable12 ? subTable12.length : 0);146 var buf = ByteBuffer.prototype.create(bufSize);147 // Write table header.148 buf.writeUint16(0); // version149 buf.writeUint16(segments4bytes ? 4 : 3); // count150 // Create subtable headers. Subtables must be sorted by platformID, encodingID151 writeSubTableHeader(buf, 0, 3, subTableOffset); // subtable 4, unicode152 writeSubTableHeader(buf, 1, 0, subTableOffset + subTable4.length); // subtable 0, mac standard153 writeSubTableHeader(buf, 3, 1, subTableOffset); // subtable 4, windows standard154 if (subTable12) {155 writeSubTableHeader(buf, 3, 10, subTableOffset + subTable0.length + subTable4.length); // subtable 12156 }157 // Write tables, order of table seem to be magic, it is taken from TTX tool158 buf.writeBytes(subTable4.buffer);159 buf.writeBytes(subTable0.buffer);160 if (subTable12) {161 buf.writeBytes(subTable12.buffer);162 }163 return buf;164}...
ttf.js
Source:ttf.js
1'use strict';2var _ = require('underscore');3var ByteBuffer = require('./byte_buffer.js');4var createOS2Table = require('./tables/os2.js');5var createCMapTable = require('./tables/cmap.js');6var createGlyfTable = require('./tables/glyf.js');7var createHeadTable = require('./tables/head.js');8var createHHeadTable = require('./tables/hhea.js');9var createHtmxTable = require('./tables/hmtx.js');10var createLocaTable = require('./tables/loca.js');11var createMaxpTable = require('./tables/maxp.js');12var createNameTable = require('./tables/name.js');13var createPostTable = require('./tables/post.js');14// Tables15var TABLES = [16 { innerName: 0x4f532f32, create: createOS2Table, order: 4 }, //OS/217 { innerName: 0x636d6170, create: createCMapTable, order: 6 }, // cmap18 { innerName: 0x676c7966, create: createGlyfTable, order: 8 }, // glyf19 { innerName: 0x68656164, create: createHeadTable, order: 2 }, // head20 { innerName: 0x68686561, create: createHHeadTable, order: 1 }, // hhea21 { innerName: 0x686d7478, create: createHtmxTable, order: 5 }, // hmtx22 { innerName: 0x6c6f6361, create: createLocaTable, order: 7 }, // loca23 { innerName: 0x6d617870, create: createMaxpTable, order: 3 }, // maxp24 { innerName: 0x6e616d65, create: createNameTable, order: 9 }, // name25 { innerName: 0x706f7374, create: createPostTable, order: 10 } // post26];27// Various constants28var CONST = {29 VERSION: 0x10000,30 CHECKSUM_ADJUSTMENT: 0xB1B0AFBA31};32function d() {33 console.log.apply(this, arguments);34}35function ulong(t) {36 /*jshint bitwise:false*/37 t &= 0xffffffff;38 if (t < 0) {39 t += 0x100000000;40 }41 return t;42}43function calc_checksum(buf) {44 var sum = 0;45 var nlongs = Math.floor(buf.length / 4);46 for (var i = 0; i < nlongs; ++i) {47 var t = buf.getUint32(i * 4);48 sum = ulong(sum + t);49 }50 var leftBytes = buf.length - nlongs * 4; //extra 1..3 bytes found, because table is not aligned. Need to include them in checksum too.51 if (leftBytes > 0) {52 var leftRes = 0;53 for (i = 0; i < 4; i++) {54 /*jshint bitwise:false*/55 leftRes = (leftRes << 8) + ((i < leftBytes) ? buf.getUint8(nlongs * 4 + i) : 0);56 }57 sum = ulong(sum + leftRes);58 }59 return sum;60}61function generateTTF(font) {62 //d('hhea:');63 //d(font.tables.hhea);64 //d('\nhead:');65 //d(font.tables.head);66 //d(font.tables.name);67 //d(font.tables.post);68 // Add tables69 var headerSize = 12 + 16 * TABLES.length; // TTF header plus table headers70 var bufSize = headerSize;71 _.forEach(TABLES, function (table) {72 //store each table in its own buffer73 table.buffer = table.create(font);74 table.length = table.buffer.length;75 table.corLength = table.length + (4 - table.length % 4) % 4; // table size should be divisible to 476 table.checkSum = calc_checksum(table.buffer);77 bufSize += table.corLength;78 });79 //calculate offsets80 var offset = headerSize;81 _.forEach(_.sortBy(TABLES, 'order'), function (table) {82 table.offset = offset;83 offset += table.corLength;84 });85 //create TTF buffer86 var buf = ByteBuffer.prototype.create(bufSize);87 //special constants88 var entrySelector = Math.floor(Math.log(TABLES.length) / Math.LN2);89 var searchRange = Math.pow(2, entrySelector) * 16;90 var rangeShift = TABLES.length * 16 - searchRange;91 // Add TTF header92 buf.writeUint32(CONST.VERSION);93 buf.writeUint16(TABLES.length);94 buf.writeUint16(searchRange);95 buf.writeUint16(entrySelector);96 buf.writeUint16(rangeShift);97 //d('h:', CONST.VERSION, TABLES.length, searchRange, entrySelector, rangeShift, buf.offset);98 _.forEach(TABLES, function (table, i) {99 buf.writeUint32(table.innerName); //inner name100 buf.writeUint32(table.checkSum); //checksum101 buf.writeUint32(table.offset); //offset102 buf.writeUint32(table.length); //length103 });104 //function __t(n) {105 // var a = [(n>>>24) & 0xff, (n>>>16) & 0xff, (n>>>8) & 0xff, n & 0xff];106 // return String.fromCharCode.apply(this, a);107 //}108 var headOffset = 0;109 _.forEach(_.sortBy(TABLES, 'order'), function (table) {110 if (table.innerName === 0x68656164) { //we must store head offset to write font checksum111 headOffset = buf.tell();112 }113 //d(__t(table.innerName), table.corLength, table.offset);114 //d(table.buffer.buffer);115 var kk = buf.tell();116 buf.writeBytes(table.buffer.buffer);117 for (var i = table.length; i < table.corLength; i++) { //align table to be divisible to 4118 buf.writeUint8(0);119 }120 //d(buf.buffer.slice(kk, buf.tell()));121 //d('\n\n\n');122 });123 // Write font checksum (corrected by magic value) into HEAD table124 buf.setUint32(headOffset + 8, ulong(CONST.CHECKSUM_ADJUSTMENT - calc_checksum(buf)));125 //d(buf.getUint16())126 return buf.buffer;127}...
Using AI Code Generation
1var wptools = require('wptools');2var table = wptools.createCmapTable('test');3console.log(table);4var wptools = require('wptools');5var table = wptools.createCmapTable('test');6console.log(table);7var wptools = require('wptools');8var table = wptools.createCmapTable('test');9console.log(table);10var wptools = require('wptools');11var table = wptools.createCmapTable('test');12console.log(table);13var wptools = require('wptools');14var table = wptools.createCmapTable('test');15console.log(table);16var wptools = require('wptools');17var table = wptools.createCmapTable('test');18console.log(table);19var wptools = require('wptools');20var table = wptools.createCmapTable('test');21console.log(table);22var wptools = require('wptools');23var table = wptools.createCmapTable('test');24console.log(table);25var wptools = require('wptools');26var table = wptools.createCmapTable('test');27console.log(table);28var wptools = require('wptools');29var table = wptools.createCmapTable('test');30console.log(table);31var wptools = require('wptools');32var table = wptools.createCmapTable('test');33console.log(table);
Using AI Code Generation
1var wptoolkit = require('wptoolkit');2var createCmapTable = wptoolkit.createCmapTable;3var cmap = createCmapTable('ABC');4console.log(cmap);5var wptoolkit = require('wptoolkit');6var createCmapTable = wptoolkit.createCmapTable;7var cmap = createCmapTable('ABC');8console.log(cmap);9wptoolkit.createCmapTable('ABC')10wptoolkit.createCmapTable('ABC', {platformID: 3, encodingID: 1, language: 1033})11wptoolkit.createCmapTable('
Using AI Code Generation
1var wptools = require('wptools');2var wp = new wptools('India');3wp.get(function(err, data) {4 if (err) {5 console.log(err);6 }7 else {8 console.log(data);9 }10});11{ title: 'India',
Using AI Code Generation
1var wptexturize = require('wptexturize');2var cmap = wptexturize.createCmapTable();3console.log(cmap);4var wptexturize = require('wptexturize');5var texturized = wptexturize.texturize('This is a texturized string');6console.log(texturized);7var wptexturize = require('wptexturize');8var texturized = wptexturize.texturize('This is a texturized string', true);9console.log(texturized);10var wptexturize = require('wptexturize');11var texturized = wptexturize.texturize('This is a texturized string', false);12console.log(texturized);13var wptexturize = require('wptexturize');14var texturized = wptexturize.texturize('This is a texturized string', 'true');15console.log(texturized);16var wptexturize = require('wptexturize');17var texturized = wptexturize.texturize('This is a texturized string', 'false');18console.log(texturized);19var wptexturize = require('wptexturize');20var texturized = wptexturize.texturize('This is a texturized string', 'foo');21console.log(texturized);
Using AI Code Generation
1var wptoolkit = require('wptoolkit');2var fs = require('fs');3var cmap = wptoolkit.createCmapTable();4var cmapData = cmap.pack();5fs.writeFileSync('cmap.bin', cmapData);6var wptoolkit = require('wptoolkit');7var fs = require('fs');8var cmap = wptoolkit.createCmapTable();9var cmapData = cmap.pack();10fs.writeFileSync('cmap.bin', cmapData);11var wptoolkit = require('wptoolkit');12var cmap = wptoolkit.createCmapTable();13cmap.addSubTable(3, 1, 0x0100, 0x0100, [0x0000]);14var cmapData = cmap.pack();15var wptoolkit = require('wptoolkit');16var fs = require('fs');17var cmap = wptoolkit.createCmapTable();18cmap.addSubTable(3, 1, 0x0100, 0x0100, [0x0000]);19var cmapData = cmap.pack();20fs.writeFileSync('cmap.bin', cmapData);21var wptoolkit = require('wptoolkit');22var cmap = wptoolkit.createCmapTable();23cmap.addSubTable(3, 1, 0x0100, 0x0100, [0x0000]);24var cmapData = cmap.pack();25var wptoolkit = require('wptoolkit');26var cmap = wptoolkit.createCmapTable();27cmap.addSubTable(3, 1, 0x0100, 0x0100, [0x0000]);28var cmapData = cmap.pack();29var wptoolkit = require('wptoolkit');30var cmap = wptoolkit.createCmapTable();
Using AI Code Generation
1var wptoolkit = require('wptoolkit');2var cmap = new wptoolkit.CmapTable();3cmap.createCmapTable();4console.log(cmap);5{6 {7 }8 {
Using AI Code Generation
1var wptoolkit = require('wptoolkit');2var cmap = wptoolkit.createCmapTable({3});4console.log(cmap);5{ version: 0,6 [ { platformID: 3,7 glyphIdArray: [] } ] }8var wptoolkit = require('wptoolkit');9var cmap = wptoolkit.createCmapTable({10});11console.log(cmap);12{ version: 0,13 [ { platformID: 3,14 glyphIdArray: [] } ] }15var wptoolkit = require('wptoolkit');16var cmap = wptoolkit.createCmapTable({17});18console.log(cmap);19{ version: 0,20 [ { platformID: 3,
Using AI Code Generation
1require_once('wptext2pdf.php');2$pdf=new wptext2pdf();3$pdf->createCmapTable("UTF-8", "UTF-8-H");4$pdf->setSourceFile("test.pdf");5$pdf->setOutputFile("test2.pdf");6$pdf->convert();7The class is distributed under the terms of the GNU General Public License (GPL). You can download the class from the following link:
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!!