/** * Copyright (c) 2003 mudGE Entertainment * * This software is provided 'as-is', without any express or implied warranty. * In no event will the authors be held liable for any damages arising * from the use of this software. * * Permission is granted to anyone to use this software for any purpose, * including commercial applications, and to alter it and redistribute * it freely, subject to the following restrictions: * * 1. The origin of this software must not be misrepresented; * you must not claim that you wrote the original software. * If you use this software in a product, an acknowledgment * in the product documentation would be appreciated but is not required. * * 2. Altered source versions must be plainly marked as such, and must * not be misrepresented as being the original software. * * 3. This notice may not be removed or altered from any source distribution. * */ // --> Include files <--------------------------------------------------------- #include "stdafx.h" #include "mudFont.h" #include "addchardlg.h" #include "mudFontDlg.h" // --> Static members <-------------------------------------------------------- #ifdef _DEBUG #define new DEBUG_NEW #undef THIS_FILE static char THIS_FILE[] = __FILE__; #endif BEGIN_MESSAGE_MAP(CMudFontDlg, CDialog) //{{AFX_MSG_MAP(CMudFontDlg) ON_WM_PAINT() ON_WM_VSCROLL() ON_WM_HSCROLL() ON_BN_CLICKED(IDC_H64, OnSizeChange) ON_BN_CLICKED(IDC_ITALIC, OnFontChange) ON_BN_CLICKED(IDC_RECT, OnRectChange) ON_EN_CHANGE(IDC_CBL_EDIT, OnCharChange) ON_CBN_SELCHANGE(IDC_CHAR, OnSelchangeChar) ON_COMMAND(ID_TOOLS_ADDCHAR, OnToolsAddchar) ON_COMMAND(ID_TOOLS_ADDWSW, OnToolsAddWarsowCharset) ON_COMMAND(ID_FILE_EXIT, OnFileExit) ON_COMMAND(ID_FILE_EXPORT, OnFileExport) ON_COMMAND(ID_FILE_NEW, OnFileNew) ON_COMMAND(ID_FILE_OPEN, OnFileOpen) ON_COMMAND(ID_FILE_SAVE, OnFileSave) ON_BN_CLICKED(IDC_H512, OnSizeChange) ON_BN_CLICKED(IDC_H256, OnSizeChange) ON_BN_CLICKED(IDC_H128, OnSizeChange) ON_BN_CLICKED(IDC_W64, OnSizeChange) ON_BN_CLICKED(IDC_W512, OnSizeChange) ON_BN_CLICKED(IDC_W256, OnSizeChange) ON_BN_CLICKED(IDC_W128, OnSizeChange) ON_CBN_SELCHANGE(IDC_QUALITY, OnFontChange) ON_EN_CHANGE(IDC_SIZE_EDIT, OnFontChange) ON_CBN_SELCHANGE(IDC_WEIGHT, OnFontChange) ON_CBN_SELCHANGE(IDC_FONT, OnFontChange) ON_EN_CHANGE(IDC_BB_EDIT, OnFontChange) ON_EN_CHANGE(IDC_BL_EDIT, OnFontChange) ON_EN_CHANGE(IDC_BR_EDIT, OnFontChange) ON_EN_CHANGE(IDC_BT_EDIT, OnFontChange) ON_EN_CHANGE(IDC_CBR_EDIT, OnCharChange) ON_CBN_SELCHANGE(IDC_AA, OnFontChange) //}}AFX_MSG_MAP END_MESSAGE_MAP() // --> Class implementation <-------------------------------------------------- CMudFontDlg::CMudFontDlg(CWnd* pParent /*=NULL*/): CDialog(CMudFontDlg::IDD, pParent), m_Running(false) { //{{AFX_DATA_INIT(CMudFontDlg) m_ZoomText = _T(""); m_Height = -1; m_Width = -1; m_Italic = FALSE; m_ShowRect = FALSE; //}}AFX_DATA_INIT } void CMudFontDlg::DoDataExchange(CDataExchange* pDX) { CDialog::DoDataExchange(pDX); //{{AFX_DATA_MAP(CMudFontDlg) DDX_Control(pDX, IDC_AA, m_AA); DDX_Control(pDX, IDC_CBR_SPIN, m_cbrSpin); DDX_Control(pDX, IDC_CBR_EDIT, m_cbrEdit); DDX_Control(pDX, IDC_CBL_SPIN, m_cblSpin); DDX_Control(pDX, IDC_CBL_EDIT, m_cblEdit); DDX_Control(pDX, IDC_CHAR, m_CharSel); DDX_Control(pDX, IDC_BT_SPIN, m_btSpin); DDX_Control(pDX, IDC_BT_EDIT, m_btEdit); DDX_Control(pDX, IDC_BR_SPIN, m_brSpin); DDX_Control(pDX, IDC_BR_EDIT, m_brEdit); DDX_Control(pDX, IDC_BL_SPIN, m_blSpin); DDX_Control(pDX, IDC_BL_EDIT, m_blEdit); DDX_Control(pDX, IDC_BB_SPIN, m_bbSpin); DDX_Control(pDX, IDC_BB_EDIT, m_bbEdit); DDX_Control(pDX, IDC_WEIGHT, m_Weight); DDX_Control(pDX, IDC_SIZE_SPIN, m_SizeSpin); DDX_Control(pDX, IDC_SIZE_EDIT, m_SizeEdit); DDX_Control(pDX, IDC_QUALITY, m_Quality); DDX_Control(pDX, IDC_FONT, m_FontName); DDX_Control(pDX, IDC_ZOOM, m_Zoom); DDX_Control(pDX, IDC_UP, m_Up); DDX_Control(pDX, IDC_RIGHT, m_Right); DDX_Control(pDX, IDC_VIEW, m_View); DDX_Text(pDX, IDC_ZOOM_TEXT, m_ZoomText); DDX_Radio(pDX, IDC_H64, m_Height); DDX_Radio(pDX, IDC_W64, m_Width); DDX_Check(pDX, IDC_ITALIC, m_Italic); DDX_Check(pDX, IDC_RECT, m_ShowRect); //}}AFX_DATA_MAP } BOOL CMudFontDlg::OnInitDialog() { int index; CDialog::OnInitDialog(); // set initial values. m_Zoom.SetRange(100,1000, TRUE); m_Zoom.SetPos(100); m_Zoom.SetTicFreq(50); m_View.ShowWindow(SW_HIDE); m_ZoomText.Format("%d%%", m_Zoom.GetPos()); m_Width = 2; m_Height = 2; m_Border = CRect(0,0,0,0); UpdateData(FALSE); // enumerate fonts. CDC* pDC = GetDC(); EnumFonts(pDC->GetSafeHdc(), NULL, (FONTENUMPROC)EnumFontsProc, (LPARAM)&m_FontName); m_FontName.SetCurSel(0); // add anti-aliasing. index = m_AA.AddString("(none)"); m_AA.SetItemData(index, 1); index = m_AA.AddString("2x2"); m_AA.SetItemData(index, 2); index = m_AA.AddString("3x3"); m_AA.SetItemData(index, 3); index = m_AA.AddString("4x4"); m_AA.SetItemData(index, 4); m_AA.SetCurSel(0); // add qualities. index = m_Quality.AddString("Antialiased"); m_Quality.SetItemData(index, ANTIALIASED_QUALITY); index = m_Quality.AddString("non-Antialiased"); m_Quality.SetItemData(index, NONANTIALIASED_QUALITY); m_Quality.SetCurSel(0); #ifdef CLEARTYPE_QUALITY index = m_Quality.AddString("Cleartype"); m_Quality.SetItemData(index, CLEARTYPE_QUALITY); #endif // add weights. index = m_Weight.AddString("Normal"); m_Weight.SetItemData(index, FW_NORMAL); index = m_Weight.AddString("Medium"); m_Weight.SetItemData(index, FW_SEMIBOLD); index = m_Weight.AddString("Bold"); m_Weight.SetItemData(index, FW_BOLD); m_Weight.SetCurSel(0); // setup size control. m_SizeSpin.SetBuddy(&m_SizeEdit); m_SizeSpin.SetRange(5, 100); m_SizeSpin.SetPos(11); // setup border controls. m_blSpin.SetBuddy(&m_blEdit); m_blSpin.SetRange(0, 16); m_blSpin.SetPos(0); m_brSpin.SetBuddy(&m_brEdit); m_brSpin.SetRange(0, 16); m_brSpin.SetPos(0); m_bbSpin.SetBuddy(&m_bbEdit); m_bbSpin.SetRange(0, 16); m_bbSpin.SetPos(0); m_btSpin.SetBuddy(&m_btEdit); m_btSpin.SetRange(0, 16); m_btSpin.SetPos(0); m_cblSpin.SetBuddy(&m_cblEdit); m_cblSpin.SetRange(0, 16); m_cblSpin.SetPos(0); m_cbrSpin.SetBuddy(&m_cbrEdit); m_cbrSpin.SetRange(0, 16); m_cbrSpin.SetPos(0); // setup initial font. SetupFont(); // create the dibs for the font. CPaintDC dc(this); m_MemDC.CreateCompatibleDC(&dc); m_MemDib.Create(512, 512, m_MemDC.GetSafeHdc()); m_Dib.Create(512, 512); // force one event. OnSizeChange(); m_Running = true; return TRUE; } void CMudFontDlg::OnPaint() { CPaintDC dc(this); CRect destRect; CRect sourceRect; m_View.GetWindowRect(&destRect); ::MapWindowPoints(HWND_DESKTOP, m_hWnd, reinterpret_cast(&destRect), 2); CRgn rgn; CPoint pnt1(destRect.left, destRect.top); CPoint pnt2(destRect.right, destRect.bottom); dc.LPtoDP(&pnt1, 1); dc.LPtoDP(&pnt2, 1); rgn.CreateRectRgn(pnt1.x, pnt1.y, pnt2.x, pnt2.y); dc.SelectClipRgn(&rgn); float zoom = (100.0f / m_Zoom.GetPos()); int width = (int)(destRect.Width() * zoom); int height = (int)(destRect.Height() * zoom); sourceRect.left = m_Right.GetScrollPos(); sourceRect.top = 512 - height - m_Up.GetScrollPos(); sourceRect.right = sourceRect.left + width; sourceRect.bottom = sourceRect.top + height; m_Dib.Render(dc.GetSafeHdc(), destRect, sourceRect); m_Right.SetScrollRange(0, 512 - width); m_Up.SetScrollRange(0, 512 - height); CPen pen(PS_DOT, 1, RGB(192, 192, 192)); dc.SelectObject(pen); if (m_ShowRect) { int len = m_Chars.GetSize(); for (int i=0; i(&rect), 2); rect.OffsetRect(-1, -1); dc.MoveTo(rect.left, rect.top); dc.LineTo(rect.right, rect.top); dc.LineTo(rect.right, rect.bottom); dc.LineTo(rect.left, rect.bottom); dc.LineTo(rect.left, rect.top); } } } void CMudFontDlg::OnVScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar) { CDialog::OnVScroll(nSBCode, nPos, pScrollBar); if (pScrollBar != NULL) { // Get the minimum and maximum scroll-bar positions. int minpos; int maxpos; pScrollBar->GetScrollRange(&minpos, &maxpos); maxpos = pScrollBar->GetScrollLimit(); // Get the current position of scroll box. int curpos = pScrollBar->GetScrollPos(); // Determine the new position of scroll box. switch (nSBCode) { case SB_TOP: curpos = minpos; break; case SB_BOTTOM: curpos = maxpos; break; case SB_ENDSCROLL: break; case SB_LINEUP: if (curpos > minpos) curpos--; break; case SB_LINEDOWN: if (curpos < maxpos) curpos++; break; case SB_PAGEUP: // Scroll one page up. if (curpos > minpos) { curpos = max(minpos, curpos - 10); } break; case SB_PAGEDOWN: // Scroll one page down. if (curpos < maxpos) { curpos = min(maxpos, curpos + 10); } break; case SB_THUMBPOSITION: curpos = nPos; break; case SB_THUMBTRACK: curpos = nPos; break; } // Set the new position of the thumb (scroll box). pScrollBar->SetScrollPos(curpos); InvalidateView(); } } void CMudFontDlg::OnHScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar) { CDialog::OnHScroll(nSBCode, nPos, pScrollBar); if (pScrollBar != NULL) { if (pScrollBar->GetSafeHwnd() == m_Zoom.GetSafeHwnd()) { int pos = m_Zoom.GetPos(); m_ZoomText.Format("%d%%", pos); UpdateData(FALSE); InvalidateView(); } else { // Get the minimum and maximum scroll-bar positions. int minpos; int maxpos; pScrollBar->GetScrollRange(&minpos, &maxpos); maxpos = pScrollBar->GetScrollLimit(); // Get the current position of scroll box. int curpos = pScrollBar->GetScrollPos(); // Determine the new position of scroll box. switch (nSBCode) { case SB_LEFT: // Scroll to far left. curpos = minpos; break; case SB_RIGHT: // Scroll to far right. curpos = maxpos; break; case SB_ENDSCROLL: // End scroll. break; case SB_LINELEFT: // Scroll left. if (curpos > minpos) curpos--; break; case SB_LINERIGHT: // Scroll right. if (curpos < maxpos) curpos++; break; case SB_PAGELEFT: // Scroll one page left. if (curpos > minpos) { curpos = max(minpos, curpos - 10); } break; case SB_PAGERIGHT: // Scroll one page right. if (curpos < maxpos) { curpos = min(maxpos, curpos + 10); } break; case SB_THUMBPOSITION: // Scroll to absolute position. nPos is the position curpos = nPos; // of the scroll box at the end of the drag // operation. break; case SB_THUMBTRACK: // Drag scroll box to specified position. nPos is // the curpos = nPos; // position that the scroll box has been dragged // to. break; } // Set the new position of the thumb (scroll box). pScrollBar->SetScrollPos(curpos); InvalidateView(); } } } void CMudFontDlg::SetupFont() { int index; int nHeight; int nWeight; int nQuality; CString Facename; UpdateData(TRUE); m_FontName.GetWindowText(Facename); nHeight = m_SizeSpin.GetPos(); if (nHeight > 100) nHeight = 100; if (nHeight < 5) nHeight = 5; index = m_Weight.GetCurSel(); nWeight = m_Weight.GetItemData(index); index = m_Quality.GetCurSel(); nQuality = m_Quality.GetItemData(index); index = m_AA.GetCurSel(); nHeight *= m_AA.GetItemData(index); // delete old font. if (m_DrawFont.GetSafeHandle() != NULL) { m_DrawFont.DeleteObject(); } // create new font. CPaintDC dc(this); int realHeight = -MulDiv(nHeight, GetDeviceCaps(dc.GetSafeHdc(), LOGPIXELSY), 72); m_DrawFont.CreateFont(realHeight, 0, 0, 0, nWeight, m_Italic, FALSE, 0, ANSI_CHARSET, OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS, nQuality, DEFAULT_PITCH | FF_SWISS, Facename); } void CMudFontDlg::UpdateTextureSize() { for (int y=0; y<512; y++) { for (int x=0; x<512; x++) { if (x < m_TextureWidth && y < m_TextureHeight) { m_Dib.SetPixel(x,y, RGB(0,0,0)); } else { if ((x % 10) == (y % 10) || ((512-x) % 10) == (y % 10)) { m_Dib.SetPixel(x,y, RGB(0,0,0)); } else { m_Dib.SetPixel(x,y, RGB(128, 128, 128)); } } } } } void CMudFontDlg::UpdateTexture() { // clear the texture. UpdateTextureSize(); UpdateSizes(); UpdatePositions(); DrawChars(); InvalidateView(); } void CMudFontDlg::UpdateSizes() { m_MemDC.SelectObject(m_DrawFont); int sel = m_CharSel.GetCurSel(); m_CharSel.ResetContent(); int index = m_AA.GetCurSel(); int sampling = m_AA.GetItemData(index); ABC abc; int len = m_Chars.GetSize(); for (int i=0; i height) { height = ch.m_Size.cy; } if (cx + ch.m_Size.cx > m_TextureWidth) { cx = 0; cy += height + 1; } ch.m_Pos.x = cx; ch.m_Pos.y = cy; cx += ch.m_Size.cx + 1; } } void CMudFontDlg::InvalidateView() { CRect rect; m_View.GetWindowRect(&rect); ::MapWindowPoints(HWND_DESKTOP, m_hWnd, reinterpret_cast(&rect), 2); InvalidateRect(&rect, FALSE); } void CMudFontDlg::OnSizeChange() { UpdateData(TRUE); switch (m_Width) { case 0: m_TextureWidth = 64; break; case 1: m_TextureWidth = 128; break; case 2: m_TextureWidth = 256; break; case 3: m_TextureWidth = 512; break; } switch (m_Height) { case 0: m_TextureHeight = 64; break; case 1: m_TextureHeight = 128; break; case 2: m_TextureHeight = 256; break; case 3: m_TextureHeight = 512; break; } UpdateTexture(); } void CMudFontDlg::DrawChars() { CBrush brush(RGB(0,0,0)); CPen pen(PS_SOLID, 1, RGB(0,0,0)); m_MemDC.SelectObject(brush); m_MemDC.SelectObject(pen); m_MemDC.SetTextColor(RGB(255,255,255)); m_MemDC.SetBkMode(TRANSPARENT); m_MemDC.SelectObject(m_DrawFont); //------------------- //public virtual COLORREF SetBkColor( COLORREF crColor ); //m_MemDC.SetBkMode(OPAQUE); //m_MemDC.SetBkColor(RGB(0,0,0)); //------------------- int index = m_AA.GetCurSel(); int sampling = m_AA.GetItemData(index); ABC abc; int len = m_Chars.GetSize(); for (int i=0; iAddString(lplf->lfFaceName); return 1; } void CMudFontDlg::OnFontChange() { m_Border.left = m_blSpin.GetPos(); m_Border.right = m_brSpin.GetPos(); m_Border.top = m_btSpin.GetPos(); m_Border.bottom = m_bbSpin.GetPos(); SetupFont(); if (m_Running) { UpdateTexture(); } } void CMudFontDlg::OnRectChange() { UpdateData(TRUE); InvalidateView(); } void CMudFontDlg::OnCharChange() { int index = m_CharSel.GetCurSel(); if (index >= 0) { m_Chars[index].m_bLeft = m_cblSpin.GetPos(); m_Chars[index].m_bRight = m_cbrSpin.GetPos(); if (m_Running) { UpdateTexture(); } } } void CMudFontDlg::OnSelchangeChar() { int index = m_CharSel.GetCurSel(); if (index >= 0) { m_cblSpin.SetPos(m_Chars[index].m_bLeft); m_cbrSpin.SetPos(m_Chars[index].m_bRight); } } void CMudFontDlg::OnToolsAddchar() { CAddCharDlg dlg; if (dlg.DoModal()) { CString chars = dlg.GetString(); int len = chars.GetLength(); for (int i=0; i 100) nHeight = 100; if (nHeight < 5) nHeight = 5; index = m_Weight.GetCurSel(); nWeight = m_Weight.GetItemData(index); index = m_Quality.GetCurSel(); nQuality = m_Quality.GetItemData(index); index = m_AA.GetCurSel(); nSampling = m_AA.GetItemData(index); FILE* file = fopen(szFile, "w+b"); if (file != NULL) { fwrite(&m_TextureWidth, sizeof(int), 1, file); fwrite(&m_TextureHeight, sizeof(int), 1, file); fwrite(&m_Border.left, sizeof(int), 1, file); fwrite(&m_Border.top, sizeof(int), 1, file); fwrite(&m_Border.right, sizeof(int), 1, file); fwrite(&m_Border.bottom, sizeof(int), 1, file); fwrite(&nHeight, sizeof(int), 1, file); fwrite(&nWeight, sizeof(int), 1, file); fwrite(&nQuality, sizeof(int), 1, file); fwrite(&nSampling, sizeof(int), 1, file); fwrite(&m_Italic, sizeof(BOOL), 1, file); int len = Facename.GetLength(); fwrite(&len, sizeof(int), 1, file); fwrite((LPCTSTR)Facename, len+1, 1, file); int numChars = m_Chars.GetSize(); fwrite(&numChars, sizeof(int), 1, file); for (int i=0; i=0; y--) { for (int x=0; x\n"); fprintf(file, "\n"); int index = m_AA.GetCurSel(); int sampling = m_AA.GetItemData(index); ABC abc; m_MemDC.SelectObject(m_DrawFont); int numChars = m_Chars.GetSize(); for (int i=0; i\n", ch.m_Char, ch.m_Pos.x, ch.m_Pos.y, ch.m_Size.cx, ch.m_Size.cy); m_MemDC.GetCharABCWidths(ch.m_Char, ch.m_Char, &abc); abc.abcA /= sampling; abc.abcB /= sampling; abc.abcC /= sampling; abc.abcA -= (m_Border.left + ch.m_bLeft); abc.abcB += (m_Border.left + ch.m_bLeft) + (m_Border.right + ch.m_bRight); abc.abcC -= (m_Border.right + ch.m_bRight); fprintf(file, "\t\n", ch.m_Char, abc.abcA, abc.abcB, abc.abcC); } fprintf(file, "\n"); fclose(file); */ } // wsw : jal : warsow font void CMudFontDlg::ExportWSW(const char* a_Filename) { char path[_MAX_PATH]; char drive[_MAX_DRIVE]; char dir[_MAX_DIR]; char fname[_MAX_FNAME]; _splitpath(a_Filename, drive, dir, fname, NULL); _makepath(path, drive, dir, fname, ".wfd"); FILE* file = fopen(path, "w+t"); if (file == NULL) return; fprintf(file, "// WARSOW Mudfont. version=\"1.1\" encoding=\"UTF-8\"\n"); fprintf(file, "// \"\" \"\"\n"); // wsw : write tga image size fprintf(file, "%d %d\n", m_TextureWidth, m_TextureHeight); // format description fprintf(file, "// \"\" \"\" \"\" \"\" \"\"\n" ); int index = m_AA.GetCurSel(); int sampling = m_AA.GetItemData(index); //ABC abc; m_MemDC.SelectObject(m_DrawFont); int numChars = m_Chars.GetSize(); for (int i=0; i End of file <-----------------------------------------------------------