BASIC compiler/interpreter for PIC32MX/MZ-80K
修訂 | 0f3767e7c0eca157a5230acd3432c5c92c63de75 (tree) |
---|---|
時間 | 2019-02-24 13:09:58 |
作者 | Katsumi <kmorimatsu@sour...> |
Commiter | Katsumi |
Class CKNJ8
@@ -0,0 +1,195 @@ | ||
1 | +REM CKNJ8.BAS ver 0.1 | |
2 | +REM Class CKNJ8 for MachiKania Type Z/M | |
3 | +REM using Misaki 8x8 font | |
4 | + | |
5 | +STATIC PRIVATE CACHE,CNUM,MODE,FBUFF,FO | |
6 | + | |
7 | +METHOD INIT | |
8 | + REM File buffer size is 8 bytes | |
9 | + dim FBUFF(1) | |
10 | + REM use 158 PCG fonts | |
11 | + dim CACHE(157) | |
12 | + REM CNUM=0-127 correspond 0x80-0xFF, CNUM=128-136 do 0x01-0x09, | |
13 | + REM and CNUM=137-157 do 0x0b-0x1f | |
14 | + CNUM=0 | |
15 | + REM Use PCG | |
16 | + usepcg | |
17 | + REM Set encoding | |
18 | + REM MODE: either "EUC-JP", or "UTF-8" | |
19 | + if 0<args(0) then | |
20 | + if 0=STRNCMP("EUC-JP",args$(1),7) then | |
21 | + MODE=1 | |
22 | + elseif 0=STRNCMP("UTF-8",args$(1),6) then | |
23 | + MODE=2 | |
24 | + else | |
25 | + print "Illegal encoding:";args$(1) | |
26 | + end | |
27 | + endif | |
28 | + else | |
29 | + REM Default: EUC-JP | |
30 | + MODE=1 | |
31 | + endif | |
32 | + return | |
33 | + | |
34 | +REM Public method, PRT and GPRT | |
35 | +REM 1st argument: string to print | |
36 | +METHOD PRT | |
37 | + var t$,b,i,j | |
38 | + fclose | |
39 | + FO=0 | |
40 | + t$=gosub$(MKSTR,args$(1)) | |
41 | + print t$; | |
42 | + fclose | |
43 | + return | |
44 | + | |
45 | +METHOD GPRT | |
46 | + var t$,b,i,j | |
47 | + fclose | |
48 | + FO=0 | |
49 | + t$=gosub$(MKSTR,args$(1)) | |
50 | + gprint ,args(2),args(3),t$ | |
51 | + fclose | |
52 | + return | |
53 | + | |
54 | +REM Private method GETPCG | |
55 | +REM 1st param: JIS code # starting from 0x2121 | |
56 | +REM return: Byte as PCG character | |
57 | +LABEL GETPCG | |
58 | + var i | |
59 | + for i=0 to 157 | |
60 | + if CACHE(i)=args(1) then | |
61 | + REM Found in CACHE | |
62 | + if i<128 then return 0x80+i | |
63 | + if i<137 then return 0x01+i-128 | |
64 | + return 0x0b + i-137 | |
65 | + endif | |
66 | + next | |
67 | + REM Not found in CACHE | |
68 | + REM Open the file and find it. | |
69 | + gosub FGETCH,args(1) | |
70 | + i=CNUM | |
71 | + CNUM=CNUM+1 | |
72 | + if 157<=CNUM then CNUM=0 | |
73 | + if i<128 then | |
74 | + i=0x80+i | |
75 | + elseif i<137 then | |
76 | + i=0x01+i-128 | |
77 | + else | |
78 | + i=i-137 | |
79 | + endif | |
80 | + pcg i,gosub(LE2BE,FBUFF(0)),gosub(LE2BE,FBUFF(1)) | |
81 | + return i | |
82 | + | |
83 | +REM Private medthod FGETCH | |
84 | +REM 1st param: JIS code # starting from 0x2121 | |
85 | +LABEL FGETCH | |
86 | + var p | |
87 | + if 0=FO then | |
88 | + REM File isn't yet open | |
89 | + REM open it | |
90 | + FO=1 | |
91 | + if 2=MODE then | |
92 | + fopen "MISAKI.UNI","r" | |
93 | + else | |
94 | + fopen "MISAKI.JIS","r" | |
95 | + endif | |
96 | + endif | |
97 | + p=args(1) | |
98 | + if 2=MODE then | |
99 | + REM UTF-8 | |
100 | + if p<0x0500 then | |
101 | + p=p-0x500 | |
102 | + elseif p<0x2000 then | |
103 | + REM ERR | |
104 | + elseif p<0x2700 then | |
105 | + p=p-0x2000+0x0500 | |
106 | + elseif p<0x3000 then | |
107 | + REM ERR | |
108 | + elseif p<0x3100 then | |
109 | + p=p-0x3000+0x0c00 | |
110 | + elseif p<0x4e00 then | |
111 | + REM ERR | |
112 | + elseif p<0xa000 then | |
113 | + p=p-0x4e00+0x0d00 | |
114 | + elseif p<0xff00 then | |
115 | + REM ERR | |
116 | + else | |
117 | + p=p-0xff00+0x5f00 | |
118 | + endif | |
119 | + else | |
120 | + REM EUC | |
121 | + p=p-0xa1a1 | |
122 | + endif | |
123 | + fseek p*8 | |
124 | + fget FBUFF,8 | |
125 | + return | |
126 | + | |
127 | +REM Private method MKSTR | |
128 | +REM 1st param: JIS/EUC/UTF string | |
129 | +REM return: string with PCG set | |
130 | +LABEL MKSTR | |
131 | + t$="" | |
132 | + i=0 | |
133 | + if 1=MODE then | |
134 | + goto EUCSTR | |
135 | + elseif 2=MODE then | |
136 | + goto UTFSTR | |
137 | + else | |
138 | + goto EUCSTR | |
139 | + endif | |
140 | + | |
141 | +REM Private method EUCSTR | |
142 | +REM supports EUC-JP string | |
143 | +LABEL EUCSTR | |
144 | + while i<len(args$(1)) | |
145 | + b=peek(args(1)+i) | |
146 | + if 0xa0<b then | |
147 | + REM Detect Kanji | |
148 | + REM Get EUC code in var j | |
149 | + j=b*256+peek(args(1)+i+1) | |
150 | + i=i+2 | |
151 | + REM Get PCG character and add to t$ | |
152 | + j=gosub(GETPCG,j) | |
153 | + t$=t$+chr$(j) | |
154 | + else | |
155 | + t$=t$+chr$(b) | |
156 | + i=i+1 | |
157 | + endif | |
158 | + wend | |
159 | + return t$ | |
160 | + | |
161 | +REM Private method UTFSTR | |
162 | +REM supports UTF-8 string | |
163 | +LABEL UTFSTR | |
164 | + while i<len(args$(1)) | |
165 | + b=peek(args(1)+i) | |
166 | + if 0xc0 = (0xe0 and b) then | |
167 | + REM Get Unicode in j | |
168 | + j=((b and 0x1f)<<6)+(peek(args(1)+i+1) and 0x3f) | |
169 | + i=i+2 | |
170 | + REM Get PCG character and add to t$ | |
171 | + j=gosub(GETPCG,j) | |
172 | + t$=t$+chr$(j) | |
173 | + elseif 0xe0 = (0xf0 and b) then | |
174 | + REM Get Unicode in j | |
175 | + j=((b and 0x0f)<<12)+((peek(args(1)+i+1) and 0x3f)<<6)+(peek(args(1)+i+2) and 0x3f) | |
176 | + i=i+3 | |
177 | + REM Get PCG character and add to t$ | |
178 | + j=gosub(GETPCG,j) | |
179 | + t$=t$+chr$(j) | |
180 | + else | |
181 | + t$=t$+chr$(b) | |
182 | + i=i+1 | |
183 | + endif | |
184 | + wend | |
185 | + return t$ | |
186 | + | |
187 | +REM Private method LE2BE | |
188 | +REM Note that PCG statement is big endian | |
189 | +LABEL LE2BE | |
190 | + var i | |
191 | + i=args(1)<<24 | |
192 | + i=i or ((args(1) and 0xff00) <<8) | |
193 | + i=i or ((args(1) and 0xff0000) >>8) | |
194 | + i=i or (args(1)>>24) | |
195 | + return i |
@@ -0,0 +1,50 @@ | ||
1 | +<?php | |
2 | + | |
3 | +/* | |
4 | + | |
5 | + Binary font file generator for Misaki 8x8 font. | |
6 | + Place 'misaki_gothic.bdf' in the same directory and run this script. | |
7 | + The font file is used for UTF-8. | |
8 | + Misaki font was downloaded from: http://www.geocities.jp/littlimi/misaki.htm | |
9 | + On 2/23/2019, Misaki font is available from: http://littlelimit.net/ | |
10 | + | |
11 | +*/ | |
12 | + | |
13 | +$tfile=file_get_contents('./misaki_gothic.bdf'); | |
14 | +$ftable=array(); | |
15 | +preg_replace_callback('/STARTCHAR[\s]+([0-9a-f]{4})[\s\S]*?(([0-9a-f]{2}[\s]+){8})/',function($m) use(&$ftable){ | |
16 | + /* JIS 0x3835: 元 */ | |
17 | + /* example: | |
18 | + STARTCHAR 3835 | |
19 | + ENCODING 14389 | |
20 | + SWIDTH 960 0 | |
21 | + DWIDTH 8 0 | |
22 | + BBX 8 8 0 -2 | |
23 | + BITMAP | |
24 | + 7c | |
25 | + 00 | |
26 | + fe | |
27 | + 28 | |
28 | + 28 | |
29 | + 4a | |
30 | + 8e | |
31 | + 00 | |
32 | + ENDCHAR | |
33 | + */ | |
34 | + $ftable[hexdec($m[1])]=preg_replace('/[\s]+/','',$m[2]); | |
35 | +},$tfile); | |
36 | +//print_r($ftable); | |
37 | + | |
38 | +$result=''; | |
39 | +for($code=0x2121;$code<=0x7426;$code++){ | |
40 | + if (isset($ftable[$code])) { | |
41 | + for($i=0;$i<16;$i+=2){ | |
42 | + $b=substr($ftable[$code],$i,2); | |
43 | + $result.=chr(hexdec($b)); | |
44 | + } | |
45 | + } else { | |
46 | + $result.="\x00\x00\x00\x00\x00\x00\x00\x00"; | |
47 | + } | |
48 | +} | |
49 | + | |
50 | +file_put_contents('./MISAKI.JIS',$result); |
@@ -0,0 +1,100 @@ | ||
1 | +<?php | |
2 | +/* | |
3 | + | |
4 | + Binary font file generator for Misaki 8x8 font. | |
5 | + Place 'misaki_gothic.bdf' in the same directory and run this script. | |
6 | + Place 'JIS0208.TXT' in the same directory and run this script. | |
7 | + The font file is used for UTF-8. | |
8 | + Misaki font was downloaded from: http://www.geocities.jp/littlimi/misaki.htm | |
9 | + On 2/23/2019, Misaki font is available from: http://littlelimit.net/ | |
10 | + Unicode - JIS table was obtained from: http://www.unicode.org/Public/MAPPINGS/OBSOLETE/EASTASIA/JIS/JIS0208.TXT | |
11 | + | |
12 | +*/ | |
13 | + | |
14 | +$tfile=file_get_contents('./misaki_gothic.bdf'); | |
15 | +$ftable=array(); | |
16 | +preg_replace_callback('/STARTCHAR[\s]+([0-9a-f]{4})[\s\S]*?(([0-9a-f]{2}[\s]+){8})/',function($m) use(&$ftable){ | |
17 | + /* JIS 0x3835: 元 */ | |
18 | + /* example: | |
19 | + STARTCHAR 3835 | |
20 | + ENCODING 14389 | |
21 | + SWIDTH 960 0 | |
22 | + DWIDTH 8 0 | |
23 | + BBX 8 8 0 -2 | |
24 | + BITMAP | |
25 | + 7c | |
26 | + 00 | |
27 | + fe | |
28 | + 28 | |
29 | + 28 | |
30 | + 4a | |
31 | + 8e | |
32 | + 00 | |
33 | + ENDCHAR | |
34 | + */ | |
35 | + $ftable[hexdec($m[1])]=preg_replace('/[\s]+/','',$m[2]); | |
36 | +},$tfile); | |
37 | +//print_r($ftable); | |
38 | + | |
39 | +$tfile=file_get_contents('./JIS0208.TXT'); | |
40 | +$jtable=array(); | |
41 | +preg_replace_callback('/[\r\n]0x([0-9A-F]{4})[\s]+0x([0-9A-F]{4})[\s]+0x([0-9A-F]{4})/',function($m) use(&$jtable,&$ftable){ | |
42 | + // $m[1]: SJIS, $m[2]: JIS, $m[3]: UTF16 | |
43 | + if ($ftable[hexdec($m[2])]) { | |
44 | + $jtable[hexdec($m[3])]=$ftable[hexdec($m[2])]; | |
45 | + } | |
46 | +},$tfile); | |
47 | + | |
48 | +$result=''; | |
49 | +for($code=0x0000;$code<=0xffff;$code++){ | |
50 | + /* | |
51 | + Skip: | |
52 | + 0500 - 1fff | |
53 | + 2700 - 2fff | |
54 | + 3100 - 4dff | |
55 | + a000 - feff | |
56 | + Valid: | |
57 | + 0000 - 04ff (0500, total 0500) | |
58 | + 2000 - 26ff (0700, total 0c00) | |
59 | + 3000 - 30ff (0100, total 0d00) | |
60 | + 4e00 - 9fff (5200, total 5f00) | |
61 | + ff00 - ffff (0100, total 6000) | |
62 | + Therefore: | |
63 | + if P<0x0500 then | |
64 | + P=P-0x500 | |
65 | + elseif P<0x2000 then | |
66 | + REM ERR | |
67 | + elseif P<0x2700 then | |
68 | + P=P-0x2000+0x0500 | |
69 | + elseif P<0x3000 then | |
70 | + REM ERR | |
71 | + elseif P<0x3100 then | |
72 | + P=P-0x3000+0x0c00 | |
73 | + elseif P<0x4e00 then | |
74 | + REM ERR | |
75 | + elseif P<0xa000 then | |
76 | + P=P-0x4e00+0x0d00 | |
77 | + elseif P<0xff00 then | |
78 | + REM ERR | |
79 | + else | |
80 | + P=P-0xff00+0x5f00 | |
81 | + endif | |
82 | + */ | |
83 | + switch($code){ | |
84 | + case 0x0500: $code=0x2000; break; | |
85 | + case 0x2700: $code=0x3000; break; | |
86 | + case 0x3100: $code=0x4e00; break; | |
87 | + case 0xa000: $code=0xff00; break; | |
88 | + default: break; | |
89 | + } | |
90 | + if (isset($jtable[$code])) { | |
91 | + for($i=0;$i<16;$i+=2){ | |
92 | + $b=substr($jtable[$code],$i,2); | |
93 | + $result.=chr(hexdec($b)); | |
94 | + } | |
95 | + } else { | |
96 | + $result.="\x00\x00\x00\x00\x00\x00\x00\x00"; | |
97 | + } | |
98 | +} | |
99 | +file_put_contents('./MISAKI.UNI',$result); | |
100 | + |
@@ -0,0 +1,43 @@ | ||
1 | +<クラス名およびバージョン> | |
2 | +CKNJ8 | |
3 | +ver 0.1 | |
4 | + | |
5 | +<ファイル名> | |
6 | +CKNJ8.BAS | |
7 | +MISAKI.JIS | |
8 | +MISAKI.UNI | |
9 | + | |
10 | +<概要> | |
11 | +日本語表示クラス。美咲フォント(8x8)を使用し、PCGを用いてキャラクターディスプレイ | |
12 | +に、また、グラフィックディスプレイに日本語を含む文字列を表示する。文字コードは、 | |
13 | +EUC-JP, UTF-8に対応。 | |
14 | + | |
15 | +<コンストラクター> | |
16 | +第1引数 | |
17 | + 文字コードとして、"EUC-JP", "UTF-8"のいずれかを選択。省略した場合は、 | |
18 | + "EUC-JP"。 | |
19 | + | |
20 | +<パブリックフィールド> | |
21 | +なし | |
22 | + | |
23 | +<パブリックメソッド> | |
24 | +PRT(x$) | |
25 | + 日本語を含む文字列x$を、キャラクターディスプレイに表示する。 | |
26 | + | |
27 | +GPRT(x$,y,z) | |
28 | + 日本語を含む文字列x$を、グラフィックディスプレイに表示する。yは文字色、zは | |
29 | + 背景色を指定。 | |
30 | + | |
31 | +<使用例> | |
32 | +テキストモードでの日本語表示例。この場合は、BASファイルをUTF-8(BOMなし)で保存す | |
33 | +る事。 | |
34 | + | |
35 | +USECLASS CKNJ8 | |
36 | +K=NEW(CKNJ8,"UTF-8") | |
37 | +K.PRT("本日は晴天なり") | |
38 | + | |
39 | +次のように使用する事も出来る。 | |
40 | + | |
41 | +USECLASS CKNJ8 | |
42 | +CKNJ8::INIT("UTF-8") | |
43 | +CKNJ8::PRT("本日は晴天なり") |