-
Notifications
You must be signed in to change notification settings - Fork 23
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add option to adjust the viewbox to match the width of the svg #43
base: gh-pages
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,6 +18,7 @@ testCases = [ | |
{ name: 'color_home', preserveFill: false, expected: 'home' }, | ||
{ name: 'implicit_lineto' }, | ||
{ name: 'implicit_r_lineto' }, | ||
{ name: 'fix_viewbox', fixViewbox: true }, | ||
] | ||
|
||
function runTests() { | ||
|
@@ -29,8 +30,10 @@ function runTests() { | |
svgAnchor.innerHTML = testData[name].svg; | ||
|
||
var preserveFill = testCases[i].preserveFill || false; | ||
|
||
var fixViewbox = testCases[i].fixViewbox || false; | ||
|
||
var output = ProcessSvg(svgAnchor.querySelector('svg'), 1, 1, 0, 0, preserveFill); | ||
var output = ProcessSvg(svgAnchor.querySelector('svg'), 1, 1, 0, 0, preserveFill, fixViewbox); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: maybe rather than |
||
|
||
var expected = testCases[i].expected || name; | ||
var expectedOutput = testData[expected]['expected']; | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -323,14 +323,28 @@ function HandleNode(svgNode, scaleX, scaleY, translateX, translateY, preserveFil | |
return output; | ||
} | ||
|
||
function ProcessSvg(svgNode, scaleX, scaleY, translateX, translateY, preserveFill) { | ||
function ProcessSvg(svgNode, scaleX, scaleY, translateX, translateY, preserveFill, fixViewbox) { | ||
var output = ''; | ||
var canvasSize = svgNode.viewBox.baseVal.width; | ||
if (canvasSize == 0) | ||
canvasSize = svgNode.width.baseVal.value; | ||
|
||
if (fixViewbox) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: document what's happening/what this is for |
||
var scaleFactor = svgNode.width.baseVal.value / svgNode.viewBox.baseVal.width; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. what if there is no width specified? |
||
canvasSize *= scaleFactor; | ||
scaleX *= scaleFactor; | ||
scaleY *= scaleFactor; | ||
if (svgNode.viewBox.baseVal.y < 0) { | ||
translateY -= svgNode.viewBox.baseVal.y * scaleFactor; | ||
} | ||
if (svgNode.viewBox.baseVal.x < 0) { | ||
translateX -= svgNode.viewBox.baseVal.x * scaleFactor; | ||
} | ||
} | ||
|
||
if (canvasSize != 48) | ||
output += 'CANVAS_DIMENSIONS, ' + canvasSize + ',\n'; | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: remove spaces |
||
output += HandleNode(svgNode, scaleX, scaleY, translateX, translateY, preserveFill); | ||
// Truncate final comma and newline. | ||
return output.slice(0, -2); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should I enable this checkbox by default? We usually don't get the right result if the width/height of the svg doesn't match the viewbox. If it matches, this checkbox is a no-op
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Only thing I'm worried about is how this breaks in cases we haven't considered like a non-square size, or unspecified size, or non-square viewbox. Therefore it might be safer to not check it by default.
Also can we update the text to
"""
Normalize viewbox and path coordinates to match the specified width of the SVG (assuming square dimensions).
"""