Skip to content

Commit

Permalink
Last place for async text drawing
Browse files Browse the repository at this point in the history
  • Loading branch information
linev committed Sep 26, 2024
1 parent cf43126 commit 3197366
Showing 1 changed file with 106 additions and 108 deletions.
214 changes: 106 additions & 108 deletions modules/hist/RPavePainter.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -175,62 +175,64 @@ class RLegendPainter extends RPavePainter {
if (!nlines || !pp) return this;

const stepy = height / nlines, margin_x = 0.02 * width;
let posy = 0;

textFont.setSize(height/(nlines * 1.2));
this.startTextDrawing(textFont, 'font');
return this.startTextDrawingAsync(textFont, 'font').then(() => {

if (legend.fTitle) {
this.drawText({ latex: 1, width: width - 2*margin_x, height: stepy, x: margin_x, y: posy, text: legend.fTitle });
posy += stepy;
}
let posy = 0;

for (let i = 0; i < legend.fEntries.length; ++i) {
const entry = legend.fEntries[i], w4 = Math.round(width/4);
let objp = null;
if (legend.fTitle) {
this.drawText({ latex: 1, width: width - 2*margin_x, height: stepy, x: margin_x, y: posy, text: legend.fTitle });
posy += stepy;
}

this.drawText({ latex: 1, width: 0.75*width - 3*margin_x, height: stepy, x: 2*margin_x + w4, y: posy, text: entry.fLabel });
for (let i = 0; i < legend.fEntries.length; ++i) {
const entry = legend.fEntries[i], w4 = Math.round(width/4);
let objp = null;

if (entry.fDrawableId !== 'custom')
objp = pp.findSnap(entry.fDrawableId, true);
else if (entry.fDrawable.fIO) {
objp = new RObjectPainter(this.getPadPainter(), entry.fDrawable.fIO);
if (entry.fLine) objp.createv7AttLine();
if (entry.fFill) objp.createv7AttFill();
if (entry.fMarker) objp.createv7AttMarker();
}
this.drawText({ latex: 1, width: 0.75*width - 3*margin_x, height: stepy, x: 2*margin_x + w4, y: posy, text: entry.fLabel });

if (entry.fFill && objp?.fillatt) {
this.draw_g
.append('svg:path')
.attr('d', `M${Math.round(margin_x)},${Math.round(posy + stepy*0.1)}h${w4}v${Math.round(stepy*0.8)}h${-w4}z`)
.call(objp.fillatt.func);
}
if (entry.fDrawableId !== 'custom')
objp = pp.findSnap(entry.fDrawableId, true);
else if (entry.fDrawable.fIO) {
objp = new RObjectPainter(this.getPadPainter(), entry.fDrawable.fIO);
if (entry.fLine) objp.createv7AttLine();
if (entry.fFill) objp.createv7AttFill();
if (entry.fMarker) objp.createv7AttMarker();
}

if (entry.fLine && objp?.lineatt) {
this.draw_g
.append('svg:path')
.attr('d', `M${Math.round(margin_x)},${Math.round(posy + stepy/2)}h${w4}`)
.call(objp.lineatt.func);
}
if (entry.fFill && objp?.fillatt) {
this.draw_g
.append('svg:path')
.attr('d', `M${Math.round(margin_x)},${Math.round(posy + stepy*0.1)}h${w4}v${Math.round(stepy*0.8)}h${-w4}z`)
.call(objp.fillatt.func);
}

if (entry.fError && objp?.lineatt) {
this.draw_g
.append('svg:path')
.attr('d', `M${Math.round(margin_x + width/8)},${Math.round(posy + stepy*0.2)}v${Math.round(stepy*0.6)}`)
.call(objp.lineatt.func);
}
if (entry.fLine && objp?.lineatt) {
this.draw_g
.append('svg:path')
.attr('d', `M${Math.round(margin_x)},${Math.round(posy + stepy/2)}h${w4}`)
.call(objp.lineatt.func);
}

if (entry.fMarker && objp?.markeratt) {
this.draw_g.append('svg:path')
.attr('d', objp.markeratt.create(margin_x + width/8, posy + stepy/2))
.call(objp.markeratt.func);
}
if (entry.fError && objp?.lineatt) {
this.draw_g
.append('svg:path')
.attr('d', `M${Math.round(margin_x + width/8)},${Math.round(posy + stepy*0.2)}v${Math.round(stepy*0.6)}`)
.call(objp.lineatt.func);
}

posy += stepy;
}
if (entry.fMarker && objp?.markeratt) {
this.draw_g.append('svg:path')
.attr('d', objp.markeratt.create(margin_x + width/8, posy + stepy/2))
.call(objp.markeratt.func);
}

posy += stepy;
}

return this.finishTextDrawing();
return this.finishTextDrawing();
});
}

/** @summary draw RLegend object */
Expand All @@ -251,7 +253,7 @@ class RLegendPainter extends RPavePainter {
class RPaveTextPainter extends RPavePainter {

/** @summary draw RPaveText content */
drawContent() {
async drawContent() {
const pavetext = this.getObject(),
textFont = this.v7EvalFont('text', { size: 12, color: 'black', align: 22 }),
width = this.pave_width,
Expand All @@ -261,20 +263,15 @@ class RPaveTextPainter extends RPavePainter {
if (!nlines) return;

const stepy = height / nlines, margin_x = 0.02 * width;
let posy = 0;

textFont.setSize(height/(nlines * 1.2));

this.startTextDrawing(textFont, 'font');

for (let i = 0; i < pavetext.fText.length; ++i) {
const line = pavetext.fText[i];
return this.startTextDrawingAsync(textFont, 'font').then(() => {
for (let i = 0, posy = 0; i < pavetext.fText.length; ++i, posy += stepy)
this.drawText({ latex: 1, width: width - 2*margin_x, height: stepy, x: margin_x, y: posy, text: pavetext.fText[i] });

this.drawText({ latex: 1, width: width - 2*margin_x, height: stepy, x: margin_x, y: posy, text: line });
posy += stepy;
}

return this.finishTextDrawing(undefined, true);
return this.finishTextDrawing(undefined, true);
});
}

/** @summary draw RPaveText object */
Expand Down Expand Up @@ -386,7 +383,7 @@ class RHistStatsPainter extends RPavePainter {
menu.addchk((obj.fShowMask & (1<<n)), obj.fEntries[n], n, action);

return this.fillObjectExecMenu(menu);
}).then(menu => menu.show());
}).then(menu => menu.show());
}

/** @summary Draw statistic */
Expand Down Expand Up @@ -419,66 +416,67 @@ class RHistStatsPainter extends RPavePainter {
text_g.selectAll('*').remove();

textFont.setSize(height/(nlines * 1.2));
this.startTextDrawing(textFont, 'font', text_g);

if (nlines === 1)
this.drawText({ width, height, text: lines[0], latex: 1, draw_g: text_g });
else {
for (let j = 0; j < nlines; ++j) {
const posy = j*stepy;

if (first_stat && (j >= first_stat)) {
const parts = lines[j].split('|');
for (let n = 0; n < parts.length; ++n) {
this.drawText({ align: 'middle', x: width * n / num_cols, y: posy, latex: 0,
width: width/num_cols, height: stepy, text: parts[n], draw_g: text_g });
return this.startTextDrawingAsync(textFont, 'font', text_g).then(() => {

if (nlines === 1)
this.drawText({ width, height, text: lines[0], latex: 1, draw_g: text_g });
else {
for (let j = 0; j < nlines; ++j) {
const posy = j*stepy;

if (first_stat && (j >= first_stat)) {
const parts = lines[j].split('|');
for (let n = 0; n < parts.length; ++n) {
this.drawText({ align: 'middle', x: width * n / num_cols, y: posy, latex: 0,
width: width/num_cols, height: stepy, text: parts[n], draw_g: text_g });
}
} else if (lines[j].indexOf('=') < 0) {
if (j === 0) {
has_head = true;
const max_hlen = Math.max(maxlen, Math.round((width-2*margin_x)/stepy/0.65));
if (lines[j].length > max_hlen + 5)
lines[j] = lines[j].slice(0, max_hlen+2) + '...';
}
this.drawText({ align: (j === 0) ? 'middle' : 'start', x: margin_x, y: posy,
width: width - 2*margin_x, height: stepy, text: lines[j], draw_g: text_g });
} else {
const parts = lines[j].split('='), args = [];

for (let n = 0; n < 2; ++n) {
const arg = {
align: (n === 0) ? 'start' : 'end', x: margin_x, y: posy,
width: width-2*margin_x, height: stepy, text: parts[n], draw_g: text_g,
_expected_width: width-2*margin_x, _args: args,
post_process(painter) {
if (this._args[0].ready && this._args[1].ready)
painter.scaleTextDrawing(1.05*(this._args[0].result_width && this._args[1].result_width)/this.__expected_width, this.draw_g);
}
};
args.push(arg);
}

for (let n = 0; n < 2; ++n)
this.drawText(args[n]);
}
} else if (lines[j].indexOf('=') < 0) {
if (j === 0) {
has_head = true;
const max_hlen = Math.max(maxlen, Math.round((width-2*margin_x)/stepy/0.65));
if (lines[j].length > max_hlen + 5)
lines[j] = lines[j].slice(0, max_hlen+2) + '...';
}
this.drawText({ align: (j === 0) ? 'middle' : 'start', x: margin_x, y: posy,
width: width - 2*margin_x, height: stepy, text: lines[j], draw_g: text_g });
} else {
const parts = lines[j].split('='), args = [];

for (let n = 0; n < 2; ++n) {
const arg = {
align: (n === 0) ? 'start' : 'end', x: margin_x, y: posy,
width: width-2*margin_x, height: stepy, text: parts[n], draw_g: text_g,
_expected_width: width-2*margin_x, _args: args,
post_process(painter) {
if (this._args[0].ready && this._args[1].ready)
painter.scaleTextDrawing(1.05*(this._args[0].result_width && this._args[1].result_width)/this.__expected_width, this.draw_g);
}
};
args.push(arg);
}

for (let n = 0; n < 2; ++n)
this.drawText(args[n]);
}
}
}

let lpath = '';
let lpath = '';

if (has_head)
lpath += 'M0,' + Math.round(stepy) + 'h' + width;
if (has_head)
lpath += 'M0,' + Math.round(stepy) + 'h' + width;

if ((first_stat > 0) && (num_cols > 1)) {
for (let nrow = first_stat; nrow < nlines; ++nrow)
lpath += 'M0,' + Math.round(nrow * stepy) + 'h' + width;
for (let ncol = 0; ncol < num_cols - 1; ++ncol)
lpath += 'M' + Math.round(width / num_cols * (ncol + 1)) + ',' + Math.round(first_stat * stepy) + 'V' + height;
}
if ((first_stat > 0) && (num_cols > 1)) {
for (let nrow = first_stat; nrow < nlines; ++nrow)
lpath += 'M0,' + Math.round(nrow * stepy) + 'h' + width;
for (let ncol = 0; ncol < num_cols - 1; ++ncol)
lpath += 'M' + Math.round(width / num_cols * (ncol + 1)) + ',' + Math.round(first_stat * stepy) + 'V' + height;
}

if (lpath) this.draw_g.append('svg:path').attr('d', lpath);
if (lpath) this.draw_g.append('svg:path').attr('d', lpath);

return this.finishTextDrawing(text_g);
return this.finishTextDrawing(text_g);
});
}

/** @summary Redraw stats box */
Expand Down

0 comments on commit 3197366

Please sign in to comment.