FontSetModeVM.cs 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. using ComPDFKit.PDFAnnotation;
  2. using PDF_Office.CustomControl.CompositeControl;
  3. using PDF_Office.Model.PropertyPanel.AnnotPanel;
  4. using Prism.Mvvm;
  5. using System;
  6. using System.Collections.Generic;
  7. using System.Linq;
  8. using System.Text;
  9. using System.Threading.Tasks;
  10. using System.Windows;
  11. using System.Windows.Media;
  12. namespace PDF_Office.ViewModels.PropertyPanel
  13. {
  14. //属性触发事件的标识
  15. public enum FontSetModeType
  16. {
  17. PresetFontStyes,
  18. FontFamilys,
  19. FontSizes,
  20. FontWeight_Style,
  21. FontColor,
  22. TextAlignment
  23. }
  24. public class FontSetMode : BindableBase
  25. {
  26. #region 变量
  27. protected event EventHandler<FontSetModeType> ChangedValue;
  28. protected bool IsCanSave = false;
  29. public List<ComboDataItem> FontFamilyItems { get; protected set; }
  30. public List<ComboDataItem> FontStyleItems { get; protected set; }
  31. public List<ComboDataItem> PresetTextItems { get; protected set; }
  32. public List<FontStyleItem> FontStyleList = new List<FontStyleItem>();
  33. #endregion
  34. #region 初始化下拉框或列表默认的数据
  35. protected void InitBaseVariable()
  36. {
  37. InitBase_PresetFontStyles();
  38. InitBase_FontFamilys();
  39. InitBase_FontStyles();
  40. }
  41. //预设字体样式
  42. private void InitBase_PresetFontStyles()
  43. {
  44. PresetTextItems = new List<ComboDataItem>();
  45. FontStyleList = TextFont.GetPresetFontStyle();
  46. foreach (var item in FontStyleList)
  47. {
  48. ComboDataItem itemData = new ComboDataItem(item.mTag, item.mTagContent);
  49. PresetTextItems.Add(itemData);
  50. }
  51. }
  52. //字体
  53. private void InitBase_FontFamilys()
  54. {
  55. FontFamilyItems = TextFont.GetFamily();
  56. }
  57. //字重
  58. private void InitBase_FontStyles()
  59. {
  60. FontStyleItems = TextFont.GetFontStyle();
  61. }
  62. #endregion
  63. #region 属性
  64. private ComboDataItem _presetTextData;
  65. public ComboDataItem PresetTextData
  66. {
  67. get { return _presetTextData; }
  68. set
  69. {
  70. bool isChange = IsEqualStrComboData(_presetTextData, value);
  71. SetProperty(ref _presetTextData, value);
  72. if (isChange)
  73. {
  74. ChangedValue?.Invoke(_presetTextData.ValueStr, FontSetModeType.PresetFontStyes);
  75. }
  76. }
  77. }
  78. #region 字体样式
  79. //下拉框列表
  80. private ComboDataItem _fontFamilyData;
  81. public ComboDataItem FontFamilyData
  82. {
  83. get { return _fontFamilyData; }
  84. set
  85. {
  86. bool isChange = IsEqualStrComboData(_fontFamilyData, value);
  87. SetProperty(ref _fontFamilyData, value);
  88. if (isChange)
  89. {
  90. ChangedValue?.Invoke(_fontFamilyData.ValueStr, FontSetModeType.FontFamilys);
  91. }
  92. }
  93. }
  94. #endregion
  95. #region 字体大小
  96. //下拉框列表:字体大小
  97. private ComboDataItem _fontSizeData = new ComboDataItem(6);
  98. public ComboDataItem FontSizeData
  99. {
  100. get { return _fontSizeData; }
  101. set
  102. {
  103. bool isChange = IsEqualComboData(_fontSizeData, value);
  104. if(value.Value > 0)
  105. SetProperty(ref _fontSizeData, value);
  106. if (isChange && value.Value > 0)
  107. {
  108. ChangedValue?.Invoke(_fontSizeData.Value, FontSetModeType.FontSizes);
  109. }
  110. }
  111. }
  112. #endregion
  113. //FontStyle & FontWeight
  114. private FontStyle _fontStyle;
  115. public FontStyle FontStyleItem
  116. {
  117. get { return _fontStyle; }
  118. set { SetProperty(ref _fontStyle, value); }
  119. }
  120. private FontWeight _fontWeight;
  121. public FontWeight FontWeightItem
  122. {
  123. get { return _fontWeight; }
  124. set { SetProperty(ref _fontWeight, value); }
  125. }
  126. private ComboDataItem _fontWeightStyleItem;
  127. public ComboDataItem FontWeightStyleItem
  128. {
  129. get { return _fontWeightStyleItem; }
  130. set
  131. {
  132. bool isChange = IsEqualStrComboData(_presetTextData, value);
  133. SetProperty(ref _fontWeightStyleItem, value);
  134. if (isChange)
  135. {
  136. ChangedValue?.Invoke(_fontWeightStyleItem, FontSetModeType.FontWeight_Style);
  137. }
  138. }
  139. }
  140. protected void UpdateFontWeight_Style()
  141. {
  142. switch (FontWeightStyleItem.ValueStr)
  143. {
  144. case "Regular":
  145. FontStyleItem = FontStyles.Normal;
  146. FontWeightItem = FontWeights.Normal;
  147. break;
  148. case "Bold":
  149. FontStyleItem = FontStyles.Normal;
  150. FontWeightItem = FontWeights.Bold;
  151. break;
  152. case "Italic":
  153. FontStyleItem = FontStyles.Italic;
  154. FontWeightItem = FontWeights.Normal;
  155. break;
  156. case "Bold Italic":
  157. FontStyleItem = FontStyles.Italic;
  158. FontWeightItem = FontWeights.Bold;
  159. break;
  160. }
  161. }
  162. private C_TEXT_ALIGNMENT _textAlignment;
  163. public C_TEXT_ALIGNMENT TextAlignmentItem
  164. {
  165. get { return _textAlignment; }
  166. set { SetProperty(ref _textAlignment, value); }
  167. }
  168. //颜色
  169. private Brush selectColor = new SolidColorBrush(Colors.Black);
  170. public Brush SelectColor
  171. {
  172. get { return selectColor; }
  173. set
  174. {
  175. SetProperty(ref selectColor, value);
  176. if (IsCanSave)
  177. {
  178. ChangedValue?.Invoke((selectColor as SolidColorBrush).Color, FontSetModeType.FontColor);
  179. CurrentColor = (selectColor as SolidColorBrush).Color;
  180. }
  181. }
  182. }
  183. private Color currentColor = Colors.Black;
  184. public Color CurrentColor
  185. {
  186. get { return currentColor; }
  187. set
  188. {
  189. SetProperty(ref currentColor, value);
  190. }
  191. }
  192. private bool IsEqualComboData(ComboDataItem oldValue, ComboDataItem newValue)
  193. {
  194. if (newValue == null || IsCanSave == false)
  195. return false;
  196. if (oldValue != null && newValue != null)
  197. {
  198. if (oldValue.Value != newValue.Value)
  199. return true;
  200. }
  201. return false;
  202. }
  203. private bool IsEqualStrComboData(ComboDataItem oldValue, ComboDataItem newValue)
  204. {
  205. if (newValue == null && string.IsNullOrEmpty(newValue.ValueStr) == true || IsCanSave == false)
  206. return false;
  207. if (oldValue != null && newValue != null)
  208. {
  209. if (oldValue.ValueStr != newValue.ValueStr)
  210. return true;
  211. }
  212. return false;
  213. }
  214. #endregion
  215. #region 列表选中赋值
  216. protected void SelectedFontWeights_Style(FontStyle fontStyle, FontWeight fontWeights)
  217. {
  218. string strValue = "";
  219. string strContent = "";
  220. if (fontStyle == FontStyles.Normal)
  221. {
  222. if (fontWeights == FontWeights.Normal)
  223. {
  224. strValue = "Regular";
  225. strContent = "常规";
  226. }
  227. else
  228. {
  229. strValue = "Bold";
  230. strContent = "粗体";
  231. }
  232. }
  233. else
  234. {
  235. if (fontWeights == FontWeights.Normal)
  236. {
  237. strValue = "Italic";
  238. strContent = "斜体";
  239. }
  240. else
  241. {
  242. strValue = "Bold Italic";
  243. strContent = "粗斜体";
  244. }
  245. }
  246. FontWeightStyleItem = new ComboDataItem(strValue, strContent);
  247. }
  248. #endregion
  249. }
  250. }