fix: multiple fixes for processing archive books

This commit is contained in:
Georgy Litvinov 2021-08-27 22:31:59 +02:00
parent 941ac1c9cc
commit 82c63c82ca
2 changed files with 128 additions and 49 deletions

View file

@ -1,11 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE script:module PUBLIC "-//OpenOffice.org//DTD OfficeDocument 1.0//EN" "module.dtd"> <!DOCTYPE script:module PUBLIC "-//OpenOffice.org//DTD OfficeDocument 1.0//EN" "module.dtd">
<script:module xmlns:script="http://openoffice.org/2000/script" script:name="Archive" script:language="StarBasic" script:moduleType="normal">Sub archMark24 <script:module xmlns:script="http://openoffice.org/2000/script" script:name="Archive" script:language="StarBasic" script:moduleType="normal">Sub archMark25
End Sub End Sub
Public Const MAX_CHAR_KERNING = 20 Public Const MAX_CHAR_KERNING = 15
Public Const MIN_CHAR_KERNING = -15 Public Const MIN_CHAR_KERNING = -10
Public Const MIN_SPACING_TO_SHRINK = 500 Public Const MIN_SPACING_TO_SHRINK = 500
Sub resetNotesStyle Sub resetNotesStyle
@ -173,12 +173,14 @@ sub convertIndesignPageBreaks
EndIf EndIf
turnOffTracking() turnOffTracking()
balanceFootNotes()
Dim oViewCursor As Object Dim oViewCursor As Object
oViewCursor = thisComponent.getCurrentController.getViewCursor oViewCursor = thisComponent.getCurrentController.getViewCursor
oViewCursor.jumpToFirstPage oViewCursor.jumpToFirstPage
Dim oSearch As Object Dim oSearch As Object
Dim oTextCursor As Object Dim oTextCursor As Object
Dim testCurs As Object
Dim firstLowercase As Boolean Dim firstLowercase As Boolean
Dim firstBreak As Boolean Dim firstBreak As Boolean
firstBreak = true firstBreak = true
@ -190,6 +192,7 @@ sub convertIndesignPageBreaks
Dim nextPara As Object Dim nextPara As Object
Dim prevPara As Object Dim prevPara As Object
Dim nextParaFirstCharacter As String Dim nextParaFirstCharacter As String
Dim stringContents As String
firstLowercase = false firstLowercase = false
oSearch = ThisComponent.createSearchDescriptor() oSearch = ThisComponent.createSearchDescriptor()
@ -202,7 +205,15 @@ sub convertIndesignPageBreaks
oTextCursor.gotoRange(oFound,false) oTextCursor.gotoRange(oFound,false)
oTextCursor.gotoStartOfParagraph(false) oTextCursor.gotoStartOfParagraph(false)
oTextCursor.gotoEndOfParagraph(true) oTextCursor.gotoEndOfParagraph(true)
testCurs = oTextCursor.Text.createTextCursorByRange(oTextCursor.End)
testCurs.goRight(1,false)
testCurs.gotoEndOfParagraph(true)
&apos;In case two page breaks go together
stringContents = testCurs.String
If stringContents &lt;&gt; pageBreakMarker Then
oTextCursor.goRight(1,true) oTextCursor.goRight(1,true)
EndIf
oTextCursor.String = &quot;&quot; oTextCursor.String = &quot;&quot;
oTextCursor.BreakType = com.sun.star.style.BreakType.PAGE_BEFORE oTextCursor.BreakType = com.sun.star.style.BreakType.PAGE_BEFORE
&apos;Go to Start of new paragraph to shrink prev page if necessary &apos;Go to Start of new paragraph to shrink prev page if necessary
@ -251,7 +262,7 @@ sub convertIndesignPageBreaks
End If End If
If pageNumber - prevPageNumber &lt; 2 Then If pageNumber - prevPageNumber &lt; 2 Then
&apos; stretchPrevPage(prevPara) &apos;stretchPrevPage(prevPara)
EndIf EndIf
prevPageNumber = pageNumber prevPageNumber = pageNumber
@ -355,7 +366,7 @@ Sub stretchPrevPage()
Dim curPara As Object Dim curPara As Object
oViewCursor = thisComponent.getCurrentController.getViewCursor oViewCursor = thisComponent.getCurrentController.getViewCursor
oSavePosition = oViewCursor.Text.createTextCursorByRange(oViewCursor) oSavePosition = oViewCursor.Text.createTextCursorByRange(oViewCursor)
oViewCursor.goToRange(prevPara, false)
&apos;oViewCursor.goToStartOfLine(false) &apos;oViewCursor.goToStartOfLine(false)
pageNumberCursor = oViewCursor.Text.createTextCursorByRange(oViewCursor) pageNumberCursor = oViewCursor.Text.createTextCursorByRange(oViewCursor)
pageNumberCursor.goToStartOfWord(false) pageNumberCursor.goToStartOfWord(false)
@ -467,15 +478,42 @@ Sub adjustLastLineCurPara()
If success And adjustType = 2 Then If success And adjustType = 2 Then
oTextCursor.ParaLastLineAdjust = 2 oTextCursor.ParaLastLineAdjust = 2
Else Else
TextCursor.ParaIsHyphenation = hyph oTextCursor.ParaIsHyphenation = hyph
EndIf EndIf
End Sub 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(&quot;com.sun.star.text.Paragraph&quot;) Then
oViewCursor.goToRange(enum1Element,false)
adjustLastLineCurPara()
EndIf
Wend
End Sub
Sub adjustLastLine(anchor As Object) Sub adjustLastLine(anchor As Object)
anchor.ParaAdjust = 2 Dim oViewCursor As Object
anchor.ParaLastLineAdjust = 2 oViewCursor = ThisComponent.CurrentController.getViewCursor()
balanceParaTail(anchor) oViewCursor.goToRange(anchor,false)
adjustLastLineCurPara()
End Sub End Sub
Sub adjustFirstLine(anchor As Object) Sub adjustFirstLine(anchor As Object)
@ -560,6 +598,9 @@ Function balanceContentTail(oContent As Object) As Boolean
fallBackSuccess = tryExpandPrevLines(oContent, minLastLineLength) fallBackSuccess = tryExpandPrevLines(oContent, minLastLineLength)
If Not fallBackSuccess Then If Not fallBackSuccess Then
oContent.CharKerning = initialCharKerning oContent.CharKerning = initialCharKerning
oViewCursor.collapseToEnd()
balanceContentTail = false
Exit Function
EndIf EndIf
Exit Do Exit Do
EndIf EndIf
@ -695,23 +736,12 @@ Function getContentLines(oContent As Object) As Variant
getContentLines = paraLines getContentLines = paraLines
End Function End Function
Sub decreaseCharKerning(oPara As Object) Sub decreaseCharKerning(oPara As Object)
Dim initialCharKerning As Integer Dim initialCharKerning As Integer
Dim textExcerpts As Object Dim textExcerpts As Object
Dim textExcerpt As Object Dim textExcerpt As Object
initialCharKerning = MIN_CHAR_KERNING initialCharKerning = MIN_CHAR_KERNING + 2
If(IsEmpty(oPara.CharKerning)) Then If Not (IsEmpty(oPara.CharKerning)) Then
textExcerpts = oPara.createEnumeration()
While textExcerpts.hasMoreElements
textExcerpt = textExcerpts.nextElement
If Not IsEmpty(textExcerpt.CharKerning) Then
If textExcerpt.CharKerning &gt; initialCharKerning Then
initialCharKerning = textExcerpt.CharKerning
EndIf
EndIf
Wend
Else
initialCharKerning = oPara.CharKerning initialCharKerning = oPara.CharKerning
EndIf EndIf
If (initialCharKerning &gt;= MIN_CHAR_KERNING) Then If (initialCharKerning &gt;= MIN_CHAR_KERNING) Then
@ -723,23 +753,12 @@ Sub increaseCharKerning(oPara As Object)
Dim initialCharKerning As Integer Dim initialCharKerning As Integer
Dim textExcerpts As Object Dim textExcerpts As Object
Dim textExcerpt As Object Dim textExcerpt As Object
initialCharKerning = MAX_CHAR_KERNING initialCharKerning = MAX_CHAR_KERNING - 2
If(IsEmpty(oPara.CharKerning)) Then If Not (IsEmpty(oPara.CharKerning)) Then
textExcerpts = oPara.createEnumeration()
While textExcerpts.hasMoreElements
textExcerpt = textExcerpts.nextElement
If Not IsEmpty(textExcerpt.CharKerning) Then
If textExcerpt.CharKerning &lt; initialCharKerning Then
initialCharKerning = textExcerpt.CharKerning
EndIf
EndIf
Wend
Else
initialCharKerning = oPara.CharKerning initialCharKerning = oPara.CharKerning
EndIf EndIf
oPara.CharKerning = initialCharKerning + 2 oPara.CharKerning = initialCharKerning + 2
End Sub End Sub
Function lastLineIsNotBalanced(lineLen As Integer,minLastLineLength As Integer) As Boolean Function lastLineIsNotBalanced(lineLen As Integer,minLastLineLength As Integer) As Boolean
lastLineIsNotBalanced = true lastLineIsNotBalanced = true
If lineLen = 0 Then If lineLen = 0 Then

View file

@ -1225,12 +1225,6 @@ Function canShrinkContent() As Boolean
oViewCursor.goToRange(oSavePosition,false) oViewCursor.goToRange(oSavePosition,false)
Exit Function Exit Function
EndIf EndIf
&apos;If current paragraph has page break before
If oSavePosition.BreakType &lt;&gt; 0 Then
canShrinkContent = false
oViewCursor.goToRange(oSavePosition,false)
Exit Function
EndIf
initPageNum = oViewcursor.getPage() initPageNum = oViewcursor.getPage()
oViewCursor.JumpToPreviousPage(false) oViewCursor.JumpToPreviousPage(false)
oViewCursor.jumpToStartOfPage() oViewCursor.jumpToStartOfPage()
@ -1241,12 +1235,23 @@ Function canShrinkContent() As Boolean
canShrinkContent = false canShrinkContent = false
Exit Function Exit Function
EndIf EndIf
&apos;If current paragraph has page break before
If oSavePosition.BreakType &lt;&gt; 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) oViewCursor.goToRange(oSavePosition,false)
oTextCursor = oViewCursor.Text.createTextCursorByRange(oViewCursor) oTextCursor = oViewCursor.Text.createTextCursorByRange(oViewCursor)
Do While oViewcursor.getPage() = initPageNum Do While oViewcursor.getPage() = initPageNum
oTextCursor.gotoPreviousParagraph(false) oTextCursor.gotoPreviousParagraph(false)
oViewCursor.goToRange(oTextCursor,false) oViewCursor.goToRange(oTextCursor,false)
If oTextCursor.BreakType &lt;&gt; 0 Then If oTextCursor.BreakType &lt;&gt; 0 AND oViewcursor.getPage() = initPageNum Then
canShrinkContent = false canShrinkContent = false
oViewCursor.goToRange(oSavePosition,false) oViewCursor.goToRange(oSavePosition,false)
Exit Function Exit Function
@ -1270,6 +1275,8 @@ Function shrinkPageContent() As Boolean
Dim rounds As Integer Dim rounds As Integer
delta = 0.1 delta = 0.1
rounds = 3 rounds = 3
Dim roundsLineHeight As Integer
roundsLineHeight = 3
Dim i As Integer Dim i As Integer
Dim j As Integer Dim j As Integer
shrinkPageContent = false shrinkPageContent = false
@ -1307,21 +1314,72 @@ Function shrinkPageContent() As Boolean
EndIf EndIf
Next i 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) For i = LBound(targetContent) To Ubound(targetContent)
increaseContentCharHeight(targetContent(i), delta * rounds ) increaseContentCharHeight(targetContent(i), delta * rounds )
Next i Next i
End Function 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) Sub decreaseContentCharHeight(oContent As Object, delta As double)
Dim contentEnum As Object&apos;paragraph enumeration Dim contentEnum As Object&apos;paragraph enumeration
Dim textContentElement As Object&apos;current paragraph Dim textContentElement As Object&apos;current paragraph
Dim curHeight As Double Dim curHeight As Double
Dim oTextCursor As Object
contentEnum = oContent.createEnumeration() contentEnum = oContent.createEnumeration()
Do While contentEnum.hasMoreElements() Do While contentEnum.hasMoreElements()
textContentElement = contentEnum.nextElement() textContentElement = contentEnum.nextElement()
curHeight = textContentElement.charHeight oTextCursor = textContentElement.Text.createTextCursorByRange(textContentElement)
textContentElement.charHeight = roundHeight(curHeight) - delta curHeight = oTextCursor.charHeight
oTextCursor.charHeight = roundHeight(curHeight) - delta
Loop Loop
End Sub End Sub
@ -1329,11 +1387,13 @@ Sub increaseContentCharHeight(oContent As Object, delta As double)
Dim contentEnum As Object&apos;paragraph enumeration Dim contentEnum As Object&apos;paragraph enumeration
Dim textContentElement As Object&apos;current paragraph Dim textContentElement As Object&apos;current paragraph
Dim curHeight As Double Dim curHeight As Double
Dim oTextCursor As Object
contentEnum = oContent.createEnumeration() contentEnum = oContent.createEnumeration()
Do While contentEnum.hasMoreElements() Do While contentEnum.hasMoreElements()
textContentElement = contentEnum.nextElement() textContentElement = contentEnum.nextElement()
curHeight = textContentElement.charHeight oTextCursor = textContentElement.Text.createTextCursorByRange(textContentElement)
textContentElement.charHeight = roundHeight(curHeight) + delta curHeight = oTextCursor.charHeight
oTextCursor.charHeight = roundHeight(curHeight) + delta
Loop Loop
End Sub End Sub