FontBoard.cs 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691
  1. using ComPDFKit.PDFAnnotation;
  2. using ComPDFKitViewer.AnnotEvent;
  3. using PDF_Master.CustomControl.CompositeControl;
  4. using PDF_Master.Model.PropertyPanel.AnnotPanel;
  5. using PDFSettings;
  6. using Prism.Mvvm;
  7. using System;
  8. using System.Collections.Generic;
  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.AnnotPanel
  15. {
  16. //属性触发事件的标识
  17. public enum FontSetModeType
  18. {
  19. PresetFontStyes,
  20. FontFamilys,
  21. FontSizes,
  22. FontWeight_Style,
  23. FontColor,
  24. TextAlignment
  25. }
  26. //设置字体大小、字体内容排版、字体颜色、字体样式、字重
  27. public class FontBoard : BindableBase
  28. {
  29. #region 变量
  30. protected event EventHandler<FontSetModeType> ChangedValue;
  31. protected bool IsCanSave = false;
  32. public List<ComboDataItem> FontFamilyItems { get; protected set; }
  33. public List<ComboDataItem> FontStyleItems { get; protected set; }
  34. public List<ComboDataItem> PresetFontItems { get; protected set; }
  35. public List<PresetFontItem> PresetFontList = new List<PresetFontItem>();
  36. #endregion 变量
  37. #region 初始化下拉框或列表默认的数据
  38. protected void InitBaseVariable()
  39. {
  40. InitBase_PresetFontStyles();
  41. InitBase_FontFamilys();
  42. InitBase_FontStyles();
  43. }
  44. //预设字体样式
  45. private void InitBase_PresetFontStyles()
  46. {
  47. PresetFontItems = new List<ComboDataItem>();
  48. PresetFontList = TextFont.GetCachePresetFontList();
  49. foreach (var item in PresetFontList)
  50. {
  51. ComboDataItem itemData = new ComboDataItem(item.mTag, item.mTagContent);
  52. PresetFontItems.Add(itemData);
  53. }
  54. }
  55. //字体
  56. private void InitBase_FontFamilys()
  57. {
  58. FontFamilyItems = TextFont.GetFamily();
  59. }
  60. //字重
  61. private void InitBase_FontStyles()
  62. {
  63. FontStyleItems = TextFont.GetFontStyle();
  64. }
  65. #endregion 初始化下拉框或列表默认的数据
  66. #region 属性
  67. private ComboDataItem _currentPresetFont;
  68. public ComboDataItem CurrentPresetFont
  69. {
  70. get { return _currentPresetFont; }
  71. set
  72. {
  73. bool isChange = IsEqualStrComboData(_currentPresetFont, value);
  74. SetProperty(ref _currentPresetFont, value);
  75. if (isChange)
  76. {
  77. ChangedValue?.Invoke(_currentPresetFont.ValueStr, FontSetModeType.PresetFontStyes);
  78. }
  79. }
  80. }
  81. #region 字体样式
  82. //下拉框列表
  83. private ComboDataItem _currentFontFamily;
  84. public ComboDataItem CurrentFontFamily
  85. {
  86. get { return _currentFontFamily; }
  87. set
  88. {
  89. bool isChange = IsEqualStrComboData(_currentFontFamily, value);
  90. SetProperty(ref _currentFontFamily, value);
  91. if (isChange)
  92. {
  93. ChangedValue?.Invoke(_currentFontFamily.ValueStr, FontSetModeType.FontFamilys);
  94. }
  95. }
  96. }
  97. #endregion 字体样式
  98. #region 字体大小
  99. //下拉框列表:字体大小
  100. private ComboDataItem _currentFontSize = new ComboDataItem(6);
  101. public ComboDataItem CurrentFontSize
  102. {
  103. get { return _currentFontSize; }
  104. set
  105. {
  106. bool isChange = IsEqualComboData(_currentFontSize, value);
  107. if (isChange && value.Value > 0)
  108. SetProperty(ref _currentFontSize, value);
  109. if (isChange && value.Value > 0)
  110. {
  111. ChangedValue?.Invoke(_currentFontSize.Value, FontSetModeType.FontSizes);
  112. }
  113. }
  114. }
  115. #endregion 字体大小
  116. //FontStyle & FontWeight
  117. private FontStyle _fontStyle;
  118. public FontStyle FontStyleItem
  119. {
  120. get { return _fontStyle; }
  121. set { SetProperty(ref _fontStyle, value); }
  122. }
  123. private FontWeight _fontWeight;
  124. public FontWeight FontWeightItem
  125. {
  126. get { return _fontWeight; }
  127. set { SetProperty(ref _fontWeight, value); }
  128. }
  129. private ComboDataItem _currrentFontWeightStyle;
  130. public ComboDataItem CurrrentFontWeightStyle
  131. {
  132. get { return _currrentFontWeightStyle; }
  133. set
  134. {
  135. bool isChange = IsEqualStrComboData(_currrentFontWeightStyle, value);
  136. SetProperty(ref _currrentFontWeightStyle, value);
  137. if (isChange)
  138. {
  139. ChangedValue?.Invoke(_currrentFontWeightStyle, FontSetModeType.FontWeight_Style);
  140. }
  141. }
  142. }
  143. protected void UpdateFontWeight_Style()
  144. {
  145. switch (CurrrentFontWeightStyle.ValueStr)
  146. {
  147. case "Regular":
  148. FontStyleItem = FontStyles.Normal;
  149. FontWeightItem = FontWeights.Normal;
  150. break;
  151. case "Bold":
  152. FontStyleItem = FontStyles.Normal;
  153. FontWeightItem = FontWeights.Bold;
  154. break;
  155. case "Italic":
  156. FontStyleItem = FontStyles.Italic;
  157. FontWeightItem = FontWeights.Normal;
  158. break;
  159. case "Bold Italic":
  160. FontStyleItem = FontStyles.Italic;
  161. FontWeightItem = FontWeights.Bold;
  162. break;
  163. }
  164. }
  165. private C_TEXT_ALIGNMENT _textAlignment;
  166. public C_TEXT_ALIGNMENT TextAlignmentItem
  167. {
  168. get { return _textAlignment; }
  169. set { SetProperty(ref _textAlignment, value); }
  170. }
  171. //颜色
  172. private Brush selectColor = new SolidColorBrush(Colors.Black);
  173. public Brush SelectColor
  174. {
  175. get { return selectColor; }
  176. set
  177. {
  178. SetProperty(ref selectColor, value);
  179. if (IsCanSave)
  180. {
  181. ChangedValue?.Invoke((selectColor as SolidColorBrush).Color, FontSetModeType.FontColor);
  182. }
  183. else
  184. {
  185. CurrentFontColor = selectColor;
  186. }
  187. }
  188. }
  189. private Brush _currentFontColor = new SolidColorBrush(Colors.Transparent);
  190. public Brush CurrentFontColor
  191. {
  192. get { return _currentFontColor; }
  193. set
  194. {
  195. SetProperty(ref _currentFontColor, value);
  196. }
  197. }
  198. private bool IsEqualComboData(ComboDataItem oldValue, ComboDataItem newValue)
  199. {
  200. if (newValue == null || IsCanSave == false)
  201. return false;
  202. if (oldValue != null && newValue != null)
  203. {
  204. if (oldValue.Value != newValue.Value)
  205. return true;
  206. }
  207. return false;
  208. }
  209. private bool IsEqualStrComboData(ComboDataItem oldValue, ComboDataItem newValue)
  210. {
  211. if (newValue == null || string.IsNullOrEmpty(newValue.ValueStr) == true || IsCanSave == false)
  212. return false;
  213. if (oldValue != null && newValue != null)
  214. {
  215. if (oldValue.ValueStr != newValue.ValueStr)
  216. return true;
  217. }
  218. return false;
  219. }
  220. #endregion 属性
  221. #region 列表选中赋值
  222. protected void GetFontWeights_Style(FontStyle fontStyle, FontWeight fontWeights)
  223. {
  224. string strValue = "";
  225. string strContent = "";
  226. if (fontStyle == FontStyles.Normal)
  227. {
  228. if (fontWeights == FontWeights.Normal)
  229. {
  230. strValue = "Regular";
  231. strContent = "常规";
  232. }
  233. else
  234. {
  235. strValue = "Bold";
  236. strContent = "粗体";
  237. }
  238. }
  239. else
  240. {
  241. if (fontWeights == FontWeights.Normal)
  242. {
  243. strValue = "Italic";
  244. strContent = "斜体";
  245. }
  246. else
  247. {
  248. strValue = "Bold Italic";
  249. strContent = "粗斜体";
  250. }
  251. }
  252. CurrrentFontWeightStyle = new ComboDataItem(strValue, strContent);
  253. }
  254. protected void GetCurrentFontSize(int size)
  255. {
  256. CurrentFontSize = new ComboDataItem(size);
  257. }
  258. protected void GetCurrentFontFamily(string fontFamily, string uiStr)
  259. {
  260. CurrentFontFamily = new ComboDataItem(fontFamily, uiStr);
  261. }
  262. #endregion 列表选中赋值
  263. }
  264. //设置字体大小、字体内容排版、字体颜色、字体样式、字重
  265. public class FontBoardVm : BindableBase
  266. {
  267. #region 变量
  268. public List<ComboDataItem> FontFamilyItems { get; protected set; }
  269. public List<ComboDataItem> FontStyleItems { get; protected set; }
  270. public List<ComboDataItem> PresetFontItems { get; protected set; }
  271. public List<PresetFontItem> PresetFontList = new List<PresetFontItem>();
  272. public FontBoardVm(bool isInitdata)
  273. {
  274. if (isInitdata)
  275. InitBaseVariable();
  276. }
  277. #endregion 变量
  278. #region 初始化下拉框或列表默认的数据
  279. protected void InitBaseVariable()
  280. {
  281. InitBase_PresetFontStyles();
  282. InitBase_FontFamilys();
  283. InitBase_FontStyles();
  284. }
  285. //预设字体样式
  286. private void InitBase_PresetFontStyles()
  287. {
  288. PresetFontItems = new List<ComboDataItem>();
  289. PresetFontList = TextFont.GetCachePresetFontList();
  290. foreach (var item in PresetFontList)
  291. {
  292. ComboDataItem itemData = new ComboDataItem(item.mTag, item.mTagContent);
  293. PresetFontItems.Add(itemData);
  294. }
  295. }
  296. //字体
  297. private void InitBase_FontFamilys()
  298. {
  299. FontFamilyItems = TextFont.GetFamily();
  300. }
  301. //字重
  302. private void InitBase_FontStyles()
  303. {
  304. FontStyleItems = TextFont.GetFontStyle();
  305. }
  306. #endregion 初始化下拉框或列表默认的数据
  307. #region 属性
  308. /// <summary>
  309. /// 预设样式
  310. /// </summary>
  311. private ComboDataItem _currentPresetFont = new ComboDataItem("Custom", "Custom");
  312. public ComboDataItem CurrentPresetFont
  313. {
  314. get { return _currentPresetFont; }
  315. set
  316. {
  317. SetProperty(ref _currentPresetFont, value);
  318. switch (value.ValueStr.ToString())
  319. {
  320. case "Custom":
  321. PresetFontSelectedIndex = 0;
  322. break;
  323. case "H1":
  324. PresetFontSelectedIndex = 1;
  325. break;
  326. case "H2":
  327. PresetFontSelectedIndex = 2;
  328. break;
  329. case "H3":
  330. PresetFontSelectedIndex = 3;
  331. break;
  332. case "B1":
  333. PresetFontSelectedIndex = 4;
  334. break;
  335. case "B2":
  336. PresetFontSelectedIndex = 5;
  337. break;
  338. case "B3":
  339. PresetFontSelectedIndex = 6;
  340. break;
  341. default:
  342. PresetFontSelectedIndex = 0;
  343. break;
  344. }
  345. }
  346. }
  347. private int presetFontSelectedIndex;
  348. public int PresetFontSelectedIndex
  349. {
  350. get { return presetFontSelectedIndex; }
  351. set
  352. {
  353. SetProperty(ref presetFontSelectedIndex, value);
  354. }
  355. }
  356. #region 字体样式
  357. //下拉框列表
  358. private ComboDataItem _currentFontFamily = new ComboDataItem("Helvetica", "Helvetica");
  359. public ComboDataItem CurrentFontFamily
  360. {
  361. get { return _currentFontFamily; }
  362. set
  363. {
  364. SetProperty(ref _currentFontFamily, value);
  365. switch (value.Content.ToString())
  366. {
  367. case "Courier New":
  368. FontFamilySelectedIndex = 0;
  369. break;
  370. case "Helvetica":
  371. case "Arial":
  372. FontFamilySelectedIndex = 1;
  373. break;
  374. case "Times New Roman":
  375. FontFamilySelectedIndex = 2;
  376. break;
  377. }
  378. }
  379. }
  380. private int fontFamilySelectedIndex;
  381. public int FontFamilySelectedIndex
  382. {
  383. get { return fontFamilySelectedIndex; }
  384. set
  385. {
  386. SetProperty(ref fontFamilySelectedIndex, value);
  387. }
  388. }
  389. #endregion 字体样式
  390. #region 字体大小
  391. //下拉框列表:字体大小
  392. private ComboDataItem _currentFontSize = new ComboDataItem(6);
  393. public ComboDataItem CurrentFontSize
  394. {
  395. get { return _currentFontSize; }
  396. set => SetProperty(ref _currentFontSize, value);
  397. }
  398. #endregion 字体大小
  399. //FontStyle & FontWeight
  400. private FontStyle _fontStyleItem;
  401. public FontStyle FontStyleItem
  402. {
  403. get { return _fontStyleItem; }
  404. set { SetProperty(ref _fontStyleItem, value); }
  405. }
  406. private FontWeight _fontWeight;
  407. public FontWeight FontWeightItem
  408. {
  409. get { return _fontWeight; }
  410. set { SetProperty(ref _fontWeight, value); }
  411. }
  412. private int fontStyleSelectedIndex;
  413. public int FontStyleSelectedIndex
  414. {
  415. get { return fontStyleSelectedIndex; }
  416. set
  417. {
  418. SetProperty(ref fontStyleSelectedIndex, value);
  419. }
  420. }
  421. private ComboDataItem _currrentFontWeightStyle = new ComboDataItem("Regular", "Regular");
  422. public ComboDataItem CurrrentFontWeightStyle
  423. {
  424. get { return _currrentFontWeightStyle; }
  425. set
  426. {
  427. SetProperty(ref _currrentFontWeightStyle, value);
  428. switch (value.Content.ToString())
  429. {
  430. case "Regular":
  431. FontStyleSelectedIndex = 0;
  432. break;
  433. case "Bold":
  434. FontStyleSelectedIndex = 1;
  435. break;
  436. case "Italic":
  437. FontStyleSelectedIndex = 2;
  438. break;
  439. case "Bold Italic":
  440. FontStyleSelectedIndex = 3;
  441. break;
  442. }
  443. }
  444. }
  445. public void UpdateFontWeight_Style()
  446. {
  447. switch (CurrrentFontWeightStyle.ValueStr)
  448. {
  449. case "Regular":
  450. FontStyleItem = FontStyles.Normal;
  451. FontWeightItem = FontWeights.Normal;
  452. break;
  453. case "Bold":
  454. FontStyleItem = FontStyles.Normal;
  455. FontWeightItem = FontWeights.Bold;
  456. break;
  457. case "Italic":
  458. FontStyleItem = FontStyles.Italic;
  459. FontWeightItem = FontWeights.Normal;
  460. break;
  461. case "Bold Italic":
  462. FontStyleItem = FontStyles.Italic;
  463. FontWeightItem = FontWeights.Bold;
  464. break;
  465. }
  466. }
  467. //文字内容对齐
  468. private string _strtextAlign;
  469. public string StrTextAlign
  470. {
  471. get { return _strtextAlign; }
  472. set { SetProperty(ref _strtextAlign, value); }
  473. }
  474. //颜色
  475. private Brush _fontColor = new SolidColorBrush(Colors.Black);
  476. public Brush FontColor
  477. {
  478. get { return _fontColor; }
  479. set { SetProperty(ref _fontColor, value); CurrentFontColor = _fontColor; }
  480. }
  481. private Brush _currentFontColor = new SolidColorBrush(Colors.Transparent);
  482. public Brush CurrentFontColor
  483. {
  484. get { return _currentFontColor; }
  485. set => SetProperty(ref _currentFontColor, value);
  486. }
  487. //外部UI引用,判断是否选中左对齐、居中对齐、右对齐,或都不选中
  488. public string strAglinState { get; private set; }
  489. //VM赋值
  490. public void SetStrAglinState(string str)
  491. {
  492. strAglinState = str;
  493. }
  494. #endregion 属性
  495. #region 列表选中赋值
  496. public void GetFontWeights_Style(FontStyle fontStyle, FontWeight fontWeights)
  497. {
  498. string strValue = "";
  499. string strContent = "";
  500. if (fontStyle == FontStyles.Normal)
  501. {
  502. if (fontWeights == FontWeights.Normal)
  503. {
  504. strValue = "Regular";
  505. strContent = "Regular";
  506. }
  507. else
  508. {
  509. strValue = "Bold";
  510. strContent = "Bold";
  511. }
  512. }
  513. else
  514. {
  515. if (fontWeights == FontWeights.Normal)
  516. {
  517. strValue = "Italic";
  518. strContent = "Italic";
  519. }
  520. else
  521. {
  522. strValue = "Bold Italic";
  523. strContent = "Bold Italic";
  524. }
  525. }
  526. CurrrentFontWeightStyle = new ComboDataItem(strValue, strContent);
  527. }
  528. public void GetCurrentFontSize(int size)
  529. {
  530. CurrentFontSize = new ComboDataItem(size);
  531. }
  532. public void GetCurrentFontFamily(string fontFamily, string uiStr)
  533. {
  534. if (fontFamily == "Arial")
  535. {
  536. fontFamily = "Helvetica";
  537. uiStr = "Helvetica";
  538. }
  539. CurrentFontFamily = new ComboDataItem(fontFamily, uiStr);
  540. }
  541. public void GetCurrentPresetFont(string presetFont, string uiStr)
  542. {
  543. CurrentPresetFont = new ComboDataItem(presetFont, uiStr);
  544. }
  545. internal bool GetCurrentPresetFont(FreeTextAnnotArgs annot)
  546. {
  547. bool isExist = false;
  548. //List<PresetFontItem> presetFontItems = TextFont.GetCachePresetFontList();
  549. foreach (var item in PresetFontList)
  550. {
  551. if (annot.FontSize == item.mFontSize && annot.FontWeight == item.mFontWeight
  552. && (annot.FontFamily.Source == item.mFontFamily.Source || annot.FontFamily.Source == "Arial" && item.mFontFamily.Source == "Helvetica")
  553. )
  554. {
  555. if (item.mTag != "Custom")
  556. {
  557. CurrentPresetFont = new ComboDataItem(item.mTag, item.mTagContent);
  558. isExist = true;
  559. }
  560. break;
  561. }
  562. }
  563. return isExist;
  564. }
  565. #endregion 列表选中赋值
  566. }
  567. }