CPDFTextStyleUI.xaml.cs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Runtime.CompilerServices;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. using System.Windows;
  8. using System.Windows.Controls;
  9. using System.Windows.Data;
  10. using System.Windows.Documents;
  11. using System.Windows.Input;
  12. using System.Windows.Media;
  13. using System.Windows.Media.Imaging;
  14. using System.Windows.Navigation;
  15. using System.Windows.Shapes;
  16. namespace compdfkit_tools.Edit
  17. {
  18. /// <summary>
  19. /// CPDFFontStyleUI.xaml 的交互逻辑
  20. /// </summary>
  21. public partial class CPDFTextStyleUI : UserControl
  22. {
  23. public event EventHandler<string> TextFontChanged;
  24. public event EventHandler<bool> TextBoldChanged;
  25. public event EventHandler<bool> TextItalicChanged;
  26. public event EventHandler<double> TextSizeChanged;
  27. public CPDFTextStyleUI()
  28. {
  29. InitializeComponent();
  30. }
  31. public void SetFontNames(List<string> fontNames)
  32. {
  33. FontNameComboBox.ItemsSource = null;
  34. if (fontNames != null && fontNames.Count > 0)
  35. {
  36. List<ComboBoxItem> fontNameList = new List<ComboBoxItem>();
  37. foreach (string fontName in fontNames)
  38. {
  39. fontNameList.Add(new ComboBoxItem()
  40. {
  41. Content = fontName,
  42. VerticalContentAlignment=VerticalAlignment.Center,
  43. HorizontalContentAlignment=HorizontalAlignment.Left
  44. });
  45. }
  46. FontNameComboBox.ItemsSource = fontNameList;
  47. }
  48. }
  49. public void SelectFontName(string fontName)
  50. {
  51. if(string.IsNullOrEmpty(fontName))
  52. {
  53. return;
  54. }
  55. List<ComboBoxItem> fontNameList = FontNameComboBox.ItemsSource as List<ComboBoxItem>;
  56. if (fontNameList != null && fontNameList.Count>0)
  57. {
  58. int selectIndex = -1;
  59. for(int i=0;i<fontNameList.Count; i++)
  60. {
  61. ComboBoxItem checkItem= fontNameList[i];
  62. if(checkItem.Content!=null && checkItem.Content.ToString().ToLower()==fontName.ToLower() )
  63. {
  64. selectIndex=i;
  65. break;
  66. }
  67. }
  68. FontNameComboBox.SelectedIndex = selectIndex;
  69. }
  70. }
  71. public void SetFontStyle(bool isBold,bool isItalic)
  72. {
  73. if (isBold == false && isItalic == false)
  74. {
  75. FontStyleBox.SelectedIndex = 0;
  76. return;
  77. }
  78. if (isBold && isItalic == false)
  79. {
  80. FontStyleBox.SelectedIndex = 1;
  81. return;
  82. }
  83. if (isBold == false && isItalic)
  84. {
  85. FontStyleBox.SelectedIndex = 0;
  86. return;
  87. }
  88. if (isBold && isItalic )
  89. {
  90. FontStyleBox.SelectedIndex = 3;
  91. }
  92. }
  93. public void SetFontSize(double newFontSize)
  94. {
  95. if (FontSizeTextBox != null)
  96. {
  97. if (newFontSize - (int)(newFontSize) > 0)
  98. {
  99. FontSizeTextBox.Text = ((int)newFontSize).ToString();
  100. }
  101. else
  102. {
  103. FontSizeTextBox.Text = ((int)(newFontSize)).ToString();
  104. }
  105. }
  106. }
  107. private void FontNameComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
  108. {
  109. if(FontNameComboBox.SelectedIndex==-1)
  110. {
  111. TextFontChanged?.Invoke(this,string.Empty);
  112. return;
  113. }
  114. ComboBoxItem selectItem= FontNameComboBox.SelectedItem as ComboBoxItem;
  115. if(selectItem != null && selectItem.Content != null)
  116. {
  117. TextFontChanged?.Invoke(this, selectItem.Content.ToString());
  118. }
  119. }
  120. private void FontStyleBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
  121. {
  122. int selectIndex = Math.Max(0, FontStyleBox.SelectedIndex);
  123. switch(selectIndex)
  124. {
  125. case 0:
  126. TextBoldChanged?.Invoke(this,false);
  127. TextItalicChanged?.Invoke(this, false);
  128. break;
  129. case 1:
  130. TextBoldChanged?.Invoke(this, true);
  131. TextItalicChanged?.Invoke(this, false);
  132. break;
  133. case 2:
  134. TextBoldChanged?.Invoke(this, false);
  135. TextItalicChanged?.Invoke(this, true);
  136. break;
  137. case 3:
  138. TextBoldChanged?.Invoke(this, true);
  139. TextItalicChanged?.Invoke(this, true);
  140. break;
  141. default:
  142. break;
  143. }
  144. }
  145. private void FontSizeComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
  146. {
  147. ComboBoxItem selectItem = FontSizeComboBox.SelectedItem as ComboBoxItem;
  148. if (selectItem != null && selectItem.Content != null)
  149. {
  150. if (int.TryParse(selectItem.Content.ToString(), out int newFontSize))
  151. {
  152. FontSizeTextBox.Text = (newFontSize).ToString();
  153. TextSizeChanged?.Invoke(this, newFontSize);
  154. }
  155. }
  156. }
  157. }
  158. }