FontBoard.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567
  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("Helvatica", "Helvetica");
  317. public ComboDataItem CurrentFontFamily
  318. {
  319. get { return _currentFontFamily; }
  320. set => SetProperty(ref _currentFontFamily, value);
  321. }
  322. #endregion 字体样式
  323. #region 字体大小
  324. //下拉框列表:字体大小
  325. private ComboDataItem _currentFontSize = new ComboDataItem(6);
  326. public ComboDataItem CurrentFontSize
  327. {
  328. get { return _currentFontSize; }
  329. set => SetProperty(ref _currentFontSize, value);
  330. }
  331. #endregion 字体大小
  332. //FontStyle & FontWeight
  333. private FontStyle _fontStyleItem;
  334. public FontStyle FontStyleItem
  335. {
  336. get { return _fontStyleItem; }
  337. set { SetProperty(ref _fontStyleItem, value); }
  338. }
  339. private FontWeight _fontWeight;
  340. public FontWeight FontWeightItem
  341. {
  342. get { return _fontWeight; }
  343. set { SetProperty(ref _fontWeight, value); }
  344. }
  345. private ComboDataItem _currrentFontWeightStyle = new ComboDataItem("Regular", "Regular");
  346. public ComboDataItem CurrrentFontWeightStyle
  347. {
  348. get { return _currrentFontWeightStyle; }
  349. set => SetProperty(ref _currrentFontWeightStyle, value);
  350. }
  351. public void UpdateFontWeight_Style()
  352. {
  353. switch (CurrrentFontWeightStyle.ValueStr)
  354. {
  355. case "Regular":
  356. FontStyleItem = FontStyles.Normal;
  357. FontWeightItem = FontWeights.Normal;
  358. break;
  359. case "Bold":
  360. FontStyleItem = FontStyles.Normal;
  361. FontWeightItem = FontWeights.Bold;
  362. break;
  363. case "Italic":
  364. FontStyleItem = FontStyles.Italic;
  365. FontWeightItem = FontWeights.Normal;
  366. break;
  367. case "Bold Italic":
  368. FontStyleItem = FontStyles.Italic;
  369. FontWeightItem = FontWeights.Bold;
  370. break;
  371. }
  372. }
  373. //文字内容对齐
  374. private string _strtextAlign;
  375. public string StrTextAlign
  376. {
  377. get { return _strtextAlign; }
  378. set { SetProperty(ref _strtextAlign, value); }
  379. }
  380. //颜色
  381. private Brush _fontColor = new SolidColorBrush(Colors.Black);
  382. public Brush FontColor
  383. {
  384. get { return _fontColor; }
  385. set { SetProperty(ref _fontColor, value); CurrentFontColor = _fontColor; }
  386. }
  387. private Brush _currentFontColor = new SolidColorBrush(Colors.Transparent);
  388. public Brush CurrentFontColor
  389. {
  390. get { return _currentFontColor; }
  391. set => SetProperty(ref _currentFontColor, value);
  392. }
  393. //外部UI引用,判断是否选中左对齐、居中对齐、右对齐,或都不选中
  394. public string strAglinState { get; private set; }
  395. //VM赋值
  396. public void SetStrAglinState(string str)
  397. {
  398. strAglinState = str;
  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 = "常规";
  412. }
  413. else
  414. {
  415. strValue = "Bold";
  416. strContent = "粗体";
  417. }
  418. }
  419. else
  420. {
  421. if (fontWeights == FontWeights.Normal)
  422. {
  423. strValue = "Italic";
  424. strContent = "斜体";
  425. }
  426. else
  427. {
  428. strValue = "Bold Italic";
  429. strContent = "粗斜体";
  430. }
  431. }
  432. CurrrentFontWeightStyle = new ComboDataItem(strValue, strContent);
  433. }
  434. public void GetCurrentFontSize(int size)
  435. {
  436. CurrentFontSize = new ComboDataItem(size);
  437. }
  438. public void GetCurrentFontFamily(string fontFamily, string uiStr)
  439. {
  440. CurrentFontFamily = new ComboDataItem(fontFamily, uiStr);
  441. }
  442. public void GetCurrentPresetFont(string presetFont, string uiStr)
  443. {
  444. CurrentPresetFont = new ComboDataItem(presetFont, uiStr);
  445. }
  446. internal void GetCurrentPresetFont(FreeTextAnnotArgs annot)
  447. {
  448. //List<PresetFontItem> presetFontItems = TextFont.GetCachePresetFontList();
  449. foreach (var item in PresetFontList)
  450. {
  451. if (annot.FontSize == item.mFontSize && annot.FontWeight == item.mFontWeight
  452. && (annot.FontFamily.Source == item.mFontFamily.Source || annot.FontFamily.Source == "Arial" && item.mFontFamily.Source == "Helvatica")
  453. )
  454. {
  455. CurrentPresetFont = new ComboDataItem(item.mTag, item.mTagContent);
  456. }
  457. }
  458. }
  459. #endregion 列表选中赋值
  460. }
  461. }