uMain.pas 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428
  1. unit uMain;
  2. interface
  3. uses
  4. Classes, SysUtils, Forms, Controls, Graphics, Dialogs, StdCtrls,
  5. ExtCtrls, ComCtrls,
  6. zgl_application,
  7. zgl_screen,
  8. zgl_window,
  9. zgl_utils,
  10. zgl_primitives_2d,
  11. zgl_sprite_2d,
  12. zgl_font,
  13. zgl_text,
  14. zgl_file,
  15. zgl_font_gen,
  16. Spin;
  17. type
  18. { TForm1 }
  19. TForm1 = class(TForm)
  20. ButtonRebuildFont: TButton;
  21. ButtonImportSymbols: TButton;
  22. ButtonDefaultSymbols: TButton;
  23. ButtonExit: TButton;
  24. ButtonSaveFont: TButton;
  25. ButtonChooseFont: TButton;
  26. CheckBoxAntialiasing: TCheckBox;
  27. CheckBoxPack: TCheckBox;
  28. ComboBoxPageSize: TComboBox;
  29. EditChars: TEdit;
  30. EditTest: TEdit;
  31. FontDialog: TFontDialog;
  32. GroupBox1: TGroupBox;
  33. GroupBox2: TGroupBox;
  34. GroupBox3: TGroupBox;
  35. GroupBox4: TGroupBox;
  36. Label1: TLabel;
  37. Label2: TLabel;
  38. Label3: TLabel;
  39. Label4: TLabel;
  40. LabelPageSize: TLabel;
  41. LabelCurrentPage: TLabel;
  42. OpenDialog: TOpenDialog;
  43. Panel1: TPanel;
  44. SaveFontDialog: TSaveDialog;
  45. SpinCurrentPage: TSpinEdit;
  46. SpinTop: TSpinEdit;
  47. SpinLeft: TSpinEdit;
  48. SpinRight: TSpinEdit;
  49. SpinBottom: TSpinEdit;
  50. Timer1: TTimer;
  51. procedure ButtonChooseFontClick(Sender: TObject);
  52. procedure ButtonDefaultSymbolsClick(Sender: TObject);
  53. procedure ButtonExitClick(Sender: TObject);
  54. procedure ButtonImportSymbolsClick(Sender: TObject);
  55. procedure ButtonRebuildFontClick(Sender: TObject);
  56. procedure ButtonSaveFontClick(Sender: TObject);
  57. procedure CheckBoxAntialiasingChange(Sender: TObject);
  58. procedure CheckBoxPackChange(Sender: TObject);
  59. procedure ComboBoxPageSizeChange(Sender: TObject);
  60. procedure FormActivate(Sender: TObject);
  61. procedure FormClose(Sender: TObject; var CloseAction: TCloseAction);
  62. procedure FormResize(Sender: TObject);
  63. procedure Panel1MouseDown(Sender: TObject; Button: TMouseButton;
  64. Shift: TShiftState; X, Y: Integer);
  65. procedure Panel1MouseMove(Sender: TObject; Shift: TShiftState; X, Y: Integer
  66. );
  67. procedure Panel1MouseUp(Sender: TObject; Button: TMouseButton;
  68. Shift: TShiftState; X, Y: Integer);
  69. procedure SpinLeftChange(Sender: TObject);
  70. procedure SpinTopChange(Sender: TObject);
  71. procedure SpinBottomChange(Sender: TObject);
  72. procedure SpinRightChange(Sender: TObject);
  73. procedure SpinCurrentPageChange(Sender: TObject);
  74. procedure Timer1Timer(Sender: TObject);
  75. private
  76. { private declarations }
  77. public
  78. { public declarations }
  79. procedure SetDefaultSymbolList;
  80. procedure UpdateSymbolList;
  81. procedure UpdateFont;
  82. end;
  83. var
  84. Form1 : TForm1;
  85. zgl_Inited : Boolean = False;
  86. fontMoving : Boolean;
  87. fontX, fontY : Integer;
  88. lastX, lastY : Integer;
  89. utf8chars : array[0..65535, 0..5] of AnsiChar;
  90. implementation
  91. {$R *.dfm}
  92. procedure Init;
  93. var
  94. i : Integer;
  95. begin
  96. wnd_SetSize( Form1.Panel1.ClientWidth, Form1.Panel1.ClientHeight);
  97. scrVSync := True;
  98. fontgen_Init();
  99. fg_Font := font_Add();
  100. Form1.SetDefaultSymbolList();
  101. Form1.UpdateFont();
  102. fontX := (Form1.Panel1.Width - fg_PageSize) div 2;
  103. fontY := (Form1.Panel1.Height - fg_PageSize) div 2;
  104. end;
  105. procedure Draw;
  106. var
  107. w : Single;
  108. begin
  109. pr2d_Rect(0, 0, Form1.Panel1.Width, Form1.Panel1.Height, $505050, 255, PR2D_FILL);
  110. pr2d_Rect(fontX, fontY, fg_PageSize, fg_PageSize, $000000, 255, PR2D_FILL);
  111. if (fg_Font <> 0) and Assigned(managerFont.Font[fg_Font].Pages) Then
  112. begin
  113. ssprite2d_Draw(managerFont.Font[fg_Font].Pages[Form1.SpinCurrentPage.Value - 1], fontX, fontY, fg_PageSize, fg_PageSize, 0);
  114. w := text_GetWidth(fg_Font, Form1.EditTest.Text);
  115. pr2d_Rect((Form1.Panel1.Width - w) / 2, Form1.Panel1.Height - managerFont.Font[fg_Font].MaxShiftY - managerFont.Font[fg_Font].MaxHeight, w, managerFont.Font[fg_Font].MaxShiftY + managerFont.Font[fg_Font].MaxHeight, $000000, 255, PR2D_FILL);
  116. text_Draw(fg_Font, Form1.Panel1.Width div 2, Form1.Panel1.Height - managerFont.Font[fg_Font].MaxHeight, Form1.EditTest.Text, TEXT_HALIGN_CENTER);
  117. end;
  118. Application.ProcessMessages();
  119. u_Sleep(10);
  120. end;
  121. { TForm1 }
  122. procedure TForm1.SetDefaultSymbolList;
  123. begin
  124. EditChars.Text := ' !"#$%&''()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}' +
  125. '~¡¢£¤¥¦§¨©ª«¬®¯°±²³´µ¶·¸¹º»¼½¾¿№' +
  126. 'ЁЄІЇАБВГДЕЖЗИЙКЛМНОПРСТУФХЦЧШЩЪЫЬЭЮЯабвгдежзийклмнопрстуфхцчшщъыьэюяёєіїҐґ';
  127. end;
  128. procedure TForm1.UpdateSymbolList;
  129. var
  130. i, j, len : Integer;
  131. c : Word;
  132. begin
  133. // Panel1.Canvas.Clear();
  134. Panel1.Update;
  135. Application.ProcessMessages();
  136. i := 1;
  137. FillChar(fg_CharsUse, 65536, 0);
  138. managerFont.Font[fg_Font].Count.Chars := 0;
  139. len := length(EditChars.Text);
  140. while i <= len do
  141. begin
  142. c := utf8_toUnicode(EditChars.Text, i, @j);
  143. if not fg_CharsUse[c] Then
  144. begin
  145. fg_CharsUse[c] := TRUE;
  146. FillChar(utf8chars[c, 0], 6, 0);
  147. Move(EditChars.Text[i], utf8chars[c, 0], j - i);
  148. INC(managerFont.Font[fg_Font].Count.Chars);
  149. end;
  150. i := j;
  151. end;
  152. EditChars.Text := '';
  153. i := 0;
  154. while i < 65536 do
  155. begin
  156. if fg_CharsUse[i] Then
  157. EditChars.Text := EditChars.Text + utf8chars[i];
  158. inc(i);
  159. end;
  160. Application.ProcessMessages();
  161. end;
  162. procedure TForm1.UpdateFont;
  163. begin
  164. UpdateSymbolList();
  165. fontgen_BuildFont(fg_Font, FontDialog.Font.Name);
  166. SpinCurrentPage.MaxValue := managerFont.Font[fg_Font].Count.Pages;
  167. if (managerFont.Font[fg_Font].Count.Pages = 0) or (managerFont.Font[fg_Font].Count.Pages = 1) then
  168. begin
  169. SpinCurrentPage.Enabled := False;
  170. SpinCurrentPage.Value := managerFont.Font[fg_Font].Count.Pages;
  171. Exit;
  172. end
  173. else
  174. SpinCurrentPage.Enabled := True;
  175. if SpinCurrentPage.Value > SpinCurrentPage.MaxValue Then
  176. SpinCurrentPage.Value := SpinCurrentPage.MaxValue;
  177. end;
  178. procedure TForm1.ButtonChooseFontClick(Sender: TObject);
  179. begin
  180. Timer1.Enabled := False;
  181. if FontDialog.Execute() Then
  182. begin
  183. fg_FontSize := FontDialog.Font.Size;
  184. fg_FontBold := fsBold in FontDialog.Font.Style;
  185. fg_FontItalic := fsItalic in FontDialog.Font.Style;
  186. UpdateFont();
  187. end;
  188. Timer1.Enabled := True;
  189. end;
  190. procedure TForm1.ButtonDefaultSymbolsClick(Sender: TObject);
  191. begin
  192. Timer1.Enabled := False;
  193. SetDefaultSymbolList();
  194. UpdateFont();
  195. Timer1.Enabled := True;
  196. end;
  197. procedure TForm1.ButtonExitClick(Sender: TObject);
  198. begin
  199. Form1.Close;
  200. end;
  201. procedure TForm1.ButtonImportSymbolsClick(Sender: TObject);
  202. var
  203. i : Integer;
  204. s : TStrings;
  205. begin
  206. Timer1.Enabled := False;
  207. s := TStringList.Create;
  208. if OpenDialog.Execute() Then
  209. begin
  210. s.LoadFromFile(OpenDialog.FileName);
  211. for i := 0 to s.Count - 1 do
  212. EditChars.Text := EditChars.Text + s.Strings[i];
  213. UpdateFont();
  214. end;
  215. Timer1.Enabled := True;
  216. end;
  217. procedure TForm1.ButtonRebuildFontClick(Sender: TObject);
  218. begin
  219. Timer1.Enabled := False;
  220. UpdateFont();
  221. Timer1.Enabled := True;
  222. end;
  223. procedure TForm1.FormClose(Sender: TObject; var CloseAction: TCloseAction);
  224. begin
  225. Timer1.Enabled := False;
  226. winOn := False;
  227. zgl_Destroy;
  228. Application.Terminate;
  229. end;
  230. procedure TForm1.FormResize(Sender: TObject);
  231. begin
  232. wnd_SetSize(Panel1.ClientWidth, Panel1.ClientHeight);
  233. end;
  234. procedure TForm1.Panel1MouseDown(Sender: TObject; Button: TMouseButton;
  235. Shift: TShiftState; X, Y: Integer);
  236. begin
  237. if not fontMoving Then
  238. begin
  239. fontMoving := TRUE;
  240. lastX := X;
  241. lastY := Y;
  242. end;
  243. end;
  244. procedure TForm1.Panel1MouseMove(Sender: TObject; Shift: TShiftState; X,
  245. Y: Integer);
  246. begin
  247. if fontMoving Then
  248. begin
  249. fontX := fontX + (X - lastX);
  250. fontY := fontY + (Y - lastY);
  251. lastX := X;
  252. lastY := Y;
  253. end;
  254. end;
  255. procedure TForm1.Panel1MouseUp(Sender: TObject; Button: TMouseButton;
  256. Shift: TShiftState; X, Y: Integer);
  257. begin
  258. fontMoving := FALSE;
  259. end;
  260. procedure TForm1.SpinLeftChange(Sender: TObject);
  261. begin
  262. Timer1.Enabled := False;
  263. if SpinLeft.Value < 0 then
  264. SpinLeft.Value := 0;
  265. if fg_FontPadding[0] <> SpinLeft.Value Then
  266. begin
  267. fg_FontPadding[0] := SpinLeft.Value;
  268. UpdateFont();
  269. end;
  270. Timer1.Enabled := True;
  271. end;
  272. procedure TForm1.SpinTopChange(Sender: TObject);
  273. begin
  274. Timer1.Enabled := False;
  275. if SpinTop.Value < 0 then
  276. SpinTop.Value := 0;
  277. if fg_FontPadding[1] <> SpinTop.Value Then
  278. begin
  279. fg_FontPadding[1] := SpinTop.Value;
  280. UpdateFont();
  281. end;
  282. Timer1.Enabled := True;
  283. end;
  284. procedure TForm1.SpinRightChange(Sender: TObject);
  285. begin
  286. Timer1.Enabled := False;
  287. if SpinRight.Value < 0 then
  288. SpinRight.Value := 0;
  289. if fg_FontPadding[2] <> SpinRight.Value Then
  290. begin
  291. fg_FontPadding[2] := SpinRight.Value;
  292. UpdateFont();
  293. end;
  294. Timer1.Enabled := True;
  295. end;
  296. procedure TForm1.SpinBottomChange(Sender: TObject);
  297. begin
  298. Timer1.Enabled := False;
  299. if SpinBottom.Value < 0 then
  300. SpinBottom.Value := 0;
  301. if fg_FontPadding[3] <> SpinBottom.Value Then
  302. begin
  303. fg_FontPadding[3] := SpinBottom.Value;
  304. UpdateFont();
  305. end;
  306. Timer1.Enabled := True;
  307. end;
  308. procedure TForm1.ButtonSaveFontClick(Sender: TObject);
  309. var
  310. style : String;
  311. name : String;
  312. dir : String;
  313. begin
  314. if fg_FontBold and fg_FontItalic Then
  315. style := 'BoldItalic'
  316. else
  317. if fg_FontBold Then
  318. style := 'Bold'
  319. else
  320. if fg_FontItalic Then
  321. style := 'Italic'
  322. else
  323. style := 'Regular';
  324. Timer1.Enabled := False;
  325. SaveFontDialog.FileName := FontDialog.Font.Name + '-' + style + '-' + IntToStr(fg_FontSize) + 'pt';
  326. // if SaveFontDialog.Execute() Then
  327. // begin
  328. name := file_GetName(SaveFontDialog.FileName);
  329. dir := file_GetDirectory(SaveFontDialog.FileName);
  330. fontgen_SaveFont(fg_Font, name);
  331. // end;
  332. ShowMessage(name + ' save');
  333. Timer1.Enabled := True;
  334. end;
  335. procedure TForm1.CheckBoxAntialiasingChange(Sender: TObject);
  336. begin
  337. Timer1.Enabled := False;
  338. fg_FontAA := CheckBoxAntialiasing.Checked;
  339. UpdateFont();
  340. Timer1.Enabled := True;
  341. end;
  342. procedure TForm1.CheckBoxPackChange(Sender: TObject);
  343. begin
  344. Timer1.Enabled := False;
  345. fg_FontPack := CheckBoxPack.Checked;
  346. UpdateFont();
  347. Timer1.Enabled := True;
  348. end;
  349. procedure TForm1.ComboBoxPageSizeChange(Sender: TObject);
  350. begin
  351. Timer1.Enabled := False;
  352. fg_PageSize := StrToInt(ComboBoxPageSize.Items[ComboBoxPageSize.ItemIndex]);
  353. UpdateFont();
  354. Timer1.Enabled := True;
  355. end;
  356. procedure TForm1.FormActivate(Sender: TObject);
  357. begin
  358. if not zgl_Inited then
  359. begin
  360. zgl_Inited := True;
  361. zgl_Disable(APP_USE_LOG);
  362. zgl_Reg(SYS_LOAD, @Init);
  363. zgl_Reg(SYS_DRAW, @Draw);
  364. zgl_InitToHandle(Panel1.Handle);
  365. Timer1.Enabled := True;
  366. end;
  367. end;
  368. procedure TForm1.SpinCurrentPageChange(Sender: TObject);
  369. begin
  370. if SpinCurrentPage.Value < 1 then
  371. SpinCurrentPage.Value := 1;
  372. end;
  373. procedure TForm1.Timer1Timer(Sender: TObject);
  374. begin
  375. app_PLoop;
  376. end;
  377. end.