[12762] | 1 | using System;
|
---|
| 2 | using System.Xml;
|
---|
| 3 | using System.Linq;
|
---|
| 4 | using System.Text;
|
---|
| 5 | using System.Diagnostics;
|
---|
| 6 | using System.Collections.Generic;
|
---|
| 7 |
|
---|
| 8 | using System.Windows;
|
---|
| 9 | using System.Windows.Media;
|
---|
| 10 |
|
---|
| 11 | using SharpVectors.Dom.Svg;
|
---|
| 12 |
|
---|
| 13 | namespace SharpVectors.Renderers.Wpf
|
---|
| 14 | {
|
---|
| 15 | public enum SvgMarkerPosition
|
---|
| 16 | {
|
---|
| 17 | Start,
|
---|
| 18 | Mid,
|
---|
| 19 | End
|
---|
| 20 | }
|
---|
| 21 |
|
---|
| 22 | public sealed class WpfMarkerRendering : WpfRendering
|
---|
| 23 | {
|
---|
| 24 | #region Private Fields
|
---|
| 25 |
|
---|
| 26 | private Matrix _matrix;
|
---|
| 27 | private DrawingGroup _drawGroup;
|
---|
| 28 |
|
---|
| 29 | #endregion
|
---|
| 30 |
|
---|
| 31 | #region Constructors and Destructor
|
---|
| 32 |
|
---|
| 33 | public WpfMarkerRendering(SvgElement element)
|
---|
| 34 | : base(element)
|
---|
| 35 | {
|
---|
| 36 | }
|
---|
| 37 |
|
---|
| 38 | #endregion
|
---|
| 39 |
|
---|
| 40 | #region Public Methods
|
---|
| 41 |
|
---|
| 42 | // disable default rendering
|
---|
| 43 | public override void BeforeRender(WpfDrawingRenderer renderer)
|
---|
| 44 | {
|
---|
| 45 | base.BeforeRender(renderer);
|
---|
| 46 |
|
---|
| 47 | _matrix = Matrix.Identity;
|
---|
| 48 |
|
---|
| 49 | WpfDrawingContext context = renderer.Context;
|
---|
| 50 | _drawGroup = new DrawingGroup();
|
---|
| 51 |
|
---|
| 52 | //string elementId = this.GetElementName();
|
---|
| 53 | //if (!String.IsNullOrEmpty(elementId))
|
---|
| 54 | //{
|
---|
| 55 | // _drawGroup.SetValue(FrameworkElement.NameProperty, elementId);
|
---|
| 56 | //}
|
---|
| 57 |
|
---|
| 58 | DrawingGroup currentGroup = context.Peek();
|
---|
| 59 |
|
---|
| 60 | if (currentGroup == null)
|
---|
| 61 | {
|
---|
| 62 | throw new InvalidOperationException("An existing group is expected.");
|
---|
| 63 | }
|
---|
| 64 |
|
---|
| 65 | currentGroup.Children.Add(_drawGroup);
|
---|
| 66 | context.Push(_drawGroup);
|
---|
| 67 | }
|
---|
| 68 |
|
---|
| 69 | public override void Render(WpfDrawingRenderer renderer)
|
---|
| 70 | {
|
---|
| 71 | if (_drawGroup != null)
|
---|
| 72 | {
|
---|
| 73 | if (!_matrix.IsIdentity)
|
---|
| 74 | {
|
---|
| 75 | _drawGroup.Transform = new MatrixTransform(_matrix);
|
---|
| 76 | }
|
---|
| 77 |
|
---|
| 78 | Geometry clipGeom = this.ClipGeometry;
|
---|
| 79 | if (clipGeom != null)
|
---|
| 80 | {
|
---|
| 81 | _drawGroup.ClipGeometry = clipGeom;
|
---|
| 82 | }
|
---|
| 83 |
|
---|
| 84 | //Transform transform = this.Transform;
|
---|
| 85 | //if (transform != null)
|
---|
| 86 | //{
|
---|
| 87 | // _drawGroup.Transform = transform;
|
---|
| 88 | //}
|
---|
| 89 | }
|
---|
| 90 |
|
---|
| 91 | base.Render(renderer);
|
---|
| 92 | }
|
---|
| 93 |
|
---|
| 94 | public override void AfterRender(WpfDrawingRenderer renderer)
|
---|
| 95 | {
|
---|
| 96 | Debug.Assert(_drawGroup != null);
|
---|
| 97 |
|
---|
| 98 | WpfDrawingContext context = renderer.Context;
|
---|
| 99 |
|
---|
| 100 | DrawingGroup currentGroup = context.Peek();
|
---|
| 101 |
|
---|
| 102 | if (currentGroup == null || currentGroup != _drawGroup)
|
---|
| 103 | {
|
---|
| 104 | throw new InvalidOperationException("An existing group is expected.");
|
---|
| 105 | }
|
---|
| 106 |
|
---|
| 107 | context.Pop();
|
---|
| 108 |
|
---|
| 109 | base.AfterRender(renderer);
|
---|
| 110 | }
|
---|
| 111 |
|
---|
| 112 | public static Matrix GetTransformMatrix(SvgElement element)
|
---|
| 113 | {
|
---|
| 114 | ISvgTransformable transElm = element as ISvgTransformable;
|
---|
| 115 | if (transElm == null)
|
---|
| 116 | return Matrix.Identity;
|
---|
| 117 |
|
---|
| 118 | SvgTransformList svgTList = (SvgTransformList)transElm.Transform.AnimVal;
|
---|
| 119 | //SvgTransform svgTransform = (SvgTransform)svgTList.Consolidate();
|
---|
| 120 | SvgMatrix svgMatrix = ((SvgTransformList)transElm.Transform.AnimVal).TotalMatrix;
|
---|
| 121 |
|
---|
| 122 | return new Matrix(svgMatrix.A, svgMatrix.B, svgMatrix.C,
|
---|
| 123 | svgMatrix.D, svgMatrix.E, svgMatrix.F);
|
---|
| 124 | }
|
---|
| 125 |
|
---|
| 126 | public void RenderMarker0(WpfDrawingRenderer renderer, WpfDrawingContext gr,
|
---|
| 127 | SvgMarkerPosition markerPos, SvgStyleableElement refElement)
|
---|
| 128 | {
|
---|
| 129 | //PathGeometry g;
|
---|
| 130 | //g.GetPointAtFractionLength(
|
---|
| 131 |
|
---|
| 132 | ISharpMarkerHost markerHostElm = (ISharpMarkerHost)refElement;
|
---|
| 133 | SvgMarkerElement markerElm = (SvgMarkerElement)_svgElement;
|
---|
| 134 |
|
---|
| 135 | SvgPointF[] vertexPositions = markerHostElm.MarkerPositions;
|
---|
| 136 | int start;
|
---|
| 137 | int len;
|
---|
| 138 |
|
---|
| 139 | // Choose which part of the position array to use
|
---|
| 140 | switch (markerPos)
|
---|
| 141 | {
|
---|
| 142 | case SvgMarkerPosition.Start:
|
---|
| 143 | start = 0;
|
---|
| 144 | len = 1;
|
---|
| 145 | break;
|
---|
| 146 | case SvgMarkerPosition.Mid:
|
---|
| 147 | start = 1;
|
---|
| 148 | len = vertexPositions.Length - 2;
|
---|
| 149 | break;
|
---|
| 150 | default:
|
---|
| 151 | // == MarkerPosition.End
|
---|
| 152 | start = vertexPositions.Length - 1;
|
---|
| 153 | len = 1;
|
---|
| 154 | break;
|
---|
| 155 | }
|
---|
| 156 |
|
---|
| 157 | for (int i = start; i < start + len; i++)
|
---|
| 158 | {
|
---|
| 159 | SvgPointF point = vertexPositions[i];
|
---|
| 160 |
|
---|
| 161 | Matrix m = GetTransformMatrix(_svgElement);
|
---|
| 162 |
|
---|
| 163 | //GraphicsContainer gc = gr.BeginContainer();
|
---|
| 164 |
|
---|
| 165 | this.BeforeRender(renderer);
|
---|
| 166 |
|
---|
| 167 | //gr.TranslateTransform(point.X, point.Y);
|
---|
| 168 |
|
---|
| 169 | //PAUL:
|
---|
| 170 | //m.Translate(point.X, point.Y);
|
---|
| 171 |
|
---|
| 172 | if (markerElm.OrientType.AnimVal.Equals(SvgMarkerOrient.Angle))
|
---|
| 173 | {
|
---|
| 174 | m.Rotate(markerElm.OrientAngle.AnimVal.Value);
|
---|
| 175 | //gr.RotateTransform((double)markerElm.OrientAngle.AnimVal.Value);
|
---|
| 176 | }
|
---|
| 177 | else
|
---|
| 178 | {
|
---|
| 179 | double angle;
|
---|
| 180 |
|
---|
| 181 | switch (markerPos)
|
---|
| 182 | {
|
---|
| 183 | case SvgMarkerPosition.Start:
|
---|
| 184 | angle = markerHostElm.GetStartAngle(i + 1);
|
---|
| 185 | break;
|
---|
| 186 | case SvgMarkerPosition.Mid:
|
---|
| 187 | //angle = (markerHostElm.GetEndAngle(i) + markerHostElm.GetStartAngle(i + 1)) / 2;
|
---|
| 188 | angle = SvgNumber.CalcAngleBisection(markerHostElm.GetEndAngle(i), markerHostElm.GetStartAngle(i + 1));
|
---|
| 189 | break;
|
---|
| 190 | default:
|
---|
| 191 | angle = markerHostElm.GetEndAngle(i);
|
---|
| 192 | break;
|
---|
| 193 | }
|
---|
| 194 | //gr.RotateTransform(angle);
|
---|
| 195 | m.Rotate(angle);
|
---|
| 196 | }
|
---|
| 197 |
|
---|
| 198 | if (markerElm.MarkerUnits.AnimVal.Equals(SvgMarkerUnit.StrokeWidth))
|
---|
| 199 | {
|
---|
| 200 | string propValue = refElement.GetPropertyValue("stroke-width");
|
---|
| 201 | if (propValue.Length == 0)
|
---|
| 202 | propValue = "1";
|
---|
| 203 |
|
---|
| 204 | SvgLength strokeWidthLength = new SvgLength("stroke-width", propValue, refElement, SvgLengthDirection.Viewport);
|
---|
| 205 | double strokeWidth = strokeWidthLength.Value;
|
---|
| 206 | //gr.ScaleTransform(strokeWidth, strokeWidth);
|
---|
| 207 | m.Scale(strokeWidth, strokeWidth);
|
---|
| 208 | }
|
---|
| 209 |
|
---|
| 210 | SvgPreserveAspectRatio spar = (SvgPreserveAspectRatio)markerElm.PreserveAspectRatio.AnimVal;
|
---|
| 211 | double[] translateAndScale = spar.FitToViewBox(
|
---|
| 212 | (SvgRect)markerElm.ViewBox.AnimVal, new SvgRect(0, 0,
|
---|
| 213 | markerElm.MarkerWidth.AnimVal.Value, markerElm.MarkerHeight.AnimVal.Value));
|
---|
| 214 |
|
---|
| 215 |
|
---|
| 216 | //PAUL:
|
---|
| 217 | //m.Translate(-(double)markerElm.RefX.AnimVal.Value * translateAndScale[2], -(double)markerElm.RefY.AnimVal.Value * translateAndScale[3]);
|
---|
| 218 |
|
---|
| 219 | //PAUL:
|
---|
| 220 | m.Scale(translateAndScale[2], translateAndScale[3]);
|
---|
| 221 | m.Translate(point.X, point.Y);
|
---|
| 222 |
|
---|
| 223 | //Matrix oldTransform = TransformMatrix;
|
---|
| 224 | //TransformMatrix = m;
|
---|
| 225 | //try
|
---|
| 226 | //{
|
---|
| 227 | //newTransform.Append(m);
|
---|
| 228 | //TransformGroup tg = new TransformGroup();
|
---|
| 229 |
|
---|
| 230 | //renderer.Canvas.re
|
---|
| 231 |
|
---|
| 232 | //gr.TranslateTransform(
|
---|
| 233 | // -(double)markerElm.RefX.AnimVal.Value * translateAndScale[2],
|
---|
| 234 | // -(double)markerElm.RefY.AnimVal.Value * translateAndScale[3]
|
---|
| 235 | // );
|
---|
| 236 |
|
---|
| 237 | //gr.ScaleTransform(translateAndScale[2], translateAndScale[3]);
|
---|
| 238 |
|
---|
| 239 | renderer.RenderChildren(markerElm);
|
---|
| 240 | // markerElm.RenderChildren(renderer);
|
---|
| 241 | //}
|
---|
| 242 | //finally
|
---|
| 243 | //{
|
---|
| 244 | // TransformMatrix = oldTransform;
|
---|
| 245 | //}
|
---|
| 246 | // //gr.EndContainer(gc);
|
---|
| 247 |
|
---|
| 248 | _matrix = m;
|
---|
| 249 | this.Render(renderer);
|
---|
| 250 |
|
---|
| 251 | //gr.EndContainer(gc);
|
---|
| 252 |
|
---|
| 253 | this.AfterRender(renderer);
|
---|
| 254 | }
|
---|
| 255 | }
|
---|
| 256 |
|
---|
| 257 | public void RenderMarker(WpfDrawingRenderer renderer, WpfDrawingContext gr,
|
---|
| 258 | SvgMarkerPosition markerPos, SvgStyleableElement refElement)
|
---|
| 259 | {
|
---|
| 260 | ISharpMarkerHost markerHostElm = (ISharpMarkerHost)refElement;
|
---|
| 261 | SvgMarkerElement markerElm = (SvgMarkerElement)_svgElement;
|
---|
| 262 |
|
---|
| 263 | SvgPointF[] vertexPositions = markerHostElm.MarkerPositions;
|
---|
| 264 | int start;
|
---|
| 265 | int len;
|
---|
| 266 |
|
---|
| 267 | // Choose which part of the position array to use
|
---|
| 268 | switch (markerPos)
|
---|
| 269 | {
|
---|
| 270 | case SvgMarkerPosition.Start:
|
---|
| 271 | start = 0;
|
---|
| 272 | len = 1;
|
---|
| 273 | break;
|
---|
| 274 | case SvgMarkerPosition.Mid:
|
---|
| 275 | start = 1;
|
---|
| 276 | len = vertexPositions.Length - 2;
|
---|
| 277 | break;
|
---|
| 278 | default:
|
---|
| 279 | // == MarkerPosition.End
|
---|
| 280 | start = vertexPositions.Length - 1;
|
---|
| 281 | len = 1;
|
---|
| 282 | break;
|
---|
| 283 | }
|
---|
| 284 |
|
---|
| 285 | for (int i = start; i < start + len; i++)
|
---|
| 286 | {
|
---|
| 287 | SvgPointF point = vertexPositions[i];
|
---|
| 288 |
|
---|
| 289 | //GdiGraphicsContainer gc = gr.BeginContainer();
|
---|
| 290 |
|
---|
| 291 | this.BeforeRender(renderer);
|
---|
| 292 |
|
---|
| 293 | //Matrix matrix = Matrix.Identity;
|
---|
| 294 |
|
---|
| 295 | Matrix matrix = GetTransformMatrix(_svgElement);
|
---|
| 296 |
|
---|
| 297 | if (markerElm.OrientType.AnimVal.Equals(SvgMarkerOrient.Angle))
|
---|
| 298 | {
|
---|
| 299 | matrix.Rotate(markerElm.OrientAngle.AnimVal.Value);
|
---|
| 300 | }
|
---|
| 301 | else
|
---|
| 302 | {
|
---|
| 303 | double angle = 0;
|
---|
| 304 |
|
---|
| 305 | switch (markerPos)
|
---|
| 306 | {
|
---|
| 307 | case SvgMarkerPosition.Start:
|
---|
| 308 | angle = markerHostElm.GetStartAngle(i + 1);
|
---|
| 309 | break;
|
---|
| 310 | case SvgMarkerPosition.Mid:
|
---|
| 311 | //angle = (markerHostElm.GetEndAngle(i) + markerHostElm.GetStartAngle(i + 1)) / 2;
|
---|
| 312 | angle = SvgNumber.CalcAngleBisection(markerHostElm.GetEndAngle(i), markerHostElm.GetStartAngle(i + 1));
|
---|
| 313 | break;
|
---|
| 314 | default:
|
---|
| 315 | angle = markerHostElm.GetEndAngle(i);
|
---|
| 316 | break;
|
---|
| 317 | }
|
---|
| 318 | matrix.Rotate(angle);
|
---|
| 319 | }
|
---|
| 320 |
|
---|
| 321 | if (markerElm.MarkerUnits.AnimVal.Equals(SvgMarkerUnit.StrokeWidth))
|
---|
| 322 | {
|
---|
| 323 | SvgLength strokeWidthLength = new SvgLength(refElement,
|
---|
| 324 | "stroke-width", SvgLengthSource.Css, SvgLengthDirection.Viewport, "1");
|
---|
| 325 | double strokeWidth = strokeWidthLength.Value;
|
---|
| 326 | matrix.Scale(strokeWidth, strokeWidth);
|
---|
| 327 | }
|
---|
| 328 |
|
---|
| 329 | SvgPreserveAspectRatio spar =
|
---|
| 330 | (SvgPreserveAspectRatio)markerElm.PreserveAspectRatio.AnimVal;
|
---|
| 331 | double[] translateAndScale = spar.FitToViewBox((SvgRect)markerElm.ViewBox.AnimVal,
|
---|
| 332 | new SvgRect(0, 0, markerElm.MarkerWidth.AnimVal.Value,
|
---|
| 333 | markerElm.MarkerHeight.AnimVal.Value));
|
---|
| 334 |
|
---|
| 335 |
|
---|
| 336 | matrix.Translate(-markerElm.RefX.AnimVal.Value * translateAndScale[2],
|
---|
| 337 | -markerElm.RefY.AnimVal.Value * translateAndScale[3]);
|
---|
| 338 |
|
---|
| 339 | matrix.Scale(translateAndScale[2], translateAndScale[3]);
|
---|
| 340 |
|
---|
| 341 | matrix.Translate(point.X, point.Y);
|
---|
| 342 |
|
---|
| 343 | _matrix = matrix;
|
---|
| 344 | this.Render(renderer);
|
---|
| 345 |
|
---|
| 346 | //Clip(gr);
|
---|
| 347 |
|
---|
| 348 | renderer.RenderChildren(markerElm);
|
---|
| 349 |
|
---|
| 350 | //gr.EndContainer(gc);
|
---|
| 351 |
|
---|
| 352 | this.AfterRender(renderer);
|
---|
| 353 | }
|
---|
| 354 | }
|
---|
| 355 |
|
---|
| 356 | public void RenderMarkerEx0(WpfDrawingRenderer renderer, WpfDrawingContext gr,
|
---|
| 357 | SvgMarkerPosition markerPos, SvgStyleableElement refElement)
|
---|
| 358 | {
|
---|
| 359 | //ISharpMarkerHost markerHostElm = (ISharpMarkerHost)refElement;
|
---|
| 360 | //SvgMarkerElement markerElm = (SvgMarkerElement)element;
|
---|
| 361 |
|
---|
| 362 | //SvgPointF[] vertexPositions = markerHostElm.MarkerPositions;
|
---|
| 363 | //int start;
|
---|
| 364 | //int len;
|
---|
| 365 |
|
---|
| 366 | //// Choose which part of the position array to use
|
---|
| 367 | //switch (markerPos)
|
---|
| 368 | //{
|
---|
| 369 | // case SvgMarkerPosition.Start:
|
---|
| 370 | // start = 0;
|
---|
| 371 | // len = 1;
|
---|
| 372 | // break;
|
---|
| 373 | // case SvgMarkerPosition.Mid:
|
---|
| 374 | // start = 1;
|
---|
| 375 | // len = vertexPositions.Length - 2;
|
---|
| 376 | // break;
|
---|
| 377 | // default:
|
---|
| 378 | // // == MarkerPosition.End
|
---|
| 379 | // start = vertexPositions.Length - 1;
|
---|
| 380 | // len = 1;
|
---|
| 381 | // break;
|
---|
| 382 | //}
|
---|
| 383 |
|
---|
| 384 | //for (int i = start; i < start + len; i++)
|
---|
| 385 | //{
|
---|
| 386 | // SvgPointF point = vertexPositions[i];
|
---|
| 387 |
|
---|
| 388 | // GdiGraphicsContainer gc = gr.BeginContainer();
|
---|
| 389 |
|
---|
| 390 | // gr.TranslateTransform(point.X, point.Y);
|
---|
| 391 |
|
---|
| 392 | // if (markerElm.OrientType.AnimVal.Equals(SvgMarkerOrient.Angle))
|
---|
| 393 | // {
|
---|
| 394 | // gr.RotateTransform((float)markerElm.OrientAngle.AnimVal.Value);
|
---|
| 395 | // }
|
---|
| 396 | // else
|
---|
| 397 | // {
|
---|
| 398 | // float angle;
|
---|
| 399 |
|
---|
| 400 | // switch (markerPos)
|
---|
| 401 | // {
|
---|
| 402 | // case SvgMarkerPosition.Start:
|
---|
| 403 | // angle = markerHostElm.GetStartAngle(i + 1);
|
---|
| 404 | // break;
|
---|
| 405 | // case SvgMarkerPosition.Mid:
|
---|
| 406 | // //angle = (markerHostElm.GetEndAngle(i) + markerHostElm.GetStartAngle(i + 1)) / 2;
|
---|
| 407 | // angle = SvgNumber.CalcAngleBisection(markerHostElm.GetEndAngle(i), markerHostElm.GetStartAngle(i + 1));
|
---|
| 408 | // break;
|
---|
| 409 | // default:
|
---|
| 410 | // angle = markerHostElm.GetEndAngle(i);
|
---|
| 411 | // break;
|
---|
| 412 | // }
|
---|
| 413 | // gr.RotateTransform(angle);
|
---|
| 414 | // }
|
---|
| 415 |
|
---|
| 416 | // if (markerElm.MarkerUnits.AnimVal.Equals(SvgMarkerUnit.StrokeWidth))
|
---|
| 417 | // {
|
---|
| 418 | // SvgLength strokeWidthLength = new SvgLength(refElement,
|
---|
| 419 | // "stroke-width", SvgLengthSource.Css, SvgLengthDirection.Viewport, "1");
|
---|
| 420 | // float strokeWidth = (float)strokeWidthLength.Value;
|
---|
| 421 | // gr.ScaleTransform(strokeWidth, strokeWidth);
|
---|
| 422 | // }
|
---|
| 423 |
|
---|
| 424 | // SvgPreserveAspectRatio spar =
|
---|
| 425 | // (SvgPreserveAspectRatio)markerElm.PreserveAspectRatio.AnimVal;
|
---|
| 426 | // float[] translateAndScale = spar.FitToViewBox((SvgRect)markerElm.ViewBox.AnimVal,
|
---|
| 427 | // new SvgRect(0, 0, markerElm.MarkerWidth.AnimVal.Value,
|
---|
| 428 | // markerElm.MarkerHeight.AnimVal.Value));
|
---|
| 429 |
|
---|
| 430 |
|
---|
| 431 | // gr.TranslateTransform(-(float)markerElm.RefX.AnimVal.Value * translateAndScale[2],
|
---|
| 432 | // -(float)markerElm.RefY.AnimVal.Value * translateAndScale[3]);
|
---|
| 433 |
|
---|
| 434 | // gr.ScaleTransform(translateAndScale[2], translateAndScale[3]);
|
---|
| 435 |
|
---|
| 436 | // Clip(gr);
|
---|
| 437 |
|
---|
| 438 | // renderer.RenderChildren(markerElm);
|
---|
| 439 |
|
---|
| 440 | // gr.EndContainer(gc);
|
---|
| 441 | //}
|
---|
| 442 | }
|
---|
| 443 |
|
---|
| 444 | #endregion
|
---|
| 445 | }
|
---|
| 446 | }
|
---|