FontStyleItem.cs 16 KB

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