CPDFFontUI.xaml.cs 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Linq;
  5. using System.Runtime.CompilerServices;
  6. using System.Windows;
  7. using System.Windows.Controls;
  8. using ComPDFKit.PDFDocument;
  9. using ComPDFKit.Controls.Helper;
  10. namespace ComPDFKit.Controls.Common
  11. {
  12. public partial class CPDFFontUI : UserControl, INotifyPropertyChanged
  13. {
  14. internal bool isFirstLoad = true;
  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. private string _familyName = string.Empty;
  21. public string FamilyName
  22. {
  23. get => _familyName;
  24. set
  25. {
  26. if (UpdateProper(ref _familyName, value))
  27. {
  28. if (FontFamilyComboBox.Items.Contains(_familyName))
  29. {
  30. FontFamilyComboBox.SelectedItem = _familyName;
  31. }
  32. }
  33. }
  34. }
  35. private string _styleName = string.Empty;
  36. public string StyleName
  37. {
  38. get => _styleName;
  39. set
  40. {
  41. if (UpdateProper(ref _styleName, value))
  42. {
  43. if (FontStyleComboBox.Items.Contains(_styleName))
  44. {
  45. FontStyleComboBox.SelectedItem = _styleName;
  46. }
  47. }
  48. }
  49. }
  50. private TextAlignment _textAlignment;
  51. public TextAlignment TextAlignment
  52. {
  53. get
  54. {
  55. if ((bool)LeftAlignRadioButton.IsChecked)
  56. {
  57. return TextAlignment.Left;
  58. }
  59. else if ((bool)CenterAlignRadioButton.IsChecked)
  60. {
  61. return TextAlignment.Center;
  62. }
  63. else
  64. {
  65. return TextAlignment.Right;
  66. }
  67. }
  68. set
  69. {
  70. if (TextAlignment.Left == value)
  71. {
  72. LeftAlignRadioButton.IsChecked = true;
  73. }
  74. else if (TextAlignment.Center == value)
  75. {
  76. CenterAlignRadioButton.IsChecked = true;
  77. }
  78. else
  79. {
  80. RightAlignRadioButton.IsChecked = true;
  81. }
  82. }
  83. }
  84. private int _fontSizeValue = 20;
  85. public int FontSizeValue
  86. {
  87. get => _fontSizeValue;
  88. set
  89. {
  90. _fontSizeValue = value;
  91. OnPropertyChanged();
  92. FontSizeChanged?.Invoke(this, EventArgs.Empty);
  93. }
  94. }
  95. public CPDFFontUI()
  96. {
  97. InitializeComponent();
  98. InitComboBox();
  99. this.DataContext = this;
  100. }
  101. public void InitComboBox()
  102. {
  103. FontFamilyComboBox.ItemsSource = CPDFFont.GetFontNameDictionary().Keys;
  104. FontFamilyComboBox.SelectedIndex = 0;
  105. FontStyleComboBox.ItemsSource = CPDFFont.GetFontNameDictionary()[FontFamilyComboBox.SelectedValue.ToString()];
  106. FontStyleComboBox.SelectedIndex = 0;
  107. List<int> fontSizeList = new List<int>()
  108. {
  109. {6},
  110. {8},
  111. {9},
  112. {10},
  113. {12},
  114. {14},
  115. {18},
  116. {20},
  117. {24},
  118. {26},
  119. {28},
  120. {32},
  121. {30},
  122. {32},
  123. {48},
  124. {72}
  125. };
  126. FontSizeComboBox.InitPresetNumberArray(fontSizeList);
  127. }
  128. private void FontFamilyComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
  129. {
  130. if (isFirstLoad)
  131. {
  132. var fontNames = CPDFFont.GetFontNameDictionary().Keys.ToList();
  133. var defaultIndex = fontNames.FindIndex(x => x == "Helvetica");
  134. if (defaultIndex == -1)
  135. {
  136. defaultIndex = fontNames.FindIndex(x => x == "Arial");
  137. }
  138. FontFamilyComboBox.SelectedIndex = defaultIndex;
  139. FontStyleComboBox.SelectedIndex = 0;
  140. FamilyName = FontFamilyComboBox.SelectedValue.ToString();
  141. isFirstLoad = false;
  142. return;
  143. }
  144. var styleNames = CPDFFont.GetFontNameDictionary()[FontFamilyComboBox.SelectedValue.ToString()];
  145. FontStyleComboBox.ItemsSource = styleNames;
  146. FamilyName = FontFamilyComboBox.SelectedValue.ToString();
  147. if (styleNames.Count != 0)
  148. {
  149. FontStyleComboBox.SelectedIndex = 0;
  150. }
  151. FontFamilyChanged?.Invoke(this, EventArgs.Empty);
  152. }
  153. private void FontStyleComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
  154. {
  155. StyleName = FontStyleComboBox.SelectedValue?.ToString();
  156. FontStyleChanged?.Invoke(this, EventArgs.Empty);
  157. }
  158. private void AlignRadioButton_Checked(object sender, RoutedEventArgs e)
  159. {
  160. FontAlignChanged?.Invoke(this, EventArgs.Empty);
  161. }
  162. protected bool UpdateProper<T>(ref T properValue, T newValue, [CallerMemberName] string properName = "")
  163. {
  164. if (object.Equals(properValue, newValue))
  165. {
  166. return false;
  167. }
  168. properValue = newValue;
  169. OnPropertyChanged(properName);
  170. return true;
  171. }
  172. protected void OnPropertyChanged([CallerMemberName] string propertyName = null)
  173. {
  174. PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
  175. }
  176. }
  177. }