FontBoard.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529
  1. using ComPDFKit.PDFAnnotation;
  2. using PDF_Office.CustomControl.CompositeControl;
  3. using PDF_Office.Model.PropertyPanel.AnnotPanel;
  4. using PDFSettings;
  5. using Prism.Mvvm;
  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_Office.Model.AnnotPanel
  14. {
  15. //属性触发事件的标识
  16. public enum FontSetModeType
  17. {
  18. PresetFontStyes,
  19. FontFamilys,
  20. FontSizes,
  21. FontWeight_Style,
  22. FontColor,
  23. TextAlignment
  24. }
  25. //设置字体大小、字体内容排版、字体颜色、字体样式、字重
  26. public class FontBoard : BindableBase
  27. {
  28. #region 变量
  29. protected event EventHandler<FontSetModeType> ChangedValue;
  30. protected bool IsCanSave = false;
  31. public List<ComboDataItem> FontFamilyItems { get; protected set; }
  32. public List<ComboDataItem> FontStyleItems { get; protected set; }
  33. public List<ComboDataItem> PresetFontItems { get; protected set; }
  34. public List<PresetFontItem> PresetFontList = new List<PresetFontItem>();
  35. #endregion
  36. #region 初始化下拉框或列表默认的数据
  37. protected void InitBaseVariable()
  38. {
  39. InitBase_PresetFontStyles();
  40. InitBase_FontFamilys();
  41. InitBase_FontStyles();
  42. }
  43. //预设字体样式
  44. private void InitBase_PresetFontStyles()
  45. {
  46. PresetFontItems = new List<ComboDataItem>();
  47. PresetFontList = TextFont.GetCachePresetFontList();
  48. foreach (var item in PresetFontList)
  49. {
  50. ComboDataItem itemData = new ComboDataItem(item.mTag, item.mTagContent);
  51. PresetFontItems.Add(itemData);
  52. }
  53. }
  54. //字体
  55. private void InitBase_FontFamilys()
  56. {
  57. FontFamilyItems = TextFont.GetFamily();
  58. }
  59. //字重
  60. private void InitBase_FontStyles()
  61. {
  62. FontStyleItems = TextFont.GetFontStyle();
  63. }
  64. #endregion
  65. #region 属性
  66. private ComboDataItem _currentPresetFont;
  67. public ComboDataItem CurrentPresetFont
  68. {
  69. get { return _currentPresetFont; }
  70. set
  71. {
  72. bool isChange = IsEqualStrComboData(_currentPresetFont, value);
  73. SetProperty(ref _currentPresetFont, value);
  74. if (isChange)
  75. {
  76. ChangedValue?.Invoke(_currentPresetFont.ValueStr, FontSetModeType.PresetFontStyes);
  77. }
  78. }
  79. }
  80. #region 字体样式
  81. //下拉框列表
  82. private ComboDataItem _currentFontFamily;
  83. public ComboDataItem CurrentFontFamily
  84. {
  85. get { return _currentFontFamily; }
  86. set
  87. {
  88. bool isChange = IsEqualStrComboData(_currentFontFamily, value);
  89. SetProperty(ref _currentFontFamily, value);
  90. if (isChange)
  91. {
  92. ChangedValue?.Invoke(_currentFontFamily.ValueStr, FontSetModeType.FontFamilys);
  93. }
  94. }
  95. }
  96. #endregion
  97. #region 字体大小
  98. //下拉框列表:字体大小
  99. private ComboDataItem _currentFontSize = new ComboDataItem(6);
  100. public ComboDataItem CurrentFontSize
  101. {
  102. get { return _currentFontSize; }
  103. set
  104. {
  105. bool isChange = IsEqualComboData(_currentFontSize, value);
  106. if (isChange && value.Value > 0)
  107. SetProperty(ref _currentFontSize, value);
  108. if (isChange && value.Value > 0)
  109. {
  110. ChangedValue?.Invoke(_currentFontSize.Value, FontSetModeType.FontSizes);
  111. }
  112. }
  113. }
  114. #endregion
  115. //FontStyle & FontWeight
  116. private FontStyle _fontStyle;
  117. public FontStyle FontStyleItem
  118. {
  119. get { return _fontStyle; }
  120. set { SetProperty(ref _fontStyle, value); }
  121. }
  122. private FontWeight _fontWeight;
  123. public FontWeight FontWeightItem
  124. {
  125. get { return _fontWeight; }
  126. set { SetProperty(ref _fontWeight, value); }
  127. }
  128. private ComboDataItem _currrentFontWeightStyle;
  129. public ComboDataItem CurrrentFontWeightStyle
  130. {
  131. get { return _currrentFontWeightStyle; }
  132. set
  133. {
  134. bool isChange = IsEqualStrComboData(_currrentFontWeightStyle, value);
  135. SetProperty(ref _currrentFontWeightStyle, value);
  136. if (isChange)
  137. {
  138. ChangedValue?.Invoke(_currrentFontWeightStyle, FontSetModeType.FontWeight_Style);
  139. }
  140. }
  141. }
  142. protected void UpdateFontWeight_Style()
  143. {
  144. switch (CurrrentFontWeightStyle.ValueStr)
  145. {
  146. case "Regular":
  147. FontStyleItem = FontStyles.Normal;
  148. FontWeightItem = FontWeights.Normal;
  149. break;
  150. case "Bold":
  151. FontStyleItem = FontStyles.Normal;
  152. FontWeightItem = FontWeights.Bold;
  153. break;
  154. case "Italic":
  155. FontStyleItem = FontStyles.Italic;
  156. FontWeightItem = FontWeights.Normal;
  157. break;
  158. case "Bold Italic":
  159. FontStyleItem = FontStyles.Italic;
  160. FontWeightItem = FontWeights.Bold;
  161. break;
  162. }
  163. }
  164. private C_TEXT_ALIGNMENT _textAlignment;
  165. public C_TEXT_ALIGNMENT TextAlignmentItem
  166. {
  167. get { return _textAlignment; }
  168. set { SetProperty(ref _textAlignment, value); }
  169. }
  170. //颜色
  171. private Brush selectColor = new SolidColorBrush(Colors.Black);
  172. public Brush SelectColor
  173. {
  174. get { return selectColor; }
  175. set
  176. {
  177. SetProperty(ref selectColor, value);
  178. if (IsCanSave)
  179. {
  180. ChangedValue?.Invoke((selectColor as SolidColorBrush).Color, FontSetModeType.FontColor);
  181. }
  182. else
  183. {
  184. CurrentFontColor = selectColor;
  185. }
  186. }
  187. }
  188. private Brush _currentFontColor = new SolidColorBrush(Colors.Transparent);
  189. public Brush CurrentFontColor
  190. {
  191. get { return _currentFontColor; }
  192. set
  193. {
  194. SetProperty(ref _currentFontColor, value);
  195. }
  196. }
  197. private bool IsEqualComboData(ComboDataItem oldValue, ComboDataItem newValue)
  198. {
  199. if (newValue == null || IsCanSave == false)
  200. return false;
  201. if (oldValue != null && newValue != null)
  202. {
  203. if (oldValue.Value != newValue.Value)
  204. return true;
  205. }
  206. return false;
  207. }
  208. private bool IsEqualStrComboData(ComboDataItem oldValue, ComboDataItem newValue)
  209. {
  210. if (newValue == null || string.IsNullOrEmpty(newValue.ValueStr) == true || IsCanSave == false)
  211. return false;
  212. if (oldValue != null && newValue != null)
  213. {
  214. if (oldValue.ValueStr != newValue.ValueStr)
  215. return true;
  216. }
  217. return false;
  218. }
  219. #endregion
  220. #region 列表选中赋值
  221. protected void GetFontWeights_Style(FontStyle fontStyle, FontWeight fontWeights)
  222. {
  223. string strValue = "";
  224. string strContent = "";
  225. if (fontStyle == FontStyles.Normal)
  226. {
  227. if (fontWeights == FontWeights.Normal)
  228. {
  229. strValue = "Regular";
  230. strContent = "常规";
  231. }
  232. else
  233. {
  234. strValue = "Bold";
  235. strContent = "粗体";
  236. }
  237. }
  238. else
  239. {
  240. if (fontWeights == FontWeights.Normal)
  241. {
  242. strValue = "Italic";
  243. strContent = "斜体";
  244. }
  245. else
  246. {
  247. strValue = "Bold Italic";
  248. strContent = "粗斜体";
  249. }
  250. }
  251. CurrrentFontWeightStyle = new ComboDataItem(strValue, strContent);
  252. }
  253. protected void GetCurrentFontSize(int size)
  254. {
  255. CurrentFontSize = new ComboDataItem(size);
  256. }
  257. protected void GetCurrentFontFamily(string fontFamily, string uiStr)
  258. {
  259. CurrentFontFamily = new ComboDataItem(fontFamily, uiStr);
  260. }
  261. #endregion
  262. }
  263. //设置字体大小、字体内容排版、字体颜色、字体样式、字重
  264. public class FontBoardVm : BindableBase
  265. {
  266. #region 变量
  267. public List<ComboDataItem> FontFamilyItems { get; protected set; }
  268. public List<ComboDataItem> FontStyleItems { get; protected set; }
  269. public List<ComboDataItem> PresetFontItems { get; protected set; }
  270. public List<PresetFontItem> PresetFontList = new List<PresetFontItem>();
  271. public FontBoardVm(bool isInitdata)
  272. {
  273. if (isInitdata)
  274. InitBaseVariable();
  275. }
  276. #endregion
  277. #region 初始化下拉框或列表默认的数据
  278. protected void InitBaseVariable()
  279. {
  280. InitBase_PresetFontStyles();
  281. InitBase_FontFamilys();
  282. InitBase_FontStyles();
  283. }
  284. //预设字体样式
  285. private void InitBase_PresetFontStyles()
  286. {
  287. PresetFontItems = new List<ComboDataItem>();
  288. PresetFontList = TextFont.GetCachePresetFontList();
  289. foreach (var item in PresetFontList)
  290. {
  291. ComboDataItem itemData = new ComboDataItem(item.mTag, item.mTagContent);
  292. PresetFontItems.Add(itemData);
  293. }
  294. }
  295. //字体
  296. private void InitBase_FontFamilys()
  297. {
  298. FontFamilyItems = TextFont.GetFamily();
  299. }
  300. //字重
  301. private void InitBase_FontStyles()
  302. {
  303. FontStyleItems = TextFont.GetFontStyle();
  304. }
  305. #endregion
  306. #region 属性
  307. private ComboDataItem _currentPresetFont = new ComboDataItem("custom", "Custom");
  308. public ComboDataItem CurrentPresetFont
  309. {
  310. get { return _currentPresetFont; }
  311. set => SetProperty(ref _currentPresetFont, value);
  312. }
  313. #region 字体样式
  314. //下拉框列表
  315. private ComboDataItem _currentFontFamily = new ComboDataItem("Arial", "Helvetica");
  316. public ComboDataItem CurrentFontFamily
  317. {
  318. get { return _currentFontFamily; }
  319. set => SetProperty(ref _currentFontFamily, value);
  320. }
  321. #endregion
  322. #region 字体大小
  323. //下拉框列表:字体大小
  324. private ComboDataItem _currentFontSize = new ComboDataItem(6);
  325. public ComboDataItem CurrentFontSize
  326. {
  327. get { return _currentFontSize; }
  328. set => SetProperty(ref _currentFontSize, value);
  329. }
  330. #endregion
  331. //FontStyle & FontWeight
  332. private FontStyle _fontStyleItem;
  333. public FontStyle FontStyleItem
  334. {
  335. get { return _fontStyleItem; }
  336. set { SetProperty(ref _fontStyleItem, value); }
  337. }
  338. private FontWeight _fontWeight;
  339. public FontWeight FontWeightItem
  340. {
  341. get { return _fontWeight; }
  342. set { SetProperty(ref _fontWeight, value); }
  343. }
  344. private ComboDataItem _currrentFontWeightStyle = new ComboDataItem("Regular", "Regular");
  345. public ComboDataItem CurrrentFontWeightStyle
  346. {
  347. get { return _currrentFontWeightStyle; }
  348. set => SetProperty(ref _currrentFontWeightStyle, value);
  349. }
  350. public void UpdateFontWeight_Style()
  351. {
  352. switch (CurrrentFontWeightStyle.ValueStr)
  353. {
  354. case "Regular":
  355. FontStyleItem = FontStyles.Normal;
  356. FontWeightItem = FontWeights.Normal;
  357. break;
  358. case "Bold":
  359. FontStyleItem = FontStyles.Normal;
  360. FontWeightItem = FontWeights.Bold;
  361. break;
  362. case "Italic":
  363. FontStyleItem = FontStyles.Italic;
  364. FontWeightItem = FontWeights.Normal;
  365. break;
  366. case "Bold Italic":
  367. FontStyleItem = FontStyles.Italic;
  368. FontWeightItem = FontWeights.Bold;
  369. break;
  370. }
  371. }
  372. //文字内容对齐
  373. private string _strtextAlign;
  374. public string StrTextAlign
  375. {
  376. get { return _strtextAlign; }
  377. set { SetProperty(ref _strtextAlign, value); }
  378. }
  379. //颜色
  380. private Brush _fontColor = new SolidColorBrush(Colors.Black);
  381. public Brush FontColor
  382. {
  383. get { return _fontColor; }
  384. set { SetProperty(ref _fontColor, value); CurrentFontColor = _fontColor; }
  385. }
  386. private Brush _currentFontColor = new SolidColorBrush(Colors.Transparent);
  387. public Brush CurrentFontColor
  388. {
  389. get { return _currentFontColor; }
  390. set => SetProperty(ref _currentFontColor, value);
  391. }
  392. //外部UI引用,判断是否选中左对齐、居中对齐、右对齐,或都不选中
  393. public string strAglinState { get; private set; }
  394. //VM赋值
  395. public void SetStrAglinState(string str)
  396. {
  397. strAglinState = str;
  398. }
  399. #endregion
  400. #region 列表选中赋值
  401. public void GetFontWeights_Style(FontStyle fontStyle, FontWeight fontWeights)
  402. {
  403. string strValue = "";
  404. string strContent = "";
  405. if (fontStyle == FontStyles.Normal)
  406. {
  407. if (fontWeights == FontWeights.Normal)
  408. {
  409. strValue = "Regular";
  410. strContent = "常规";
  411. }
  412. else
  413. {
  414. strValue = "Bold";
  415. strContent = "粗体";
  416. }
  417. }
  418. else
  419. {
  420. if (fontWeights == FontWeights.Normal)
  421. {
  422. strValue = "Italic";
  423. strContent = "斜体";
  424. }
  425. else
  426. {
  427. strValue = "Bold Italic";
  428. strContent = "粗斜体";
  429. }
  430. }
  431. CurrrentFontWeightStyle = new ComboDataItem(strValue, strContent);
  432. }
  433. public void GetCurrentFontSize(int size)
  434. {
  435. CurrentFontSize = new ComboDataItem(size);
  436. }
  437. public void GetCurrentFontFamily(string fontFamily, string uiStr)
  438. {
  439. CurrentFontFamily = new ComboDataItem(fontFamily, uiStr);
  440. }
  441. #endregion
  442. }
  443. }