pdf.js/src/core/ps_parser.js

244 lines
6.4 KiB
JavaScript
Raw Normal View History

2014-01-28 04:50:30 +09:00
/* Copyright 2014 Mozilla Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/* eslint no-var: error */
2014-01-28 04:50:30 +09:00
import { FormatError, isSpace, shadow } from '../shared/util';
import { EOF } from './primitives';
class PostScriptParser {
constructor(lexer) {
2014-01-28 04:50:30 +09:00
this.lexer = lexer;
this.operators = [];
this.token = null;
this.prev = null;
}
nextToken() {
this.prev = this.token;
this.token = this.lexer.getToken();
}
accept(type) {
if (this.token.type === type) {
2014-01-28 04:50:30 +09:00
this.nextToken();
return true;
}
return false;
}
2014-01-28 04:50:30 +09:00
expect(type) {
if (this.accept(type)) {
return true;
}
throw new FormatError(
`Unexpected symbol: found ${this.token.type} expected ${type}.`);
}
parse() {
this.nextToken();
this.expect(PostScriptTokenTypes.LBRACE);
this.parseBlock();
this.expect(PostScriptTokenTypes.RBRACE);
return this.operators;
}
parseBlock() {
while (true) {
if (this.accept(PostScriptTokenTypes.NUMBER)) {
this.operators.push(this.prev.value);
} else if (this.accept(PostScriptTokenTypes.OPERATOR)) {
this.operators.push(this.prev.value);
2014-01-28 04:50:30 +09:00
} else if (this.accept(PostScriptTokenTypes.LBRACE)) {
this.parseCondition();
2014-01-28 04:50:30 +09:00
} else {
return;
2014-01-28 04:50:30 +09:00
}
}
}
2014-01-28 04:50:30 +09:00
parseCondition() {
// Add two place holders that will be updated later
const conditionLocation = this.operators.length;
this.operators.push(null, null);
this.parseBlock();
this.expect(PostScriptTokenTypes.RBRACE);
if (this.accept(PostScriptTokenTypes.IF)) {
// The true block is right after the 'if' so it just falls through on true
// else it jumps and skips the true block.
this.operators[conditionLocation] = this.operators.length;
this.operators[conditionLocation + 1] = 'jz';
} else if (this.accept(PostScriptTokenTypes.LBRACE)) {
const jumpLocation = this.operators.length;
this.operators.push(null, null);
const endOfTrue = this.operators.length;
this.parseBlock();
this.expect(PostScriptTokenTypes.RBRACE);
this.expect(PostScriptTokenTypes.IFELSE);
// The jump is added at the end of the true block to skip the false block.
this.operators[jumpLocation] = this.operators.length;
this.operators[jumpLocation + 1] = 'j';
this.operators[conditionLocation] = endOfTrue;
this.operators[conditionLocation + 1] = 'jz';
} else {
throw new FormatError('PS Function: error parsing conditional.');
}
}
}
const PostScriptTokenTypes = {
2014-01-28 04:50:30 +09:00
LBRACE: 0,
RBRACE: 1,
NUMBER: 2,
OPERATOR: 3,
IF: 4,
Fix inconsistent spacing and trailing commas in objects in `src/core/` files, so we can enable the `comma-dangle` and `object-curly-spacing` ESLint rules later on *Unfortunately this patch is fairly big, even though it only covers the `src/core` folder, but splitting it even further seemed difficult.* http://eslint.org/docs/rules/comma-dangle http://eslint.org/docs/rules/object-curly-spacing Given that we currently have quite inconsistent object formatting, fixing this in *one* big patch probably wouldn't be feasible (since I cannot imagine anyone wanting to review that); hence I've opted to try and do this piecewise instead. Please note: This patch was created automatically, using the ESLint --fix command line option. In a couple of places this caused lines to become too long, and I've fixed those manually; please refer to the interdiff below for the only hand-edits in this patch. ```diff diff --git a/src/core/evaluator.js b/src/core/evaluator.js index abab9027..dcd3594b 100644 --- a/src/core/evaluator.js +++ b/src/core/evaluator.js @@ -2785,7 +2785,8 @@ var EvaluatorPreprocessor = (function EvaluatorPreprocessorClosure() { t['Tz'] = { id: OPS.setHScale, numArgs: 1, variableArgs: false, }; t['TL'] = { id: OPS.setLeading, numArgs: 1, variableArgs: false, }; t['Tf'] = { id: OPS.setFont, numArgs: 2, variableArgs: false, }; - t['Tr'] = { id: OPS.setTextRenderingMode, numArgs: 1, variableArgs: false, }; + t['Tr'] = { id: OPS.setTextRenderingMode, numArgs: 1, + variableArgs: false, }; t['Ts'] = { id: OPS.setTextRise, numArgs: 1, variableArgs: false, }; t['Td'] = { id: OPS.moveText, numArgs: 2, variableArgs: false, }; t['TD'] = { id: OPS.setLeadingMoveText, numArgs: 2, variableArgs: false, }; diff --git a/src/core/jbig2.js b/src/core/jbig2.js index 5a17d482..71671541 100644 --- a/src/core/jbig2.js +++ b/src/core/jbig2.js @@ -123,19 +123,22 @@ var Jbig2Image = (function Jbig2ImageClosure() { { x: -1, y: -1, }, { x: 0, y: -1, }, { x: 1, y: -1, }, { x: -2, y: 0, }, { x: -1, y: 0, }], [{ x: -3, y: -1, }, { x: -2, y: -1, }, { x: -1, y: -1, }, { x: 0, y: -1, }, - { x: 1, y: -1, }, { x: -4, y: 0, }, { x: -3, y: 0, }, { x: -2, y: 0, }, { x: -1, y: 0, }] + { x: 1, y: -1, }, { x: -4, y: 0, }, { x: -3, y: 0, }, { x: -2, y: 0, }, + { x: -1, y: 0, }] ]; var RefinementTemplates = [ { coding: [{ x: 0, y: -1, }, { x: 1, y: -1, }, { x: -1, y: 0, }], - reference: [{ x: 0, y: -1, }, { x: 1, y: -1, }, { x: -1, y: 0, }, { x: 0, y: 0, }, - { x: 1, y: 0, }, { x: -1, y: 1, }, { x: 0, y: 1, }, { x: 1, y: 1, }], + reference: [{ x: 0, y: -1, }, { x: 1, y: -1, }, { x: -1, y: 0, }, + { x: 0, y: 0, }, { x: 1, y: 0, }, { x: -1, y: 1, }, + { x: 0, y: 1, }, { x: 1, y: 1, }], }, { - coding: [{ x: -1, y: -1, }, { x: 0, y: -1, }, { x: 1, y: -1, }, { x: -1, y: 0, }], - reference: [{ x: 0, y: -1, }, { x: -1, y: 0, }, { x: 0, y: 0, }, { x: 1, y: 0, }, - { x: 0, y: 1, }, { x: 1, y: 1, }], + coding: [{ x: -1, y: -1, }, { x: 0, y: -1, }, { x: 1, y: -1, }, + { x: -1, y: 0, }], + reference: [{ x: 0, y: -1, }, { x: -1, y: 0, }, { x: 0, y: 0, }, + { x: 1, y: 0, }, { x: 0, y: 1, }, { x: 1, y: 1, }], } ]; ```
2017-06-02 18:16:24 +09:00
IFELSE: 5,
2014-01-28 04:50:30 +09:00
};
const PostScriptToken = (function PostScriptTokenClosure() {
const opCache = Object.create(null);
2014-01-28 04:50:30 +09:00
class PostScriptToken {
constructor(type, value) {
this.type = type;
this.value = value;
}
2014-01-28 04:50:30 +09:00
static getOperator(op) {
const opValue = opCache[op];
if (opValue) {
return opValue;
}
return opCache[op] = new PostScriptToken(PostScriptTokenTypes.OPERATOR,
op);
}
2014-01-28 04:50:30 +09:00
static get LBRACE() {
return shadow(this, 'LBRACE',
new PostScriptToken(PostScriptTokenTypes.LBRACE, '{'));
}
static get RBRACE() {
return shadow(this, 'RBRACE',
new PostScriptToken(PostScriptTokenTypes.RBRACE, '}'));
}
static get IF() {
return shadow(this, 'IF',
new PostScriptToken(PostScriptTokenTypes.IF, 'IF'));
}
static get IFELSE() {
return shadow(this, 'IFELSE',
new PostScriptToken(PostScriptTokenTypes.IFELSE, 'IFELSE'));
}
}
2014-01-28 04:50:30 +09:00
return PostScriptToken;
})();
class PostScriptLexer {
constructor(stream) {
2014-01-28 04:50:30 +09:00
this.stream = stream;
this.nextChar();
this.strBuf = [];
2014-01-28 04:50:30 +09:00
}
nextChar() {
return (this.currentChar = this.stream.getByte());
}
getToken() {
let comment = false;
let ch = this.currentChar;
// skip comments
while (true) {
if (ch < 0) {
return EOF;
2014-01-28 04:50:30 +09:00
}
if (comment) {
if (ch === 0x0A || ch === 0x0D) {
comment = false;
2014-01-28 04:50:30 +09:00
}
} else if (ch === 0x25) { // '%'
comment = true;
} else if (!isSpace(ch)) {
break;
2014-01-28 04:50:30 +09:00
}
ch = this.nextChar();
}
switch (ch | 0) {
case 0x30: case 0x31: case 0x32: case 0x33: case 0x34: // '0'-'4'
case 0x35: case 0x36: case 0x37: case 0x38: case 0x39: // '5'-'9'
case 0x2B: case 0x2D: case 0x2E: // '+', '-', '.'
return new PostScriptToken(PostScriptTokenTypes.NUMBER,
this.getNumber());
case 0x7B: // '{'
this.nextChar();
return PostScriptToken.LBRACE;
case 0x7D: // '}'
this.nextChar();
return PostScriptToken.RBRACE;
}
// operator
const strBuf = this.strBuf;
strBuf.length = 0;
strBuf[0] = String.fromCharCode(ch);
while ((ch = this.nextChar()) >= 0 && // and 'A'-'Z', 'a'-'z'
((ch >= 0x41 && ch <= 0x5A) || (ch >= 0x61 && ch <= 0x7A))) {
strBuf.push(String.fromCharCode(ch));
}
const str = strBuf.join('');
switch (str.toLowerCase()) {
case 'if':
return PostScriptToken.IF;
case 'ifelse':
return PostScriptToken.IFELSE;
default:
return PostScriptToken.getOperator(str);
}
}
getNumber() {
let ch = this.currentChar;
const strBuf = this.strBuf;
strBuf.length = 0;
strBuf[0] = String.fromCharCode(ch);
while ((ch = this.nextChar()) >= 0) {
if ((ch >= 0x30 && ch <= 0x39) || // '0'-'9'
ch === 0x2D || ch === 0x2E) { // '-', '.'
strBuf.push(String.fromCharCode(ch));
} else {
break;
}
}
const value = parseFloat(strBuf.join(''));
if (isNaN(value)) {
throw new FormatError(`Invalid floating point number: ${value}`);
}
return value;
}
}
export {
PostScriptLexer,
PostScriptParser,
};