All files / generator/rirekisho layout.ts

93.79% Statements 136/145
85.41% Branches 41/48
100% Functions 11/11
93.66% Lines 133/142

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 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605                                                                        151x 151x                           1311x                                                                                                                                   151x   151x 151x     151x     151x 151x 151x     151x     151x 151x     151x 151x 151x 151x     151x 135x       151x 135x 135x 135x       151x     176x 7x   169x 169x 130x 130x           39x 39x             151x         176x 176x 176x 176x       151x             253x 253x 253x 253x           151x             234x             234x       151x 151x     151x           151x     151x                 132x                           19x     2x 2x   2x                   2x 11x 11x                       17x     3x 6x 6x         6x   6x                 1x     3x                   2x 2x         14x 14x     14x                   14x   14x         14x                           14x         14x                             14x 14x 14x   14x   121x     121x 121x     121x 121x 121x   121x         121x   31x 31x     90x       121x       14x 14x     14x 14x     14x 14x                           19x         19x       19x               19x                                                   151x 151x     151x 151x     151x 151x 151x 151x 151x 151x 151x     151x     151x 151x   151x         151x     151x 151x 151x           151x 151x                           151x 151x     151x     151x 151x       151x         151x                                                                               13x 4x   9x    
/**
 * Layout calculation for Rirekisho (履歴書)
 *
 * This module calculates the optimal layout for the rirekisho document,
 * ensuring that:
 * 1. All content fits within the page
 * 2. Left and right page bottoms align perfectly
 * 3. Font sizes don't go below minimum
 * 4. Each section has at least its minimum height
 */
 
import type {
  LayoutDimensions,
  LayoutInput,
  PaperDimensions,
  PaperSize,
} from './types.js';
import {
  FIXED_DIMENSIONS,
  FONT_SIZE,
  PAPER_SIZES,
  PREFERRED_ROW_COUNTS,
  ROW_HEIGHT,
  SCALE_FACTORS,
  SECTION_HEIGHTS,
} from './types.js';
 
// ============================================================================
// Helper Functions
// ============================================================================
 
/**
 * Calculate the height of the left page header section
 * (title, name, birth/gender, addresses)
 */
function calculateLeftHeaderHeight(scale: number): number {
  const d = FIXED_DIMENSIONS;
  return (
    d.headerHeight * scale +
    d.nameRowHeight * scale +
    d.nameMainHeight * scale +
    d.birthGenderHeight * scale +
    d.addressRowHeight * scale * 2
  );
}
 
/**
 * Calculate table height from row count and row height
 * +1 for header row
 */
function calculateTableHeight(dataRowCount: number, rowHeight: number): number {
  return (dataRowCount + 1) * rowHeight;
}
 
// ============================================================================
// Layout Optimization
// ============================================================================
 
/**
 * Layout allocation result for right page sections
 */
interface RightPageAllocation {
  readonly rightHistoryRows: number;
  readonly licenseRows: number;
  readonly motivationHeight: number;
  readonly notesHeight: number;
  readonly totalHeight: number;
  /** Adjusted row height (may be smaller than default if content is large) */
  readonly adjustedRowHeight: number;
  /** Adjusted font size (may be smaller than default if content is large) */
  readonly adjustedFontSize: number;
  /** Whether the content overflows (doesn't fit even with minimum row height) */
  readonly overflows: boolean;
}
 
/**
 * Allocation input parameters
 */
interface AllocationParams {
  readonly availableHeight: number;
  readonly historyOverflow: number;
  readonly licenseDataRows: number;
  readonly tableRowHeight: number;
  readonly tableMargin: number;
  readonly scale: number;
  readonly paperSize: PaperSize;
  readonly hideMotivation: boolean;
  /** Total history data rows (education + work) */
  readonly totalHistoryDataRows: number;
  /** Available height for left page table area */
  readonly leftTableAreaHeight: number;
}
 
/**
 * Calculate optimal allocation of space on the right page
 *
 * Priority rules:
 * 1. When license data is large: reduce motivation/notes height first, then reduce history rows
 * 2. When history data is large: reduce license rows if possible, then reduce motivation/notes height
 * 3. When both are large: reduce motivation/notes to minimum height
 *    - If still doesn't fit: reduce row height and font size (applied to all tables on both pages)
 *
 * The algorithm starts with preferred row counts and reduces as needed while
 * maintaining minimum constraints.
 */
function allocateRightPageSpace(params: AllocationParams): RightPageAllocation {
  const {
    availableHeight,
    historyOverflow,
    licenseDataRows,
    tableRowHeight,
    tableMargin,
    scale,
    paperSize,
    hideMotivation,
    totalHistoryDataRows,
    leftTableAreaHeight,
  } = params;
 
  const preferred = PREFERRED_ROW_COUNTS[paperSize];
  const motivationMinHeight = hideMotivation
    ? 0
    : SECTION_HEIGHTS.motivationMin * scale;
  const notesMinHeight = SECTION_HEIGHTS.notesMin * scale;
 
  // Row height and font size constraints
  const minRowHeight = ROW_HEIGHT.min * scale;
  const defaultFontSize = FONT_SIZE.default * scale;
  const minFontSize = FONT_SIZE.min * scale;
 
  // History extra rows (学歴, 職歴, 現在に至る, 以上)
  const historyExtraRows = 4;
 
  // Minimum row counts: must show all data, at least 1 row
  const minHistoryRows = Math.max(1, historyOverflow);
  const minLicenseRows = Math.max(1, licenseDataRows);
 
  // Start with preferred row counts (but at least enough for data)
  let rightHistoryRows = Math.max(preferred.rightHistory, minHistoryRows);
  let licenseRows = Math.max(preferred.license, minLicenseRows);
  let currentRowHeight = tableRowHeight;
  let currentFontSize = defaultFontSize;
 
  // Calculate how many left history rows fit with a given row height
  const calcLeftHistoryRows = (rowHeight: number): number => {
    return Math.floor(leftTableAreaHeight / rowHeight) - 2; // -1 for header, -1 for safety
  };
 
  // Calculate history overflow with a given row height
  const calcHistoryOverflow = (rowHeight: number): number => {
    const leftRows = calcLeftHistoryRows(rowHeight);
    const totalNeeded = totalHistoryDataRows + historyExtraRows;
    return Math.max(0, totalNeeded - leftRows);
  };
 
  // Calculate initial heights with preferred 6:4 ratio for motivation/notes
  const calcMotivationNotesHeights = (
    remaining: number,
  ): { motivation: number; notes: number } => {
    if (hideMotivation) {
      return { motivation: 0, notes: Math.max(notesMinHeight, remaining) };
    }
    const totalMin = motivationMinHeight + notesMinHeight;
    if (remaining >= totalMin) {
      const extra = remaining - totalMin;
      return {
        motivation: motivationMinHeight + extra * 0.6,
        notes: notesMinHeight + extra * 0.4,
      };
    }
    // Below minimum - proportionally reduce
    const ratio = Math.max(0, remaining / totalMin);
    return {
      motivation: motivationMinHeight * ratio,
      notes: notesMinHeight * ratio,
    };
  };
 
  // Calculate remaining space for motivation/notes (with variable row height)
  const calcRemainingForSections = (
    histRows: number,
    licRows: number,
    rowHeight: number,
  ): number => {
    const histHeight = calculateTableHeight(histRows, rowHeight);
    const licHeight = calculateTableHeight(licRows, rowHeight);
    const marginCount = hideMotivation ? 2 : 3;
    return availableHeight - histHeight - licHeight - marginCount * tableMargin;
  };
 
  // Calculate total height (with variable row height)
  const calcTotalHeight = (
    histRows: number,
    licRows: number,
    motHeight: number,
    notHeight: number,
    rowHeight: number,
  ): number => {
    const histHeight = calculateTableHeight(histRows, rowHeight);
    const licHeight = calculateTableHeight(licRows, rowHeight);
    const marginCount = hideMotivation ? 2 : 3;
    return (
      histHeight + licHeight + motHeight + notHeight + marginCount * tableMargin
    );
  };
 
  // Check if current allocation fits (with variable row height)
  const fitsInAvailable = (
    histRows: number,
    licRows: number,
    motHeight: number,
    notHeight: number,
    rowHeight: number,
  ): boolean => {
    const total = calcTotalHeight(
      histRows,
      licRows,
      motHeight,
      notHeight,
      rowHeight,
    );
    return total <= availableHeight + 0.1; // Small tolerance for floating point
  };
 
  // Determine which data is "large" (exceeds preferred)
  const historyIsLarge = historyOverflow > preferred.rightHistory;
  const licenseIsLarge = licenseDataRows > preferred.license;
 
  // Initial calculation with preferred rows
  let remaining = calcRemainingForSections(
    rightHistoryRows,
    licenseRows,
    currentRowHeight,
  );
  let { motivation: motivationHeight, notes: notesHeight } =
    calcMotivationNotesHeights(remaining);
 
  // If everything fits, we're done
  if (
    fitsInAvailable(
      rightHistoryRows,
      licenseRows,
      motivationHeight,
      notesHeight,
      currentRowHeight,
    )
  ) {
    return {
      rightHistoryRows,
      licenseRows,
      motivationHeight,
      notesHeight,
      totalHeight: availableHeight,
      adjustedRowHeight: currentRowHeight,
      adjustedFontSize: currentFontSize,
      overflows: false,
    };
  }
 
  // Need to reduce - apply priority rules
 
  if (licenseIsLarge && !historyIsLarge) {
    // Priority 1: License is large
    // Step 1: Reduce motivation/notes to minimum
    motivationHeight = motivationMinHeight;
    notesHeight = notesMinHeight;
 
    Eif (
      !fitsInAvailable(
        rightHistoryRows,
        licenseRows,
        motivationHeight,
        notesHeight,
        currentRowHeight,
      )
    ) {
      // Step 2: Reduce history rows (empty rows first)
      while (rightHistoryRows > minHistoryRows) {
        rightHistoryRows--;
        Iif (
          fitsInAvailable(
            rightHistoryRows,
            licenseRows,
            motivationHeight,
            notesHeight,
            currentRowHeight,
          )
        )
          break;
      }
    }
  } else if (historyIsLarge && !licenseIsLarge) {
    // Priority 2: History is large
    // Step 1: Reduce license rows (empty rows first)
    while (licenseRows > minLicenseRows) {
      licenseRows--;
      remaining = calcRemainingForSections(
        rightHistoryRows,
        licenseRows,
        currentRowHeight,
      );
      ({ motivation: motivationHeight, notes: notesHeight } =
        calcMotivationNotesHeights(remaining));
      if (
        fitsInAvailable(
          rightHistoryRows,
          licenseRows,
          motivationHeight,
          notesHeight,
          currentRowHeight,
        )
      )
        break;
    }
 
    if (
      !fitsInAvailable(
        rightHistoryRows,
        licenseRows,
        motivationHeight,
        notesHeight,
        currentRowHeight,
      )
    ) {
      // Step 2: Reduce motivation/notes to minimum
      motivationHeight = motivationMinHeight;
      notesHeight = notesMinHeight;
    }
  } else {
    // Priority 3: Both are large (or neither but still doesn't fit)
    // Reduce motivation/notes to minimum first
    motivationHeight = motivationMinHeight;
    notesHeight = notesMinHeight;
 
    // If still doesn't fit, reduce empty rows from both tables
    while (
      !fitsInAvailable(
        rightHistoryRows,
        licenseRows,
        motivationHeight,
        notesHeight,
        currentRowHeight,
      )
    ) {
      // Alternate reducing license and history empty rows
      let reduced = false;
 
      Iif (licenseRows > minLicenseRows) {
        licenseRows--;
        reduced = true;
      }
 
      Iif (
        !fitsInAvailable(
          rightHistoryRows,
          licenseRows,
          motivationHeight,
          notesHeight,
          currentRowHeight,
        ) &&
        rightHistoryRows > minHistoryRows
      ) {
        rightHistoryRows--;
        reduced = true;
      }
 
      Eif (!reduced) break; // Can't reduce anymore
    }
 
    // If still doesn't fit after reducing rows, reduce row height and font size
    // This affects all tables on both left and right pages
    Eif (
      !fitsInAvailable(
        rightHistoryRows,
        licenseRows,
        motivationHeight,
        notesHeight,
        currentRowHeight,
      )
    ) {
      // Calculate the optimal row height that fits all content
      // We need to find a row height where:
      // 1. Left page can fit leftHistoryRows (calculated from row height)
      // 2. Right page can fit rightHistoryRows + licenseRows + motivation + notes
 
      // Binary search for optimal row height
      let lowHeight = minRowHeight;
      let highHeight = tableRowHeight;
      let bestRowHeight = minRowHeight;
 
      for (let i = 0; i < 20; i++) {
        // Max 20 iterations for precision
        const midHeight = (lowHeight + highHeight) / 2;
 
        // Recalculate overflow with this row height
        const newOverflow = calcHistoryOverflow(midHeight);
        const newRightHistoryRows = Math.max(1, newOverflow);
 
        // Check if right page fits
        const marginCount = hideMotivation ? 2 : 3;
        const rightTableRows = newRightHistoryRows + 1 + licenseRows + 1; // +1 for headers
        const rightTablesHeight = rightTableRows * midHeight;
        const rightTotalHeight =
          rightTablesHeight +
          motivationMinHeight +
          notesMinHeight +
          marginCount * tableMargin;
 
        if (rightTotalHeight <= availableHeight + 0.1) {
          // This height works, try larger
          bestRowHeight = midHeight;
          lowHeight = midHeight;
        } else {
          // Too large, try smaller
          highHeight = midHeight;
        }
 
        // Stop if converged
        if (highHeight - lowHeight < 0.01) break;
      }
 
      // Use the best row height found
      if (bestRowHeight >= minRowHeight) {
        currentRowHeight = bestRowHeight;
 
        // Recalculate right history rows with new row height
        const newOverflow = calcHistoryOverflow(currentRowHeight);
        rightHistoryRows = Math.max(1, newOverflow);
 
        // Scale font size proportionally
        const heightRatio = currentRowHeight / tableRowHeight;
        currentFontSize = Math.max(minFontSize, defaultFontSize * heightRatio);
      } else E{
        // Use minimum row height - content may overflow but we can't go smaller
        currentRowHeight = minRowHeight;
        currentFontSize = minFontSize;
 
        // Recalculate right history rows with minimum row height
        const newOverflow = calcHistoryOverflow(currentRowHeight);
        rightHistoryRows = Math.max(1, newOverflow);
      }
    }
  }
 
  // Final recalculation - distribute any remaining space to motivation/notes
  remaining = calcRemainingForSections(
    rightHistoryRows,
    licenseRows,
    currentRowHeight,
  );
  ({ motivation: motivationHeight, notes: notesHeight } =
    calcMotivationNotesHeights(remaining));
 
  // Check if content still overflows after all adjustments
  const finalFits = fitsInAvailable(
    rightHistoryRows,
    licenseRows,
    motivationHeight,
    notesHeight,
    currentRowHeight,
  );
 
  return {
    rightHistoryRows,
    licenseRows,
    motivationHeight,
    notesHeight,
    totalHeight: calcTotalHeight(
      rightHistoryRows,
      licenseRows,
      motivationHeight,
      notesHeight,
      currentRowHeight,
    ),
    adjustedRowHeight: currentRowHeight,
    adjustedFontSize: currentFontSize,
    overflows: !finalFits,
  };
}
 
// ============================================================================
// Main Layout Calculation
// ============================================================================
 
/**
 * Calculate complete layout dimensions for the rirekisho document
 */
export function calculateLayout(input: LayoutInput): LayoutDimensions {
  const { paperSize, hideMotivation, dataCounts } = input;
  const { historyDataRows, licenseDataRows } = dataCounts;
 
  // Paper and scale
  const paper: PaperDimensions = PAPER_SIZES[paperSize];
  const scale = SCALE_FACTORS[paperSize];
 
  // Page dimensions
  const margin = FIXED_DIMENSIONS.margin * scale;
  const marginBottom = FIXED_DIMENSIONS.marginBottom * scale;
  const centerGap = FIXED_DIMENSIONS.centerGap * scale;
  const pageWidth = (paper.width - margin * 2 - centerGap) / 2;
  const pageHeight = paper.height - margin - marginBottom;
  const footerHeight = FIXED_DIMENSIONS.footerHeight * scale;
  const tableMargin = FIXED_DIMENSIONS.tableMargin * scale;
 
  // Table dimensions (default values, may be adjusted by allocation)
  const defaultTableRowHeight = ROW_HEIGHT.default * scale;
 
  // Left page layout
  const leftHeaderHeight = calculateLeftHeaderHeight(scale);
  const leftContentHeight = pageHeight - footerHeight;
  const leftTableAreaHeight =
    leftContentHeight - leftHeaderHeight - tableMargin;
 
  // Calculate how many rows fit on left page (using default row height initially)
  // -1 for header row, -1 for safety margin
  const defaultLeftHistoryRows =
    Math.floor(leftTableAreaHeight / defaultTableRowHeight) - 2;
 
  // Calculate history overflow to right page (with default row height)
  const historyExtraRows = 4; // 学歴, 職歴, 現在に至る, 以上
  const totalHistoryNeeded = historyDataRows + historyExtraRows;
  const historyOverflow = Math.max(
    0,
    totalHistoryNeeded - defaultLeftHistoryRows,
  );
 
  // Right page layout - allocate space optimally
  const rightContentHeight = pageHeight - footerHeight;
  const allocation = allocateRightPageSpace({
    availableHeight: rightContentHeight,
    historyOverflow,
    licenseDataRows,
    tableRowHeight: defaultTableRowHeight,
    tableMargin,
    scale,
    paperSize,
    hideMotivation,
    totalHistoryDataRows: historyDataRows,
    leftTableAreaHeight,
  });
 
  // Use adjusted row height and font size from allocation
  const tableRowHeight = allocation.adjustedRowHeight;
  const tableFontSize = allocation.adjustedFontSize;
 
  // Recalculate left history rows with the (possibly adjusted) row height
  const leftHistoryRows = Math.floor(leftTableAreaHeight / tableRowHeight) - 2;
 
  // Calculate table heights with the (possibly adjusted) row height
  const leftTableHeight = calculateTableHeight(leftHistoryRows, tableRowHeight);
  const rightHistoryTableHeight = calculateTableHeight(
    allocation.rightHistoryRows,
    tableRowHeight,
  );
  const licenseTableHeight = calculateTableHeight(
    allocation.licenseRows,
    tableRowHeight,
  );
 
  return {
    paper,
    scale,
    margin,
    marginBottom,
    centerGap,
    pageWidth,
    pageHeight,
    photoWidth: FIXED_DIMENSIONS.photoWidth * scale,
    contactWidth: FIXED_DIMENSIONS.contactWidth * scale,
    genderWidth: FIXED_DIMENSIONS.genderWidth * scale,
    headerHeight: FIXED_DIMENSIONS.headerHeight * scale,
    nameRowHeight: FIXED_DIMENSIONS.nameRowHeight * scale,
    nameMainHeight: FIXED_DIMENSIONS.nameMainHeight * scale,
    birthGenderHeight: FIXED_DIMENSIONS.birthGenderHeight * scale,
    addressRowHeight: FIXED_DIMENSIONS.addressRowHeight * scale,
    addressFuriganaHeight: FIXED_DIMENSIONS.addressFuriganaHeight * scale,
    tableRowHeight,
    tableFontSize,
    tableMargin,
    yearColumnWidth: FIXED_DIMENSIONS.yearColumnWidth * scale,
    monthColumnWidth: FIXED_DIMENSIONS.monthColumnWidth * scale,
    leftHistoryRows,
    rightHistoryRows: allocation.rightHistoryRows,
    licenseRows: allocation.licenseRows,
    leftTableHeight,
    rightHistoryTableHeight,
    licenseTableHeight,
    motivationMinHeight: allocation.motivationHeight,
    notesMinHeight: allocation.notesHeight,
    footerHeight,
    hideMotivation,
    overflows: allocation.overflows,
  };
}
 
/**
 * Validate that the layout can accommodate the data
 */
export function validateLayout(layout: LayoutDimensions): string | null {
  if (layout.overflows) {
    return 'データが多すぎてページに収まりません。学歴・職歴または免許・資格の数を減らしてください。';
  }
  return null;
}