From 60127b08305ae26afb23b389b64b5820b26f4fd9 Mon Sep 17 00:00:00 2001 From: sbarman Date: Thu, 18 Aug 2011 11:28:37 -0700 Subject: [PATCH 01/20] increased size of loaded font --- fonts.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/fonts.js b/fonts.js index 1860df2e5..2c737b050 100755 --- a/fonts.js +++ b/fonts.js @@ -975,7 +975,7 @@ var Font = (function Font() { // Create a new file to hold the new version of our truetype with a new // header and new offsets - var ttf = new Uint8Array(kMaxFontFileSize); + var ttf = new Uint8Array(kMaxFontFileSize*3); // The offsets object holds at the same time a representation of where // to write the table entry information about a table and another offset @@ -1102,6 +1102,8 @@ var Font = (function Font() { for (var i = 0; i < tables.length; i++) { var table = tables[i]; var tableData = table.data; + if (tableData.length + offsets.currentOffset > ttf.length) + log('blah'); ttf.set(tableData, offsets.currentOffset); offsets.currentOffset += tableData.length; From bf9092b6609043e567cd06b8ec201a3f3934cc1b Mon Sep 17 00:00:00 2001 From: sbarman Date: Tue, 16 Aug 2011 10:25:02 -0700 Subject: [PATCH 02/20] working on subroutines --- fonts.js | 40 +++++++++++++++++++++++++++++++++++++--- 1 file changed, 37 insertions(+), 3 deletions(-) diff --git a/fonts.js b/fonts.js index 66d8428e2..e38edef8d 100755 --- a/fonts.js +++ b/fonts.js @@ -1598,6 +1598,17 @@ var Type1Parser = function() { var c = ''; var count = eexecStr.length; for (var i = 0; i < count; i++) { + var getToken = function() { + while(i < count && (eexecStr[i] == ' ' || eexecStr[i] == '\n')) + ++i; + + var t = ''; + while(i < count && !(eexecStr[i] == ' ' || eexecStr[i] == '\n')) + t += eexecStr[i++]; + + return t; + } + var c = eexecStr[i]; if ((glyphsSection || subrsSection) && c == 'R') { @@ -1627,7 +1638,25 @@ var Type1Parser = function() { glyphsSection = true; break; case '/Subrs': - subrsSection = true; + ++i; + var num = parseInt(getToken()); + getToken(); // read in 'array' + for (var j = 0; j < num; ++j) { + var t = getToken(); // read in 'dup' + if (t == 'ND') + break; + var index = parseInt(getToken()); + if (index > j) + j = index; + var length = parseInt(getToken()); + getToken(); // read in 'RD' + var data = eexec.slice(i + 1, i + 1 + length); + var encoded = decrypt(data, kCharStringsEncryptionKey, 4); + var str = decodeCharString(encoded); + i = i + 1 + length; + getToken(); //read in 'NP' + program.subrs[index] = str.charstring; + } break; case '/BlueValues': case '/OtherBlues': @@ -1909,8 +1938,13 @@ CFF.prototype = { for (var i = 0; i < bias; i++) type2Subrs.push([0x0B]); - for (var i = 0; i < count; i++) - type2Subrs.push(this.flattenCharstring(type1Subrs[i], this.commandsMap)); + for (var i = 0; i < count; i++) { + var subr = type1Subrs[i]; + if (!subr) + subr = [0x0B]; + + type2Subrs.push(this.flattenCharstring(subr, this.commandsMap)); + } return type2Subrs; }, From 5b359f1c1eef9e8a102f9c93b1f27ec046567516 Mon Sep 17 00:00:00 2001 From: sbarman Date: Tue, 16 Aug 2011 14:49:12 -0700 Subject: [PATCH 03/20] still mucking with subroutines --- fonts.js | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/fonts.js b/fonts.js index e38edef8d..356d7f948 100755 --- a/fonts.js +++ b/fonts.js @@ -1469,7 +1469,7 @@ var Type1Parser = function() { var value = ''; var count = array.length; for (var i = 0; i < count; i++) { - value = parseInt(array[i]); + value = array[i]; if (value < 32) { var command = null; @@ -1479,8 +1479,8 @@ var Type1Parser = function() { // TODO Clean this code if (escape == 16) { var index = charstring.pop(); - var argc = charstring.pop(); - var data = charstring.pop(); +// var argc = charstring.pop(); +// var data = charstring.pop(); // If the flex mechanishm is not used in a font program, Adobe // state that that entries 0, 1 and 2 can simply be replace by @@ -1492,8 +1492,8 @@ var Type1Parser = function() { // This is the same things about hint replacement, if it is not used // entry 3 can be replaced by {3} if (index == 3) { - charstring.push(3); - i++; +// charstring.push(3); +// i++; continue; } } @@ -1532,11 +1532,11 @@ var Type1Parser = function() { value = command; } else if (value <= 246) { - value = parseInt(value) - 139; + value = value - 139; } else if (value <= 250) { - value = ((value - 247) * 256) + parseInt(array[++i]) + 108; + value = ((value - 247) * 256) + array[++i] + 108; } else if (value <= 254) { - value = -((value - 251) * 256) - parseInt(array[++i]) - 108; + value = -((value - 251) * 256) - array[++i] - 108; } else { value = (array[++i] & 0xff) << 24 | (array[++i] & 0xff) << 16 | (array[++i] & 0xff) << 8 | (array[++i] & 0xff) << 0; From 7d5dcb5d43936f7d4144b99c3fa4e64743990f3d Mon Sep 17 00:00:00 2001 From: sbarman Date: Thu, 21 Jul 2011 11:51:39 -0700 Subject: [PATCH 04/20] added ability to write data to file --- fonts.js | 8 ++++++++ utils/fonts_utils.js | 4 +--- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/fonts.js b/fonts.js index 356d7f948..7dfa6ead7 100755 --- a/fonts.js +++ b/fonts.js @@ -446,6 +446,14 @@ var Font = (function Font() { break; } + var fileArr = []; + file.reset(); + for (var i = 0, ii = file.length; i < ii; ++i) + fileArr.push(file[i]); + + writeToFile(data, '/tmp/' + name + '_orig'); + writeToFile(fileArr, '/tmp/' + name + '_new'); + this.data = data; this.type = properties.type; this.textMatrix = properties.textMatrix; diff --git a/utils/fonts_utils.js b/utils/fonts_utils.js index edfc22186..98ea60757 100644 --- a/utils/fonts_utils.js +++ b/utils/fonts_utils.js @@ -1,8 +1,6 @@ /* -*- Mode: Java; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- / /* vim: set shiftwidth=2 tabstop=2 autoindent cindent expandtab: */ -'use strict'; - /** * The Type2 reader code below is only used for debugging purpose since Type2 * is only a CharString format and is never used directly as a Font file. @@ -391,7 +389,7 @@ function writeToFile(aBytes, aFilePath) { var stream = Cc['@mozilla.org/network/file-output-stream;1'] .createInstance(Ci.nsIFileOutputStream); - stream.init(file, 0x04 | 0x08 | 0x20, 0x180, 0); + stream.init(file, 0x04 | 0x08 | 0x20, 0666, 0); var bos = Cc['@mozilla.org/binaryoutputstream;1'] .createInstance(Ci.nsIBinaryOutputStream); From 2ad39c20cf1f9fd597fa25e95bffbf5fcab112af Mon Sep 17 00:00:00 2001 From: sbarman Date: Thu, 21 Jul 2011 15:13:18 -0700 Subject: [PATCH 05/20] added include to fonts_util.js --- web/viewer.html | 1 + 1 file changed, 1 insertion(+) diff --git a/web/viewer.html b/web/viewer.html index 285dadb01..1abded240 100644 --- a/web/viewer.html +++ b/web/viewer.html @@ -10,6 +10,7 @@ + From 259f8c731771ab7fef5adfa8a1df76d95ce6d300 Mon Sep 17 00:00:00 2001 From: sbarman Date: Thu, 21 Jul 2011 15:13:49 -0700 Subject: [PATCH 06/20] added include to fonts_util.js --- web/viewer.html | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/web/viewer.html b/web/viewer.html index 1abded240..5498d8a1c 100644 --- a/web/viewer.html +++ b/web/viewer.html @@ -10,7 +10,7 @@ - + From 04b14f7b8a51873583a3b0f305759e26ed0a1640 Mon Sep 17 00:00:00 2001 From: sbarman Date: Thu, 21 Jul 2011 15:18:00 -0700 Subject: [PATCH 07/20] fixed reading from sream --- fonts.js | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/fonts.js b/fonts.js index 7dfa6ead7..e2f278523 100755 --- a/fonts.js +++ b/fonts.js @@ -448,11 +448,12 @@ var Font = (function Font() { var fileArr = []; file.reset(); + file = file.getBytes(); for (var i = 0, ii = file.length; i < ii; ++i) fileArr.push(file[i]); - writeToFile(data, '/tmp/' + name + '_orig'); - writeToFile(fileArr, '/tmp/' + name + '_new'); + writeToFile(data, '/tmp/' + name + '_new'); + writeToFile(fileArr, '/tmp/' + name + '_orig'); this.data = data; this.type = properties.type; From 8acc31ec83f424fafc54133abfa94c8867fbb02c Mon Sep 17 00:00:00 2001 From: sbarman Date: Tue, 16 Aug 2011 16:14:29 -0700 Subject: [PATCH 08/20] Still testing type1 charstring conversion --- fonts.js | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/fonts.js b/fonts.js index e2f278523..77183a602 100755 --- a/fonts.js +++ b/fonts.js @@ -1488,8 +1488,9 @@ var Type1Parser = function() { // TODO Clean this code if (escape == 16) { var index = charstring.pop(); -// var argc = charstring.pop(); -// var data = charstring.pop(); + var argc = charstring.pop(); + for (var j = 0; j < argc; j++) + var data = charstring.pop(); // If the flex mechanishm is not used in a font program, Adobe // state that that entries 0, 1 and 2 can simply be replace by @@ -1501,8 +1502,8 @@ var Type1Parser = function() { // This is the same things about hint replacement, if it is not used // entry 3 can be replaced by {3} if (index == 3) { -// charstring.push(3); -// i++; + charstring.push(3); + i++; continue; } } From 6fc25241cceaea916225a6ce5ce822a62af138ea Mon Sep 17 00:00:00 2001 From: Kalervo Kujala Date: Wed, 17 Aug 2011 01:33:00 +0300 Subject: [PATCH 09/20] Report results properly to test.py from driver.js. If the PDFDoc creation failed in driver.js then that was not informed back to test.py. This lead to State.remaining being off by one. And that did not let the test to end as expected. Instead the test hung indefinitely. This change now reveals TEST-UNEXPECTED-FAIL which was hidden previously. --- test/driver.js | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/test/driver.js b/test/driver.js index 92fc00af1..716046c4b 100644 --- a/test/driver.js +++ b/test/driver.js @@ -80,10 +80,19 @@ function nextTask() { } function isLastPage(task) { - return (!task.pdfDoc || (task.pageNum > task.pdfDoc.numPages)); + return (task.pageNum > task.pdfDoc.numPages); } function nextPage(task, loadError) { + var failure = loadError || ''; + + if (!task.pdfDoc) { + sendTaskResult(canvas.toDataURL('image/png'), task, failure); + log('done' + (failure ? ' (failed !: ' + failure + ')' : '') + '\n'); + ++currentTaskIdx, nextTask(); + return; + } + if (isLastPage(task)) { if (++task.round < task.rounds) { log(' Round ' + (1 + task.round) + '\n'); @@ -94,15 +103,13 @@ function nextPage(task, loadError) { } } - var failure = loadError || ''; - - var ctx = null; var page = null; + if (!failure) { try { log(' loading page ' + task.pageNum + '/' + task.pdfDoc.numPages + '... '); - ctx = canvas.getContext('2d'); + var ctx = canvas.getContext('2d'); page = task.pdfDoc.getPage(task.pageNum); var pdfToCssUnitsCoef = 96.0 / 72.0; @@ -179,7 +186,7 @@ var inFlightRequests = 0; function sendTaskResult(snapshot, task, failure) { var result = { browser: browser, id: task.id, - numPages: task.pdfDoc.numPages, + numPages: task.pdfDoc ? task.pdfDoc.numPages : 0, failure: failure, file: task.file, round: task.round, From b18b63a9a963dd5bb36547b56007f1047ddeef09 Mon Sep 17 00:00:00 2001 From: sbarman Date: Tue, 16 Aug 2011 16:47:48 -0700 Subject: [PATCH 10/20] implemented curve2, partial fix for #325 --- pdf.js | 28 ++++++++++++++++++++++++++-- 1 file changed, 26 insertions(+), 2 deletions(-) diff --git a/pdf.js b/pdf.js index 79ae776f6..081abfd20 100644 --- a/pdf.js +++ b/pdf.js @@ -4,7 +4,7 @@ 'use strict'; var ERRORS = 0, WARNINGS = 1, TODOS = 5; -var verbosity = WARNINGS; +var verbosity = TODOS; function log(msg) { if (console && console.log) @@ -4137,6 +4137,9 @@ var CanvasExtraState = (function() { this.charSpacing = 0; this.wordSpacing = 0; this.textHScale = 1; + // Path variables + this.pathX = 0; + this.pathY = 0; // Color spaces this.fillColorSpaceObj = null; this.strokeColorSpaceObj = null; @@ -4276,18 +4279,39 @@ var CanvasGraphics = (function() { // Path moveTo: function(x, y) { this.ctx.moveTo(x, y); + + var current = this.current; + current.pathX = x; + current.pathY = y; }, lineTo: function(x, y) { this.ctx.lineTo(x, y); + + var current = this.current; + current.pathX = x; + current.pathY = y; }, curveTo: function(x1, y1, x2, y2, x3, y3) { this.ctx.bezierCurveTo(x1, y1, x2, y2, x3, y3); + + var current = this.current; + current.pathX = x3; + current.pathY = y3; }, curveTo2: function(x2, y2, x3, y3) { - TODO("'v' operator: need current point in gfx context"); + var current = this.current; + this.ctx.bezierCurveTo(current.pathX, current.pathY, x2, y2, x3, y3); +// TODO("'v' operator: need current point in gfx context"); + + current.pathX = x3; + current.pathY = y3; }, curveTo3: function(x1, y1, x3, y3) { this.curveTo(x1, y1, x3, y3, x3, y3); + + var current = this.current; + current.pathX = x3; + current.pathY = y3; }, closePath: function() { this.ctx.closePath(); From ac1000cc86d41e6bf0bf027e56550744c47b4bc5 Mon Sep 17 00:00:00 2001 From: sbarman Date: Tue, 16 Aug 2011 16:49:26 -0700 Subject: [PATCH 11/20] cleanup --- pdf.js | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/pdf.js b/pdf.js index 081abfd20..d9c1007bb 100644 --- a/pdf.js +++ b/pdf.js @@ -4,7 +4,7 @@ 'use strict'; var ERRORS = 0, WARNINGS = 1, TODOS = 5; -var verbosity = TODOS; +var verbosity = WARNINGS; function log(msg) { if (console && console.log) @@ -4301,7 +4301,6 @@ var CanvasGraphics = (function() { curveTo2: function(x2, y2, x3, y3) { var current = this.current; this.ctx.bezierCurveTo(current.pathX, current.pathY, x2, y2, x3, y3); -// TODO("'v' operator: need current point in gfx context"); current.pathX = x3; current.pathY = y3; From a48a74862ac4bc2e620bb931ca1885dab25c17b4 Mon Sep 17 00:00:00 2001 From: sbarman Date: Tue, 16 Aug 2011 18:15:20 -0700 Subject: [PATCH 12/20] added stiched functions --- pdf.js | 57 +++++++++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 53 insertions(+), 4 deletions(-) diff --git a/pdf.js b/pdf.js index d9c1007bb..45f5484f6 100644 --- a/pdf.js +++ b/pdf.js @@ -5727,7 +5727,7 @@ var PDFFunction = (function() { if (!typeFn) error('Unknown type of function'); - typeFn.call(this, fn, dict); + typeFn.call(this, fn, dict, xref); }; constructor.prototype = { @@ -5872,9 +5872,58 @@ var PDFFunction = (function() { return out; } }, - constructStiched: function() { - TODO('unhandled type of function'); - this.func = function() { return [255, 105, 180]; } + constructStiched: function(fn, dict, xref) { + var domain = dict.get('Domain'); + var range = dict.get('Range'); + + if (!domain) + error('No domain'); + + var inputSize = domain.length / 2; + if (inputSize != 1) + error('Bad domain for stiched function'); + + var fnRefs = dict.get('Functions'); + var fns = []; + for (var i = 0, ii = fnRefs.length; i < ii; ++i) + fns.push(new PDFFunction(xref, xref.fetchIfRef(fnRefs[i]))); + + var bounds = dict.get('Bounds'); + var encode = dict.get('Encode'); + + this.func = function(args) { + var clip = function(v, min, max) { + if (v > max) + v = max; + else if (v < min) + v = min; + return v; + } + + // clip to domain + var v = clip(args[0], domain[0], domain[1]); + // calulate which bound the value is in + for (var i = 0, ii = bounds.length; i < ii; ++i) { + if (v < bounds[i]) + break; + } + + // encode value into domain of function + var dmin = domain[0]; + if (i > 0) + dmin = bounds[i - 1]; + var dmax = domain[1]; + if (i < bounds.length) + dmax = bounds[i]; + + var rmin = encode[2 * i]; + var rmax = encode[2 * i + 1]; + + var v2 = rmin + (v - dmin) * (rmax - rmin) / (dmax - dmin); + + // call the appropropriate function + return fns[i].func([v2]); + } }, constructPostScript: function() { TODO('unhandled type of function'); From d24a00452ea43a33ddd8d7507d8d28b53bee5415 Mon Sep 17 00:00:00 2001 From: sbarman Date: Wed, 17 Aug 2011 09:22:54 -0700 Subject: [PATCH 13/20] combined pathX/Y with x/y and wrote setCurrentPoint function --- pdf.js | 35 +++++++++++------------------------ 1 file changed, 11 insertions(+), 24 deletions(-) diff --git a/pdf.js b/pdf.js index 45f5484f6..2afed5f6b 100644 --- a/pdf.js +++ b/pdf.js @@ -4137,9 +4137,6 @@ var CanvasExtraState = (function() { this.charSpacing = 0; this.wordSpacing = 0; this.textHScale = 1; - // Path variables - this.pathX = 0; - this.pathY = 0; // Color spaces this.fillColorSpaceObj = null; this.strokeColorSpaceObj = null; @@ -4152,7 +4149,11 @@ var CanvasExtraState = (function() { constructor.prototype = { clone: function canvasextra_clone() { return Object.create(this); - } + }, + setCurrentPoint: function canvasextra_setCurrentPoint(x, y) { + this.x = x; + this.y = y; + }, }; return constructor; })(); @@ -4279,38 +4280,24 @@ var CanvasGraphics = (function() { // Path moveTo: function(x, y) { this.ctx.moveTo(x, y); - - var current = this.current; - current.pathX = x; - current.pathY = y; + this.current.setCurrentPoint(x, y); }, lineTo: function(x, y) { this.ctx.lineTo(x, y); - - var current = this.current; - current.pathX = x; - current.pathY = y; + this.current.setCurrentPoint(x, y); }, curveTo: function(x1, y1, x2, y2, x3, y3) { this.ctx.bezierCurveTo(x1, y1, x2, y2, x3, y3); - - var current = this.current; - current.pathX = x3; - current.pathY = y3; + this.current.setCurrentPoint(x3, y3); }, curveTo2: function(x2, y2, x3, y3) { var current = this.current; - this.ctx.bezierCurveTo(current.pathX, current.pathY, x2, y2, x3, y3); - - current.pathX = x3; - current.pathY = y3; + this.ctx.bezierCurveTo(current.x, current.y, x2, y2, x3, y3); + current.setCurrentPoint(x3, y3); }, curveTo3: function(x1, y1, x3, y3) { this.curveTo(x1, y1, x3, y3, x3, y3); - - var current = this.current; - current.pathX = x3; - current.pathY = y3; + this.current.setCurrentPoint(x3, y3); }, closePath: function() { this.ctx.closePath(); From c9f9b8b1935128d9e159db155f4ac7361c7e5c6b Mon Sep 17 00:00:00 2001 From: sbarman Date: Wed, 17 Aug 2011 15:55:09 -0700 Subject: [PATCH 14/20] working font --- fonts.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/fonts.js b/fonts.js index 77183a602..a55fc63a2 100755 --- a/fonts.js +++ b/fonts.js @@ -1490,7 +1490,7 @@ var Type1Parser = function() { var index = charstring.pop(); var argc = charstring.pop(); for (var j = 0; j < argc; j++) - var data = charstring.pop(); + charstring.push('drop'); // If the flex mechanishm is not used in a font program, Adobe // state that that entries 0, 1 and 2 can simply be replace by @@ -1976,6 +1976,8 @@ CFF.prototype = { 'sub': [12, 11], 'div': [12, 12], 'pop': [1, 12, 18], +// 'pop': [], + 'drop' : [12, 18], 'endchar': 14, 'rmoveto': 21, 'hmoveto': 22, From 32bde231570f9c12bcaafa82f58817ae1be1e0e6 Mon Sep 17 00:00:00 2001 From: sbarman Date: Wed, 17 Aug 2011 16:04:14 -0700 Subject: [PATCH 15/20] cleanup --- fonts.js | 1 - 1 file changed, 1 deletion(-) diff --git a/fonts.js b/fonts.js index a55fc63a2..eba88c6bf 100755 --- a/fonts.js +++ b/fonts.js @@ -1976,7 +1976,6 @@ CFF.prototype = { 'sub': [12, 11], 'div': [12, 12], 'pop': [1, 12, 18], -// 'pop': [], 'drop' : [12, 18], 'endchar': 14, 'rmoveto': 21, From b1aab2f730a430e79c257250f3593b0994efd08c Mon Sep 17 00:00:00 2001 From: sbarman Date: Thu, 18 Aug 2011 20:17:54 -0700 Subject: [PATCH 16/20] Fixed fonts to use strings instead of Uint8Arrays --- fonts.js | 94 +++++++++++++++++++++++--------------------------------- 1 file changed, 39 insertions(+), 55 deletions(-) diff --git a/fonts.js b/fonts.js index 2c737b050..346b547ea 100755 --- a/fonts.js +++ b/fonts.js @@ -4,11 +4,6 @@ 'use strict'; var isWorker = (typeof window == 'undefined'); -/** - * Maximum file size of the font. - */ -var kMaxFontFileSize = 300000; - /** * Maximum time to wait for a font to be loaded by font-face rules. */ @@ -466,6 +461,14 @@ var Font = (function Font() { return array; }; + + function arrayToString(arr) { + var str = ""; + for (var i = 0; i < arr.length; ++i) + str += String.fromCharCode(arr[i]); + + return str; + }; function int16(bytes) { return (bytes[0] << 8) + (bytes[1] & 0xff); @@ -502,7 +505,7 @@ var Font = (function Font() { String.fromCharCode(value & 0xff); }; - function createOpenTypeHeader(sfnt, file, offsets, numTables) { + function createOpenTypeHeader(sfnt, file, numTables) { // sfnt version (4 bytes) var header = sfnt; @@ -520,14 +523,13 @@ var Font = (function Font() { // rangeShift (2 bytes) header += string16(numTables * 16 - searchRange); - file.set(stringToArray(header), offsets.currentOffset); - offsets.currentOffset += header.length; - offsets.virtualOffset += header.length; + file.file += header; + file.virtualOffset += header.length; }; - function createTableEntry(file, offsets, tag, data) { + function createTableEntry(file, tag, data) { // offset - var offset = offsets.virtualOffset; + var offset = file.virtualOffset; // length var length = data.length; @@ -536,8 +538,8 @@ var Font = (function Font() { while (data.length & 3) data.push(0x00); - while (offsets.virtualOffset & 3) - offsets.virtualOffset++; + while (file.virtualOffset & 3) + file.virtualOffset++; // checksum var checksum = 0, n = data.length; @@ -547,11 +549,8 @@ var Font = (function Font() { var tableEntry = (tag + string32(checksum) + string32(offset) + string32(length)); - tableEntry = stringToArray(tableEntry); - file.set(tableEntry, offsets.currentOffset); - - offsets.currentOffset += tableEntry.length; - offsets.virtualOffset += data.length; + file.file += tableEntry; + file.virtualOffset += data.length; }; function getRanges(glyphs) { @@ -973,23 +972,19 @@ var Font = (function Font() { tables.push(table); } - // Create a new file to hold the new version of our truetype with a new - // header and new offsets - var ttf = new Uint8Array(kMaxFontFileSize*3); - - // The offsets object holds at the same time a representation of where - // to write the table entry information about a table and another offset - // representing the offset where to put the actual data of a particular - // table var numTables = header.numTables + requiredTables.length; - var offsets = { - currentOffset: 0, + + // header and new offsets. Table entry information is appended to the + // end of file. The virtualOffset represents where to put the actual + // data of a particular table; + var ttf = { + file: "", virtualOffset: numTables * (4 * 4) - }; + } // The new numbers of tables will be the last one plus the num // of missing tables - createOpenTypeHeader('\x00\x01\x00\x00', ttf, offsets, numTables); + createOpenTypeHeader('\x00\x01\x00\x00', ttf, numTables); if (requiredTables.indexOf('OS/2') != -1) { tables.push({ @@ -1095,28 +1090,21 @@ var Font = (function Font() { var tableData = table.data; for (var j = 0; j < tableData.length; j++) data.push(tableData[j]); - createTableEntry(ttf, offsets, table.tag, data); + createTableEntry(ttf, table.tag, data); } // Add the table datas for (var i = 0; i < tables.length; i++) { var table = tables[i]; var tableData = table.data; - if (tableData.length + offsets.currentOffset > ttf.length) - log('blah'); - ttf.set(tableData, offsets.currentOffset); - offsets.currentOffset += tableData.length; + ttf.file += arrayToString(tableData); // 4-byte aligned data - while (offsets.currentOffset & 3) - offsets.currentOffset++; + while (ttf.file.length & 3) + ttf.file += String.fromCharCode(0); } - var fontData = []; - for (var i = 0; i < offsets.currentOffset; i++) - fontData.push(ttf[i]); - - return fontData; + return stringToArray(ttf.file); }, convert: function font_convert(fontName, font, properties) { @@ -1133,13 +1121,13 @@ var Font = (function Font() { // representing the offset where to draw the actual data of a particular // table var kRequiredTablesCount = 9; - var offsets = { - currentOffset: 0, - virtualOffset: 9 * (4 * 4) - }; - var otf = new Uint8Array(kMaxFontFileSize); - createOpenTypeHeader('\x4F\x54\x54\x4F', otf, offsets, 9); + var otf = { + file: "", + virtualOffset: 9 * (4 * 4) + } + + createOpenTypeHeader('\x4F\x54\x54\x4F', otf, 9); var charstrings = font.charstrings; properties.fixedPitch = isFixedPitch(charstrings); @@ -1223,18 +1211,14 @@ var Font = (function Font() { }; for (var field in fields) - createTableEntry(otf, offsets, field, fields[field]); + createTableEntry(otf, field, fields[field]); for (var field in fields) { var table = fields[field]; - otf.set(table, offsets.currentOffset); - offsets.currentOffset += table.length; + otf.file += arrayToString(table); } - var fontData = []; - for (var i = 0; i < offsets.currentOffset; i++) - fontData.push(otf[i]); - return fontData; + return stringToArray(otf.file); }, bindWorker: function font_bindWorker(data) { From 085f8dc1c917a9489e078947c25d787870d6b418 Mon Sep 17 00:00:00 2001 From: sbarman Date: Thu, 18 Aug 2011 20:40:16 -0700 Subject: [PATCH 17/20] cleanup --- fonts.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/fonts.js b/fonts.js index 346b547ea..ce49a82b9 100755 --- a/fonts.js +++ b/fonts.js @@ -980,7 +980,7 @@ var Font = (function Font() { var ttf = { file: "", virtualOffset: numTables * (4 * 4) - } + }; // The new numbers of tables will be the last one plus the num // of missing tables @@ -1125,7 +1125,7 @@ var Font = (function Font() { var otf = { file: "", virtualOffset: 9 * (4 * 4) - } + }; createOpenTypeHeader('\x4F\x54\x54\x4F', otf, 9); From 5f233d7cdede04ad735df9cf6c7c5c9c190ffd10 Mon Sep 17 00:00:00 2001 From: sbarman Date: Thu, 18 Aug 2011 22:05:08 -0700 Subject: [PATCH 18/20] cleanup --- fonts.js | 3 --- utils/fonts_utils.js | 5 ++++- web/viewer.html | 1 - 3 files changed, 4 insertions(+), 5 deletions(-) diff --git a/fonts.js b/fonts.js index 1b2da707a..064459df2 100755 --- a/fonts.js +++ b/fonts.js @@ -447,9 +447,6 @@ var Font = (function Font() { for (var i = 0, ii = file.length; i < ii; ++i) fileArr.push(file[i]); - writeToFile(data, '/tmp/' + name + '_new'); - writeToFile(fileArr, '/tmp/' + name + '_orig'); - this.data = data; this.type = properties.type; this.textMatrix = properties.textMatrix; diff --git a/utils/fonts_utils.js b/utils/fonts_utils.js index 98ea60757..194bf6b55 100644 --- a/utils/fonts_utils.js +++ b/utils/fonts_utils.js @@ -1,6 +1,9 @@ /* -*- Mode: Java; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- / /* vim: set shiftwidth=2 tabstop=2 autoindent cindent expandtab: */ +'use strict'; + + /** * The Type2 reader code below is only used for debugging purpose since Type2 * is only a CharString format and is never used directly as a Font file. @@ -389,7 +392,7 @@ function writeToFile(aBytes, aFilePath) { var stream = Cc['@mozilla.org/network/file-output-stream;1'] .createInstance(Ci.nsIFileOutputStream); - stream.init(file, 0x04 | 0x08 | 0x20, 0666, 0); + stream.init(file, 0x04 | 0x08 | 0x20, 0x180, 0); var bos = Cc['@mozilla.org/binaryoutputstream;1'] .createInstance(Ci.nsIBinaryOutputStream); diff --git a/web/viewer.html b/web/viewer.html index 5498d8a1c..285dadb01 100644 --- a/web/viewer.html +++ b/web/viewer.html @@ -10,7 +10,6 @@ - From 0923e940d1e6838b64b532cec01bc9ed03a3e707 Mon Sep 17 00:00:00 2001 From: sbarman Date: Thu, 18 Aug 2011 22:05:54 -0700 Subject: [PATCH 19/20] cleanup --- utils/fonts_utils.js | 1 - 1 file changed, 1 deletion(-) diff --git a/utils/fonts_utils.js b/utils/fonts_utils.js index 194bf6b55..edfc22186 100644 --- a/utils/fonts_utils.js +++ b/utils/fonts_utils.js @@ -3,7 +3,6 @@ 'use strict'; - /** * The Type2 reader code below is only used for debugging purpose since Type2 * is only a CharString format and is never used directly as a Font file. From 28051220236e78e153b648f0599fe9067521487d Mon Sep 17 00:00:00 2001 From: sbarman Date: Fri, 19 Aug 2011 08:19:32 -0700 Subject: [PATCH 20/20] cleanup --- fonts.js | 6 ------ 1 file changed, 6 deletions(-) diff --git a/fonts.js b/fonts.js index 064459df2..ce49a82b9 100755 --- a/fonts.js +++ b/fonts.js @@ -441,12 +441,6 @@ var Font = (function Font() { break; } - var fileArr = []; - file.reset(); - file = file.getBytes(); - for (var i = 0, ii = file.length; i < ii; ++i) - fileArr.push(file[i]); - this.data = data; this.type = properties.type; this.textMatrix = properties.textMatrix;