From a25ebdfd4de5643165cf70292ed366e6a20688eb Mon Sep 17 00:00:00 2001 From: Calcitem Date: Sat, 15 Jan 2022 00:03:52 +0800 Subject: [PATCH] =?UTF-8?q?=E9=83=A8=E5=88=86=20if=20=E6=9D=A1=E4=BB=B6?= =?UTF-8?q?=E7=BF=BB=E8=BD=AC=E4=BB=A5=E7=BC=A9=E5=B0=8F=E4=BB=A3=E7=A0=81?= =?UTF-8?q?=E5=B5=8C=E5=A5=97=E5=B1=82=E7=BA=A7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 一般是在 if 控制范围很大的情况下进行调整,如果控制范围小则不调整。 从第三方代码的 Demo 中复制的代码块不修改。 --- App/Common/FileHelper.cs | 57 ++++++------ App/Common/FilePath.cs | 18 ++-- .../AutoBookmark/FontNameConditionEditor.cs | 18 ++-- .../AutoBookmark/TextConditionEditor.cs | 30 +++--- App/Functions/AutoBookmarkControl.cs | 13 +-- App/Functions/CustomButton/GlassButton.cs | 20 ++-- App/Functions/DocumentInspectorControl.cs | 93 ++++++++++--------- App/Functions/FormState.cs | 23 ++--- App/Functions/FunctionControl.cs | 24 ++--- App/Functions/MacroMenu.cs | 30 +++--- App/MainForm.cs | 24 ++--- App/Model/ColorSpaces/ColorSpaceHelper.cs | 30 +++--- App/Model/PdfPath/PathAxes.cs | 15 +-- App/Model/SourceItem.cs | 46 ++++----- App/Processor/DocInfoExporter.cs | 47 +++++----- App/Processor/DocInfoImporter.cs | 78 ++++++++-------- App/Processor/Imaging/ImageInfo.cs | 66 ++++++------- .../ClearDestinationOffsetProcessor.cs | 17 ++-- .../InfoXmlProcessors/LevelDownProcessor.cs | 18 ++-- App/Processor/InfoXmlProcessors/UndoAction.cs | 22 ++--- App/Processor/Mupdf/MuText.cs | 18 ++-- App/Processor/Mupdf/RenderResultCache.cs | 27 +++--- App/Processor/PdfContentExport.cs | 9 +- App/Processor/PdfDocumentCreator.cs | 37 ++++---- 24 files changed, 407 insertions(+), 373 deletions(-) diff --git a/App/Common/FileHelper.cs b/App/Common/FileHelper.cs index 180eac0..89bb9dd 100644 --- a/App/Common/FileHelper.cs +++ b/App/Common/FileHelper.cs @@ -112,34 +112,37 @@ namespace PDFPatcher.Common } internal static bool CheckOverwrite(string targetFile) { - if (File.Exists(targetFile)) { - switch (__OverwriteMode) { - case OverwriteType.Prompt: - var r = Common.FormHelper.YesNoCancelBox(String.Join("\n", new string[] { - "是否覆盖目标文件?", targetFile, "\n按住 Shift 键重复此对话框的选择,本次操作不再弹出覆盖文件提示。" - })); - if (r == System.Windows.Forms.DialogResult.No) { - if (FormHelper.IsShiftKeyDown) { - __OverwriteMode = OverwriteType.Skip; - } - goto case OverwriteType.Skip; - } - else if (r == System.Windows.Forms.DialogResult.Cancel) { - throw new OperationCanceledException(); - } - if (FormHelper.IsShiftKeyDown) { - __OverwriteMode = OverwriteType.Overwrite; - } - break; - case OverwriteType.Overwrite: - return true; - case OverwriteType.Skip: - Tracker.TraceMessage(Tracker.Category.ImportantMessage, "取消覆盖文件:" + targetFile); - return false; - default: - goto case OverwriteType.Prompt; - } + if (!File.Exists(targetFile)) { + return true; } + + switch (__OverwriteMode) { + case OverwriteType.Prompt: + var r = Common.FormHelper.YesNoCancelBox(String.Join("\n", new string[] { + "是否覆盖目标文件?", targetFile, "\n按住 Shift 键重复此对话框的选择,本次操作不再弹出覆盖文件提示。" + })); + if (r == System.Windows.Forms.DialogResult.No) { + if (FormHelper.IsShiftKeyDown) { + __OverwriteMode = OverwriteType.Skip; + } + goto case OverwriteType.Skip; + } + else if (r == System.Windows.Forms.DialogResult.Cancel) { + throw new OperationCanceledException(); + } + if (FormHelper.IsShiftKeyDown) { + __OverwriteMode = OverwriteType.Overwrite; + } + break; + case OverwriteType.Overwrite: + return true; + case OverwriteType.Skip: + Tracker.TraceMessage(Tracker.Category.ImportantMessage, "取消覆盖文件:" + targetFile); + return false; + default: + goto case OverwriteType.Prompt; + } + return true; } diff --git a/App/Common/FilePath.cs b/App/Common/FilePath.cs index 4b6705b..4433154 100644 --- a/App/Common/FilePath.cs +++ b/App/Common/FilePath.cs @@ -190,14 +190,16 @@ namespace PDFPatcher.Common /// 删除路径尾部的 或 字符。 /// 删除了尾部“\”字符的路径。 public FilePath TrimPathSeparator() { - if (_value != null) { - string p = _value; - int i; - for (i = p.Length - 1; i >= 0; i--) { - char c = p[i]; - if (!Char.IsWhiteSpace(c) && IsDirectorySeparator(c) == false) { - return _value.Substring(0, i + 1); - } + if (_value == null) { + return Empty; + } + + string p = _value; + int i; + for (i = p.Length - 1; i >= 0; i--) { + char c = p[i]; + if (!Char.IsWhiteSpace(c) && IsDirectorySeparator(c) == false) { + return _value.Substring(0, i + 1); } } return Empty; diff --git a/App/Functions/AutoBookmark/FontNameConditionEditor.cs b/App/Functions/AutoBookmark/FontNameConditionEditor.cs index 9b7625d..65df376 100644 --- a/App/Functions/AutoBookmark/FontNameConditionEditor.cs +++ b/App/Functions/AutoBookmark/FontNameConditionEditor.cs @@ -32,15 +32,17 @@ namespace PDFPatcher.Functions #endregion private void ControlChanged(object sender, EventArgs e) { - if (_lock == false) { - if (sender == _FontNameBox) { - _filter.FontName = _FontNameBox.Text; - } - else if (sender == _FullMatchBox) { - _filter.MatchFullName = _FullMatchBox.Checked; - } - EditAdjustmentForm.UpdateFilter(this); + if (_lock) { + return; } + + if (sender == _FontNameBox) { + _filter.FontName = _FontNameBox.Text; + } + else if (sender == _FullMatchBox) { + _filter.MatchFullName = _FullMatchBox.Checked; + } + EditAdjustmentForm.UpdateFilter(this); } } } diff --git a/App/Functions/AutoBookmark/TextConditionEditor.cs b/App/Functions/AutoBookmark/TextConditionEditor.cs index c8854c2..9eb7e1d 100644 --- a/App/Functions/AutoBookmark/TextConditionEditor.cs +++ b/App/Functions/AutoBookmark/TextConditionEditor.cs @@ -34,21 +34,23 @@ namespace PDFPatcher.Functions #endregion private void ControlChanged(object sender, EventArgs e) { - if (_lock == false) { - if (sender == _PatternBox) { - _filter.Pattern.Text = _PatternBox.Text; - } - else if (sender == _FullMatchBox) { - _filter.Pattern.FullMatch = _FullMatchBox.Checked; - } - else if (sender == _MatchCaseBox) { - _filter.Pattern.MatchCase = _MatchCaseBox.Checked; - } - else if (sender == _UseRegexBox) { - _filter.Pattern.UseRegularExpression = _UseRegexBox.Checked; - } - EditAdjustmentForm.UpdateFilter(this); + if (_lock) { + return; } + + if (sender == _PatternBox) { + _filter.Pattern.Text = _PatternBox.Text; + } + else if (sender == _FullMatchBox) { + _filter.Pattern.FullMatch = _FullMatchBox.Checked; + } + else if (sender == _MatchCaseBox) { + _filter.Pattern.MatchCase = _MatchCaseBox.Checked; + } + else if (sender == _UseRegexBox) { + _filter.Pattern.UseRegularExpression = _UseRegexBox.Checked; + } + EditAdjustmentForm.UpdateFilter(this); } } } diff --git a/App/Functions/AutoBookmarkControl.cs b/App/Functions/AutoBookmarkControl.cs index db9e545..7607ed6 100644 --- a/App/Functions/AutoBookmarkControl.cs +++ b/App/Functions/AutoBookmarkControl.cs @@ -317,14 +317,15 @@ namespace PDFPatcher.Functions private void _AddFilterMenu_ItemClicked(object sender, ToolStripItemClickedEventArgs e) { var c = EditAdjustmentForm.CreateCondition(e.ClickedItem.Name); - if (c != null) { - using (var dialog = new EditAdjustmentForm(new AutoBookmarkOptions.LevelAdjustmentOption { Condition = c })) { - if (dialog.ShowDialog() == DialogResult.OK && dialog.Filter.Condition != null) { - _LevelAdjustmentBox.AddObject(dialog.Filter); - } + if (c == null) { + return; + } + + using (var dialog = new EditAdjustmentForm(new AutoBookmarkOptions.LevelAdjustmentOption { Condition = c })) { + if (dialog.ShowDialog() == DialogResult.OK && dialog.Filter.Condition != null) { + _LevelAdjustmentBox.AddObject(dialog.Filter); } } } - } } diff --git a/App/Functions/CustomButton/GlassButton.cs b/App/Functions/CustomButton/GlassButton.cs index f387bd7..46cc77a 100644 --- a/App/Functions/CustomButton/GlassButton.cs +++ b/App/Functions/CustomButton/GlassButton.cs @@ -618,18 +618,20 @@ namespace EnhancedGlassButton /// A that contains the event data. protected override void OnMouseMove(MouseEventArgs mevent) { base.OnMouseMove(mevent); - if (mevent.Button != MouseButtons.None) { - if (!ClientRectangle.Contains(mevent.X, mevent.Y)) { - if (isHovered) { - isHovered = false; - Invalidate(); - } - } - else if (!isHovered) { - isHovered = true; + if (mevent.Button == MouseButtons.None) { + return; + } + + if (!ClientRectangle.Contains(mevent.X, mevent.Y)) { + if (isHovered) { + isHovered = false; Invalidate(); } } + else if (!isHovered) { + isHovered = true; + Invalidate(); + } } /// diff --git a/App/Functions/DocumentInspectorControl.cs b/App/Functions/DocumentInspectorControl.cs index 9793a26..5c86dd1 100644 --- a/App/Functions/DocumentInspectorControl.cs +++ b/App/Functions/DocumentInspectorControl.cs @@ -75,38 +75,41 @@ namespace PDFPatcher.Functions if (d.ImageKey != null) { return d.ImageKey; } - if (d.Type != PdfObjectType.Normal) { - switch (d.Type) { - case PdfObjectType.Trailer: - return __OpNameIcons["Document"]; - case PdfObjectType.Root: - break; - case PdfObjectType.Pages: - return __OpNameIcons["Pages"]; - case PdfObjectType.Page: - return __OpNameIcons["Page"]; - case PdfObjectType.Image: - return __OpNameIcons["Image"]; - case PdfObjectType.Outline: - return __OpNameIcons["Outline"]; - case PdfObjectType.PageCommands: - return __OpNameIcons["PageCommands"]; - case PdfObjectType.PageCommand: - if (d.ImageKey == null) { - var n = d.ExtensiveObject as string; - if ((n != null && __OpNameIcons.TryGetValue(n, out int ic)) - || (d.Name.StartsWith(Constants.ContentPrefix + ":") && __OpNameIcons.TryGetValue(d.Name, out ic)) - ) { - d.ImageKey = ic; - } - else { - d.ImageKey = __OpNameIcons["Null"]; - } + + if (d.Type == PdfObjectType.Normal) { + return GetImageKey(d); + } + + switch (d.Type) { + case PdfObjectType.Trailer: + return __OpNameIcons["Document"]; + case PdfObjectType.Root: + break; + case PdfObjectType.Pages: + return __OpNameIcons["Pages"]; + case PdfObjectType.Page: + return __OpNameIcons["Page"]; + case PdfObjectType.Image: + return __OpNameIcons["Image"]; + case PdfObjectType.Outline: + return __OpNameIcons["Outline"]; + case PdfObjectType.PageCommands: + return __OpNameIcons["PageCommands"]; + case PdfObjectType.PageCommand: + if (d.ImageKey == null) { + var n = d.ExtensiveObject as string; + if ((n != null && __OpNameIcons.TryGetValue(n, out int ic)) + || (d.Name.StartsWith(Constants.ContentPrefix + ":") && __OpNameIcons.TryGetValue(d.Name, out ic)) + ) { + d.ImageKey = ic; } - return d.ImageKey; - case PdfObjectType.Hidden: - return __OpNameIcons["Hidden"]; - } + else { + d.ImageKey = __OpNameIcons["Null"]; + } + } + return d.ImageKey; + case PdfObjectType.Hidden: + return __OpNameIcons["Hidden"]; } return GetImageKey(d); } @@ -501,19 +504,21 @@ namespace PDFPatcher.Functions private void ShowDescription(string name, string description, string type) { _DescriptionBox.Text = String.Empty; - if (String.IsNullOrEmpty(name) == false) { - _DescriptionBox.SetSelectionFontSize(13); - _DescriptionBox.SetSelectionBold(true); - _DescriptionBox.AppendText(name); - _DescriptionBox.SetSelectionFontSize(9); - if (type != null) { - _DescriptionBox.AppendText(Environment.NewLine); - _DescriptionBox.AppendText("类型:" + type); - } - if (description != null) { - _DescriptionBox.AppendText(Environment.NewLine); - _DescriptionBox.AppendText(description); - } + if (String.IsNullOrEmpty(name)) { + return; + } + + _DescriptionBox.SetSelectionFontSize(13); + _DescriptionBox.SetSelectionBold(true); + _DescriptionBox.AppendText(name); + _DescriptionBox.SetSelectionFontSize(9); + if (type != null) { + _DescriptionBox.AppendText(Environment.NewLine); + _DescriptionBox.AppendText("类型:" + type); + } + if (description != null) { + _DescriptionBox.AppendText(Environment.NewLine); + _DescriptionBox.AppendText(description); } } diff --git a/App/Functions/FormState.cs b/App/Functions/FormState.cs index 63a44ed..3ce8b8b 100644 --- a/App/Functions/FormState.cs +++ b/App/Functions/FormState.cs @@ -1,5 +1,4 @@ -using System; -using System.Drawing; +using System.Drawing; using System.Windows.Forms; namespace PDFPatcher.Functions @@ -16,16 +15,18 @@ namespace PDFPatcher.Functions bool IsMaximized; public void Maximize(Form targetForm) { - if (!IsMaximized) { - IsMaximized = true; - if (targetForm.WindowState == FormWindowState.Maximized) { - targetForm.WindowState = FormWindowState.Normal; - } - Save(targetForm); - targetForm.FormBorderStyle = FormBorderStyle.None; - targetForm.TopMost = true; - targetForm.Bounds = Screen.FromControl(targetForm).Bounds; + if (IsMaximized) { + return; } + + IsMaximized = true; + if (targetForm.WindowState == FormWindowState.Maximized) { + targetForm.WindowState = FormWindowState.Normal; + } + Save(targetForm); + targetForm.FormBorderStyle = FormBorderStyle.None; + targetForm.TopMost = true; + targetForm.Bounds = Screen.FromControl(targetForm).Bounds; } void Save(Form targetForm) { diff --git a/App/Functions/FunctionControl.cs b/App/Functions/FunctionControl.cs index 4a94e54..ab87d3a 100644 --- a/App/Functions/FunctionControl.cs +++ b/App/Functions/FunctionControl.cs @@ -1,7 +1,5 @@ using System; -using System.Collections.Generic; using System.ComponentModel; -using System.Text; using System.Windows.Forms; using PDFPatcher.Common; @@ -26,16 +24,18 @@ namespace PDFPatcher.Functions } public virtual void ExecuteCommand(string commandName, params string[] parameters) { - if (Commands.OpenFile == commandName) { - // 将第一个文本框设置为文件路径 - if (parameters.Length > 0 && String.IsNullOrEmpty(parameters[0]) == false - && FileHelper.HasExtension(parameters[0], Constants.FileExtensions.Pdf) - ) { - foreach (Control c in Controls) { - if (c is SourceFileControl i) { - i.Text = parameters[0]; - return; - } + if (Commands.OpenFile != commandName) { + return; + } + + // 将第一个文本框设置为文件路径 + if (parameters.Length > 0 && String.IsNullOrEmpty(parameters[0]) == false + && FileHelper.HasExtension(parameters[0], Constants.FileExtensions.Pdf) + ) { + foreach (Control c in Controls) { + if (c is SourceFileControl i) { + i.Text = parameters[0]; + return; } } } diff --git a/App/Functions/MacroMenu.cs b/App/Functions/MacroMenu.cs index 93e92b7..868d680 100644 --- a/App/Functions/MacroMenu.cs +++ b/App/Functions/MacroMenu.cs @@ -1,7 +1,4 @@ -using System; -using System.Collections.Generic; -using System.Text; -using System.Windows.Forms; +using System.Windows.Forms; namespace PDFPatcher.Functions { @@ -49,19 +46,20 @@ namespace PDFPatcher.Functions internal void ProcessInsertMacroCommand(object sender, ToolStripItemClickedEventArgs e) { var t = e.ClickedItem.Text; - if (t.StartsWith(InsertText)) { - t = t.Substring(InsertText.Length); - var c = SourceControl as TextBoxBase; - if (c != null) { - c.SelectedText = t; - return; - } - var cb = SourceControl as ComboBox; - if (cb != null) { - cb.SelectedText = t; - } + if (!t.StartsWith(InsertText)) { + return; + } + + t = t.Substring(InsertText.Length); + var c = SourceControl as TextBoxBase; + if (c != null) { + c.SelectedText = t; + return; + } + var cb = SourceControl as ComboBox; + if (cb != null) { + cb.SelectedText = t; } } - } } diff --git a/App/MainForm.cs b/App/MainForm.cs index 620189d..3f76070 100644 --- a/App/MainForm.cs +++ b/App/MainForm.cs @@ -201,18 +201,20 @@ namespace PDFPatcher void OnDocumentChanged(object sender, DocumentChangedEventArgs args) { var p = args.Path; _MainStatusLabel.Text = p ?? String.Empty; - if (FileHelper.IsPathValid(p)) { - p = System.IO.Path.GetFileNameWithoutExtension(p); - if (p.Length > 20) { - p = p.Substring(0, 17) + "..."; - } - if (sender is Control f) { - f = f.Parent; - if (f == null) { - return; - } - f.Text = p; + if (!FileHelper.IsPathValid(p)) { + return; + } + + p = System.IO.Path.GetFileNameWithoutExtension(p); + if (p.Length > 20) { + p = p.Substring(0, 17) + "..."; + } + if (sender is Control f) { + f = f.Parent; + if (f == null) { + return; } + f.Text = p; } } diff --git a/App/Model/ColorSpaces/ColorSpaceHelper.cs b/App/Model/ColorSpaces/ColorSpaceHelper.cs index 8fdf65b..caa901b 100644 --- a/App/Model/ColorSpaces/ColorSpaceHelper.cs +++ b/App/Model/ColorSpaces/ColorSpaceHelper.cs @@ -1,7 +1,5 @@ using System; using System.Drawing; -using System.Drawing.Imaging; -using System.Text; namespace Devcorp.Controls.Design { @@ -153,22 +151,22 @@ namespace Devcorp.Controls.Design public static Color HexToColor(string hexColor) { string r, g, b; - if (hexColor != String.Empty) { - hexColor = hexColor.Trim(); - if (hexColor[0] == '#') hexColor = hexColor.Substring(1, hexColor.Length - 1); - - r = hexColor.Substring(0, 2); - g = hexColor.Substring(2, 2); - b = hexColor.Substring(4, 2); - - r = Convert.ToString(16 * GetIntFromHex(r.Substring(0, 1)) + GetIntFromHex(r.Substring(1, 1))); - g = Convert.ToString(16 * GetIntFromHex(g.Substring(0, 1)) + GetIntFromHex(g.Substring(1, 1))); - b = Convert.ToString(16 * GetIntFromHex(b.Substring(0, 1)) + GetIntFromHex(b.Substring(1, 1))); - - return Color.FromArgb(Convert.ToInt32(r), Convert.ToInt32(g), Convert.ToInt32(b)); + if (hexColor == String.Empty) { + return Color.Empty; } - return Color.Empty; + hexColor = hexColor.Trim(); + if (hexColor[0] == '#') hexColor = hexColor.Substring(1, hexColor.Length - 1); + + r = hexColor.Substring(0, 2); + g = hexColor.Substring(2, 2); + b = hexColor.Substring(4, 2); + + r = Convert.ToString(16 * GetIntFromHex(r.Substring(0, 1)) + GetIntFromHex(r.Substring(1, 1))); + g = Convert.ToString(16 * GetIntFromHex(g.Substring(0, 1)) + GetIntFromHex(g.Substring(1, 1))); + b = Convert.ToString(16 * GetIntFromHex(b.Substring(0, 1)) + GetIntFromHex(b.Substring(1, 1))); + + return Color.FromArgb(Convert.ToInt32(r), Convert.ToInt32(g), Convert.ToInt32(b)); } /// diff --git a/App/Model/PdfPath/PathAxes.cs b/App/Model/PdfPath/PathAxes.cs index bb3334d..e5eda1e 100644 --- a/App/Model/PdfPath/PathAxes.cs +++ b/App/Model/PdfPath/PathAxes.cs @@ -1,6 +1,5 @@ using System; using System.Collections.Generic; -using System.Text; using PDFPatcher.Common; namespace PDFPatcher.Model.PdfPath @@ -97,12 +96,14 @@ namespace PDFPatcher.Model.PdfPath public PathAxisType Type => PathAxisType.Children; public DocumentObject SelectObject(DocumentObject source, string name, IEnumerable predicates) { - if (source != null && source.HasChildren) { - var r = new List(); - foreach (var item in source.Children) { - if (MatchesPredicate(item, name, predicates)) { - return item; - } + if (source == null || !source.HasChildren) { + return null; + } + + var r = new List(); + foreach (var item in source.Children) { + if (MatchesPredicate(item, name, predicates)) { + return item; } } return null; diff --git a/App/Model/SourceItem.cs b/App/Model/SourceItem.cs index ef12331..c0929d7 100644 --- a/App/Model/SourceItem.cs +++ b/App/Model/SourceItem.cs @@ -303,16 +303,18 @@ namespace PDFPatcher.Model public void Reload() { Items.Clear(); - if (FilePath.ExistsDirectory) { - var p = FilePath.ToString(); - var l = Items; - if (AppContext.Merger.SubFolderBeforeFiles) { - AddSubDirectories(p, l); - AddFiles(p, l); - } - else { - AddSubDirectoriesAndFiles(p, l); - } + if (!FilePath.ExistsDirectory) { + return; + } + + var p = FilePath.ToString(); + var l = Items; + if (AppContext.Merger.SubFolderBeforeFiles) { + AddSubDirectories(p, l); + AddFiles(p, l); + } + else { + AddSubDirectoriesAndFiles(p, l); } } @@ -373,17 +375,19 @@ namespace PDFPatcher.Model protected SourceItem(FilePath path, int pageCount) { PageCount = pageCount; - if (path.IsValidPath) { - FilePath = path; - FileName = path.FileName; - FolderName = path.Directory; - if (AppContext.Merger.AutoBookmarkTitle == false) { - return; - } - var t = path.ExistsDirectory ? FileName : path.FileNameWithoutExtension; - if (t.Length > 0) { - Bookmark = CreateBookmarkSettings(t); - } + if (!path.IsValidPath) { + return; + } + + FilePath = path; + FileName = path.FileName; + FolderName = path.Directory; + if (AppContext.Merger.AutoBookmarkTitle == false) { + return; + } + var t = path.ExistsDirectory ? FileName : path.FileNameWithoutExtension; + if (t.Length > 0) { + Bookmark = CreateBookmarkSettings(t); } } diff --git a/App/Processor/DocInfoExporter.cs b/App/Processor/DocInfoExporter.cs index 1833aa0..e55b626 100644 --- a/App/Processor/DocInfoExporter.cs +++ b/App/Processor/DocInfoExporter.cs @@ -264,31 +264,34 @@ namespace PDFPatcher.Processor } w.WriteEndElement(); } - if (catalog.Contains(PdfName.PAGELABELS)) { - var labels = ExtractPageLabels(catalog.GetAsDict(PdfName.PAGELABELS)); - if (labels.Count > 0) { - w.WriteStartElement(Constants.PageLabels); - foreach (var item in labels) { - w.WriteStartElement(Constants.PageLabelsAttributes.Style); - w.WriteAttributeString(Constants.PageLabelsAttributes.PageNumber, item.PageNumber.ToText()); - if (item.StartPage != 0) { - w.WriteAttributeString(Constants.PageLabelsAttributes.StartPage, item.StartPage.ToText()); - } - if (String.IsNullOrEmpty(item.Prefix) == false) { - w.WriteAttributeString(Constants.PageLabelsAttributes.Prefix, item.Prefix); - } - if (String.IsNullOrEmpty(item.Style) == false) { - w.WriteAttributeString(Constants.PageLabelsAttributes.Style, - ValueHelper.MapValue(item.Style[0], - Constants.PageLabelStyles.PdfValues, - Constants.PageLabelStyles.Names, - item.Style) - ); - } - w.WriteEndElement(); + + if (!catalog.Contains(PdfName.PAGELABELS)) { + return; + } + + var labels = ExtractPageLabels(catalog.GetAsDict(PdfName.PAGELABELS)); + if (labels.Count > 0) { + w.WriteStartElement(Constants.PageLabels); + foreach (var item in labels) { + w.WriteStartElement(Constants.PageLabelsAttributes.Style); + w.WriteAttributeString(Constants.PageLabelsAttributes.PageNumber, item.PageNumber.ToText()); + if (item.StartPage != 0) { + w.WriteAttributeString(Constants.PageLabelsAttributes.StartPage, item.StartPage.ToText()); + } + if (String.IsNullOrEmpty(item.Prefix) == false) { + w.WriteAttributeString(Constants.PageLabelsAttributes.Prefix, item.Prefix); + } + if (String.IsNullOrEmpty(item.Style) == false) { + w.WriteAttributeString(Constants.PageLabelsAttributes.Style, + ValueHelper.MapValue(item.Style[0], + Constants.PageLabelStyles.PdfValues, + Constants.PageLabelStyles.Names, + item.Style) + ); } w.WriteEndElement(); } + w.WriteEndElement(); } } diff --git a/App/Processor/DocInfoImporter.cs b/App/Processor/DocInfoImporter.cs index 8f4b6e8..76bb6ae 100644 --- a/App/Processor/DocInfoImporter.cs +++ b/App/Processor/DocInfoImporter.cs @@ -418,21 +418,23 @@ namespace PDFPatcher.Processor private static void ImportBorder(string border, PdfAnnotation ann) { var bs = ToInt32Array(border); PdfBorderArray a; - if (bs != null) { - switch (bs.Length) { - case 3: a = new PdfBorderArray(bs[0], bs[1], bs[2]); break; - case 4: - var dp = new int[bs.Length - 3]; - bs.CopyTo(dp, 3); - a = new PdfBorderArray(bs[0], bs[1], bs[2], GetPdfDashPattern(dp)); - break; - default: - a = null; - break; - } - if (a != null) { - ann.Put(PdfName.BORDER, a); - } + if (bs == null) { + return; + } + + switch (bs.Length) { + case 3: a = new PdfBorderArray(bs[0], bs[1], bs[2]); break; + case 4: + var dp = new int[bs.Length - 3]; + bs.CopyTo(dp, 3); + a = new PdfBorderArray(bs[0], bs[1], bs[2], GetPdfDashPattern(dp)); + break; + default: + a = null; + break; + } + if (a != null) { + ann.Put(PdfName.BORDER, a); } } @@ -664,31 +666,33 @@ namespace PDFPatcher.Processor bs.Put(PdfName.W, new PdfNumber(bw)); } - if (string.IsNullOrEmpty(borderStyle) == false) { - PdfName s; - switch (borderStyle) { - case "方框": s = PdfName.S; break; - case "下划线": s = PdfName.U; break; - case "凸起": s = PdfName.B; break; - case "凹陷": s = PdfName.I; break; - case "虚线": - s = PdfName.D; - if (string.IsNullOrEmpty(borderPattern) == false) { - var p = ToInt32Array(borderPattern); - if (p != null) { - var dp = GetPdfDashPattern(p); - if (dp != null) { - bs.Put(PdfName.D, dp); - } + if (string.IsNullOrEmpty(borderStyle)) { + return bs; + } + + PdfName s; + switch (borderStyle) { + case "方框": s = PdfName.S; break; + case "下划线": s = PdfName.U; break; + case "凸起": s = PdfName.B; break; + case "凹陷": s = PdfName.I; break; + case "虚线": + s = PdfName.D; + if (string.IsNullOrEmpty(borderPattern) == false) { + var p = ToInt32Array(borderPattern); + if (p != null) { + var dp = GetPdfDashPattern(p); + if (dp != null) { + bs.Put(PdfName.D, dp); } } - break; - default: - s = new PdfName(borderStyle); - break; - } - bs.Put(PdfName.S, s); + } + break; + default: + s = new PdfName(borderStyle); + break; } + bs.Put(PdfName.S, s); return bs; } diff --git a/App/Processor/Imaging/ImageInfo.cs b/App/Processor/Imaging/ImageInfo.cs index 5e2c33c..de8959f 100644 --- a/App/Processor/Imaging/ImageInfo.cs +++ b/App/Processor/Imaging/ImageInfo.cs @@ -410,42 +410,44 @@ namespace PDFPatcher.Processor.Imaging if (info.ColorSpace != null) { return; } - if (cs.Type == PdfObject.ARRAY) { - var colorspace = cs as PdfArray; - // todo: 是否需要将所有 ColorSpace 换成 PaletteColorSpace - if (PdfName.ICCBASED.Equals(colorspace.GetAsName(0))) { - var iccs = colorspace.GetDirectObject(1) as PRStream; - info.ColorSpace = iccs.GetAsName(PdfName.ALTERNATE); - return; - } - if (PdfName.INDEXED.Equals(colorspace.GetAsName(0))) { - var o = colorspace.GetDirectObject(1); - info.PaletteColorSpace = o as PdfName; - if (info.PaletteColorSpace == null) { - var arr = o as PdfArray; - if (arr != null && arr.Size == 2) { - if (PdfName.ICCBASED.Equals(arr.GetAsName(0)) && arr.Size == 2) { - var iccs = arr.GetDirectObject(1) as PRStream; - info.ColorSpace = iccs.GetAsName(PdfName.ALTERNATE) ?? PdfName.DEVICERGB; - info.ICCProfile = PdfReader.GetStreamBytes(iccs); - } - else { - info.ColorSpace = arr.GetAsName(0); - //Tracker.TraceMessage (String.Concat ("不支持此图片的色域:", info.ColorSpace)); - } + + if (cs.Type != PdfObject.ARRAY) { + return; + } + + var colorspace = cs as PdfArray; + // todo: 是否需要将所有 ColorSpace 换成 PaletteColorSpace + if (PdfName.ICCBASED.Equals(colorspace.GetAsName(0))) { + var iccs = colorspace.GetDirectObject(1) as PRStream; + info.ColorSpace = iccs.GetAsName(PdfName.ALTERNATE); + return; + } + if (PdfName.INDEXED.Equals(colorspace.GetAsName(0))) { + var o = colorspace.GetDirectObject(1); + info.PaletteColorSpace = o as PdfName; + if (info.PaletteColorSpace == null) { + var arr = o as PdfArray; + if (arr != null && arr.Size == 2) { + if (PdfName.ICCBASED.Equals(arr.GetAsName(0)) && arr.Size == 2) { + var iccs = arr.GetDirectObject(1) as PRStream; + info.ColorSpace = iccs.GetAsName(PdfName.ALTERNATE) ?? PdfName.DEVICERGB; + info.ICCProfile = PdfReader.GetStreamBytes(iccs); + } + else { + info.ColorSpace = arr.GetAsName(0); + //Tracker.TraceMessage (String.Concat ("不支持此图片的色域:", info.ColorSpace)); } } - var csp = colorspace.GetDirectObject(3); - if (csp.IsString()) { - info.PaletteBytes = ((PdfString)csp).GetOriginalBytes(); - } - else if (csp is PRStream) { - info.PaletteBytes = PdfReader.GetStreamBytes((PRStream)csp); - } - //} } + var csp = colorspace.GetDirectObject(3); + if (csp.IsString()) { + info.PaletteBytes = ((PdfString)csp).GetOriginalBytes(); + } + else if (csp is PRStream) { + info.PaletteBytes = PdfReader.GetStreamBytes((PRStream)csp); + } + //} } - return; } internal void ConvertDecodedBytes(ref byte[] bytes) { diff --git a/App/Processor/InfoXmlProcessors/ClearDestinationOffsetProcessor.cs b/App/Processor/InfoXmlProcessors/ClearDestinationOffsetProcessor.cs index c08f37c..c1d6570 100644 --- a/App/Processor/InfoXmlProcessors/ClearDestinationOffsetProcessor.cs +++ b/App/Processor/InfoXmlProcessors/ClearDestinationOffsetProcessor.cs @@ -78,15 +78,16 @@ namespace PDFPatcher.Processor } private static IUndoAction ClearPositionOffset(System.Xml.XmlElement item, string coordinate) { - if (item.HasAttribute(coordinate)) { - var l = item.GetAttribute(coordinate); - if (l.Trim() == "0") { - return null; - } - item.RemoveAttribute(coordinate); - return new SetAttributeAction(item, coordinate, l); + if (!item.HasAttribute(coordinate)) { + return null; } - return null; + + var l = item.GetAttribute(coordinate); + if (l.Trim() == "0") { + return null; + } + item.RemoveAttribute(coordinate); + return new SetAttributeAction(item, coordinate, l); } #endregion diff --git a/App/Processor/InfoXmlProcessors/LevelDownProcessor.cs b/App/Processor/InfoXmlProcessors/LevelDownProcessor.cs index f689fd1..ad61cec 100644 --- a/App/Processor/InfoXmlProcessors/LevelDownProcessor.cs +++ b/App/Processor/InfoXmlProcessors/LevelDownProcessor.cs @@ -1,7 +1,4 @@ - -using System; - -namespace PDFPatcher.Processor +namespace PDFPatcher.Processor { sealed class LevelDownProcessor : IPdfInfoXmlProcessor { @@ -15,13 +12,14 @@ namespace PDFPatcher.Processor } var undo = new UndoActionGroup(); var n = item.SelectSingleNode("preceding-sibling::" + Constants.Bookmark + "[1]"); - if (n != null) { - undo.Add(new AddElementAction(item)); - n.AppendChild(item); - undo.Add(new RemoveElementAction(item)); - return undo; + if (n == null) { + return null; } - return null; + + undo.Add(new AddElementAction(item)); + n.AppendChild(item); + undo.Add(new RemoveElementAction(item)); + return undo; } #endregion diff --git a/App/Processor/InfoXmlProcessors/UndoAction.cs b/App/Processor/InfoXmlProcessors/UndoAction.cs index 5c9ca59..0d376c9 100644 --- a/App/Processor/InfoXmlProcessors/UndoAction.cs +++ b/App/Processor/InfoXmlProcessors/UndoAction.cs @@ -46,19 +46,19 @@ namespace PDFPatcher.Processor } public IEnumerable Undo() { - if (CanUndo) { - _names.RemoveAt(_names.Count - 1); - if (_names.Count > 100 && _names.Capacity > 200) { - _names.TrimExcess(); - } - var a = _undoActions.Pop(); - a.Undo(); - OnUndo?.Invoke(this, a); - return a.AffectedElements; + if (!CanUndo) { + return null; } - return null; - } + _names.RemoveAt(_names.Count - 1); + if (_names.Count > 100 && _names.Capacity > 200) { + _names.TrimExcess(); + } + var a = _undoActions.Pop(); + a.Undo(); + OnUndo?.Invoke(this, a); + return a.AffectedElements; + } } sealed class UndoActionGroup : IUndoAction diff --git a/App/Processor/Mupdf/MuText.cs b/App/Processor/Mupdf/MuText.cs index 496e6ef..70d63d8 100644 --- a/App/Processor/Mupdf/MuText.cs +++ b/App/Processor/Mupdf/MuText.cs @@ -1,8 +1,6 @@ using System; using System.Collections.Generic; using System.Diagnostics; -using System.Runtime.InteropServices; -using CC = System.Runtime.InteropServices.CallingConvention; #pragma warning disable 649, 169 namespace MuPdfSharp @@ -82,14 +80,16 @@ namespace MuPdfSharp private bool disposedValue = false; // 要检测冗余调用 void Dispose(bool disposing) { - if (!disposedValue) { - if (disposing) { - _Blocks = null; - } - - _handle.DisposeHandle(); - disposedValue = true; + if (disposedValue) { + return; } + + if (disposing) { + _Blocks = null; + } + + _handle.DisposeHandle(); + disposedValue = true; } ~MuTextPage() { diff --git a/App/Processor/Mupdf/RenderResultCache.cs b/App/Processor/Mupdf/RenderResultCache.cs index f18b1bf..1b56ed1 100644 --- a/App/Processor/Mupdf/RenderResultCache.cs +++ b/App/Processor/Mupdf/RenderResultCache.cs @@ -1,7 +1,6 @@ using System; using System.Collections.Generic; using System.Drawing; -using System.Text; using PDFPatcher.Common; namespace MuPdfSharp @@ -48,19 +47,21 @@ namespace MuPdfSharp } private void TrimBitmapBuffer(int pageNumber) { - if (_buffer.Count > __bufferSize) { - int x = 0; - int i = 0; - foreach (var item in _buffer.Keys) { - if (Math.Abs(item - pageNumber) > x) { - x = Math.Abs(item - pageNumber); - i = item; - } - } - _buffer[i].Dispose(); - _buffer.Remove(i); - //Tracker.DebugMessage ("removed buffered result " + i); + if (__bufferSize >= _buffer.Count) { + return; } + + int x = 0; + int i = 0; + foreach (var item in _buffer.Keys) { + if (Math.Abs(item - pageNumber) > x) { + x = Math.Abs(item - pageNumber); + i = item; + } + } + _buffer[i].Dispose(); + _buffer.Remove(i); + //Tracker.DebugMessage ("removed buffered result " + i); } public void Clear() { diff --git a/App/Processor/PdfContentExport.cs b/App/Processor/PdfContentExport.cs index 9fac5e7..c37be37 100644 --- a/App/Processor/PdfContentExport.cs +++ b/App/Processor/PdfContentExport.cs @@ -19,11 +19,12 @@ namespace PDFPatcher.Processor private bool AddReferenceRecord(PdfIndirectReference r, string type) { var k = String.Concat(r.Number, ' ', r.Generation); - if (_resolvedReferences.ContainsKey(k) == false) { - _resolvedReferences.Add(k, type); - return true; + if (_resolvedReferences.ContainsKey(k)) { + return false; } - return false; + + _resolvedReferences.Add(k, type); + return true; } diff --git a/App/Processor/PdfDocumentCreator.cs b/App/Processor/PdfDocumentCreator.cs index 56b67f6..e2802a2 100644 --- a/App/Processor/PdfDocumentCreator.cs +++ b/App/Processor/PdfDocumentCreator.cs @@ -134,24 +134,27 @@ namespace PDFPatcher.Processor default: break; } - if (sourceFile.HasSubItems) { - var p = false; - var pn = _writer.CurrentPageNumber; - foreach (var item in sourceFile.Items) { - ProcessFile(item, b ?? bookmarkContainer); - if (p) { - continue; - } - p = true; - var t = b; - while (b?.Page == 0) { - b.Page = pn; - b.DestinationView = Constants.DestinationAttributes.ViewType.XYZ; - b.Top = _doc.PageSize.Height; - b = b.ParentBookmark; - } - b = t; + + if (!sourceFile.HasSubItems) { + return; + } + + var p = false; + var pn = _writer.CurrentPageNumber; + foreach (var item in sourceFile.Items) { + ProcessFile(item, b ?? bookmarkContainer); + if (p) { + continue; } + p = true; + var t = b; + while (b?.Page == 0) { + b.Page = pn; + b.DestinationView = Constants.DestinationAttributes.ViewType.XYZ; + b.Top = _doc.PageSize.Height; + b = b.ParentBookmark; + } + b = t; } }