-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpage.go
More file actions
688 lines (629 loc) · 14.6 KB
/
Copy pathpage.go
File metadata and controls
688 lines (629 loc) · 14.6 KB
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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
package teletext
import (
"bytes"
"errors"
"fmt"
"image"
"image/color"
"image/draw"
"image/gif"
"time"
"github.com/textmodes/parser"
"github.com/textmodes/parser/chargen"
"github.com/textmodes/parser/data"
)
const DefaultHeader = "XXXXXXXXGOLANG mpp DAY dd MTH \x03 hh:nn.ss"
type Page struct {
// CycleTime in seconds.
CycleTime int // (CT) Seconds
// CycleTimeType byte.
CycleTimeType uint8 // (CT)
// FastExtLinks to different pages.
FastExtLinks [6]int // (FL)
// Number of the page.
Number int // (PN)
// Language of the page, defaults to English.
Language Language
// Palette is used for the Image function and defaults to the TeleText Level 1
// default palette.
Palette color.Palette
sub *Page
destination string // (DS)
sourcePage string // (SP)
description string // (DE)
subCode uint // (SC)
status int // (PS)
region int // (RE)
coding Coding
function Function
lastPacket uint8
data [25][40]byte
attr [25][40]attr
}
const defaultPage = 0x1ff00
func NewPage() *Page {
page := &Page{Number: defaultPage}
page.Clear()
return page
}
// Clear page.
func (page *Page) Clear() {
for y := 0; y < len(page.data); y++ {
for x := 0; x < len(page.data[y]); x++ {
page.data[y][x] = 0x20
page.attr[y][x].fg = 7
page.attr[y][x].bg = 0
page.attr[y][x].doubleWidth = false
page.attr[y][x].doubleHeight = false
}
}
copy(page.data[0][:], []byte(DefaultHeader))
}
// Line returns the row bytes.
func (page Page) Line(row int) [40]byte {
if row == 0 {
return page.Header()
}
return page.data[row]
}
// LineAt returns the row bytes.
func (page Page) LineAt(row int, now time.Time) [40]byte {
if row == 0 {
return page.HeaderAt(now)
}
return page.data[row]
}
// SetLine sets the row bytes.
func (page *Page) SetLine(row int, line [40]byte) {
if row < 0 || row >= len(page.data) {
return
}
copy(page.data[row][:], line[:])
}
// SetLineBytes sets the row from escaped bytes.
func (page *Page) SetLineBytes(row int, line []byte) {
if row < 0 || row >= len(page.data) {
return
}
var i, col int
for i = 0; i < len(line) && col < 40; i++ {
switch c := line[i] & 0x7f; c {
case 0x1b: // Escaped
i++
if i >= len(line) {
break
}
page.data[row][col] = line[i] & 0x3f
col++
case 0x00: // NULL
page.data[row][col] = 0x80 // Black text
col++
default:
page.data[row][col] = c
col++
}
}
for ; col < 40; col++ {
page.data[row][col] = 0x20
}
return
}
// Header bytes.
func (page Page) Header() [40]byte {
return page.HeaderAt(time.Now())
}
// HeaderAt are the header bytes at time now.
func (page Page) HeaderAt(now time.Time) (line [40]byte) {
raw := make([]byte, 40)
// Page number
var k = page.Number / 0x100
if k < 0x100 || k > 0x8ff {
k = 0x100
}
copy(raw[:], page.data[0][:])
replace := func(b, sub []byte, formats ...string) []byte {
for _, format := range formats {
if i := bytes.Index(b, []byte(format)); i != -1 {
return append(append(b[:i], sub...), b[i+len(sub):]...)
}
}
return b
}
// Magazine and page number
raw = replace(raw, []byte(fmt.Sprintf("P%03x ", k)), "XXXXXXXX")
raw = replace(raw, []byte(fmt.Sprintf("%03x", k)), "mpp", "%%#", "%%£")
// Day number and name
raw = replace(raw, []byte(now.Format("02")), "dd", "%d")
raw = replace(raw, []byte(now.Format("Mon")), "DAY", "%%a")
// Month name and seconds
raw = replace(raw, []byte(now.Format("Jan")), "MTH", "%%b")
raw = replace(raw, []byte(now.Format("01")), "uu", "%m")
// Year, hours, minutes and seconds
raw = replace(raw, []byte(now.Format("06")), "yy", "%Y")
raw = replace(raw, []byte(now.Format("15")), "hh", "%H")
raw = replace(raw, []byte(now.Format("04")), "nn", "%M")
raw = replace(raw, []byte(now.Format("05")), "ss", "%S")
copy(line[:], raw)
return
}
var palette = color.Palette{
color.RGBA{0x00, 0x00, 0x00, 0xff}, // 0b000 Black
color.RGBA{0xff, 0x00, 0x00, 0xff}, // 0b100 Red
color.RGBA{0x00, 0xff, 0x00, 0xff}, // 0b010 Green
color.RGBA{0xff, 0xff, 0x00, 0xff}, // 0b110 Yellow
color.RGBA{0x00, 0x00, 0xff, 0xff}, // 0b001 Blue
color.RGBA{0xff, 0x00, 0xff, 0xff}, // 0b101 Magenta
color.RGBA{0x00, 0xff, 0xff, 0xff}, // 0b011 Cyan
color.RGBA{0xff, 0xff, 0xff, 0xff}, // 0b111 White
}
func (page Page) Image() (image.Image, error) {
return page.ImageAt(time.Now())
}
func (page Page) ImageAt(now time.Time) (image.Image, error) {
return page.render(true, now)
}
type charType uint8
const (
alphanumeric charType = iota
contiguousGraphics
separatedGraphics
)
func (page Page) render(flashing bool, now time.Time) (*image.Paletted, error) {
var (
im = image.NewPaletted(image.Rect(0, 0, 40*12, 25*20), palette)
rom, err = data.Bytes(fmt.Sprintf("font/chargen/saa505%d.bin", page.Language))
doubleHeightBottom bool
nextCharType charType
)
if err != nil {
return nil, err
}
draw.Draw(im, im.Bounds(), image.NewUniform(palette[0]), image.ZP, draw.Src)
var (
opts = chargen.MaskOptions{Size: image.Pt(8, 10)}
mask = chargen.RoundCharacters(chargen.NewBytesMask(rom, opts))
//mask = chargen.NewBytesMask(rom, opts)
font = chargen.New(mask)
)
for row := 0; row < 25; row++ {
var (
separated bool
graphics bool
graphicsHold bool
doubleHeight bool
doubleHeightNext bool
doubleWidth bool
doubleWidthNext bool
doubleWidthRight bool
heldCharType charType
flash bool
fg = palette[7]
bg = palette[0]
graphicsLast uint8
)
nextCharType = alphanumeric
heldCharType = alphanumeric
debugf("line %d: %q", row, page.LineAt(row, now))
for col, code := range page.LineAt(row, now) {
var (
ctrl = code & 0x7f // 7-bit
graphicsHoldOff bool
graphicsHoldClear bool
currCharType = nextCharType
)
tracef("(%d, %d) code %#02x (%s, %#02x)", col, row, code, codeName(code), ctrl)
if ctrl < 0x20 {
switch ctrl {
case
controlAlphaBlack,
controlAlphaRed,
controlAlphaGreen,
controlAlphaYellow,
controlAlphaBlue,
controlAlphaMagenta,
controlAlphaCyan,
controlAlphaWhite:
fg = &color.RGBA{
0xff * ((ctrl & 1) >> 0),
0xff * ((ctrl & 2) >> 1),
0xff * ((ctrl & 4) >> 2),
0xff,
}
graphics = false
graphicsHoldClear = true
tracef("(%d, %d) alpha, fg %+v", col, row, fg)
nextCharType = alphanumeric
case controlFlash:
flash = true
tracef("(%d, %d) flash on", col, row)
case controlSteady:
flash = false
tracef("(%d, %d) flash off", col, row)
case controlEndBox, controlStartBox:
case controlNormalHeight:
doubleHeight = false
case controlDoubleHeight:
doubleHeight = true
if !doubleHeightBottom {
doubleHeightNext = true
}
case controlDoubleWidth:
doubleWidth = true
if !doubleWidthRight {
doubleWidthNext = true
}
case controlDoubleSize:
if col < 39 {
doubleHeight = true
if !doubleHeightBottom {
doubleHeightNext = true
}
}
if row < 23 {
doubleWidth = true
if !doubleWidthRight {
doubleWidthNext = true
}
}
case
controlMosaicBlack,
controlMosaicRed,
controlMosaicGreen,
controlMosaicYellow,
controlMosaicBlue,
controlMosaicMagenta,
controlMosaicCyan,
controlMosaicWhite:
fg = &color.RGBA{
0xff * ((ctrl & 1) >> 0),
0xff * ((ctrl & 2) >> 1),
0xff * ((ctrl & 4) >> 2),
0xff,
}
graphics = true
if separated {
nextCharType = separatedGraphics
} else {
nextCharType = contiguousGraphics
}
tracef("(%d, %d) graphics, fg %+v", col, row, fg)
case controlConcealDisplay:
fg = bg
tracef("(%d, %d) conceal, fg %+v", col, row, fg)
case controlContiguousMosaic:
separated = false
nextCharType = contiguousGraphics
case controlSeparatedMosaic:
separated = true
nextCharType = separatedGraphics
case controlESC:
case controlBlackBackground:
bg = palette[0]
tracef("(%d, %d) black background, bg %+v", col, row, bg)
case controlNewBackground:
bg = fg
tracef("(%d, %d) new background, bg %+v", col, row, bg)
case controlHoldMosaic:
graphicsHold = true
case controlReleaseMosaic:
graphicsHoldOff = true
}
if graphics && graphicsHold {
tracef("graphics held: %q -> %q", code, graphicsLast)
code = graphicsLast
if code >= 0x40 && code < 0x60 {
code = 0x20
}
currCharType = heldCharType
} else {
code = 0x20
}
} else /* code < ' ' */ if graphics {
graphicsLast = code
heldCharType = currCharType
} else if code == 0x20 {
graphics = false
}
var skip bool
if graphicsHoldOff {
graphicsHold = false
graphicsLast = ' '
}
if graphicsHoldClear {
graphicsLast = ' '
}
// Only display char if we're *not* flashing
if flash && !flashing {
tracef("flash but not flashing: %q -> %q", code, graphicsLast)
code = ' '
}
if doubleHeight {
} else if doubleHeightBottom {
skip = true
}
if doubleWidth {
} else if doubleWidthRight {
skip = true
}
if !skip {
if graphics {
tracef("(%d, %d) graphics %#02x", col, row, code)
page.renderMosaic(im, font, col, row, fg, bg, code, separated, doubleHeight, doubleWidth)
} else {
tracef("(%d, %d) alpha %q (%d) color %v on %v", col, row, code&0x7f, code, fg, bg)
page.renderChar(im, font, col, row, fg, bg, code, doubleHeight, doubleWidth)
}
} else {
tracef("(%d, %d) skip", col, row)
}
doubleWidthRight = doubleWidthNext
}
doubleHeightBottom = doubleHeightNext
}
return im, nil
}
func (page Page) renderChar(im *image.Paletted, font *chargen.Font, col, row int, fg, bg color.Color, code byte, doubleHeight, doubleWidth bool) {
var (
char = code & 0x7f // 7-bit
w = 12
h = 20
)
if doubleHeight {
h <<= 1
}
if doubleWidth {
w <<= 1
}
var (
x = col * w
y = row * h
)
draw.Draw(im, image.Rect(x, y, x+w, y+h), image.NewUniform(bg), image.ZP, draw.Src)
if char >= 0x20 {
mask, sp := font.CharMask(uint16(char - 0x20))
sp = sp.Add(image.Pt(4, 0)) // First two columns are empty in the font ROM
draw.DrawMask(im, im.Bounds().Add(image.Pt(x, y)), image.NewUniform(fg), image.ZP, mask, sp, draw.Over)
} else {
debugf("can't render char %q (%#02x)", char, char)
}
}
func (page Page) renderMosaic(im *image.Paletted, font *chargen.Font, col, row int, fg, bg color.Color, code byte, separated, doubleHeight, doubleWidth bool) {
var (
char = code - 0x20
w = 12
h = 20
)
if doubleHeight {
h <<= 1
}
if doubleWidth {
w <<= 1
}
draw.Draw(im, image.Rect(w*col, h*row, w*(col+1), h*(row+1)), image.NewUniform(bg), image.ZP, draw.Src)
if code < 0x20 {
return
}
var (
b1 = (char & 0x01) == 0x01
b2 = (char & 0x02) == 0x02
b3 = (char & 0x04) == 0x04
b4 = (char & 0x08) == 0x08
b5 = (char & 0x10) == 0x10
b6 = (char & 0x40) == 0x40
)
for y := 0; y < h; y++ {
var (
r = y
oy = y + h*row
)
if doubleHeight {
r /= 2
}
for x := 0; x < w; x++ {
var (
c = x
ox = x + w*col
)
if doubleWidth {
c /= 2
}
if (c < 6 && r < 6 && b1) ||
(c > 5 && r < 6 && b2) ||
(c < 6 && r > 5 && r < 14 && b3) ||
(c > 5 && r > 5 && r < 14 && b4) ||
(c < 6 && r > 13 && b5) ||
(c > 5 && r > 13 && b6) {
im.Set(ox, oy, fg)
} else {
im.Set(ox, oy, bg)
}
/*
if (x+y)%4 == 3 {
im.Set(ox, oy, &color.RGBA{0xff, 0x80, 0x00, 0xff})
}
*/
}
}
}
func (page Page) renderMosaicChar(im *image.RGBA, font *chargen.Font, col, row int, fg, bg color.Color, code byte, doubleHeight, doubleWidth, separated bool) {
var (
char = code // 5-bit
w = 12
h = 20
)
if doubleHeight {
h <<= 1
}
if doubleWidth {
w <<= 1
}
var (
x = col * w
y = row * h
)
draw.Draw(im, image.Rect(x, y, x+w, y+h), image.NewUniform(bg), image.ZP, draw.Src)
if char >= 0x20 {
tracef("mosaic (%d, %d) %d: %08b -> %08b", col, row, code, code, code-0x20)
mask, sp := font.CharMask(uint16(char-0x20) & 0x3f)
sp = sp.Add(image.Pt(4, 0)) // First two columns are empty in the font ROM
draw.DrawMask(im, im.Bounds().Add(image.Pt(x, y)), image.NewUniform(fg), image.ZP, mask, sp, draw.Over)
}
}
func (page Page) generateMosaic(data uint8, row int, separated, doubleHeight, doubleWidth bool) (code uint16) {
mask := uint(1)
if doubleHeight {
mask++
}
switch row >> mask {
case 0, 1:
if data&0x01 == 0x01 {
code += 0xfc0
}
if data&0x02 == 0x02 {
code += 0x03f
}
if separated {
code &= 0x3cf
}
case 2:
if separated {
break
}
if data&0x01 == 0x01 {
code += 0xfc0
}
if data&0x02 == 0x02 {
code += 0x03f
}
case 3, 4, 5:
if data&0x04 == 0x04 {
code += 0xfc0
}
if data&0x08 == 0x08 {
code += 0x03f
}
if separated {
code &= 0x3cf
}
case 6:
if separated {
break
}
if data&0x04 == 0x04 {
code += 0xfc0
}
if data&0x08 == 0x08 {
code += 0x03f
}
case 7, 8:
if data&0x10 == 0x10 {
code += 0xfc0
}
if data&0x40 == 0x40 {
code += 0x03f
}
if separated {
code &= 0x3cf
}
case 9:
if separated {
break
}
if data&0x10 == 0x10 {
code += 0xfc0
}
if data&0x40 == 0x40 {
code += 0x03f
}
}
return
}
// Pages are multiple pages.
type Pages []*Page
func (pages Pages) Image() (image.Image, error) {
if len(pages) == 0 {
return nil, errors.New("teletext: no pages")
}
return pages[0].Image()
}
func (pages Pages) AnimateDelay(delay time.Duration) (*gif.GIF, error) {
var (
g = new(gif.GIF)
d = int(delay / (time.Millisecond * 100))
)
for _, page := range pages {
i, err := page.Image()
if err != nil {
return nil, err
}
g.Image = append(g.Image, i.(*image.Paletted))
g.Delay = append(g.Delay, d)
}
return g, nil
}
// Language code.
type Language int
func (lang Language) String() string {
switch lang {
case English:
return "en"
case French:
return "fr"
case Swedish:
return "se"
case Czech:
return "cz/si"
case German:
return "de"
case Spanish:
return "es/pt"
case Italian:
return "it"
default:
return "unknown"
}
}
// Languages.
const (
English Language = iota
French
Swedish
Czech
German
Spanish
Italian
// Aliases
Slovac = Czech
Portuguese = Spanish
)
// Coding is the page coding.
type Coding uint8
// Codings
const (
Coding7BitText Coding = iota
Coding8BitData
Coding13Triplets
CodingHamming8_4
)
// Function is the page function.
type Function uint8
// Functions
const (
LOP Function = iota
DATABROADCAST
GPOP
POP
GDRCS
DRCS
MOT
MIP
BTT
AIT
MPT
MPTEX
)
// Interface checks
var (
_ parser.Image = (*Page)(nil)
_ parser.Image = (*Pages)(nil)
)