Skip to content

Commit

Permalink
feat(text-canvas): add support for table cell format overrides
Browse files Browse the repository at this point in the history
  • Loading branch information
postspectacular committed Feb 18, 2020
1 parent 8609e8c commit 8909ce0
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 19 deletions.
45 changes: 27 additions & 18 deletions packages/text-canvas/src/table.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { peek } from "@thi.ng/arrays";
import { Border, TableOpts } from "./api";
import { isString } from "@thi.ng/checks";
import { Border, NONE, TableOpts } from "./api";
import {
beginClip,
beginStyle,
Expand All @@ -13,7 +14,10 @@ import { fillRect, strokeRect } from "./rect";
import { horizontalOnly, verticalOnly } from "./style";
import { textLines, wordWrappedLines } from "./text";

export const initTable = (opts: TableOpts, cells: string[][]) => {
type RawCell = { body: string; format: number };
type Cell = { body: string[]; format: number };

export const initTable = (opts: TableOpts, cells: (string | RawCell)[][]) => {
const b = opts.border !== undefined ? opts.border : Border.ALL;
const bH = b & Border.H ? 1 : 0;
const bV = b & Border.V ? 1 : 0;
Expand All @@ -26,13 +30,16 @@ export const initTable = (opts: TableOpts, cells: string[][]) => {
const numCols = cols.length - 1;
const numRows = cells.length - 1;
const rowHeights = new Array<number>(numRows + 1).fill(0);
const wrapped: string[][][] = [];
const wrapped: Cell[][] = [];
for (let i = 0; i <= numRows; i++) {
const row = cells[i];
const wrappedRow: string[][] = [];
for (let j = 0; j < row.length; j++) {
const lines = wordWrappedLines(cols[j].width, row[j]);
wrappedRow.push(lines);
const wrappedRow: Cell[] = [];
for (let j = 0; j <= numCols; j++) {
const cell = isString(row[j])
? { body: <string>row[j], format: NONE }
: <RawCell>row[j];
const lines = wordWrappedLines(cols[j].width, cell.body);
wrappedRow.push({ body: lines, format: cell.format });
rowHeights[i] = Math.max(rowHeights[i], lines.length);
}
wrapped.push(wrappedRow);
Expand Down Expand Up @@ -88,8 +95,7 @@ export const drawTable = (
bFV
} = opts;
const fmt = opts.format !== undefined ? opts.format : canvas.format;
const fmtHd =
opts.formatHead !== undefined ? opts.formatHead : canvas.format;
const fmtHd = opts.formatHead !== undefined ? opts.formatHead : fmt;
const currFormat = canvas.format;
canvas.format = fmt;
let style = opts.style || peek(canvas.styles);
Expand Down Expand Up @@ -122,15 +128,18 @@ export const drawTable = (
}
for (let j = 0, xx = x + bFV; j <= numCols; j++) {
const col = cols[j];
beginClip(canvas, xx, yy, col.width + padH, rowH + padV);
textLines(
canvas,
xx + padH / 2,
yy + padV / 2,
row[j] || [],
i ? fmt : fmtHd
);
endClip(canvas);
const curr = row[j];
if (curr.body) {
beginClip(canvas, xx, yy, col.width + padH, rowH + padV);
textLines(
canvas,
xx + padH / 2,
yy + padV / 2,
curr.body,
curr.format || (i ? fmt : fmtHd)
);
endClip(canvas);
}
if (bH && bV && j > 0 && i < numRows) {
setAt(canvas, xx - 1, y2, style.jct);
}
Expand Down
2 changes: 1 addition & 1 deletion packages/text-canvas/src/text.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ export const textBox = (
export const wordWrappedLines = (width: number, txt: string) => {
const lines: string[] = [];
for (let line of txt.split("\n")) {
for (let words of wordWrap(width, line.split(" "))) {
for (let words of wordWrap(width, { always: false }, line.split(" "))) {
lines.push(words.join(" "));
}
}
Expand Down

0 comments on commit 8909ce0

Please sign in to comment.