Skip to content

Commit

Permalink
Declare and use prSVG
Browse files Browse the repository at this point in the history
  • Loading branch information
linev committed Sep 2, 2024
1 parent ecddeca commit a669adb
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 12 deletions.
8 changes: 6 additions & 2 deletions modules/base/BasePainter.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ import { approximateLabelWidth, replaceSymbolsInTextNode } from './latex.mjs';
import { getColor } from './colors.mjs';


/** @summary Standard prefix for SVG file context as data url
* @private */
const prSVG = 'data:image/svg+xml;charset=utf-8,';

/** @summary Returns visible rect of element
* @param {object} elem - d3.select object with element
* @param {string} [kind] - which size method is used
Expand Down Expand Up @@ -886,7 +890,7 @@ async function svgToImage(svg, image_format, as_buffer) {
});
svg = decodeURIComponent(svg);

const img_src = 'data:image/svg+xml;base64,' + btoa_func(svg);
const img_src = prSVG + btoa_func(svg);

if (isNodeJs()) {
return import('canvas').then(async handle => {
Expand Down Expand Up @@ -953,6 +957,6 @@ function convertDate(dt) {
return res || dt.toLocaleString('en-GB');
}

export { getElementRect, getAbsPosInCanvas, getTDatime, convertDate,
export { prSVG, getElementRect, getAbsPosInCanvas, getTDatime, convertDate,
DrawOptions, TRandom, floatToString, buildSvgCurve, compressSVG,
BasePainter, _loadJSDOM, makeTranslate, addHighlightStyle, svgToImage };
4 changes: 2 additions & 2 deletions modules/base/base3d.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { WebGLRenderer, WebGLRenderTarget, CanvasTexture, TextureLoader, Raycast
LineSegments, LineDashedMaterial, LineBasicMaterial } from '../three.mjs';
import { Font, OrbitControls, SVGRenderer } from '../three_addons.mjs';
import { browser, settings, constants, isBatchMode, nsSVG, isNodeJs, isObject, isFunc, isStr, getDocument } from '../core.mjs';
import { getElementRect, getAbsPosInCanvas, makeTranslate } from './BasePainter.mjs';
import { prSVG, getElementRect, getAbsPosInCanvas, makeTranslate } from './BasePainter.mjs';
import { TAttMarkerHandler } from './TAttMarkerHandler.mjs';
import { getSvgLineStyle } from './TAttLineHandler.mjs';

Expand Down Expand Up @@ -1510,7 +1510,7 @@ class PointsCreator {
imgdata = '<svg width="64" height="64" xmlns="http://www.w3.org/2000/svg">' +
`<path d="${handler.create(32, 32)}" style="stroke: ${handler.getStrokeColor()}; stroke-width: ${w}; fill: ${handler.getFillColor()}"></path>`+
'</svg>',
dataUrl = 'data:image/svg+xml;charset=utf8,' + (isNodeJs() ? imgdata : encodeURIComponent(imgdata));
dataUrl = prSVG + (isNodeJs() ? imgdata : encodeURIComponent(imgdata));
let promise;

if (isNodeJs()) {
Expand Down
4 changes: 2 additions & 2 deletions modules/gpad/RPadPainter.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { gStyle, settings, browser, constants, internals, addMethods,
isPromise, getPromise, postponePromise, isBatchMode, isObject, isFunc, isStr, clTPad, clTFrame, nsREX } from '../core.mjs';
import { ColorPalette, addColor, getRootColors, convertColor } from '../base/colors.mjs';
import { RObjectPainter } from '../base/RObjectPainter.mjs';
import { getElementRect, getAbsPosInCanvas, DrawOptions, compressSVG, makeTranslate, svgToImage } from '../base/BasePainter.mjs';
import { prSVG, getElementRect, getAbsPosInCanvas, DrawOptions, compressSVG, makeTranslate, svgToImage } from '../base/BasePainter.mjs';
import { selectActivePad, getActivePad } from '../base/ObjectPainter.mjs';
import { registerForResize, saveFile } from '../gui/utils.mjs';
import { BrowserLayout } from '../gui/display.mjs';
Expand Down Expand Up @@ -1337,7 +1337,7 @@ class RPadPainter extends RObjectPainter {
if (res)
this.getCanvPainter()?.sendWebsocket(`SAVE:${filename}:${res}`);
} else
saveFile(filename, (kind !== 'svg') ? imgdata : 'data:image/svg+xml;charset=utf-8,'+encodeURIComponent(imgdata));
saveFile(filename, (kind !== 'svg') ? imgdata : prSVG + encodeURIComponent(imgdata));
});
}

Expand Down
4 changes: 2 additions & 2 deletions modules/gpad/TPadPainter.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { gStyle, settings, constants, browser, internals, BIT,
import { select as d3_select, rgb as d3_rgb } from '../d3.mjs';
import { ColorPalette, adoptRootColors, getColorPalette, getGrayColors, extendRootColors,
getRGBfromTColor, decodeWebCanvasColors } from '../base/colors.mjs';
import { getElementRect, getAbsPosInCanvas, DrawOptions, compressSVG, makeTranslate,
import { prSVG, getElementRect, getAbsPosInCanvas, DrawOptions, compressSVG, makeTranslate,
getTDatime, convertDate, svgToImage } from '../base/BasePainter.mjs';
import { ObjectPainter, selectActivePad, getActivePad } from '../base/ObjectPainter.mjs';
import { TAttLineHandler } from '../base/TAttLineHandler.mjs';
Expand Down Expand Up @@ -2234,7 +2234,7 @@ class TPadPainter extends ObjectPainter {
if (res)
this.getCanvPainter()?.sendWebsocket(`SAVE:${filename}:${res}`);
} else {
const prefix = (kind === 'svg') ? 'data:image/svg+xml;charset=utf-8,' : (kind === 'json' ? 'data:application/json;charset=utf-8,' : '');
const prefix = (kind === 'svg') ? prSVG : (kind === 'json' ? 'data:application/json;charset=utf-8,' : '');
saveFile(filename, prefix ? prefix + encodeURIComponent(imgdata) : imgdata);
}
});
Expand Down
6 changes: 2 additions & 4 deletions modules/gui/utils.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -502,10 +502,8 @@ let _saveFileFunc = null;
* @private */

function getBinFileContent(content) {
const svg_prefix = 'data:image/svg+xml;charset=utf-8,';

if (content.indexOf(svg_prefix) === 0)
return decodeURIComponent(content.slice(svg_prefix.length));
if (content.indexOf(prSVG) === 0)
return decodeURIComponent(content.slice(prSVG.length));

if (content.indexOf('data:image/') === 0) {
const p = content.indexOf('base64,');
Expand Down

0 comments on commit a669adb

Please sign in to comment.