From a1c816e6031ff51e8478e93eabd64f0c3063ecb8 Mon Sep 17 00:00:00 2001 From: Hasinjato Date: Wed, 18 Nov 2026 19:19:09 +0300 Subject: [PATCH] Implement voice labels marker "${v:.....}" for notelines "Nx:" --- .../kotlin/mg/dot/feufaro/solfa/Solfa.kt | 44 ++++- .../mg/dot/feufaro/solfa/TimeUnitObject.kt | 181 +++++++++++++++--- 2 files changed, 194 insertions(+), 31 deletions(-) diff --git a/composeApp/src/commonMain/kotlin/mg/dot/feufaro/solfa/Solfa.kt b/composeApp/src/commonMain/kotlin/mg/dot/feufaro/solfa/Solfa.kt index 7dd8dbc..730b904 100644 --- a/composeApp/src/commonMain/kotlin/mg/dot/feufaro/solfa/Solfa.kt +++ b/composeApp/src/commonMain/kotlin/mg/dot/feufaro/solfa/Solfa.kt @@ -31,6 +31,7 @@ class Solfa(val sharedScreenModel: SharedScreenModel, private val fileRepository private val L: MutableList = mutableListOf() private val O: MutableMap> = mutableMapOf() private val unparsedNote = mutableListOf() + private val voiceMarkers: MutableMap> = mutableMapOf() private var refrainBeginsAt = -1 private var refrainEndsAt = -1 private var refrainMaxStanza = -1 @@ -66,6 +67,7 @@ class Solfa(val sharedScreenModel: SharedScreenModel, private val fileRepository val REGEX_DC_REFRAIN = Regex("""\$\{R=\}\$\{D:(.*?)\}""") val REGEX_D_BLOCK = Regex("""\$\{D=\}(.*?)\$\{D!\}""", RegexOption.DOT_MATCHES_ALL) val REGEX_OVERRIDE_TOKEN = Regex("""\$\{O:\d+\}""") + val REGEX_VOICE_MARKER = Regex("\\$\\{v:([^\\}]+)\\}") val KEEP_MARKERS = listOf("\${L}", "\$T") } @@ -125,6 +127,10 @@ class Solfa(val sharedScreenModel: SharedScreenModel, private val fileRepository N.forEachIndexed { voiceNumber, pOneVoiceNote -> run { unitObject.addNote(voiceNumber, pOneVoiceNote.oneVoiceNote, nextNIndex, nextMarker) + val vMarker = voiceMarkers[voiceNumber]?.get(nextNIndex) + if (vMarker != null) { + unitObject.addVoiceMarker(voiceNumber, vMarker) + } } } nextMarker = '0' @@ -275,6 +281,7 @@ class Solfa(val sharedScreenModel: SharedScreenModel, private val fileRepository refrainEndsAt = -1 refrainMaxStanza = -1 unparsedNote.clear() + voiceMarkers.clear() templateString = "" nextTIndex = -1 nextNIndex = -1 @@ -455,6 +462,7 @@ class Solfa(val sharedScreenModel: SharedScreenModel, private val fileRepository pitches.clear() refrainBeginsAt = -1 unparsedNote.clear() + voiceMarkers.clear() templateString = "" nextTIndex = -1 nextNIndex = -1 @@ -1983,7 +1991,41 @@ class Solfa(val sharedScreenModel: SharedScreenModel, private val fileRepository private fun preloadN(noteLine: String) { val voiceNumber = noteLine.substring(0, 1).toInt() - val parsedNoteLine = parseNoteSplit(noteLine) + val rawBody = noteLine.substring(1) + + val vMap = voiceMarkers.getOrPut(voiceNumber) { mutableMapOf() } + var lastIdx = 0 + val cleanBuilder = StringBuilder() + + fun countNotesInSegment(segment: String): Int { + val withoutComments = segment + .replace(REGEX_TEMPLATE_COMMENT, "") + .replace(Regex("#[drmfsltDRFSTz][',]*"), "") + .replace(Regex("<\\s*\\{[^\\}]*\\}"), "") + .replace(" ", "") + val expanded = REGEX_REPETITION.replace(withoutComments) { m -> + m.groupValues[1].repeat(m.groupValues[2].toInt()) + } + return expanded.count { + it in listOf( + 'd', 'r', 'm', 'f', 's', 'l', 't', + 'D', 'R', 'M', 'F', 'S', 'L', 'T', + 'z', 'Z' + ) + } + } + + REGEX_VOICE_MARKER.findAll(rawBody).forEach { match -> + val beforeText = rawBody.substring(lastIdx, match.range.first) + cleanBuilder.append(beforeText) + val noteOffset = countNotesInSegment(cleanBuilder.toString()) + vMap[noteOffset] = match.groupValues[1].trim() + lastIdx = match.range.last + 1 + } + cleanBuilder.append(rawBody.substring(lastIdx)) + val cleanLineContent = cleanBuilder.toString() + + val parsedNoteLine = parseNoteSplit(voiceNumber.toString() + cleanLineContent) val templateComments = mutableListOf() val templateStripped = REGEX_TEMPLATE_COMMENT.replace(templateString) { matchResult -> templateComments.add(matchResult.value) diff --git a/composeApp/src/commonMain/kotlin/mg/dot/feufaro/solfa/TimeUnitObject.kt b/composeApp/src/commonMain/kotlin/mg/dot/feufaro/solfa/TimeUnitObject.kt index 3b476b5..af09ec8 100644 --- a/composeApp/src/commonMain/kotlin/mg/dot/feufaro/solfa/TimeUnitObject.kt +++ b/composeApp/src/commonMain/kotlin/mg/dot/feufaro/solfa/TimeUnitObject.kt @@ -174,6 +174,14 @@ class TimeUnitObject (val pTemplate: PTemplate, val prevTUO: TimeUnitObject?, co tuNote.from(thisNote) } } + + val voiceMarkers: MutableMap = mutableMapOf() + + fun addVoiceMarker(voiceNumber: Int, markerText: String) { + if (markerText.isNotBlank()) { + voiceMarkers[voiceNumber] = markerText + } + } private fun newTUNote(voiceNumber: Int, thisNote: Note?, newMarker: Char, markerBefore: String): TUNote { val prevTUNote: TUNote? = prevTUO?.tuNotes?.getOrNull(voiceNumber) val newNote = TUNote() @@ -422,32 +430,37 @@ fun TimeUnitComposable( } val rawLine = rawLines.getOrNull(ta.voiceNumber - 1).orEmpty() fun shifted(idx: Int) = if (isLitany) idx + SolfaLabels.extraChars(rawLine, idx) else idx - if (us.x > -1) { - val lineGlobalStartOffset = textLayoutResult?.getLineStart(ta.voiceNumber - 1) ?: 0 - val globalStartIndex = lineGlobalStartOffset + shifted(us.x) - xStart = textLayoutResult?.getCursorRect(globalStartIndex)?.left ?: 0f - if (us.isNewParen) { - xStart += leftMarginUnderline - } - } - var xEnd = size.width - if (us.y > -1 ) { - val lineGlobalStartOffset = textLayoutResult?.getLineStart(ta.voiceNumber - 1) ?: 0 - // don't ask me why + 1 here makes it work, but it works. - val globalEndIndex = min(lineGlobalStartOffset + shifted(us.y + separatorLength + 1), multiLineText.length) + val lineCount = textLayoutResult?.lineCount ?: 0 + val targetLineIndex = ta.voiceNumber - 1 - xEnd = textLayoutResult?.getCursorRect(globalEndIndex)?.right ?: size.width + if (targetLineIndex >= 0 && targetLineIndex < lineCount) { + if (us.x > -1) { + val lineGlobalStartOffset = textLayoutResult?.getLineStart(ta.voiceNumber - 1) ?: 0 + val globalStartIndex = lineGlobalStartOffset + shifted(us.x) + xStart = textLayoutResult?.getCursorRect(globalStartIndex)?.left ?: 0f + if (us.isNewParen) { + xStart += leftMarginUnderline + } + } + var xEnd = size.width + if (us.y > -1 ) { + val lineGlobalStartOffset = textLayoutResult?.getLineStart(ta.voiceNumber - 1) ?: 0 + // don't ask me why + 1 here makes it work, but it works. + val globalEndIndex = min(lineGlobalStartOffset + shifted(us.y + separatorLength + 1), multiLineText.length) + + xEnd = textLayoutResult?.getCursorRect(globalEndIndex)?.right ?: size.width + } + //if (it.underlineSpec.size > 0) { + val totalHeight = textLayoutResult?.size?.height ?: 0 + // # FIX + val nbNotes = multiLineText.count { it == '\n' } +1 + val yPos = ta.voiceNumber * totalHeight.toFloat() / nbNotes + drawLine( + color = colorUnderline, + start = Offset(xStart, yPos), + end = Offset(xEnd, yPos) + ) } - //if (it.underlineSpec.size > 0) { - val totalHeight = textLayoutResult?.size?.height ?: 0 - // # FIX - val nbNotes = multiLineText.count { it == '\n' } +1 - val yPos = ta.voiceNumber * totalHeight.toFloat() / nbNotes - drawLine( - color = colorUnderline, - start = Offset(xStart, yPos), - end = Offset(xEnd, yPos) - ) } } } @@ -795,8 +808,50 @@ fun LazyVerticalGridTUO( val activeRowIndex by sharedScreenModel.activeIndex.collectAsStateWithLifecycle() val abModeState by sharedScreenModel.abModeState.collectAsState() - val measures = remember(tuoList, gridColumnCount) { - tuoList.drop(1).chunked(gridColumnCount) + val hasVoiceLabels = tuoList.drop(1).any { it.voiceMarkers.isNotEmpty() } + + val maxLabelLength = remember(tuoList) { + tuoList.drop(1).maxOfOrNull { tuo -> + tuo.voiceMarkers.values.maxOfOrNull { it.length } ?: 0 + } ?: 0 + } + + val voiceLabelColumnWidth = remember(maxLabelLength) { + println("→$maxLabelLength") + when { + maxLabelLength > 15 -> 130.dp + maxLabelLength > 12 -> 115.dp + maxLabelLength > 10 -> 100.dp + else -> 65.dp + } + } + + val reductionCols = when { + maxLabelLength > 12 -> 3 + maxLabelLength > 10 -> 2 + maxLabelLength > 0 -> 1 + else -> 0 + } + val firstRowCols = if (hasVoiceLabels) (gridColumnCount - reductionCols).coerceAtLeast(1) else gridColumnCount + + + val validTuosForVoice = tuoList.drop(1) + val firstValidTuo = validTuosForVoice.firstOrNull() + val measures = remember(tuoList, gridColumnCount, hasVoiceLabels, firstRowCols) { + val allTuos = tuoList.drop(1) + val result = mutableListOf>() + if (allTuos.isEmpty()) return@remember result + + val firstChunkSize = minOf(allTuos.size, firstRowCols) + result.add(allTuos.take(firstChunkSize)) + var currentIndex = firstChunkSize + + while (currentIndex < allTuos.size) { + val nextChunkSize = minOf(allTuos.size - currentIndex, gridColumnCount) + result.add(allTuos.subList(currentIndex, currentIndex + nextChunkSize)) + currentIndex += nextChunkSize + } + result } val hairpinByEndBlock: Map = remember(tuoList) { @@ -897,6 +952,14 @@ fun LazyVerticalGridTUO( ){ measures.forEachIndexed { measureIndex, measureTUOs -> key(measureIndex) { + val isFirstMeasureWithVoiceCol = (measureIndex == 0 && hasVoiceLabels) + val rowColumnCount = if (isFirstMeasureWithVoiceCol) firstRowCols else gridColumnCount + val rowCellWidth = if (isFirstMeasureWithVoiceCol) { + (gridWidthDp - voiceLabelColumnWidth).coerceAtLeast(0.dp) / rowColumnCount + } else { + gridWidthDp / gridColumnCount + } + Column(modifier = Modifier.fillMaxWidth().padding(vertical = 4.dp)) { Row( @@ -904,10 +967,13 @@ fun LazyVerticalGridTUO( horizontalArrangement = Arrangement.Start, verticalAlignment = Alignment.CenterVertically ) { + if (isFirstMeasureWithVoiceCol) { + Spacer(modifier = Modifier.width(voiceLabelColumnWidth)) + } measureTUOs.forEachIndexed { indexInMeasure, tuo -> Column( modifier = Modifier - .width(gridWidthDp / gridColumnCount) + .width(rowCellWidth) ) { if (TimeUnitObject._hasMarker) { val lineHeight = 20.sp @@ -943,7 +1009,7 @@ fun LazyVerticalGridTUO( TuoChordRowItem( tuo, sharedScreenModel, - gridWidthDp, + rowCellWidth, gridColumnCount, toggleWithDegreeAndChord, onToggleChange = { @@ -955,6 +1021,13 @@ fun LazyVerticalGridTUO( } } Row(modifier = Modifier.fillMaxWidth()) { + if (isFirstMeasureWithVoiceCol && hasVoiceLabels) { + VoiceLabelCell( + tuoList = tuoList, + width = voiceLabelColumnWidth + ) + } + measureTUOs.forEachIndexed { indexInMeasure, oneTUO -> val globalIndex = (measureIndex * gridColumnCount) + indexInMeasure val loopingState by sharedScreenModel.loopingState.collectAsState() @@ -974,7 +1047,7 @@ fun LazyVerticalGridTUO( val isAbsoluteLastGrid = (globalIndex == tuoList.size - 2) if (editMode && isAbsoluteLastGrid) { - val lastGridwidth = if(isAndroid) (gridWidthDp / gridColumnCount)*2 else ((gridWidthDp / gridColumnCount)*11)/10 + val lastGridwidth = if(isAndroid) (rowCellWidth)*2 else ((rowCellWidth)*11)/10 Box( modifier = Modifier .width(lastGridwidth) @@ -1213,6 +1286,10 @@ fun LazyVerticalGridTUO( modifier = Modifier.fillMaxWidth(), horizontalArrangement = Arrangement.Start ) { + val isFirstMeasureWithVoiceCol = (measureIndex == 0 && hasVoiceLabels) + if (isFirstMeasureWithVoiceCol) { + Spacer(modifier = Modifier.width(voiceLabelColumnWidth)) + } val columnWidthDp = gridWidthDp / gridColumnCount val cleanAllTemps = allTemps.map { column -> column.map { syllable -> @@ -1324,7 +1401,7 @@ private fun TuoChordRowItem( Column( modifier = Modifier - .width(gridWidthDp / gridColumnCount) + .width(gridWidthDp) ) { val maxFontSize = 12.sp val minFontSize = 10.sp @@ -1860,6 +1937,50 @@ fun EditorActionButtons(isUndoVisible: Boolean, onUndo: () -> Unit, onBuild: () } } +@Composable +fun VoiceLabelCell( + tuoList: List, + width: Dp, + modifier: Modifier = Modifier +) { + val aggregatedMarkers = remember(tuoList) { + val map = mutableMapOf() + tuoList.take(10).forEach { tuo -> + tuo.voiceMarkers.forEach { (voiceNum, text) -> + if (text.isNotBlank() && !map.containsKey(voiceNum)) { + map[voiceNum] = text + } + } + } + map.toSortedMap() + } + val maxVoiceNum = maxOf(aggregatedMarkers.keys.maxOrNull() ?: 4, 4) + val voiceNumbers = (1..maxVoiceNum).toList() + + Column( + modifier = modifier + .width(width) + .padding(end = 0.dp), + verticalArrangement = Arrangement.SpaceBetween + ) { + voiceNumbers.forEach { voiceNum -> + val labelText = aggregatedMarkers[voiceNum]?.trim().orEmpty() + Text( + text = labelText, + style = MaterialTheme.typography.bodyLarge.copy( + fontWeight = FontWeight.Bold, + fontStyle = FontStyle.Italic, + color = MaterialTheme.colorScheme.onSecondary + ), + maxLines = 1, + softWrap = false, + overflow = TextOverflow.Ellipsis, + modifier = Modifier.fillMaxWidth() + ) + } + } +} + fun autoFixNote(rawNote: String, template: String): String { // println("\n==================================================") // println(" ENTRÉE autoFixNote")