CPDFTextStyleUI.xaml.cs 5.1 KB

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