CPDFFontUI.xaml.cs 6.2 KB

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