From 9497d50e2f42ac65d553e2ec6319fb7f8c3a2455 Mon Sep 17 00:00:00 2001 From: "Kirill Lipatov (Leency)" Date: Thu, 17 Dec 2015 19:15:49 +0000 Subject: [PATCH] font.h: make spaghetti code a little bit less ugly kolibri font viewer: revert to older version, because code readability matters git-svn-id: svn://kolibrios.org@5981 a494cfbc-eb01-0410-851d-a64ba20cac60 --- programs/cmm/browser/WebView.c | 31 +- programs/cmm/eolite/Eolite.c | 2 +- programs/cmm/eolite/include/settings.h | 6 +- .../kolibri font viewer/kolibri font viewer.c | 63 +--- programs/cmm/lib/font.h | 294 ++++++++---------- .../cmm/software_widget/software_widget.c | 6 +- 6 files changed, 174 insertions(+), 228 deletions(-) diff --git a/programs/cmm/browser/WebView.c b/programs/cmm/browser/WebView.c index ffd025a327..29169c8d67 100644 --- a/programs/cmm/browser/WebView.c +++ b/programs/cmm/browser/WebView.c @@ -8,12 +8,14 @@ #endif //libraries -#define MEMSIZE 0x100000 +#define MEMSIZE 1060000 #include "..\lib\gui.h" #include "..\lib\draw_buf.h" #include "..\lib\list_box.h" #include "..\lib\cursor.h" #include "..\lib\collection.h" +#include "..\lib\font.h" + //*.obj libraries #include "..\lib\obj\box_lib.h" #include "..\lib\obj\libio_lib.h" @@ -46,7 +48,6 @@ char homepage[] = FROM "html\\homepage.htm"; #define URL_SERVICE_HOME "WebView://home" #define URL_SERVICE_SOURCE "WebView://source:" - proc_info Form; //char search_path[]="http://nigma.ru/index.php?s="; @@ -115,6 +116,15 @@ void main() WB1.DrawBuf.zoom = 1; WB1.list.SetFont(8, 14, 10111000b); WB1.list.no_selection = true; + //font init + font.no_bg_copy = true; + font.bg_color = 0xFFFFFF; + font.load(DEFAULT_FONT); + if (!font.data) { + notify("'Error: Font is not loaded.' -E"); + ExitProcess(); + } + // SetEventMask(0xa7); BEGIN_LOOP_APPLICATION: WaitEventTimeout(2); @@ -250,6 +260,8 @@ void SetElementSizes() if (WB1.list.w!=WB1.DrawBuf.bufw) WB1.DrawBuf.Init(WB1.list.x, WB1.list.y, WB1.list.w, WB1.list.h * 30); } +int list__w; + void Draw_Window() { DrawBar(0,0, Form.cwidth,TOOLBAR_H-2, panel_color); @@ -268,6 +280,21 @@ void Draw_Window() if (!header) OpenPage(); else { WB1.DrawPage(); DrawEditBox(); } DrawRectangle(scroll_wv.start_x, scroll_wv.start_y, scroll_wv.size_x, scroll_wv.size_y-1, scroll_wv.bckg_col); DrawProgress(); + + list__w = 200; + font.buffer_size = 0; + font.size.height = 200; + font.color = 0; + font.size.text = 11; + font.prepare_buf(10,10,list__w,font.size.height, "Hello World!"); + font.color = 0xFF00FF; + font.size.text = 12; + font.prepare_buf(10,23,list__w,font.size.height, "How are you?"); + font.color = 0x2E74BB; + font.size.text = 15; + font.prepare_buf(11,40,list__w,font.size.height, "Fine"); + SmoothFont(font.buffer, font.size.width, font.size.height); + _PutImage(0,0,list__w,font.size.height,font.buffer); } diff --git a/programs/cmm/eolite/Eolite.c b/programs/cmm/eolite/Eolite.c index 19886e2f34..0a9eb618f2 100644 --- a/programs/cmm/eolite/Eolite.c +++ b/programs/cmm/eolite/Eolite.c @@ -718,7 +718,7 @@ void Line_ReDraw(dword bgcol, filenum){ else { font.bg_color = bgcol; - font.use_smooth = smooth_font; + font.smooth = smooth_font; font.prepare(files.x + 23, files.item_h - font.height / 2 + y, file_name_off); font.show(); } diff --git a/programs/cmm/eolite/include/settings.h b/programs/cmm/eolite/include/settings.h index e2ad09969a..3c618164a3 100644 --- a/programs/cmm/eolite/include/settings.h +++ b/programs/cmm/eolite/include/settings.h @@ -6,7 +6,7 @@ ?define SHOW_REAL_NAMES "Показывать имена файлов не меняя регистр" ?define FONT_SIZE_LABEL "Размер шрифта" ?define USE_TWO_PANELS "Две панели" - ?define USE_SMOOTH_FONT "Использовать сглаженный шрифт" + ?define smooth_FONT "Использовать сглаженный шрифт" ?define LIST_LINE_HEIGHT "Высота строки в списке" ?define NOTIFY_COPY_END "Уведомлять о завершении копирования" ?define T_DOUBLE_CLICK "Время двойного клика (в сотых)" @@ -18,7 +18,7 @@ ?define SHOW_REAL_NAMES "Show real file names without changing case" ?define FONT_SIZE_LABEL "Font size" ?define USE_TWO_PANELS "Two panels" - ?define USE_SMOOTH_FONT "Use smooth font" + ?define smooth_FONT "Use smooth font" ?define LIST_LINE_HEIGHT "List line height" ?define NOTIFY_COPY_END "Notify when copying finished" ?define T_DOUBLE_CLICK "Double click time (in hundredths)" @@ -92,7 +92,7 @@ void DrawSettingsCheckBoxes() CheckBox2(10, 33, 21, SHOW_REAL_NAMES, real_files_names_case); CheckBox2(10, 55, 22, NOTIFY_COPY_END, info_after_copy); CheckBox2(10, 77, 24, USE_TWO_PANELS, two_panels); - //CheckBox2(10, 99, 29, USE_SMOOTH_FONT, smooth_font); + //CheckBox2(10, 99, 29, smooth_FONT, smooth_font); MoreLessBox(10, 99, 18, 27, 28, #system.color, MOUSE_TIME, T_DOUBLE_CLICK); MoreLessBox(10, 125, 18, 25, 26, #system.color, files.item_h, LIST_LINE_HEIGHT); if (font.data) MoreLessBox(10, 152, 18, 30, 31, #system.color, font.size.text, FONT_SIZE_LABEL); diff --git a/programs/cmm/kolibri font viewer/kolibri font viewer.c b/programs/cmm/kolibri font viewer/kolibri font viewer.c index b813e350d7..abe6e2711e 100644 --- a/programs/cmm/kolibri font viewer/kolibri font viewer.c +++ b/programs/cmm/kolibri font viewer/kolibri font viewer.c @@ -8,36 +8,15 @@ void main() { proc_info Form; - FONT font_title = 0; - FONT font_option = 0; word i, y, btn; char line[256], title[4196]; font.no_bg_copy = true; font.color = 0; font.bg_color = 0xFFFFFF; - font.left = 5; - font.top = SKIN.height+PANELH; - if (!param) strcpy(#param, "/sys/fonts/Tahoma.kf"); + if (!param) strcpy(#param, DEFAULT_FONT); font.load(#param); - - font_title.no_bg_copy = true; - font_title.load("/sys/fonts/Tahoma.kf"); - font_title.size.text = 12; - font_title.color = 0x444444; - font_title.weight = 1; - font_title.use_smooth = 1; - font_title.bg_color = 0xE1E1E1; - - font_option.no_bg_copy = true; - font_option.load("/sys/fonts/Tahoma.kf"); - font_option.size.text = 13; - font_option.color = 0x222222; - font_option.use_smooth = 1; - font_option.bg_color = 0xDADADA; - strcpy(#title, "Font preview: "); strcat(#title, #param); - font_title.prepare(5, 4, #title); loop() { switch(WaitEvent()) @@ -45,49 +24,33 @@ void main() case evButton: btn = GetButtonID(); if (btn==1) ExitProcess(); - if (btn==2) font.weight ^=1; + if (btn==2) font.bold ^=1; if (btn==3) font.italic ^=1; if (btn==4) font.smooth ^=1; goto _DRAW_WINDOW_CONTENT; case evReDraw: - DefineAndDrawWindow(215,100,500,320,0x04,0xFFFFFF,""); - font_title.show(); + DefineAndDrawWindow(215,100,500,320,0x74,0xFFFFFF,#title); GetProcessInfo(#Form, SelfInfo); _DRAW_WINDOW_CONTENT: - DrawBar(5, SKIN.height, Form.cwidth, PANELH, 0xDADADA); - - font_option.italic = font_option.smooth = 0; - font_option.weight = 1; - font_option.prepare(30, SKIN.height+7, "Bold"); - CheckBox2(10, SKIN.height+8, 2, "", font.weight); - font_option.show(); - - font_option.weight = font_option.smooth = 0; - font_option.italic = 1; - font_option.prepare(90, SKIN.height+7, "Italic"); - CheckBox2(70, SKIN.height+8, 3, "", font.italic); - font_option.show(); - - font_option.weight = font_option.italic = 0; - font_option.smooth = 1; - font_option.prepare(160, SKIN.height+7, "Smooth"); - CheckBox2(140, SKIN.height+8, 4, "Smooth", font.smooth); - font_option.show(); - - IF(font.buffer)font.buffer_size = 0; - + DrawBar(0, 0, Form.cwidth, PANELH, 0xCCCccc); + CheckBox2(10, 8, 2, "Bold", font.bold); + CheckBox2(70, 8, 3, "Italic", font.italic); + CheckBox2(140, 8, 4, "Smooth", font.smooth); + font.buffer_size = free(font.buffer); if (!font.data) { - DrawBar(5, SKIN.height+PANELH, Form.cwidth, Form.cheight - PANELH, 0xFFFfff); - WriteText(15, 50, 0x82, 0xFF00FF, "Font is not loaded."); + DrawBar(0, PANELH, Form.cwidth, Form.cheight - PANELH, 0xFFFfff); + WriteText(10, 50, 0x82, 0xFF00FF, "Font is not loaded."); } else for (i=10, y=5; i<22; i++, y+=font.height;) //not flexible, need to calculate font count and max line length { font.size.text = i; sprintf(#line,"Размер шрифта/size font %d пикселей.",i); - font.prepare_buf(15,SKIN.height+y,Form.cwidth,Form.cheight-PANELH, #line); + font.prepare_buf(10,y,Form.cwidth,Form.cheight-PANELH, #line); } if (font.smooth) SmoothFont(font.buffer, font.size.width, font.size.height); + font.left = 0; + font.top = PANELH; font.show(); } } diff --git a/programs/cmm/lib/font.h b/programs/cmm/lib/font.h index c59f8e5e58..772c4f3102 100644 --- a/programs/cmm/lib/font.h +++ b/programs/cmm/lib/font.h @@ -9,6 +9,8 @@ #include "../lib/obj/fs.h" #endif +#define DEFAULT_FONT "/sys/fonts/Tahoma.kf" + :struct __OFFSET_FONT { signed x,y; @@ -24,11 +26,10 @@ :struct FONT { __SIZE size; - byte r,g,b,weight,italic, smooth; - byte width,height; - byte use_smooth; - int left,top; - byte encoding; + int left,top,width,height; + byte bold,italic,smooth; + byte no_bg_copy; + dword bg_color; dword color; dword file_size; dword buffer; @@ -46,26 +47,13 @@ dword getsize(dword text1); byte changeSIZE(); void PixelRGB(dword x,y); - //dword GetPixel(word x,y); - byte no_bg_copy; - dword bg_color; }; FONT font = 0; -/* -:dword FONT::GetPixel(word x,y) -{ - dword tmp = y*size.width*3; - tmp += x*3 + buffer; - r = DSBYTE[tmp]; tmp++; - g = DSBYTE[tmp]; tmp++; - b = DSBYTE[tmp]; -}*/ + :void FONT::PixelRGB(dword x,y) { dword offs = y*size.width+x*3 + buffer; - DSBYTE[offs] = r; offs++; - DSBYTE[offs] = g; offs++; - DSBYTE[offs] = b; + DSDWORD[offs] = DSDWORD[offs] & 0xFF000000 | color; } :byte FONT::changeSIZE() { @@ -119,43 +107,38 @@ FONT font = 0; } :byte FONT::symbol_size(byte s) { - dword xi,yi; - dword tmp,_; - dword iii; - byte rw=0; + dword xi,yi; + dword tmp,_; + dword iii; + byte rw=0; byte X; size.TMP_WEIGHT = math.ceil(size.text/17); - IF(s==32) + IF(s==32) { size.width += width/4; - IF(weight) size.width+=size.TMP_WEIGHT; + IF(bold) size.width+=size.TMP_WEIGHT; return; } IF(s==9) { size.width += width; - IF(weight) size.width+=size.TMP_WEIGHT; + IF(bold) size.width+=size.TMP_WEIGHT; return; } - IF(!encoding){ - IF(s>=128)&&(s<=175)s+=64; - ELSE IF(s>=224)&&(s<=239)s+=16; - ELSE IF(s==241)s=184; //yo - ELSE IF(s==240)s=168; //YO - } - iii = 0; - tmp = 4*block*s + data; - for(yi=0; yi>= 1; + s = AnsiToCp866(s); + iii = 0; + tmp = 4*block*s + data; + for(yi=0; yi>= 1; ELSE { - tmp += 4; - _ = DSDWORD[tmp]; - } - IF(_&1) + tmp += 4; + _ = DSDWORD[tmp]; + } + IF(_&1) { IF(xi>rw)rw=xi; IF(size.heightxi)X = xi; } - iii++; - } - } + iii++; + } + } size.width += rw; - IF(weight) size.width+=size.TMP_WEIGHT; + IF(bold) size.width+=size.TMP_WEIGHT; IF(s=='_') size.width--; IF(size.offset.x<0)size.offset.x = X; } +:byte FONT::symbol(signed x,y;byte s) +{ + dword xi,yi; + dword iii; + float ital = -size.w_italic; + dword ___x; + byte rw=0; + IF(s==32)return width/4; + IF(s==9)return width; + s = AnsiToCp866(s); + iii = 0; + EBX = block*s << 2 + data; + for(yi=0; yirw)rw=xi; + ___x = x+xi; + IF(italic)___x+=math.ceil(ital); + PixelRGB(___x,EDI); + IF(bold) PixelRGB(___x+1,EDI); + } + iii++; + } + if (italic) ital-=size.offset_i; + } + return rw; +} + +:void FONT::show(){ + _PutImage(left,top,size.width,size.height,buffer); +} + +byte AnsiToCp866(byte s) { + IF(s>=128)&&(s<=175)s+=64; + ELSE IF(s>=224)&&(s<=239)s+=16; + ELSE IF(s==241)s=184; //yo + ELSE IF(s==240)s=168; //YO + return s; +} + +inline fastcall dword b24(EBX) { return DSDWORD[EBX] << 8; } +:void SmoothFont(dword image, w, h) +{ + dword i,line_w,to; + line_w = w * 3; + to = w*h*3 + image - line_w - 3; + for(i = image; i < to; i+=3) { + IF(i-image%line_w +3 == line_w) continue; + IF(b24(i)==0x000000) && (b24(i+3)!=0x000000) && (b24(i+line_w)!=0x000000) && (b24(i+3+line_w)==0x000000) + { + ShadowImage(i+3, 1, 1, 2); + ShadowImage(i+line_w, 1, 1, 2); + } + ELSE IF(b24(i)!=0x000000) && (b24(i+3)==0x000000) && (b24(i+line_w)==0x000000) && (b24(i+3+line_w)!=0x000000) + { + ShadowImage(i, 1, 1, 2); + ShadowImage(i+3+line_w, 1, 1, 2); + } + } +} +:byte FONT::load(dword path) +{ + lib_init_fs(); + buffer_size = 0; + smooth = true; + IF(data)free(data); + IF(!fs.read(path)) { debug("Error while loading font: "); debugln(path); return false; } + begin = data = EAX; + EBX = begin + ECX; + height = DSBYTE[EBX-1]; + width = DSBYTE[EBX-2]; + block = math.ceil(height*width/32); + return true; +} + :dword FONT::prepare(int x,y;dword text1) { signed len=0; proc_info Form_SELF_FONTS; dword c; - c = color; - left = x; IF(!text1)return false; IF(size.text)IF(!changeSIZE())return false; - AX = c; r = AL; g = AH; c>>=16; AX = c; b = AL; + left = x; getsize(text1); y -= size.offset.y; top = y; @@ -199,13 +265,8 @@ FONT font = 0; IF (no_bg_copy) { EBX = bg_color; - EDI = buffer; - EAX = buffer_size+EDI; - WHILE (EDI=128)&&(s<=175)s+=64; - ELSE IF(s>=224)&&(s<=239)s+=16; - ELSE IF(s==241)s=184; //yo - ELSE IF(s==240)s=168; //YO - } - iii = 0; - EBX = block*s; - $shl ebx,2 - $add ebx,data - yi = 0; - while(yirw)rw=xi; - ___x = x+xi; - IF(italic)___x+=math.ceil(ital); - PixelRGB(___x,EDI); - //_TMP_WEIGHT = 2; - //WHILE(_TMP_WEIGHT) - //{ - // _TMP_WEIGHT--; - IF(weight) PixelRGB(___x+1,EDI); - //} - } - iii++; - } - IF(italic) ital-=size.offset_i; - $inc yi - } - return rw; -} -:byte FONT::load(dword path) -{ - lib_init_fs(); - buffer_size = 0; - use_smooth = true; - IF(data)free(data); - IF(!fs.read(path)) { debug("Error while loading font: "); debugln(path); return false; } - begin = data = EAX; - EBX = begin + ECX; - $dec ebx - height = DSBYTE[EBX]; - $dec ebx - width = DSBYTE[EBX]; - block = math.ceil(height*width/32); - return true; -} :void FONT::prepare_buf(int x,y,w,h; dword text1) { dword c, new_buffer_size; - c = color; - //left = x; IF(!text1)return; IF(size.text)IF(!changeSIZE())return; - AX = c; r = AL; g = AH; c>>=16; AX = c; b = AL; getsize(text1); y -= size.offset.y; - //top = y; + size.width = w; size.height = h; new_buffer_size = w*h*3; - IF(buffer_size!=new_buffer_size) + IF(buffer_size != w*h*3) { buffer_size = new_buffer_size; free(buffer); buffer = malloc(buffer_size); EBX = bg_color; - EDI = buffer; EAX = buffer_size+buffer; - WHILE (EDI