Skip to content

Commit

Permalink
!优化自动生成书签的逻辑
Browse files Browse the repository at this point in the history
  • Loading branch information
wmjordan committed Mar 9, 2024
1 parent 83b556d commit 16b9366
Showing 1 changed file with 52 additions and 9 deletions.
61 changes: 52 additions & 9 deletions App/Functions/Editor/Controller.cs
Original file line number Diff line number Diff line change
Expand Up @@ -782,8 +782,14 @@ internal void AutoBookmark(IEnumerable<EditModel.AutoBookmarkSettings> list, boo
var h = p.VisualBound.Height;
var dh = p.VisualBound.Bottom - h;
foreach (var block in p.TextPage.Blocks) {
string jointBlockText = null;
bool hasJointText = false;
foreach (var line in block.Lines) {
var matchLine = false;
foreach (var span in line.Spans) {
if (matchLine) {
break;
}
for (var si = 0; si < bs.Count; si++) {
var style = bs[si];
var matcher = mp[si];
Expand All @@ -797,8 +803,13 @@ internal void AutoBookmark(IEnumerable<EditModel.AutoBookmarkSettings> list, boo
}
var b = span.Box;
if (bl < style.Level) {
if (matcher?.Invoke(line.Text) == false) {
continue;
if (matcher != null) {
if (jointBlockText == null) {
jointBlockText = GetLineText(line, block, out hasJointText);
}
if ((matchLine = matcher(jointBlockText)) == false) {
continue;
}
}
bm = CreateNewSiblingBookmark(bm, spans);
++bl;
Expand All @@ -812,7 +823,7 @@ internal void AutoBookmark(IEnumerable<EditModel.AutoBookmarkSettings> list, boo
var lb = b.Bottom;
if (cb.Page == p.PageNumber
&& (bb >= lt && bb <= lb || bt >= lt && bt <= lb || bt < lt && bb > lb)
&& (mergeAdjacentTitle || spans[spans.Count - 1].Point.Y == span.Point.Y)) {
&& (mergeAdjacentTitle || spans[spans.Count - 1].Box.IsHorizontalNeighbor(span.Box))) {
if (/*m == false &&*/ t.Length > 0) {
// 保留英文和数字文本之间的空格
var ct = cb.Title;
Expand All @@ -828,8 +839,13 @@ internal void AutoBookmark(IEnumerable<EditModel.AutoBookmarkSettings> list, boo
}
continue;
}
if (matcher?.Invoke(line.Text) == false) {
continue;
if (matcher != null) {
if (jointBlockText == null) {
jointBlockText = GetLineText(line, block, out hasJointText);
}
if ((matchLine = matcher(jointBlockText)) == false) {
continue;
}
}
bm = CreateNewSiblingBookmarkForParent(bm, spans);
}
Expand All @@ -838,8 +854,13 @@ internal void AutoBookmark(IEnumerable<EditModel.AutoBookmarkSettings> list, boo
bm = bm.ParentBookmark;
--bl;
}
if (matcher?.Invoke(line.Text) == false) {
continue;
if (matcher != null) {
if (jointBlockText == null) {
jointBlockText = GetLineText(line, block, out hasJointText);
}
if ((matchLine = matcher(jointBlockText)) == false) {
continue;
}
}
bm = CreateNewSiblingBookmarkForParent(bm, spans);
}
Expand All @@ -851,7 +872,7 @@ internal void AutoBookmark(IEnumerable<EditModel.AutoBookmarkSettings> list, boo
: s.IsItalic ? FontStyle.Italic
: FontStyle.Regular;
}
be.Title = t;
be.Title = matchLine ? jointBlockText : t;
be.Top = s.GoToTop ? h + dh : h - b.Top + b.Height + dh;
be.Bottom = h - b.Bottom + dh;
be.Action = Constants.ActionType.Goto;
Expand All @@ -866,7 +887,14 @@ internal void AutoBookmark(IEnumerable<EditModel.AutoBookmarkSettings> list, boo
break;
}
}
if (matchLine) {
if (hasJointText) {
goto NEXT_BLOCK;
}
break;
}
}
NEXT_BLOCK:;
}
}
}
Expand All @@ -881,10 +909,25 @@ static BookmarkContainer CreateNewSiblingBookmarkForParent(BookmarkContainer bm,
return bm;
}

static string GetLineText(MuPdfSharp.MuTextLine line, MuPdfSharp.MuTextBlock block, out bool jointLines) {
string t = null;
jointLines = false;
foreach (var item in block.Lines) {
if (line == item) {
t += item.Text;
}
else if (line.BBox.IsHorizontalNeighbor(item.BBox)) {
jointLines = true;
t += item.Text;
}
}
return t;
}

static void TrimBookmarkText(BookmarkContainer bm) {
if (bm is BookmarkElement b) {
var t = b.Title;
var t2 = b.Title.Trim();
var t2 = t.Trim();
if (t2 != t) {
b.Title = t2;
}
Expand Down

0 comments on commit 16b9366

Please sign in to comment.