CPDFFontUI.xaml.cs 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Windows;
  5. using System.Windows.Controls;
  6. using Compdfkit_Tools.Helper;
  7. namespace Compdfkit_Tools.Common
  8. {
  9. public partial class CPDFFontUI : UserControl, INotifyPropertyChanged
  10. {
  11. private string regular = LanguageHelper.PropertyPanelManager.GetString("Font_Regular");
  12. private string bold = LanguageHelper.PropertyPanelManager.GetString("Font_Bold");
  13. private string italic = LanguageHelper.PropertyPanelManager.GetString("Font_Oblique");
  14. private string boldItalic = LanguageHelper.PropertyPanelManager.GetString("Font_BoldOblique");
  15. public event PropertyChangedEventHandler PropertyChanged;
  16. public event EventHandler FontFamilyChanged;
  17. public event EventHandler FontStyleChanged;
  18. public event EventHandler FontSizeChanged;
  19. public event EventHandler FontAlignChanged;
  20. public string FontFamilyValue
  21. {
  22. get => FontFamilyComboBox.SelectedItem.ToString();
  23. set
  24. {
  25. if (value == "Courier New")
  26. {
  27. FontFamilyComboBox.SelectedIndex = 0;
  28. }
  29. else if (value == "Arial")
  30. {
  31. FontFamilyComboBox.SelectedIndex = 1;
  32. }
  33. else
  34. {
  35. FontFamilyComboBox.SelectedIndex = 2;
  36. }
  37. }
  38. }
  39. private bool _isBold;
  40. public bool IsBold
  41. {
  42. get => _isBold;
  43. set
  44. {
  45. _isBold = value;
  46. if (_isBold && IsItalic)
  47. {
  48. FontStyleComboBox.SelectedIndex = 3;
  49. }
  50. else if (_isBold && !IsItalic)
  51. {
  52. FontStyleComboBox.SelectedIndex = 1;
  53. }
  54. else if (!_isBold && IsItalic)
  55. {
  56. FontStyleComboBox.SelectedIndex = 2;
  57. }
  58. else
  59. {
  60. FontStyleComboBox.SelectedIndex = 0;
  61. }
  62. }
  63. }
  64. private bool _isItalic;
  65. public bool IsItalic
  66. {
  67. get => _isItalic;
  68. set
  69. {
  70. _isItalic = value;
  71. if (IsBold && _isItalic)
  72. {
  73. FontStyleComboBox.SelectedIndex = 3;
  74. }
  75. else if (IsBold && !_isItalic)
  76. {
  77. FontStyleComboBox.SelectedIndex = 1;
  78. }
  79. else if (!IsBold && _isItalic)
  80. {
  81. FontStyleComboBox.SelectedIndex = 2;
  82. }
  83. else
  84. {
  85. FontStyleComboBox.SelectedIndex = 0;
  86. }
  87. }
  88. }
  89. private TextAlignment _textAlignment;
  90. public TextAlignment TextAlignment
  91. {
  92. get
  93. {
  94. if ((bool)LeftAlignRadioButton.IsChecked)
  95. {
  96. return TextAlignment.Left;
  97. }
  98. else if ((bool)CenterAlignRadioButton.IsChecked)
  99. {
  100. return TextAlignment.Center;
  101. }
  102. else
  103. {
  104. return TextAlignment.Right;
  105. }
  106. }
  107. set
  108. {
  109. if (TextAlignment.Left == value)
  110. {
  111. LeftAlignRadioButton.IsChecked = true;
  112. }
  113. else if (TextAlignment.Center == value)
  114. {
  115. CenterAlignRadioButton.IsChecked = true;
  116. }
  117. else
  118. {
  119. RightAlignRadioButton.IsChecked = true;
  120. }
  121. }
  122. }
  123. private int _fontSizeValue = 20;
  124. public int FontSizeValue
  125. {
  126. get => _fontSizeValue;
  127. set
  128. {
  129. _fontSizeValue = value;
  130. OnPropertyChanged(nameof(FontSizeValue));
  131. FontSizeChanged?.Invoke(this, EventArgs.Empty);
  132. }
  133. }
  134. public CPDFFontUI()
  135. {
  136. InitializeComponent();
  137. InitComboBox();
  138. this.DataContext = this;
  139. }
  140. public void InitComboBox()
  141. {
  142. List<string> fontNameList = new List<string>()
  143. {
  144. {"Courier" },
  145. {"Helvetica" },
  146. {"Times" }
  147. };
  148. FontFamilyComboBox.ItemsSource = fontNameList;
  149. FontFamilyComboBox.SelectedIndex = 1;
  150. List<string> fontStyleList = new List<string>()
  151. {
  152. regular,
  153. bold,
  154. italic,
  155. boldItalic
  156. };
  157. FontStyleComboBox.ItemsSource = fontStyleList;
  158. FontStyleComboBox.SelectedIndex = 0;
  159. List<int> fontSizeList = new List<int>()
  160. {
  161. {6},
  162. {8},
  163. {9},
  164. {10},
  165. {12},
  166. {14},
  167. {18},
  168. {20},
  169. {24},
  170. {26},
  171. {28},
  172. {32},
  173. {30},
  174. {32},
  175. {48},
  176. {72}
  177. };
  178. FontSizeComboBox.InitPresetNumberArray(fontSizeList);
  179. }
  180. protected void OnPropertyChanged(string propertyName)
  181. {
  182. PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
  183. }
  184. private void FontFamilyComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
  185. {
  186. FontFamilyTextBox.Text = (sender as ComboBox).SelectedItem.ToString();
  187. FontFamilyChanged?.Invoke(this, EventArgs.Empty);
  188. }
  189. private void FontStyleComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
  190. {
  191. FontStyleTextBox.Text = (sender as ComboBox).SelectedItem.ToString();
  192. if (FontStyleTextBox.Text == regular)
  193. {
  194. IsBold = false;
  195. IsItalic = false;
  196. }
  197. else if (FontStyleTextBox.Text == italic)
  198. {
  199. IsBold = false;
  200. IsItalic = true;
  201. }
  202. else if (FontStyleTextBox.Text == bold)
  203. {
  204. IsBold = true;
  205. IsItalic = false;
  206. }
  207. else
  208. {
  209. IsBold = true;
  210. IsItalic = true;
  211. }
  212. FontStyleChanged?.Invoke(this, EventArgs.Empty);
  213. }
  214. private void AlignRadioButton_Checked(object sender, RoutedEventArgs e)
  215. {
  216. FontAlignChanged?.Invoke(this, EventArgs.Empty);
  217. }
  218. }
  219. }