From a5979647fc77d4cf654fa44c438368c8f71c6fd4 Mon Sep 17 00:00:00 2001 From: Dylan Pizzo Date: Mon, 6 Jan 2025 23:51:51 -0500 Subject: [PATCH] Add bolding for plusses --- src/dominiontext.ts | 38 +++++++++++++++++++++++++++++++++++--- src/draw.ts | 4 ++-- src/sampleData.ts | 3 ++- 3 files changed, 39 insertions(+), 6 deletions(-) diff --git a/src/dominiontext.ts b/src/dominiontext.ts index 3db715a..500cf19 100644 --- a/src/dominiontext.ts +++ b/src/dominiontext.ts @@ -66,16 +66,31 @@ const pieceDef = ( const textPiece = pieceDef({ type: "text", measure(context, piece) { + context.save(); + const fontInfo = parseFont(context.font); + if (piece.isBold) { + fontInfo.weight = "bold"; + } + if (piece.isItalic) { + fontInfo.style = "italic"; + } + const font = stringifyFont(fontInfo); + context.font = font; const metrics = context.measureText(piece.text); + context.restore(); return { type: "content", width: metrics.width, ascent: metrics.fontBoundingBoxAscent, descent: metrics.fontBoundingBoxDescent, + font, }; }, - render(context, piece, x, y) { + render(context, piece, x, y, measure) { + context.save(); + context.font = measure.font; context.fillText(piece.text, x, y); + context.restore(); }, }); @@ -100,8 +115,8 @@ const breakPiece = pieceDef({ return { type: "break", width: 0, - ascent: metrics.fontBoundingBoxAscent, - descent: metrics.fontBoundingBoxDescent, + ascent: metrics.fontBoundingBoxAscent / 3, + descent: metrics.fontBoundingBoxDescent / 3, }; }, render() {}, @@ -310,6 +325,23 @@ export const parse = (text: string): Piece[] => { const end = text.slice(i).match(/\$\d*/)![0].length; pieces.push({ type: "coin", text: text.slice(i + 1, i + end) }); i += end - 1; + } else if (char === "+") { + const match = text.slice(i).match(/\+\d* \S+/); + if (match) { + const end = match[0].length; + pieces.push({ + type: "text", + isBold: true, + text: text.slice(i, i + end), + }); + i += end - 1; + } else { + pieces.push({ + type: "text", + isBold: true, + text: "+", + }); + } } else { const end = text.slice(i).match(/[^$ \n]+/)![0].length; pieces.push({ type: "text", text: text.slice(i, i + end) }); diff --git a/src/draw.ts b/src/draw.ts index 58aa40e..2db83c5 100644 --- a/src/draw.ts +++ b/src/draw.ts @@ -159,8 +159,8 @@ const drawStandardCard = async ( context, parse(card.description), w / 2, - 1520, - 1100 + 1490, + 1000 ); // Draw the types let size = 65; diff --git a/src/sampleData.ts b/src/sampleData.ts index bc8f992..c50152a 100644 --- a/src/sampleData.ts +++ b/src/sampleData.ts @@ -8,7 +8,8 @@ import { export const sampleCard1: DominionCard = { orientation: "card", title: "Title", - description: "Hello, world.", + description: + "+1 Action\n\nReveal the top card of your deck. If it's an Action card, put it into your hand.", types: [TYPE_ACTION, TYPE_REACTION], image: "https://wiki.dominionstrategy.com/images/7/76/AdventurerArt.jpg", artist: "Dall-E",