⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ctrlmisc.c

📁 基于minigui开发的一套图形控件系统
💻 C
📖 第 1 页 / 共 2 页
字号:
// Note:

//  Although there was a version by Zhao Jianghua, this version of

//  EDIT control is written by Wei Yongming from scratch.

//

// Create date: 1999/8/26

//

// Modify records:

//

//  Who             When        Where       For What                Status

//-----------------------------------------------------------------------------

//  高来红    2004/02/19  TCL		    Add MPL License         Finished

//

//

// TODO:

//    * Selection.

//    * Undo.

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

#include <unistd.h>



#include <minigui/common.h>

#include <minigui/minigui.h>

#include <minigui/gdi.h>

#include <minigui/window.h>

#include <minigui/control.h>

#include <freetype/freetype.h>     //add by hj 2005-04-26

#include <freetype/ftxkern.h>     //add by hj 2005-04-26

#include "ftype.h"





//#define  HJ_DEBUG(x...)   fprintf(stderr,x);

#define  HJ_DEBUG(x...)



#if 0



int my_get_char_width(LOGFONT* logfont, DEVFONT * devfont,const char* mchar, int len)

{

    int x0=0,y0=0;

    int bbox_x = 0, bbox_y = 0;

    int bbox_w, bbox_h, bold = 0;

    int old_x, old_y;

      

    if (devfont->font_ops->get_char_bbox) {

        (*devfont->font_ops->get_char_bbox) (logfont, devfont,

                            mchar, len, &bbox_x, &bbox_y, &bbox_w, &bbox_h);

    }

    else {

        bbox_w = (*devfont->font_ops->get_char_width) (logfont, devfont, 

                mchar, len);

    }



    if (logfont->style & FS_WEIGHT_BOLD 

        && !(devfont->style & FS_WEIGHT_BOLD)) {

        bold = 1;

    }

      

    bbox_w = (bbox_w + 7) >> 3; 

  

    if (devfont->font_ops->get_char_advance) {

        (*devfont->font_ops->get_char_advance) (logfont, devfont, &x0, &y0);

    }

    else {

        x0 = bbox_w + bold;

    }

    return x0;

}



int my_get_First_extent (LOGFONT* log_font, const char* text, int len, int * cx)

{

    DEVFONT* sbc_devfont = log_font->sbc_devfont;

    DEVFONT* mbc_devfont = log_font->mbc_devfont;

    int len_cur_char = 0,cbExtra;

    int left_bytes = len;

        

    gdi_start_new_line (log_font);

    cbExtra = GetTextCharacterExtra(HDC_SCREEN);



    if (mbc_devfont != NULL) {

            len_cur_char = (*mbc_devfont->charset_ops->len_first_char) (text, len);

            if (len_cur_char != 0) {

                *cx += my_get_char_width (log_font, mbc_devfont, 

                    text, len_cur_char);

                *cx += cbExtra;

                return len_cur_char;

            }

    }



    len_cur_char = (*sbc_devfont->charset_ops->len_first_char) (text, len);

    if (len_cur_char != 0) {

         *cx += my_get_char_width (log_font, sbc_devfont, 

                    text, len_cur_char);

         *cx += cbExtra;

    }

    return len_cur_char;

}   

    

void my_get_TextOut_extent (LOGFONT* log_font, const char* text, int len, SIZE* size)

{

    DEVFONT* sbc_devfont = log_font->sbc_devfont;

    DEVFONT* mbc_devfont = log_font->mbc_devfont;

    int len_cur_char,cbExtra;

    int left_bytes = len;

        

    gdi_start_new_line (log_font);

    cbExtra = GetTextCharacterExtra(HDC_SCREEN);



    /* FIXME: cy is not the height when rotate font */

    size->cy = log_font->size;

    size->cx = 0;

    while (left_bytes) {

        if (mbc_devfont != NULL) {

            len_cur_char = (*mbc_devfont->charset_ops->len_first_char) (text, left_bytes);

            if (len_cur_char != 0) {

                size->cx += my_get_char_width (log_font, mbc_devfont, 

                    text, len_cur_char);

                size->cx += cbExtra;

                left_bytes -= len_cur_char;

                text += len_cur_char;

                continue;

            }

        }



        len_cur_char = (*sbc_devfont->charset_ops->len_first_char) (text, left_bytes);

        if (len_cur_char != 0) {

            size->cx += my_get_char_width (log_font, sbc_devfont, 

                    text, len_cur_char);

            size->cx += cbExtra;

        }

        else

            break;



        left_bytes -= len_cur_char;

        text += len_cur_char;

    }

    

}

#endif



int GetFontCharWidth (PLOGFONT font)

{

    DEVFONT* sbc_devfont = font->sbc_devfont;

    return sbc_devfont->font_ops->get_ave_width (font, sbc_devfont);

}



int GetFontCCharWidth (PLOGFONT font)

{

    DEVFONT* mbc_devfont = font->mbc_devfont;



    if (mbc_devfont)

    	return mbc_devfont->font_ops->get_max_width (font, mbc_devfont);

    else

        return GetSysCharWidth () * 2;

        

}



int GetFontSingleCharWidth (PLOGFONT font,const char* mchar,int len)

{

    DEVFONT* mbc_devfont;

    DEVFONT* sbc_devfont;

    

#if 0

	int cx = 0;

	my_get_First_extent(font,mchar,len,&cx);

	return cx;

	

#else

  

    if(len < 2)

    {

    	//return my_get_char_width(font,font->sbc_devfont,mchar,len);

    	if(!font)

    		return GetSysCharWidth();

    		

    	sbc_devfont = font->sbc_devfont;

	if(sbc_devfont)

    		return sbc_devfont->font_ops->get_char_width (font, sbc_devfont,mchar,len);

    	else

    		return GetSysCharWidth();

    		

    }

    else

    {

    	//return my_get_char_width(font,font->mbc_devfont,mchar,len);

    	if(!font)

    		return GetSysCharWidth()*2;

    	mbc_devfont = font->mbc_devfont;

    	if (mbc_devfont)

    		return mbc_devfont->font_ops->get_max_width (font, mbc_devfont);

   	 else

        	return GetSysCharWidth () * 2;

        	

    }

#endif

}



int GetStrDispWidth(HWND hWnd,const char * mchar,int len)

{

#if 0

   SIZE fsize;

   PLOGFONT pfont = GetWindowFont(hWnd);

   my_get_TextOut_extent(pfont,mchar,len,&fsize);

   return fsize.cx;

#else



   

    int i;

    int nTextWidth = 0;

    int fontsize = GetSysCCharWidth();

    PLOGFONT pfont = GetWindowFont(hWnd);

    if(pfont)

    	fontsize = GetFontCCharWidth(pfont);



	HJ_DEBUG("\nlen=%d\n",len);      



    for (i = 0; i < len; i++) {

    	HJ_DEBUG("\nGetStrDispWidth:nTextWidth=%d\n",nTextWidth);

        if ((BYTE)mchar[i] > 0xA0) {

            i++;

            if (i < len) {

                if ((BYTE)mchar[i] > 0xA0) {

                    	nTextWidth += fontsize;

                }

                else

                    i--;

            }

            else {

                nTextWidth += GetFontSingleCharWidth(pfont,(char*)&mchar[i],1);

            }

        }

        else {

            nTextWidth += GetFontSingleCharWidth(pfont,(char*)&mchar[i],1);

        }

    }



	HJ_DEBUG("\nGetStrDispWidth:before return nTextWidth=%d\n",nTextWidth);

    return nTextWidth;

#endif

}





static int compute_kernval (TTFINSTANCEINFO* ttf_inst_info)

{

    int i = 0;

    int kernval;

    TTFGLYPHINFO* ttf_glyph_info = ttf_inst_info->ttf_glyph_info;

    int nPairs = ttf_glyph_info->directory.tables->t.kern0.nPairs;

    TT_Kern_0_Pair *pair = ttf_glyph_info->directory.tables->t.kern0.pairs;



    if (ttf_inst_info->last_glyph_code != -1) {

        while ((pair->left != ttf_inst_info->last_glyph_code)

            && (pair->right != ttf_inst_info->cur_glyph_code))

        {

            pair++;

            i++;

            if (i == nPairs)

                break;

        }



        if (i == nPairs)

            kernval = 0;

        else

            /* We round the value (hence the +32) */

            kernval = (pair->value + 32) & -64;

    } else

        kernval = 0;



    return kernval;

}



int  New_GetStrDispWidth(HWND hWnd,const char * mchar,int len)

{

	int i;

	unsigned short uni_char;

	TT_Glyph_Metrics metrics;

	TT_UShort   index;

	int nTextWidth, left_bytes, len_cur_char;

	TTFINSTANCEINFO* ttf_inst_info = NULL;

	TTFGLYPHINFO* ttf_glyph_info = NULL;

	TT_Pos vec_x, vec_y;

	DEVFONT* sbc_devfont = NULL;

        DEVFONT* mbc_devfont = NULL;

	//int sbc_height = 0;

        //int mbc_height = 0;

	int fontsize = GetSysCCharWidth();

	PLOGFONT pfont = GetWindowFont(hWnd);

	left_bytes =len;

	nTextWidth =0;

	HJ_DEBUG("\ntext len=%d,nTextWidth=%d\n",len,nTextWidth);

	

	if(pfont && strcasecmp(pfont->type,"ttf") ==0)

	{

		sbc_devfont =pfont->sbc_devfont;

		//sbc_height = (*sbc_devfont->font_ops->get_font_height) (pfont, sbc_devfont);

		mbc_devfont =pfont->mbc_devfont;

		//if (mbc_devfont)

        	//	mbc_height = (*mbc_devfont->font_ops->get_font_height) (pfont, mbc_devfont);

		while(left_bytes)

		{

			if (mbc_devfont != NULL) {

		            len_cur_char = (*mbc_devfont->charset_ops->len_first_char) (mchar, left_bytes);

		            if (len_cur_char != 0) {

		            	HJ_DEBUG("\nmbc_devfont previous nTextWidth=%d\n",nTextWidth);

		            	ttf_inst_info =TTF_INST_INFO_P (mbc_devfont);

		                ttf_glyph_info =ttf_inst_info->ttf_glyph_info;

		            	uni_char = (*mbc_devfont->charset_ops->conv_to_uc16) (mchar, len_cur_char);

		            	index = TT_Char_Index (ttf_glyph_info->char_map, uni_char);

		            	ttf_inst_info->cur_glyph_code = index;

		            	if(TT_Load_Glyph(ttf_inst_info->instance,ttf_glyph_info->glyph,index,

				   TTLOAD_DEFAULT) == TT_Err_Ok)

				   {

				   	TT_Get_Glyph_Metrics (ttf_glyph_info->glyph, &metrics);

				   	nTextWidth += metrics.advance / 64;

				   	HJ_DEBUG("\nmbc_devfont:TT_Load_Glyph ok.\n");

				   }

				   else

				   {

				  	nTextWidth += ttf_inst_info->ave_width;

				  	HJ_DEBUG("\nmbc_devfont:TT_Load_Glyph error use the ave_width.\n");

				   }

		                HJ_DEBUG("\nmbc_devfont later nTextWidth=%d\n",nTextWidth);

		                if ((pfont->style & FS_SETWIDTH_CONDENSED) && ttf_glyph_info->can_kern) 

		                {

		                	HJ_DEBUG("\nmbc_devfont can_kern\n");

			                if (ttf_inst_info->rotation) {

				            vec_x = compute_kernval (ttf_inst_info);

				            vec_y = 0;

				            TT_Transform_Vector (&vec_x, &vec_y, &ttf_inst_info->matrix);

				            nTextWidth += vec_x / 64;

				            //y -= vec_y / 64;

				        } else

				            nTextWidth += compute_kernval (ttf_inst_info) / 64;

			        }

		                //nTextWidth += pdc->cExtra;

		                left_bytes -= len_cur_char;

		                mchar += len_cur_char;

		                ttf_inst_info->last_glyph_code = index;

		                continue;

		            }

		        }

		

		        len_cur_char = (*sbc_devfont->charset_ops->len_first_char) (mchar, left_bytes);

		        if (len_cur_char != 0) {

		            HJ_DEBUG("\nsbc_devfont previous nTextWidth=%d,text=%c,len_cur_char=%d\n",nTextWidth,*mchar,len_cur_char);

		            ttf_inst_info =TTF_INST_INFO_P (sbc_devfont);

		            ttf_glyph_info =ttf_inst_info->ttf_glyph_info;

		            uni_char = (*sbc_devfont->charset_ops->conv_to_uc16) (mchar, len_cur_char);

		            index = TT_Char_Index (ttf_glyph_info->char_map, uni_char);

		            ttf_inst_info->cur_glyph_code = index;

					

		            if(TT_Load_Glyph(ttf_inst_info->instance,ttf_glyph_info->glyph,index,

				TTLOAD_DEFAULT) == TT_Err_Ok)

			    {

				TT_Get_Glyph_Metrics (ttf_glyph_info->glyph, &metrics);

				nTextWidth += metrics.advance / 64;

				HJ_DEBUG("\nsbc_devfont:TT_Load_Glyph ok.\n");

		            }

			    else

			    {

				  nTextWidth += ttf_inst_info->ave_width;

				  HJ_DEBUG("\nsbc_devfont:TT_Load_Glyph error use the ave_width.\n");

			    }

		            HJ_DEBUG("\nsbc_devfont later nTextWidth=%d\n",nTextWidth);

		                if ((pfont->style & FS_SETWIDTH_CONDENSED) && ttf_glyph_info->can_kern) 

		                {

		                	HJ_DEBUG("\nsbc_devfont can_kern\n");

			                if (ttf_inst_info->rotation) {

				            vec_x = compute_kernval (ttf_inst_info);

				            vec_y = 0;

				            TT_Transform_Vector (&vec_x, &vec_y, &ttf_inst_info->matrix);

				            nTextWidth += vec_x / 64;

				            //y -= vec_y / 64;

				        } else

				            nTextWidth += compute_kernval (ttf_inst_info) / 64;

			        }

			    ttf_inst_info->last_glyph_code = index;

		            //nTextWidth += pdc->cExtra;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -