-
-
Notifications
You must be signed in to change notification settings - Fork 64
Expand file tree
/
Copy pathfontlibc.asm
More file actions
2086 lines (1928 loc) · 45.1 KB
/
fontlibc.asm
File metadata and controls
2086 lines (1928 loc) · 45.1 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
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
;-------------------------------------------------------------------------------
include '../include/library.inc'
include '../include/include_library.inc'
;-------------------------------------------------------------------------------
library FONTLIBC,2
;-------------------------------------------------------------------------------
; Dependencies
;-------------------------------------------------------------------------------
include_library '../graphx/graphx.asm'
;-------------------------------------------------------------------------------
; v1 functions
;-------------------------------------------------------------------------------
export fontlib_SetWindow
export fontlib_SetWindowFullScreen
export fontlib_GetWindowXMin
export fontlib_GetWindowYMin
export fontlib_GetWindowWidth
export fontlib_GetWindowHeight
export fontlib_SetCursorPosition
export fontlib_GetCursorX
export fontlib_GetCursorY
export fontlib_ShiftCursorPosition
export fontlib_SetFont
export fontlib_SetForegroundColor
export fontlib_SetBackgroundColor
export fontlib_SetColors
export fontlib_GetForegroundColor
export fontlib_GetBackgroundColor
export fontlib_SetTransparency
export fontlib_GetTransparency
export fontlib_SetLineSpacing
export fontlib_GetSpaceAbove
export fontlib_GetSpaceBelow
export fontlib_SetItalicSpacingAdjustment
export fontlib_GetItalicSpacingAdjustment
export fontlib_GetCurrentFontHeight
export fontlib_ValidateCodePoint
export fontlib_GetTotalGlyphs
export fontlib_GetFirstGlyph
export fontlib_SetNewlineCode
export fontlib_GetNewlineCode
export fontlib_SetAlternateStopCode
export fontlib_GetAlternateStopCode
export fontlib_SetFirstPrintableCodePoint
export fontlib_GetFirstPrintableCodePoint
export fontlib_SetDrawIntCodePoints
export fontlib_GetDrawIntMinus
export fontlib_GetDrawIntZero
export fontlib_GetGlyphWidth
export fontlib_GetStringWidth
export fontlib_GetStringWidthL
export fontlib_GetLastCharacterRead
export fontlib_DrawGlyph
export fontlib_DrawString
export fontlib_DrawStringL
export fontlib_DrawInt
export fontlib_DrawUInt
export fontlib_ClearEOL
export fontlib_ClearWindow
export fontlib_Newline
export fontlib_SetNewlineOptions
export fontlib_GetNewlineOptions
;-------------------------------------------------------------------------------
; v1 font pack functions
;-------------------------------------------------------------------------------
export fontlib_GetFontPackName
export fontlib_GetFontByIndex
export fontlib_GetFontByIndexRaw
export fontlib_GetFontByStyle
export fontlib_GetFontByStyleRaw
;-------------------------------------------------------------------------------
; v2 functions
;-------------------------------------------------------------------------------
export fontlib_ScrollWindowDown
export fontlib_ScrollWindowUp
export fontlib_Home
export fontlib_HomeUp
;-------------------------------------------------------------------------------
CurrentBuffer := ti.mpLcdLpbase
TEXT_FG_COLOR := 0
TEXT_BG_COLOR := 255
local2 := -9
local1 := -6
local0 := -3
arg00 := 0
arg0 := 3
arg1 := 6
arg2 := 9
arg3 := 12
arg4 := 15
arg5 := 18
arg6 := 21
chFirstPrintingCode := $10
chNewline := $0A
bEnableAutoWrap := 0
mEnableAutoWrap := 1 shl bEnableAutoWrap
bAutoClearToEOL := 1
mAutoClearToEOL := 1 shl bAutoClearToEOL
bPreclearNewline := 2
mPreclearNewline := 1 shl bPreclearNewline
bWasNewline := 7
mWasNewline := 1 shl bWasNewline
bAutoScroll := 3
mAutoScroll := 1 shl bAutoScroll
;-------------------------------------------------------------------------------
; Declare the structure of a raw font
struc strucFont
label .: 15
.version rb 1
.height rb 1
.totalGlyphs rb 1
.firstGlyph rb 1
.widthsTablePtr rl 1
.bitmapsTablePtr rl 1
.italicSpaceAdjust rb 1
.spaceAbove rb 1
.spaceBelow rb 1
.weight rb 1
.style rb 1
end struc
virtual at 0
strucFont strucFont
end virtual
strucFont.fontPropertiesSize := strucFont.italicSpaceAdjust
struc strucFontPackHeader
label .: 13
.identifier rb 8
.metadatOffset rl 1
.fontCount rb 1
.fontList rl 1
end struc
virtual at 0
strucFontPackHeader strucFontPackHeader
end virtual
;-------------------------------------------------------------------------------
macro mIsHLLessThanDE?
or a,a
sbc hl,de
add hl,hl
jp po,$+5
ccf
end macro
macro mIsHLLessThanBC?
or a,a
sbc hl,bc
add hl,hl
jp po,$+5
ccf
end macro
macro s8 op,imm
local i
i = imm
assert i >= -128 & i < 128
op,i
end macro
;-------------------------------------------------------------------------------
macro setSmcBytes name*
local addr,data
postpone
virtual at addr
irpv each,name
if % = 1
db %%
end if
assert each >= addr + 1 + 2*%%
dw each - $ - 2
end irpv
load data: $-$$ from $$
end virtual
end postpone
call _SetSmcBytes
addr db data
end macro
macro setSmcBytesFast name*
local temp,list
postpone
temp equ each
irpv each,name
temp equ temp,each
end irpv
list equ temp
end postpone
pop de ; de = return vetor
ex (sp),hl ; l = byte
ld a,l ; a = byte
match expand,list
iterate expand
if % = 1
ld hl,each
ld c,(hl)
ld (hl),a
else
ld (each),a
end if
end iterate
end match
ld a,c ; a = old byte
ex de,hl ; hl = return vector
jp (hl)
end macro
macro smcByte name*,addr: $-1
local link
link := addr
name equ link
end macro
;-------------------------------------------------------------------------------
;-------------------------------------------------------------------------------
fontlib_SetWindowFullScreen:
; Sets the bounds of the box all text will appear in to be the full screen
; Arguments:
; None
; Returns:
; Nothing
ld hl,_TextDefaultWindow
ld de,_TextXMin
jp ti.Mov8b
;-------------------------------------------------------------------------------
fontlib_SetWindow:
; Sets the bounds of the box all text will appear in
; Arguments:
; arg0: X min
; arg1: Y min
; arg2: width
; arg3: height
; Returns:
; Nothing
pop de ; de = return vector
pop hl ; hl = xmin
ld (_TextXMin),hl
pop bc ; c = ymin
ld a,c ; a = ymin
ld (_TextYMin),a
pop bc ; bc = width
add hl,bc ; hl = xmin + width = xmax
ld (_TextXMax),hl
ex (sp),hl ; l = height
add a,l ; a = ymin + height = ymax
ld (_TextYMax),a
push hl
push hl
push hl ; sp -> arg0
ex de,hl ; hl = return vector
jp (hl)
;-------------------------------------------------------------------------------
fontlib_GetWindowXMin:
; Returns the starting column of the current text window
; Arguments:
; None
; Returns:
; Data
ld hl,(_TextXMin)
ret
;-------------------------------------------------------------------------------
fontlib_GetWindowYMin:
; Returns the starting row of the current text window
; Arguments:
; None
; Returns:
; Data
ld a,(_TextYMin)
ret
;-------------------------------------------------------------------------------
fontlib_GetWindowWidth:
; Returns the width of the current text window
; Arguments:
; None
; Returns:
; Data
; Should preserve DE, as window scrolling needs DE preserved
ld hl,(_TextXMax)
ld bc,(_TextXMin)
or a,a
sbc hl,bc
ret
;-------------------------------------------------------------------------------
fontlib_GetWindowHeight:
; Returns the height of the current text window
; Arguments:
; None
; Returns:
; Data
ld a,(_TextYMax)
ld hl,_TextYMin
sub a,(hl)
ret
;-------------------------------------------------------------------------------
fontlib_SetCursorPosition:
; Sets the cursor position for text drawing
; Arguments:
; arg0: X
; arg1: Y
; Returns:
; Nothing
ld hl,arg0
add hl,sp
ld de,_TextX
ld bc,4
ldir
ret
;-------------------------------------------------------------------------------
fontlib_GetCursorX:
; Gets the cursor column
; Arguments:
; None
; Returns:
; Column
ld hl,(_TextX)
ret
;-------------------------------------------------------------------------------
fontlib_GetCursorY:
; Gets the cursor row
; Arguments:
; None
; Returns:
; Row
ld a,(_TextY)
ret
;-------------------------------------------------------------------------------
fontlib_ShiftCursorPosition:
; Shifts the cursor position by a given signed delta.
; By limiting x to 0xFFFF and y to 0xFF, the cursor can't get far enough out of
; VRAM to cause corruption of any other regions of memory.
; Arguments:
; arg0: delta X
; arg1: delta Y
; Returns:
; Nothing
pop de ; de = return vector
; Shift x
pop bc ; bc = dx
ld hl,(_TextX) ; hl = x
add.s hl,bc ; hl = (x + dx) & 0xFFFF
ld (_TextX),hl ; x = x + dx
; Shift y
pop bc ; bc = dy
ld hl,_TextY
ld a,(hl) ; a = y
add a,c ; a = (y + dy) & 0xFF
ld (hl),a ; y = (y + dy) & 0xFF
; Finish
push hl
push hl ; sp -> arg0
ex de,hl ; hl = return vector
jp (hl)
;-------------------------------------------------------------------------------
fontlib_HomeUp:
; Moves the cursor to the upper left corner of the text window
; Arguments:
; None
; Returns:
; Nothing
ld a,(_TextYMin)
ld (_TextY),a
; Fall through to fontlib_Home
assert $ = fontlib_Home
;-------------------------------------------------------------------------------
; Moves the cursor back to the start of the current line
fontlib_Home:
; Arguments:
; None
; Returns:
; Nothing
ld hl,(_TextXMin)
ld (_TextX),hl
ret
;-------------------------------------------------------------------------------
fontlib_SetFont:
; Sets the current font to the data at the pointer given
; Arguments:
; arg0: Pointer to font
; arg1: Load flags
; Returns:
; bool:
; - true if font loaded successfully
; - false on failure (invalid font, or you tried to use the version byte)
ld hl,arg0
add hl,sp
ld hl,(hl)
; Verify version byte is zero like it's supposed to be
; The literal only reason there's any validation here at all is to
; enforce keeping the version byte reserved.
xor a,a
cp a,(hl)
ret nz
; Load font data
ld (_CurrentFontRoot),hl
push hl
ld de,_CurrentFontProperties
ld bc,strucFont.fontPropertiesSize
ldir
pop bc
ld iy,_CurrentFontProperties
; Verify height at least looks semi-reasonable
ld a,(iy + strucFont.height)
or a,a
ret z ; Also unreasonable: a zero-height font
and a,$80
jr nz,.false
ld a,63
cp a,(iy + strucFont.spaceAbove)
jr c,.false
cp a,(iy + strucFont.spaceBelow)
jr c,.false
.validateOffsets:
; Now convert offsets into actual pointers
; Validate that offset is at least semi-reasonable
xor a,a
ld de,$ff00 ; Maximum reasonable font data size
ld hl,(iy + strucFont.widthsTablePtr)
sbc hl,de ; Doesn't really matter if we're off-by-one here
ret nc
add hl,de
add hl,bc
ld (iy + strucFont.widthsTablePtr),hl
ld hl,(iy + strucFont.bitmapsTablePtr)
sbc hl,de ; C reset from ADD HL,BC above
ret nc ; (we're in Crazytown if there was carry)
add hl,de
add hl,bc
ld (iy + strucFont.bitmapsTablePtr),hl
; Check for the ignore ling spacing flag
ld hl,arg0
add hl,sp
ld a,(hl)
or a,a
jr z,.true
lea hl,iy + strucFont.spaceAbove
xor a
ld (hl),a
inc hl
ld (hl),a
.true: ld a,1
ret
.false:
xor a,a
ret
;-------------------------------------------------------------------------------
fontlib_DrawGlyph:
; Draws a glyph to the current cursor position
; Arguments:
; arg0: codepoint
; Returns:
; New X cursor value
ld hl,arg0
add hl,sp
ld a,(hl)
push ix ; _DrawGlyphRaw destroys IX
; Compute write pointer
ld hl,(_TextY)
ld h,ti.lcdWidth / 2
mlt hl
add hl,hl
ld de,(_TextX)
push de
add hl,de
ld de,(CurrentBuffer)
add hl,de
call util.DrawGlyphRaw ; Draw glyph
; Update _TextX
lea.sis de,iy + 0
pop hl
add hl,de
ld a,(_CurrentFontProperties.italicSpaceAdjust)
pop ix
ld e,a
sbc hl,de
ld (_TextX),hl
ret
;-------------------------------------------------------------------------------
util.DrawGlyphRaw:
; Handles the actual main work of drawing a glyph.
; Arguments:
; HL: Draw pointer
; A: Glyph index
; Font properties variables
; Returns:
; IYL: Width of glyph (not including any italicSpaceAdjust)
; IYH: Zero
; IYU: Untouched
; Glyph drawn
; Destroys:
; Basically everything except shadow and configuration registers.
; And don't count on that not changing.
push hl
; Subtract out firstGlyph
ld hl,_CurrentFontProperties.firstGlyph
sub a,(hl)
; Get glyph width
ld c,a
or a,a
sbc hl,hl
ld l,a
ld de,(_CurrentFontProperties.widthsTablePtr)
add hl,de
ld a,(hl)
pop de
call gfx_Wait
; jp util.DrawGlyphRawKnownWidth
assert $ = util.DrawGlyphRawKnownWidth
;-------------------------------------------------------------------------------
util.DrawGlyphRawKnownWidth:
; Handles the actual main work of drawing a glyph.
; Arguments:
; DE: Draw pointer
; C: Glyph index, with _CurrentFontProperties.firstGlyph subtracted out
; A: Glyph width
; Font properties variables
; Returns:
; IYL: Width of glyph (not including any italicSpaceAdjust)
; IYH: Zero
; IYU: Untouched
; Glyph drawn
; Destroys:
; Basically everything except shadow and configuration registers.
; And don't count on that not changing.
; Update loop controls
ld iyl,a
dec a
rra
srl a
srl a
inc a
ld (_TextStraightBytesPerRow),a
ld a,320 and 255
sub a,iyl
ld (_TextStraightRowDelta - 2),a
; Get pointer to bitmap
ld hl,(_CurrentFontProperties.bitmapsTablePtr)
ld b,2
mlt bc ; Performs both the multiply and zeros BCU
add hl,bc
ld ix,(hl)
lea.sis ix,ix + 0 ; Truncate to 16-bits
ld bc,(_CurrentFontRoot)
add ix,bc
; Write SMC
ld a,(_TextTransparentMode)
ld b,a
ld a,.unsetColumnLoopStart - (.unsetColumnLoopJr1 + 2)
ld c,.unsetColumnLoopStart - (.unsetColumnLoopJr2 + 2)
djnz .writeSmc
ld a,.unsetColumnLoopMiddleTransparent - (.unsetColumnLoopJr1 + 2)
ld c,.unsetColumnLoopMiddleTransparent - (.unsetColumnLoopJr2 + 2)
.writeSmc:
ld (.unsetColumnLoopJr1 + 1),a
ld a,c
ld (.unsetColumnLoopJr2 + 1),a
push bc
; Now deal with the spaceAbove metric
ld a,(_CurrentFontProperties.spaceAbove)
or a,a
call nz,util.DrawEmptyLines
ld c,TEXT_FG_COLOR ; SMCd to have correct foreground color
smcByte _TextStraightForegroundColor
ld a,(_CurrentFontProperties.height)
ld iyh,a
ld a,c
; Registers:
; B: Bit counter for each row
; C: Foreground color
; IYL: Glyph data width
; IYH: Row counter
; IX: Read pointer
; DE: Write pointer
; HL: Current line bitmap
; This is split into three loops: one for set pixels, one for unset pixels that
; are transparent, and one for unset pixels that are opaque.
; The idea is that pixels are not randomly black or white; rather, there tend
; to be horizontal lines in text, giving straight runs of pixels the same color.
; Thus, we can optimize for that case.
.rowLoop:
ld hl,(ix)
lea ix,ix + 0 ; SMCd to have correct byte count per row
smcByte _TextStraightBytesPerRow
ld b,iyl
.columnLoopStart:
add hl,hl
.unsetColumnLoopJr1:
jr nc,.unsetColumnLoopStart
; For set pixels
.setColumnLoopStart:
ld a,c
ld (de),a
inc de
dec b
jr z,.columnLoopEnd
.setColumnLoop:
add hl,hl
.unsetColumnLoopJr2:
jr nc,.unsetColumnLoopStart
.setColumnLoopMiddle:
ld (de),a
inc de
djnz .setColumnLoop
jr .columnLoopEnd
; For unset pixels, we use a special loop if transparency is requested
.unsetColumnLoopTransparent:
add hl,hl
jr c,.setColumnLoopMiddle
.unsetColumnLoopMiddleTransparent:
inc de
djnz .unsetColumnLoopTransparent
jr .columnLoopEnd
; For unset pixels with opacity on
.unsetColumnLoopStart:
ld a,TEXT_BG_COLOR ; SMCd to have correct background color
smcByte _TextStraightBackgroundColor
ld (de),a
inc de
dec b
jr z,.columnLoopEnd
.unsetColumnLoop:
add hl,hl
jr c,.setColumnLoopStart
.unsetColumnLoopMiddle:
ld (de),a
inc de
djnz .unsetColumnLoop
.columnLoopEnd:
ld hl,ti.lcdWidth - 0 ; SMCd to have correct row delta
smcByte _TextStraightRowDelta
add hl,de
ex de,hl
dec iyh
jr nz,.rowLoop
; OK done with the main work!
; Now deal with the spaceBelow metric
pop bc
ld a,(_CurrentFontProperties.spaceBelow)
or a,a
ret z
util.DrawEmptyLines:
; Internal routine that draws empty space for a glyph
; Arguments:
; A: Number of lines to draw (nonzero)
; B: -1 = opaque, 0 = transparent
; IYL: Width of line to draw
; DE: Drawing target
; (_TextStraightRowDelta - 2): Row delta
; Returns:
; Lines drawn
; Destroys:
; AF
; BC
; HL
ex de,hl
ld c,a
inc b
jr nz,.transparentLines
; Deal with clearing out pixels
ld a,(_TextStraightBackgroundColor)
ld de,(_TextStraightRowDelta - 2)
.clearLinesLoop:
ld b,iyl
.clearLinesInnerLoop:
ld (hl),a
inc hl
djnz .clearLinesInnerLoop
add hl,de
dec c
jr nz,.clearLinesLoop
ex de,hl
ret
.transparentLines:
ld b,ti.lcdWidth / 2
mlt bc
add hl,bc
add hl,bc
ex de,hl
ret
;-------------------------------------------------------------------------------
fontlib_DrawString:
; Draws a string,ending when either:
; an unknown control code is encountered (or NULL), or there is no more space
; left in the window.
; Arguments:
; arg0: Pointer to string
; arg1: Maximum number of characters have been printed
; Returns:
; New X cursor value
pop bc
ld (.retter + 1),bc
pop de
scf
sbc hl,hl
push hl
push de
call fontlib_DrawStringL
pop de
.retter:
jp 0
;-------------------------------------------------------------------------------
fontlib_DrawStringL:
; Draws a string, ending when any of the following is true:
; arg1 characters have been printed;
; an unknown control code is encountered (or NULL); or,
; there is no more space left in the window.
; Arguments:
; arg0: Pointer to string
; arg1: Maximum number of characters have been printed
; Returns:
; New X cursor value
push ix
; Since reentrancy isn't likely to be needed. . . .
; Instead of using stack locals, just access all our local and global
; variables via the (IX + offset) addressing mode.
ld ix,DataBaseAddr
res bWasNewline,(ix + newlineControl)
ld iy,0
add iy,sp
ld hl,(iy + arg1)
dec hl ; We're reading the string with pre-increment,
ld (ix + strReadPtr),hl ; so we need an initial pre-decrement
ld hl,(iy + arg2)
ld (ix + charactersLeft),hl
.restartX:
; Compute target drawing address
ld hl,(_TextY)
ld h,ti.lcdWidth / 2
mlt hl
add hl,hl
ld bc,(ix + textX)
add hl,bc
ld bc,(CurrentBuffer)
add hl,bc
ex de,hl
call gfx_Wait
.mainLoop:
; Check that we haven't exceeded our glyph printing limit
ld bc,(ix + charactersLeft)
sbc hl,hl
adc hl,bc
jr z,.exit
dec bc
ld (ix + charactersLeft),bc
; Read & validate glyph
ld hl,(ix + strReadPtr)
inc hl
ld (ix + strReadPtr),hl
; Read character
ld a,(hl)
; Check if control code
cp a,(ix + firstPrintableCodePoint)
jr nc,.notControlCode
or a,a
jr z,.exit
cp a,(ix + newLineCode)
jr z,.printNewline
.exit: ld hl,(ix + textX)
pop ix
ret
.notControlCode:
cp a,(ix + alternateStopCode)
jr z,.exit
; Check if font has given codepoint
sub a,(ix + strucFont.firstGlyph)
jr c,.exit
sbc hl,hl ; Zero for later
ld l,a
sub a,(ix + strucFont.totalGlyphs)
jr c,.definitelyValid
cp a,l ; 0 = 256 total glyphs,so check for zero
jr nz,.exit ; Z iff L == A, which is true iff totalGlyphs == 0
.definitelyValid:
ld (ix + readCharacter),l
; Look up width
ld bc,(ix + strucFont.widthsTablePtr)
add hl,bc
ld a,(hl)
; Check if glyph will fit in window
ld hl,(ix + textX)
ld bc,0
ld c,a
add hl,bc
ld bc,(ix + textXMax)
; or a,a ; C should already be reset from ADD HL,BC
sbc hl,bc
add hl,bc
jr z,.colOK
jr nc,.newline
; Correct for italicness
.colOK: ld c,(ix + strucFont.italicSpaceAdjust)
ld b,0
or a,a
sbc hl,bc
ld (ix + textX),hl
; OK,ready to draw the glyph
ld c,(ix + readCharacter)
push de
call util.DrawGlyphRawKnownWidth
pop de
ld ix,DataBaseAddr
; Update write pointer
ld a,iyl
sub a,(ix + strucFont.italicSpaceAdjust)
sbc hl,hl ; Sign-extend A for HL
ld l,a
add hl,de
ex de,hl
jr .mainLoop
.printNewline:
; Keep track of whether or not printing the current character needs to be retried
set bWasNewline,(ix + newlineControl)
.newline:
bit bWasNewline,(ix + newlineControl)
jr nz,.doNewline
bit bEnableAutoWrap,(ix + newlineControl)
jr z,.exit
.doNewline:
call fontlib_Newline
or a,a
jr nz,.exit
bit bWasNewline,(ix + newlineControl)
res bWasNewline,(ix + newlineControl)
jp nz,.restartX
ld hl,(ix + strReadPtr)
dec hl
ld (ix + strReadPtr),hl
jp .restartX
;-------------------------------------------------------------------------------
fontlib_DrawInt:
; Places an int at the current cursor position
; Shamelessly ripped from GraphX, which admittedly adapted it from Z80 Bits
; Arguments:
; arg0 : Number to print
; arg1 : Number of characters to print
; Returns:
; None
pop de
pop hl
push hl
push de
add hl,hl
db $3E ; xor a,a -> ld a,*
;-------------------------------------------------------------------------------
fontlib_DrawUInt:
; Places an unsigned int at the current cursor position
; Arguments:
; arg0 : Number to print
; arg1 : Minimum number of characters to print
; Returns:
; None
xor a,a
pop de
pop hl ; hl = uint
pop bc ; c = min num chars
push bc
push hl
push de
jr nc,.begin ; c ==> actually a negative int
ex de,hl
or a,a
sbc hl,hl
sbc hl,de ; hl = -int
ld a,'-'
smcByte _DrawIntMinus
call .printchar
dec c
jr nz,.begin
inc c
.begin:
ld de,-10000000
call .num1
ld de,-1000000
call .num1
ld de,-100000
call .num1
ld de,-10000
call .num1
ld de,-1000
call .num1
ld de,-100
call .num1
ld de,-10
call .num1
ld de,-1
.num1:
xor a,a
.num2:
inc a
add hl,de
jr c,.num2
sbc hl,de
dec a ; a = next digit
jr nz,.printdigit ; z ==> digit is zero, maybe don't print
ld a,c
inc c
cp a,8
ret c ; nc ==> a digit has already been
; printed, or must start printing
; to satisfy min num chars
xor a,a
.printdigit:
add a,'0'
smcByte _DrawIntZero
ld c,a ; mark that a digit has been printed
.printchar:
push bc
push hl
ld c,a
push bc
call fontlib_DrawGlyph
pop bc
pop hl
pop bc
ret
;-------------------------------------------------------------------------------
fontlib_SetForegroundColor:
; Sets the foreground color
; Arguments:
; arg0: Color
; Returns:
; Nothing
ld hl,arg0
add hl,sp
ld a,(hl)
ld (_TextStraightForegroundColor),a
ret
;-------------------------------------------------------------------------------
fontlib_SetBackgroundColor:
; Sets the background color
; Arguments:
; arg0: Color
; Returns:
; Nothing
ld hl,arg0
add hl,sp
ld a,(hl)
ld (_TextStraightBackgroundColor),a
ret
;-------------------------------------------------------------------------------
fontlib_SetColors:
; Sets both foreground and background color