部分 if 条件翻转以缩小代码嵌套层级

一般是在 if 控制范围很大的情况下进行调整,如果控制范围小则不调整。
从第三方代码的 Demo 中复制的代码块不修改。
This commit is contained in:
Calcitem
2022-01-15 00:03:52 +08:00
committed by WMJ
parent a6ef580a17
commit a25ebdfd4d
24 changed files with 407 additions and 373 deletions
+30 -27
View File
@@ -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;
}
+10 -8
View File
@@ -190,14 +190,16 @@ namespace PDFPatcher.Common
/// <summary>删除路径尾部的 <see cref="Path.DirectorySeparatorChar"/> 或 <see cref="Path.AltDirectorySeparatorChar"/> 字符。</summary>
/// <returns>删除了尾部“\”字符的路径。</returns>
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;
@@ -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);
}
}
}
@@ -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);
}
}
}
+7 -6
View File
@@ -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);
}
}
}
}
}
+11 -9
View File
@@ -618,18 +618,20 @@ namespace EnhancedGlassButton
/// <param name="mevent">A <see cref="T:System.Windows.Forms.MouseEventArgs" /> that contains the event data.</param>
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();
}
}
/// <summary>
+49 -44
View File
@@ -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);
}
}
+12 -11
View File
@@ -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) {
+12 -12
View File
@@ -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;
}
}
}
+14 -16
View File
@@ -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;
}
}
}
}
+13 -11
View File
@@ -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;
}
}
+14 -16
View File
@@ -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));
}
/// <summary>
+8 -7
View File
@@ -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<IPathPredicate> predicates) {
if (source != null && source.HasChildren) {
var r = new List<DocumentObject>();
foreach (var item in source.Children) {
if (MatchesPredicate(item, name, predicates)) {
return item;
}
if (source == null || !source.HasChildren) {
return null;
}
var r = new List<DocumentObject>();
foreach (var item in source.Children) {
if (MatchesPredicate(item, name, predicates)) {
return item;
}
}
return null;
+25 -21
View File
@@ -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);
}
}
+25 -22
View File
@@ -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();
}
}
+41 -37
View File
@@ -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;
}
+34 -32
View File
@@ -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) {
@@ -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
@@ -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
+11 -11
View File
@@ -46,19 +46,19 @@ namespace PDFPatcher.Processor
}
public IEnumerable<XmlNode> 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
+9 -9
View File
@@ -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() {
+14 -13
View File
@@ -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() {
+5 -4
View File
@@ -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;
}
+20 -17
View File
@@ -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;
}
}