• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>
            隨筆 - 16, 文章 - 0, 評論 - 55, 引用 - 0
            數(shù)據(jù)加載中……

            在windows下,當FLTK界面上包含中文的時候啟動速度很慢,以下為修正過程

            問題描述:
            在windows下,當FLTK界面包含中文的時候,打開程序的時候會花費好幾秒的時間才能完整顯示界面

            原因:
            查了代碼,最后發(fā)現(xiàn)原因在于繪制字符的時候通過GetTextExtentPoint32W這個函數(shù)獲取字符寬度,由于這個函數(shù)本身速度不夠快,所以FLTK使用緩存方式來保存寬度,問題在于緩存的方式不適合中文這種寬字符,當前的緩存方式是每當獲取一個字符寬度時,把這個字符左右共1024個相鄰字符的寬度提前獲取并保存,以后每次獲取字符寬度之前先搜索緩存,如果沒有再通過API實際獲取。

            這個做法對于英文沒有問題,因為GetTextExtentPoint32W處理英文的速度很快,而且一次獲取1024個相鄰字符基本就把程序可能會用到的字符全部囊括了,但是當界面出現(xiàn)中文的時候這種做法就出現(xiàn)問題了,中文的字符集是很大的,一次獲取相鄰個1024字符寬度并不能保證囊括了絕大多數(shù)的字符,所以每次界面顯示之前都會花很多時間獲取很多用不到的字符寬度,雖然顯示一次之后的速度很快,但是啟動程序的時候會出現(xiàn)卡頓

            所以我做了修正,每當需要獲取字符寬度的時候只保存當前字符的寬度,不獲取相鄰字符的寬度,這樣就避免了問題

            修正代碼:
            一共修改2個文件,當前修改的是FLTK3,如果要修改FLTK1.3.X,只要找到對應的代碼即可
            src/fltk3/font.h (fltk1.3.X對應的文件是src/fl_font.h)
            class Fl_Font_Descriptor {
            public:
              ...
            #  ifdef WIN32
              HFONT fid;
              int *width[64];
              unsigned char *widthcached[64]; // 0-not cache, 1-cached //這里增加定義
            ...
            };

            src/fltk3/win32_font.cxx(fltk1.3.X對應的文件是src/fl_font_win32.cxx)
            Fl_Font_Descriptor::Fl_Font_Descriptor(const char* name, fltk3::Fontsize fsize)
            {
               ...
                int i;
                for (i = 0; i < 64; i++) {
                    width[i] = NULL;
                    widthcached[i] = NULL; // 這里增加
                }
              ...
            }

            Fl_Font_Descriptor::~Fl_Font_Descriptor()
            {
              ...
                int i;
                for (i = 0; i < 64; i++) {
                    if ( width[i] != NULL ) free(width[i]);
                    if ( widthcached[i] != NULL ) free(widthcached[i]); // 這里增加
                }
            }
            double fltk3::GDIGraphicsDriver::width(unsigned int c) // 修改主體就是這個方法,具體代碼如下
            {
                Fl_Font_Descriptor *fontsize = font_descriptor();
                unsigned int r;
                SIZE s;
                // Special Case Handling of Unicode points over U+FFFF.
                // The logic (below) computes a lookup table for char widths
                // on-the-fly, but the table only covers codepoints up to
                // U+FFFF, which covers the basic multilingual plane, but
                // not any higher plane, or glyphs that require surrogate-pairs
                // to encode them in WinXX, which is UTF16.
                // This code assumes that these glyphs are rarely used and simply
                // measures them explicitly if they occur - This will be slow...
                if(c > 0x0000FFFF) { // UTF16 surrogate pair is needed
                    if (!fl_gc) { // We have no valid gc, so nothing to measure - bail out
                        return 0.0;
                    }
                    int cc; // cell count
                    unsigned short u16[4]; // Array for UTF16 representation of c
                    // Creates a UTF16 string from a UCS code point.
                    cc = fltk3::ucs_to_Utf16(c, u16, 4);
                    // Make sure the current font is selected before we make the measurement
                    SelectObject(fl_gc, fontsize->fid);
                    // measure the glyph width
                    GetTextExtentPoint32W(fl_gc, (WCHAR*)u16, cc, &s);
                    return (double)s.cx;
                }
                // else - this falls through to the lookup-table for glyph widths
                // in the basic multilingual plane
                r = (c & 0xFC00) >> 10;
                unsigned short ii;
                HDC gc;
                HWND hWnd;
                if (!fontsize->width[r]) {
                    fontsize->width[r] = (int*) malloc(sizeof(int) * 0x0400);
                    fontsize->widthcached[r] = (unsigned char *)malloc(sizeof(unsigned char) * 0x0400);
                    for (int k=0; k<0x0400; k++) fontsize->widthcached[r][k] = 0;
                    ii = r * 0x400;
                    // The following code makes a best effort attempt to obtain a valid fl_gc.
                    // If no fl_gc is available at the time we call fltk3::width(), then we first
                    // try to obtain a gc from the first fltk window.
                    // If that is null then we attempt to obtain the gc from the current screen
                    // using (GetDC(NULL)).
                    // This should resolve STR #2086
                    gc = fl_gc;
                    hWnd = 0;
                    if (!gc) { // We have no valid gc, try and obtain one
                        // Use our first fltk window, or fallback to using the screen via GetDC(NULL)
                        hWnd = fltk3::first_window() ? fl_xid(fltk3::first_window()) : NULL;
                        gc = GetDC(hWnd);
                    }
                    if (!gc) fltk3::fatal("Invalid graphic context: fltk3::width() failed because no valid HDC was found!");
                    SelectObject(gc, fontsize->fid);
                    ii += c &0x03FF;
                    GetTextExtentPoint32W(gc, (WCHAR*)&ii, 1, &s);
                    fontsize->width[r][c&0x03FF] = s.cx;
                    fontsize->widthcached[r][c&0x03FF] = 1;
                    if (gc && gc!=fl_gc) ReleaseDC(hWnd, gc);
                    //printf("[%d,%X]\n", s.cx, c);
                } else {
                    if ( fontsize->widthcached[r][c&0x03FF] == 1 ) return (double) fontsize->width[r][c & 0x03FF];
                    ii = r * 0x400;
                    gc = fl_gc;
                    hWnd = 0;
                    if (!gc) { // We have no valid gc, try and obtain one
                        // Use our first fltk window, or fallback to using the screen via GetDC(NULL)
                        hWnd = fltk3::first_window() ? fl_xid(fltk3::first_window()) : NULL;
                        gc = GetDC(hWnd);
                    }
                    if (!gc) fltk3::fatal("Invalid graphic context: fltk3::width() failed because no valid HDC was found!");
                    SelectObject(gc, fontsize->fid);
                    ii += c &0x03FF;
                    GetTextExtentPoint32W(gc, (WCHAR*)&ii, 1, &s);
                    fontsize->width[r][c&0x03FF] = s.cx;
                    fontsize->widthcached[r][c&0x03FF] = 1;
                    if (gc && gc!=fl_gc) ReleaseDC(hWnd, gc);
                    //printf("[%d,%X]\n", s.cx, c);
                }
                return (double) fontsize->width[r][c & 0x03FF];
            }

            posted on 2014-04-29 17:28 cyantree 閱讀(2058) 評論(0)  編輯 收藏 引用

            久久99精品久久久久久久不卡 | 无遮挡粉嫩小泬久久久久久久| 久久久无码精品亚洲日韩蜜臀浪潮 | 久久影院午夜理论片无码| 综合久久国产九一剧情麻豆| 99re这里只有精品热久久 | 国产精品久久久久久久| 日韩久久无码免费毛片软件 | 综合久久精品色| 久久久精品一区二区三区| 久久久久久久精品妇女99| 久久99国产精品久久99| 久久精品国产99国产精品亚洲| 亚洲国产成人久久综合一| 人妻少妇久久中文字幕一区二区| 国产成人精品久久综合| 久久久av波多野一区二区| 久久亚洲精品国产精品婷婷| 精品久久人人妻人人做精品| AV狠狠色丁香婷婷综合久久| 久久久久亚洲av综合波多野结衣 | 蜜臀av性久久久久蜜臀aⅴ| 久久人妻少妇嫩草AV无码蜜桃| 狠狠色丁香久久综合五月| 人妻无码αv中文字幕久久| 午夜精品久久久久| 天堂无码久久综合东京热| 青青青国产成人久久111网站| 久久久久久毛片免费播放| 一本色道久久综合亚洲精品| 久久综合鬼色88久久精品综合自在自线噜噜 | 国产午夜福利精品久久| 国产精品久久久久…| 久久国产精品99国产精| 久久99国产综合精品| 久久w5ww成w人免费| 亚洲国产精品18久久久久久| 亚洲国产精品无码久久| 久久99国产综合精品女同| 狠色狠色狠狠色综合久久| 久久99国产精一区二区三区|