Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/main' into jonmmease/vega_5.27.0
Browse files Browse the repository at this point in the history
  • Loading branch information
jonmmease committed Jan 18, 2024
2 parents 4d04f07 + 7dea625 commit 40ab0c3
Show file tree
Hide file tree
Showing 5 changed files with 1,057 additions and 455 deletions.
69 changes: 68 additions & 1 deletion vl-convert-rs/src/converter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -394,6 +394,62 @@ function vegaToSvg(vgSpec, allowedBaseUrls, formatLocale, timeFormatLocale, erro
return svgPromise
}
function cloneScenegraph(obj) {
const keys = [
'marktype', 'name', 'role', 'interactive', 'clip', 'items', 'zindex',
'x', 'y', 'width', 'height', 'align', 'baseline', // layout
'fill', 'fillOpacity', 'opacity', 'blend', // fill
'stroke', 'strokeOpacity', 'strokeWidth', 'strokeCap',
'strokeJoin', // stroke
'strokeDash', 'strokeDashOffset', // stroke dash
'strokeForeground', 'strokeOffset', // group
'startAngle', 'endAngle', 'innerRadius', 'outerRadius', // arc
'cornerRadius', 'padAngle', // arc, rect
'cornerRadiusTopLeft', 'cornerRadiusTopRight', // rect, group
'cornerRadiusBottomLeft', 'cornerRadiusBottomRight',
'interpolate', 'tension', 'orient', 'defined', // area, line
'url', 'aspect', 'smooth', // image
'path', 'scaleX', 'scaleY', // path
'x2', 'y2', // rule
'size', 'shape', // symbol
'text', 'angle', 'theta', 'radius', 'dir', 'dx', 'dy', // text
'ellipsis', 'limit', 'lineBreak', 'lineHeight',
'font', 'fontSize', 'fontWeight', 'fontStyle', 'fontVariant', // font
'description', 'aria', 'ariaRole', 'ariaRoleDescription' // aria
];
// Check if the input is an object (including an array) or null
if (typeof obj !== 'object' || obj === null) {
return obj;
}
// Initialize the clone as an array or object based on the input type
const clone = Array.isArray(obj) ? [] : {};
// If the object is an array, iterate over its elements
if (Array.isArray(obj)) {
for (let i = 0; i < obj.length; i++) {
// Apply the function recursively to each element
clone.push(cloneScenegraph(obj[i]));
}
} else {
// If the object is not an array, iterate over its keys
for (const key in obj) {
// Clone only the properties with specified keys
if (key === "shape" && typeof obj[key] === "function") {
// Convert path object to SVG path string.
// Initialize context. This is needed for obj.shape(obj) to work.
obj.shape.context();
clone["shape"] = obj.shape(obj) ?? "";
} else if (keys.includes(key)) {
clone[key] = cloneScenegraph(obj[key]);
}
}
}
return clone;
}
function vegaToScenegraph(vgSpec, allowedBaseUrls, formatLocale, timeFormatLocale, errors) {
if (formatLocale != null) {
vega.formatLocale(formatLocale);
Expand All @@ -411,7 +467,18 @@ function vegaToScenegraph(vgSpec, allowedBaseUrls, formatLocale, timeFormatLocal
}
}).then(() => {
return view.runAsync().then(
() => JSON.parse(JSON.parse(vega.sceneToJSON(view.scenegraph())))
() => {
let padding = view.padding();
return {
width: Math.max(0, view._viewWidth + padding.left + padding.right),
height: Math.max(0, view._viewHeight + padding.top + padding.bottom),
origin: [
padding.left + view._origin[0],
padding.top + view._origin[1]
],
scenegraph: cloneScenegraph(view.scenegraph().root)
}
}
).finally(() => {
view.finalize();
vega.resetDefaultLocale();
Expand Down
3 changes: 2 additions & 1 deletion vl-convert-rs/tests/test_specs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -591,7 +591,8 @@ mod test_scenegraph {
fn test(
#[values(
// This one has round-trip stable numeric values
"no_text_in_font_metrics"
"no_text_in_font_metrics",
"geoScale",
)]
name: &str,
) {
Expand Down
Binary file modified vl-convert-rs/tests/vl-specs/expected/v5_8/geoScale.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit 40ab0c3

Please sign in to comment.