- make fontview look closer to MS fontview, patch by roytam1, modified by me.

- Create fonts only once
- Remove possibility to get page height without drawing


See issue #2260 for more details.

svn path=/trunk/; revision=26871
This commit is contained in:
Timo Kreuzer 2007-05-23 00:25:31 +00:00
parent 5cecf17b6f
commit 96b50f2d59
5 changed files with 124 additions and 65 deletions

View file

@ -34,10 +34,17 @@ LRESULT CALLBACK DisplayProc(HWND, UINT, WPARAM, LPARAM);
/* Internal data storage type */ /* Internal data storage type */
typedef struct typedef struct
{ {
int nHeight; int nPageHeight;
WCHAR szTypeFaceName[MAX_TYPEFACENAME]; WCHAR szTypeFaceName[MAX_TYPEFACENAME];
WCHAR szFormat[MAX_FORMAT]; WCHAR szFormat[MAX_FORMAT];
WCHAR szString[MAX_STRING]; WCHAR szString[MAX_STRING];
HFONT hCaptionFont;
HFONT hCharSetFont;
HFONT hSizeFont;
HFONT hFonts[MAX_SIZES];
int nSizes[MAX_SIZES];
int nHeights[MAX_SIZES];
} DISPLAYDATA; } DISPLAYDATA;
/* This is the only public function, it registers the class */ /* This is the only public function, it registers the class */
@ -69,9 +76,9 @@ Display_InitClass(HINSTANCE hInstance)
} }
static int static int
Display_DrawText(HDC hDC, DISPLAYDATA* pData, int nYPos, BOOL bDraw) Display_DrawText(HDC hDC, DISPLAYDATA* pData, int nYPos)
{ {
HFONT hOldFont, hFont, hFontNums; HFONT hOldFont;
TEXTMETRIC tm; TEXTMETRIC tm;
int i, y; int i, y;
const int nSizes[7] = {12, 18, 24, 36, 48, 60, 72}; const int nSizes[7] = {12, 18, 24, 36, 48, 60, 72};
@ -81,65 +88,61 @@ Display_DrawText(HDC hDC, DISPLAYDATA* pData, int nYPos, BOOL bDraw)
/* This is the location on the DC where we draw */ /* This is the location on the DC where we draw */
y = -nYPos; y = -nYPos;
/* Draw font name */ hOldFont = SelectObject(hDC, pData->hCaptionFont);
hFont = CreateFontW(50, 0, 0, 0, FW_NORMAL, FALSE, FALSE, FALSE, GetTextMetrics(hDC, &tm);
ANSI_CHARSET, OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS, PROOF_QUALITY,
DEFAULT_PITCH , L"Ms Shell Dlg"); if (*pData->szFormat == 0)
hOldFont = SelectObject(hDC, hFont); {
if (bDraw) swprintf(szCaption, pData->szTypeFaceName);
}
else
{ {
swprintf(szCaption, L"%s (%s)", pData->szTypeFaceName, pData->szFormat); swprintf(szCaption, L"%s (%s)", pData->szTypeFaceName, pData->szFormat);
TextOutW(hDC, 0, y, szCaption, wcslen(szCaption));
} }
GetTextMetrics(hDC, &tm); TextOutW(hDC, 0, y, szCaption, wcslen(szCaption));
y += tm.tmHeight + SPACING1; y += tm.tmHeight + SPACING1;
SelectObject(hDC, hOldFont);
DeleteObject(hFont);
/* Draw a seperation Line */ /* Draw a seperation Line */
if (bDraw) SelectObject(hDC, GetStockObject(BLACK_PEN));
{ MoveToEx(hDC, 0, y, NULL);
SelectObject(hDC, GetStockObject(BLACK_PEN)); LineTo(hDC, 10000, y);
MoveToEx(hDC, 0, y, NULL);
LineTo(hDC, 10000, y);
}
y += SPACING2; y += SPACING2;
/* Output font info */ /* TODO: Output font info */
hFont = CreateFontW(12, 0, 0, 0, FW_NORMAL, FALSE, FALSE, FALSE,
ANSI_CHARSET, OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS, PROOF_QUALITY,
DEFAULT_PITCH , pData->szTypeFaceName);
hOldFont = SelectObject(hDC, hFont);
SelectObject(hDC, hOldFont);
DeleteObject(hFont);
/* Outout the lines for different sizes */ /* Output Character set */
hFontNums = CreateFontW(12, 0, 0, 0, FW_NORMAL, FALSE, FALSE, FALSE, hOldFont = SelectObject(hDC, pData->hCharSetFont);
ANSI_CHARSET, OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS, PROOF_QUALITY, GetTextMetrics(hDC, &tm);
DEFAULT_PITCH , L"Ms Shell Dlg"); swprintf(szCaption, L"abcdefghijklmnopqrstuvwxyz");
TextOutW(hDC, 0, y, szCaption, wcslen(szCaption));
y += tm.tmHeight + 1;
for (i = 0; i < 7; i++) swprintf(szCaption, L"ABCDEFGHIJKLMNOPQRSTUVWXYZ");
TextOutW(hDC, 0, y, szCaption, wcslen(szCaption));
y += tm.tmHeight + 1;
swprintf(szCaption, L"0123456789.:,;(\"~!@#$%^&*')");
TextOutW(hDC, 0, y, szCaption, wcslen(szCaption));
y += tm.tmHeight + 1;
/* Draw a seperation Line */
SelectObject(hDC, GetStockObject(BLACK_PEN));
MoveToEx(hDC, 0, y, NULL);
LineTo(hDC, 10000, y);
y += SPACING2;
/* Output the strings for different sizes */
for (i = 0; i < MAX_SIZES; i++)
{ {
hOldFont = SelectObject(hDC, hFontNums); SelectObject(hDC, pData->hFonts[i]);
if (bDraw) TextOutW(hDC, 20, y, pData->szString, wcslen(pData->szString));
{
swprintf(szSize, L"%d", nSizes[i]);
TextOutW(hDC, 0, y, szSize, wcslen(szSize));
}
hFont = CreateFontW(nSizes[i], 0, 0, 0, FW_NORMAL, FALSE, FALSE, FALSE,
ANSI_CHARSET, OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS, PROOF_QUALITY,
DEFAULT_PITCH , pData->szTypeFaceName);
SelectObject(hDC, hFont);
if (bDraw)
{
TextOutW(hDC, 20, y, pData->szString, wcslen(pData->szString));
}
GetTextMetrics(hDC, &tm); GetTextMetrics(hDC, &tm);
y += tm.tmHeight + 2; y += tm.tmHeight + 1;
SelectObject(hDC, hOldFont); SelectObject(hDC, pData->hSizeFont);
DeleteObject(hFont); swprintf(szSize, L"%d", nSizes[i]);
TextOutW(hDC, 0, y - 13 - tm.tmDescent, szSize, wcslen(szSize));
} }
DeleteObject(hFontNums); SelectObject(hDC, hOldFont);
return y; return y;
} }
@ -148,14 +151,29 @@ static LRESULT
Display_OnCreate(HWND hwnd) Display_OnCreate(HWND hwnd)
{ {
DISPLAYDATA* pData; DISPLAYDATA* pData;
const int nSizes[7] = {12, 18, 24, 36, 48, 60, 72};
int i;
/* Initialize data structure */ /* Initialize data structure */
pData = malloc(sizeof(DISPLAYDATA)); pData = malloc(sizeof(DISPLAYDATA));
pData->nHeight = 0; pData->nPageHeight = 0;
swprintf(pData->szTypeFaceName, L""); swprintf(pData->szTypeFaceName, L"");
swprintf(pData->szFormat, L""); swprintf(pData->szFormat, L"");
swprintf(pData->szString, L""); swprintf(pData->szString, L"");
for (i = 0; i < MAX_SIZES; i++)
{
pData->nSizes[i] = nSizes[i];
}
pData->hCaptionFont = CreateFontW(50, 0, 0, 0, FW_NORMAL, FALSE, FALSE, FALSE,
ANSI_CHARSET, OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS, PROOF_QUALITY,
DEFAULT_PITCH , L"Ms Shell Dlg");
pData->hSizeFont = CreateFontW(12, 0, 0, 0, FW_NORMAL, FALSE, FALSE, FALSE,
ANSI_CHARSET, OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS, PROOF_QUALITY,
DEFAULT_PITCH , L"Ms Shell Dlg");
/* Set the window's GWLP_USERDATA to our data structure */ /* Set the window's GWLP_USERDATA to our data structure */
SetWindowLongPtr(hwnd, GWLP_USERDATA, (LONG_PTR)pData); SetWindowLongPtr(hwnd, GWLP_USERDATA, (LONG_PTR)pData);
@ -182,7 +200,7 @@ Display_OnPaint(HWND hwnd)
FillRect(ps.hdc, &ps.rcPaint, GetStockObject(WHITE_BRUSH)); FillRect(ps.hdc, &ps.rcPaint, GetStockObject(WHITE_BRUSH));
/* Draw the text */ /* Draw the text */
Display_DrawText(ps.hdc, pData, si.nPos, TRUE); Display_DrawText(ps.hdc, pData, si.nPos);
EndPaint(hwnd, &ps); EndPaint(hwnd, &ps);
@ -267,6 +285,7 @@ Display_OnVScroll(HWND hwnd, WPARAM wParam)
SetScrollInfo(hwnd, SB_VERT, &si, TRUE); SetScrollInfo(hwnd, SB_VERT, &si, TRUE);
UpdateWindow(hwnd); UpdateWindow(hwnd);
} }
return 0; return 0;
} }
@ -274,17 +293,42 @@ static LRESULT
Display_SetTypeFace(HWND hwnd, LPARAM lParam) Display_SetTypeFace(HWND hwnd, LPARAM lParam)
{ {
DISPLAYDATA* pData; DISPLAYDATA* pData;
TEXTMETRIC tm;
HDC hDC; HDC hDC;
RECT rect; RECT rect;
SCROLLINFO si; SCROLLINFO si;
int i;
/* Set the new type face name */ /* Set the new type face name */
pData = (DISPLAYDATA*)GetWindowLongPtr(hwnd, GWLP_USERDATA); pData = (DISPLAYDATA*)GetWindowLongPtr(hwnd, GWLP_USERDATA);
snwprintf(pData->szTypeFaceName, MAX_TYPEFACENAME, (WCHAR*)lParam); snwprintf(pData->szTypeFaceName, MAX_TYPEFACENAME, (WCHAR*)lParam);
/* Calculate new page dimensions */ /* Create the new fonts */
hDC = GetDC(hwnd); hDC = GetDC(hwnd);
pData->nHeight = Display_DrawText(hDC, pData, 0, FALSE); pData->hCharSetFont = CreateFontW(-MulDiv(16, GetDeviceCaps(GetDC(NULL), LOGPIXELSY), 72), 0, 0, 0, FW_NORMAL, FALSE, FALSE, FALSE,
ANSI_CHARSET, OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS, PROOF_QUALITY,
DEFAULT_PITCH , pData->szTypeFaceName);
/* Get font format */
SelectObject(hDC, pData->hCharSetFont);
GetTextMetrics(hDC, &tm);
if ((tm.tmPitchAndFamily & TMPF_TRUETYPE) == TMPF_TRUETYPE)
{
swprintf(pData->szFormat, L"TrueType");
}
for (i = 0; i < MAX_SIZES; i++)
{
pData->hFonts[i] = CreateFontW(MulDiv(pData->nSizes[i], GetDeviceCaps(hDC, LOGPIXELSY), 72),
0, 0, 0, FW_NORMAL, FALSE, FALSE, FALSE,
ANSI_CHARSET, OUT_DEFAULT_PRECIS,
CLIP_DEFAULT_PRECIS, PROOF_QUALITY,
DEFAULT_PITCH , pData->szTypeFaceName);
SelectObject(hDC, pData->hFonts[i]);
}
/* Calculate new page dimensions */
pData->nPageHeight = Display_DrawText(hDC, pData, 0);
ReleaseDC(hwnd, hDC); ReleaseDC(hwnd, hDC);
/* Set the vertical scrolling range and page size */ /* Set the vertical scrolling range and page size */
@ -292,7 +336,7 @@ Display_SetTypeFace(HWND hwnd, LPARAM lParam)
si.cbSize = sizeof(si); si.cbSize = sizeof(si);
si.fMask = SIF_RANGE | SIF_PAGE | SIF_POS | SIF_TRACKPOS; si.fMask = SIF_RANGE | SIF_PAGE | SIF_POS | SIF_TRACKPOS;
si.nMin = 0; si.nMin = 0;
si.nMax = pData->nHeight; si.nMax = pData->nPageHeight;
si.nPage = rect.bottom; si.nPage = rect.bottom;
si.nPos = 0; si.nPos = 0;
si.nTrackPos = 0; si.nTrackPos = 0;
@ -318,9 +362,21 @@ static LRESULT
Display_OnDestroy(HWND hwnd) Display_OnDestroy(HWND hwnd)
{ {
DISPLAYDATA* pData; DISPLAYDATA* pData;
int i;
pData = (DISPLAYDATA*)GetWindowLongPtr(hwnd, GWLP_USERDATA);
/* Delete the fonts */
DeleteObject(pData->hCaptionFont);
DeleteObject(pData->hCharSetFont);
DeleteObject(pData->hSizeFont);
for (i = 0; i < MAX_SIZES; i++)
{
DeleteObject(pData->hFonts[i]);
}
/* Free the data structure */ /* Free the data structure */
pData = (DISPLAYDATA*)GetWindowLongPtr(hwnd, GWLP_USERDATA);
free(pData); free(pData);
return 0; return 0;
@ -353,7 +409,7 @@ DisplayProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
return Display_OnDestroy(hwnd); return Display_OnDestroy(hwnd);
default: default:
return DefWindowProc (hwnd, message, wParam, lParam); return DefWindowProcW(hwnd, message, wParam, lParam);
} }
return 0; return 0;

View file

@ -10,6 +10,8 @@
#define MAX_TYPEFACENAME 32 #define MAX_TYPEFACENAME 32
#define MAX_FORMAT 20 #define MAX_FORMAT 20
#define MAX_SIZES 7
extern const WCHAR g_szFontDisplayClassName[]; extern const WCHAR g_szFontDisplayClassName[];
/* Public function */ /* Public function */

View file

@ -23,7 +23,6 @@
#include "fontview.h" #include "fontview.h"
HINSTANCE g_hInstance; HINSTANCE g_hInstance;
WCHAR g_szTitle[MAX_LOADSTRING];
WCHAR g_szTypeFaceName[MAX_TYPEFACENAME]; WCHAR g_szTypeFaceName[MAX_TYPEFACENAME];
static const WCHAR g_szFontViewClassName[] = L"FontViewWClass"; static const WCHAR g_szFontViewClassName[] = L"FontViewWClass";
@ -130,7 +129,7 @@ WinMain (HINSTANCE hThisInstance,
} }
/* The main window class */ /* The main window class */
wincl.cbSize = sizeof (WNDCLASSEX); wincl.cbSize = sizeof (WNDCLASSEXW);
wincl.style = CS_DBLCLKS; wincl.style = CS_DBLCLKS;
wincl.lpfnWndProc = MainWndProc; wincl.lpfnWndProc = MainWndProc;
wincl.cbClsExtra = 0; wincl.cbClsExtra = 0;
@ -154,7 +153,7 @@ WinMain (HINSTANCE hThisInstance,
hMainWnd = CreateWindowExW( hMainWnd = CreateWindowExW(
0, /* Extended possibilites for variation */ 0, /* Extended possibilites for variation */
g_szFontViewClassName, /* Classname */ g_szFontViewClassName, /* Classname */
g_szTitle, /* Title Text */ g_szTypeFaceName, /* Title Text */
WS_OVERLAPPEDWINDOW, /* default window */ WS_OVERLAPPEDWINDOW, /* default window */
CW_USEDEFAULT, /* Windows decides the position */ CW_USEDEFAULT, /* Windows decides the position */
CW_USEDEFAULT, /* where the window ends up on the screen */ CW_USEDEFAULT, /* where the window ends up on the screen */
@ -165,7 +164,7 @@ WinMain (HINSTANCE hThisInstance,
hThisInstance, /* Program Instance handler */ hThisInstance, /* Program Instance handler */
NULL /* No Window Creation data */ NULL /* No Window Creation data */
); );
ShowWindow (hMainWnd, nCmdShow); ShowWindow(hMainWnd, nCmdShow);
/* Main message loop */ /* Main message loop */
while (GetMessage (&msg, NULL, 0, 0)) while (GetMessage (&msg, NULL, 0, 0))
@ -203,11 +202,11 @@ MainWnd_OnCreate(HWND hwnd)
NULL /* Window Creation data */ NULL /* Window Creation data */
); );
/* Init the display window wit the font name */ /* Init the display window with the font name */
LoadStringW(g_hInstance, IDS_STRING, szString, MAX_STRING); LoadStringW(g_hInstance, IDS_STRING, szString, MAX_STRING);
SendMessage(hDisplay, FVM_SETSTRING, 0, (LPARAM)szString); SendMessage(hDisplay, FVM_SETSTRING, 0, (LPARAM)szString);
SendMessage(hDisplay, FVM_SETTYPEFACE, 0, (LPARAM)g_szTypeFaceName); SendMessage(hDisplay, FVM_SETTYPEFACE, 0, (LPARAM)g_szTypeFaceName);
/* Create the quit button */ /* Create the quit button */
LoadStringW(g_hInstance, IDS_QUIT, szQuit, MAX_BUTTONNAME); LoadStringW(g_hInstance, IDS_QUIT, szQuit, MAX_BUTTONNAME);
hButtonQuit = CreateWindowExW( hButtonQuit = CreateWindowExW(
@ -307,7 +306,7 @@ MainWndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
break; break;
default: /* for messages that we don't deal with */ default: /* for messages that we don't deal with */
return DefWindowProc (hwnd, message, wParam, lParam); return DefWindowProcW(hwnd, message, wParam, lParam);
} }
return 0; return 0;

View file

@ -1,5 +1,6 @@
#include "lang/en-US.rc"
#include "lang/de-DE.rc" #include "lang/de-DE.rc"
STRINGTABLE DISCARDABLE STRINGTABLE DISCARDABLE

View file

@ -2,13 +2,13 @@
#include "windows.h" #include "windows.h"
#include "../resource.h" #include "../resource.h"
LANGUAGE LANG_GERMAN, SUBLANG_GERMAN LANGUAGE LANG_ENGLISH, SUBLANG_ENGLISH_US
STRINGTABLE DISCARDABLE STRINGTABLE DISCARDABLE
{ {
IDS_QUIT, "Quit" IDS_QUIT, "Quit"
IDS_PRINT, "Print" IDS_PRINT, "Print"
IDS_STRING, "Jackdaws love my big sphinx of quartz. 123456890" IDS_STRING, "Jackdaws love my big sphinx of quartz. 1234567890"
IDS_ERROR, "Error" IDS_ERROR, "Error"
IDS_ERROR_NOMEM, "There's not enough memory to complete the operation." IDS_ERROR_NOMEM, "There's not enough memory to complete the operation."
IDS_ERROR_NOFONT, "The file %1 ist not a valid font file." IDS_ERROR_NOFONT, "The file %1 ist not a valid font file."
@ -16,3 +16,4 @@ STRINGTABLE DISCARDABLE
IDS_ERROR_BADCMD, "No font file given.\nSyntax:\n fontview.exe <font file>" IDS_ERROR_BADCMD, "No font file given.\nSyntax:\n fontview.exe <font file>"
} }