Example #1
0
        static void BuildTextSpan(GeometryGroup gp, TextStyle textStyle, TextShape.TSpan.Element tspan, ref double x, ref double y)
        {
            foreach (TextShape.TSpan.Element child in tspan.Children)
            {
                if (child.ElementType == TextShape.TSpan.Element.eElementType.Text)
                {
                    string txt        = child.Text;
                    double totalwidth = 0;
                    double baseline   = y;

                    if (child.TextStyle.BaseLineShift == "sub")
                    {
                        baseline += child.TextStyle.FontSize * 0.5;
                    }                                                                       /* * cap height ? fontSize*/
                    ;
                    if (child.TextStyle.BaseLineShift == "super")
                    {
                        baseline -= tspan.TextStyle.FontSize + (child.TextStyle.FontSize * 0.25) /*font.CapsHeight * fontSize*/;
                    }

                    Geometry gm = BuildGlyphRun(child.TextStyle, txt, x, baseline, ref totalwidth);
                    TextRender.SetElement(gm, child);
                    gp.Children.Add(gm);
                    x += totalwidth;
                    continue;
                }
                if (child.ElementType == TextShape.TSpan.Element.eElementType.Tag)
                {
                    BuildTextSpan(gp, textStyle, child, ref x, ref y);
                }
            }
        }
Example #2
0
        DrawingGroup LoadGroup(IList <Shape> elements)
        {
            List <ControlLine> debugPoints = new List <ControlLine>();
            DrawingGroup       grp         = new DrawingGroup();

            foreach (ClipArtViewer.Shape shape in elements)
            {
                if (shape is ClipArtViewer.UseShape)
                {
                    ClipArtViewer.UseShape useshape = shape as ClipArtViewer.UseShape;
                    ClipArtViewer.Group    group    = SVG.GetShape(useshape.hRef) as ClipArtViewer.Group;
                    if (group != null)
                    {
                        Shape oldparent = group.Parent;
                        group.Parent = useshape;                         // this to get proper style propagated
                        DrawingGroup subgroup = LoadGroup(group.Elements);
                        subgroup.Transform = new TranslateTransform(useshape.X, useshape.Y);
                        grp.Children.Add(subgroup);
                        group.Parent = oldparent;
                    }
                    continue;
                }
                if (shape is ClipArtViewer.Group)
                {
                    DrawingGroup subgroup = LoadGroup((shape as ClipArtViewer.Group).Elements);
                    if (shape.Transform != null)
                    {
                        subgroup.Transform = shape.Transform;
                    }
                    grp.Children.Add(subgroup);
                    continue;
                }
                if (shape is ClipArtViewer.RectangleShape)
                {
                    ClipArtViewer.RectangleShape r    = shape as ClipArtViewer.RectangleShape;
                    RectangleGeometry            rect = new RectangleGeometry(new Rect(r.X, r.Y, r.Width, r.Height));
                    rect.RadiusX = r.RX;
                    rect.RadiusY = r.RY;
                    if (rect.RadiusX == 0 && rect.RadiusY > 0)
                    {
                        rect.RadiusX = rect.RadiusY;
                    }
                    grp.Children.Add(NewDrawingItem(shape, rect));
                }
                if (shape is ClipArtViewer.LineShape)
                {
                    ClipArtViewer.LineShape r    = shape as ClipArtViewer.LineShape;
                    LineGeometry            line = new LineGeometry(r.P1, r.P2);
                    grp.Children.Add(NewDrawingItem(shape, line));
                }
                if (shape is ClipArtViewer.PolylineShape)
                {
                    ClipArtViewer.PolylineShape r = shape as ClipArtViewer.PolylineShape;
                    PathGeometry path             = new PathGeometry();
                    PathFigure   p = new PathFigure();
                    path.Figures.Add(p);
                    p.IsClosed   = false;
                    p.StartPoint = r.Points[0];
                    for (int index = 1; index < r.Points.Length; index++)
                    {
                        p.Segments.Add(new LineSegment(r.Points[index], true));
                    }
                    grp.Children.Add(NewDrawingItem(shape, path));
                }
                if (shape is ClipArtViewer.PolygonShape)
                {
                    ClipArtViewer.PolygonShape r = shape as ClipArtViewer.PolygonShape;
                    PathGeometry path            = new PathGeometry();
                    PathFigure   p = new PathFigure();
                    path.Figures.Add(p);
                    p.IsClosed   = true;
                    p.StartPoint = r.Points[0];
                    for (int index = 1; index < r.Points.Length; index++)
                    {
                        p.Segments.Add(new LineSegment(r.Points[index], true));
                    }
                    grp.Children.Add(NewDrawingItem(shape, path));
                }
                if (shape is ClipArtViewer.CircleShape)
                {
                    ClipArtViewer.CircleShape r = shape as ClipArtViewer.CircleShape;
                    EllipseGeometry           c = new EllipseGeometry(new Point(r.CX, r.CY), r.R, r.R);
                    grp.Children.Add(NewDrawingItem(shape, c));
                }
                if (shape is ClipArtViewer.EllipseShape)
                {
                    ClipArtViewer.EllipseShape r = shape as ClipArtViewer.EllipseShape;
                    EllipseGeometry            c = new EllipseGeometry(new Point(r.CX, r.CY), r.RX, r.RY);
                    grp.Children.Add(NewDrawingItem(shape, c));
                }
                if (shape is ClipArtViewer.ImageShape)
                {
                    ClipArtViewer.ImageShape image = shape as ClipArtViewer.ImageShape;
                    ImageDrawing             i     = new ImageDrawing(image.ImageSource, new Rect(image.X, image.Y, image.Width, image.Height));
                    grp.Children.Add(i);
                }
                if (shape is ClipArtViewer.TextShape)
                {
                    GeometryGroup gp = TextRender.BuildTextGeometry(shape as ClipArtViewer.TextShape);
                    if (gp != null)
                    {
                        foreach (Geometry gm in gp.Children)
                        {
                            TextShape.TSpan.Element tspan = TextRender.GetElement(gm);
                            if (tspan != null)
                            {
                                grp.Children.Add(NewDrawingItem(tspan, gm));
                            }
                            else
                            {
                                grp.Children.Add(NewDrawingItem(shape, gm));
                            }
                        }
                    }
                }
                if (shape is ClipArtViewer.PathShape)
                {
                    ClipArtViewer.PathShape r = shape as ClipArtViewer.PathShape;
                    PathFigure p         = null;
                    Point      lastPoint = new Point(0, 0);

                    ClipArtViewer.PathShape.CurveTo lastc = null;
                    Point lastcirPoint = new Point(0, 0);

                    PathGeometry path = new PathGeometry();
                    foreach (ClipArtViewer.PathShape.PathElement element in r.Elements)
                    {
                        bool isRelative = element.IsRelative;
                        if (element is ClipArtViewer.PathShape.MoveTo)
                        {
                            p          = new PathFigure();
                            p.IsClosed = r.ClosePath;
                            if (isRelative)
                            {
                                p.StartPoint = lastPoint + (Vector)((ClipArtViewer.PathShape.MoveTo)element).Point;
                            }
                            else
                            {
                                p.StartPoint = ((ClipArtViewer.PathShape.MoveTo)element).Point;
                            }
                            lastPoint = p.StartPoint;
                            path.Figures.Add(p);
                            continue;
                        }
                        if (element is ClipArtViewer.PathShape.LineTo)
                        {
                            ClipArtViewer.PathShape.LineTo lineto = element as ClipArtViewer.PathShape.LineTo;
                            foreach (Point point in lineto.Points)
                            {
                                if (isRelative)
                                {
                                    Point newpoint = lastPoint + (Vector)point;
                                    lastPoint = newpoint;
                                    p.Segments.Add(new LineSegment(newpoint, true));
                                }
                                else
                                {
                                    if (lineto.PositionType == PathShape.LineTo.eType.Point)
                                    {
                                        lastPoint = point;
                                    }
                                    if (lineto.PositionType == PathShape.LineTo.eType.Horizontal)
                                    {
                                        lastPoint = new Point(point.X, lastPoint.Y);
                                    }
                                    if (lineto.PositionType == PathShape.LineTo.eType.Vertical)
                                    {
                                        lastPoint = new Point(lastPoint.X, point.Y);
                                    }
                                    p.Segments.Add(new LineSegment(lastPoint, true));
                                }
                            }
                            continue;
                        }
                        if (element is ClipArtViewer.PathShape.CurveTo)
                        {
                            ClipArtViewer.PathShape.CurveTo c = element as ClipArtViewer.PathShape.CurveTo;
                            Point         startPoint          = lastPoint;
                            BezierSegment s = new BezierSegment();
                            if (isRelative)
                            {
                                s.Point1 = lastPoint + (Vector)c.CtrlPoint1;

                                if (c.Command == 's')
                                {
                                    // first control point is a mirrored point of last end control point
                                    //s.Point1 = lastPoint + new Vector(lastc.Point.X - dx, lastc.Point.Y - dy);
                                    //s.Point1 = new Point(lastctrlpoint.X+2, lastctrlpoint.Y+2);

                                    double dx = lastc.CtrlPoint2.X - lastc.Point.X;
                                    double dy = lastc.CtrlPoint2.Y - lastc.Point.Y;
                                    s.Point1 = new Point(lastcirPoint.X - dx, lastcirPoint.Y - dy);
                                    //s.Point1 = lastctrlpoint;
                                }

                                s.Point2 = lastPoint + (Vector)c.CtrlPoint2;
                                s.Point3 = lastPoint + (Vector)c.Point;
                            }
                            else
                            {
                                s.Point1 = c.CtrlPoint1;
                                s.Point2 = c.CtrlPoint2;
                                s.Point3 = c.Point;
                            }
                            lastPoint = s.Point3;
                            p.Segments.Add(s);

                            lastc        = c;
                            lastcirPoint = s.Point3;

                            //debugPoints.Add(new ControlLine(startPoint, s.Point1));
                            //debugPoints.Add(new ControlLine(s.Point3, s.Point2));
                            continue;
                        }
                        if (element is ClipArtViewer.PathShape.EllipticalArcTo)
                        {
                            ClipArtViewer.PathShape.EllipticalArcTo c = element as ClipArtViewer.PathShape.EllipticalArcTo;
                            ArcSegment s = new ArcSegment();
                            if (isRelative)
                            {
                                s.Point = lastPoint + new Vector(c.X, c.Y);
                            }
                            else
                            {
                                s.Point = new Point(c.X, c.Y);
                            }

                            s.Size           = new Size(c.RX, c.RY);
                            s.RotationAngle  = c.AxisRotation;
                            s.SweepDirection = SweepDirection.Counterclockwise;
                            if (c.Clockwise)
                            {
                                s.SweepDirection = SweepDirection.Clockwise;
                            }
                            s.IsLargeArc = c.LargeArc;
                            lastPoint    = s.Point;
                            p.Segments.Add(s);
                            continue;
                        }
                    }

                    /*
                     *                  if (r.Transform != null)
                     *                          path.Transform = r.Transform;
                     */
                    grp.Children.Add(NewDrawingItem(shape, path));
                    //}
                }
            }


            if (debugPoints != null)
            {
                foreach (ControlLine line in debugPoints)
                {
                    grp.Children.Add(line.Draw());
                }
            }
            return(grp);
        }