epublishing/ePublishing/Hyphenations.xba

436 lines
12 KiB
Text
Raw Normal View History

2020-03-12 13:01:19 +01:00
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE script:module PUBLIC "-//OpenOffice.org//DTD OfficeDocument 1.0//EN" "module.dtd">
2020-03-12 19:13:33 +01:00
<script:module xmlns:script="http://openoffice.org/2000/script" script:name="Hyphenations" script:language="StarBasic">Private Sub markHyph4
2020-03-12 13:01:19 +01:00
End sub
2020-03-12 14:03:24 +01:00
Private Sub convertHyphInDoc()
Dim description As String
Dim allNotes As Object
dim aNote As Object
2020-05-05 14:01:41 +02:00
description = getTranslation(&quot;hyphenationConfirmation&quot;)
2020-03-12 14:03:24 +01:00
If NOT confirm(description) Then
Exit Sub
EndIf
2020-03-12 13:01:19 +01:00
Dim statusIndicator as Object
statusIndicator = ThisComponent.getCurrentController.StatusIndicator
2020-05-05 14:01:41 +02:00
statusIndicator.Start(getTranslation(&quot;hyphenationsInProgress&quot;),10)
2020-03-12 14:03:24 +01:00
turnOffTracking
2020-03-12 13:01:19 +01:00
AcceptAllTrackedChanges
convertHyphInText(ThisComponent.Text)
&apos; Globalscope.BasicLibraries.LoadLibrary( &quot;MRILib&quot; )
2020-05-05 14:01:41 +02:00
statusIndicator.Start(getTranslation(&quot;hyphenationsInProgress&quot;),70)
2020-03-12 13:01:19 +01:00
allNotes = ThisComponent.FootNotes
For x = 0 to allNotes.Count -1
aNote = allNotes.getByIndex(x)
convertHyphInText(aNote.Text)
Next
&apos;disableAutoHyph()
statusIndicator.end()
2020-05-05 14:01:41 +02:00
MsgBox getTranslation(&quot;hyphenationsSuccess&quot;)
2020-03-12 14:03:24 +01:00
2020-03-12 13:01:19 +01:00
End Sub
2020-03-12 14:03:24 +01:00
Private sub disableAutoHyph()
2020-03-12 13:01:19 +01:00
Dim propertySetInfo As Object
Dim oPositionOfMatch As Long
2020-05-08 16:35:26 +02:00
Dim oFamilies As Object
Dim sElements() As String
Dim oFamily As Object
Dim oStyle As Object
Dim j As Integer
2020-03-12 13:01:19 +01:00
oFamilies = ThisComponent.StyleFamilies
sElements() = oFamilies.getElementNames()
oFamily = oFamilies.getByName(sElements(1))
For j = 0 to oFamily.getCount -1
oStyle = oFamily.getByIndex(j)
propertySetInfo = oStyle.getPropertySetInfo()
If propertySetInfo.hasPropertyByName(&quot;ParaIsHyphenation&quot;) Then
oStyle.ParaIsHyphenation = false
EndIf
Next
End Sub
2020-03-21 12:57:09 +01:00
Private Sub convertHyphInText(textElement As Object)
2020-03-12 13:01:19 +01:00
Dim enum1Element As Object
Dim enum1 As Object
Dim i As Integer
Dim cell As Object
2020-05-08 16:35:26 +02:00
Dim cellNames() As Object
Dim cellText As Object
2020-03-12 13:01:19 +01:00
enum1 = textElement.createEnumeration()
While enum1.hasMoreElements
enum1Element = enum1.nextElement
If enum1Element.supportsService(&quot;com.sun.star.text.Paragraph&quot;) Then
convertParaHyphens(enum1Element)
ElseIf enum1Element.supportsService(&quot;com.sun.star.text.TextTable&quot;) Then
cellNames = enum1Element.cellNames
For i = LBound(cellNames) To Ubound(cellNames)
cell = enum1Element.getCellByName(cellNames(i))
cellText = cell.getText()
convertHyphInText(cellText)
Next i
Else
EndIf
Wend
End Sub
2020-05-08 16:35:26 +02:00
Private Sub convertParaHyphens(para As Object)
2020-03-12 17:48:36 +01:00
Dim lineEnds() As Object
2020-03-12 13:01:19 +01:00
Dim autoHyphens() As Object
Dim autoBreaks() As Object
Dim lineNumbers() As Integer
2020-03-12 17:48:36 +01:00
&apos; Dim lineShifts() As Integer
2020-06-23 16:07:11 +02:00
Dim charNum as Long
2020-03-12 13:01:19 +01:00
Dim lineLength As Integer
2020-05-08 16:35:26 +02:00
Dim lineCursor As Object
2020-03-12 13:01:19 +01:00
Dim i As Integer
2020-03-12 17:48:36 +01:00
Dim vCurs As Object
Dim tCurs As Object
Dim lineCurs As Object
2020-05-08 16:35:26 +02:00
Dim lastChar As String
Dim nextChar As String
2020-03-12 13:01:19 +01:00
&apos;No hyphenation needed
If para.ParaIsHyphenation = false Then
Exit Sub
EndIf
vCurs = ThisComponent.currentController.getViewCursor()
2020-03-12 17:48:36 +01:00
vCurs.goToRange(para.Start,false)
tCurs = para.Text.createTextCursorByRange(vCurs)
2020-03-12 13:01:19 +01:00
tCurs.goToStartOfParagraph(false)
vCurs.goToRange(tCurs,false)
i = 0
Do While NOT tCurs.isEndOfParagraph() OR NOT tCurs.isStartOfParagraph()
2020-03-12 17:48:36 +01:00
vCurs.gotoEndOfLine(false)
lineCursor = para.Text.createTextCursorByRange(vCurs.End)
lineCursor.goToRange(tCurs,true)
lineLength = Len(lineCursor.String)
2020-03-12 13:01:19 +01:00
vCurs.collapseToEnd()
i = i + 1
tCurs.goToRange(vCurs.End,false)
If (tCurs.isEndOfParagraph()) Then
Exit Do
EndIf
lastChar = getPrevChar(tCurs)
nextChar = getNextChar(tCurs)
If Len(nextChar) &gt; 0 Then
charNum = Asc(nextChar)
If charNum &gt; 767 AND charNum &lt; 880 THen
tCurs.goRight(1,false)
lastChar = getPrevChar(tCurs)
nextChar = getNextChar(tCurs)
EndIf
EndIf
If needHyphen(lastChar,nextChar,lineLength) Then
addToArray(autoHyphens(),tCurs.Text.createTextCursorByRange(tCurs))
addToArray(lineNumbers(),i)
2020-03-12 17:48:36 +01:00
&apos; addToArray(lineShifts(),1)
2020-03-12 13:01:19 +01:00
&apos;replaceHyphen(tCurs,vCurs)
ElseIf needLineBreak(lastChar,newChar) Then
addToArray(autoBreaks(),tCurs.Text.createTextCursorByRange(tCurs))
2020-03-12 17:48:36 +01:00
&apos; addToArray(lineShifts(),1)
Else
&apos; addToArray(lineShifts(),0)
2020-03-12 13:01:19 +01:00
EndIf
2020-03-12 17:48:36 +01:00
addToArray(lineEnds(),tCurs.Text.createTextCursorByRange(tCurs))
2020-03-12 13:01:19 +01:00
tCurs.goRight(1,false)
vCurs.goToRange(tCurs,false)
Loop
If Ubound(autoBreaks) &lt;&gt; -1 Then
For i = 0 To (Ubound(autoBreaks))
insertBreak(autoBreaks(i))
Next i
EndIf
para.ParaIsHyphenation = false
If Ubound(autoHyphens) &lt;&gt; -1 Then
For i = 0 To (Ubound(autoHyphens))
replaceHyphen(autoHyphens(i),lineNumbers(i))
Next i
EndIf
2020-03-12 19:13:33 +01:00
verifyHyphenation(lineEnds)
2020-03-12 13:01:19 +01:00
End Sub
2020-03-12 17:48:36 +01:00
Private Sub verifyHyphenation(lineEnds() As Object)
Dim i As Integer
If Lbound(lineEnds) &gt; Ubound(lineEnds) Then
Exit Sub
EndIf
Dim vCurs As Object
vCurs = ThisComponent.currentController.getViewCursor()
For i = (Lbound(lineEnds)) To (Ubound(lineEnds))
vCurs.goToRange(lineEnds(i),false)
If NOT (vCurs.isAtEndOfLine() OR vCurs.isAtStartOfLine()) Then
2020-05-05 14:01:41 +02:00
MsgBox getTranslation(&quot;hyphenationsFailed&quot;)
2020-03-12 17:48:36 +01:00
Stop
EndIf
Next i
2020-03-12 19:13:33 +01:00
&apos; dim description as String
&apos; description = &quot;Проверка прошла успешно&quot;
&apos; If NOT confirm(description) Then
&apos; Stop
&apos; EndIf
2020-03-12 17:48:36 +01:00
End Sub
2020-03-12 14:03:24 +01:00
Private Sub insertBreak(tCursor As Object)
2020-03-12 13:01:19 +01:00
&apos;U+200B Zero length space
tCursor.String = &quot;&quot;
End Sub
2020-05-08 16:35:26 +02:00
Private Function getPrevChar(tCurs As Object) As String
2020-03-12 13:01:19 +01:00
tCurs.goLeft(1,true)
getPrevChar = tCurs.getString()
tCurs.goRight(1,false)
End Function
2020-05-08 16:35:26 +02:00
Private Function getNextChar(tCurs As Object) As String
2020-03-12 13:01:19 +01:00
tCurs.goRight(1,true)
getNextChar = tCurs.getString()
tCurs.goLeft(1,false)
End Function
2020-03-12 14:03:24 +01:00
Private Sub replaceHyphen(tCurs As Object,lineNumber As Integer)
2020-03-12 13:01:19 +01:00
Dim curLineNum As Integer
Dim vCurs As Object
Dim tLine As Object
vCurs = ThisComponent.currentController.getViewCursor()
&apos;insert soft hyphen character U+00AD
tCurs.Text.insertControlCharacter(tCurs.End,com.sun.star.text.ControlCharacter.SOFT_HYPHEN,true)
vCurs.goToRange(tCurs.End,false)
If vCurs.isAtEndOfLine() = false AND vCurs.isAtStartOfLine() = false Then
curLineNum = getParaLineNumber(vCurs)
If curLineNum &gt; lineNumber Then
compressLine(tCurs)
Else
stretchLine(tCurs)
EndIf
EndIf
If vCurs.isAtEndOfLine() = false AND vCurs.isAtStartOfLine() = false Then
2020-05-05 14:01:41 +02:00
MsgBox getTranslation(&quot;hyphenationsFailed&quot;)
2020-03-12 13:01:19 +01:00
Stop
EndIf
End Sub
2020-03-12 14:03:24 +01:00
Private Sub compressLine(tCurs As Object)
2020-03-12 13:01:19 +01:00
Dim vCurs As Object
Dim tLine As Object
Dim kerning As Integer
Dim lineLength As Integer
Dim portions As Integer
Dim portionSize As Integer
Dim textPortions() As Object
Dim startPosition As Object
Dim tPortion As Object
portions = 4
vCurs = ThisComponent.currentController.getViewCursor()
vCurs.goToRange(tCurs,false)
vCurs.goToStartOfLine(true)
vCurs.goLeft(1,true)
vCurs.goToStartOfLine(true)
tLine = vCurs.Text.createTextCursorByRange(vCurs)
lineLength = Len(tLine.String)
startPosition = tLine.End
If lineLength &gt;= 20 Then
portionSize = lineLength/portions
For i = 0 To portions - 2
tPortion = vCurs.Text.createTextCursorByRange(startPosition)
tPortion.goLeft(portionSize,true)
addToArray(textPortions(),tPortion)
startPosition = tPortion.End
Next i
tPortion = vCurs.Text.createTextCursorByRange(startPosition)
tPortion.goToRange(tLine.Start,true)
addToArray(textPortions(),tPortion)
2020-03-12 19:13:33 +01:00
Else
addToArray(textPortions(),tLine)
2020-03-12 13:01:19 +01:00
EndIf
vCurs.goToRange(tCurs,false)
kerning = 10
tLine.CharKerning = kerning
If lineLength &gt; 8 Then
Do While vCurs.isAtEndOfLine() = false AND vCurs.isAtStartOfLine() = false
kerning = kerning - 1
For i = (Lbound(textPortions)) To (Ubound(textPortions))
textPortions(i).CharKerning = kerning
vCurs.goToRange(tCurs,false)
If vCurs.isAtEndOfLine() = true OR vCurs.isAtStartOfLine() = true Then
Exit Do
EndIf
Next i
If kerning &lt; -15 Then
Exit Do
EndIf
Loop
Else
Do While vCurs.isAtEndOfLine() = false AND vCurs.isAtStartOfLine() = false
kerning = kerning - 1
tLine.CharKerning = kerning
vCurs.goToRange(tCurs,false)
If kerning &lt; -15 Then
Exit Do
EndIf
Loop
EndIf
End Sub
2020-03-12 14:03:24 +01:00
Private Sub stretchLine(tCurs As Object)
2020-03-12 13:01:19 +01:00
Dim vCurs As Object
Dim tLine As Object
Dim kerning As Integer
vCurs = ThisComponent.currentController.getViewCursor()
vCurs.goToRange(tCurs,false)
vCurs.goToStartOfLine(true)
tLine = vCurs.Text.createTextCursorByRange(vCurs)
vCurs.goToRange(tCurs,false)
kerning = -10
tLine.CharKerning = kerning
Do While vCurs.isAtEndOfLine() = false AND vCurs.isAtStartOfLine() = false
kerning = kerning + 1
tLine.CharKerning = kerning
vCurs.goToRange(tCurs,false)
If tLine.CharKerning &gt; 15 Then
Exit Do
EndIf
Loop
End Sub
2020-05-08 16:35:26 +02:00
Private Function getParaLineNumber(vCursor As Object) As Integer
2020-03-12 13:01:19 +01:00
Dim lineNumber As Integer
Dim oSavePosition As Object
Dim tCursor As Object
oSavePosition = vCursor.Text.createTextCursorByRange(vCursor)
tCursor = vCursor.Text.createTextCursorByRange(vCursor)
lineNumber = 1
vCursor.gotoStartOfLine(false)
tCursor.gotoRange(vCursor,false)
Do While NOT tCursor.isStartOfParagraph()
vCursor.goLeft(1,false)
vCursor.gotoStartOfLine(false)
tCursor.gotoRange(vCursor,false)
lineNumber = lineNumber + 1
Loop
vCursor.goToRange(oSavePosition,false)
getParaLineNumber = lineNumber
End Function
2020-03-12 14:03:24 +01:00
Private Function needLineBreak(before,after)
2020-03-12 13:01:19 +01:00
needLineBreak = false
If before = &quot;/&quot; Then
If after &lt;&gt; &quot; &quot; AND after &lt;&gt; Chr(10) Then
needLineBreak = true
EndIf
EndIf
End Function
Sub insertLineBreak(tCurs)
tCurs.Text.insertControlCharacter(tCurs.End,com.sun.star.text.ControlCharacter.LINE_BREAK,False)
End Sub
2020-05-08 16:35:26 +02:00
Private Function needHyphen(before As String, after As String,lineLength As Integer) As boolean
2020-06-23 16:07:11 +02:00
Dim charNumBefore as Long
Dim charNumAfter as Long
2020-03-12 13:01:19 +01:00
needHyphen = true
If lineLength &lt; 2 Then
needHyphen = false
EndIf
&apos;MsgBox Asc(newChar)
&apos;Both are numbers
If Len(before) &gt; 0 Then
charNumBefore = Asc(before)
If charNumBefore &gt; 47 AND charNumBefore &lt; 58 Then
If Len(after) &gt; 0 Then
charNumAfter = Asc(after)
If charNumAfter &gt; 47 AND charNumAfter &lt; 58 Then
needHyphen = false
EndIf
EndIf
EndIf
EndIf
Select Case before
Case &quot; &quot;
needHyphen = false
&apos;U+002D Hyphen minus
Case &quot;-&quot;
needHyphen = false
&apos;U+2010 Hyphen
Case &quot;&quot;
needHyphen = false
Case &quot;­&quot;
needHyphen = false
&apos;U+2014 Em dash
Case &quot;—&quot;
needHyphen = false
&apos;U+2013 En dash
Case &quot;&quot;
needHyphen = false
&apos;U+2012 Figure Dash
Case &quot;&quot;
needHyphen = false
Case &quot;…&quot;
needHyphen = false
Case &quot;/&quot;
needHyphen = false
End Select
Select Case after
Case &quot; &quot;
needHyphen = false
Case &quot;-&quot;
needHyphen = false
Case &quot;­&quot;
needHyphen = false
&apos;newline U+000A
Case Chr(10)
needHyphen = false
End Select
End Function
2020-03-12 14:03:24 +01:00
Private sub AcceptAllTrackedChanges
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:AcceptAllTrackedChanges&quot;, &quot;&quot;, 0, Array())
2020-03-12 13:01:19 +01:00
2020-03-12 14:03:24 +01:00
End sub
2020-03-12 13:01:19 +01:00
2020-03-12 14:03:24 +01:00
Private Sub turnOffTracking
document = ThisComponent.CurrentController.Frame
dispatcher = createUnoService(&quot;com.sun.star.frame.DispatchHelper&quot;)
Dim args(0) as new com.sun.star.beans.PropertyValue
args(0).Name = &quot;TrackChanges&quot;
args(0).Value = false
dispatcher.executeDispatch(document, &quot;.uno:TrackChanges&quot;, &quot;&quot;, 0, args())
End Sub
2020-03-12 13:01:19 +01:00
2020-03-12 14:03:24 +01:00
Private Function confirm(description)
If MsgBox (description, 4) =6 Then
confirm = true
Else
confirm = false
EndIf
End Function
2020-03-16 13:53:48 +01:00
Sub Macro1
2020-03-21 12:57:09 +01:00
End Sub
</script:module>