CPDFTextStyleUI.xaml.cs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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. });
  43. }
  44. FontNameComboBox.ItemsSource = fontNameList;
  45. }
  46. }
  47. public void SelectFontName(string fontName)
  48. {
  49. if(string.IsNullOrEmpty(fontName))
  50. {
  51. return;
  52. }
  53. List<ComboBoxItem> fontNameList = FontNameComboBox.ItemsSource as List<ComboBoxItem>;
  54. if (fontNameList != null && fontNameList.Count>0)
  55. {
  56. int selectIndex = -1;
  57. for(int i=0;i<fontNameList.Count; i++)
  58. {
  59. ComboBoxItem checkItem= fontNameList[i];
  60. if(checkItem.Content!=null && checkItem.Content.ToString().ToLower()==fontName.ToLower() )
  61. {
  62. selectIndex=i;
  63. break;
  64. }
  65. }
  66. FontNameComboBox.SelectedIndex = selectIndex;
  67. }
  68. }
  69. public void SetFontStyle(bool isBold,bool isItalic)
  70. {
  71. if (isBold == false && isItalic == false)
  72. {
  73. FontStyleBox.SelectedIndex = 0;
  74. return;
  75. }
  76. if (isBold && isItalic == false)
  77. {
  78. FontStyleBox.SelectedIndex = 1;
  79. return;
  80. }
  81. if (isBold == false && isItalic)
  82. {
  83. FontStyleBox.SelectedIndex = 0;
  84. return;
  85. }
  86. if (isBold && isItalic )
  87. {
  88. FontStyleBox.SelectedIndex = 3;
  89. }
  90. }
  91. public void SetFontSize(double newFontSize)
  92. {
  93. if(FontSizeTextBox!=null)
  94. {
  95. if (newFontSize - (int)(newFontSize) > 0)
  96. {
  97. FontSizeTextBox.Text = (newFontSize).ToString("F2");
  98. }
  99. else
  100. {
  101. FontSizeTextBox.Text = ((int)(newFontSize)).ToString();
  102. }
  103. }
  104. }
  105. private void FontNameComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
  106. {
  107. if(FontNameComboBox.SelectedIndex==-1)
  108. {
  109. TextFontChanged?.Invoke(this,string.Empty);
  110. return;
  111. }
  112. ComboBoxItem selectItem= FontNameComboBox.SelectedItem as ComboBoxItem;
  113. if(selectItem != null && selectItem.Content != null)
  114. {
  115. TextFontChanged?.Invoke(this, selectItem.Content.ToString());
  116. }
  117. }
  118. private void FontStyleBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
  119. {
  120. int selectIndex = Math.Max(0, FontStyleBox.SelectedIndex);
  121. switch(selectIndex)
  122. {
  123. case 0:
  124. TextBoldChanged?.Invoke(this,false);
  125. TextItalicChanged?.Invoke(this, false);
  126. break;
  127. case 1:
  128. TextBoldChanged?.Invoke(this, true);
  129. TextItalicChanged?.Invoke(this, false);
  130. break;
  131. case 2:
  132. TextBoldChanged?.Invoke(this, false);
  133. TextItalicChanged?.Invoke(this, true);
  134. break;
  135. case 3:
  136. TextBoldChanged?.Invoke(this, true);
  137. TextItalicChanged?.Invoke(this, true);
  138. break;
  139. default:
  140. break;
  141. }
  142. }
  143. private void FontSizeComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
  144. {
  145. ComboBoxItem selectItem = FontSizeComboBox.SelectedItem as ComboBoxItem;
  146. if (selectItem != null && selectItem.Content != null)
  147. {
  148. if (int.TryParse(selectItem.Content.ToString(), out int newFontSize))
  149. {
  150. FontSizeTextBox.Text = (newFontSize).ToString();
  151. TextSizeChanged?.Invoke(this, newFontSize);
  152. }
  153. }
  154. }
  155. }
  156. }