Skip to content

Commit

Permalink
Support for SVG files using percentage of viewbox in width and height…
Browse files Browse the repository at this point in the history
… attributes.
  • Loading branch information
JoanCharmant committed Mar 28, 2011
1 parent d41dc14 commit 9e4de0d
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 51 deletions.
21 changes: 18 additions & 3 deletions ScreenManager/PlayerScreen/Drawings/DrawingSVG.cs
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,8 @@ public override InfosFading infosFading
private int m_iOriginalHeight;
private double m_fOriginalAspectRatio;

private bool m_bSizeInPercentage;

private bool m_bFinishedResizing;

// Decoration
Expand Down Expand Up @@ -111,8 +113,18 @@ public DrawingSVG(int _iWidth, int _iHeight, long _iTimestamp, long _iAverageTim
m_SvgWindow.Src = _filename;
m_bLoaded = true;

m_iOriginalWidth = (int)m_SvgWindow.Document.RootElement.Width.BaseVal.Value;
m_iOriginalHeight = (int)m_SvgWindow.Document.RootElement.Height.BaseVal.Value;
if(m_SvgWindow.Document.RootElement.Width.BaseVal.UnitType == SvgLengthType.Percentage)
{
m_bSizeInPercentage = true;
m_iOriginalWidth = (int)(m_SvgWindow.Document.RootElement.ViewBox.BaseVal.Width * (m_SvgWindow.Document.RootElement.Width.BaseVal.Value/100));
m_iOriginalHeight = (int)(m_SvgWindow.Document.RootElement.ViewBox.BaseVal.Height * (m_SvgWindow.Document.RootElement.Height.BaseVal.Value/100));
}
else
{
m_bSizeInPercentage = false;
m_iOriginalWidth = (int)m_SvgWindow.Document.RootElement.Width.BaseVal.Value;
m_iOriginalHeight = (int)m_SvgWindow.Document.RootElement.Height.BaseVal.Value;
}

// Set the initial scale so that the drawing is some part of the image height, to make sure it fits well.
m_fInitialScale = (float) (((float)_iHeight * 0.75) / m_iOriginalHeight);
Expand Down Expand Up @@ -396,7 +408,10 @@ private void RenderAtNewScale()

if(m_svgRendered == null || m_fDrawingRenderingScale != m_SvgWindow.Document.RootElement.CurrentScale)
{
m_SvgWindow.Document.RootElement.CurrentScale = (float)m_fDrawingRenderingScale;
// In the case of percentage, CurrentScale is always 100%. But since there is a cache for the transformation matrix,
// we need to set it anyway to clear the cache.
m_SvgWindow.Document.RootElement.CurrentScale = m_bSizeInPercentage ? 1.0f : (float)m_fDrawingRenderingScale;

m_SvgWindow.InnerWidth = m_RescaledRectangle.Width;
m_SvgWindow.InnerHeight = m_RescaledRectangle.Height;

Expand Down
4 changes: 2 additions & 2 deletions SharpVectorRenderingEngine/SharpVectorRenderingEngine.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,10 @@
<OutputPath>bin\x86\Debug\</OutputPath>
<AllowUnsafeBlocks>False</AllowUnsafeBlocks>
<NoStdLib>False</NoStdLib>
<DebugType>None</DebugType>
<DebugType>Full</DebugType>
<WarningLevel>4</WarningLevel>
<TreatWarningsAsErrors>false</TreatWarningsAsErrors>
<DebugSymbols>false</DebugSymbols>
<DebugSymbols>true</DebugSymbols>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<ConfigurationOverrideFile>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,46 +1,41 @@
using System;
using System.Diagnostics;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Text.RegularExpressions;

using SharpVectors.Dom.Svg;
using SharpVectors.Dom.Svg.Rendering;

namespace SharpVectors.Renderer.Gdi
{
/// <summary>
/// Summary description for SvgElementGraphicsNode.
/// </summary>
public class SvgElementGraphicsNode : GraphicsNode
{
#region Constructor
public SvgElementGraphicsNode(SvgElement element) : base(element)
{
}
#endregion

#region Public Methods
public override void Render(ISvgRenderer renderer)
{
GraphicsWrapper graphics = ((GdiRenderer) renderer).GraphicsWrapper;

SvgSvgElement svgElm = (SvgSvgElement) element;

float x = (float)svgElm.X.AnimVal.Value;
float y = (float)svgElm.Y.AnimVal.Value;
float width = (float)svgElm.Width.AnimVal.Value;
float height = (float)svgElm.Height.AnimVal.Value;

RectangleF elmRect = new RectangleF(x, y, width, height);

if ( element.ParentNode is SvgElement )
{
// TODO: should it be moved with x and y?
}

fitToViewbox(graphics, elmRect);
}
#endregion
}
}
using System;
using System.Diagnostics;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Text.RegularExpressions;

using SharpVectors.Dom.Svg;
using SharpVectors.Dom.Svg.Rendering;

namespace SharpVectors.Renderer.Gdi
{
/// <summary>
/// Summary description for SvgElementGraphicsNode.
/// </summary>
public class SvgElementGraphicsNode : GraphicsNode
{
#region Constructor
public SvgElementGraphicsNode(SvgElement element) : base(element)
{
}
#endregion

#region Public Methods
public override void Render(ISvgRenderer renderer)
{
GraphicsWrapper graphics = ((GdiRenderer) renderer).GraphicsWrapper;

SvgSvgElement svgElm = (SvgSvgElement) element;

float x = (float)svgElm.X.AnimVal.Value;
float y = (float)svgElm.Y.AnimVal.Value;
float width = (float)svgElm.Width.AnimVal.Value;
float height = (float)svgElm.Height.AnimVal.Value;

RectangleF elmRect = new RectangleF(x, y, width, height);

fitToViewbox(graphics, elmRect);
}
#endregion
}
}

0 comments on commit 9e4de0d

Please sign in to comment.