using System; using System.Collections.Generic; using System.ComponentModel; using System.Runtime.CompilerServices; using System.Windows; using System.Windows.Controls; using ComPDFKit.PDFDocument; using Compdfkit_Tools.Helper; namespace Compdfkit_Tools.Common { public partial class CPDFFontUI : UserControl, INotifyPropertyChanged { internal bool lockFamilyName = false; internal bool lockStyleName = false; private string regular = LanguageHelper.PropertyPanelManager.GetString("Font_Regular"); private string bold = LanguageHelper.PropertyPanelManager.GetString("Font_Bold"); private string italic = LanguageHelper.PropertyPanelManager.GetString("Font_Oblique"); private string boldItalic = LanguageHelper.PropertyPanelManager.GetString("Font_BoldOblique"); public event PropertyChangedEventHandler PropertyChanged; public event EventHandler FontFamilyChanged; public event EventHandler FontStyleChanged; public event EventHandler FontSizeChanged; public event EventHandler FontAlignChanged; private string _familyName = string.Empty; public string FamilyName { get => _familyName; set { _familyName = value; if (lockFamilyName && FontFamilyComboBox.Items.Contains(_familyName)) { FontFamilyComboBox.SelectedItem = _familyName; } } } private string _styleName = string.Empty; public string StyleName { get => _styleName; set { _styleName = value; if (lockStyleName && FontStyleComboBox.Items.Contains(_styleName)) { FontStyleComboBox.SelectedItem = _styleName; } } } private TextAlignment _textAlignment; public TextAlignment TextAlignment { get { if ((bool)LeftAlignRadioButton.IsChecked) { return TextAlignment.Left; } else if ((bool)CenterAlignRadioButton.IsChecked) { return TextAlignment.Center; } else { return TextAlignment.Right; } } set { if (TextAlignment.Left == value) { LeftAlignRadioButton.IsChecked = true; } else if (TextAlignment.Center == value) { CenterAlignRadioButton.IsChecked = true; } else { RightAlignRadioButton.IsChecked = true; } } } private int _fontSizeValue = 20; public int FontSizeValue { get => _fontSizeValue; set { _fontSizeValue = value; OnPropertyChanged(nameof(FontSizeValue)); FontSizeChanged?.Invoke(this, EventArgs.Empty); } } public CPDFFontUI() { InitializeComponent(); InitComboBox(); this.DataContext = this; } public void InitComboBox() { FontFamilyComboBox.ItemsSource = CPDFFont.GetFontNameDictionary().Keys; FontFamilyComboBox.SelectedIndex = 0; List fontStyleList = new List() { regular, bold, italic, boldItalic }; FontStyleComboBox.ItemsSource = CPDFFont.GetFontNameDictionary()[FontFamilyComboBox.SelectedValue.ToString()]; FontStyleComboBox.SelectedIndex = 0; List fontSizeList = new List() { {6}, {8}, {9}, {10}, {12}, {14}, {18}, {20}, {24}, {26}, {28}, {32}, {30}, {32}, {48}, {72} }; FontSizeComboBox.InitPresetNumberArray(fontSizeList); } protected void OnPropertyChanged(string propertyName) { PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); } private void FontFamilyComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e) { var styleNames = CPDFFont.GetFontNameDictionary()[FontFamilyComboBox.SelectedValue.ToString()]; FontStyleComboBox.ItemsSource = styleNames; if (!lockFamilyName) { FamilyName = FontFamilyComboBox.SelectedValue.ToString(); if (styleNames.Count != 0) { FontStyleComboBox.SelectedIndex = 0; } FontStyleChanged?.Invoke(this, EventArgs.Empty); } lockFamilyName = false; } private void FontStyleComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e) { if (!lockStyleName) { StyleName = FontStyleComboBox.SelectedValue?.ToString(); FontStyleChanged?.Invoke(this, EventArgs.Empty); } lockStyleName = false; } private void AlignRadioButton_Checked(object sender, RoutedEventArgs e) { FontAlignChanged?.Invoke(this, EventArgs.Empty); } } }