FontBoard.cs 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634
  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. private ComboDataItem _currentPresetFont = new ComboDataItem("Custom", "Custom");
  309. public ComboDataItem CurrentPresetFont
  310. {
  311. get { return _currentPresetFont; }
  312. set => SetProperty(ref _currentPresetFont, value);
  313. }
  314. #region 字体样式
  315. //下拉框列表
  316. private ComboDataItem _currentFontFamily = new ComboDataItem("Helvetica", "Helvetica");
  317. public ComboDataItem CurrentFontFamily
  318. {
  319. get { return _currentFontFamily; }
  320. set
  321. {
  322. SetProperty(ref _currentFontFamily, value);
  323. switch (value.Content.ToString())
  324. {
  325. case "Courier New":
  326. FontFamilySelectedIndex = 0;
  327. break;
  328. case "Helvetica":
  329. case "Arial":
  330. FontFamilySelectedIndex = 1;
  331. break;
  332. case "Times New Roman":
  333. FontFamilySelectedIndex = 2;
  334. break;
  335. }
  336. }
  337. }
  338. private int fontFamilySelectedIndex;
  339. public int FontFamilySelectedIndex
  340. {
  341. get { return fontFamilySelectedIndex; }
  342. set
  343. {
  344. SetProperty(ref fontFamilySelectedIndex, value);
  345. }
  346. }
  347. #endregion 字体样式
  348. #region 字体大小
  349. //下拉框列表:字体大小
  350. private ComboDataItem _currentFontSize = new ComboDataItem(6);
  351. public ComboDataItem CurrentFontSize
  352. {
  353. get { return _currentFontSize; }
  354. set => SetProperty(ref _currentFontSize, value);
  355. }
  356. #endregion 字体大小
  357. //FontStyle & FontWeight
  358. private FontStyle _fontStyleItem;
  359. public FontStyle FontStyleItem
  360. {
  361. get { return _fontStyleItem; }
  362. set { SetProperty(ref _fontStyleItem, value); }
  363. }
  364. private FontWeight _fontWeight;
  365. public FontWeight FontWeightItem
  366. {
  367. get { return _fontWeight; }
  368. set { SetProperty(ref _fontWeight, value); }
  369. }
  370. private int fontStyleSelectedIndex;
  371. public int FontStyleSelectedIndex
  372. {
  373. get { return fontStyleSelectedIndex; }
  374. set
  375. {
  376. SetProperty(ref fontStyleSelectedIndex, value);
  377. }
  378. }
  379. private ComboDataItem _currrentFontWeightStyle = new ComboDataItem("Regular", "Regular");
  380. public ComboDataItem CurrrentFontWeightStyle
  381. {
  382. get { return _currrentFontWeightStyle; }
  383. set {
  384. SetProperty(ref _currrentFontWeightStyle, value);
  385. switch (value.Content.ToString())
  386. {
  387. case "Regular":
  388. FontStyleSelectedIndex = 0;
  389. break;
  390. case "Bold":
  391. FontStyleSelectedIndex = 1;
  392. break;
  393. case "Italic":
  394. FontStyleSelectedIndex = 2;
  395. break;
  396. case "Bold Italic":
  397. FontStyleSelectedIndex = 3;
  398. break;
  399. }
  400. }
  401. }
  402. public void UpdateFontWeight_Style()
  403. {
  404. switch (CurrrentFontWeightStyle.ValueStr)
  405. {
  406. case "Regular":
  407. FontStyleItem = FontStyles.Normal;
  408. FontWeightItem = FontWeights.Normal;
  409. break;
  410. case "Bold":
  411. FontStyleItem = FontStyles.Normal;
  412. FontWeightItem = FontWeights.Bold;
  413. break;
  414. case "Italic":
  415. FontStyleItem = FontStyles.Italic;
  416. FontWeightItem = FontWeights.Normal;
  417. break;
  418. case "Bold Italic":
  419. FontStyleItem = FontStyles.Italic;
  420. FontWeightItem = FontWeights.Bold;
  421. break;
  422. }
  423. }
  424. //文字内容对齐
  425. private string _strtextAlign;
  426. public string StrTextAlign
  427. {
  428. get { return _strtextAlign; }
  429. set { SetProperty(ref _strtextAlign, value); }
  430. }
  431. //颜色
  432. private Brush _fontColor = new SolidColorBrush(Colors.Black);
  433. public Brush FontColor
  434. {
  435. get { return _fontColor; }
  436. set { SetProperty(ref _fontColor, value); CurrentFontColor = _fontColor; }
  437. }
  438. private Brush _currentFontColor = new SolidColorBrush(Colors.Transparent);
  439. public Brush CurrentFontColor
  440. {
  441. get { return _currentFontColor; }
  442. set => SetProperty(ref _currentFontColor, value);
  443. }
  444. //外部UI引用,判断是否选中左对齐、居中对齐、右对齐,或都不选中
  445. public string strAglinState { get; private set; }
  446. //VM赋值
  447. public void SetStrAglinState(string str)
  448. {
  449. strAglinState = str;
  450. }
  451. #endregion 属性
  452. #region 列表选中赋值
  453. public void GetFontWeights_Style(FontStyle fontStyle, FontWeight fontWeights)
  454. {
  455. string strValue = "";
  456. string strContent = "";
  457. if (fontStyle == FontStyles.Normal)
  458. {
  459. if (fontWeights == FontWeights.Normal)
  460. {
  461. strValue = "Regular";
  462. strContent = "Regular";
  463. }
  464. else
  465. {
  466. strValue = "Bold";
  467. strContent = "Bold";
  468. }
  469. }
  470. else
  471. {
  472. if (fontWeights == FontWeights.Normal)
  473. {
  474. strValue = "Italic";
  475. strContent = "Italic";
  476. }
  477. else
  478. {
  479. strValue = "Bold Italic";
  480. strContent = "Bold Italic";
  481. }
  482. }
  483. CurrrentFontWeightStyle = new ComboDataItem(strValue, strContent);
  484. }
  485. public void GetCurrentFontSize(int size)
  486. {
  487. CurrentFontSize = new ComboDataItem(size);
  488. }
  489. public void GetCurrentFontFamily(string fontFamily, string uiStr)
  490. {
  491. if(fontFamily == "Arial")
  492. {
  493. fontFamily = "Helvetica";
  494. uiStr = "Helvetica";
  495. }
  496. CurrentFontFamily = new ComboDataItem(fontFamily, uiStr);
  497. }
  498. public void GetCurrentPresetFont(string presetFont, string uiStr)
  499. {
  500. CurrentPresetFont = new ComboDataItem(presetFont, uiStr);
  501. }
  502. internal bool GetCurrentPresetFont(FreeTextAnnotArgs annot)
  503. {
  504. bool isExist = false;
  505. //List<PresetFontItem> presetFontItems = TextFont.GetCachePresetFontList();
  506. foreach (var item in PresetFontList)
  507. {
  508. if (annot.FontSize == item.mFontSize && annot.FontWeight == item.mFontWeight
  509. && (annot.FontFamily.Source == item.mFontFamily.Source || annot.FontFamily.Source == "Arial" && item.mFontFamily.Source == "Helvetica")
  510. )
  511. {
  512. if (item.mTag != "Custom")
  513. {
  514. CurrentPresetFont = new ComboDataItem(item.mTag, item.mTagContent);
  515. isExist = true;
  516. }
  517. break;
  518. }
  519. }
  520. return isExist;
  521. }
  522. #endregion 列表选中赋值
  523. }
  524. }