Selaa lähdekoodia

compdfkit(win) - 补充了Freetext字体相关的交互

liuaoran 11 kuukautta sitten
vanhempi
commit
cafcc758ed

+ 4 - 15
Demo/Examples/Compdfkit_Tools/Annotation/CPDFAnnotationPreviewerControl.xaml.cs

@@ -165,26 +165,15 @@ namespace Compdfkit_Tools.PDFControl
             StampGrid.Visibility = Visibility.Visible;
             StampImage.Source = writeableBitmap;
         }
-        
+
         public void DrawFreeTextPreview(CPDFFreeTextData freeTextData)
         {
             CollapsedAll();
             FreeTextGrid.Visibility = Visibility.Visible;
-            
-            if (freeTextData.FontFamily == "Helvetica")
-            {
-                FreeText.FontFamily = new FontFamily("Arial");
-            }
-            else if (freeTextData.FontFamily == "Times")
-            {
-                FreeText.FontFamily = new FontFamily("Times New Roman");
-            }
-            else
-            {
-                FreeText.FontFamily = new FontFamily("Courier New");
-            }
 
-            FreeText.FontSize = freeTextData.FontSize/1.2;
+            FreeText.FontFamily = new FontFamily(freeTextData.FontFamily);
+
+            FreeText.FontSize = freeTextData.FontSize / 1.2;
             FreeText.Foreground = new SolidColorBrush(freeTextData.BorderColor);
             FreeText.Opacity = freeTextData.Opacity;
             if (freeTextData.IsBold)

+ 6 - 9
Demo/Examples/Compdfkit_Tools/Annotation/PDFAnnotationPanel/PDFAnnotationUI/CPDFFreeTextUI.xaml.cs

@@ -14,6 +14,7 @@ using ComPDFKit.Tool.Help;
 using ComPDFKit.Tool.UndoManger;
 using ComPDFKitViewer.Helper;
 using static ComPDFKit.PDFAnnotation.CTextAttribute.CFontNameHelper;
+using ComPDFKit.PDFDocument;
 
 namespace Compdfkit_Tools.PDFControlUI
 {
@@ -110,8 +111,8 @@ namespace Compdfkit_Tools.PDFControlUI
                 if (textAnnot != null && textAnnot.IsValid())
                 {
                     CTextAttribute textAttr = textAnnot.FreeTextDa;
-                    FontType fontType = GetFontType(textAttr.FontName);
-                    textAttr.FontName = ObtainFontName(fontType, CPDFFontControl.IsBold, CPDFFontControl.IsItalic);
+                    string postScriptName = string.Empty; 
+                    textAttr.FontName = CPDFFontControl.PostScriptName;
                     textAnnot.SetFreetextDa(textAttr);
 
                     textAnnot.UpdateAp();
@@ -242,10 +243,8 @@ namespace Compdfkit_Tools.PDFControlUI
                     textParam.FontColor[1],
                     textParam.FontColor[2]));
                 CPDFOpacityControl.OpacityValue = (int)(textParam.Transparency / 255D * 100);
-                CPDFFontControl.FontFamilyValue = textParam.FontName;
+                CPDFFontControl.PostScriptName = textParam.FontName;
                 CPDFFontControl.FontSizeValue = (int)textParam.FontSize;
-                CPDFFontControl.IsBold = textParam.IsBold;
-                CPDFFontControl.IsItalic = textParam.IsItalic;
                 switch(textParam.Alignment)
                 {
                     case C_TEXT_ALIGNMENT.ALIGNMENT_LEFT:
@@ -275,13 +274,11 @@ namespace Compdfkit_Tools.PDFControlUI
         public CPDFFreeTextData GetFreeTextData()
         {
             CPDFFreeTextData pdfFreeTextData = new CPDFFreeTextData();
-            pdfFreeTextData.AnnotationType = CPDFAnnotationType.FreeText;
+             pdfFreeTextData.AnnotationType = CPDFAnnotationType.FreeText;
             pdfFreeTextData.BorderColor = ((SolidColorBrush)ColorPickerControl.Brush).Color;
             pdfFreeTextData.Opacity = CPDFOpacityControl.OpacityValue / 100.0;
-            pdfFreeTextData.FontFamily = CPDFFontControl.FontFamilyValue;
+            pdfFreeTextData.FontFamily = CPDFFontControl.PostScriptName;
             pdfFreeTextData.FontSize = CPDFFontControl.FontSizeValue;
-            pdfFreeTextData.IsBold = CPDFFontControl.IsBold;
-            pdfFreeTextData.IsItalic = CPDFFontControl.IsItalic;
             pdfFreeTextData.TextAlignment = CPDFFontControl.TextAlignment;
             pdfFreeTextData.Note = NoteTextBox.Text;
             return pdfFreeTextData;

+ 3 - 3
Demo/Examples/Compdfkit_Tools/Asset/Styles/ComboBoxStyle.xaml

@@ -365,7 +365,7 @@
                                                                     <Storyboard >
                                                                         <DoubleAnimationUsingKeyFrames Storyboard.TargetName="ArrowVbx" Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[2].(RotateTransform.Angle)">
                                                                             <EasingDoubleKeyFrame KeyTime="00:00:00" Value="0"/>
-                                                                            <EasingDoubleKeyFrame KeyTime="00:00:00.2000000" Value="180"/>
+                                                                            <EasingDoubleKeyFrame KeyTime="00:00:00.1500000" Value="180"/>
                                                                         </DoubleAnimationUsingKeyFrames>
                                                                     </Storyboard>
                                                                 </BeginStoryboard>
@@ -376,7 +376,7 @@
                                                                     <Storyboard >
                                                                         <DoubleAnimationUsingKeyFrames Storyboard.TargetName="ArrowVbx" Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[2].(RotateTransform.Angle)">
                                                                             <EasingDoubleKeyFrame KeyTime="00:00:00" Value="180"/>
-                                                                            <EasingDoubleKeyFrame KeyTime="00:00:00.2000000" Value="0"/>
+                                                                            <EasingDoubleKeyFrame KeyTime="00:00:00.1500000" Value="0"/>
                                                                         </DoubleAnimationUsingKeyFrames>
                                                                     </Storyboard>
                                                                 </BeginStoryboard>
@@ -406,7 +406,7 @@
                             <Setter TargetName="comboBoxBorder" Property="BorderBrush" Value="#388EF7"></Setter>
                             <Setter Property="Effect">
                                 <Setter.Value>
-                                    <DropShadowEffect Color="#00BFFF" ShadowDepth="0" BlurRadius="5" Opacity="0.5"/>
+                                    <DropShadowEffect Color="#00BFFF" ShadowDepth="0" BlurRadius="5" Opacity="0.2"/>
                                 </Setter.Value>
                             </Setter>
                         </Trigger>

+ 34 - 10
Demo/Examples/Compdfkit_Tools/Common/PropertyControl/PDFFont/CPDFFontControl.xaml.cs

@@ -1,4 +1,6 @@
-using System;
+using ComPDFKit.PDFDocument;
+using ComPDFKit.Tool.UndoManger;
+using System;
 using System.Windows;
 using System.Windows.Controls;
 
@@ -15,22 +17,44 @@ namespace Compdfkit_Tools.Common
             set => CPDFFontUI.FontSizeValue = value;
         }
 
-        public string FontFamilyValue
+        private string _postScriptName = string.Empty;
+        public string PostScriptName
         {
-            get => CPDFFontUI.FontFamilyValue;
-            set => CPDFFontUI.FontFamilyValue = value;
+            get
+            {
+                var psName = string.Empty;
+                CPDFFont.GetPostScriptName(FontFamilyValue, FontStyleValue, ref psName);
+                return psName;
+            }
+            set
+            {
+                var _fontFamilyName = string.Empty;
+                var _fontStyleName = string.Empty;
+                _postScriptName = value;
+                CPDFFont.GetFamlyStyleName(_postScriptName, ref _fontFamilyName, ref _fontStyleName);
+                FontFamilyValue = _fontFamilyName;
+                FontStyleValue = _fontStyleName;
+            }
         }
 
-        public bool IsBold
+        public string FontFamilyValue
         {
-            get => CPDFFontUI.IsBold;
-            set => CPDFFontUI.IsBold = value;
+            get => CPDFFontUI.FamilyName;
+            set
+            {
+                CPDFFontUI.lockFamilyName = true;
+                CPDFFontUI.FamilyName = value;
+            }
         }
 
-        public bool IsItalic
+        public string FontStyleValue
         {
-            get => CPDFFontUI.IsItalic;
-            set => CPDFFontUI.IsItalic = value;
+            get => CPDFFontUI.StyleName;
+            set
+            {
+                CPDFFontUI.lockStyleName = true;
+                CPDFFontUI.StyleName = value;
+            }
         }
 
         public TextAlignment TextAlignment

+ 36 - 43
Demo/Examples/Compdfkit_Tools/Common/PropertyControl/PDFFont/CPDFFontUI.xaml

@@ -6,18 +6,19 @@
              xmlns:local="clr-namespace:Compdfkit_Tools.Common"
              mc:Ignorable="d" 
              d:DesignHeight="120" d:DesignWidth="228">
-    <UserControl.Resources>
-        <Style x:Key="GreyBgRadioBtnStyle" TargetType="{x:Type RadioButton}">
-            <Setter Property="BorderThickness" Value="1" />
-            <Setter Property="Foreground" Value="#252629" />
-            <Setter Property="Template">
-                <Setter.Value>
-                    <ControlTemplate TargetType="{x:Type RadioButton}">
-                        <Border
+    <UserControl.Resources> 
+        <ResourceDictionary>
+            <Style x:Key="GreyBgRadioBtnStyle" TargetType="{x:Type RadioButton}">
+                <Setter Property="BorderThickness" Value="1" />
+                <Setter Property="Foreground" Value="#252629" />
+                <Setter Property="Template">
+                    <Setter.Value>
+                        <ControlTemplate TargetType="{x:Type RadioButton}">
+                            <Border
                         x:Name="templateRoot"
                         Background="{TemplateBinding Background}"
                         SnapsToDevicePixels="True">
-                            <ContentPresenter
+                                <ContentPresenter
                             x:Name="contentPresenter"
                             Grid.Column="1"
                             Margin="{TemplateBinding Padding}"
@@ -26,25 +27,29 @@
                             Focusable="False"
                             RecognizesAccessKey="True"
                             SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" />
-                        </Border>
-                        <ControlTemplate.Triggers>
-                            <Trigger Property="IsEnabled" Value="false">
-                                <Setter TargetName="templateRoot" Property="Opacity" Value="0.5" />
-                            </Trigger>
-                            <Trigger Property="IsMouseOver" Value="True">
-                                <Setter Property="Background" Value="#294982E6"></Setter>
-                            </Trigger>
-                            <Trigger Property="IsPressed" Value="true">
-                                <Setter Property="Background" Value="#604982E6"></Setter>
-                            </Trigger>
-                            <Trigger Property="IsChecked" Value="true">
-                                <Setter Property="Background" Value="#294982E6"></Setter>
-                            </Trigger>
-                        </ControlTemplate.Triggers>
-                    </ControlTemplate>
-                </Setter.Value>
-            </Setter>
-        </Style>
+                            </Border>
+                            <ControlTemplate.Triggers>
+                                <Trigger Property="IsEnabled" Value="false">
+                                    <Setter TargetName="templateRoot" Property="Opacity" Value="0.5" />
+                                </Trigger>
+                                <Trigger Property="IsMouseOver" Value="True">
+                                    <Setter Property="Background" Value="#294982E6"></Setter>
+                                </Trigger>
+                                <Trigger Property="IsPressed" Value="true">
+                                    <Setter Property="Background" Value="#604982E6"></Setter>
+                                </Trigger>
+                                <Trigger Property="IsChecked" Value="true">
+                                    <Setter Property="Background" Value="#294982E6"></Setter>
+                                </Trigger>
+                            </ControlTemplate.Triggers>
+                        </ControlTemplate>
+                    </Setter.Value>
+                </Setter>
+            </Style>
+            <ResourceDictionary.MergedDictionaries> 
+                <ResourceDictionary Source="pack://application:,,,/Compdfkit_Tools;component/Asset/Styles/ComboBoxStyle.xaml"></ResourceDictionary>
+            </ResourceDictionary.MergedDictionaries>
+        </ResourceDictionary>
     </UserControl.Resources>
     <Grid>
         <Grid.RowDefinitions>
@@ -53,27 +58,15 @@
             <RowDefinition></RowDefinition>
         </Grid.RowDefinitions>
         <Grid>
-            <ComboBox x:Name="FontFamilyComboBox" SelectionChanged="FontFamilyComboBox_SelectionChanged" Width="228" Height="28" Margin="0,0,0,4"></ComboBox>
-            <TextBox x:Name="FontFamilyTextBox" IsHitTestVisible="False" Width="228" Height="28" VerticalContentAlignment="Center" IsReadOnly="True" Background="#FFFFFF" Padding="0,0,15,0" Margin="0,0,0,4">Helvetica</TextBox>
-            <Path Fill="#43474D" HorizontalAlignment="Right" VerticalAlignment="Center" Margin="0,0,5,0" IsHitTestVisible="False">
-                <Path.Data>
-                    M0.5 0.510248L4.5041 5.5L8.5 0.5L0.5 0.510248Z
-                </Path.Data>
-            </Path>
+            <ComboBox x:Name="FontFamilyComboBox" SelectionChanged="FontFamilyComboBox_SelectionChanged" Style="{StaticResource ComboBoxStyle1}" Width="228" Height="28" Margin="0,0,0,4" ></ComboBox>
         </Grid>
-        <Grid Grid.Row="1" Margin="0,4,0,0">
+        <Grid Grid.Row="1" Margin="0,4,0,0" HorizontalAlignment="Left" Width="228">
             <Grid.ColumnDefinitions>
                 <ColumnDefinition Width="auto"></ColumnDefinition>
                 <ColumnDefinition></ColumnDefinition>
             </Grid.ColumnDefinitions>
             <Grid Margin="0,0,8,0">
-                <ComboBox x:Name="FontStyleComboBox" SelectionChanged="FontStyleComboBox_SelectionChanged" Width="148" Height="28" BorderBrush="#1E000000" ></ComboBox>
-                <TextBox x:Name="FontStyleTextBox" IsHitTestVisible="False" Width="148" Height="28" VerticalContentAlignment="Center" IsReadOnly="True" Background="#FFFFFF" Padding="0,0,15,0" >Common</TextBox>
-                <Path Fill="#43474D" HorizontalAlignment="Right" VerticalAlignment="Center" Margin="0,0,5,0" IsHitTestVisible="False">
-                    <Path.Data>
-                        M0.5 0.510248L4.5041 5.5L8.5 0.5L0.5 0.510248Z
-                    </Path.Data>
-                </Path>
+                <ComboBox x:Name="FontStyleComboBox" Style="{StaticResource ComboBoxStyle1}" SelectionChanged="FontStyleComboBox_SelectionChanged" Width="148" Height="28" BorderBrush="#1E000000" ></ComboBox>
             </Grid>
 
             <local:DropDownNumberBoxControl x:Name="FontSizeComboBox" Maxium="100" Minimum="1" Grid.Column="1" Width="72" Height="28" Unit="pt" Text="{Binding FontSizeValue,Mode=TwoWay}"></local:DropDownNumberBoxControl>

+ 42 - 91
Demo/Examples/Compdfkit_Tools/Common/PropertyControl/PDFFont/CPDFFontUI.xaml.cs

@@ -1,94 +1,54 @@
 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;
 
-        public string FontFamilyValue
+        private string _familyName = string.Empty;
+        public string FamilyName
         {
-            get => FontFamilyComboBox.SelectedItem.ToString();
+            get => _familyName;
             set
-            {
-                if (value == "Courier")
+            { 
+                _familyName = value;
+                if (lockFamilyName && FontFamilyComboBox.Items.Contains(_familyName))
                 {
-                    FontFamilyComboBox.SelectedIndex = 0;
+                    FontFamilyComboBox.SelectedItem = _familyName;
                 }
-                else if (value == "Helvetica")
-                {
-                    FontFamilyComboBox.SelectedIndex = 1;
-
-                }
-                else
-                {
-                    FontFamilyComboBox.SelectedIndex = 2;
-                }
-
             }
         }
 
-        private bool _isBold;
-        public bool IsBold
+        private string _styleName = string.Empty;
+        public string StyleName
         {
-            get => _isBold;
+            get => _styleName;
             set
-            {
-                _isBold = value;
-                if (_isBold && IsItalic)
-                {
-                    FontStyleComboBox.SelectedIndex = 3;
-                }
-                else if (_isBold && !IsItalic)
+            { 
+                _styleName = value;
+                if (lockStyleName && FontStyleComboBox.Items.Contains(_styleName))
                 {
-                    FontStyleComboBox.SelectedIndex = 1;
-                }
-                else if (!_isBold && IsItalic)
-                {
-                    FontStyleComboBox.SelectedIndex = 2;
-                }
-                else
-                {
-                    FontStyleComboBox.SelectedIndex = 0;
-                }
-            }
-        }
-
-        private bool _isItalic;
-        public bool IsItalic
-        {
-            get => _isItalic;
-            set
-            {
-                _isItalic = value;
-                if (IsBold && _isItalic)
-                {
-                    FontStyleComboBox.SelectedIndex = 3;
-                }
-                else if (IsBold && !_isItalic)
-                {
-                    FontStyleComboBox.SelectedIndex = 1;
-                }
-                else if (!IsBold && _isItalic)
-                {
-                    FontStyleComboBox.SelectedIndex = 2;
-                }
-                else
-                {
-                    FontStyleComboBox.SelectedIndex = 0;
+                    FontStyleComboBox.SelectedItem = _styleName;
                 }
             }
         }
@@ -150,14 +110,8 @@ namespace Compdfkit_Tools.Common
 
         public void InitComboBox()
         {
-            List<string> fontNameList = new List<string>()
-            {
-                {"Courier" },
-                {"Helvetica" },
-                {"Times" }
-            };
-            FontFamilyComboBox.ItemsSource = fontNameList;
-            FontFamilyComboBox.SelectedIndex = 1;
+            FontFamilyComboBox.ItemsSource = CPDFFont.GetFontNameDictionary().Keys;
+            FontFamilyComboBox.SelectedIndex = 0;
 
             List<string> fontStyleList = new List<string>()
             {
@@ -166,7 +120,8 @@ namespace Compdfkit_Tools.Common
                 italic,
                 boldItalic
             };
-            FontStyleComboBox.ItemsSource = fontStyleList;
+
+            FontStyleComboBox.ItemsSource = CPDFFont.GetFontNameDictionary()[FontFamilyComboBox.SelectedValue.ToString()];
             FontStyleComboBox.SelectedIndex = 0;
 
             List<int> fontSizeList = new List<int>()
@@ -190,6 +145,7 @@ namespace Compdfkit_Tools.Common
             };
             FontSizeComboBox.InitPresetNumberArray(fontSizeList);
         }
+
         protected void OnPropertyChanged(string propertyName)
         {
             PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
@@ -197,34 +153,29 @@ namespace Compdfkit_Tools.Common
 
         private void FontFamilyComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
         {
-            FontFamilyTextBox.Text = (sender as ComboBox).SelectedItem.ToString();
-            FontFamilyChanged?.Invoke(this, EventArgs.Empty);
+            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)
         {
-            FontStyleTextBox.Text = (sender as ComboBox).SelectedItem.ToString();
-            if (FontStyleTextBox.Text == regular)
-            {
-                IsBold = false;
-                IsItalic = false;
-            }
-            else if (FontStyleTextBox.Text == italic)
-            {
-                IsBold = false;
-                IsItalic = true;
-            }
-            else if (FontStyleTextBox.Text == bold)
-            {
-                IsBold = true;
-                IsItalic = false;
-            }
-            else
+
+            if (!lockStyleName)
             {
-                IsBold = true;
-                IsItalic = true;
+                StyleName = FontStyleComboBox.SelectedValue?.ToString();
+                FontStyleChanged?.Invoke(this, EventArgs.Empty);
             }
-            FontStyleChanged?.Invoke(this, EventArgs.Empty);
+            lockStyleName = false;
         }
 
         private void AlignRadioButton_Checked(object sender, RoutedEventArgs e)

+ 1 - 0
Demo/Examples/PDFViewer/App.xaml.cs

@@ -12,6 +12,7 @@ using System.Windows.Data;
 using System.Windows.Threading;
 using System.Xml;
 using ComPDFKit.NativeMethod;
+using ComPDFKit.PDFDocument;
 using Compdfkit_Tools.Helper;
 using PDFViewer.Properties;