FontStyleItem.cs 17 KB

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