All files / web/src/app/create/worksheets/typstHelpers/subtraction subtrahendRow.ts

100% Statements 66/66
100% Branches 1/1
100% Functions 1/1
100% Lines 66/66

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 671x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x  
// Subtrahend row rendering for subtraction problems
// Shows the bottom number being subtracted with − sign
 
import type { CellDimensions } from '../shared/types'
 
/**
 * Generate Typst code for the subtrahend row
 *
 * The subtrahend row shows the number being subtracted (bottom number) with
 * a − sign in the operator column. When borrow notation is enabled, cells
 * include invisible spacer boxes to maintain alignment with the minuend row's
 * scratch boxes.
 *
 * @param cellDimensions - Cell sizing information
 * @returns Typst code for subtrahend row
 */
export function generateSubtrahendRow(cellDimensions: CellDimensions): string {
  const { cellSize, cellSizeIn, cellSizePt } = cellDimensions
 
  return String.raw`
      // Subtrahend row (operator sign rendered separately via place() for proper layering)
      [],  // Empty cell for operator column (operator overlaid later)
      ..for i in range(0, grid-digits).rev() {
        let digit = s-digits.at(i)
        let place-color = place-colors.at(i)
        let fill-color = if show-colors { place-color } else { color-none }
 
        // Check if this column has borrowing (need to match minuend row alignment)
        let column-has-borrow = i < grid-digits and (m-digits.at(i) < s-digits.at(i))
 
        // Show digit if within subtrahend's actual range
        if i <= s-highest {
          if show-borrow-notation and column-has-borrow {
            // Add invisible box space to maintain alignment with minuend row
            (box(width: ${cellSizeIn}, height: ${cellSizeIn}, fill: fill-color)[
              #align(center + horizon)[
                #stack(
                  dir: ltr,
                  spacing: 3pt,
                  // Invisible box (same size as minuend's borrow box) to maintain alignment
                  box(
                    width: ${cellSizeIn} * 0.45,
                    height: ${cellSizeIn} * 0.95,
                  )[],
                  // Original digit
                  text(size: ${cellSizePt.toFixed(1)}pt, font: "New Computer Modern Math")[#str(digit)]
                )
              ]
            ],)
          } else {
            // Normal digit display (no borrow notation mode or column doesn't need borrowing)
            (box(width: ${cellSizeIn}, height: ${cellSizeIn}, fill: fill-color)[
              #align(center + horizon)[
                #text(size: ${cellSizePt.toFixed(1)}pt, font: "New Computer Modern Math")[#str(digit)]
              ]
            ],)
          }
        } else {
          // Leading zero position - don't show
          (box(width: ${cellSizeIn}, height: ${cellSizeIn})[
            #h(0pt)
          ],)
        }
      },
`
}