Skip to content

Commit

Permalink
improvements for ximera.cloud
Browse files Browse the repository at this point in the history
  • Loading branch information
kisonecat committed Jun 3, 2020
1 parent 0159488 commit 63ff28b
Show file tree
Hide file tree
Showing 13 changed files with 77 additions and 65 deletions.
2 changes: 1 addition & 1 deletion fonts.css
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
@font-face { font-family: esint10; src: url('./esint10.ttf'); }
@font-face { font-family: esint10; src: url('./esint/esint10.ttf'); }
@font-face { font-family: cmb10; src: url('bakoma/ttf/cmb10.ttf'); }
@font-face { font-family: cmbsy10; src: url('bakoma/ttf/cmbsy10.ttf'); }
@font-face { font-family: cmbsy6; src: url('bakoma/ttf/cmbsy6.ttf'); }
Expand Down
5 changes: 3 additions & 2 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { dvi2html } from "./src";
import { Writable } from 'stream';

let fonts = "";
fonts = fonts + `@font-face { font-family: esint10; src: url('./esint10.ttf'); }\n`;
fonts = fonts + `@font-face { font-family: esint10; src: url('./esint/esint10.ttf'); }\n`;
fs.readdirSync('./bakoma/ttf').forEach(file => {
let name = file.replace(/.ttf/, '');
fonts = fonts + `@font-face { font-family: ${name}; src: url('bakoma/ttf/${file}'); }\n`;
Expand All @@ -16,6 +16,7 @@ fs.writeFileSync("fonts.css", fonts);
let filename = 'sample.dvi';

let stream = fs.createReadStream(filename, { highWaterMark: 256 });
var buffer = fs.readFileSync(filename);

let html = "";
html = html + "<!doctype html>\n";
Expand All @@ -37,7 +38,7 @@ const myWritable = new Writable({
});

async function main() {
await dvi2html( stream, myWritable );
dvi2html( buffer, myWritable );

html = html + '</div>\n';
html = html + '</body>\n';
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
"types": "lib/index.d.ts",
"scripts": {
"prepare": "npm run build",
"build": "node tools/tfm2json.js && tsc"
"compile": "tsc",
"build": "node tools/tfm2json.js && npm run compile"
},
"files": [
"lib/**/*"
Expand Down
29 changes: 17 additions & 12 deletions src/html.ts
Original file line number Diff line number Diff line change
Expand Up @@ -151,18 +151,23 @@ export default class HTMLMachine extends Machine {
textDepth = Math.max(textDepth, metrics.depth);

// This is ridiculous.
if ((c >= 0) && (c <= 9)) {
htmlText += `&#${161 + c};`;
} else if ((c >= 10) && (c <= 19)) {
htmlText += `&#${173 + c - 10};`;
} else if (c == 20) {
htmlText += `&#${8729};`; // O RLLY?!
} else if ((c >= 21) && (c <= 32)) {
htmlText += `&#${184 + c - 21};`;
} else if (c == 127) {
htmlText += `&#${196};`;
} else {
htmlText += String.fromCharCode(c);
if (this.font.name === 'esint10') {
htmlText += `&#${c + 65 - 1};`;
}
else {
if ((c >= 0) && (c <= 9)) {
htmlText += `&#${161 + c};`;
} else if ((c >= 10) && (c <= 19)) {
htmlText += `&#${173 + c - 10};`;
} else if (c == 20) {
htmlText += `&#${8729};`; // O RLLY?!
} else if ((c >= 21) && (c <= 32)) {
htmlText += `&#${184 + c - 21};`;
} else if (c == 127) {
htmlText += `&#${196};`;
} else {
htmlText += String.fromCharCode(c);
}
}
}

Expand Down
18 changes: 15 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@ import papersize from "./specials/papersize";
import title from "./specials/title";
import ximera from "./specials/ximera";

import VDomMachine from "./vdom";
import HTMLMachine from "./html";
import TextMachine from "./text";

export var Machines = { HTML: HTMLMachine,
vdom: VDomMachine,
text: TextMachine };

import { dviParser, execute, mergeText } from "./parser";
Expand All @@ -21,15 +23,25 @@ export var specials = {
papersize: papersize
};

export async function dvi2html( dviStream, htmlStream ) {
function dvi2html( dviStream, htmlStream ) {
let parser = ximera(title(papersize(html(svg(color(mergeText(dviParser(dviStream))))))));

let machine = new HTMLMachine( htmlStream );

await execute( parser, machine );
execute( parser, machine );

return machine;
}

function dvi2vdom( dviStream, h, callback ) {
let parser = ximera(title(papersize(html(svg(color(mergeText(dviParser(dviStream))))))));

let machine = new VDomMachine( h, callback );

execute( parser, machine );

return machine;
}

import { tfmData } from "./tfm/index";
export { tfmData };
export { tfmData, dvi2html, dvi2vdom };
58 changes: 25 additions & 33 deletions src/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -872,53 +872,45 @@ function parseCommand( opcode : Opcode, buffer : Buffer ) : Command | void {
return undefined;
}

export async function* dviParser(stream) {
let buffer = Buffer.alloc(0);
export function* dviParser(buffer) {
let isAfterPostamble = false;
let offset = 0;

for await (const chunk of stream) {
buffer = Buffer.concat([buffer, chunk]);
let offset = 0;

while(offset < buffer.length) {
let opcode : Opcode = buffer.readUInt8(offset);

if (isAfterPostamble) {
if (opcode == 223) {
offset++;
continue;
} else {
throw Error('Only 223 bytes are permitted after the post-postamble.');
}
while(offset < buffer.length) {
let opcode : Opcode = buffer.readUInt8(offset);

if (isAfterPostamble) {
if (opcode == 223) {
offset++;
continue;
} else {
throw Error('Only 223 bytes are permitted after the post-postamble.');
}

let command = parseCommand( opcode, buffer.slice(offset+1) );

if (command) {
yield command;
offset += command.length;

if (command.opcode == Opcode.post_post)
isAfterPostamble = true;
} else
break;
}

let command = parseCommand( opcode, buffer.slice(offset+1) );

buffer = buffer.slice(offset);
if (command) {
yield command;
offset += command.length;

if (command.opcode == Opcode.post_post)
isAfterPostamble = true;
} else
break;
}
}

export async function execute(commands, machine) {
for await (const command of commands) {
// console.log(command.toString());
export function execute(commands, machine) {
for (const command of commands) {
command.execute(machine);
}
}

export async function* merge(commands, filter, merge) {
export function* merge(commands, filter, merge) {
let queue = [];

for await (const command of commands) {
for (const command of commands) {
if (filter(command)) {
queue.push( command );
} else {
Expand Down
4 changes: 2 additions & 2 deletions src/specials/color.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,10 @@ function texColor(name) {
return 'black';
}

export default async function* (commands) {
export default function* (commands) {
let queue = [];

for await (const command of commands) {
for (const command of commands) {
if (! command.special) {
yield command;
} else {
Expand Down
4 changes: 2 additions & 2 deletions src/specials/html.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ class HTML extends DviCommand {
}
}

async function* specialsToHTML(commands) {
for await (const command of commands) {
function* specialsToHTML(commands) {
for (const command of commands) {
if (! command.special) {
yield command;
} else {
Expand Down
4 changes: 2 additions & 2 deletions src/specials/papersize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ class Papersize extends DviCommand {
}
}

export default async function*(commands) {
for await (const command of commands) {
export default function*(commands) {
for (const command of commands) {
if (! command.special) {
yield command;
} else {
Expand Down
4 changes: 2 additions & 2 deletions src/specials/svg.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ class EndSVG extends DviCommand {
}
}

async function* specialsToSVG(commands) {
for await (const command of commands) {
function* specialsToSVG(commands) {
for (const command of commands) {
if (! command.special) {
yield command;
} else {
Expand Down
4 changes: 2 additions & 2 deletions src/specials/title.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ class Title extends DviCommand {
}
}

async function* specialsToTitle(commands) {
for await (const command of commands) {
function* specialsToTitle(commands) {
for (const command of commands) {
if (! command.special) {
yield command;
} else {
Expand Down
4 changes: 2 additions & 2 deletions src/specials/ximera.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ class XimeraRestore extends DviCommand {
}


async function* specialsToXimera(commands) {
for await (const command of commands) {
function* specialsToXimera(commands) {
for (const command of commands) {
if (! command.special) {
yield command;
} else {
Expand Down
3 changes: 2 additions & 1 deletion tools/tfm2json.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ var desiredFonts = [
"eurm5", "eurm6", "eurm7", "eurm8", "eurm9", "eusb10", "eusb5",
"eusb6", "eusb7", "eusb8", "eusb9", "eusm10", "eusm5", "eusm6",
"eusm7", "eusm8", "eusm9", "msam10", "msam5", "msam6", "msam7",
"msam8", "msam9", "msbm10", "msbm5", "msbm6", "msbm7", "msbm8", "msbm9"
"msam8", "msam9", "msbm10", "msbm5", "msbm6", "msbm7", "msbm8", "msbm9",
"esint10"
];

desiredFonts.forEach( function(fontname) {
Expand Down

0 comments on commit 63ff28b

Please sign in to comment.