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

📄 fontconvertdlg.cpp

📁 VIA CDMA 字体库转换工具原码 FontConvert是一个字库转换工具
💻 CPP
字号:
// FontConvertDlg.cpp : implementation file
//

#include "stdafx.h"
#include "FontConvert.h"
#include "FontConvertDlg.h"
#include "stdio.h"
#include "io.h"
#include "direct.h"
#include "stdlib.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About

class CAboutDlg : public CDialog
{
public:
	CAboutDlg();

// Dialog Data
	//{{AFX_DATA(CAboutDlg)
	enum { IDD = IDD_ABOUTBOX };
	//}}AFX_DATA

	// ClassWizard generated virtual function overrides
	//{{AFX_VIRTUAL(CAboutDlg)
	protected:
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
	//}}AFX_VIRTUAL

// Implementation
protected:
	//{{AFX_MSG(CAboutDlg)
	//}}AFX_MSG
	DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
	//{{AFX_DATA_INIT(CAboutDlg)
	//}}AFX_DATA_INIT
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CAboutDlg)
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
	//{{AFX_MSG_MAP(CAboutDlg)
		// No message handlers
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CFontConvertDlg dialog

CFontConvertDlg::CFontConvertDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CFontConvertDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CFontConvertDlg)
	m_StartIndex = 0;
	m_EndIndex = 0;
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CFontConvertDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CFontConvertDlg)
	DDX_Text(pDX, IDC_EDIT_START_INDEX, m_StartIndex);
	DDX_Text(pDX, IDC_EDIT_END_INDEX, m_EndIndex);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CFontConvertDlg, CDialog)
	//{{AFX_MSG_MAP(CFontConvertDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(ID_BUTTON_BROWSE_BMF, OnButtonBrowseBmf)
	ON_BN_CLICKED(ID_BUTTON_BROWSE_TXT, OnButtonBrowseTxt)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CFontConvertDlg message handlers

BOOL CFontConvertDlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	// Add "About..." menu item to system menu.

	// IDM_ABOUTBOX must be in the system command range.
	ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
	ASSERT(IDM_ABOUTBOX < 0xF000);

	CMenu* pSysMenu = GetSystemMenu(FALSE);
	if (pSysMenu != NULL)
	{
		CString strAboutMenu;
		strAboutMenu.LoadString(IDS_ABOUTBOX);
		if (!strAboutMenu.IsEmpty())
		{
			pSysMenu->AppendMenu(MF_SEPARATOR);
			pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
		}
	}

	// Set the icon for this dialog.  The framework does this automatically
	//  when the application's main window is not a dialog
	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon
	
	// TODO: Add extra initialization here
	
	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CFontConvertDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
	if ((nID & 0xFFF0) == IDM_ABOUTBOX)
	{
		CAboutDlg dlgAbout;
		dlgAbout.DoModal();
	}
	else
	{
		CDialog::OnSysCommand(nID, lParam);
	}
}

// If you add a minimize button to your dialog, you will need the code below
//  to draw the icon.  For MFC applications using the document/view model,
//  this is automatically done for you by the framework.

void CFontConvertDlg::OnPaint() 
{
	if (IsIconic())
	{
		CPaintDC dc(this); // device context for painting

		SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);

		// Center icon in client rectangle
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		// Draw the icon
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialog::OnPaint();
	}
}

// The system calls this to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CFontConvertDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

void CFontConvertDlg::OnButtonBrowseBmf() 
{
	// TODO: Add your control notification handler code here
	CFileDialog dlg(TRUE, NULL, NULL, OFN_HIDEREADONLY, "Bmf Files (*.bmf)|*.bmf|| ", NULL);
	
	if (dlg.DoModal() == IDOK)
	{
		m_BmfFileName = dlg.GetPathName();
		CWnd* pwnd = GetDlgItem(IDC_EDIT_BMFFILE);
		pwnd->SetWindowText(m_BmfFileName);
		UpdateData(FALSE);
	}
}

void CFontConvertDlg::OnButtonBrowseTxt() 
{
	// TODO: Add your control notification handler code here
	CFileDialog dlg(TRUE, NULL, NULL, OFN_HIDEREADONLY, "Txt Files (*.txt)|*.txt|| ", NULL);
	
	if (dlg.DoModal() == IDOK)
	{
		m_TxtFileName = dlg.GetPathName();
		CWnd* pwnd = GetDlgItem(IDC_EDIT_TXTFILE);
		pwnd->SetWindowText(m_TxtFileName);
		UpdateData(FALSE);
	}
}

void CFontConvertDlg::OnOK() 
{
	// TODO: Add extra validation here
	long int BmfFileLength, TxtFileLength;

	UpdateData(TRUE);
	CWnd* pwnd;
	if (m_BmfFileName.IsEmpty())
	{
		AfxMessageBox("Please select bmf file!", MB_OK | MB_ICONEXCLAMATION);	
		pwnd = GetDlgItem(IDC_EDIT_BMFFILE);
		pwnd->SetFocus();
		return;
	}
	if (m_TxtFileName.IsEmpty())
	{
		AfxMessageBox("Please select index file!", MB_OK | MB_ICONEXCLAMATION);	
		pwnd = GetDlgItem(IDC_EDIT_TXTFILE);
		pwnd->SetFocus();		
		return;
	}
	if (m_StartIndex == m_EndIndex)
	{
		AfxMessageBox("Start index can't equal to end index!", MB_OK | MB_ICONEXCLAMATION);	
		return;
	}

	FILE* pBmfFile = fopen(m_BmfFileName, "r");
	if (pBmfFile == NULL)
	{
		AfxMessageBox("Bmf file open error!", MB_OK);
		return;
	}
	BmfFileLength = _filelength(pBmfFile->_file);
	byte* pBmfFileData = new byte[BmfFileLength];
	fread(pBmfFileData, BmfFileLength, sizeof(byte), pBmfFile);
	rewind(pBmfFile);
//	fclose(pBmfFile);
	

	FILE* pTxtFile = fopen(m_TxtFileName, "r");
	if (pTxtFile == NULL)
	{
		AfxMessageBox("Txt file open error!", MB_OK);
		return;
	}
	TxtFileLength = _filelength(pTxtFile->_file);
	byte* pTxtFileData = new byte[TxtFileLength];
	fread(pTxtFileData, TxtFileLength, sizeof(byte), pTxtFile);
	fclose(pTxtFile);

	int driver;
	char path[MAX_PATH];
	char ppath[MAX_PATH];
	
	//在工作目录下生成VTUI font目录
	driver = _getdrive();
	_getdcwd(driver, path, MAX_PATH);
	_searchenv("VTUI font", path, ppath);
	if (strlen(ppath) == 0)
	{
		strcpy(ppath, strcat(path, "\\VTUI font"));		
		if (_mkdir(ppath) == -1)			//建立文件存放目录出错
		{
			AfxMessageBox("Create directory error!", MB_OK);
			return;
		}
	}
	CString VTUI2ChineseCharBmfFile;
	VTUI2ChineseCharBmfFile.Format("%s%s", path, "\\VTUIfont.bmf");
	CString VTUI2ChineseCharTxtFile;
	VTUI2ChineseCharTxtFile.Format("%s%s", path, "\\VTUIfont.txt");
	
	FILE* pConvertedBmfFile = fopen(VTUI2ChineseCharBmfFile, "a + t");
	if (pConvertedBmfFile == NULL)
	{
		AfxMessageBox("VTUIfont.bmf file open error!", MB_OK);
		return;
	}
	FILE* pConvertedTxtFile = fopen(VTUI2ChineseCharTxtFile, "a + t");
	if (pConvertedTxtFile == NULL)
	{
		AfxMessageBox("VTUIfont.txt file open error!", MB_OK);
		return;
	}

	long int BeginIndex = 0, EndIndex = 0, Offset = 0;
	byte OneCharBmp[32];
	byte ConvertBmp[28];
	byte Matrix[256];
	byte temp[4];
	char tempbyte[2];
	DWORD index = 0, oldindex = 0;
	int i, j;
	int CharCount = BmfFileLength / 32;
	for(i = 0; i < CharCount; i++)
	{
		for(; *pTxtFileData != NULL; pTxtFileData++)
		{
			if (*pTxtFileData == 0x0a)
			{
				memcpy(temp, pTxtFileData + 1, 4);
				pTxtFileData = pTxtFileData + 4;
				break;
			}
		}

		oldindex = index;
		index = 0;
		index = index | (ASCII2Int(temp[0]) << 12) | (ASCII2Int(temp[1]) << 8) | (ASCII2Int(temp[2]) << 4) | ASCII2Int(temp[3]);
		memset(temp, 0, 4);

		if (index < m_StartIndex || index > m_EndIndex)
		{
			continue;
		}

		if (BeginIndex == 0)
		{
			BeginIndex = index;
		}
		if (EndIndex == 0)
		{
			EndIndex = index;
		}
		if (index - oldindex > 1)				//the index is not continuous
		{
			EndIndex = oldindex;
		}

		//write index to .txt
		if (index - oldindex != 1)				//the index is not continuous
		{	
			if (BeginIndex <= EndIndex)
			{
				fprintf(pConvertedTxtFile, "{0x%04x,0x%04x,0x%04x},\n", BeginIndex, EndIndex, Offset);
			}
			if (BeginIndex == EndIndex)
			{
				Offset++;
			}
			else
			{
				Offset = Offset + EndIndex - BeginIndex + 1;
				if (Offset < 0)
				{
					Offset = 0;
				}
			}
			BeginIndex = index;				
		}

		//write matrix data to .bmf
		fseek(pBmfFile, i * 32, 0);
		fread(OneCharBmp, 1, 32, pBmfFile);
//			memcpy(OneCharBmp, pBmfFileData + i * 32, 32);
		Bmp2Bin(OneCharBmp, Matrix);
		Bin2Bmp(Matrix, ConvertBmp);
		for(j = 0; j < sizeof(ConvertBmp); j++)
		{
			fprintf(pConvertedBmfFile, "0x%02x", ConvertBmp[j]);
			fputc(',', pConvertedBmfFile);
		}
		fputc('\n', pConvertedBmfFile);
	}

	//写最后一行索引表
	EndIndex = index;
	fprintf(pConvertedTxtFile, "{0x%04x,0x%04x,0x%04x},\n", BeginIndex, EndIndex, Offset);
	
	fclose(pBmfFile);
	fclose(pConvertedBmfFile);
	fclose(pConvertedTxtFile);
	delete pBmfFileData;
//	delete pTxtFileData;
	AfxMessageBox("Font convert succeed!", MB_OK | MB_ICONINFORMATION);	
	
	CDialog::OnOK();
}

int CFontConvertDlg::ASCII2Int(byte asic)
{
	int number;
	if (asic >= '0' && asic <= '9')
	{
		number =  asic - '0';
	}
	else
	{
		switch(asic)
		{
		case 'a':
			number = 10;
			break;
		case 'b':
			number = 11;
			break;
		case 'c':
			number = 12;
			break;
		case 'd':
			number = 13;
			break;
		case 'e':
			number = 14;
			break;
		case 'f':
			number = 15;
			break;
		default:
			break;
		}
	}
	return number;
}

void CFontConvertDlg::Bmp2Bin(byte resource[], byte destine[])
{
	memset(destine, 0, 256);
	int i;
	for(i = 0; i < 256; i++)
	{
		if (ReverseByte(resource[i/8]) & (0x01 << (i % 8)))
		{
			destine[i] = 1;
		}
	}
}

byte CFontConvertDlg::ReverseByte(byte a)
{
	byte b = 0;
	int i;
	for(i = 0; i < 8; i++)
	{
		if (a & (0x01 << i))
		{
			b = b | 0x01 << (7 - i);
		}
	}
	return b;
}

void CFontConvertDlg::Bin2Bmp(byte resource[], byte destine[])
{
	int x, y, i, byteindex, bitindex;
	memset(destine, 0, 28);
	for(i = 0; i < 256; i++)
	{
		if (resource[i])
		{
			x = i % 16;
			y = i / 16;
			if (x >= 14 || y >= 14)			//cut the rect of 16*16 into 14*14
			{
				continue;
			}
			if (y <= 7)						//the upper line
			{
				byteindex = x;
				bitindex = y;
			}
			else
			{
				byteindex = x + 14;			//the down line
				bitindex = y - 8;
			}
			destine[byteindex] = destine[byteindex] | (0x01 << bitindex);
		}
	}
}

⌨️ 快捷键说明

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