]> git.sur5r.net Git - cc65/blob - libsrc/tgi/tgi_gettextwidth.s
Relocate register values outside structs, + more
[cc65] / libsrc / tgi / tgi_gettextwidth.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         umul8x16r16
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_gettextwidth (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_gettextwidth
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         ldy     _tgi_charwidth+1
41         sty     ptr1
42         jmp     umul8x16r16
43
44 ; Return the width of the string for the vector font. To save some code, we
45 ; will add up all the character widths and then multiply by the scale factor.
46 ; Since the output routine will scale each single character, the result may
47 ; be slightly different.
48
49 @L1:    sta     Text
50         stx     Text+1                  ; Save pointer to string
51
52         lda     _tgi_vectorfont+1
53         tax
54         ora     _tgi_vectorfont
55         beq     @L9                     ; Return zero if no font
56
57         lda     _tgi_vectorfont
58         clc
59         adc     #<(TGI_VECTORFONT::WIDTHS - TGI_VF_FIRSTCHAR)
60         sta     WTab
61         txa
62         adc     #>(TGI_VECTORFONT::WIDTHS - TGI_VF_FIRSTCHAR)
63         sta     WTab+1
64
65         ldy     #0
66         sty     Width
67         sty     Width+1                 ; Zero the total width
68
69 ; Sum up the widths of the single characters
70
71 @L2:    ldy     #0
72         lda     (Text),y                ; Get next char
73         beq     @L4                     ; Bail out if end of text reached
74         jsr     _toascii                ; Convert to ascii
75         tay
76         lda     (WTab),y                ; Get width of this char
77         clc
78         adc     Width
79         sta     Width
80         bcc     @L3
81         inc     Width+1
82 @L3:    inc     Text
83         bne     @L2
84         inc     Text+1
85         bne     @L2
86
87 ; We have the total width now, scale and return it
88
89 @L4:    lda     _tgi_textscalew
90         ldx     _tgi_textscalew+1
91         jmp     tgi_imulround
92
93 ; Exit point if no font installed
94
95 @L9:    rts
96
97 .endproc
98
99
100