FontStyleItem.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439
  1. using ImTools;
  2. using PDF_Master.CustomControl.CompositeControl;
  3. using PDF_Master.Helper;
  4. using PDF_Master.Model.AnnotPanel;
  5. using PDF_Master.Properties;
  6. using PDFSettings;
  7. using System;
  8. using System.Collections.Generic;
  9. using System.Globalization;
  10. using System.Linq;
  11. using System.Text;
  12. using System.Threading.Tasks;
  13. using System.Windows;
  14. using System.Windows.Controls;
  15. using System.Windows.Media;
  16. namespace PDF_Master.Model.PropertyPanel.AnnotPanel
  17. {
  18. public class TextFont
  19. {
  20. //获取本地缓存数据
  21. public static List<PresetFontItem> GetCachePresetFontList()
  22. {
  23. List<PresetFontItem> cacheTempList = new List<PresetFontItem>();
  24. if (Settings.Default.PresetFontList == null)
  25. Settings.Default.PresetFontList = new PresetFontList();
  26. if (Settings.Default.PreinstallFontList == null)
  27. Settings.Default.PreinstallFontList = new PreinstallFontList();
  28. if (Settings.Default.PreinstallFontList.Count == 0/*Settings.Default.PresetFontList.Count == 0*/ )
  29. {
  30. cacheTempList = GetPresetFontStyle();
  31. foreach (var cacheItem in cacheTempList)
  32. {
  33. var newItem = new PresetFontItem();
  34. newItem.mTag = cacheItem.mTag;
  35. newItem.mTagContent = cacheItem.mTagContent;
  36. newItem.mFontStyle = cacheItem.mFontStyle;
  37. newItem.mFontWeight = cacheItem.mFontWeight;
  38. newItem.mFontSize = cacheItem.mFontSize;
  39. newItem.mFontFamily = cacheItem.mFontFamily;
  40. Settings.Default.PresetFontList.Add(newItem);
  41. //复杂数据类型 , 不可本地缓存到配置文件,需要转换一下(先 string保存,再根据类型缓存)
  42. var newItem1 = new PreinstallItem();
  43. newItem1.mTag = cacheItem.mTag;
  44. newItem1.mTagContent = cacheItem.mTagContent;
  45. newItem1.mFontStyle = cacheItem.mFontStyle.ToString();
  46. newItem1.mFontWeight = cacheItem.mFontWeight.ToString();
  47. newItem1.mFontSize = cacheItem.mFontSize;
  48. newItem1.mFontFamily = cacheItem.mFontFamily.Source;
  49. Settings.Default.PreinstallFontList.Add(newItem1);
  50. }
  51. Settings.Default.Save();
  52. }
  53. else
  54. {
  55. //根据 对应的字段 内容 ,对照相对应的类型
  56. foreach (var item in Settings.Default.PreinstallFontList)
  57. {
  58. var newItem = new PresetFontItem();
  59. newItem.mTag = item.mTag;
  60. newItem.mTagContent = item.mTagContent;
  61. switch (item.mFontStyle)
  62. {
  63. case "Normal":
  64. newItem.mFontStyle = FontStyles.Normal;
  65. break;
  66. case "Italic":
  67. newItem.mFontStyle = FontStyles.Italic;
  68. break;
  69. case "Oblique":
  70. newItem.mFontStyle = FontStyles.Oblique;
  71. break;
  72. }
  73. switch (item.mFontWeight)
  74. {
  75. case "Bold":
  76. newItem.mFontWeight = FontWeights.Bold;
  77. break;
  78. case "Normal":
  79. newItem.mFontWeight = FontWeights.Normal;
  80. break;
  81. }
  82. newItem.mFontSize = item.mFontSize;
  83. newItem.mFontFamily = new FontFamily(item.mFontFamily);
  84. cacheTempList.Add(newItem);
  85. }
  86. //foreach (var item in Settings.Default.PresetFontList)
  87. //{
  88. // var newItem = new PresetFontItem();
  89. // newItem.mTag = item.mTag;
  90. // newItem.mTagContent = item.mTagContent;
  91. // newItem.mFontStyle = item.mFontStyle;
  92. // newItem.mFontWeight = item.mFontWeight;
  93. // newItem.mFontSize = item.mFontSize;
  94. // newItem.mFontFamily = item.mFontFamily;
  95. // cacheTempList.Add(newItem);
  96. //}
  97. }
  98. return cacheTempList;
  99. }
  100. //保存到本地缓存数据
  101. public static void SavePresetFontList(List<PresetFontItem> list)
  102. {
  103. if (list == null) return;
  104. if (Settings.Default.PresetFontList == null)
  105. Settings.Default.PresetFontList = new PresetFontList();
  106. if (Settings.Default.PreinstallFontList == null)
  107. Settings.Default.PreinstallFontList = new PreinstallFontList();
  108. bool isCanSave = false;
  109. List<PresetFontItem> TempLists = new List<PresetFontItem>();
  110. foreach (var item in list)
  111. {
  112. //根据预设标题 ,找到 相对应数据
  113. var cacheItem = Settings.Default.PreinstallFontList.FirstOrDefault(temp => temp.mTag == item.mTag);
  114. if (cacheItem != null)
  115. {
  116. //比对数据,如果不一样 ,更新数据
  117. if (cacheItem.mFontFamily != item.mFontFamily.Source ||
  118. cacheItem.mFontSize != item.mFontSize ||
  119. cacheItem.mFontStyle != item.mFontStyle.ToString() ||
  120. cacheItem.mFontWeight != item.mFontWeight.ToString()
  121. )
  122. {
  123. isCanSave = true;
  124. var index = Settings.Default.PreinstallFontList.FindIndex(temp => temp.mTag == cacheItem.mTag);
  125. if (index != -1)
  126. {
  127. Settings.Default.PreinstallFontList.Remove(cacheItem);
  128. var newItem1 = new PreinstallItem();
  129. newItem1.mTag = item.mTag;
  130. newItem1.mTagContent = item.mTagContent;
  131. newItem1.mFontStyle = item.mFontStyle.ToString();
  132. newItem1.mFontWeight = item.mFontWeight.ToString();
  133. newItem1.mFontSize = item.mFontSize;
  134. newItem1.mFontFamily = item.mFontFamily.Source;
  135. Settings.Default.PreinstallFontList.Insert(index, newItem1);
  136. }
  137. //cacheItem.mFontFamily = new FontFamily(item.mFontFamily.Source);
  138. //cacheItem.mFontSize = item.mFontSize;
  139. //cacheItem.mFontStyle = item.mFontStyle;
  140. //cacheItem.mFontWeight = item.mFontWeight;
  141. break;
  142. }
  143. }
  144. //else
  145. //{
  146. // TempLists.Add(item);
  147. //}
  148. }
  149. //foreach (var itemTemp in TempLists)
  150. //{
  151. // Settings.Default.PresetFontList.Add(itemTemp);
  152. //}
  153. if (isCanSave)
  154. Settings.Default.Save();
  155. }
  156. public static void BackDefaultPresetFontStyle(string tag)
  157. {
  158. bool isCanSave = false;
  159. var list = GetPresetFontStyle();
  160. var itemDefault = list.FirstOrDefault(temp => temp.mTag == tag);
  161. if (Settings.Default.PresetFontList == null)
  162. Settings.Default.PresetFontList = new PresetFontList();
  163. if (Settings.Default.PresetFontList.Count == 0)
  164. {
  165. Settings.Default.PresetFontList.Add(itemDefault);
  166. isCanSave = true;
  167. }
  168. else
  169. {
  170. foreach (var item in Settings.Default.PresetFontList)
  171. {
  172. if (item.mTag == itemDefault.mTag)
  173. {
  174. item.mTagContent = itemDefault.mTagContent;
  175. item.mFontFamily = itemDefault.mFontFamily;
  176. item.mFontWeight = itemDefault.mFontWeight;
  177. item.mFontStyle = itemDefault.mFontStyle;
  178. item.mFontSize = itemDefault.mFontSize;
  179. isCanSave = true;
  180. break;
  181. }
  182. }
  183. }
  184. if (isCanSave == true)
  185. Settings.Default.Save();
  186. }
  187. //获取拟定的预设样式
  188. public static List<PresetFontItem> GetPresetFontStyle()
  189. {
  190. List<PresetFontItem> fontStyleList = new List<PresetFontItem>();
  191. PresetFontItem custom = new PresetFontItem();
  192. custom.mTag = "Custom";
  193. custom.mTagContent = App.MainPageLoader.GetString("PresetTextStyle_Customize");
  194. custom.mFontSize = 24;
  195. custom.mFontFamily = new FontFamily("Arial");
  196. custom.mFontStyle = FontStyles.Normal;
  197. custom.mFontWeight = FontWeights.Normal;
  198. PresetFontItem h1 = new PresetFontItem();
  199. h1.mTag = "H1";
  200. h1.mTagContent = App.MainPageLoader.GetString("PresetTextStyle_H1Title");
  201. h1.mFontSize = 36;
  202. h1.mFontFamily = new FontFamily("Arial");
  203. h1.mFontStyle = FontStyles.Normal;
  204. h1.mFontWeight = FontWeights.Bold;
  205. PresetFontItem h2 = new PresetFontItem();
  206. h2.mTag = "H2";
  207. h2.mTagContent = App.MainPageLoader.GetString("PresetTextStyle_H2Title");
  208. h2.mFontSize = 24;
  209. h2.mFontFamily = new FontFamily("Arial");
  210. h2.mFontStyle = FontStyles.Normal;
  211. h2.mFontWeight = FontWeights.Bold;
  212. PresetFontItem h3 = new PresetFontItem();
  213. h3.mTag = "H3";
  214. h3.mTagContent = App.MainPageLoader.GetString("PresetTextStyle_H3Title");
  215. h3.mFontSize = 18;
  216. h3.mFontFamily = new FontFamily("Arial");
  217. h3.mFontStyle = FontStyles.Normal;
  218. h3.mFontWeight = FontWeights.Bold;
  219. PresetFontItem b1 = new PresetFontItem();
  220. b1.mTag = "B1";
  221. b1.mTagContent = App.MainPageLoader.GetString("PresetTextStyle_B1NormalTextStandard");
  222. b1.mFontSize = 14;
  223. b1.mFontFamily = new FontFamily("Arial");
  224. b1.mFontStyle = FontStyles.Normal;
  225. b1.mFontWeight = FontWeights.Regular;
  226. PresetFontItem b2 = new PresetFontItem();
  227. b2.mTag = "B2";
  228. b2.mTagContent = App.MainPageLoader.GetString("PresetTextStyle_B2NormalTextSmall");
  229. b2.mFontSize = 12;
  230. b2.mFontFamily = new FontFamily("Arial");
  231. b2.mFontStyle = FontStyles.Normal;
  232. b2.mFontWeight = FontWeights.Regular;
  233. PresetFontItem b3 = new PresetFontItem();
  234. b3.mTag = "B3";
  235. b3.mTagContent = App.MainPageLoader.GetString("PresetTextStyle_B3Description");
  236. b3.mFontSize = 11;
  237. b3.mFontFamily = new FontFamily("Arial");
  238. b3.mFontStyle = FontStyles.Normal;
  239. b3.mFontWeight = FontWeights.Regular;
  240. fontStyleList.Add(custom);
  241. fontStyleList.Add(h1);
  242. fontStyleList.Add(h2);
  243. fontStyleList.Add(h3);
  244. fontStyleList.Add(b1);
  245. fontStyleList.Add(b2);
  246. fontStyleList.Add(b3);
  247. return fontStyleList;
  248. }
  249. public static List<ComboDataItem> GetFontStyle()
  250. {
  251. var FontStyleItems = new List<ComboDataItem>();
  252. ComboDataItem item = new ComboDataItem("Regular", "Regular");
  253. FontStyleItems.Add(item);
  254. item = new ComboDataItem("Bold", "Bold");
  255. FontStyleItems.Add(item);
  256. item = new ComboDataItem("Italic", "Italic");
  257. FontStyleItems.Add(item);
  258. item = new ComboDataItem("Bold Italic", "Bold Italic");
  259. FontStyleItems.Add(item);
  260. return FontStyleItems;
  261. }
  262. internal static List<ComboDataItem> GetFontSize()
  263. {
  264. var FontSizeItems = new List<ComboDataItem>();
  265. ComboDataItem item = new ComboDataItem(8);
  266. FontSizeItems.Add(item);
  267. item = new ComboDataItem(9);
  268. FontSizeItems.Add(item);
  269. item = new ComboDataItem(10);
  270. FontSizeItems.Add(item);
  271. item = new ComboDataItem(11);
  272. FontSizeItems.Add(item);
  273. item = new ComboDataItem(12);
  274. FontSizeItems.Add(item);
  275. item = new ComboDataItem(14);
  276. FontSizeItems.Add(item);
  277. item = new ComboDataItem(16);
  278. FontSizeItems.Add(item);
  279. item = new ComboDataItem(18);
  280. FontSizeItems.Add(item);
  281. item = new ComboDataItem(20);
  282. FontSizeItems.Add(item);
  283. item = new ComboDataItem(22);
  284. FontSizeItems.Add(item);
  285. item = new ComboDataItem(24);
  286. FontSizeItems.Add(item);
  287. item = new ComboDataItem(26);
  288. FontSizeItems.Add(item);
  289. item = new ComboDataItem(28);
  290. FontSizeItems.Add(item);
  291. item = new ComboDataItem(36);
  292. FontSizeItems.Add(item);
  293. item = new ComboDataItem(48);
  294. FontSizeItems.Add(item);
  295. item = new ComboDataItem(72);
  296. FontSizeItems.Add(item);
  297. return FontSizeItems;
  298. }
  299. public static List<ComboDataItem> GetFamily()
  300. {
  301. var FontFamilyItems = new List<ComboDataItem>();
  302. ComboDataItem item = new ComboDataItem("Courier", "Courier New");
  303. FontFamilyItems.Add(item);
  304. item = new ComboDataItem("Arial", "Arial");
  305. FontFamilyItems.Add(item);
  306. item = new ComboDataItem(/*"Times-Roman"*/"Times", "Times New Roman");
  307. FontFamilyItems.Add(item);
  308. return FontFamilyItems;
  309. }
  310. public static List<ComboDataItem> GetFamilyEdit()
  311. {
  312. System.Drawing.Text.InstalledFontCollection objFont = new System.Drawing.Text.InstalledFontCollection();
  313. var FontFamilyItems = new List<ComboDataItem>();
  314. ComboDataItem item;
  315. foreach (var itemFam in EditHelper.GetFontFamily())
  316. {
  317. string content = itemFam;
  318. if (itemFam.Equals("Informal"))
  319. {
  320. content = "Informal Roman";
  321. }
  322. //屏蔽 SDK暂时无效的字体
  323. if (itemFam.Equals("Marlett") || itemFam.Equals("MT Extra")
  324. || itemFam.Equals("Symbol") || itemFam.Equals("HoloLens MDL2 Assets")
  325. || itemFam.Equals("Wingdings") || itemFam.Equals("Wingdings 2")
  326. || itemFam.Equals("Wingdings 3") || itemFam.Equals("Webdings")
  327. || itemFam.Equals("ZWAdobeF") || itemFam.Equals("Algerian")
  328. || itemFam.Equals("Bookshelf Symbol 7") || itemFam.Equals("Castellar")
  329. || itemFam.Equals("MS Outlook") || itemFam.Equals("MS Reference Specialty")
  330. || itemFam.Equals("Segoe Fluent Icons") || itemFam.Equals("Segoe MDL2 Assets")
  331. || itemFam.Equals("Stencil"))
  332. {
  333. continue;
  334. }
  335. item = new ComboDataItem(DeleteCharacters(itemFam), content);
  336. item.FontFamily = new FontFamily(content);
  337. FontFamilyItems.Add(item);
  338. }
  339. return FontFamilyItems;
  340. }
  341. public static string DeleteCharacters(string str)
  342. {
  343. str = str.Replace(" ", string.Empty);
  344. str = str.Replace("-", string.Empty);
  345. str = str.Replace("_", string.Empty);
  346. return str;
  347. }
  348. public static List<ComboDataItem> GetDateFormats()
  349. {
  350. var dateFormatItems = new List<ComboDataItem>();
  351. foreach (var itemFormat in GetTimesFormats())
  352. {
  353. ComboDataItem item = new ComboDataItem(itemFormat, "format");
  354. dateFormatItems.Add(item);
  355. }
  356. return dateFormatItems;
  357. }
  358. private static List<string> GetTimesFormats()
  359. {
  360. List<string> TimesItems = new List<string>();
  361. TimesItems.Add("yyyy年M月d日");
  362. TimesItems.Add("M/d");
  363. TimesItems.Add("M/d/yy");
  364. TimesItems.Add("M/d/yyyy");
  365. TimesItems.Add("MM/dd/yy");
  366. TimesItems.Add("MM/dd/yyyy");
  367. TimesItems.Add("d/M/yy");
  368. TimesItems.Add("d/M/yyyy");
  369. TimesItems.Add("dd/MM/yy");
  370. TimesItems.Add("dd/MM/yyyy");
  371. TimesItems.Add("MM/yy");
  372. TimesItems.Add("MM/yyyy");
  373. TimesItems.Add("M.d.yy");
  374. TimesItems.Add("M.d.yyyy");
  375. TimesItems.Add("MM.dd.yy");
  376. TimesItems.Add("MM.dd.yyyy");
  377. TimesItems.Add("MM.yy");
  378. TimesItems.Add("MM.yyyy");
  379. TimesItems.Add("d.M.yy");
  380. TimesItems.Add("d.M.yyyy");
  381. TimesItems.Add("dd.MM.yy");
  382. TimesItems.Add("dd.MM.yyyy");
  383. TimesItems.Add("yy-MM-dd");
  384. TimesItems.Add("yyyy-MM-dd");
  385. return TimesItems;
  386. }
  387. }
  388. }