FontBoard.cs 16 KB

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