]> git.sur5r.net Git - cc65/blob - libsrc/tgi/tgi_textwidth.s
477510d8e40449a396afad09350d024831430977
[cc65] / libsrc / tgi / tgi_textwidth.s
1 ;
2 ; Ullrich von Bassewitz, 2009-10-30
3 ;
4
5
6         .include        "tgi-kernel.inc"
7         .include        "tgi-vectorfont.inc"
8         .include        "zeropage.inc"
9
10         .import         _strlen, _toascii
11         .import         umul16x16r32
12
13 ;-----------------------------------------------------------------------------
14 ; Aliases for zero page locations
15
16 Width   := ptr1
17 WTab    := ptr2
18 Text    := ptr3
19
20
21
22 ;-----------------------------------------------------------------------------
23 ; unsigned __fastcall__ tgi_textwidth (const char* s);
24 ; /* Calculate the width of the text in pixels according to the current text
25 ;  * style.
26 ;  */
27 ;
28 ; Result is  strlen (s) * tgi_textmagw * tgi_fontsizex
29 ;
30
31 .code
32 .proc   _tgi_textwidth
33
34         ldy     _tgi_font
35         bne     @L1                     ; Jump if vector font
36
37 ; Return the width of the string for the bitmap font
38
39         jsr     _strlen
40         sta     ptr1
41         stx     ptr1+1
42         lda     _tgi_charwidth
43         ldx     #0
44         jmp     umul16x16r32
45
46 ; Return the width of the string for the vector font. To save some code, we
47 ; will add up all the character widths and then multiply by the scale factor.
48 ; Since the output routine will scale each single character, the result may
49 ; be slightly different.
50
51 @L1:    sta     Text
52         stx     Text+1                  ; Save pointer to string
53
54         lda     _tgi_vectorfont+1
55         tax
56         ora     _tgi_vectorfont
57         beq     @L9                     ; Return zero if no font
58
59         lda     _tgi_vectorfont
60         clc
61         adc     #<(TGI_VECTORFONT::WIDTHS - TGI_VF_FIRSTCHAR)
62         sta     WTab
63         txa
64         adc     #>(TGI_VECTORFONT::WIDTHS - TGI_VF_FIRSTCHAR)
65         sta     WTab+1
66
67         ldy     #0
68         sty     Width
69         sty     Width+1                 ; Zero the total width
70
71 ; Sum up the widths of the single characters
72
73 @L2:    ldy     #0
74         lda     (Text),y                ; Get next char
75         beq     @L4                     ; Bail out if end of text reached
76         jsr     _toascii                ; Convert to ascii
77         tay
78         lda     (WTab),y                ; Get width of this char
79         clc
80         adc     Width   
81         sta     Width
82         bcc     @L3
83         inc     Width+1
84 @L3:    inc     Text
85         bne     @L2
86         inc     Text+1
87         bne     @L2
88
89 ; We have the total width now, scale and return it
90
91 @L4:    lda     _tgi_textscalew
92         ldx     _tgi_textscalew+1
93         jmp     tgi_imulround
94
95 ; Exit point if no font installed
96
97 @L9:    rts
98
99 .endproc
100
101
102