I have a large collection of symbols in svg format that need to be draw in a Canvas drawing session in Win2D. Many of these will be repeated, so I intend to load them in advance to a library. There seem to be different ways to do this. One is the CanvasSvgDocument, which I can load from a Uri with its LoadAsync method and draw with the DrawingSession method DrawSvg and its overrides. Trouble is, DrawSvg ignores the size argument and always draws the image at the same size. For example, inside a drawing session:
float2 symbolSize;
symbolSize.x = 400;//just to experiment
symbolSize.y = 300;
session.DrawSvg(m_test_svg, symbolSize, 100, 100);
Regardless of symbolSize the image is always rendered at the same small size. Is there something amiss with the size argument, or do I misunderstand what it's for? Perhaps this is not the best way to draw the Svg in any case. These are in a zoomable view and will need to be drawn sharp at any magnification.
[Update] I notice that I had the wrong type for the size argument, it should be
winrt::Windows::Foundation::Size symbolSize;
symbolSize.Width = 400;
symbolSize.Height = 300;
session.DrawSvg(m_test_svg, symbolSize,100,100);
But the results are the same, which is maybe why the compiler accepts both. That is, I expect that Size types to Float2. But shouldn't it affect the drawing?
[Update] The doc says the Size argument refers to viewport size, not viewbox size. Perhaps it would be enough if someone involved in the creation of this class could describe the purpose of the Size argument in this call - and if it is not intended to determine the size of the rendered image, is there some other way to do that? Thanks. [Update]To be more specific, after looking into this further: scaling this scalable vector image requires changing the viewbox, while the argument to DrawSvg apparently affects only the viewPort, i.e the portion of the image to be rendered. Is there any way to scale the image larger or smaller? If not, I might as well use pngs. If you can't scale an svg, what would be the point of it?
[Update] Still hoping for feedback on this. I have heard elsewhere that one could change the transform for the whole canvas before drawing any of the hundreds of symbols. Haven't figured out how to do that yet, but another idea came up: the CanvasSvgAspectScaling enumeration appears to include something that would be relevant: PreserveAspectRatio with an appropriate value means to "scale viewbox up to the maximum needed to make the entire viewbox visible in viewport." It sounds like that would mean: set a viewport the size of the desired image, set the CanvasSvgAspectScaling to have that enumerated value, and the svg would then draw to fill the viewport, which is the argument provided in CanvasDrawingSession.DrawSvg. This would be good, but it has no effect.
OK, here's the answer. Totally undocumented as near as I can tell, so I hope it's stable. If so I really should make it a trade secret, but I'm feeling public-spirited. It appears that if I simply delete the Height and Width attributes of an svg then the viewport size argument in DrawingSession.DrawSvg will become the size of the drawn image - whatever scale you want, with no need to set transforms anywhere. Viewbox and Viewport become one. Very simple. If there is anyone at MS who knows a reason why I should not do this, please speak!