Help with lookup table

Just starting out? Need help? Post your questions and find answers here.
miskox
User
User
Posts: 95
Joined: Sun Aug 27, 2017 7:37 pm
Location: Slovenia

Help with lookup table

Post by miskox »

Hello!

It has been a while since I registered here. Yesterday I finally managed to make my first PureBasic [console] application.

For the past years I've been doing lots of programming in Windows .bat so I have one .bat that has few thousands of lines. One of the tasks this app can do is it generates a special stream file (binary values 0x00 ... 0xff are created and written with pure .bat (see dostips.com for more info)..
I generate these files for a 3rd party application that displays some information from these files but with TrueType fonts. First image shows the correct number of capital letter 'I' and the next image shows that there is one character too many.

Image

Image

Same goes for capital letter 'M':

Image

Image

Looks like the total width of the usable sreen is 455 pixels (window cannot be resized). So I PrtScreened all the characters and created a table with characters' widths.

So to avoid this I decided to use only first 40 characters from the string but this causes that there is usually lots of trailing spaces.

So I added a code to calculate all the characters lengths so I would maximize the line displayed. Everything works great but it is very sloooow. I wrote a COBOL* program to calculate the lenghts and it is now a little better (this is very old pure DOS MS COBOL 5.0 - so 16-bit exectuable is build). So I decided I will try and do it PureBasic. And here I need your help.

Code: Select all

OpenConsole()
OpenFile (0, "input_file.txt", #PB_Ascii)
CreateFile(1,"output_file.txt",#PB_Ascii)

Label11:
If Eof (0) = 1
    Goto end
EndIf
niz$=ReadString (0)
sum.w=0
offset_cnt.w=1
Label00:
tmp_char$=Mid(niz$,offset_cnt.w,1)
tt_chr_len.w=13
If Asc(tmp_char$)=32
tt_chr_len.w=5
EndIf
If Asc(tmp_char$)=33
tt_chr_len.w=5
EndIf
If Asc(tmp_char$)=34
tt_chr_len.w=6
EndIf
If Asc(tmp_char$)=35
tt_chr_len.w=9
EndIf
If Asc(tmp_char$)=36
tt_chr_len.w=9
EndIf
If Asc(tmp_char$)=37
tt_chr_len.w=15
EndIf
If Asc(tmp_char$)=38
tt_chr_len.w=11
EndIf
If Asc(tmp_char$)=39
tt_chr_len.w=3
EndIf
If Asc(tmp_char$)=40
tt_chr_len.w=6
EndIf
If Asc(tmp_char$)=41
tt_chr_len.w=6
EndIf
If Asc(tmp_char$)=42
tt_chr_len.w=7
EndIf
If Asc(tmp_char$)=43
tt_chr_len.w=10
EndIf
If Asc(tmp_char$)=44
tt_chr_len.w=5
EndIf
If Asc(tmp_char$)=45
tt_chr_len.w=6
EndIf
If Asc(tmp_char$)=46
tt_chr_len.w=5
EndIf
If Asc(tmp_char$)=47
tt_chr_len.w=5
EndIf
If Asc(tmp_char$)=48
tt_chr_len.w=9
EndIf
If Asc(tmp_char$)=49
tt_chr_len.w=9
EndIf
If Asc(tmp_char$)=50
tt_chr_len.w=9
EndIf
If Asc(tmp_char$)=51
tt_chr_len.w=9
EndIf
If Asc(tmp_char$)=52
tt_chr_len.w=9
EndIf
If Asc(tmp_char$)=53
tt_chr_len.w=9
EndIf
If Asc(tmp_char$)=54
tt_chr_len.w=9
EndIf
If Asc(tmp_char$)=55
tt_chr_len.w=9
EndIf
If Asc(tmp_char$)=56
tt_chr_len.w=9
EndIf
If Asc(tmp_char$)=57
tt_chr_len.w=9
EndIf
If Asc(tmp_char$)=58
tt_chr_len.w=5
EndIf
If Asc(tmp_char$)=59
tt_chr_len.w=5
EndIf
If Asc(tmp_char$)=60
tt_chr_len.w=10
EndIf
If Asc(tmp_char$)=61
tt_chr_len.w=10
EndIf
If Asc(tmp_char$)=62
tt_chr_len.w=10
EndIf
If Asc(tmp_char$)=63
tt_chr_len.w=9
EndIf
If Asc(tmp_char$)=64
tt_chr_len.w=17
EndIf
If Asc(tmp_char$)=65
tt_chr_len.w=11
EndIf
If Asc(tmp_char$)=66
tt_chr_len.w=11
EndIf
If Asc(tmp_char$)=67
tt_chr_len.w=12
EndIf
If Asc(tmp_char$)=68
tt_chr_len.w=12
EndIf
If Asc(tmp_char$)=69
tt_chr_len.w=11
EndIf
If Asc(tmp_char$)=70
tt_chr_len.w=10
EndIf
If Asc(tmp_char$)=71
tt_chr_len.w=12
EndIf
If Asc(tmp_char$)=72
tt_chr_len.w=11
EndIf
If Asc(tmp_char$)=73
tt_chr_len.w=5
EndIf
If Asc(tmp_char$)=74
tt_chr_len.w=9
EndIf
If Asc(tmp_char$)=75
tt_chr_len.w=11
EndIf
If Asc(tmp_char$)=76
tt_chr_len.w=9
EndIf
If Asc(tmp_char$)=77
tt_chr_len.w=13
EndIf
If Asc(tmp_char$)=78
tt_chr_len.w=11
EndIf
If Asc(tmp_char$)=79
tt_chr_len.w=12
EndIf
If Asc(tmp_char$)=80
tt_chr_len.w=11
EndIf
If Asc(tmp_char$)=81
tt_chr_len.w=12
EndIf
If Asc(tmp_char$)=82
tt_chr_len.w=11
EndIf
If Asc(tmp_char$)=83
tt_chr_len.w=11
EndIf
If Asc(tmp_char$)=84
tt_chr_len.w=9
EndIf
If Asc(tmp_char$)=85
tt_chr_len.w=11
EndIf
If Asc(tmp_char$)=86
tt_chr_len.w=11
EndIf
If Asc(tmp_char$)=87
tt_chr_len.w=17
EndIf
If Asc(tmp_char$)=88
tt_chr_len.w=11
EndIf
If Asc(tmp_char$)=89
tt_chr_len.w=11
EndIf
If Asc(tmp_char$)=90
tt_chr_len.w=9
EndIf
If Asc(tmp_char$)=91
tt_chr_len.w=5
EndIf
If Asc(tmp_char$)=92
tt_chr_len.w=5
EndIf
If Asc(tmp_char$)=93
tt_chr_len.w=5
EndIf
If Asc(tmp_char$)=94
tt_chr_len.w=7
EndIf
If Asc(tmp_char$)=95
tt_chr_len.w=9
EndIf
If Asc(tmp_char$)=96
tt_chr_len.w=6
EndIf
If Asc(tmp_char$)=97
tt_chr_len.w=9
EndIf
If Asc(tmp_char$)=98
tt_chr_len.w=9
EndIf
If Asc(tmp_char$)=99
tt_chr_len.w=9
EndIf
If Asc(tmp_char$)=100
tt_chr_len.w=9
EndIf
If Asc(tmp_char$)=101
tt_chr_len.w=9
EndIf
If Asc(tmp_char$)=102
tt_chr_len.w=5
EndIf
If Asc(tmp_char$)=103
tt_chr_len.w=9
EndIf
If Asc(tmp_char$)=104
tt_chr_len.w=9
EndIf
If Asc(tmp_char$)=105
tt_chr_len.w=4
EndIf
If Asc(tmp_char$)=106
tt_chr_len.w=3
EndIf
If Asc(tmp_char$)=107
tt_chr_len.w=9
EndIf
If Asc(tmp_char$)=108
tt_chr_len.w=3
EndIf
If Asc(tmp_char$)=109
tt_chr_len.w=13
EndIf
If Asc(tmp_char$)=110
tt_chr_len.w=9
EndIf
If Asc(tmp_char$)=111
tt_chr_len.w=9
EndIf
If Asc(tmp_char$)=112
tt_chr_len.w=9
EndIf
If Asc(tmp_char$)=113
tt_chr_len.w=9
EndIf
If Asc(tmp_char$)=114
tt_chr_len.w=6
EndIf
If Asc(tmp_char$)=115
tt_chr_len.w=8
EndIf
If Asc(tmp_char$)=116
tt_chr_len.w=4
EndIf
If Asc(tmp_char$)=117
tt_chr_len.w=9
EndIf
If Asc(tmp_char$)=118
tt_chr_len.w=7
EndIf
If Asc(tmp_char$)=119
tt_chr_len.w=11
EndIf
If Asc(tmp_char$)=120
tt_chr_len.w=7
EndIf
If Asc(tmp_char$)=121
tt_chr_len.w=9
EndIf
If Asc(tmp_char$)=122
tt_chr_len.w=8
EndIf
If Asc(tmp_char$)=123
tt_chr_len.w=6
EndIf
If Asc(tmp_char$)=124
tt_chr_len.w=5
EndIf
If Asc(tmp_char$)=125
tt_chr_len.w=6
EndIf
If Asc(tmp_char$)=126
tt_chr_len.w=10
EndIf
If Asc(tmp_char$)=127
tt_chr_len.w=13
EndIf
If Asc(tmp_char$)=128
tt_chr_len.w=9
EndIf
If Asc(tmp_char$)=129
tt_chr_len.w=13
EndIf
If Asc(tmp_char$)=130
tt_chr_len.w=4
EndIf
If Asc(tmp_char$)=131
tt_chr_len.w=13
EndIf
If Asc(tmp_char$)=132
tt_chr_len.w=6
EndIf
If Asc(tmp_char$)=133
tt_chr_len.w=17
EndIf
If Asc(tmp_char$)=134
tt_chr_len.w=9
EndIf
If Asc(tmp_char$)=135
tt_chr_len.w=9
EndIf
If Asc(tmp_char$)=136
tt_chr_len.w=13
EndIf
If Asc(tmp_char$)=137
tt_chr_len.w=17
EndIf
If Asc(tmp_char$)=138
tt_chr_len.w=11
EndIf
If Asc(tmp_char$)=139
tt_chr_len.w=6
EndIf
If Asc(tmp_char$)=140
tt_chr_len.w=11
EndIf
If Asc(tmp_char$)=141
tt_chr_len.w=9
EndIf
If Asc(tmp_char$)=142
tt_chr_len.w=9
EndIf
If Asc(tmp_char$)=143
tt_chr_len.w=9
EndIf
If Asc(tmp_char$)=144
tt_chr_len.w=13
EndIf
If Asc(tmp_char$)=145
tt_chr_len.w=4
EndIf
If Asc(tmp_char$)=146
tt_chr_len.w=4
EndIf
If Asc(tmp_char$)=147
tt_chr_len.w=6
EndIf
If Asc(tmp_char$)=148
tt_chr_len.w=6
EndIf
If Asc(tmp_char$)=149
tt_chr_len.w=6
EndIf
If Asc(tmp_char$)=150
tt_chr_len.w=9
EndIf
If Asc(tmp_char$)=151
tt_chr_len.w=17
EndIf
If Asc(tmp_char$)=152
tt_chr_len.w=13
EndIf
If Asc(tmp_char$)=153
tt_chr_len.w=17
EndIf
If Asc(tmp_char$)=154
tt_chr_len.w=8
EndIf
If Asc(tmp_char$)=155
tt_chr_len.w=6
EndIf
If Asc(tmp_char$)=156
tt_chr_len.w=8
EndIf
If Asc(tmp_char$)=157
tt_chr_len.w=6
EndIf
If Asc(tmp_char$)=158
tt_chr_len.w=8
EndIf
If Asc(tmp_char$)=159
tt_chr_len.w=8
EndIf
If Asc(tmp_char$)=160
tt_chr_len.w=5
EndIf
If Asc(tmp_char$)=161
tt_chr_len.w=6
EndIf
If Asc(tmp_char$)=162
tt_chr_len.w=6
EndIf
If Asc(tmp_char$)=163
tt_chr_len.w=9
EndIf
If Asc(tmp_char$)=164
tt_chr_len.w=9
EndIf
If Asc(tmp_char$)=165
tt_chr_len.w=11
EndIf
If Asc(tmp_char$)=166
tt_chr_len.w=5
EndIf
If Asc(tmp_char$)=167
tt_chr_len.w=9
EndIf
If Asc(tmp_char$)=168
tt_chr_len.w=6
EndIf
If Asc(tmp_char$)=169
tt_chr_len.w=13
EndIf
If Asc(tmp_char$)=170
tt_chr_len.w=11
EndIf
If Asc(tmp_char$)=171
tt_chr_len.w=9
EndIf
If Asc(tmp_char$)=172
tt_chr_len.w=10
EndIf
If Asc(tmp_char$)=173
tt_chr_len.w=6
EndIf
If Asc(tmp_char$)=174
tt_chr_len.w=13
EndIf
If Asc(tmp_char$)=175
tt_chr_len.w=9
EndIf
If Asc(tmp_char$)=176
tt_chr_len.w=7
EndIf
If Asc(tmp_char$)=177
tt_chr_len.w=9
EndIf
If Asc(tmp_char$)=178
tt_chr_len.w=6
EndIf
If Asc(tmp_char$)=179
tt_chr_len.w=3
EndIf
If Asc(tmp_char$)=180
tt_chr_len.w=6
EndIf
If Asc(tmp_char$)=181
tt_chr_len.w=9
EndIf
If Asc(tmp_char$)=182
tt_chr_len.w=9
EndIf
If Asc(tmp_char$)=183
tt_chr_len.w=5
EndIf
If Asc(tmp_char$)=184
tt_chr_len.w=6
EndIf
If Asc(tmp_char$)=185
tt_chr_len.w=9
EndIf
If Asc(tmp_char$)=186
tt_chr_len.w=8
EndIf
If Asc(tmp_char$)=187
tt_chr_len.w=9
EndIf
If Asc(tmp_char$)=188
tt_chr_len.w=9
EndIf
If Asc(tmp_char$)=189
tt_chr_len.w=6
EndIf
If Asc(tmp_char$)=190
tt_chr_len.w=4
EndIf
If Asc(tmp_char$)=191
tt_chr_len.w=8
EndIf
If Asc(tmp_char$)=192
tt_chr_len.w=11
EndIf
If Asc(tmp_char$)=193
tt_chr_len.w=11
EndIf
If Asc(tmp_char$)=194
tt_chr_len.w=11
EndIf
If Asc(tmp_char$)=195
tt_chr_len.w=11
EndIf
If Asc(tmp_char$)=196
tt_chr_len.w=11
EndIf
If Asc(tmp_char$)=197
tt_chr_len.w=9
EndIf
If Asc(tmp_char$)=198
tt_chr_len.w=12
EndIf
If Asc(tmp_char$)=199
tt_chr_len.w=12
EndIf
If Asc(tmp_char$)=200
tt_chr_len.w=12
EndIf
If Asc(tmp_char$)=201
tt_chr_len.w=11
EndIf
If Asc(tmp_char$)=202
tt_chr_len.w=11
EndIf
If Asc(tmp_char$)=203
tt_chr_len.w=11
EndIf
If Asc(tmp_char$)=204
tt_chr_len.w=11
EndIf
If Asc(tmp_char$)=205
tt_chr_len.w=5
EndIf
If Asc(tmp_char$)=206
tt_chr_len.w=5
EndIf
If Asc(tmp_char$)=207
tt_chr_len.w=12
EndIf
If Asc(tmp_char$)=208
tt_chr_len.w=12
EndIf
If Asc(tmp_char$)=209
tt_chr_len.w=11
EndIf
If Asc(tmp_char$)=210
tt_chr_len.w=11
EndIf
If Asc(tmp_char$)=211
tt_chr_len.w=12
EndIf
If Asc(tmp_char$)=212
tt_chr_len.w=12
EndIf
If Asc(tmp_char$)=213
tt_chr_len.w=12
EndIf
If Asc(tmp_char$)=214
tt_chr_len.w=12
EndIf
If Asc(tmp_char$)=215
tt_chr_len.w=10
EndIf
If Asc(tmp_char$)=216
tt_chr_len.w=11
EndIf
If Asc(tmp_char$)=217
tt_chr_len.w=11
EndIf
If Asc(tmp_char$)=218
tt_chr_len.w=11
EndIf
If Asc(tmp_char$)=219
tt_chr_len.w=11
EndIf
If Asc(tmp_char$)=220
tt_chr_len.w=11
EndIf
If Asc(tmp_char$)=221
tt_chr_len.w=11
EndIf
If Asc(tmp_char$)=222
tt_chr_len.w=9
EndIf
If Asc(tmp_char$)=223
tt_chr_len.w=10
EndIf
If Asc(tmp_char$)=224
tt_chr_len.w=6
EndIf
If Asc(tmp_char$)=225
tt_chr_len.w=9
EndIf
If Asc(tmp_char$)=226
tt_chr_len.w=9
EndIf
If Asc(tmp_char$)=227
tt_chr_len.w=9
EndIf
If Asc(tmp_char$)=228
tt_chr_len.w=9
EndIf
If Asc(tmp_char$)=229
tt_chr_len.w=3
EndIf
If Asc(tmp_char$)=230
tt_chr_len.w=9
EndIf
If Asc(tmp_char$)=231
tt_chr_len.w=9
EndIf
If Asc(tmp_char$)=232
tt_chr_len.w=9
EndIf
If Asc(tmp_char$)=233
tt_chr_len.w=9
EndIf
If Asc(tmp_char$)=234
tt_chr_len.w=9
EndIf
If Asc(tmp_char$)=235
tt_chr_len.w=9
EndIf
If Asc(tmp_char$)=236
tt_chr_len.w=9
EndIf
If Asc(tmp_char$)=237
tt_chr_len.w=5
EndIf
If Asc(tmp_char$)=238
tt_chr_len.w=5
EndIf
If Asc(tmp_char$)=239
tt_chr_len.w=10
EndIf
If Asc(tmp_char$)=240
tt_chr_len.w=10
EndIf
If Asc(tmp_char$)=241
tt_chr_len.w=9
EndIf
If Asc(tmp_char$)=242
tt_chr_len.w=9
EndIf
If Asc(tmp_char$)=243
tt_chr_len.w=9
EndIf
If Asc(tmp_char$)=244
tt_chr_len.w=9
EndIf
If Asc(tmp_char$)=245
tt_chr_len.w=9
EndIf
If Asc(tmp_char$)=246
tt_chr_len.w=9
EndIf
If Asc(tmp_char$)=247
tt_chr_len.w=9
EndIf
If Asc(tmp_char$)=248
tt_chr_len.w=6
EndIf
If Asc(tmp_char$)=249
tt_chr_len.w=9
EndIf
If Asc(tmp_char$)=250
tt_chr_len.w=9
EndIf
If Asc(tmp_char$)=251
tt_chr_len.w=9
EndIf
If Asc(tmp_char$)=252
tt_chr_len.w=9
EndIf
If Asc(tmp_char$)=253
tt_chr_len.w=9
EndIf
If Asc(tmp_char$)=254
tt_chr_len.w=4
EndIf
If Asc(tmp_char$)=255
tt_chr_len.w=6
EndIf
   
      
sum.w=sum.w+tt_chr_len.w
If sum.w>450
  PrintN ("sum " + sum)
  PrintN ("offset_cnt " + offset_cnt)

  WriteString(1,Str(offset_cnt))
  Goto label11
EndIf

offset_cnt.w=offset_cnt.w + 1

Goto Label00   

end:
CloseFile (0)
CloseFile (1)
Input()



So my question is: how can 'if' part of the code be optimized (dim array)?
I need:

Code: Select all

if asc(character)=32 then truetype_font_character_length=5 in more efficient way.
Thanks.
Saso

(this is a temporary solution to skip the .bat part of calculating the required offset of input string - so why there are input and output file. Please focus only to the 'if' part. Thanks.

This table shows all the lengths for decimal values 32...255 if it is of any help (to import to a table or something - just first replace ### with required code, then ## and so on). For example: space (dec 32) has length of 5 pixels.

Code: Select all

#032##5###
#033##5###
#034##6###
#035##9###
#036##9###
#037##15###
#038##11###
#039##3###
#040##6###
#041##6###
#042##7###
#043##10###
#044##5###
#045##6###
#046##5###
#047##5###
#048##9###
#049##9###
#050##9###
#051##9###
#052##9###
#053##9###
#054##9###
#055##9###
#056##9###
#057##9###
#058##5###
#059##5###
#060##10###
#061##10###
#062##10###
#063##9###
#064##17###
#065##11###
#066##11###
#067##12###
#068##12###
#069##11###
#070##10###
#071##12###
#072##11###
#073##5###
#074##9###
#075##11###
#076##9###
#077##13###
#078##11###
#079##12###
#080##11###
#081##12###
#082##11###
#083##11###
#084##9###
#085##11###
#086##11###
#087##17###
#088##11###
#089##11###
#090##9###
#091##5###
#092##5###
#093##5###
#094##7###
#095##9###
#096##6###
#097##9###
#098##9###
#099##9###
#100##9###
#101##9###
#102##5###
#103##9###
#104##9###
#105##4###
#106##3###
#107##9###
#108##3###
#109##13###
#110##9###
#111##9###
#112##9###
#113##9###
#114##6###
#115##8###
#116##4###
#117##9###
#118##7###
#119##11###
#120##7###
#121##9###
#122##8###
#123##6###
#124##5###
#125##6###
#126##10###
#127##13###
#128##9###
#129##13###
#130##4###
#131##13###
#132##6###
#133##17###
#134##9###
#135##9###
#136##13###
#137##17###
#138##11###
#139##6###
#140##11###
#141##9###
#142##9###
#143##9###
#144##13###
#145##4###
#146##4###
#147##6###
#148##6###
#149##6###
#150##9###
#151##17###
#152##13###
#153##17###
#154##8###
#155##6###
#156##8###
#157##6###
#158##8###
#159##8###
#160##5###
#161##6###
#162##6###
#163##9###
#164##9###
#165##11###
#166##5###
#167##9###
#168##6###
#169##13###
#170##11###
#171##9###
#172##10###
#173##6###
#174##13###
#175##9###
#176##7###
#177##9###
#178##6###
#179##3###
#180##6###
#181##9###
#182##9###
#183##5###
#184##6###
#185##9###
#186##8###
#187##9###
#188##9###
#189##6###
#190##4###
#191##8###
#192##11###
#193##11###
#194##11###
#195##11###
#196##11###
#197##9###
#198##12###
#199##12###
#200##12###
#201##11###
#202##11###
#203##11###
#204##11###
#205##5###
#206##5###
#207##12###
#208##12###
#209##11###
#210##11###
#211##12###
#212##12###
#213##12###
#214##12###
#215##10###
#216##11###
#217##11###
#218##11###
#219##11###
#220##11###
#221##11###
#222##9###
#223##10###
#224##6###
#225##9###
#226##9###
#227##9###
#228##9###
#229##3###
#230##9###
#231##9###
#232##9###
#233##9###
#234##9###
#235##9###
#236##9###
#237##5###
#238##5###
#239##10###
#240##10###
#241##9###
#242##9###
#243##9###
#244##9###
#245##9###
#246##9###
#247##9###
#248##6###
#249##9###
#250##9###
#251##9###
#252##9###
#253##9###
#254##4###
#255##6###

* some background: I have experience with: ZX Spectrum BASIC, SHARP Pocket Computer Basic, Z80A assembler, SC61860 assembled, COBOL (MS-DOS and VAX/VMS), Visual Basic 6.0... maybe some more. I only have licensed MS COBOL 5.0 for pure MS-DOS (in a virtual machine) so I was forced to use it. And now I found PureBasic... I think I will register very soon...
wilbert
PureBasic Expert
PureBasic Expert
Posts: 3870
Joined: Sun Aug 08, 2004 5:21 am
Location: Netherlands

Re: Help with lookup table

Post by wilbert »

You could do something like

Code: Select all

DataSection
  CharLengths:
  Data.a 9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9; 0-15
  Data.a 9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9; 16-31
  Data.a 9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9; 32-47
  Data.a 9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9; 48-63
  Data.a 9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9 
  Data.a 9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9
  Data.a 9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9
  Data.a 9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9
  Data.a 9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9
  Data.a 9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9
  Data.a 9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9
  Data.a 9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9
  Data.a 9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9
  Data.a 9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9
  Data.a 9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9
  Data.a 9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9; 240-255
EndDataSection

Structure CharLengths
  len.a[256]
EndStructure

*CharLengths.CharLengths = ?CharLengths


Text.s = "This is a text"
*Char.Ascii = @Text

Sum = 0

While *Char\a
  Sum + *CharLengths\len[*Char\a]
  *Char + SizeOf(Character)
Wend

Debug Sum
The DataSection contains 256 width values; one for each ascii code.
You loop through a line of text using a pointer to an ascii character, lookup the width and add it to the sum.
Windows (x64)
Raspberry Pi OS (Arm64)
User avatar
Kukulkan
Addict
Addict
Posts: 1352
Joined: Mon Jun 06, 2005 2:35 pm
Location: germany
Contact:

Re: Help with lookup table

Post by Kukulkan »

Hi miskox,

easy to see that you are not familiar with modern development concepts. I strongly suggest to take a look into arrays, maps etc and also into the (not recommended) usage of goto or (better) other constructs for loops and repeated things.

In general, you try to find out how many characters there can be in one line, if the font is not a monospaced font (proportional). For this, functions like TextWidth() and TextHeight() are in PureBasic (https://www.purebasic.com/documentation ... width.html). But they need a drawing context/font, which is missing in console (you don't know the used font, or do you?). I suggest to do a windowed application and you can use them for handling text width and height easily.

Second, you did a huge bunch of if/endif constructs just to get a value for each of the ascii codes. How about using a map?

I would not do it like you did anyway, but see how more easy using a map would be:

Code: Select all

EnableExplicit

Define SomeWidth.w
NewMap CharWidth.w()

CharWidth(~" ") = 5 ; space
CharWidth(~"!") = 5  
CharWidth(~"\"") = 6 ; "
CharWidth(~"#") = 9  
CharWidth(~"$") = 9  
CharWidth(~"%") = 15  
CharWidth(~"&") = 11
; other characters and their widths

SomeWidth.w = CharWidth("$") ; will return 9

Debug SomeWidth.w
See how easy you can access the values? The approach of Wilbert is even more compact, but not that easy to handle (hard to see which value for which character).

Maybe you try to go through the PureBasic help and also try to understand the examples and hints and tips there first?
User avatar
Mijikai
Addict
Addict
Posts: 1360
Joined: Sun Sep 11, 2016 2:17 pm

Re: Help with lookup table

Post by Mijikai »

Heres wilberts example in assembly (x64).

CharLen() -> returns the size of a a chr value
CharLenStr() -> retuns the size of a string

I have padded the size form 0 - 32 with zeros!

Code:

Code: Select all

EnableExplicit

Global TestString.s = "Hello World!"

Procedure.i CharLen(Code.i)
  !mov rcx,[p.v_Code]
  !mov rax,@f
  !movzx rax,byte[rax + rcx]
  ProcedureReturn
  !@@:
  !db 00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00
  !db 00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00
  !db 05,05,06,09,09,15,11,03,06,06,07,10,05,06,05,05
  !db 09,09,09,09,09,09,09,09,09,09,05,05,10,10,10,09
  !db 17,11,11,12,12,11,10,12,11,05,09,11,09,13,11,12
  !db 11,12,11,11,09,11,11,17,11,11,09,05,05,05,07,09
  !db 06,09,09,09,09,09,05,09,09,04,03,09,03,13,09,09
  !db 09,09,06,08,04,09,07,11,07,09,08,06,05,06,10,13
  !db 09,13,04,13,06,17,09,09,13,17,11,06,11,09,09,09
  !db 13,04,04,06,06,06,09,17,13,17,08,06,08,06,08,08
  !db 05,06,06,09,09,11,05,09,06,13,11,09,10,06,13,09
  !db 07,09,06,03,06,09,09,05,06,09,08,09,09,06,04,08
  !db 11,11,11,11,11,09,12,12,12,11,11,11,11,05,05,12
  !db 12,11,11,12,12,12,12,10,11,11,11,11,11,11,09,10
  !db 06,09,09,09,09,03,09,09,09,09,09,09,09,05,05,10
  !db 10,09,09,09,09,09,09,09,06,09,09,09,09,09,04,06
EndProcedure

Procedure.i CharLenStr(*String)
  !mov rcx,[p.p_String]
  !mov rdx,CharLenTable
  !xor rax,rax
  !test rcx,rcx
  !jz @f
  !CharLenLoop:
  !mov r8w,word[rcx]
  !test r8w,r8w
  !jz @f
  !movzx r9,byte[rdx + r8]
  !lea rax,[rax+r9]
  !add rcx,2h
  !jmp CharLenLoop
  !@@:
  ProcedureReturn
  !CharLenTable:
  !db 00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00
  !db 00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00
  !db 05,05,06,09,09,15,11,03,06,06,07,10,05,06,05,05
  !db 09,09,09,09,09,09,09,09,09,09,05,05,10,10,10,09
  !db 17,11,11,12,12,11,10,12,11,05,09,11,09,13,11,12
  !db 11,12,11,11,09,11,11,17,11,11,09,05,05,05,07,09
  !db 06,09,09,09,09,09,05,09,09,04,03,09,03,13,09,09
  !db 09,09,06,08,04,09,07,11,07,09,08,06,05,06,10,13
  !db 09,13,04,13,06,17,09,09,13,17,11,06,11,09,09,09
  !db 13,04,04,06,06,06,09,17,13,17,08,06,08,06,08,08
  !db 05,06,06,09,09,11,05,09,06,13,11,09,10,06,13,09
  !db 07,09,06,03,06,09,09,05,06,09,08,09,09,06,04,08
  !db 11,11,11,11,11,09,12,12,12,11,11,11,11,05,05,12
  !db 12,11,11,12,12,12,12,10,11,11,11,11,11,11,09,10
  !db 06,09,09,09,09,03,09,09,09,09,09,09,09,05,05,10
  !db 10,09,09,09,09,09,09,09,06,09,09,09,09,09,04,06
EndProcedure

Debug CharLen(32)
Debug CharLenStr(@TestString)

End
miskox
User
User
Posts: 95
Joined: Sun Aug 27, 2017 7:37 pm
Location: Slovenia

Re: Help with lookup table

Post by miskox »

@all: Thank you for your replies.

As Kukulkan correctly noticed: I don't know any modern programming concepts. That is why I registered here in 2017 and only just yesterday tried to do something...

Also @Kukulkan: "..you don't know the used font, or do you?"

No. This application uses one TrueType font.With the help of PrintScreen and zoom-in I managed to determine length of the characters.

I will try and study to see which example is better for my needs (though I think I will not need to update the data - i calculated the length of TrueType characters used in this 3rd party application).

Again, thanks.
I am sure I will have more questions...

Saso
infratec
Always Here
Always Here
Posts: 6883
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: Help with lookup table

Post by infratec »

You can avoid an additional program:

Code: Select all

EnableExplicit

Procedure GetFontWidth(Font$, Size.i, Array Fontsize.i(1))
  
  Protected Font.i, Img.i, i.i
  
  
  Font = LoadFont(#PB_Any, Font$, Size)
  If Font
    Img = CreateImage(#PB_Any, 50, 50)
    If StartDrawing(ImageOutput(Img))
      DrawingFont(FontID(Font))
      For i = 0 To 255
        Fontsize(i) = TextWidth(Chr(i))
      Next i
      StopDrawing()
    EndIf
    FreeImage(Img)
  EndIf
  
EndProcedure


Procedure.i GetStringWidth(Text$, Array Fontsize.i(1))
  
  Protected *Char.Ascii, Sum.i
  
  
  *Char = @Text$
  
  While *Char\a
    Sum + Fontsize(*Char\a)
    *Char + SizeOf(Character)
  Wend
  
  ProcedureReturn Sum
  
EndProcedure


Define i.i
Dim FontArray.i(255)

GetFontWidth("Arial", 12, FontArray())

For i = 0 To 255
  Debug Str(i) + ":" + Chr(i) + " -> " + Str(FontArray(i))
Next i

Debug ""

Debug GetStringWidth("Hello world", FontArray())
Bernd
User avatar
Mijikai
Addict
Addict
Posts: 1360
Joined: Sun Sep 11, 2016 2:17 pm

Re: Help with lookup table

Post by Mijikai »

@infratec nice :)
miskox
User
User
Posts: 95
Joined: Sun Aug 27, 2017 7:37 pm
Location: Slovenia

Re: Help with lookup table

Post by miskox »

@infratec: great...but this is not what I am looking for. This 3rd party application was written in National Instrument's LabVIEW - I cannot control the fonts (I don't know what fonts this app is using). I have a table with font's widths.

My solution is this (enter a value at the end to display it's value):

Code: Select all

OpenConsole()
Define a.i

Dim a(255)

DataSection
  
;         0   1   2   3   4   5   6   7   8   9  10  11  12  13  14  15
  Data.i 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13

;        16  17  18  19  20  21  22  23  24  25  26  27  28  29  30  31
  Data.i 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13

;        32  33  34  35  36  37  38  39  40  41  42  43  44  45  46  47
  data.i  5,  5,  6,  9,  9, 15, 11,  3,  6,  6,  7, 10,  5,  6,  5,  5

;        48  49  50  51  52  53  54  55  56  57  58  59  60  61  62  63
  data.i  9,  9,  9,  9,  9,  9,  9,  9,  9,  9,  5,  5, 10, 10, 10,  9

;        64  65  66  67  68  69  70  71  72  73  74  75  76  77  78  79
  data.i 17, 11, 11, 12, 12, 11, 10, 12, 11,  5,  9, 11,  9, 13, 11, 12

;        80  81  82  83  84  85  86  87  88  89  90  91  92  93  94  95
  data.i 11, 12, 11, 11,  9, 11, 11, 17, 11, 11,  9,  5,  5,  5,  7,  9

;        96  97  98  99 100 101 102 103 104 105 106 107 108 109 110 111
  data.i  6,  9,  9,  9,  9,  9,  5,  9,  9,  4,  3,  9,  3, 13,  9,  9

;       112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127
  data.i  9,  9,  6,  8,  4,  9,  7, 11,  7,  9,  8,  6,  5,  6, 10, 13

;       128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143
  data.i  9, 13,  4, 13,  6, 17,  9,  9, 13, 17, 11,  6, 11,  9,  9,  9

;       144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159

  data.i 13,  4,  4,  6,  6,  6,  9, 17, 13, 17,  8,  6,  8,  6,  8,  8

;       160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175
  data.i  5,  6,  6,  9,  9, 11,  5,  9,  6, 13, 11,  9, 10,  6, 13,  9

;       176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191
  data.i  7,  9,  6,  3,  6,  9,  9,  5,  6,  9,  8,  9,  9,  6,  4,  8

;       192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207
  data.i 11, 11, 11, 11, 11,  9, 12, 12, 12, 11, 11, 11, 11,  5,  5, 12

;       208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223
  data.i 12, 11, 11, 12, 12, 12, 12, 10, 11, 11, 11, 11, 11, 11,  9, 10

;       224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239
  data.i  6,  9,  9,  9,  9,  3,  9,  9,  9,  9,  9,  9,  9,  5,  5, 10

;       240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255
  data.i 10,  9,  9,  9,  9,  9,  9,  9,  6,  9,  9,  9,  9,  9,  4,  6

  
EndDataSection

For n=0 To 255
    Read a(n)
    
    PrintN (Str(n) + " " + Str(a(n)))
        
Next n

m$=Input ()

m.i=Val(m$)

PrintN (Str(m) + " " + Str(a(m)))

Input()

; IDE Options = PureBasic 5.70 LTS (Windows - x86)
; CursorPosition = 30
; EnableXP
Thanks.

Saso
miskox
User
User
Posts: 95
Joined: Sun Aug 27, 2017 7:37 pm
Location: Slovenia

Re: Help with lookup table

Post by miskox »

Hello again!

Here it is - the latest version:

(notes:
- len - 1 is calculated because 'input_file.txt' is created by .bat which echo's string with

Code: Select all

echo %string%;>input_file.txt
to avoid getting any errors (if string ends with a number that would mean writing to a special pipe (4> or something). So I must add ; to be sure everything is ok)
- openconsole() is missing because I call this program from a .bat file. If openconsole is present command interpreter opens new console window any time .exe is called
)

Code: Select all

Define.i chr_len, n, sum, offset_cnt

Dim chr_len(255)

For n=0 To 255
	Read chr_len(n)
Next n

If ReadFile (0, "input_file.txt", #PB_Ascii)
While Eof (0) = 0
  input_string$=ReadString (0)
  input_string$=Left(input_string$,Len(input_string$)-1)
  offset_cnt=0
	sum=0
	While (sum < 450 And offset_cnt < Len(input_string$))
		offset_cnt = offset_cnt + 1
	  tmp_char$=Mid(input_string$,offset_cnt,1)
	  *ascii_tmp=Ascii (tmp_char$)
	  ascii_value=PeekA(*ascii_tmp)
	  FreeMemory (*ascii_tmp)
	  sum=sum + chr_len(ascii_value)
	Wend
  If CreateFile(1,"output_file.txt",#PB_Ascii)
	WriteString(1,Str(offset_cnt))
  	CloseFile (1)
  Else
  	PrintN ("Error creating output file.")
  	End
  EndIf

Wend
	CloseFile (0)
Else
	PrintN ("Error opening input file.")
EndIf

End

 DataSection
   
; ----------------------------------------------------------------------
;         0   1   2   3   4   5   6   7   8   9  10  11  12  13  14  15
  Data.i 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13
; ----------------------------------------------------------------------
;        16  17  18  19  20  21  22  23  24  25  26  27  28  29  30  31
  Data.i 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13
; ----------------------------------------------------------------------
;        32  33  34  35  36  37  38  39  40  41  42  43  44  45  46  47
  Data.i  5,  5,  6,  9,  9, 15, 11,  3,  6,  6,  7, 10,  5,  6,  5,  5
; ----------------------------------------------------------------------
;        48  49  50  51  52  53  54  55  56  57  58  59  60  61  62  63
  Data.i  9,  9,  9,  9,  9,  9,  9,  9,  9,  9,  5,  5, 10, 10, 10,  9
; ----------------------------------------------------------------------
;        64  65  66  67  68  69  70  71  72  73  74  75  76  77  78  79
  Data.i 17, 11, 11, 12, 12, 11, 10, 12, 11,  5,  9, 11,  9, 13, 11, 12
; ----------------------------------------------------------------------
;        80  81  82  83  84  85  86  87  88  89  90  91  92  93  94  95
  Data.i 11, 12, 11, 11,  9, 11, 11, 17, 11, 11,  9,  5,  5,  5,  7,  9
; ----------------------------------------------------------------------
;        96  97  98  99 100 101 102 103 104 105 106 107 108 109 110 111
  Data.i  6,  9,  9,  9,  9,  9,  5,  9,  9,  4,  3,  8,  3, 13,  9,  9
; ----------------------------------------------------------------------
;       112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127
  Data.i  9,  9,  6,  8,  4,  9,  7, 11,  7,  9,  8,  6,  5,  6, 10, 13
; ----------------------------------------------------------------------
;       128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143
  Data.i  9, 13,  4, 13,  6, 17,  9,  9, 13, 17, 11,  6, 11,  9,  9,  9
; ----------------------------------------------------------------------
;       144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159
  Data.i 13,  4,  4,  6,  6,  6,  9, 17, 13, 17,  8,  6,  8,  6,  8,  8
; ----------------------------------------------------------------------
;       160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175
  Data.i  5,  6,  6,  9,  9, 11,  5,  9,  6, 13, 11,  9, 10,  6, 13,  9
; ----------------------------------------------------------------------
;       176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191
  Data.i  7,  9,  6,  3,  6,  9,  9,  5,  6,  9,  8,  9,  9,  6,  4,  8
; ----------------------------------------------------------------------
;       192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207
  Data.i 11, 11, 11, 11, 11,  9, 12, 12, 12, 11, 11, 11, 11,  5,  5, 12
; ----------------------------------------------------------------------
;       208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223
  Data.i 12, 11, 11, 12, 12, 12, 12, 10, 11, 11, 11, 11, 11, 11,  9, 10
; ----------------------------------------------------------------------
;       224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239
  Data.i  6,  9,  9,  9,  9,  3,  9,  9,  9,  9,  9,  9,  9,  5,  5, 10
; ----------------------------------------------------------------------
;       240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255
  Data.i 10,  9,  9,  9,  9,  9,  9,  9,  6,  9,  9,  9,  9,  9,  4,  6
; ----------------------------------------------------------------------

EndDataSection

; IDE Options = PureBasic 5.70 LTS (Windows - x86)
; ExecutableFormat = Console
; CursorPosition = 36
; FirstLine = 12
; EnableXP
; Executable = TrueTypeFontLength.exe
; DisableDebugger
I will slowly port my .bat to PureBasic...

Thank you all for your help.

Saso
Post Reply