Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

665 lines
17 KiB

  1. /******************************Module*Header*******************************\
  2. * Module Name: test.c
  3. *
  4. * Created: 09-Dec-1992 10:51:46
  5. * Author: Kirk Olynyk [kirko]
  6. *
  7. * Copyright (c) 1991 Microsoft Corporation
  8. *
  9. * Contains the test
  10. *
  11. \**************************************************************************/
  12. #include "precomp.hpp"
  13. // globals
  14. Font *gFont = NULL;
  15. BOOL gTextAntiAlias = FALSE;
  16. InstalledFontCollection gInstalledFontCollection;
  17. PrivateFontCollection gPrivateFontCollection;
  18. ///////////////////////////////////////////////////////////////////////////////
  19. // Test function prototypes
  20. VOID TestFonts(VOID);
  21. GraphicsPath* CreateHeartPath(const RectF& rect);
  22. VOID TestGradients(Graphics *g);
  23. ///////////////////////////////////////////////////////////////////////////////
  24. VOID Test(HWND hwnd)
  25. {
  26. Graphics *g = Graphics::FromHWND(hwnd);
  27. TestGradients(g);
  28. TestFonts();
  29. delete g;
  30. }
  31. ///////////////////////////////////////////////////////////////////////////////
  32. // Font test functions
  33. VOID TestFonts(VOID)
  34. {
  35. // Test font family enumeration
  36. INT numFamilies = gInstalledFontCollection.GetFamilyCount();
  37. Dbgprintf("%d installed font families loaded.", numFamilies);
  38. Dbgprintf("");
  39. FontFamily* families = new FontFamily[numFamilies];
  40. INT numFound;
  41. gInstalledFontCollection.GetFamilies(numFamilies, families, &numFound);
  42. Dbgprintf("Enumerated font families:");
  43. for (int f = 0; f < numFound; f++)
  44. {
  45. WCHAR faceName[LF_FACESIZE];
  46. families[f].GetFamilyName(faceName);
  47. Dbgprintf(" %ws", faceName);
  48. }
  49. Dbgprintf("");
  50. delete [] families;
  51. // Enumerate the private font families
  52. numFamilies = gPrivateFontCollection.GetFamilyCount();
  53. Dbgprintf("%d private font families loaded.", numFamilies);
  54. Dbgprintf("");
  55. if (numFamilies != 0)
  56. {
  57. families = new FontFamily[numFamilies];
  58. gPrivateFontCollection.GetFamilies(numFamilies, families, &numFound);
  59. Dbgprintf("PRIVATE enumerated font families:");
  60. for (int f = 0; f < numFound; f++)
  61. {
  62. WCHAR faceName[LF_FACESIZE];
  63. families[f].GetFamilyName(faceName);
  64. Dbgprintf(" %ws", faceName);
  65. }
  66. Dbgprintf("");
  67. delete [] families;
  68. }
  69. //HFONT hfont = NULL;
  70. //Font* font = new Font(hfont);//10, "Arial");
  71. Font* font = new Font(&FontFamily(L"Arial"), 10);
  72. // Test text output
  73. Color blue(0, 0, 255, 255);
  74. SolidBrush blueBrush(blue);
  75. #if 0
  76. g->DrawStringI(
  77. L"Hi",
  78. NULL,
  79. 0, 0, // x,y
  80. NULL, 0, // pdx, flags
  81. &blueBrush // GpBrush*
  82. );
  83. #endif
  84. if (font != NULL)
  85. {
  86. delete font;
  87. }
  88. }
  89. ///////////////////////////////////////////////////////////////////////////////
  90. GraphicsPath* CreateHeartPath(const RectF& rect)
  91. {
  92. GpPointF points[7];
  93. points[0].X = 0;
  94. points[0].Y = 0;
  95. points[1].X = 1.00;
  96. points[1].Y = -1.00;
  97. points[2].X = 2.00;
  98. points[2].Y = 1.00;
  99. points[3].X = 0;
  100. points[3].Y = 2.00;
  101. points[4].X = -2.00;
  102. points[4].Y = 1.00;
  103. points[5].X = -1.00;
  104. points[5].Y = -1.00;
  105. points[6].X = 0;
  106. points[6].Y = 0;
  107. Matrix matrix;
  108. matrix.Scale(rect.Width/2, rect.Height/3, MatrixOrderAppend);
  109. matrix.Translate(3*rect.Width/2, 4*rect.Height/3, MatrixOrderAppend);
  110. matrix.TransformPoints(&points[0], 7);
  111. GraphicsPath* path = new GraphicsPath();
  112. if(path)
  113. {
  114. path->AddBeziers(&points[0], 7);
  115. path->CloseFigure();
  116. }
  117. return path;
  118. }
  119. /**************************************************************************\
  120. * TestGradients
  121. *
  122. * A test for rectangle and radial gradients.
  123. *
  124. \**************************************************************************/
  125. VOID TestGradients(Graphics* g)
  126. {
  127. REAL width = 4; // Pen width
  128. // Create a rectangular gradient brush.
  129. RectF brushRect(0, 0, 32, 32);
  130. Color colors[5] = {
  131. Color(255, 255, 255, 255),
  132. Color(255, 255, 0, 0),
  133. Color(255, 0, 255, 0),
  134. Color(255, 0, 0, 255),
  135. Color(255, 0, 0, 0)
  136. };
  137. Color blackColor(0, 0, 0);
  138. SolidBrush blackBrush(blackColor);
  139. Pen blackPen(&blackBrush, width);
  140. g->DrawRectangle(&blackPen, brushRect);
  141. // Create a radial gradient brush.
  142. Color centerColor(255, 255, 255, 255);
  143. Color boundaryColor(255, 0, 0, 0);
  144. brushRect.X = 380;
  145. brushRect.Y = 130;
  146. brushRect.Width = 60;
  147. brushRect.Height = 32;
  148. PointF center;
  149. center.X = brushRect.X + brushRect.Width/2;
  150. center.Y = brushRect.Y + brushRect.Height/2;
  151. // Triangle gradient.
  152. PointF points[7];
  153. points[0].X = 50;
  154. points[0].Y = 10;
  155. points[1].X = 200;
  156. points[1].Y = 20;
  157. points[2].X = 100;
  158. points[2].Y = 100;
  159. points[3].X = 30;
  160. points[3].Y = 120;
  161. Color colors1[5] = {
  162. Color(255, 255, 255, 0),
  163. Color(255, 255, 0, 0),
  164. Color(255, 0, 255, 0),
  165. Color(255, 0, 0, 255),
  166. Color(255, 0, 0, 0)
  167. };
  168. points[0].X = 200;
  169. points[0].Y = 300;
  170. points[1].X = 280;
  171. points[1].Y = 350;
  172. points[2].X = 220;
  173. points[2].Y = 420;
  174. points[3].X = 160;
  175. points[3].Y = 440;
  176. points[4].X = 120;
  177. points[4].Y = 370;
  178. PathGradientBrush polyGrad(points, 5);
  179. REAL blend[10];
  180. Color presetColors[10];
  181. REAL positions[10];
  182. INT count;
  183. INT i;
  184. count = 3;
  185. blend[0] = (REAL) 0;
  186. blend[1] = (REAL) 0;
  187. blend[2] = (REAL) 1;
  188. positions[0] = (REAL) 0;
  189. positions[1] = (REAL) 0.4;
  190. positions[2] = (REAL) 1;
  191. // Test for blending factors.
  192. polyGrad.SetBlend(&blend[0], &positions[0], count);
  193. polyGrad.SetCenterColor(centerColor);
  194. INT colorset = 5;
  195. polyGrad.SetSurroundColors(&colors1[0], &colorset);
  196. // g->FillPolygon(&polyGrad, points, 5);
  197. RectF polyRect;
  198. polyGrad.GetRectangle(&polyRect);
  199. g->FillRectangle(&polyGrad, polyRect);
  200. // Create a heart shaped path.
  201. RectF rect;
  202. rect.X = 300;
  203. rect.Y = 300;
  204. rect.Width = 150;
  205. rect.Height = 150;
  206. GraphicsPath *path = CreateHeartPath(rect);
  207. // Create a gradient from a path.
  208. PathGradientBrush pathGrad(path);
  209. delete path;
  210. pathGrad.SetCenterColor(centerColor);
  211. INT colorsset = 5;
  212. colors1[0] = Color(255, 255, 0, 0);
  213. pathGrad.SetSurroundColors(&colors1[0], &colorsset);
  214. pathGrad.GetRectangle(&polyRect);
  215. // Test for LineGradientBrush.
  216. RectF lineRect(120, -20, 200, 60);
  217. Color color1(200, 255, 255, 0);
  218. Color color2(200, 0, 0, 255);
  219. }
  220. /////////////////////////////////////////////////////////////////////////////
  221. // CreateNewFont
  222. //
  223. // History
  224. // Aug-1999 -by- Xudong Wu [tessiew]
  225. /////////////////////////////////////////////////////////////////////////////
  226. void CreateNewFont(char *name, FLOAT size, FontStyle style, Unit unit)
  227. {
  228. Dbgprintf("Calling CreateNewFont");
  229. // convert ansi to unicode
  230. WCHAR wcname[MAX_PATH];
  231. memset(wcname, 0, sizeof(wcname));
  232. MultiByteToWideChar(CP_ACP, 0, name, strlen(name), wcname, MAX_PATH);
  233. // for now ignore all other unit than UnitWorld
  234. FontFamily * pFamily;
  235. pFamily = new FontFamily(wcname);
  236. if (pFamily->GetLastStatus() != Ok)
  237. {
  238. pFamily = new FontFamily(wcname, &gPrivateFontCollection);
  239. }
  240. if (gFont != NULL)
  241. {
  242. delete gFont;
  243. }
  244. gFont = (Font*) new Font(pFamily, size, style, unit);
  245. if (gFont == NULL)
  246. {
  247. Dbgprintf("failed to create a new font");
  248. }
  249. else
  250. {
  251. if (gFont->IsAvailable())
  252. {
  253. Dbgprintf("new font created");
  254. }
  255. else
  256. {
  257. Dbgprintf("can't create font");
  258. delete gFont;
  259. gFont = NULL;
  260. }
  261. }
  262. if (pFamily != NULL)
  263. {
  264. delete pFamily;
  265. }
  266. Dbgprintf("");
  267. }
  268. ///////////////////////////////////////////////////////////////////////////////
  269. // TestDrawGlyphs
  270. //
  271. // History
  272. // Aug-1999 -by- Xudong Wu [tessiew]
  273. //////////////////////////////////////////////////////////////////////////////
  274. VOID TestDrawGlyphs(
  275. HWND hwnd,
  276. UINT16 *glyphIndices,
  277. INT count,
  278. INT *px,
  279. INT *py,
  280. INT flags)
  281. {
  282. FontFamily family;
  283. INT style;
  284. REAL size;
  285. Unit unit;
  286. if (gFont)
  287. {
  288. Status status;
  289. status = gFont->GetFamily(&family);
  290. style = gFont->GetStyle();
  291. size = gFont->GetSize();
  292. unit = gFont->GetUnit();
  293. SolidBrush redBrush(Color(255,0,0));
  294. //HatchBrush hatBrush(HatchStyleDiagonalCross, Color(255,0,0), Color(128,128,128));
  295. Graphics *g = Graphics::FromHWND(hwnd);
  296. Dbgprintf("Graphics.DrawGlyphs");
  297. Dbgprintf("Font:: size %f style %d unit %d", size, style, unit);
  298. Dbgprintf("glyphIndices px py");
  299. for (INT i=0; i<count; i++)
  300. {
  301. Dbgprintf("%d %d %d", glyphIndices[i], px[i], py[i]);
  302. }
  303. Dbgprintf("");
  304. //g->DrawGlyphs(glyphIndices, count, gFont, &redBrush, px, py, flags);
  305. // Gradient brush
  306. RectF gradRect(0, 0, 32, 32);
  307. Color colors[5] = {
  308. Color(255, 255, 255, 255),
  309. Color(255, 255, 0, 0),
  310. Color(255, 0, 255, 0),
  311. Color(255, 0, 0, 255),
  312. Color(255, 0, 0, 0)
  313. };
  314. PVOID Gpg = (PVOID)(g);
  315. PVOID font = (PVOID)gFont;
  316. PVOID brush = (PVOID)&redBrush;
  317. GpGraphics* gpg = *((GpGraphics**)Gpg);
  318. GpFont* gpfont = *((GpFont**)font);
  319. GpBrush* gpbrush = ((GpBrush**)brush)[1];
  320. //GpBrush* gpbrushHat = ((GpBrush**)&hatBrush)[1];
  321. if (gTextAntiAlias)
  322. (*gfnGdipSetTextRenderingHint)(gpg, TextRenderingHintAntiAlias);
  323. else
  324. (*gfnGdipSetTextRenderingHint)(gpg, TextRenderingHintSingleBitPerPixelGridFit);
  325. if (gfnGdipDrawGlyphs)
  326. {
  327. (*gfnGdipDrawGlyphs)(gpg, glyphIndices, count, gpfont, gpbrush, px, py, flags);
  328. /*
  329. if (flags & DG_XCONSTANT)
  330. px[0] += 1600;
  331. else if (flags & DG_YCONSTANT)
  332. py[0] += 1600;
  333. (*gfnGdipDrawGlyphs)(gpg, glyphIndices, count, gpfont, gpbrushHat, px, py, flags);
  334. */
  335. } else
  336. {
  337. PointF *origins;
  338. origins = new PointF[count];
  339. for (INT i=0; i<count; i++)
  340. {
  341. origins[i].X = (float)px[i] / (float)16.0;
  342. origins[i].Y = (float)py[i] / (float)16.0;
  343. }
  344. g->DrawDriverString(
  345. glyphIndices,
  346. count,
  347. gFont,
  348. &redBrush,
  349. origins,
  350. 0, //g_DriverOptions,
  351. NULL //&g_DriverTransform
  352. );
  353. }
  354. g->Flush();
  355. delete g;
  356. }
  357. }
  358. ///////////////////////////////////////////////////////////////////////////////
  359. // TestPathGlyphs
  360. //
  361. // History
  362. // Aug-1999 -by- Xudong Wu [tessiew]
  363. //////////////////////////////////////////////////////////////////////////////
  364. VOID TestPathGlyphs(
  365. HWND hwnd,
  366. UINT16 *glyphIndices,
  367. INT count,
  368. REAL *px,
  369. REAL *py,
  370. INT flags)
  371. {
  372. INT style;
  373. REAL size;
  374. Unit unit;
  375. if (gFont)
  376. {
  377. style = gFont->GetStyle();
  378. size = gFont->GetSize();
  379. unit = gFont->GetUnit();
  380. SolidBrush redBrush(Color(255,0,0));
  381. SolidBrush blkBrush(Color(0,0,0));
  382. Pen blkPen(&blkBrush, (REAL)1);
  383. Graphics *g = Graphics::FromHWND(hwnd);
  384. Dbgprintf("Add Glyphs To Path");
  385. Dbgprintf("Font:: size %f style %d unit %d", size, style, unit);
  386. Dbgprintf("glyphIndices px py");
  387. for (INT i=0; i<count; i++)
  388. {
  389. Dbgprintf("%d %d %d", glyphIndices[i], INT(px[i]), INT(py[i]));
  390. }
  391. Dbgprintf("");
  392. GraphicsPath pathRed;
  393. GraphicsPath pathBlk;
  394. PVOID ptrv = (PVOID)gFont;
  395. GpFont* gpfont = *((GpFont**)ptrv);
  396. ptrv = (PVOID)&pathRed;
  397. GpPath* gpPathRed = *((GpPath**)ptrv);
  398. ptrv = (PVOID)&pathBlk;
  399. GpPath* gpPathBlk = *((GpPath**)ptrv);
  400. if (gfnGdipPathAddGlyphs)
  401. {
  402. (*gfnGdipPathAddGlyphs)(gpPathRed, glyphIndices, count, gpfont, px, py, flags);
  403. g->FillPath(&redBrush, &pathRed);
  404. for (INT i=0; i<count; i++)
  405. {
  406. py[i] += 50.0;
  407. }
  408. (*gfnGdipPathAddGlyphs)(gpPathBlk, glyphIndices, count, gpfont, px, py, flags);
  409. g->DrawPath(&blkPen, &pathBlk);
  410. }
  411. delete g;
  412. }
  413. }
  414. /////////////////////////////////////////////////////////////////////////////
  415. // AddFontFile
  416. //
  417. // History
  418. // Nov-1999 -by- Xudong Wu [tessiew]
  419. /////////////////////////////////////////////////////////////////////////////
  420. void TestAddFontFile(char *fileName, INT flag, BOOL loadAsImage)
  421. {
  422. Dbgprintf("Calling AddFontFile");
  423. Dbgprintf("filename %s flag %d loadAsImage %d", fileName, flag, loadAsImage);
  424. if ((flag == AddFontFlagPublic) && loadAsImage)
  425. {
  426. Dbgprintf("Cannot load a memory image in the installed font collection");
  427. return;
  428. }
  429. if (loadAsImage)
  430. {
  431. HANDLE hFile, hFileMapping;
  432. hFile = CreateFileA(fileName,
  433. GENERIC_READ,
  434. FILE_SHARE_READ,
  435. 0,
  436. OPEN_EXISTING,
  437. FILE_ATTRIBUTE_NORMAL,
  438. 0);
  439. if (hFile != INVALID_HANDLE_VALUE)
  440. {
  441. DWORD cjSize;
  442. PVOID pFontFile;
  443. cjSize = GetFileSize(hFile, NULL);
  444. if (cjSize == -1)
  445. Dbgprintf("GetFileSize() failed\n");
  446. else
  447. {
  448. hFileMapping = CreateFileMapping(hFile, 0, PAGE_READONLY, 0, 0, NULL);
  449. if (hFileMapping)
  450. {
  451. pFontFile = MapViewOfFile(hFileMapping, FILE_MAP_READ, 0, 0, 0);
  452. if(pFontFile)
  453. {
  454. if (gPrivateFontCollection.AddMemoryFont((BYTE*)pFontFile,
  455. cjSize) == Ok)
  456. Dbgprintf("AddMemoryFont to private font collection");
  457. else
  458. Dbgprintf("AddMemoryFont to private font collection failed");
  459. UnmapViewOfFile(pFontFile);
  460. }
  461. else
  462. Dbgprintf("MapViewOfFile() failed");
  463. CloseHandle(hFileMapping);
  464. }
  465. else
  466. Dbgprintf("CreateFileMapping() failed");
  467. }
  468. CloseHandle(hFile);
  469. }
  470. else
  471. Dbgprintf("CreateFileA failed");
  472. }
  473. else
  474. {
  475. WCHAR wcname[MAX_PATH];
  476. memset(wcname, 0, sizeof(wcname));
  477. MultiByteToWideChar(CP_ACP, 0, fileName, strlen(fileName), wcname, MAX_PATH);
  478. if (flag == AddFontFlagPublic)
  479. {
  480. /* // add this code in version 2 (when InstallFontFile is exposed)
  481. if (gInstalledFontCollection.InstallFontFile(wcname) == Ok)
  482. {
  483. Dbgprintf("InstallFontFile to installed font collection");
  484. }
  485. else
  486. {
  487. Dbgprintf("InstallFontFile to installed font collection failed");
  488. }
  489. */
  490. Dbgprintf("InstallFontFile to installed font collection failed (API not yet exposed)");
  491. }
  492. else
  493. {
  494. if (gPrivateFontCollection.AddFontFile(wcname) == Ok)
  495. {
  496. Dbgprintf("AddFontFile to private font collection");
  497. }
  498. else
  499. {
  500. Dbgprintf("AddFontFile to private font collection failed");
  501. }
  502. }
  503. }
  504. }
  505. /////////////////////////////////////////////////////////////////////////////
  506. // RemoveFontFile
  507. //
  508. // History
  509. // Nov-1999 -by- Xudong Wu [tessiew]
  510. /////////////////////////////////////////////////////////////////////////////
  511. void TestRemoveFontFile(char* fileName)
  512. {
  513. WCHAR wcname[MAX_PATH];
  514. memset(wcname, 0, sizeof(wcname));
  515. MultiByteToWideChar(CP_ACP, 0, fileName, strlen(fileName), wcname, MAX_PATH);
  516. /* // add this code in version 2 (when UninstallFontFile is exposed)
  517. if (gInstalledFontCollection.UninstallFontFile(wcname) == Ok)
  518. {
  519. Dbgprintf("UninstallFontFile from installed font collection");
  520. }
  521. else
  522. {
  523. Dbgprintf("UninstallFontFile from installed font collection failed");
  524. }
  525. */
  526. Dbgprintf("UninstallFontFile from installed font collection failed (API not yet exposed)");
  527. }
  528. void TestTextAntiAliasOn()
  529. {
  530. gTextAntiAlias = TRUE;
  531. }
  532. void TestTextAntiAliasOff()
  533. {
  534. gTextAntiAlias = FALSE;
  535. }