FontStyleItem.cs 16 KB

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