Skip to content

Commit

Permalink
multiline/BBcode: add linespace setting
Browse files Browse the repository at this point in the history
  • Loading branch information
ray-pH committed Feb 8, 2024
1 parent d7fd067 commit aab84cc
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 6 deletions.
4 changes: 2 additions & 2 deletions src/BBcode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ export class BB_Lexer {
}

export class BB_multiline {
static from_BBCode(text : string) {
static from_BBCode(text : string, linespace : string = "1em") {
let tspans : {text : string, style : {}}[] = []
let tag_stack : { [key: string]: string }[] = [];
let tokens = BB_Lexer.parse(text);
Expand All @@ -104,7 +104,7 @@ export class BB_multiline {
case BB_TokenType.OPEN_TAG: {
// if the token is [br] then add a new line
if (token.attributes['_tag_name'] === "br") {
tspans.push({text: "\n", style: {}});
tspans.push({text: "\n", style: {dy: linespace}});
break;
}
tag_stack.push(token.attributes);
Expand Down
4 changes: 2 additions & 2 deletions src/diagram.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1044,8 +1044,8 @@ export function multiline(spans : ([string] | [string,Partial<TextData>])[]) : D
return dmulti;
}

export function multiline_bb(bbstr : string) : Diagram {
let tspans : TextSpanData[] = BB_multiline.from_BBCode(bbstr) as TextSpanData[];
export function multiline_bb(bbstr : string, linespace? : string) : Diagram {
let tspans : TextSpanData[] = BB_multiline.from_BBCode(bbstr,linespace) as TextSpanData[];
let dmulti = new Diagram(DiagramType.MultilineText, {
multilinedata : { content : tspans },
path : new Path([new Vector2(0, 0)]),
Expand Down
9 changes: 7 additions & 2 deletions src/draw_svg.ts
Original file line number Diff line number Diff line change
Expand Up @@ -295,9 +295,14 @@ function draw_multiline_texts(svgelement : SVGSVGElement, diagrams : Diagram[],
if (diagram.multilinedata?.content == undefined) { throw new Error("MultilineText must have multilinedata"); }
// let current_line : number = 0;
let is_in_front : boolean = true;
let newline_dy : string = "1em";
for (let tspandata of diagram.multilinedata.content) {

if (tspandata.text == "\n") { is_in_front = true; continue; }
if (tspandata.text == "\n") {
is_in_front = true;
newline_dy = tspandata.style['dy'] ?? "1em";
continue;
}

// create tspan for each tspandata
let tspan = document.createElementNS("http://www.w3.org/2000/svg", "tspan");
Expand All @@ -313,7 +318,7 @@ function draw_multiline_texts(svgelement : SVGSVGElement, diagrams : Diagram[],

if (is_in_front) {
tspan.setAttribute("x", "0");
if (not_setting_dy) tspanstyle.dy = "1em";
if (not_setting_dy) tspanstyle.dy = newline_dy;
is_in_front = false;
}

Expand Down

0 comments on commit aab84cc

Please sign in to comment.