Skip to content

Commit 35d936c

Browse files
committed
Combine together latex and text post-processing
Many steps are similar
1 parent 01445c2 commit 35d936c

File tree

1 file changed

+64
-74
lines changed

1 file changed

+64
-74
lines changed

scripts/JSRoot.painter.js

Lines changed: 64 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -2657,111 +2657,101 @@ JSROOT.define(['d3'], (d3) => {
26572657
}
26582658
});
26592659

2660-
// now hidden text after rescaling can be shown
2660+
// now process text and latex drawings
26612661
all_args.forEach(arg => {
2662-
if (!arg.txt_node) return; // only normal text is processed
2663-
any_text = true;
2664-
let txt = arg.txt_node;
2665-
delete arg.txt_node;
2666-
txt.attr('visibility', null);
2667-
2668-
if (JSROOT.nodejs) {
2669-
if (arg.scale && (f > 0)) { arg.box.width = arg.box.width / f; arg.box.height = arg.box.height / f; }
2670-
} else if (!arg.plain && !arg.fast) {
2671-
// exact box dimension only required when complex text was build
2672-
arg.box = jsrp.getElementRect(txt, 'bbox');
2662+
let txt, is_txt, scale = 1;
2663+
if (arg.txt_node) {
2664+
txt = arg.txt_node;
2665+
delete arg.txt_node;
2666+
is_txt = true;
2667+
} else if (arg.txt_g) {
2668+
txt = arg.txt_g;
2669+
delete arg.txt_g;
2670+
is_txt = false;
2671+
} else {
2672+
return;
26732673
}
26742674

2675-
// if (arg.text.length>20) console.log(arg.box, arg.align, arg.x, arg.y, 'plain', arg.plain, 'inside', arg.width, arg.height);
2675+
txt.attr('visibility', null);
2676+
2677+
any_text = true;
26762678

26772679
if (arg.width) {
26782680
// adjust x position when scale into specified rectangle
2679-
if (arg.align[0] == "middle") arg.x += arg.width / 2; else
2680-
if (arg.align[0] == "end") arg.x += arg.width;
2681-
}
2682-
2683-
arg.dx = arg.dy = 0;
2684-
2685-
if (arg.plain) {
2686-
txt.attr("text-anchor", arg.align[0]);
2687-
} else {
2688-
txt.attr("text-anchor", "start");
2689-
arg.dx = ((arg.align[0] == "middle") ? -0.5 : ((arg.align[0] == "end") ? -1 : 0)) * arg.box.width;
2681+
if (arg.align[0] == "middle")
2682+
arg.x += arg.width / 2;
2683+
else if (arg.align[0] == "end")
2684+
arg.x += arg.width;
26902685
}
26912686

26922687
if (arg.height) {
2693-
if (arg.align[1].indexOf('bottom') === 0) arg.y += arg.height; else
2694-
if (arg.align[1] == 'middle') arg.y += arg.height / 2;
2688+
if (arg.align[1].indexOf('bottom') === 0)
2689+
arg.y += arg.height;
2690+
else if (arg.align[1] == 'middle')
2691+
arg.y += arg.height / 2;
26952692
}
26962693

2697-
if (arg.plain) {
2698-
if (arg.align[1] == 'top') txt.attr("dy", ".8em"); else
2699-
if (arg.align[1] == 'middle') {
2700-
if (JSROOT.nodejs) txt.attr("dy", ".4em"); else txt.attr("dominant-baseline", "middle");
2701-
}
2702-
} else {
2703-
arg.dy = ((arg.align[1] == 'top') ? (arg.top_shift || 1) : (arg.align[1] == 'middle') ? (arg.mid_shift || 0.5) : 0) * arg.box.height;
2704-
}
2694+
arg.dx = arg.dy = 0;
27052695

2706-
if (!arg.rotate) { arg.x += arg.dx; arg.y += arg.dy; arg.dx = arg.dy = 0; }
2696+
if (is_txt) {
27072697

2708-
// use translate and then rotate to avoid complex sign calculations
2709-
let trans = (arg.x || arg.y) ? "translate(" + Math.round(arg.x) + "," + Math.round(arg.y) + ")" : "";
2710-
if (arg.rotate) trans += " rotate(" + Math.round(arg.rotate) + ")";
2711-
if (arg.dx || arg.dy) trans += " translate(" + Math.round(arg.dx) + "," + Math.round(arg.dy) + ")";
2712-
if (trans) txt.attr("transform", trans);
2713-
});
2698+
// handle simple text drawing
27142699

2715-
// finally process TLatex drawings
2716-
all_args.forEach(arg => {
2717-
if (!arg.txt_g) return;
2718-
any_text = true;
2719-
let txt_g = arg.txt_g;
2720-
delete arg.txt_g;
2721-
txt_g.attr('visibility', null);
2700+
if (JSROOT.nodejs) {
2701+
if (arg.scale && (f > 0)) { arg.box.width *= 1/f; arg.box.height *= 1/f; }
2702+
} else if (!arg.plain && !arg.fast) {
2703+
// exact box dimension only required when complex text was build
2704+
arg.box = jsrp.getElementRect(txt, 'bbox');
2705+
}
27222706

2723-
let box = arg.text_rect;
2707+
if (arg.plain) {
2708+
txt.attr("text-anchor", arg.align[0]);
2709+
if (arg.align[1] == 'top')
2710+
txt.attr("dy", ".8em");
2711+
else if (arg.align[1] == 'middle') {
2712+
if (JSROOT.nodejs) txt.attr("dy", ".4em"); else txt.attr("dominant-baseline", "middle");
2713+
}
2714+
} else {
2715+
txt.attr("text-anchor", "start");
2716+
arg.dx = ((arg.align[0] == "middle") ? -0.5 : ((arg.align[0] == "end") ? -1 : 0)) * arg.box.width;
2717+
arg.dy = ((arg.align[1] == 'top') ? (arg.top_shift || 1) : (arg.align[1] == 'middle') ? (arg.mid_shift || 0.5) : 0) * arg.box.height;
2718+
}
27242719

2725-
if (arg.width) {
2726-
// adjust x position when scale into specified rectangle
2727-
if (arg.align[0] == "middle") arg.x += arg.width / 2; else
2728-
if (arg.align[0] == "end") arg.x += arg.width;
2729-
}
2720+
} else {
27302721

2731-
arg.dx = arg.dy = 0;
2722+
// handle latext drawing
2723+
let box = arg.text_rect;
27322724

2733-
let scale = (f > 0) && (Math.abs(1-f)>0.01) ? 1/f : 1;
2725+
scale = (f > 0) && (Math.abs(1-f)>0.01) ? 1/f : 1;
27342726

2735-
arg.dx = ((arg.align[0] == "middle") ? -0.5 : ((arg.align[0] == "end") ? -1 : 0)) * box.width * scale;
2727+
arg.dx = ((arg.align[0] == "middle") ? -0.5 : ((arg.align[0] == "end") ? -1 : 0)) * box.width * scale;
27362728

2737-
if (arg.height) {
2738-
if (arg.align[1].indexOf('bottom') === 0) arg.y += arg.height; else
2739-
if (arg.align[1] == 'middle') arg.y += arg.height / 2;
2740-
}
2729+
if (arg.align[1] == 'top')
2730+
arg.dy = -box.y1*scale;
2731+
else if (arg.align[1] == 'bottom')
2732+
arg.dy = -box.y2*scale;
2733+
else if (arg.align[1] == 'middle')
2734+
arg.dy = -0.5*(box.y1 + box.y2)*scale;
27412735

2742-
if (arg.align[1] == 'top')
2743-
arg.dy = -box.y1*scale;
2744-
else if (arg.align[1] == 'bottom')
2745-
arg.dy = -box.y2*scale;
2746-
else if (arg.align[1] == 'middle')
2747-
arg.dy = -0.5*(box.y1 + box.y2)*scale;
2736+
}
27482737

27492738
if (!arg.rotate) { arg.x += arg.dx; arg.y += arg.dy; arg.dx = arg.dy = 0; }
27502739

2740+
// use translate and then rotate to avoid complex sign calculations
27512741
let trans = "";
2752-
if (arg.y)
2742+
if (arg.y || arg.x)
27532743
trans = "translate(" + Math.round(arg.x) + "," + Math.round(arg.y) + ")";
2754-
else if (arg.x)
2755-
trans = "translate(" + Math.round(arg.x) + ")";
2744+
//else if (arg.x)
2745+
// trans = "translate(" + Math.round(arg.x) + ")";
27562746
if (arg.rotate)
27572747
trans += " rotate(" + Math.round(arg.rotate) + ")";
27582748
if (scale !== 1)
27592749
trans += " scale(" + scale.toFixed(3) + ")";
2760-
if (arg.dy)
2750+
if (arg.dy || arg.dx)
27612751
trans += " translate(" + Math.round(arg.dx) + "," + Math.round(arg.dy) + ")";
2762-
else if (arg.dx)
2763-
trans += " translate(" + Math.round(arg.dx) + ")";
2764-
if (trans) txt_g.attr("transform", trans);
2752+
//else if (arg.dx)
2753+
// trans += " translate(" + Math.round(arg.dx) + ")";
2754+
if (trans) txt.attr("transform", trans);
27652755
});
27662756

27672757

0 commit comments

Comments
 (0)