fix rewritten shrink content function

This commit is contained in:
Georgy Litvinov 2021-08-26 16:38:55 +02:00
parent f58b4f58de
commit 5bb9600439
2 changed files with 203 additions and 331 deletions

View file

@ -6,6 +6,7 @@ End Sub
Public Const MAX_CHAR_KERNING = 20
Public Const MIN_CHAR_KERNING = -15
Public Const MIN_SPACING_TO_SHRINK = 500
Sub resetNotesStyle
Dim oDescriptor As Object
@ -487,13 +488,26 @@ Function isLowerCase(character As String) As Boolean
End Function
Sub balanceParaTail(targetPara As Object)
' Globalscope.BasicLibraries.LoadLibrary( "MRILib" )
Dim oViewCursor As Object
Dim oTextCursor As Object
Dim oPara As Object
Dim oParaStart As Object
Dim oParaEnd As Object
Dim oContent As Object
Dim oContentStart As Object
Dim oContentEnd As Object
oViewCursor = ThisComponent.CurrentController.getViewCursor()
oViewCursor.goToRange(targetPara, false)
oTextCursor = oViewCursor.Text.createTextCursorByRange(oViewCursor)
oContentStart = getParaStart(oTextCursor)
oContentEnd = getParaEnd(oTextCursor)
oContent = getParaSelected(oContentStart,oContentEnd)
balanceContentTail(oContent)
End Sub
Sub balanceContentTail(oContent As Object)
' Globalscope.BasicLibraries.LoadLibrary( "MRILib" )
Dim oViewCursor As Object
Dim paraLen As Integer
Dim lineCount As Integer
Dim initialLineCount As Integer
@ -507,24 +521,16 @@ Sub balanceParaTail(targetPara As Object)
fallBackSuccess = false
decreaseKerningFailed = false
increaseKerningFailed = false
oViewCursor = ThisComponent.CurrentController.getViewCursor()
oViewCursor.goToRange(targetPara, false)
oTextCursor = oViewCursor.Text.createTextCursorByRange(oViewCursor)
oParaStart = getParaStart(oTextCursor)
oParaEnd = getParaEnd(oTextCursor)
oPara = getParaSelected(oParaStart,oParaEnd)
paraLen = Len(oPara.String)
paraLines = getParaLines(oPara)
paraLen = Len(oContent.String)
paraLines = getContentLines(oContent)
initialLineCount = getParaLinesCount(paraLines)
lineLen = getParaLineLength(paraLines, 0)
medianLen = calculateMedianParaLen(oPara)
medianLen = calculateMedianParaLen(oContent)
minLastLineLength = medianLen * 0.93
If Not IsEmpty(oPara.CharKerning) Then
initialCharKerning = oPara.CharKerning
If Not IsEmpty(oContent.CharKerning) Then
initialCharKerning = oContent.CharKerning
Else
initialCharKerning = 0
End If
@ -534,15 +540,15 @@ Sub balanceParaTail(targetPara As Object)
EndIf
Do While lastLineIsNotBalanced(lineLen, minLastLineLength)
decreaseCharKerning(oPara)
paraLines = getParaLines(oPara)
decreaseCharKerning(oContent)
paraLines = getContentLines(oContent)
lineCount = getParaLinesCount(paraLines)
lineLen = getParaLineLength(paraLines,0)
If (lineCount < initialLineCount ) OR (oPara.CharKerning < MIN_CHAR_KERNING) Then
If (lineCount < initialLineCount ) OR (oContent.CharKerning < MIN_CHAR_KERNING) Then
'Tightened last line but it is still smaller than we need
fallBackSuccess = tryExpandPrevLines(oPara, minLastLineLength)
fallBackSuccess = tryExpandPrevLines(oContent, minLastLineLength)
If Not fallBackSuccess Then
oPara.CharKerning = initialCharKerning
oContent.CharKerning = initialCharKerning
EndIf
Exit Do
EndIf
@ -550,13 +556,56 @@ Sub balanceParaTail(targetPara As Object)
oViewCursor.collapseToEnd()
End Sub
Function shrinkContentWithKerning(oContent As Object) As Boolean
' Globalscope.BasicLibraries.LoadLibrary( "MRILib" )
shrinkContentWithKerning = false
Dim paraLen As Integer
Dim lineCount As Integer
Dim saveLineCount As Integer
Dim initialLineCount As Integer
Dim contentLines() As Object
Dim kerningValue As Integer
contentLines = getContentLines(oContent)
initialLineCount = getParaLinesCount(contentLines)
saveLineCount = initialLineCount
If Not IsEmpty(oContent.CharKerning) Then
initialCharKerning = oContent.CharKerning
Else
initialCharKerning = 0
End If
kerningValue = initialCharKerning
'Reduce kerning is useless if content is less than 2 lines long
If initialLineCount < 2 Then
Exit Function
EndIf
Do While oContent.CharKerning > MIN_CHAR_KERNING
decreaseCharKerning(oContent)
contentLines = getContentLines(oContent)
lineCount = getParaLinesCount(contentLines)
If lineCount < saveLineCount Then
kerningValue = oContent.CharKerning
saveLineCount = lineCount
EndIf
Loop
'If decrease kerning didn't reduce lines then retreat to latest value that changed lines count
If kerningValue <> oContent.CharKerning Then
oContent.CharKerning = kerningValue
EndIf
If initialCharKerning <> kerningValue Then
shrinkContentWithKerning = true
EndIf
End Function
Function tryExpandPrevLines(oPara As Object, minLastLineLength As Integer) As Boolean
Dim lineCount As Integer
Dim initialLineCount As Integer
Dim paraLine As Object
Dim lineNum As Integer
Dim failedLines() As Integer
paraLines = getParaLines(oPara)
paraLines = getContentLines(oPara)
lineLen = getParaLineLength(paraLines,0)
initialLineCount = getParaLinesCount(paraLines)
lineCount = initialLineCount
@ -567,13 +616,13 @@ Function tryExpandPrevLines(oPara As Object, minLastLineLength As Integer) As Bo
EndIf
paraLine = paraLines(UBound(paraLines) - lineNum)
increaseCharKerning(paraLine)
paraLines = getParaLines(oPara)
paraLines = getContentLines(oPara)
lineCount = getParaLinesCount(paraLines)
lineLen = getParaLineLength(paraLines,0)
If lineNum > 1 And (lineCount <> initialLineCount Or paraLine.CharKerning > MAX_CHAR_KERNING) Then
AddToArray(failedLines, lineNum)
decreaseCharKerning(paraLine)
paraLines = getParaLines(oPara)
paraLines = getContentLines(oPara)
lineCount = getParaLinesCount(paraLines)
lineLen = getParaLineLength(paraLines,0)
lineNum = 0
@ -606,24 +655,32 @@ Function getParaLineLength(paraLines() As Object, lineNumber As Integer) As Inte
End Function
Function getParaLines(oPara As Object) As Variant
Function getContentLines(oContent As Object) As Variant
Dim oTextCursor As Object
Dim oViewCursor As Object
Dim paraLine As Object
Dim paraLines() As Object
oViewCursor = ThisComponent.CurrentController.getViewCursor()
'initial value is 1 As paragraph can't be 0 lines long
oTextCursor = oPara.Text.createTextCursorByRange(oPara)
oTextCursor = oContent.Text.createTextCursorByRange(oContent)
oTextCursor.collapseToStart()
oViewCursor.goToRange(oTextCursor,false)
While NOT oTextCursor.isEndOfParagraph()
oViewCursor.gotoEndOfLine(true)
paraLine = oViewCursor.Text.createTextCursorByRange(oViewCursor)
AddToArray(paraLines, paraLine)
If (Len(oViewCursor.String) = 0) Then
oTextCursor.goToRange(oViewCursor,false)
oTextCursor.goRight(1,false)
oViewCursor.goToRange(oTextCursor,false)
oViewCursor.gotoEndOfLine(true)
EndIf
If (Len(oViewCursor.String) > 0) Then
paraLine = oViewCursor.Text.createTextCursorByRange(oViewCursor)
AddToArray(paraLines, paraLine)
EndIf
oViewCursor.collapseToEnd()
oTextCursor.goToRange(oViewCursor,false)
Wend
getParaLines = paraLines
getContentLines = paraLines
End Function
@ -644,8 +701,10 @@ Sub decreaseCharKerning(oPara As Object)
Wend
Else
initialCharKerning = oPara.CharKerning
EndIf
oPara.CharKerning = initialCharKerning - 2
EndIf
If (initialCharKerning >= MIN_CHAR_KERNING) Then
oPara.CharKerning = initialCharKerning - 2
EndIf
End Sub
Sub increaseCharKerning(oPara As Object)