CPDFFontUI.xaml.cs 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Runtime.CompilerServices;
  5. using System.Windows;
  6. using System.Windows.Controls;
  7. using ComPDFKit.PDFDocument;
  8. using Compdfkit_Tools.Helper;
  9. namespace Compdfkit_Tools.Common
  10. {
  11. public partial class CPDFFontUI : UserControl, INotifyPropertyChanged
  12. {
  13. internal bool lockFamilyName = false;
  14. internal bool lockStyleName = false;
  15. private string regular = LanguageHelper.PropertyPanelManager.GetString("Font_Regular");
  16. private string bold = LanguageHelper.PropertyPanelManager.GetString("Font_Bold");
  17. private string italic = LanguageHelper.PropertyPanelManager.GetString("Font_Oblique");
  18. private string boldItalic = LanguageHelper.PropertyPanelManager.GetString("Font_BoldOblique");
  19. public event PropertyChangedEventHandler PropertyChanged;
  20. public event EventHandler FontFamilyChanged;
  21. public event EventHandler FontStyleChanged;
  22. public event EventHandler FontSizeChanged;
  23. public event EventHandler FontAlignChanged;
  24. private string _familyName = string.Empty;
  25. public string FamilyName
  26. {
  27. get => _familyName;
  28. set
  29. {
  30. _familyName = value;
  31. if (lockFamilyName && FontFamilyComboBox.Items.Contains(_familyName))
  32. {
  33. FontFamilyComboBox.SelectedItem = _familyName;
  34. }
  35. }
  36. }
  37. private string _styleName = string.Empty;
  38. public string StyleName
  39. {
  40. get => _styleName;
  41. set
  42. {
  43. _styleName = value;
  44. if (lockStyleName && FontStyleComboBox.Items.Contains(_styleName))
  45. {
  46. FontStyleComboBox.SelectedItem = _styleName;
  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(nameof(FontSizeValue));
  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. List<string> fontStyleList = new List<string>()
  106. {
  107. regular,
  108. bold,
  109. italic,
  110. boldItalic
  111. };
  112. FontStyleComboBox.ItemsSource = CPDFFont.GetFontNameDictionary()[FontFamilyComboBox.SelectedValue.ToString()];
  113. FontStyleComboBox.SelectedIndex = 0;
  114. List<int> fontSizeList = new List<int>()
  115. {
  116. {6},
  117. {8},
  118. {9},
  119. {10},
  120. {12},
  121. {14},
  122. {18},
  123. {20},
  124. {24},
  125. {26},
  126. {28},
  127. {32},
  128. {30},
  129. {32},
  130. {48},
  131. {72}
  132. };
  133. FontSizeComboBox.InitPresetNumberArray(fontSizeList);
  134. }
  135. protected void OnPropertyChanged(string propertyName)
  136. {
  137. PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
  138. }
  139. private void FontFamilyComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
  140. {
  141. var styleNames = CPDFFont.GetFontNameDictionary()[FontFamilyComboBox.SelectedValue.ToString()];
  142. FontStyleComboBox.ItemsSource = styleNames;
  143. if (!lockFamilyName)
  144. {
  145. FamilyName = FontFamilyComboBox.SelectedValue.ToString();
  146. if (styleNames.Count != 0)
  147. {
  148. FontStyleComboBox.SelectedIndex = 0;
  149. }
  150. FontStyleChanged?.Invoke(this, EventArgs.Empty);
  151. }
  152. lockFamilyName = false;
  153. }
  154. private void FontStyleComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
  155. {
  156. if (!lockStyleName)
  157. {
  158. StyleName = FontStyleComboBox.SelectedValue?.ToString();
  159. FontStyleChanged?.Invoke(this, EventArgs.Empty);
  160. }
  161. lockStyleName = false;
  162. }
  163. private void AlignRadioButton_Checked(object sender, RoutedEventArgs e)
  164. {
  165. FontAlignChanged?.Invoke(this, EventArgs.Empty);
  166. }
  167. }
  168. }