Skip to content

Commit

Permalink
Don't draw dangling lines on functional heads
Browse files Browse the repository at this point in the history
  • Loading branch information
robintown committed Jul 23, 2023
1 parent a8f5fea commit b426e3e
Showing 1 changed file with 24 additions and 13 deletions.
37 changes: 24 additions & 13 deletions src/draw-tree.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,24 @@ import { createCanvas, CanvasRenderingContext2D } from 'canvas';
import { DBranch, DLeaf, DTree, formulaToText } from './denote';
import { Branch, Leaf, Rose, Tree } from './tree';

interface PlacedLeaf {
interface PlacedLeafBase {
depth: 0;
width: number;
label: string;
word: string;
gloss?: string;
denotation?: string;
}

interface HasWord {
word: string;
gloss: string | undefined;
}

interface NoWord {
word: undefined;
}

type PlacedLeaf = PlacedLeafBase & (HasWord | NoWord);

interface PlacedBranch {
depth: number;
width: number;
Expand All @@ -31,15 +40,15 @@ export function placeLeaf(
const label = leaf.label;
const word =
leaf.word === 'functional'
? ''
? undefined
: leaf.word === 'covert'
? 'Ø'
: leaf.word.text;
const denotation =
'denotation' in leaf ? formulaToText(leaf.denotation) : undefined;
const width = Math.max(
ctx.measureText(label).width,
ctx.measureText(word).width,
ctx.measureText(word ?? '').width,
ctx.measureText(gloss ?? '').width,
ctx.measureText(denotation ?? '').width,
);
Expand Down Expand Up @@ -158,14 +167,16 @@ function drawTree(
text(denotation, x, y + 30);
ctx.strokeStyle = '#DCDDDE';
ctx.lineWidth = 1;
ctx.beginPath();
ctx.moveTo(x, y + (denotation ? 75 : 45));
ctx.lineTo(x, y + 95);
ctx.stroke();
ctx.fillStyle = '#99EEFF';
text(tree.word, x, y + 100);
ctx.fillStyle = '#DCDDDE';
text(tree.gloss ?? '', x, y + 130);
if (tree.word !== undefined) {
ctx.beginPath();
ctx.moveTo(x, y + (denotation ? 75 : 45));
ctx.lineTo(x, y + 95);
ctx.stroke();
ctx.fillStyle = '#99EEFF';
text(tree.word, x, y + 100);
ctx.fillStyle = '#DCDDDE';
text(tree.gloss ?? '', x, y + 130);
}
} else {
ctx.fillStyle = '#DCDDDE';
text(tree.label, x, y);
Expand Down

0 comments on commit b426e3e

Please sign in to comment.