📄 fontgen.c
字号:
if (index > elecount - 1) index = elecount - 1;
break;
}
SetScrollPos (hCtrl, SB_CTL, index, TRUE);
// reset the font selection, save checkbox
hCtrl = GetDlgItem(hwnd,IDC_FONT_COMBO);
SendMessage (hCtrl, CB_SETCURSEL, 0, 0);
CheckDlgButton (hwnd, IDC_SAV_CHECK, BST_UNCHECKED);
InvalidateRect (hwnd, &rc, TRUE) ;
InvalidateRect (hwnd, &rc2, TRUE);
return 0 ;
case WM_PAINT:
hdc = BeginPaint (hwnd, &ps);
hdcMem = CreateCompatibleDC (hdc);
hdcMemRaw = CreateCompatibleDC (hdc);
hdcMemShadow = CreateCompatibleDC (hdc);
// draw font
hBitmap = CreateCompatibleBitmap (hdc, (scalex>8)?16:8, 16);
SelectObject (hdcMem, hBitmap);
hBitmapShadow = MakeShadow(aTile[index].hBitmap);
SelectObject (hdcMemShadow, hBitmapShadow);
SelectObject (hdcMem, hbrush);
StretchBlt (hdcMem, 0, 0, (scalex>8)?16:8, 16, hdcMemShadow, 0, 0, (scalex>8)?16:8, 16, 0x00F3022A); // PSno
hBitmapRaw = aTile[index].hBitmap;
SelectObject (hdcMemRaw, hBitmapRaw);
StretchBlt (hdcMem, 0, 0, (scalex>8)?16:8, 16, hdcMemRaw, 0, 0, (scalex>8)?16:8, 16, 0x00220326); // DSna
StretchBlt (hdc, rc.left, rc.top, rc.right - rc.left, rc.bottom - rc.top,
hdcMem, 0, 0, (scalex>8)?16:8, 16, SRCCOPY);
StretchBlt (hdc, rc2.left, rc2.top, (scalex>8)?16:8, 16,
hdcMem, 0, 0, (scalex>8)?16:8, 16, SRCCOPY);
StretchBlt (hdc, rc2.left + 16, rc2.top, ((scalex>8)?16:8) * 2, 16 * 2,
hdcMem, 0, 0, (scalex>8)?16:8, 16, SRCCOPY);
// plot grids
if (aTile[index].hBitmap)
{
WCHAR wbuf [10];
hobj = SelectObject (hdc, CreatePen (PS_SOLID, 0, RGB (0xd4, 0xd5, 0xd8)));
for (i = 0; i <= 16; i++) // horz line
{
MoveToEx (hdc, rc.left, rc.top + i * (rc.bottom - rc.top) / 16, NULL);
LineTo(hdc, rc.right, rc.top + i * (rc.bottom - rc.top) / 16);
if (i==16) break;
swprintf (wbuf, L"%02i",i + 1);
TextOut (hdc, rc.right + 2, rc.top + i * (rc.bottom - rc.top) / 16, wbuf, 2);
}
for (i = 0; i <= ((scalex>8)?16:8); i++) //vert line
{
MoveToEx (hdc, rc.left + i * (rc.right - rc.left) / ((scalex>8)?16:8), rc.top, NULL);
LineTo(hdc, rc.left + i * (rc.right - rc.left) / ((scalex>8)?16:8), rc.bottom);
if (i==((scalex>8)?16:8)) break;
swprintf (wbuf, L"%02i",i + 1);
TextOut (hdc, rc.left + i * (rc.right - rc.left) / ((scalex>8)?16:8), rc.bottom + 2, wbuf, 2);
}
DeleteObject (SelectObject (hdc, hobj));
}
DeleteObject (hBitmapShadow);
DeleteObject (hBitmap);
DeleteDC (hdcMemShadow);
DeleteDC (hdcMemRaw);
DeleteDC (hdcMem);
//display current char
SetWindowText (GetDlgItem (hwnd,IDC_UCODE_TEXT), &aTile[index].unicode);
//debug 4609
WCHAR wbuf2 [10];
ccc++;
swprintf (wbuf2, L"%i %i",ccc,h);
SetWindowText (GetDlgItem (hwnd,IDC_DEBUG_TEXT), wbuf2);
EndPaint (hwnd, &ps);
return 0;
case WM_DESTROY:
CleanTileArray (aTile);
DeleteObject (hbrush);
PostQuitMessage (0);
return 0 ;
}
return DefWindowProc (hwnd, message, wParam, lParam) ;
}
void Txt2TileArray (PTSTR szText, Tile aTile[])
{
UINT i;
for (i = 0; i < elecount; i++)
{
if (szText[i] == 0x00)
break;
aTile[i].unicode = szText[i];
aTile[i].hBitmap = Char2Bmp(szText + i);
}
}
void Txt2TileArrayCode (PTSTR szText, Tile aTile[])
{
UINT i;
for (i = 0; i < elecount; i++)
{
if (szText[i] == 0x00)
break;
//fixme szText[i]
aTile[i].unicode = szText[i];
}
}
void TileArrayCode2Txt (Tile aTile[], PTSTR szText)
{
UINT i;
szText[0] = 0xFEFF; //Unicode header
for (i = 0; i < elecount; i++)
{
if (szText[i] == 0x00)
break;
szText[i + 1] = aTile[i].unicode;
}
szText[i + 1] = 0x0000;
}
HBITMAP Char2Bmp (PTSTR szText)
{
HBITMAP hBitmap, hBitmapScale;
HDC hdc, hdcMem, hdcMemScale;
HFONT hFont;
hdc = CreateIC (TEXT ("DISPLAY"), NULL, NULL, NULL);
hdcMem = CreateCompatibleDC (hdc);
hdcMemScale = CreateCompatibleDC (hdc);
hFont = CreateFontIndirect (&lf);
hFont = (HFONT) SelectObject (hdcMem, hFont);
hBitmap = CreateBitmap (lf.lfHeight, lf.lfHeight, 1, 1, NULL);
SelectObject (hdcMem, hBitmap);
if (scalex > 8)
hBitmapScale = CreateBitmap (16, 16, 1, 1, NULL);
else
hBitmapScale = CreateBitmap (8, 16, 1, 1, NULL);
SelectObject (hdcMemScale, hBitmapScale);
TextOut (hdcMem, 0, 0, szText, 2);
StretchBlt (hdcMemScale, 0, ptOffset.y, scalex, scaley, hdcMem, 0, 0, lf.lfHeight, lf.lfHeight, NOTSRCCOPY);
//hBitmapScale will be deleted after Char2Bmp() call
DeleteObject (hBitmap);
DeleteObject (SelectObject (hdcMem, hFont));
DeleteDC (hdcMemScale);
DeleteDC (hdcMem);
DeleteDC (hdc);
return hBitmapScale;
}
HBITMAP MakeShadow (HBITMAP hBitmapRaw)
{
HDC hdc, hdcMem, hdcMemRaw, hdcMemShadow;
HBITMAP hBitmap, hBitmapShadow;
hdc = CreateIC (TEXT ("DISPLAY"), NULL, NULL, NULL);
hdcMem = CreateCompatibleDC (hdc);
hdcMemRaw = CreateCompatibleDC (hdc);
hdcMemShadow = CreateCompatibleDC (hdc);
SelectObject (hdcMemRaw, hBitmapRaw);
hBitmapShadow = CreateBitmap ((scalex>8)?16:8, 16, 1, 1, 0);
SelectObject (hdcMemShadow, hBitmapShadow);
StretchBlt (hdcMemShadow, 1, 1, (scalex>8)?16:8, 16, hdcMemRaw, 0, 0, (scalex>8)?16:8, 16, SRCCOPY);
StretchBlt (hdcMemShadow, 1, 0, (scalex>8)?16:8, 16, hdcMemRaw, 0, 0, (scalex>8)?16:8, 16, SRCPAINT);
hBitmap = CreateBitmap ((scalex>8)?16:8, 16, 1, 1, 0);
SelectObject (hdcMem, hBitmap);
StretchBlt (hdcMem, 0, 0, (scalex>8)?16:8, 16, hdcMemRaw, 0, 0, (scalex>8)?16:8, 16, SRCCOPY);
StretchBlt (hdcMem, 0, 0, (scalex>8)?16:8, 16, hdcMemShadow, 0, 0, (scalex>8)?16:8, 16, SRCERASE);
DeleteObject (hBitmapShadow);
// DeleteObject (hBitmap); //fixme
DeleteDC (hdcMemShadow);
DeleteDC (hdcMemRaw);
DeleteDC (hdcMem);
DeleteDC (hdc);
return hBitmap;
}
void TileArray2Byte (Tile aTile[], PBYTE pBuf, int shadow)
{
UINT iChar, iBit, iBaseOffset;
static BYTE byBits[32]; //buffer to receive font bmp, 2bytes(16bits) per line * 16 lines
for (iChar = 0; iChar < elecount; iChar ++) //per char
{
if (NULL == GetBitmapBits (aTile[iChar].hBitmap, 32, byBits)) // 32 is the # of bytes to copy: same as byBits
break;
for (iBit = 0; iBit < 32; iBit ++) //per bytes
{
if (scalex > 8)
iBaseOffset = (32 * iChar + iBit) + (iChar / 8) * 0x100; // (bytes_per_char * iChar + iBit) is the total
else // count of bytes; (iChar / plane_width) * 0x100
iBaseOffset = (16 * iChar + iBit) + (iChar / 16) * 0x100; // is char wrap (align)
if ((iBit / 16) % 2 == 0) //first 16bytes, upper half
{
if (iBit % 2 == 0) //even bytes, left half, always process
pBuf[iBaseOffset] = byBits[iBit];
else if (scalex > 8) //odd bytes, right half, drop if <= 8
pBuf[iBaseOffset + 0x10 - 1] = byBits[iBit];
}
else //last 16bytes, lower half
{
if (iBit % 2 == 0)
pBuf[iBaseOffset + 0xf0] = byBits[iBit];
else if (scalex > 8)
pBuf[iBaseOffset + 0xf0 + 0x10 - 1]=byBits[iBit];
}
}
}
if (shadow == 1)
{
for (iChar = 0; iChar < elecount; iChar ++) //per char
{
if (NULL == GetBitmapBits (MakeShadow(aTile[iChar].hBitmap), 32, byBits)) // 32 is the # of bytes to copy: same as byBits
break;
for (iBit = 0; iBit < 32; iBit ++) //per bytes
{
if (scalex > 8)
iBaseOffset = (32 * iChar + iBit) + (iChar / 8) * 0x100; // (bytes_per_char * iChar + iBit) is the total
else // count of bytes; (iChar / plane_width) * 0x100
iBaseOffset = (16 * iChar + iBit) + (iChar / 16) * 0x100; // is char wrap (align)
if ((iBit / 16) % 2 == 0) //first 16bytes, upper half
{
if (iBit % 2 == 0) //even bytes, left half, always process
{
pBuf[iBaseOffset + 1] = byBits[iBit];
pBuf[iBaseOffset] |= byBits[iBit];
}
else if (scalex > 8) //odd bytes, right half, drop if <= 8
{
pBuf[iBaseOffset + 0x10 - 1 + 1] = byBits[iBit];
pBuf[iBaseOffset + 0x10 - 1] |= byBits[iBit];
}
}
else //last 16bytes, lower half
{
if (iBit % 2 == 0)
{
pBuf[iBaseOffset + 0xf0 + 1] = byBits[iBit];
pBuf[iBaseOffset + 0xf0] |= byBits[iBit];
}
else if (scalex > 8)
{
pBuf[iBaseOffset + 0xf0 + 0x10 - 1 + 1] = byBits[iBit];
pBuf[iBaseOffset + 0xf0 + 0x10 - 1] |= byBits[iBit];
}
}
}
}
}
}
void Byte2TileArray (PBYTE pBuf, Tile aTile[])
{
UINT iChar, iBit, iBaseOffset;
HBITMAP hBitmap;
static BYTE byBits[32]; //MAX 16px
for (iChar = 0; iChar < elecount; iChar ++) //per char
{
for (iBit = 0; iBit < 32; iBit ++) //per bit
{
if (scalex > 8)
iBaseOffset = (32 * iChar + iBit) + (iChar / 8) * 0x100; // (bytes_per_char * iChar + iBit) is the total
else // count of bytes; (iChar / plane_width) * 0x100
iBaseOffset = (16 * iChar + iBit) + (iChar / 16) * 0x100; // is char wrap (align)
if ((iBit / 16) % 2 == 0) //first 16b, upper part
{
if (iBit % 2 == 0) //even bit, left part
byBits[iBit] = pBuf[iBaseOffset];
else if (scalex > 8) //odd bit
byBits[iBit] = pBuf[iBaseOffset + 0x10 - 1];
}
else //last 16b, lower part
{
if (iBit % 2 == 0)
byBits[iBit] = pBuf[iBaseOffset + 0xf0];
else if (scalex > 8)
byBits[iBit] = pBuf[iBaseOffset + 0xf0 + 0x10 - 1];
}
}
hBitmap = CreateBitmap ((scalex>8)?16:8, 16, 1, 1, NULL) ;
aTile[iChar].unicode = '0';
SetBitmapBits (hBitmap, 32, byBits);
aTile[iChar].hBitmap = hBitmap;
}
}
BOOL ReadTextFile (PTSTR szFileName, PTSTR szText)
{
BYTE bySwap;
HANDLE hFile;
DWORD dwBytesRead ;
int i, iFileLength, iUniTest;
PBYTE pBuffer, pText, pConv;
// Open the file.
if (INVALID_HANDLE_VALUE ==
(hFile = CreateFile (szFileName, GENERIC_READ, FILE_SHARE_READ,
NULL, OPEN_EXISTING, 0, NULL)))
return FALSE ;
// Get file size in bytes and allocate memory for read.
// Add an extra two bytes for zero termination.
iFileLength = GetFileSize (hFile, NULL) ;
pBuffer = (PBYTE) malloc (iFileLength + 2) ;
// Read file and put terminating zeros at end.
ReadFile (hFile, pBuffer, iFileLength, &dwBytesRead, NULL) ;
pBuffer[iFileLength] = '\0' ;
pBuffer[iFileLength + 1] = '\0' ;
CloseHandle (hFile) ;
// Test to see if the text is Unicode
iUniTest = IS_TEXT_UNICODE_SIGNATURE | IS_TEXT_UNICODE_REVERSE_SIGNATURE ;
if (IsTextUnicode (pBuffer, iFileLength, &iUniTest))
{
pText = pBuffer + 2 ;
iFileLength -= 2 ;
if (iUniTest & IS_TEXT_UNICODE_REVERSE_SIGNATURE)
{
for (i = 0 ; i < iFileLength / 2 ; i++)
{
bySwap = ((BYTE *) pText) [2 * i] ;
((BYTE *) pText) [2 * i] = ((BYTE *) pText) [2 * i + 1] ;
((BYTE *) pText) [2 * i + 1] = bySwap ;
}
}
lstrcpy( szText, (LPCTSTR) pText) ;
}
else // the file is not Unicode
{
pText = pBuffer ;
// Allocate memory for possibly converted string.
pConv = (PBYTE) malloc (iFileLength * 2 + 2) ;
MultiByteToWideChar (CP_ACP, 0, (LPCSTR) pText, -1, (PTSTR) pConv, iFileLength + 1) ;
lstrcpy( szText, (LPCTSTR) pConv) ;
free (pConv) ;
}
free (pBuffer);
return TRUE ;
}
int ReadXFile (PTSTR szFileName, LPVOID szText)
{
HANDLE hFile;
DWORD dwBytesRead ;
int iFileLength;
PBYTE pBuffer;
// Open the file.
if (INVALID_HANDLE_VALUE ==
(hFile = CreateFile (szFileName, GENERIC_READ, FILE_SHARE_READ,
NULL, OPEN_EXISTING, 0, NULL)))
return -1 ;
// Get file size in bytes and allocate memory for read.
// Add an extra two bytes for zero termination.
iFileLength = GetFileSize (hFile, NULL) ;
pBuffer = (PBYTE) malloc (iFileLength + 2) ;
// Read file and put terminating zeros at end.
ReadFile (hFile, pBuffer, iFileLength, &dwBytesRead, NULL) ;
pBuffer[iFileLength] = '\0' ;
pBuffer[iFileLength + 1] = '\0' ;
CloseHandle (hFile) ;
memcpy(szText, pBuffer, iFileLength) ;
free (pBuffer);
return iFileLength ;
}
BOOL WriteXFile (PTSTR szFileName, LPCVOID pBuffer, int len)
{
DWORD dwBytesWritten ;
HANDLE hFile ;
// Open the file, creating it if necessary
if (INVALID_HANDLE_VALUE ==
(hFile = CreateFile (szFileName, GENERIC_WRITE, 0,
NULL, CREATE_ALWAYS, 0, NULL)))
return FALSE ;
WriteFile (hFile, pBuffer, len, &dwBytesWritten, NULL) ;
CloseHandle (hFile) ;
return TRUE ;
}
void RemoveDupDBCS (PTSTR szText)
{
PBYTE pMByteText,pDBCSText;
int i, j, iMByteLen;
char key[4];
iMByteLen = WideCharToMultiByte (CP_ACP, 0, szText, -1, NULL, 0, NULL, NULL);
pMByteText = (PBYTE) malloc (iMByteLen + 2) ;
pDBCSText = (PBYTE) malloc (iMByteLen + 2) ;
ZeroMemory (pDBCSText, iMByteLen + 2);
WideCharToMultiByte (CP_ACP, 0, szText, -1, (LPSTR) pMByteText, iMByteLen, NULL, NULL) ;
// pick DBCS strings
for (i = 0, j = 0 ; i < iMByteLen ; i++)
{
if (IsDBCSLeadByte( ((BYTE *) pMByteText) [i]))
{
strncpy(key, (char *)(pMByteText+i), 2);
key [2] = '\0';
// remove dup str
if ( NULL == _mbsstr((unsigned char *) pDBCSText,(unsigned char *) key))
{
((BYTE *) pDBCSText) [j] = ((BYTE *) pMByteText) [i] ;
((BYTE *) pDBCSText) [j + 1] = ((BYTE *) pMByteText) [i + 1] ;
j += 2 ;
}
i ++ ;
}
}
pDBCSText[j] = '\0' ;
pDBCSText[j + 1] = '\0' ;
MultiByteToWideChar (CP_ACP, 0, (LPCSTR) pDBCSText, -1, (PTSTR) szText, 2 * j + 2) ;
free (pDBCSText) ;
free (pMByteText) ;
}
int CleanTileArray (Tile aTile[])
{
UINT i, j;
j = 0;
for (i = 0; i < elecount; i++)
{
if (aTile[i].hBitmap)
{
if (DeleteObject (aTile[i].hBitmap))
j ++;
}
}
return j;
}
int CALLBACK EnumFontFamExProc(ENUMLOGFONTEX *lpelfe, NEWTEXTMETRICEX *lpntme, DWORD FontType, LPARAM lParam)
{
static int i;
int j;
static bool skip;
for (skip=0, j=0; j<i ;j++)
{
if(!lstrcmp(szFontList[j], lpelfe->elfLogFont.lfFaceName))
{
skip = 1;
break;
}
}
if (
'@'!=lpelfe->elfLogFont.lfFaceName[0] &&
lstrcmp(TEXT("System"), lpelfe->elfLogFont.lfFaceName) &&
lstrcmp(TEXT("Fixedsys"), lpelfe->elfLogFont.lfFaceName) &&
lstrcmp(TEXT("\u65b0\u5b8b\u4f53"), lpelfe->elfLogFont.lfFaceName) && //NSimsun
lstrcmp(TEXT("Tahoma"), lpelfe->elfLogFont.lfFaceName) &&
lstrcmp(TEXT("Microsoft Sans Serif"), lpelfe->elfLogFont.lfFaceName) &&
lstrcmp(TEXT("Arial Unicode MS"), lpelfe->elfLogFont.lfFaceName) &&
lstrcmp(TEXT("Lucida Sans Unicode"), lpelfe->elfLogFont.lfFaceName) &&
lstrcmp(TEXT("Terminal"), lpelfe->elfLogFont.lfFaceName) && !skip
)
lstrcpy (szFontList[i++], lpelfe->elfLogFont.lfFaceName);
return 1;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -