Skip to content

A lightweight Clip Studio Paint alternative with real-time multiplayer collaborative drawing. Built with Tauri and React.

5.3 KB
Raw Blame
import { describe, it, expect, vi } from 'vitest';
import {
  computeMangaTextLayout,
  isLatinChar,
  renderMangaTextToCanvas,
} from '../mangaTextEngine';

describe('Manga Text Engine (Tate-gaki & Yokogaki)', () => {
  it('correctly identifies Latin characters', () => {
    expect(isLatinChar('A')).toBe(true);
    expect(isLatinChar('z')).toBe(true);
    expect(isLatinChar('9')).toBe(true);
    expect(isLatinChar('あ')).toBe(false);
    expect(isLatinChar('漢')).toBe(false);
  });

  it('computes horizontal layout with glyph positions', () => {
    const layout = computeMangaTextLayout('HELLO\nWORLD', {
      fontSize: 20,
      fontFamily: 'Arial',
      writingMode: 'horizontal-tb',
    });

    expect(layout.glyphs.length).toBe(10);
    expect(layout.width).toBeGreaterThan(0);
    expect(layout.height).toBeGreaterThan(0);
    expect(layout.glyphs[0].rotateDeg).toBe(0);
  });

  it('computes vertical-rl layout with right-to-left columns and top-to-bottom glyphs', () => {
    // Two lines: col 0 should be on the right, col 1 on the left
    const layout = computeMangaTextLayout('あいう\nえおか', {
      fontSize: 20,
      fontFamily: 'sans-serif',
      writingMode: 'vertical-rl',
    });

    expect(layout.glyphs.length).toBe(6);
    // Find glyph for 'あ' (col 0, row 0) and 'え' (col 1, row 0)
    const glyphA = layout.glyphs.find((g) => g.char === 'あ');
    const glyphE = layout.glyphs.find((g) => g.char === 'え');
    expect(glyphA).toBeDefined();
    expect(glyphE).toBeDefined();
    // Col 0 ('あ') should have a higher X coordinate than Col 1 ('え') in vertical-rl
    expect(glyphA!.x).toBeGreaterThan(glyphE!.x);
    // Within column: 'い' should have higher Y than 'あ'
    const glyphI = layout.glyphs.find((g) => g.char === 'い');
    expect(glyphI!.y).toBeGreaterThan(glyphA!.y);
  });

  it('rotates em-dashes and brackets 90 degrees in vertical mode', () => {
    const layout = computeMangaTextLayout('「愛」—', {
      fontSize: 20,
      fontFamily: 'sans-serif',
      writingMode: 'vertical-rl',
    });

    const openBracket = layout.glyphs.find((g) => g.char === '「');
    const closeBracket = layout.glyphs.find((g) => g.char === '」');
    const dash = layout.glyphs.find((g) => g.char === '—');
    const kanji = layout.glyphs.find((g) => g.char === '愛');

    expect(openBracket!.rotateDeg).toBe(90);
    expect(closeBracket!.rotateDeg).toBe(90);
    expect(dash!.rotateDeg).toBe(90);
    expect(kanji!.rotateDeg).toBe(0);
  });

  it('uses proportional character widths rather than fixed monospace spacing in horizontal mode', () => {
    const narrow = computeMangaTextLayout('iiiii', {
      fontSize: 36,
      fontFamily: "Inter, 'Segoe UI', sans-serif",
      writingMode: 'horizontal-tb',
    });
    const wide = computeMangaTextLayout('WWWWW', {
      fontSize: 36,
      fontFamily: "Inter, 'Segoe UI', sans-serif",
      writingMode: 'horizontal-tb',
    });

    expect(wide.width).toBeGreaterThan(narrow.width * 1.5);
    expect(narrow.lines).toHaveLength(1);
    expect(wide.lines).toHaveLength(1);
  });

  it('wraps horizontal paragraphs cleanly when maxWidth is specified', () => {
    const layout = computeMangaTextLayout('Ink Studio creates natural authentic typography', {
      fontSize: 24,
      fontFamily: 'Arial, sans-serif',
      writingMode: 'horizontal-tb',
      maxWidth: 160,
    });

    expect(layout.lines.length).toBeGreaterThan(2);
    expect(layout.lines.map((l) => l.text).join(' ')).toContain('natural');
    expect(layout.height).toBeGreaterThan(layout.lineHeight * 2);
  });

  it('renders plain white text without outlines by default and uses two-pass outline-then-fill when outline is enabled', () => {
    const callOrder: string[] = [];
    const fillStyles: string[] = [];
    const mockCtx = {
      save: vi.fn(),
      restore: vi.fn(),
      translate: vi.fn(),
      rotate: vi.fn(),
      beginPath: vi.fn(),
      moveTo: vi.fn(),
      lineTo: vi.fn(),
      stroke: vi.fn(),
      fillRect: vi.fn(),
      measureText: vi.fn((s: string) => ({ width: s.length * 10 })),
      strokeText: vi.fn((text: string) => {
        callOrder.push(`stroke:${text}`);
      }),
      fillText: vi.fn((text: string) => {
        callOrder.push(`fill:${text}`);
        fillStyles.push(String(mockCtx.fillStyle));
      }),
      font: '',
      fillStyle: '',
      strokeStyle: '',
      lineWidth: 1,
      lineJoin: 'miter',
      lineCap: 'butt',
      miterLimit: 10,
      textAlign: 'left',
      textBaseline: 'alphabetic',
      globalAlpha: 1,
    } as unknown as CanvasRenderingContext2D;

    // Plain white without outline
    renderMangaTextToCanvas(mockCtx, 'Hello\nWorld', 100, 120, {
      fontSize: 36,
      fontFamily: 'Arial, sans-serif',
      outline: false,
      anchorTopLeft: true,
    });

    expect(callOrder).toEqual(['fill:Hello', 'fill:World']);
    expect(fillStyles).toEqual(['#ffffff', '#ffffff']);

    // Two-pass outline: ALL strokes before ANY fills
    callOrder.length = 0;
    renderMangaTextToCanvas(mockCtx, 'Line 1\nLine 2', 100, 120, {
      fontSize: 36,
      fontFamily: 'Arial, sans-serif',
      color: '#ffffff',
      outline: true,
      outlineColor: '#000000',
      outlineWidth: 3,
      anchorTopLeft: true,
    });

    expect(callOrder).toEqual([
      'stroke:Line 1',
      'stroke:Line 2',
      'fill:Line 1',
      'fill:Line 2',
    ]);
  });
});