FontBoardVM.cs 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  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.ViewModels.PropertyPanel
  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. CurrentFontColor = (selectColor as SolidColorBrush).Color;
  181. ChangedValue?.Invoke((selectColor as SolidColorBrush).Color, FontSetModeType.FontColor);
  182. }
  183. }
  184. }
  185. private Color _currentFontColor = Colors.Black;
  186. public Color CurrentFontColor
  187. {
  188. get { return _currentFontColor; }
  189. set
  190. {
  191. SetProperty(ref _currentFontColor, value);
  192. }
  193. }
  194. private bool IsEqualComboData(ComboDataItem oldValue, ComboDataItem newValue)
  195. {
  196. if (newValue == null || IsCanSave == false)
  197. return false;
  198. if (oldValue != null && newValue != null)
  199. {
  200. if (oldValue.Value != newValue.Value)
  201. return true;
  202. }
  203. return false;
  204. }
  205. private bool IsEqualStrComboData(ComboDataItem oldValue, ComboDataItem newValue)
  206. {
  207. if (newValue == null || string.IsNullOrEmpty(newValue.ValueStr) == true || IsCanSave == false)
  208. return false;
  209. if (oldValue != null && newValue != null)
  210. {
  211. if (oldValue.ValueStr != newValue.ValueStr)
  212. return true;
  213. }
  214. return false;
  215. }
  216. #endregion
  217. #region 列表选中赋值
  218. protected void GetFontWeights_Style(FontStyle fontStyle, FontWeight fontWeights)
  219. {
  220. string strValue = "";
  221. string strContent = "";
  222. if (fontStyle == FontStyles.Normal)
  223. {
  224. if (fontWeights == FontWeights.Normal)
  225. {
  226. strValue = "Regular";
  227. strContent = "常规";
  228. }
  229. else
  230. {
  231. strValue = "Bold";
  232. strContent = "粗体";
  233. }
  234. }
  235. else
  236. {
  237. if (fontWeights == FontWeights.Normal)
  238. {
  239. strValue = "Italic";
  240. strContent = "斜体";
  241. }
  242. else
  243. {
  244. strValue = "Bold Italic";
  245. strContent = "粗斜体";
  246. }
  247. }
  248. CurrrentFontWeightStyle = new ComboDataItem(strValue, strContent);
  249. }
  250. protected void GetCurrentFontSize(int size)
  251. {
  252. CurrentFontSize = new ComboDataItem(size);
  253. }
  254. protected void GetCurrentFontFamily(string fontFamily,string uiStr)
  255. {
  256. CurrentFontFamily = new ComboDataItem(fontFamily, uiStr);
  257. }
  258. #endregion
  259. }
  260. }