From d54bc0b42ae13b9f29f062054f249e9420a4b56e Mon Sep 17 00:00:00 2001 From: Ken Perry Date: Fri, 22 May 2026 10:10:32 -0400 Subject: [PATCH 1/2] Fix inserting text at the correct position before page breaks When the user typed inside a PageBreakWhiteSpaceElement, the new element was always inserted one line below the previous text no matter where the cursor was. This happened because the blank page region is a single map-list entry, so the index-difference formula always returned 1. Changed to Store the cursor widget offset at message receipt, then overrode blankLinesBefore with the line distance from the end of the previous text element (curElement.getEnd()) to the cursor. Using curElement.getEnd() as the baseline rather than wse.getStart() When I did it the other way the offset was off by one amd the line inserted actually moved up a line while the cursor went to the correct place. --- .../braille/stylers/WhitespaceTransformer.kt | 32 +++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/brailleblaster-core/src/main/java/org/brailleblaster/perspectives/braille/stylers/WhitespaceTransformer.kt b/brailleblaster-core/src/main/java/org/brailleblaster/perspectives/braille/stylers/WhitespaceTransformer.kt index 08bc217b..41755e78 100644 --- a/brailleblaster-core/src/main/java/org/brailleblaster/perspectives/braille/stylers/WhitespaceTransformer.kt +++ b/brailleblaster-core/src/main/java/org/brailleblaster/perspectives/braille/stylers/WhitespaceTransformer.kt @@ -32,17 +32,22 @@ import org.brailleblaster.math.template.Template.Companion.isTemplate import org.brailleblaster.perspectives.braille.Manager import org.brailleblaster.perspectives.braille.mapping.elements.BoxLineTextMapElement import org.brailleblaster.perspectives.braille.mapping.elements.LineBreakElement +import org.brailleblaster.perspectives.braille.mapping.elements.PageBreakWhiteSpaceElement import org.brailleblaster.perspectives.braille.mapping.elements.WhiteSpaceElement import org.brailleblaster.perspectives.braille.messages.Sender import org.brailleblaster.perspectives.braille.messages.WhitespaceMessage import org.brailleblaster.perspectives.mvc.XMLTextCaret import org.brailleblaster.perspectives.mvc.events.XMLCaretEvent +import org.brailleblaster.util.LINE_BREAK import org.brailleblaster.util.WhitespaceUtils.appendLineBreakElement import org.brailleblaster.util.WhitespaceUtils.prependLineBreakElement import org.brailleblaster.util.WhitespaceUtils.removeLineBreakElements class WhitespaceTransformer(manager: Manager) : Handler(manager, manager.viewInitializer, manager.mapList) { + private var pendingCursorOffset: Int = -1 + fun transformWhiteSpace(message: WhitespaceMessage) { + pendingCursorOffset = message.offset //if(isInline(list.getCurrent())) // transformInlineText((WhiteSpaceElement)list.get(message.getIndex()), message.getNewText()); //else @@ -73,6 +78,33 @@ class WhitespaceTransformer(manager: Manager) : Handler(manager, manager.viewIni blankLinesBefore-- //An extra line break element is added to the previous tme } + // When typing within a PageBreakWhiteSpaceElement, the entire blank page area is + // represented by a single maplist entry, so the maplist-index-based blankLinesBefore + // is always 1. Override it with the actual visual distance from the previous text + // element to the cursor so that the correct number of blank lines is inserted before + // the new text. + if (wse is PageBreakWhiteSpaceElement && pendingCursorOffset >= 0 && !atBeginning && curElement != null) { + val view = manager.text.view + // curElement.getEnd(list) is the charCount at the moment curElement finished + // rendering, which equals the widget offset of the '\r' that terminates + // curElement's own line. getLineAtOffset() of that position returns + // curElement's line number. + // + // The number of NEW_LINE elements needed to place text at the cursor's visual + // line is exactly (cursorLine - curElementLine), so we use curElement.getEnd() + // directly as the reference point rather than curElement.getEnd() + LINE_BREAK + // (which would point to the start of the NEXT line and undercount by 1). + val pbStart = curElement.getEnd(list) + if (view.charCount > 0 && pbStart >= 0 && pbStart < view.charCount) { + val pbStartLine = view.getLineAtOffset(pbStart) + val cursorLine = view.getLineAtOffset(pendingCursorOffset.coerceIn(0, view.charCount - 1)) + val visualBlankLines = cursorLine - pbStartLine + if (visualBlankLines > blankLinesBefore) { + blankLinesBefore = visualBlankLines + } + } + } + if (nextElement != null) { //nextElement is null at the end of the document //Remove all existing line breaks between the two elements try { From 08c8e315c665126b150f98de8b3985cb8f6ffad3 Mon Sep 17 00:00:00 2001 From: Ken Perry Date: Fri, 22 May 2026 10:41:19 -0400 Subject: [PATCH 2/2] Add "Page Break" marker to style view Added a "Page Break" label in the style pane (StylePane.kt) at the line corresponding to the end of each PageBreakWhiteSpaceElement region. This places the marker on the last blank line before the next page begins, making it visible in the style view alongside existing style labels. --- .../perspectives/braille/views/style/StylePane.kt | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/brailleblaster-core/src/main/java/org/brailleblaster/perspectives/braille/views/style/StylePane.kt b/brailleblaster-core/src/main/java/org/brailleblaster/perspectives/braille/views/style/StylePane.kt index a75ddcc2..ce554751 100644 --- a/brailleblaster-core/src/main/java/org/brailleblaster/perspectives/braille/views/style/StylePane.kt +++ b/brailleblaster-core/src/main/java/org/brailleblaster/perspectives/braille/views/style/StylePane.kt @@ -22,6 +22,7 @@ import org.brailleblaster.easierxml.ImageUtils.getImageNavigateBlock import org.brailleblaster.math.mathml.MathModuleUtils import org.brailleblaster.perspectives.braille.Manager import org.brailleblaster.perspectives.braille.mapping.elements.BraillePageBrlMapElement +import org.brailleblaster.perspectives.braille.mapping.elements.PageBreakWhiteSpaceElement import org.brailleblaster.perspectives.braille.mapping.elements.PageIndicator import org.brailleblaster.perspectives.braille.mapping.maps.MapList import org.brailleblaster.perspectives.braille.mapping.maps.PaintedElementsList @@ -97,6 +98,16 @@ class StylePane(parent: Composite, private val m: Manager) : BBEditorView { textMapElement.node ) ) + if (textMapElement is PageBreakWhiteSpaceElement) { + val pbEnd = textMapElement.getEnd(mapList) + if (pbEnd >= 0 && pbEnd <= textViewWidget.charCount) { + val pbLine = textViewWidget.getLineAtOffset(pbEnd) + if (!lines.containsKey(pbLine)) { + lines[pbLine] = "Page Break" + } + } + continue + } if (textMapElement.node == null) { log.trace("skipping null node") continue