epublishing/ePublishing/MakeUp.xba

309 lines
No EOL
9.5 KiB
XML

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE script:module PUBLIC "-//OpenOffice.org//DTD OfficeDocument 1.0//EN" "module.dtd">
<script:module xmlns:script="http://openoffice.org/2000/script" script:name="MakeUp" script:language="StarBasic" script:moduleType="normal">Sub markMakeUp1
End Sub
Sub incrementCharWidth()
changeCharWidth(1)
End Sub
Sub decrementCharWidth()
changeCharWidth(-1)
End Sub
Sub incrementKern()
changeKern(2)
End Sub
Sub decrementKern()
changeKern(-2)
End Sub
Sub balanceDoublePagesFootnotes
Dim oViewCursor As Object
oViewCursor = Thiscomponent.currentController.getViewCursor()
Dim leftHeight As Long
Dim rightHeight As Long
Dim odd As Integer
Dim rightPageNum As Integer
Dim leftPageNum As Integer
Dim startPage As Integer
Dim docPages As Integer
startPage = oViewCursor.getPage()
docPages = thiscomponent.currentController.pageCount
odd = startPage Mod 2
If startPage &lt;&gt; 1 AND (startPage &lt;&gt; docPages Or odd = 1) Then
If odd = 1 Then
rightPageNum = startPage
leftPageNum = startPage - 1
Else
leftPageNum = startPage
rightPageNum = startPage + 1
EndIf
If pageHasNotes(leftPageNum) And pageHasNotes(rightPageNum) Then
leftHeight = getFootnoteAreaHeight(leftPageNum)
rightHeight = getFootnoteAreaHeight(rightPageNum)
EndIf
MsgBox leftHeight &amp; &quot; &quot; &amp; rightHeight
EndIf
End Sub
Function getFootnoteAreaHeight(pageNumber As Integer)
Dim oViewCursor As Object
Dim fnAreaEnd As Object
Dim pageStyleName As String
Dim oStyles As Object
Dim newHeight As Integer
Dim initialHeight As Integer
oStyles = ThisComponent.StyleFamilies
Dim pageSTyles As Object
pageStyles = oStyles.getByName(oStyles.elementNames(2))
Dim pageStyle As Object
Dim decrement As Integer
Dim tmpPageNum As Integer
decrement = 10000
oViewCursor = Thiscomponent.currentController.getViewCursor()
oViewCursor.jumpToPage(pageNumber)
pageStyleName = getCurPageStyleName()
pageStyle = pageStyles.getByName(pageStyleName)
initialHeight = pageStyle.FootnoteHeight
If pageStyle.FootnoteHeight = 0 Then
pageStyle.FootnoteHeight = pageStyle.Height - pageStyle.TopMargin - pageStyle.BottomMargin
EndIf
&apos;TODO ADD CHECK IF THERE IS A FOOTNOTE AREA ON PAGE
fnAreaEnd = getFNAreaEnd(pageNumber)
Dim savePageHeight As Long
savePageHeight = pageStyle.Height
Do While pageStyle.FootnoteHeight &gt; 10
Do While pageStyle.FootnoteHeight &lt; decrement
decrement = decrement / 2
Loop
newHeight = pageStyle.FootnoteHeight - decrement
pageStyle.FootnoteHeight = newHeight
tmpPageNum = getPageNumberVCurs(fnAreaEnd)
If tmpPageNum &lt;&gt; pageNumber Then
If decrement &lt; 4 Then
pageStyle.FootnoteHeight = pageStyle.FootnoteHeight + decrement * 2
&apos;Hack to force libreoffice to refresh page layout
tmpPageNum = getPageNumberVCurs(fnAreaEnd)
If tmpPageNum &lt;&gt; pageNumber Then
pageStyle.Height = savePageHeight + 100
pageStyle.Height = savePageHeight
EndIf
Exit Do
Else
newHeight = pageStyle.FootnoteHeight + decrement
decrement = decrement / 2
pageStyle.FootnoteHeight = newHeight
&apos;Hack to force libreoffice to refresh page layout
tmpPageNum = getPageNumberVCurs(fnAreaEnd)
If tmpPageNum &lt;&gt; pageNumber Then
pageStyle.Height = savePageHeight + 100
pageStyle.Height = savePageHeight
EndIf
EndIf
EndIf
Loop
tmpPageNum = getPageNumberVCurs(fnAreaEnd)
If tmpPageNum &lt;&gt; pageNumber Then
pageStyle.FootnoteHeight = 0
MsgBox &quot;Error&quot;
Stop
EndIf
getFootnoteAreaHeight = pageStyle.FootnoteHeight
End Function
sub pageDialog
rem ----------------------------------------------------------------------
rem define variables
dim document as object
dim dispatcher as object
rem ----------------------------------------------------------------------
rem get access to the document
document = ThisComponent.CurrentController.Frame
dispatcher = createUnoService(&quot;com.sun.star.frame.DispatchHelper&quot;)
rem ----------------------------------------------------------------------
dispatcher.executeDispatch(document, &quot;.uno:PageSettingDialog&quot;, &quot;&quot;, 0, Array())
end sub
Function getPageNumberVCurs(cursor As object) As Integer
Dim oViewCursor As Object
Dim oSavePosition As Object
oViewCursor = Thiscomponent.currentController.getViewCursor()
oSavePosition = oViewCursor.Text.createTextCursorByRange(oViewCursor)
oViewCursor.goToRange(cursor.Start,false)
getPageNumberVCurs = oViewCursor.getPage()
oViewCursor.goToRange(oViewCursor, false)
End Function
Function pageHasNotes(pageNumber As Integer) As Boolean
Dim allNotes As Object
allNotes = thisComponent.FootNotes
Dim curNote As Object
Dim noteStartPage As Integer
Dim noteEndPage As Integer
Dim oEnum As Object
&apos; Dim noteStartCursor As Object
Dim endtText As Object
pageHasNotes = false
Dim x As Integer
For x = 0 to allNotes.Count -1
curNote = allNotes.getByIndex(x)
noteEndPage = getPageNumberVCurs(curNote.Text.End)
If noteEndPage &gt;= pageNumber Then
noteStartPage = getPageNumberVCurs(curNote.Text.Start)
If pageNumber = noteStartPage Or (noteStartPage &lt; pageNumber And noteEndPage &gt;= pageNumber) Then
pageHasNotes = true
Exit function
EndIf
EndIf
Next x
End Function
Function getFNAreaEnd(pageNumber As Integer)
Dim allNotes As Object
allNotes = thisComponent.FootNotes
Dim curNote As Object
Dim noteStartPage As Integer
Dim noteEndPage As Integer
Dim prevNote As Object
Dim oEnum As Object
&apos; Dim noteStartCursor As Object
Dim endtText As Object
Dim pageHasNotes As Boolean
pageHasNotes = false
Dim x As Integer
For x = 0 to allNotes.Count -1
curNote = allNotes.getByIndex(x)
noteEndPage = getPageNumberVCurs(curNote.Text.End)
If noteEndPage &gt;= pageNumber Then
noteStartPage = getPageNumberVCurs(curNote.Text.Start)
If pageNumber = noteStartPage Or (noteStartPage &lt; pageNumber And noteEndPage &gt;= pageNumber) Then
pageHasNotes = true
EndIf
If pageHasNotes AND pageNumber &lt; noteStartPage Then
getFNAreaEnd = prevNote.Text.createTextCursorByRange(prevNote.Text.End)
Exit function
Endif
If pageHasNotes AND pageNumber &lt; noteEndPage Then
getFNAreaEnd = getFNAreaEndInFootnote(curNote,pageNumber)
Exit function
EndIf
prevNote = curNote
EndIf
Next x
End Function
Function getFNAreaEndInFootnote(note As Object, pageNum As Integer)
Dim oViewCursor As Object
Dim oSavePosition As Object
Dim tmpPageNum As Integer
If getPageNumber(note.Text.Start) &gt; pageNum Then
&apos;ERROR Footnote starts after this page number
Exit Function
EndIf
oViewCursor = Thiscomponent.currentController.getViewCursor()
oSavePosition = oViewCursor.Text.createTextCursorByRange(oViewCursor)
oViewCursor.goToRange(note.Text.End,false)
Do
oViewCursor.gotoStartOfLine(false)
tmpPageNum = getPageNumber(oViewCursor)
If tmpPageNum &lt;= pageNum Then
Exit Do
EndIf
oViewCursor.goLeft(1,false)
tmpPageNum = getPageNumber(oViewCursor)
Loop While tmpPageNum &gt; pageNum
getFNAreaEndInFootnote = oViewCursor.Text.createTextCursorByRange(oViewCursor)
oViewCursor.goToRange(oSavePosition,false)
End Function
Sub getFNAreaStart
Dim pageNumber As Integer
pageNumber = getPageNumber(Thiscomponent.currentController.getViewCursor())
Dim allNotes As Object
allNotes = thisComponent.FootNotes
Dim aNote As Object
Dim oEnum As Object
Dim noteStart As Object
Dim noteEnd As Object
Dim x As Integer
For x = 0 to allNotes.Count -1
aNote = allNotes.getByIndex(x)
&apos;oEnum = aNote.Text.createEnumeration()
noteStart = aNote.Text.createTextCursorByRange(aNote.Text.Start)
noteEnd = aNote.Text.createTextCursorByRange(aNote.Text.End)
If pageNumber = getPageNumber(noteStart) Then
MsgBox &quot;Starts here &quot; &amp; x &amp; &quot; page number &quot; &amp; pageNumber
Exit sub
Endif
If pageNumber = getPageNumber(noteEnd) Then
MsgBox &quot;ends here &quot; + x
Exit sub
EndIf
&apos;Do While oEnum.hasMoreElements()
&apos; oCurPar = oEnum.nextElement()
&apos;Loop
Next
&apos;Globalscope.BasicLibraries.LoadLibrary(&quot;MRILib&quot;)
&apos;mri ThisComponent
End Sub
Function changeCharWidth(delta As Integer) As Boolean
Dim oViewCursor As Object
Dim oTextCursor As Object
Dim charScale As Integer
oViewCursor = ThisComponent.CurrentController.getViewCursor()
If(IsEmpty(oViewCursor.charScaleWidth)) Then
charScale = 100
Else
charScale = oViewCursor.charScaleWidth
End If
oViewCursor = ThisComponent.CurrentController.getViewCursor()
oViewCursor.charScaleWidth = charScale + delta
End Function
Function changeKern(delta As Integer) As Boolean
Dim oViewCursor As Object
Dim oTextCursor As Object
Dim kerning As Integer
oViewCursor = ThisComponent.CurrentController.getViewCursor()
If(IsEmpty(oViewCursor.CharKerning)) Then
kerning = 0
Else
kerning = oViewCursor.CharKerning
End If
oViewCursor.CharKerning = kerning + delta
End Function
sub undoUno
rem ----------------------------------------------------------------------
rem define variables
dim document as object
dim dispatcher as object
rem ----------------------------------------------------------------------
rem get access to the document
document = ThisComponent.CurrentController.Frame
dispatcher = createUnoService(&quot;com.sun.star.frame.DispatchHelper&quot;)
rem ----------------------------------------------------------------------
dispatcher.executeDispatch(document, &quot;.uno:Undo&quot;, &quot;&quot;, 0, Array())
end sub
</script:module>