-
Notifications
You must be signed in to change notification settings - Fork 1
/
page.ts
208 lines (193 loc) · 7.52 KB
/
page.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
import moment from 'moment';
import * as codes from './teletext_codes.json';
import { b64ToMode7RAM, rawToB64, rawToJsStringLiteral } from './format';
enum FrameFormat {
Mode7 = 1,
EditTF = 2,
JSLiteral = 3,
}
/**
* Page
*
* Represents a carousel of one or more frames, where a frame is a grid
* of Teletext characters and/or display codes. Each page has a number,
* which is used by the user to select or recall the page for viewing.
* A page can also have a name, for example to allow other pages to
* refer to them, or for logging purposes.
*/
class Page {
/**
* In CastleText the page number is a three-digit hexadecimal number.
* The number is stored as a string to allow compatibility
* with JSON.
*/
readonly pageNumber:string;
/**
* The page name - for reference in logs or when a generated page
* needs to refer back up to a parent menu page
*/
readonly name:string;
protected _rawFrames:string[];
get rawFrames():string[] {
return this._rawFrames;
}
constructor(pageNumber:string, name:string = 'Unnamed Section') {
this.pageNumber = pageNumber;
this.name = name;
}
getDisplayFrame(index:number, outputFormat:FrameFormat = FrameFormat.Mode7) {
const currentDate = moment().format('ddd DD MMM Y' + codes.TEXT_YELLOW + 'HH:mm.ss');
const header = `P${this.pageNumber}${codes.TEXT_RED}CastleText${codes.TEXT_CYAN}${currentDate}`;
const displayFrame = header + this.rawFrames[index];
switch (outputFormat) {
case FrameFormat.Mode7:
return displayFrame;
case FrameFormat.JSLiteral:
return rawToJsStringLiteral(displayFrame);
case FrameFormat.EditTF:
return rawToB64(displayFrame);
}
}
}
/**
* StaticPage
*
* Represents a Teletext page which has content fixed when the server starts.
*
* Static pages are an array of one or more display frames provided in edit.tf
* format. The format consists of a frame of 25 rows of 40 7-bit Teletext codes/
* characters, which are then base-64 encoded (6 bits per b64 character).
*
* This class converts the edit.tf format frames into strings where each
* character is a Teletext code/character, and removes the first line/row of
* the frame if there are 25 rows, so that we can add our own header line.
* Frames without the header line at the top are referred to as 'raw' frames.
*/
class StaticPage extends Page {
constructor(pageNumber:string, editTfFrames:string[], name?:string) {
super(pageNumber, name);
const rawFrames = [];
for (const editTfFrame of editTfFrames) {
const headedFrame = b64ToMode7RAM(editTfFrame);
if (headedFrame.length === (24 * 40)) {
// Static frame only had 24 lines which isn't typical, but means we
// can just take all of them for content.
rawFrames.push(headedFrame);
} else if (headedFrame.length === (25 * 40)) {
// Static frame included a header line, as expected. Strip this, i.e.
// just keep the bottom 24 lines of the static frame, as CastleText
// will include its own header line at the top.
rawFrames.push(headedFrame.slice(-(24 * 40)));
} else {
console.log(`Static frame for P${this.pageNumber} had unexpected length, skipping...`);
}
}
this._rawFrames = rawFrames;
}
}
class PageTemplate {
readonly masthead:string;
readonly footerPrefix:string;
constructor(sectionName:string, masthead?:string, footerPrefix?:string) {
if (masthead && footerPrefix) {
this.masthead = masthead;
this.footerPrefix = footerPrefix;
} else {
var masthead:string = (codes.TEXT_BLUE + codes.NEW_BACKGRD + codes.DOUBLE_HEIGHT + codes.TEXT_WHITE + '>' + codes.TEXT_CYAN + sectionName).padEnd(40);
masthead += masthead;
masthead = masthead + (codes.GFX_BLUE + codes.NEW_BACKGRD).padEnd(40);
this.masthead = masthead;
this.footerPrefix = codes.TEXT_BLUE + codes.NEW_BACKGRD + codes.TEXT_CYAN;
}
}
}
class GeneratedPage extends Page {
readonly parent:Page;
readonly template:PageTemplate;
protected lines:string[] = [];
constructor(pageNumber:string, parent:Page, template:PageTemplate, name?:string) {
super(pageNumber, name);
this.parent = parent;
this.template = template;
}
addContentLine(line:string):void {
this.lines.push(line);
}
protected getContentFrames():string[] {
const CONTENT_FRAME_HEIGHT = 18;
let contentFrames = [];
let contentFrame = '';
let frameLinesLeft = CONTENT_FRAME_HEIGHT;
for (let line = 0; line < this.lines.length; line++) {
if (frameLinesLeft === CONTENT_FRAME_HEIGHT && this.lines[line] === codes.BLANK_LINE) {
// Ignore this blank line as we're at the top of a new frame
} else {
contentFrame += this.lines[line];
}
if (line === (this.lines.length - 1)) {
// We've used the last content line, so fill any remaining frame space with blank lines
while ((contentFrame.length / 40) < CONTENT_FRAME_HEIGHT) contentFrame += codes.BLANK_LINE;
}
frameLinesLeft = CONTENT_FRAME_HEIGHT - (Math.trunc(contentFrame.length / 40));
if (frameLinesLeft === 0) {
// We've completed a content frame
contentFrames.push(contentFrame);
contentFrame = '';
frameLinesLeft = CONTENT_FRAME_HEIGHT;
}
}
return contentFrames;
}
/**
* Get raw frames for this page.
*
* A raw frame is an uncompressed array of BBC Mode 7/Teletext characters,
* representing 24 display lines, each of 40 characters.
*
* The display is 25 lines high - these raw frames don't include the
* header line with the time and date at the top of the display.
*/
get rawFrames():string[] {
const contentFrames = this.getContentFrames();
let rawFrames = [];
for (let f = 0; f < contentFrames.length; f++) {
let output = this.template.masthead;
// Top pagination line, just below masthead
if (contentFrames.length > 1) {
output += `${codes.TEXT_CYAN}${f + 1}/${contentFrames.length}`.padStart(40);
} else {
output += codes.BLANK_LINE;
}
// Frame content lines
output += contentFrames[f];
// Bottom pagination line, just above footer line
if (f < contentFrames.length - 1) {
output += `${codes.TEXT_CYAN}contd...`.padStart(40);
} else {
output += codes.BLANK_LINE;
}
// Footer line
let footer = '';
footer += this.template.footerPrefix;
footer += this.parent.name;
footer = footer.padEnd(36);
footer += this.parent.pageNumber;
footer += ' ';
output += footer;
// Fully rendered raw frame complete, add to the array
rawFrames.push(output);
}
this._rawFrames = rawFrames;
return this._rawFrames;
}
set rawFrames(frames:string[]) {
this._rawFrames = frames;
}
}
export {
Page,
StaticPage,
GeneratedPage,
PageTemplate,
FrameFormat
}