fmFontDialog.pas 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424
  1. // ==============================================================================
  2. // 文本显示格式设置对话框
  3. // ver 1.1 by gale 2024-12-05
  4. // ==============================================================================
  5. unit fmFontDialog;
  6. interface
  7. uses
  8. {$IFDEF MACOS}
  9. MacApi.Appkit, MacApi.CoreFoundation, MacApi.Foundation,
  10. {$ENDIF}
  11. {$IFDEF MSWINDOWS}
  12. Winapi.Messages, Winapi.Windows,
  13. {$ENDIF}
  14. System.Rtti, System.TypInfo,
  15. System.SysUtils, System.Types, System.UITypes, System.Classes, System.Variants,
  16. FMX.Types, FMX.Controls, FMX.Forms, FMX.Graphics, FMX.Dialogs, FMX.Layouts,
  17. FMX.ListBox, FMX.Edit, FMX.Controls.Presentation, FMX.StdCtrls, FMX.Colors,
  18. FMX.Objects, System.UIConsts, FMX.Ani;
  19. type
  20. TFontDialog = class(TForm)
  21. grp1: TGroupBox;
  22. chkBold: TCheckBox;
  23. chkItalic: TCheckBox;
  24. chkUnderline: TCheckBox;
  25. chkStrikeOut: TCheckBox;
  26. Label1: TLabel;
  27. edtFontSize: TEdit;
  28. lstFontSize: TListBox;
  29. Button1: TButton;
  30. btnCancel: TButton;
  31. grp2: TGroupBox;
  32. txtDemo: TText;
  33. GroupBox1: TGroupBox;
  34. cbbHorzAlign: TComboBox;
  35. cbbVertAlign: TComboBox;
  36. Label2: TLabel;
  37. Label3: TLabel;
  38. Label4: TLabel;
  39. cbbTrimming: TComboBox;
  40. edtFontColor: TEdit;
  41. cpnlFontColor: TColorPanel;
  42. chkWordWrap: TCheckBox;
  43. edtFontName: TEdit;
  44. lstFontName: TListBox;
  45. Label5: TLabel;
  46. rctTitleBar: TRectangle;
  47. rctTitleBarForMove: TRectangle;
  48. Layout1: TLayout;
  49. rctBtnClose: TRectangle;
  50. aniForCloseBtn: TColorAnimation;
  51. pathCloseBtn: TPath;
  52. lblCaption: TLabel;
  53. Path4: TPath;
  54. pnlSizeBorder: TPanel;
  55. procedure lstFontSizeChange(Sender: TObject);
  56. procedure edtFontSizeChange(Sender: TObject);
  57. procedure lstFontNameChange(Sender: TObject);
  58. procedure edtFontNameChange(Sender: TObject);
  59. procedure cpnlFontColorChange(Sender: TObject);
  60. procedure edtFontColorChange(Sender: TObject);
  61. procedure chkFontStyleChange(Sender: TObject);
  62. procedure cbbTextAlignChange(Sender: TObject);
  63. procedure chkWordWrapChange(Sender: TObject);
  64. procedure FormCreate(Sender: TObject);
  65. procedure rctBtnCloseClick(Sender: TObject);
  66. private
  67. FTextSettings: TTextSettings;
  68. FOldTextSettings: TTextSettings;
  69. public
  70. type
  71. TPropPart = (peFamily, peSize, peColor, peBold, peItalic, peUnderLine, peStrikeOut, peHorzAlign, peVertAlign, peTrimming, peWordWarp);
  72. TPropParts = set of TPropPart;
  73. public
  74. constructor Create(AOwner: TComponent); override;
  75. destructor Destroy; override;
  76. function ShowModal(ATextSettings: TTextSettings; ADisProps: TPropParts = []): TModalResult; overload;
  77. end;
  78. TTypeStr = class
  79. public
  80. class function SetToStr<T>(AValue: T): string;
  81. class function StrToSet<T>(AValue: string): T;
  82. class function EnumToStr<T>(AValue: T): string;
  83. class function StrToEnum<T>(AValue: string): T;
  84. end;
  85. var
  86. FontDialog: TFontDialog;
  87. implementation
  88. uses WinSizeUtil;
  89. {$IFDEF MSWINDOWS }
  90. function EnumFontsProc(var LogFont: TLogFont; var TextMetric: TTextMetric;
  91. FontType: Integer; Data: Pointer): Integer;
  92. stdcall;
  93. var
  94. S: TStrings;
  95. Temp: string;
  96. begin
  97. S := TStrings(Data);
  98. Temp := LogFont.lfFaceName;
  99. if (S.Count = 0) or (AnsiCompareText(S[S.Count - 1], Temp) <> 0) then
  100. begin
  101. if Temp[1] <> '@' then
  102. begin
  103. S.Add(Temp);
  104. end;
  105. end;
  106. Result := 1;
  107. end;
  108. {$ENDIF}
  109. procedure CollectFontList(FontList: TStrings);
  110. var
  111. strs: TStringList;
  112. var
  113. {$IFDEF MACOS}
  114. fManager: NsFontManager;
  115. list: NSArray;
  116. lItem: NSString;
  117. i: Integer;
  118. {$ENDIF}
  119. {$IFDEF MSWINDOWS}
  120. DC: HDC;
  121. LFont: TLogFont;
  122. {$ENDIF}
  123. begin
  124. {$IFDEF MACOS}
  125. fManager := TNsFontManager.Wrap(TNsFontManager.OCClass.sharedFontManager);
  126. list := fManager.availableFontFamilies;
  127. if (list <> nil) and (list.Count > 0) then
  128. begin
  129. for i := 0 to list.Count - 1 do
  130. begin
  131. lItem := TNSString.Wrap(list.objectAtIndex(i));
  132. FontList.Add(String(lItem.UTF8String))
  133. end;
  134. end;
  135. {$ENDIF}
  136. {$IFDEF MSWINDOWS}
  137. DC := GetDC(0);
  138. FillChar(LFont, sizeof(LFont), 0);
  139. LFont.lfCharset := DEFAULT_CHARSET;
  140. EnumFontFamiliesEx(DC, LFont, @EnumFontsProc, Winapi.Windows.LPARAM(FontList), 0);
  141. ReleaseDC(0, DC);
  142. {$ENDIF}
  143. strs := TStringList.Create;
  144. try
  145. strs.Text := FontList.Text;
  146. strs.Sort;
  147. FontList.Text := strs.Text;
  148. finally
  149. strs.Free;
  150. end;
  151. end;
  152. {$R *.fmx}
  153. class function TTypeStr.SetToStr<T>(AValue: T): string;
  154. begin
  155. Result := SetToString(PTypeInfo(TypeInfo(T)), @AValue, true);
  156. end;
  157. class function TTypeStr.StrToSet<T>(AValue: string): T;
  158. begin
  159. StringToSet(PTypeInfo(TypeInfo(T)), AValue, @Result);
  160. end;
  161. class function TTypeStr.EnumToStr<T>(AValue: T): string;
  162. begin
  163. Result := GetEnumName(TypeInfo(T), PByte(@AValue)^);
  164. end;
  165. class function TTypeStr.StrToEnum<T>(AValue: string): T;
  166. begin
  167. if sizeof(T) = 4 then
  168. begin
  169. PInteger(@Result)^ := Integer(GetEnumValue(TypeInfo(T), AValue));
  170. if PInteger(@Result)^ = 255 then
  171. begin
  172. PInteger(@Result)^ := 0;
  173. end;
  174. end
  175. else if sizeof(T) = 2 then
  176. begin
  177. PWord(@Result)^ := Word(GetEnumValue(TypeInfo(T), AValue));
  178. if PWord(@Result)^ = 255 then
  179. begin
  180. PWord(@Result)^ := 0;
  181. end;
  182. end
  183. else
  184. begin
  185. PByte(@Result)^ := Byte(GetEnumValue(TypeInfo(T), AValue));
  186. if PByte(@Result)^ = 255 then
  187. begin
  188. PByte(@Result)^ := 0;
  189. end;
  190. end;
  191. end;
  192. // ==============================================================================
  193. // 显示对话框
  194. // ==============================================================================
  195. function TFontDialog.ShowModal(ATextSettings: TTextSettings; ADisProps: TPropParts = []): TModalResult;
  196. begin
  197. FTextSettings := ATextSettings;
  198. FOldTextSettings.Assign(FTextSettings);
  199. txtDemo.TextSettings.Assign(ATextSettings);
  200. chkBold.IsChecked := TFontstyle.fsBold in txtDemo.TextSettings.Font.Style;
  201. chkItalic.IsChecked := TFontstyle.fsItalic in txtDemo.TextSettings.Font.Style;
  202. chkUnderline.IsChecked := TFontstyle.fsUnderline in txtDemo.TextSettings.Font.Style;
  203. chkStrikeOut.IsChecked := TFontstyle.fsStrikeOut in txtDemo.TextSettings.Font.Style;
  204. cbbHorzAlign.ItemIndex := cbbHorzAlign.Items.IndexOf(TTypeStr.EnumToStr<TTextAlign>(txtDemo.TextSettings.HorzAlign));
  205. cbbVertAlign.ItemIndex := cbbVertAlign.Items.IndexOf(TTypeStr.EnumToStr<TTextAlign>(txtDemo.TextSettings.VertAlign));
  206. cbbTrimming.ItemIndex := cbbTrimming.Items.IndexOf(TTypeStr.EnumToStr<TTextTrimming>(txtDemo.TextSettings.Trimming));
  207. chkWordWrap.IsChecked := txtDemo.TextSettings.WordWrap;
  208. edtFontName.Text := txtDemo.TextSettings.Font.Family;
  209. lstFontName.ItemIndex := lstFontName.Items.IndexOf(edtFontName.Text);
  210. edtFontSize.Text := txtDemo.TextSettings.Font.Size.ToString;
  211. lstFontSize.ItemIndex := lstFontSize.Items.IndexOf(edtFontSize.Text);
  212. cpnlFontColor.Color := txtDemo.TextSettings.FontColor;
  213. Result := ShowModal;
  214. if Result = mrOK then
  215. begin
  216. FTextSettings.Assign(txtDemo.TextSettings);
  217. end
  218. else
  219. begin
  220. FTextSettings.Assign(FOldTextSettings);
  221. end;
  222. end;
  223. // ==============================================================================
  224. // 对齐数据修改
  225. // ==============================================================================
  226. procedure TFontDialog.cbbTextAlignChange(Sender: TObject);
  227. begin
  228. if txtDemo.Locked then
  229. Exit;
  230. if Sender = cbbHorzAlign then
  231. begin
  232. if TComboBox(Sender).Selected <> nil then
  233. begin
  234. txtDemo.TextSettings.HorzAlign := TTypeStr.StrToEnum<TTextAlign>(TComboBox(Sender).Selected.Text);
  235. end;
  236. end
  237. else if Sender = cbbVertAlign then
  238. begin
  239. if TComboBox(Sender).Selected <> nil then
  240. begin
  241. txtDemo.TextSettings.VertAlign := TTypeStr.StrToEnum<TTextAlign>(TComboBox(Sender).Selected.Text);
  242. end;
  243. end
  244. else if Sender = cbbTrimming then
  245. begin
  246. if TComboBox(Sender).Selected <> nil then
  247. begin
  248. txtDemo.TextSettings.Trimming := TTypeStr.StrToEnum<TTextTrimming>(TComboBox(Sender).Selected.Text);
  249. end;
  250. end;
  251. end;
  252. // ==============================================================================
  253. // 字体风格数据修改
  254. // ==============================================================================
  255. procedure TFontDialog.chkFontStyleChange(Sender: TObject);
  256. begin
  257. if txtDemo.Locked then
  258. Exit;
  259. txtDemo.TextSettings.Font.Style := [];
  260. if chkBold.IsChecked then
  261. txtDemo.TextSettings.Font.Style := txtDemo.TextSettings.Font.Style + [TFontstyle.fsBold];
  262. if chkItalic.IsChecked then
  263. txtDemo.TextSettings.Font.Style := txtDemo.TextSettings.Font.Style + [TFontstyle.fsItalic];
  264. if chkUnderline.IsChecked then
  265. txtDemo.TextSettings.Font.Style := txtDemo.TextSettings.Font.Style + [TFontstyle.fsUnderline];
  266. if chkStrikeOut.IsChecked then
  267. txtDemo.TextSettings.Font.Style := txtDemo.TextSettings.Font.Style + [TFontstyle.fsStrikeOut];
  268. end;
  269. procedure TFontDialog.chkWordWrapChange(Sender: TObject);
  270. begin
  271. txtDemo.TextSettings.WordWrap := chkWordWrap.IsChecked;
  272. FTextSettings.Assign(txtDemo.TextSettings);
  273. end;
  274. // ==============================================================================
  275. // 颜色选择修改
  276. // ==============================================================================
  277. procedure TFontDialog.cpnlFontColorChange(Sender: TObject);
  278. begin
  279. edtFontColor.Text := AlphaColorToString(cpnlFontColor.Color);
  280. end;
  281. constructor TFontDialog.Create(AOwner: TComponent);
  282. begin
  283. inherited;
  284. FOldTextSettings := TTextSettings.Create(nil);
  285. end;
  286. destructor TFontDialog.Destroy;
  287. begin
  288. FOldTextSettings.Free;
  289. inherited;
  290. end;
  291. // ==============================================================================
  292. // 颜色输入框修改
  293. // ==============================================================================
  294. procedure TFontDialog.edtFontColorChange(Sender: TObject);
  295. begin
  296. if txtDemo.Locked then
  297. Exit;
  298. try
  299. cpnlFontColor.Color := StringToAlphaColor(edtFontColor.Text);
  300. txtDemo.TextSettings.FontColor := cpnlFontColor.Color;
  301. FTextSettings.Assign(txtDemo.TextSettings);
  302. except
  303. edtFontColor.Text := AlphaColorToString(cpnlFontColor.Color);
  304. edtFontColor.SelectAll;
  305. end;
  306. end;
  307. // ==============================================================================
  308. // 字体列表框修改
  309. // ==============================================================================
  310. procedure TFontDialog.lstFontNameChange(Sender: TObject);
  311. begin
  312. if lstFontName.Selected <> nil then
  313. edtFontName.Text := lstFontName.Selected.Text;
  314. end;
  315. // ==============================================================================
  316. // 文字输入框修改
  317. // ==============================================================================
  318. procedure TFontDialog.edtFontNameChange(Sender: TObject);
  319. begin
  320. if txtDemo.Locked then
  321. Exit;
  322. txtDemo.TextSettings.Font.Family := edtFontName.Text;
  323. lstFontName.ItemIndex := lstFontName.Items.IndexOf(edtFontName.Text);
  324. FTextSettings.Assign(txtDemo.TextSettings);
  325. end;
  326. // ==============================================================================
  327. // 文字大小列表框修改
  328. // ==============================================================================
  329. procedure TFontDialog.lstFontSizeChange(Sender: TObject);
  330. begin
  331. if lstFontSize.Selected <> nil then
  332. edtFontSize.Text := lstFontSize.Selected.Text;
  333. end;
  334. procedure TFontDialog.rctBtnCloseClick(Sender: TObject);
  335. begin
  336. Close;
  337. end;
  338. // ==============================================================================
  339. // 文字大小输入修改
  340. // ==============================================================================
  341. procedure TFontDialog.edtFontSizeChange(Sender: TObject);
  342. var
  343. fTemp: Single;
  344. begin
  345. if txtDemo.Locked then
  346. Exit;
  347. if TryStrToFloat(edtFontSize.Text, fTemp) then
  348. begin
  349. txtDemo.TextSettings.Font.Size := fTemp;
  350. lstFontSize.ItemIndex := -1;
  351. lstFontSize.ItemIndex := lstFontSize.Items.IndexOf(edtFontSize.Text);
  352. FTextSettings.Assign(txtDemo.TextSettings);
  353. end;
  354. end;
  355. // ==============================================================================
  356. // 窗口显示
  357. // ==============================================================================
  358. procedure TFontDialog.FormCreate(Sender: TObject);
  359. var
  360. LFonts: TStrings;
  361. begin
  362. var
  363. FWinSizeHelper := TWinSizeHelper.Create(self);
  364. FWinSizeHelper.SetTitleBar(rctTitleBarForMove);
  365. LFonts := TStringList.Create;
  366. try
  367. CollectFontList(LFonts);
  368. lstFontName.Items.AddStrings(LFonts);
  369. finally
  370. LFonts.Free;
  371. end;
  372. for var i := 0 to lstFontName.Items.Count - 1 do
  373. begin
  374. lstFontName.ItemByIndex(i).StyledSettings := [TStyledSetting.Size, TStyledSetting.Style, TStyledSetting.FontColor, TStyledSetting.Other];
  375. lstFontName.ItemByIndex(i).TextSettings.Font.Family := lstFontName.ItemByIndex(i).Text;
  376. end
  377. end;
  378. end.