From 82c63c82caea14a87f4e4be21bf5d4ed20ed745f Mon Sep 17 00:00:00 2001 From: Georgy Litvinov Date: Fri, 27 Aug 2021 22:31:59 +0200 Subject: [PATCH] fix: multiple fixes for processing archive books --- ePublishing/Archive.xba | 93 ++++++++++++++++++++++++---------------- ePublishing/journals.xba | 84 ++++++++++++++++++++++++++++++------ 2 files changed, 128 insertions(+), 49 deletions(-) diff --git a/ePublishing/Archive.xba b/ePublishing/Archive.xba index a636b07..51fad40 100644 --- a/ePublishing/Archive.xba +++ b/ePublishing/Archive.xba @@ -1,11 +1,11 @@ -Sub archMark24 +Sub archMark25 End Sub -Public Const MAX_CHAR_KERNING = 20 -Public Const MIN_CHAR_KERNING = -15 +Public Const MAX_CHAR_KERNING = 15 +Public Const MIN_CHAR_KERNING = -10 Public Const MIN_SPACING_TO_SHRINK = 500 Sub resetNotesStyle @@ -173,12 +173,14 @@ sub convertIndesignPageBreaks EndIf turnOffTracking() + balanceFootNotes() Dim oViewCursor As Object oViewCursor = thisComponent.getCurrentController.getViewCursor oViewCursor.jumpToFirstPage Dim oSearch As Object Dim oTextCursor As Object + Dim testCurs As Object Dim firstLowercase As Boolean Dim firstBreak As Boolean firstBreak = true @@ -190,6 +192,7 @@ sub convertIndesignPageBreaks Dim nextPara As Object Dim prevPara As Object Dim nextParaFirstCharacter As String + Dim stringContents As String firstLowercase = false oSearch = ThisComponent.createSearchDescriptor() @@ -202,7 +205,15 @@ sub convertIndesignPageBreaks oTextCursor.gotoRange(oFound,false) oTextCursor.gotoStartOfParagraph(false) oTextCursor.gotoEndOfParagraph(true) - oTextCursor.goRight(1,true) + testCurs = oTextCursor.Text.createTextCursorByRange(oTextCursor.End) + testCurs.goRight(1,false) + testCurs.gotoEndOfParagraph(true) + 'In case two page breaks go together + stringContents = testCurs.String + If stringContents <> pageBreakMarker Then + oTextCursor.goRight(1,true) + EndIf + oTextCursor.String = "" oTextCursor.BreakType = com.sun.star.style.BreakType.PAGE_BEFORE 'Go to Start of new paragraph to shrink prev page if necessary @@ -251,7 +262,7 @@ sub convertIndesignPageBreaks End If If pageNumber - prevPageNumber < 2 Then - ' stretchPrevPage(prevPara) + 'stretchPrevPage(prevPara) EndIf prevPageNumber = pageNumber @@ -355,7 +366,7 @@ Sub stretchPrevPage() Dim curPara As Object oViewCursor = thisComponent.getCurrentController.getViewCursor oSavePosition = oViewCursor.Text.createTextCursorByRange(oViewCursor) - oViewCursor.goToRange(prevPara, false) + 'oViewCursor.goToStartOfLine(false) pageNumberCursor = oViewCursor.Text.createTextCursorByRange(oViewCursor) pageNumberCursor.goToStartOfWord(false) @@ -467,15 +478,42 @@ Sub adjustLastLineCurPara() If success And adjustType = 2 Then oTextCursor.ParaLastLineAdjust = 2 Else - TextCursor.ParaIsHyphenation = hyph + oTextCursor.ParaIsHyphenation = hyph EndIf End Sub +Sub balanceFootNotes() + Dim allNotes As Object + Dim aNote As Object + Dim x As Long + allNotes = ThisComponent.FootNotes + For x = 0 to allNotes.Count -1 + aNote = allNotes.getByIndex(x) + balanceFootNote(aNote) + Next +End Sub + +Sub balanceFootNote(textElement As Object) + Dim enum1Element As Object + Dim enum1 As Object + Dim oViewCursor As Object + oViewCursor = ThisComponent.CurrentController.getViewCursor() + enum1 = textElement.createEnumeration() + While enum1.hasMoreElements + enum1Element = enum1.nextElement + If enum1Element.supportsService("com.sun.star.text.Paragraph") Then + oViewCursor.goToRange(enum1Element,false) + adjustLastLineCurPara() + EndIf + Wend +End Sub + Sub adjustLastLine(anchor As Object) - anchor.ParaAdjust = 2 - anchor.ParaLastLineAdjust = 2 - balanceParaTail(anchor) + Dim oViewCursor As Object + oViewCursor = ThisComponent.CurrentController.getViewCursor() + oViewCursor.goToRange(anchor,false) + adjustLastLineCurPara() End Sub Sub adjustFirstLine(anchor As Object) @@ -559,7 +597,10 @@ Function balanceContentTail(oContent As Object) As Boolean 'Tightened last line but it is still smaller than we need fallBackSuccess = tryExpandPrevLines(oContent, minLastLineLength) If Not fallBackSuccess Then - oContent.CharKerning = initialCharKerning + oContent.CharKerning = initialCharKerning + oViewCursor.collapseToEnd() + balanceContentTail = false + Exit Function EndIf Exit Do EndIf @@ -695,23 +736,12 @@ Function getContentLines(oContent As Object) As Variant getContentLines = paraLines End Function - Sub decreaseCharKerning(oPara As Object) Dim initialCharKerning As Integer Dim textExcerpts As Object Dim textExcerpt As Object - initialCharKerning = MIN_CHAR_KERNING - If(IsEmpty(oPara.CharKerning)) Then - textExcerpts = oPara.createEnumeration() - While textExcerpts.hasMoreElements - textExcerpt = textExcerpts.nextElement - If Not IsEmpty(textExcerpt.CharKerning) Then - If textExcerpt.CharKerning > initialCharKerning Then - initialCharKerning = textExcerpt.CharKerning - EndIf - EndIf - Wend - Else + initialCharKerning = MIN_CHAR_KERNING + 2 + If Not (IsEmpty(oPara.CharKerning)) Then initialCharKerning = oPara.CharKerning EndIf If (initialCharKerning >= MIN_CHAR_KERNING) Then @@ -723,23 +753,12 @@ Sub increaseCharKerning(oPara As Object) Dim initialCharKerning As Integer Dim textExcerpts As Object Dim textExcerpt As Object - initialCharKerning = MAX_CHAR_KERNING - If(IsEmpty(oPara.CharKerning)) Then - textExcerpts = oPara.createEnumeration() - While textExcerpts.hasMoreElements - textExcerpt = textExcerpts.nextElement - If Not IsEmpty(textExcerpt.CharKerning) Then - If textExcerpt.CharKerning < initialCharKerning Then - initialCharKerning = textExcerpt.CharKerning - EndIf - EndIf - Wend - Else + initialCharKerning = MAX_CHAR_KERNING - 2 + If Not (IsEmpty(oPara.CharKerning)) Then initialCharKerning = oPara.CharKerning EndIf oPara.CharKerning = initialCharKerning + 2 End Sub - Function lastLineIsNotBalanced(lineLen As Integer,minLastLineLength As Integer) As Boolean lastLineIsNotBalanced = true If lineLen = 0 Then diff --git a/ePublishing/journals.xba b/ePublishing/journals.xba index 16b168a..703a46e 100644 --- a/ePublishing/journals.xba +++ b/ePublishing/journals.xba @@ -1225,12 +1225,6 @@ Function canShrinkContent() As Boolean oViewCursor.goToRange(oSavePosition,false) Exit Function EndIf - 'If current paragraph has page break before - If oSavePosition.BreakType <> 0 Then - canShrinkContent = false - oViewCursor.goToRange(oSavePosition,false) - Exit Function - EndIf initPageNum = oViewcursor.getPage() oViewCursor.JumpToPreviousPage(false) oViewCursor.jumpToStartOfPage() @@ -1241,12 +1235,23 @@ Function canShrinkContent() As Boolean canShrinkContent = false Exit Function EndIf + 'If current paragraph has page break before + If oSavePosition.BreakType <> 0 Then + oTextCursor = oSavePosition.Text.createTextCursorByRange(oSavePosition) + oTextCursor.gotoStartOfParagraph(false) + oViewCursor.goToRange(oTextCursor,false) + If oViewCursor.getPage() = initPageNum Then + canShrinkContent = false + oViewCursor.goToRange(oSavePosition,false) + Exit Function + EndIf + EndIf oViewCursor.goToRange(oSavePosition,false) oTextCursor = oViewCursor.Text.createTextCursorByRange(oViewCursor) Do While oViewcursor.getPage() = initPageNum oTextCursor.gotoPreviousParagraph(false) oViewCursor.goToRange(oTextCursor,false) - If oTextCursor.BreakType <> 0 Then + If oTextCursor.BreakType <> 0 AND oViewcursor.getPage() = initPageNum Then canShrinkContent = false oViewCursor.goToRange(oSavePosition,false) Exit Function @@ -1270,6 +1275,8 @@ Function shrinkPageContent() As Boolean Dim rounds As Integer delta = 0.1 rounds = 3 + Dim roundsLineHeight As Integer + roundsLineHeight = 3 Dim i As Integer Dim j As Integer shrinkPageContent = false @@ -1306,22 +1313,73 @@ Function shrinkPageContent() As Boolean Exit Function EndIf Next i - Next j + Next j + For j = 1 To roundsLineHeight + For i = LBound(targetContent) To Ubound(targetContent) + decreaseIntervalHeight(targetContent(i)) + If isShrinkPageSucceded(initPosition, initPageNum) Then + Exit Function + EndIf + Next i + Next j + For j = 1 To roundsLineHeight + For i = LBound(targetContent) To Ubound(targetContent) + increaseIntervalHeight(targetContent(i)) + Next i + Next j For i = LBound(targetContent) To Ubound(targetContent) increaseContentCharHeight(targetContent(i), delta * rounds ) Next i End Function +Function increaseIntervalHeight(oContent As Object) As Boolean + increaseIntervalHeight = false + Dim oParEnum As Object + Dim oPar As Object + Dim oSecEnum As Object + Dim oParSection As Object + Dim pageNum As Integer + Dim lineSpacing As Object + If Not IsMissing(oContent.ParaLineSpacing) Then + lineSpacing = oContent.ParaLineSpacing + If lineSpacing.Mode = 3 Then + lineSpacing.Height = lineSpacing.Height / 0.98 + oContent.ParaLineSpacing = lineSpacing + increaseIntervalHeight = true + End If + End If +End Function + +Function decreaseIntervalHeight(oContent As Object) As Boolean + decreaseIntervalHeight = false + Dim oParEnum As Object + Dim oPar As Object + Dim oSecEnum As Object + Dim oParSection As Object + Dim pageNum As Integer + Dim lineSpacing As Object + If Not IsMissing(oContent.ParaLineSpacing) Then + lineSpacing = oContent.ParaLineSpacing + If lineSpacing.Mode = 3 Then + lineSpacing.Height = lineSpacing.Height * 0.98 + oContent.ParaLineSpacing = lineSpacing + decreaseIntervalHeight = true + End If + EndIf +End Function + Sub decreaseContentCharHeight(oContent As Object, delta As double) Dim contentEnum As Object'paragraph enumeration Dim textContentElement As Object'current paragraph Dim curHeight As Double + Dim oTextCursor As Object contentEnum = oContent.createEnumeration() Do While contentEnum.hasMoreElements() textContentElement = contentEnum.nextElement() - curHeight = textContentElement.charHeight - textContentElement.charHeight = roundHeight(curHeight) - delta + oTextCursor = textContentElement.Text.createTextCursorByRange(textContentElement) + curHeight = oTextCursor.charHeight + oTextCursor.charHeight = roundHeight(curHeight) - delta Loop End Sub @@ -1329,11 +1387,13 @@ Sub increaseContentCharHeight(oContent As Object, delta As double) Dim contentEnum As Object'paragraph enumeration Dim textContentElement As Object'current paragraph Dim curHeight As Double + Dim oTextCursor As Object contentEnum = oContent.createEnumeration() Do While contentEnum.hasMoreElements() textContentElement = contentEnum.nextElement() - curHeight = textContentElement.charHeight - textContentElement.charHeight = roundHeight(curHeight) + delta + oTextCursor = textContentElement.Text.createTextCursorByRange(textContentElement) + curHeight = oTextCursor.charHeight + oTextCursor.charHeight = roundHeight(curHeight) + delta Loop End Sub