[12762] | 1 | using System;
|
---|
| 2 | using System.Xml;
|
---|
| 3 | using System.Text;
|
---|
| 4 | using System.Drawing;
|
---|
| 5 | using System.Drawing.Drawing2D;
|
---|
| 6 | using System.Diagnostics;
|
---|
| 7 | using System.Globalization;
|
---|
| 8 | using System.Collections.Generic;
|
---|
| 9 |
|
---|
| 10 | using SharpVectors.Dom.Css;
|
---|
| 11 | using SharpVectors.Dom.Svg;
|
---|
| 12 |
|
---|
| 13 | namespace SharpVectors.Renderers.Gdi
|
---|
| 14 | {
|
---|
| 15 | public sealed class GdiRenderingHelper
|
---|
| 16 | {
|
---|
| 17 | #region Private Fields
|
---|
| 18 |
|
---|
| 19 | private string _currentLang;
|
---|
| 20 | private GdiGraphicsRenderer _renderer;
|
---|
| 21 | //private Dictionary<ISvgElement, GdiRenderingBase> _rendererMap;
|
---|
| 22 | private Stack<GdiRenderingBase> _rendererMap;
|
---|
| 23 |
|
---|
| 24 | #endregion
|
---|
| 25 |
|
---|
| 26 | #region Constructors and Destructor
|
---|
| 27 |
|
---|
| 28 | public GdiRenderingHelper(GdiGraphicsRenderer renderer)
|
---|
| 29 | {
|
---|
| 30 | _currentLang = CultureInfo.CurrentCulture.TwoLetterISOLanguageName;
|
---|
| 31 | _renderer = renderer;
|
---|
| 32 | //_rendererMap = new Dictionary<ISvgElement, GdiRenderingBase>();
|
---|
| 33 | _rendererMap = new Stack<GdiRenderingBase>();
|
---|
| 34 | }
|
---|
| 35 |
|
---|
| 36 | #endregion
|
---|
| 37 |
|
---|
| 38 | #region Public Methods
|
---|
| 39 |
|
---|
| 40 | public void Render(ISvgDocument docElement)
|
---|
| 41 | {
|
---|
| 42 | SvgSvgElement root = docElement.RootElement as SvgSvgElement;
|
---|
| 43 |
|
---|
| 44 | if (root != null)
|
---|
| 45 | {
|
---|
| 46 | this.Render(root);
|
---|
| 47 | }
|
---|
| 48 | }
|
---|
| 49 |
|
---|
| 50 | public void Render(SvgDocument docElement)
|
---|
| 51 | {
|
---|
| 52 | SvgSvgElement root = docElement.RootElement as SvgSvgElement;
|
---|
| 53 |
|
---|
| 54 | if (root != null)
|
---|
| 55 | {
|
---|
| 56 | this.Render(root);
|
---|
| 57 | }
|
---|
| 58 | }
|
---|
| 59 |
|
---|
| 60 | public void Render(ISvgElement svgElement)
|
---|
| 61 | {
|
---|
| 62 | if (svgElement == null)
|
---|
| 63 | {
|
---|
| 64 | return;
|
---|
| 65 | }
|
---|
| 66 |
|
---|
| 67 | string elementName = svgElement.LocalName;
|
---|
| 68 |
|
---|
| 69 | if (String.Equals(elementName, "use"))
|
---|
| 70 | {
|
---|
| 71 | RenderUseElement(svgElement);
|
---|
| 72 | }
|
---|
| 73 | else
|
---|
| 74 | {
|
---|
| 75 | RenderElement(svgElement);
|
---|
| 76 | }
|
---|
| 77 | }
|
---|
| 78 |
|
---|
| 79 | public void RenderChildren(ISvgElement svgElement)
|
---|
| 80 | {
|
---|
| 81 | if (svgElement == null)
|
---|
| 82 | {
|
---|
| 83 | return;
|
---|
| 84 | }
|
---|
| 85 |
|
---|
| 86 | string elementName = svgElement.LocalName;
|
---|
| 87 |
|
---|
| 88 | if (String.Equals(elementName, "switch"))
|
---|
| 89 | {
|
---|
| 90 | RenderSwitchChildren(svgElement);
|
---|
| 91 | }
|
---|
| 92 | else
|
---|
| 93 | {
|
---|
| 94 | RenderElementChildren(svgElement);
|
---|
| 95 | }
|
---|
| 96 | }
|
---|
| 97 |
|
---|
| 98 | #endregion
|
---|
| 99 |
|
---|
| 100 | #region Private Methods
|
---|
| 101 |
|
---|
| 102 | private void RenderElement(ISvgElement svgElement)
|
---|
| 103 | {
|
---|
| 104 | bool isNotRenderable = !svgElement.IsRenderable || String.Equals(svgElement.LocalName, "a");
|
---|
| 105 |
|
---|
| 106 | if (isNotRenderable)
|
---|
| 107 | {
|
---|
| 108 | return;
|
---|
| 109 | }
|
---|
| 110 |
|
---|
| 111 | GdiRenderingBase renderingNode = GdiRendering.Create(svgElement);
|
---|
| 112 | if (renderingNode == null)
|
---|
| 113 | {
|
---|
| 114 | return;
|
---|
| 115 | }
|
---|
| 116 |
|
---|
| 117 | if (!renderingNode.NeedRender(_renderer))
|
---|
| 118 | {
|
---|
| 119 | renderingNode.Dispose();
|
---|
| 120 | renderingNode = null;
|
---|
| 121 | return;
|
---|
| 122 | }
|
---|
| 123 |
|
---|
| 124 | //_rendererMap[svgElement] = renderingNode;
|
---|
| 125 | _rendererMap.Push(renderingNode);
|
---|
| 126 | renderingNode.BeforeRender(_renderer);
|
---|
| 127 |
|
---|
| 128 | renderingNode.Render(_renderer);
|
---|
| 129 |
|
---|
| 130 | if (!renderingNode.IsRecursive && svgElement.HasChildNodes)
|
---|
| 131 | {
|
---|
| 132 | RenderChildren(svgElement);
|
---|
| 133 | }
|
---|
| 134 |
|
---|
| 135 | //renderingNode = _rendererMap[svgElement];
|
---|
| 136 | renderingNode = _rendererMap.Pop();
|
---|
| 137 | Debug.Assert(renderingNode.Element == svgElement);
|
---|
| 138 | renderingNode.AfterRender(_renderer);
|
---|
| 139 |
|
---|
| 140 | //_rendererMap.Remove(svgElement);
|
---|
| 141 |
|
---|
| 142 | renderingNode.Dispose();
|
---|
| 143 | renderingNode = null;
|
---|
| 144 | }
|
---|
| 145 |
|
---|
| 146 | private void RenderUseElement(ISvgElement svgElement)
|
---|
| 147 | {
|
---|
| 148 | SvgUseElement useElement = (SvgUseElement)svgElement;
|
---|
| 149 |
|
---|
| 150 | XmlElement refEl = useElement.ReferencedElement;
|
---|
| 151 | if (refEl == null)
|
---|
| 152 | return;
|
---|
| 153 | XmlElement refElParent = (XmlElement)refEl.ParentNode;
|
---|
| 154 | useElement.OwnerDocument.Static = true;
|
---|
| 155 | useElement.CopyToReferencedElement(refEl);
|
---|
| 156 | refElParent.RemoveChild(refEl);
|
---|
| 157 | useElement.AppendChild(refEl);
|
---|
| 158 |
|
---|
| 159 | this.RenderElement(svgElement);
|
---|
| 160 |
|
---|
| 161 | useElement.RemoveChild(refEl);
|
---|
| 162 | useElement.RestoreReferencedElement(refEl);
|
---|
| 163 | refElParent.AppendChild(refEl);
|
---|
| 164 | useElement.OwnerDocument.Static = false;
|
---|
| 165 | }
|
---|
| 166 |
|
---|
| 167 | private void RenderElementChildren(ISvgElement svgElement)
|
---|
| 168 | {
|
---|
| 169 | foreach (XmlNode node in svgElement.ChildNodes)
|
---|
| 170 | {
|
---|
| 171 | if (node.NodeType != XmlNodeType.Element)
|
---|
| 172 | {
|
---|
| 173 | continue;
|
---|
| 174 | }
|
---|
| 175 |
|
---|
| 176 | SvgElement element = node as SvgElement;
|
---|
| 177 | if (element != null)
|
---|
| 178 | {
|
---|
| 179 | this.Render(element);
|
---|
| 180 | }
|
---|
| 181 | }
|
---|
| 182 | }
|
---|
| 183 |
|
---|
| 184 | private void RenderSwitchChildren(ISvgElement svgElement)
|
---|
| 185 | {
|
---|
| 186 | // search through all child elements and find one that passes all tests
|
---|
| 187 | foreach (XmlNode node in svgElement.ChildNodes)
|
---|
| 188 | {
|
---|
| 189 | if (node.NodeType != XmlNodeType.Element)
|
---|
| 190 | {
|
---|
| 191 | continue;
|
---|
| 192 | }
|
---|
| 193 |
|
---|
| 194 | SvgElement element = node as SvgElement;
|
---|
| 195 | ISvgTests testsElement = node as ISvgTests;
|
---|
| 196 | if (element != null && testsElement != null && PassesSwitchAllTest(testsElement))
|
---|
| 197 | {
|
---|
| 198 | this.Render(element);
|
---|
| 199 |
|
---|
| 200 | // make sure we only render the first element that passes
|
---|
| 201 | break;
|
---|
| 202 | }
|
---|
| 203 | }
|
---|
| 204 | }
|
---|
| 205 |
|
---|
| 206 | private bool PassesSwitchAllTest(ISvgTests element)
|
---|
| 207 | {
|
---|
| 208 | SvgDocument ownerDocument = ((SvgElement)element).OwnerDocument;
|
---|
| 209 |
|
---|
| 210 | bool requiredFeatures = true;
|
---|
| 211 | if (element.RequiredFeatures.NumberOfItems > 0)
|
---|
| 212 | {
|
---|
| 213 | foreach (string req in element.RequiredFeatures)
|
---|
| 214 | {
|
---|
| 215 | if (!ownerDocument.Supports(req, String.Empty))
|
---|
| 216 | {
|
---|
| 217 | requiredFeatures = false;
|
---|
| 218 | break;
|
---|
| 219 | }
|
---|
| 220 | }
|
---|
| 221 | }
|
---|
| 222 | if (!requiredFeatures)
|
---|
| 223 | {
|
---|
| 224 | return false;
|
---|
| 225 | }
|
---|
| 226 |
|
---|
| 227 | bool requiredExtensions = true;
|
---|
| 228 | if (element.RequiredExtensions.NumberOfItems > 0)
|
---|
| 229 | {
|
---|
| 230 | foreach (string req in element.RequiredExtensions)
|
---|
| 231 | {
|
---|
| 232 | if (!ownerDocument.Supports(req, String.Empty))
|
---|
| 233 | {
|
---|
| 234 | requiredExtensions = false;
|
---|
| 235 | break;
|
---|
| 236 | }
|
---|
| 237 | }
|
---|
| 238 | }
|
---|
| 239 | if (!requiredExtensions)
|
---|
| 240 | {
|
---|
| 241 | return false;
|
---|
| 242 | }
|
---|
| 243 |
|
---|
| 244 | bool systemLanguage = true;
|
---|
| 245 | if (element.SystemLanguage.NumberOfItems > 0)
|
---|
| 246 | {
|
---|
| 247 | systemLanguage = false;
|
---|
| 248 | // TODO: or if one of the languages indicated by user preferences exactly
|
---|
| 249 | // equals a prefix of one of the languages given in the value of this
|
---|
| 250 | // parameter such that the first tag character following the prefix is "-".
|
---|
| 251 |
|
---|
| 252 | foreach (string req in element.SystemLanguage)
|
---|
| 253 | {
|
---|
| 254 | if (String.Equals(req, _currentLang, StringComparison.OrdinalIgnoreCase))
|
---|
| 255 | {
|
---|
| 256 | systemLanguage = true;
|
---|
| 257 | }
|
---|
| 258 | }
|
---|
| 259 | }
|
---|
| 260 |
|
---|
| 261 | return systemLanguage;
|
---|
| 262 | }
|
---|
| 263 |
|
---|
| 264 | #endregion
|
---|
| 265 | }
|
---|
| 266 | }
|
---|