Rev 7798 | Go to most recent revision | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 7798 | Rev 7853 | ||
---|---|---|---|
1 | // Сам шрифт представляет. |
1 | // Сам шрифт представляет. |
2 | 2 | ||
3 | // Голова: |
3 | // Голова: |
4 | // [2 байта символы:KF] |
4 | // [2 байта символы:KF] |
5 | // [4 байта:указатель на название шрифта] |
5 | // [4 байта:указатель на название шрифта] |
6 | // [1 байт:размер массива указателей на размеры шрифтов, указатель 4 байта, т.е. размер = размер массива*4] |
6 | // [1 байт:размер массива указателей на размеры шрифтов, указатель 4 байта, т.е. размер = размер массива*4] |
7 | // [размер массива*4 байт:указатели..] |
7 | // [размер массива*4 байт:указатели..] |
8 | 8 | ||
9 | // Тело файла: |
9 | // Тело файла: |
10 | // [4 байта:масштаб ширина][4 байта:масштаб высота][255*4 байт:указатели на символы][масштаб ширина*масштаб высота байт: данные символов..] |
10 | // [4 байта:масштаб ширина][4 байта:масштаб высота][255*4 байт:указатели на символы][масштаб ширина*масштаб высота байт: данные символов..] |
11 | 11 | ||
12 | // Конец: |
12 | // Конец: |
13 | // [Название шрифта:"Times New Roman"] |
13 | // [Название шрифта:"Times New Roman"] |
14 | 14 | ||
15 | 15 | ||
16 | #ifndef INCLUDE_KFONT_H |
16 | #ifndef INCLUDE_KFONT_H |
17 | #define INCLUDE_KFONT_H |
17 | #define INCLUDE_KFONT_H |
18 | 18 | ||
19 | #ifndef INCLUDE_MATH_H |
19 | #ifndef INCLUDE_MATH_H |
20 | #include "../lib/math.h" |
20 | #include "../lib/math.h" |
21 | #endif |
21 | #endif |
22 | 22 | ||
23 | #ifndef INCLUDE_IO_H |
23 | #ifndef INCLUDE_IO_H |
24 | #include "../lib/io.h" |
24 | #include "../lib/io.h" |
25 | #endif |
25 | #endif |
26 | 26 | ||
27 | #include "../lib/patterns/rgb.h" |
27 | #include "../lib/patterns/rgb.h" |
28 | 28 | ||
29 | 29 | ||
30 | #define DEFAULT_FONT "/sys/fonts/Tahoma.kf" |
30 | #define DEFAULT_FONT "/sys/fonts/Tahoma.kf" |
31 | 31 | ||
32 | #ifndef KFONT_BPP |
32 | #ifndef KFONT_BPP |
33 | #define KFONT_BPP 4 |
33 | #define KFONT_BPP 4 |
34 | #endif |
34 | #endif |
35 | 35 | ||
36 | int kfont_char_width[255]; |
36 | int kfont_char_width[255]; |
37 | 37 | ||
38 | :struct __SIZE |
38 | :struct __SIZE |
39 | { |
39 | { |
40 | dword width,height; |
40 | dword width,height; |
41 | signed offset_x, offset_y; |
41 | signed offset_x, offset_y; |
42 | byte pt; |
42 | byte pt; |
43 | }; |
43 | }; |
44 | :struct KFONT |
44 | :struct KFONT |
45 | { |
45 | { |
46 | __SIZE size; |
46 | __SIZE size; |
47 | int width,height; |
47 | int width,height; |
48 | byte bold,smooth; |
48 | byte bold,smooth; |
49 | dword color, background; |
49 | dword color, background; |
50 | dword font,font_begin; |
50 | dword font,font_begin; |
51 | word block; |
51 | word block; |
52 | dword raw; |
52 | dword raw; |
53 | dword raw_size; |
53 | dword raw_size; |
54 | 54 | ||
55 | bool init(); |
55 | bool init(); |
56 | bool changeSIZE(); |
56 | bool changeSIZE(); |
57 | byte symbol(); |
57 | byte symbol(); |
58 | byte symbol_size(); |
58 | byte symbol_size(); |
59 | dword getsize(); |
59 | dword getsize(); |
60 | int get_label_width(); |
60 | int get_label_width(); |
61 | 61 | ||
62 | void ApplySmooth(); |
62 | void ApplySmooth(); |
63 | int WriteIntoWindow(); |
63 | int WriteIntoWindow(); |
64 | int WriteIntoWindowCenter(); |
64 | int WriteIntoWindowCenter(); |
65 | void WriteIntoBuffer(); |
65 | dword WriteIntoBuffer(); |
66 | void ShowBuffer(); |
66 | void ShowBuffer(); |
67 | void ShowBufferPart(); |
67 | void ShowBufferPart(); |
68 | } kfont; |
68 | } kfont; |
69 | 69 | ||
70 | :bool KFONT::init(dword font_path) |
70 | :bool KFONT::init(dword font_path) |
71 | { |
71 | { |
72 | IO label_io; |
72 | IO label_io; |
73 | if(font)free(font); |
73 | if(font)free(font); |
74 | label_io.read(font_path); |
74 | label_io.read(font_path); |
75 | if(!EAX) { |
75 | if(!EAX) { |
76 | label_io.run("/sys/@notify", "'Error: KFONT is not loaded.' -E"); |
76 | label_io.run("/sys/@notify", "'Error: KFONT is not loaded.' -E"); |
77 | return false; |
77 | return false; |
78 | } |
78 | } |
79 | font_begin = label_io.buffer_data; |
79 | font_begin = label_io.buffer_data; |
80 | changeSIZE(); |
80 | changeSIZE(); |
81 | smooth = true; |
81 | smooth = true; |
82 | return true; |
82 | return true; |
83 | } |
83 | } |
84 | 84 | ||
85 | :bool KFONT::changeSIZE() |
85 | :bool KFONT::changeSIZE() |
86 | { |
86 | { |
87 | int i; |
87 | int i; |
88 | dword file_size; |
88 | dword file_size; |
89 | dword ofs; |
89 | dword ofs; |
90 | if(size.pt<9) size.pt = 9; |
90 | if(size.pt<9) size.pt = 9; |
91 | font = font_begin; |
91 | font = font_begin; |
92 | ofs = DSDWORD[calc(size.pt-8<<2+font_begin)]; |
92 | ofs = DSDWORD[calc(size.pt-8<<2+font_begin)]; |
93 | if(ofs==-1)return false; |
93 | if(ofs==-1)return false; |
94 | font += ofs + 156; |
94 | font += ofs + 156; |
95 | file_size = DSDWORD[calc(font)]; |
95 | file_size = DSDWORD[calc(font)]; |
96 | height = DSBYTE[calc(font+file_size) - 1]; |
96 | height = DSBYTE[calc(font+file_size) - 1]; |
97 | width = DSBYTE[calc(font+file_size) - 2]; |
97 | width = DSBYTE[calc(font+file_size) - 2]; |
98 | block = math.ceil(height*width/32); |
98 | block = math.ceil(height*width/32); |
99 | for (i=0; i<256; i++) { |
99 | for (i=0; i<256; i++) { |
100 | kfont_char_width[i] = symbol_size((byte) i); |
100 | kfont_char_width[i] = symbol_size((byte) i); |
101 | } |
101 | } |
102 | return true; |
102 | return true; |
103 | } |
103 | } |
104 | 104 | ||
105 | :dword KFONT::getsize(byte font_size, dword text1) |
105 | :dword KFONT::getsize(byte font_size, dword text1) |
106 | { |
106 | { |
107 | size.height = size.width = 0; |
107 | size.height = size.width = 0; |
108 | size.offset_x = size.offset_y = -1; |
108 | size.offset_x = size.offset_y = -1; |
109 | if (size.pt != font_size) { |
109 | if (size.pt != font_size) { |
110 | size.pt = font_size; |
110 | size.pt = font_size; |
111 | if(!changeSIZE())return 0; |
111 | if(!changeSIZE())return 0; |
112 | } |
112 | } |
113 | WHILE(DSBYTE[text1]) |
113 | WHILE(DSBYTE[text1]) |
114 | { |
114 | { |
115 | size.width += symbol_size(DSBYTE[text1]); |
115 | size.width += symbol_size(DSBYTE[text1]); |
116 | text1++; |
116 | text1++; |
117 | } |
117 | } |
118 | $neg size.offset_y |
118 | $neg size.offset_y |
119 | $neg size.offset_x |
119 | $neg size.offset_x |
120 | size.height += size.offset_y+1; |
120 | size.height += size.offset_y+1; |
121 | size.width += size.offset_x+1; |
121 | size.width += size.offset_x+1; |
122 | return size.width; |
122 | return size.width; |
123 | } |
123 | } |
124 | 124 | ||
125 | //WILL NOT WORK if requested font_size |
125 | //WILL NOT WORK if requested font_size |
126 | //is differ from precalculated kfont_char_width[] |
126 | //is differ from precalculated kfont_char_width[] |
127 | :int KFONT::get_label_width(dword _label) |
127 | :int KFONT::get_label_width(dword _label) |
128 | { |
128 | { |
129 | int len=0; |
129 | int len=0; |
130 | while (ESBYTE[_label]) { |
130 | while (ESBYTE[_label]) { |
131 | len += kfont_char_width[ ESBYTE[_label] ]; |
131 | len += kfont_char_width[ ESBYTE[_label] ]; |
132 | _label++; |
132 | _label++; |
133 | } |
133 | } |
134 | return len; |
134 | return len; |
135 | } |
135 | } |
136 | 136 | ||
137 | :byte KFONT::symbol_size(byte s) |
137 | :byte KFONT::symbol_size(byte s) |
138 | { |
138 | { |
139 | int chaw_width; |
139 | int chaw_width; |
140 | chaw_width = symbol(0,0, s, 0); |
140 | chaw_width = symbol(0,0, s, 0); |
141 | if(bold) chaw_width += math.ceil(size.pt/17); |
141 | if(bold) chaw_width += math.ceil(size.pt/17); |
142 | return chaw_width; |
142 | return chaw_width; |
143 | } |
143 | } |
144 | 144 | ||
145 | :byte KFONT::symbol(signed x,y; byte s; dword image_raw) |
145 | :byte KFONT::symbol(signed x,y; byte s; dword image_raw) |
146 | { |
146 | { |
147 | dword xi,yi; |
147 | dword xi,yi; |
148 | dword iii = 0; |
148 | dword iii = 0; |
149 | dword offs; |
149 | dword offs; |
150 | dword tmp, _; |
150 | dword tmp, _; |
151 | byte X; |
151 | byte X; |
152 | byte chaw_width=0; |
152 | byte chaw_width=0; |
153 | if(s==32)return width/4+1; |
153 | if(s==32)return width/4+1; |
154 | if(s==9)return width; |
154 | if(s==9)return width; |
155 | s = Cp866ToAnsi(s); |
155 | s = Cp866ToAnsi(s); |
156 | tmp = block*s << 2 + font; |
156 | tmp = block*s << 2 + font; |
157 | for(yi=0; yi |
157 | for(yi=0; yi |
158 | { |
158 | { |
159 | EDI = size.offset_y + yi + y * size.width * KFONT_BPP + image_raw; |
159 | EDI = size.offset_y + yi + y * size.width * KFONT_BPP + image_raw; |
160 | for(xi=0; xi |
160 | for(xi=0; xi |
161 | { |
161 | { |
162 | if(iii%32) _ >>= 1; |
162 | if(iii%32) _ >>= 1; |
163 | else |
163 | else |
164 | { |
164 | { |
165 | tmp += 4; |
165 | tmp += 4; |
166 | _ = DSDWORD[tmp]; |
166 | _ = DSDWORD[tmp]; |
167 | } |
167 | } |
168 | if(_&1) //check does the pixel set |
168 | if(_&1) //check does the pixel set |
169 | { |
169 | { |
170 | if(xi>chaw_width)chaw_width=xi; |
170 | if(xi>chaw_width)chaw_width=xi; |
171 | //in case of image_raw!=0 draw font into bug |
171 | //in case of image_raw!=0 draw font into bug |
172 | //in case of image_raw==0 calculate size |
172 | //in case of image_raw==0 calculate size |
173 | if (image_raw) |
173 | if (image_raw) |
174 | { |
174 | { |
175 | offs = x + xi * KFONT_BPP + EDI; |
175 | offs = x + xi * KFONT_BPP + EDI; |
176 | DSDWORD[offs] = color; |
176 | DSDWORD[offs] = color; |
177 | if(bold) DSDWORD[offs+KFONT_BPP] = color; |
177 | if(bold) DSDWORD[offs+KFONT_BPP] = color; |
178 | } |
178 | } |
179 | else |
179 | else |
180 | { |
180 | { |
181 | if(size.height |
181 | if(size.height |
182 | if(size.offset_y<0)size.offset_y = yi; else if(yi |
182 | if(size.offset_y<0)size.offset_y = yi; else if(yi |
183 | if(!X) X = xi; else if(X>xi)X = xi; |
183 | if(!X) X = xi; else if(X>xi)X = xi; |
184 | if(size.offset_x<0)size.offset_x = X; |
184 | if(size.offset_x<0)size.offset_x = X; |
185 | } |
185 | } |
186 | } |
186 | } |
187 | iii++; |
187 | iii++; |
188 | } |
188 | } |
189 | } |
189 | } |
190 | return chaw_width; |
190 | return chaw_width; |
191 | } |
191 | } |
192 | 192 | ||
193 | inline fastcall Cp866ToAnsi(AL) { |
193 | inline fastcall Cp866ToAnsi(AL) { |
194 | if (AL>=128)&&(AL<=175) return AL+64; |
194 | if (AL>=128)&&(AL<=175) return AL+64; |
195 | if (AL>=224)&&(AL<=239) return AL+16; |
195 | if (AL>=224)&&(AL<=239) return AL+16; |
196 | if (AL==241) return 184; //e ruAL with dotAL (yo) |
196 | if (AL==241) return 184; //e ruAL with dotAL (yo) |
197 | if (AL==240) return 168; //E ruAL with dotAL (yo) |
197 | if (AL==240) return 168; //E ruAL with dotAL (yo) |
198 | if (AL==242) return 'E'; //E ukr (ye) |
198 | if (AL==242) return 'E'; //E ukr (ye) |
199 | if (AL==243) return 186; //e ukr (ye) |
199 | if (AL==243) return 186; //e ukr (ye) |
200 | if (AL==244) return 'I'; //I ukr (yi) |
200 | if (AL==244) return 'I'; //I ukr (yi) |
201 | if (AL==245) return 191; //i ukr (yi) |
201 | if (AL==245) return 191; //i ukr (yi) |
202 | return AL; |
202 | return AL; |
203 | } |
203 | } |
204 | 204 | ||
205 | /*===================================================================================== |
205 | /*===================================================================================== |
206 | =========================== =========================== |
206 | =========================== =========================== |
207 | =========================== RAW =========================== |
207 | =========================== RAW =========================== |
208 | =========================== =========================== |
208 | =========================== =========================== |
209 | =====================================================================================*/ |
209 | =====================================================================================*/ |
210 | 210 | ||
211 | inline fastcall dword b32(EAX) { return DSDWORD[EAX]; } |
211 | inline fastcall dword b32(EAX) { return DSDWORD[EAX]; } |
212 | :void KFONT::ApplySmooth() |
212 | :void KFONT::ApplySmooth() |
213 | { |
213 | { |
214 | dword i,line_w,to,dark_background; |
214 | dword i,line_w,to,dark_background; |
215 | line_w = size.width * KFONT_BPP; |
215 | line_w = size.width * KFONT_BPP; |
216 | to = size.height - 1 * line_w + raw - KFONT_BPP; |
216 | to = size.height - 1 * line_w + raw - KFONT_BPP; |
217 | for(i=raw; i < to; i+=KFONT_BPP) |
217 | for(i=raw; i < to; i+=KFONT_BPP) |
218 | { |
218 | { |
219 | if(i-raw%line_w +KFONT_BPP == line_w) continue; |
219 | if(i-raw%line_w +KFONT_BPP == line_w) continue; |
220 | // pixels position, where b - black, w - write |
220 | // pixels position, where b - black, w - write |
221 | // bw |
221 | // bw |
222 | // wb |
222 | // wb |
223 | if(b32(i)!=background) { |
223 | if(b32(i)!=background) { |
224 | if (b32(i+KFONT_BPP)==background) |
224 | if (b32(i+KFONT_BPP)==background) |
225 | && (b32(i+line_w)==background) && (b32(i+KFONT_BPP+line_w)!=background) |
225 | && (b32(i+line_w)==background) && (b32(i+KFONT_BPP+line_w)!=background) |
226 | { |
226 | { |
227 | dark_background = MixColors(background,b32(i),200); |
227 | dark_background = MixColors(background,b32(i),200); |
228 | DSDWORD[i+KFONT_BPP] = dark_background; |
228 | DSDWORD[i+KFONT_BPP] = dark_background; |
229 | DSDWORD[i+line_w] = dark_background; |
229 | DSDWORD[i+line_w] = dark_background; |
230 | } |
230 | } |
231 | } |
231 | } |
232 | // wb |
232 | // wb |
233 | // bw |
233 | // bw |
234 | else if (b32(i+KFONT_BPP)!=background) |
234 | else if (b32(i+KFONT_BPP)!=background) |
235 | && (b32(i+line_w)!=background) && (b32(i+KFONT_BPP+line_w)==background) |
235 | && (b32(i+line_w)!=background) && (b32(i+KFONT_BPP+line_w)==background) |
236 | { |
236 | { |
237 | dark_background = MixColors(background,b32(i+KFONT_BPP),200); |
237 | dark_background = MixColors(background,b32(i+KFONT_BPP),200); |
238 | DSDWORD[i] = dark_background; |
238 | DSDWORD[i] = dark_background; |
239 | DSDWORD[i+KFONT_BPP+line_w] = dark_background; |
239 | DSDWORD[i+KFONT_BPP+line_w] = dark_background; |
240 | } |
240 | } |
241 | } |
241 | } |
242 | } |
242 | } |
243 | 243 | ||
244 | :void KFONT::WriteIntoBuffer(int x,y,w,h; dword _background, _color; byte font_size; dword text1) |
244 | :dword KFONT::WriteIntoBuffer(int x,y,w,h; dword _background, _color; byte font_size; dword text1) |
245 | { |
245 | { |
246 | dword new_raw_size; |
246 | dword new_raw_size; |
247 | if(!text1)return; |
247 | if(!text1)return; |
248 | 248 | ||
249 | if (size.pt != font_size) { |
249 | if (size.pt != font_size) { |
250 | getsize(font_size, text1); |
250 | getsize(font_size, text1); |
251 | y -= size.offset_y; |
251 | y -= size.offset_y; |
252 | } |
252 | } |
253 | color = _color; |
253 | color = _color; |
254 | background = _background; |
254 | background = _background; |
255 | 255 | ||
256 | size.width = w; |
256 | size.width = w; |
257 | size.height = h; |
257 | size.height = h; |
258 | 258 | ||
259 | new_raw_size = w*h*KFONT_BPP; |
259 | new_raw_size = w*h*KFONT_BPP; |
260 | if(raw_size != new_raw_size) |
260 | if(raw_size != new_raw_size) |
261 | { |
261 | { |
262 | raw_size = new_raw_size; |
262 | raw_size = new_raw_size; |
263 | free(raw); |
263 | free(raw); |
264 | raw = malloc(raw_size); |
264 | raw = malloc(raw_size); |
265 | // Fill background color |
265 | // Fill background color |
266 | EBX = background; |
266 | EBX = background; |
267 | EAX = raw_size+raw; |
267 | EAX = raw_size+raw; |
268 | for (EDI=raw; EDI |
268 | for (EDI=raw; EDI |
269 | } |
269 | } |
270 | WHILE(DSBYTE[text1]) |
270 | WHILE(DSBYTE[text1]) |
271 | { |
271 | { |
272 | x+=symbol(x,y,DSBYTE[text1], raw); |
272 | x+=symbol(x,y,DSBYTE[text1], raw); |
273 | if(bold)x+=math.ceil(size.pt/17); |
273 | if(bold)x+=math.ceil(size.pt/17); |
274 | text1++; |
274 | text1++; |
275 | } |
275 | } |
276 | return; |
276 | return x; |
277 | } |
277 | } |
278 | 278 | ||
279 | :int KFONT::WriteIntoWindow(int x,y; dword _background, _color; byte font_size; dword text1) |
279 | :int KFONT::WriteIntoWindow(int x,y; dword _background, _color; byte font_size; dword text1) |
280 | { |
280 | { |
281 | if(!text1)return 0; |
281 | if(!text1)return 0; |
282 | getsize(font_size, text1); |
282 | getsize(font_size, text1); |
283 | raw_size = NULL; |
283 | raw_size = NULL; |
284 | WriteIntoBuffer(0, -size.offset_y, size.width-size.offset_x, |
284 | WriteIntoBuffer(0, -size.offset_y, size.width-size.offset_x, |
285 | size.height-size.offset_y, _background, _color, font_size, text1); |
285 | size.height-size.offset_y, _background, _color, font_size, text1); |
286 | if (smooth) ApplySmooth(); |
286 | if (smooth) ApplySmooth(); |
287 | ShowBuffer(x,y); |
287 | ShowBuffer(x,y); |
288 | return size.offset_x + size.width; |
288 | return size.offset_x + size.width; |
289 | } |
289 | } |
290 | 290 | ||
291 | :int KFONT::WriteIntoWindowCenter(dword x, _y,w,h, _background, _color; byte font_size; dword text1) |
291 | :int KFONT::WriteIntoWindowCenter(dword x, _y,w,h, _background, _color; byte font_size; dword text1) |
292 | { |
292 | { |
293 | getsize(font_size, text1); |
293 | getsize(font_size, text1); |
294 | return WriteIntoWindow(w-size.width/2+x-1, _y, _background, _color, font_size, text1); |
294 | return WriteIntoWindow(w-size.width/2+x-1, _y, _background, _color, font_size, text1); |
295 | } |
295 | } |
296 | 296 | ||
297 | :void KFONT::ShowBuffer(dword _x, _y) |
297 | :void KFONT::ShowBuffer(dword _x, _y) |
298 | { |
298 | { |
299 | if (4==KFONT_BPP) PutPaletteImage(raw, size.width, size.height, _x, _y, 32, 0); |
299 | if (4==KFONT_BPP) PutPaletteImage(raw, size.width, size.height, _x, _y, 32, 0); |
300 | //if (1==KFONT_BPP) PutPaletteImage(raw, size.width, size.height, _x, _y, 8, #palette); |
300 | //if (1==KFONT_BPP) PutPaletteImage(raw, size.width, size.height, _x, _y, 8, #palette); |
301 | } |
301 | } |
302 | 302 | ||
303 | :void KFONT::ShowBufferPart(dword _x, _y, _w, _h, _buf_offset) |
303 | :void KFONT::ShowBufferPart(dword _x, _y, _w, _h, _buf_offset) |
304 | { |
304 | { |
305 | if (4==KFONT_BPP) PutPaletteImage(_buf_offset * KFONT_BPP + raw, _w, _h, _x, _y, 32, 0); |
305 | if (4==KFONT_BPP) PutPaletteImage(_buf_offset * KFONT_BPP + raw, _w, _h, _x, _y, 32, 0); |
306 | //if (1==KFONT_BPP) PutPaletteImage(_buf_offset * KFONT_BPP + raw, _w, _h, _x, _y, 8, #palette); |
306 | //if (1==KFONT_BPP) PutPaletteImage(_buf_offset * KFONT_BPP + raw, _w, _h, _x, _y, 8, #palette); |
307 | } |
307 | } |
308 | 308 | ||
309 | #endif>=239)>=175)>0)size.offset_x>0)size.offset_y>><>256;>2+font_begin)]; |
309 | #endif>=239)>=175)>0)size.offset_x>0)size.offset_y>><>256;>2+font_begin)]; |
310 | ><2+font_begin)]; |
310 | ><2+font_begin)]; |
311 | >9)> |
311 | >9)> |