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