-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpcface.js
155 lines (148 loc) · 6.4 KB
/
pcface.js
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
(function () {
'use strict'
/**
* Exported module of PC Face.
*
* @exports pcface
*/
const pcface = {}
/**
* Draw a single character on a canvas using the given canvas
* context. The (x, y) coordinates represent the top-left corner of
* the glyph. The x-coordinate represents the column index the
* leftmost edge of the glyph. Similarly, the y-coordinate
* represents the row index of the topmost edge of the glyph. The
* character `c` may either be an integer ASCII code of a CP437
* character or a string containing a single Unicode character that
* represents a CP437 character.
*
* If `c` is an ASCII code, then `bitmaps` must be an array of
* bitmaps. If `s` is a string containing a single Unicode
* character, then `bitmaps` must be an object that maps each
* Unicode character that represents a CP437 character to its
* bitmap array.
*
* @param {Array|Object} bitmaps - An array/object containing bitmap arrays.
* @param {CanvasRenderingContext2D} ctx - Canvas context.
* @param {number|string} c - Character to draw on canvas.
* @param {number} x - X coordinate of the leftmost edge of the glyph.
* @param {number} y - Y coordinate of the topmost edge of the glyph.
* @param {number} scale - Scale multiplier.
*/
pcface.drawChar = function (bitmaps, ctx, w, c, x, y, scale) {
const bitmap = bitmaps[c]
for (let i = 0; i < bitmap.length; i++) {
for (let j = 0; j < w; j++) {
const mask = 1 << (w - 1 - j)
if ((bitmap[i] & mask) !== 0) {
ctx.fillRect(x + scale * j, y + scale * i, scale, scale)
}
}
}
}
/**
* Draw a single string with its top-left corner placed at
* coordinate (x, y). The string `s` may either be an array of
* integers where each integer is an ASCII code of a CP437 character
* of it may be a string of Unicode characters where each Unicode
* character represents a CP437 character.
*
* If `s` is an array of ASCII codes, then `bitmaps` must be an
* array of bitmaps. If `s` is a string of Unicode characters, then
* `bitmaps` must be an object that maps each Unicode character that
* represents a CP437 character to its bitmap array.
*
* @param {Array|Object} bitmaps - An array/object containing bitmap arrays.
* @param {CanvasRenderingContext2D} ctx - Canvas context.
* @param {Array|string} s - Characers to draw on canvas.
* @param {number} w - Horizontal width to allocate for each glyph.
* @param {number} x - X coordinate of the leftmost edge of the glyphs.
* @param {number} y - Y coordinate of the topmost edge of the glyphs.
* @param {number} scale - Scale multiplier (should be a positive integer).
*/
pcface.drawString = function (bitmaps, ctx, w, s, x, y, scale) {
for (let i = 0; i < s.length; i++) {
pcface.drawChar(bitmaps, ctx, w, s[i], x + i * scale * w, y, scale)
}
}
/**
* Draw multiple strings with the top-left corner of the first
* string placed at coordinate (x, y). Each subsequent string is
* placed below the previous one. The height `h` specifies the
* distance between the top edge of one string and the top edge of
* the next one. The width `w` specifies the distance between the
* left edge of one glyph and the left edge of the next one.
*
* The string `s` may either be an array of array of integers where
* each integer is an ASCII code of a CP437 character of it may be
* an array of strings of Unicode characters where each Unicode
* character represents a CP437 character.
*
* If `s` is an array of array of ASCII codes, then `bitmaps` must
* be an array of bitmaps. If `s` is an array of strings of Unicode
* characters, then `bitmaps` must be an object that maps each
* Unicode character that represents a CP437 character to its
* bitmap.
*
* @param {CanvasRenderingContext2D} ctx - Canvas context.
* @param {Array|Object} bitmaps - An array or an object of bitmap arrays.
* @param {number} w - Horizontal width to allocate for each glyph.
* @param {number} h - Horizontal height to allocate for each glyph.
* @param {Array} ss - An array of array of integers or an array of strings.
* @param {number} x - X coordinate of the leftmost edge of the glyphs.
* @param {number} y - Y coordinate of the topmost edge of the glyphs.
* @param {number} scale - Scale multiplier (should be a positive integer).
*/
pcface.drawStrings = function (bitmaps, ctx, w, h, ss, x, y, scale) {
for (let i = 0; i < ss.length; i++) {
pcface.drawString(bitmaps, ctx, w, ss[i], x, y + i * scale * h, scale)
}
}
/**
* Create an array of consecutive integers with `start` as the first
* integer and `stop - 1` as the last integer in the array. This
* function may be useful for creating a range of CP437 character
* codes for demos. For example, calling `pcface.range(65, 70)`
* returns `[65, 66, 67, 68, 69]` which can then be passed to
* {@linkcode module:pcface.drawString} to draw the glyphs
* represented by these integers.
*
* @param {number} start - Start integer.
* @param {number} stop - Stop integer.
*/
pcface.range = function (start, stop) {
const result = []
for (let i = start; i < stop; i++) {
result.push(i)
}
return result
}
/**
* Return an array of arrays of integers. This function may be
* useful for creating ranges of CP437 character codes for demos.
* All nested arrays together contain consecutive integers from
* `start` to `stop - 1`. These integers are split into multiple
* nested arrays such that each nested array contains no more than
* `chunk` number of integers. For example, calling
* `pcface.range(65, 78, 5)` returns `[[65, 66, 67, 68, 69], [70,
* 71, 72, 73, 74], [75, 76, 77]]`. This array of arrays can then be
* passed to {@linkCode module:pcface.drawStrings} to draw the
* glyphs represented by these integers.
*
* @param {number} start - Start integer.
* @param {number} stop - Stop integer.
* @param {number} chunk - Maximum length of each nested array.
*/
pcface.ranges = function (start, stop, chunk) {
const result = []
for (let chunkStart = start; chunkStart < stop; chunkStart += chunk) {
result.push(pcface.range(chunkStart, Math.min(stop, chunkStart + chunk)))
}
return result
}
if (typeof window !== 'undefined') {
window.pcface = pcface
} else {
module.exports = pcface
}
})()