示例#1
0
            public void LineTo(LineTo lineTo)
            {
                float x = (float)lineTo.X;
                float y = (float)lineTo.Y;

                _path.AddLine(_currentPoint.X, _currentPoint.Y, x, y);
                _currentPoint.X = x;
                _currentPoint.Y = y;
            }
示例#2
0
        public void DrawPath(System.Windows.Point start, IList <IPathCommand> commands, double thickness, bool fill = false)
        {
            m_pen.Width = (float)thickness;

            GraphicsPath path = new GraphicsPath();

            System.Windows.Point previous = start;
            foreach (var command in commands)
            {
                switch (command.Type)
                {
                case CommandType.MoveTo:
                {
                    path.StartFigure();
                    break;
                }

                case CommandType.LineTo:
                {
                    LineTo line = command as LineTo;
                    path.AddLine((float)previous.X, (float)previous.Y, (float)line.X + (float)start.X, (float)line.Y + (float)start.Y);
                    break;
                }

                case CommandType.CurveTo:
                {
                    CurveTo curveTo = command as CurveTo;

                    break;
                }

                case CommandType.EllipticalArcTo:
                {
                    EllipticalArcTo ellipticalArcTo = command as EllipticalArcTo;
                    //path.AddArc((float)previous.X, (float)previous.Y, (float)ellipticalArcTo.Size.Width, (float)ellipticalArcTo.Size.Height, (float)ellipticalArcTo.RotationAngle, (float)ellipticalArcTo.RotationAngle);
                    break;
                }

                case CommandType.QuadraticBeizerCurveTo:
                {
                    QuadraticBeizerCurveTo qbCurveTo = command as QuadraticBeizerCurveTo;
                    //path.AddBezier((float)previous.X, (float)previous.Y, (float)qbCurveTo.Control.X, (float)qbCurveTo.Control.Y, (float)qbCurveTo.Control.X, (float)qbCurveTo.Control.Y, (float)qbCurveTo.End.X, (float)qbCurveTo.End.Y);
                    break;
                }
                }

                previous = new System.Windows.Point(start.X + command.End.X, start.Y + command.End.Y);
            }

            if (fill)
            {
                m_graphics.FillPath(Brushes.Black, path);
            }
            m_graphics.DrawPath(m_pen, path);
        }
        private static void ListPath(Path path, int pgno)
        {
            IList <Segment> segments = path.Segments;

            Console.WriteLine("Path on page {0}:", pgno);
            Console.WriteLine("Transformation matrix: {0}", path.Matrix);
            foreach (Segment segment in segments)
            {
                if (segment is MoveTo)
                {
                    MoveTo moveto = segment as MoveTo;
                    Console.WriteLine("  MoveTo x={0}, y={1}", moveto.Point.H, moveto.Point.V);
                }
                else if (segment is LineTo)
                {
                    LineTo lineto = segment as LineTo;
                    Console.WriteLine("  LineTo x={0}, y={1}",
                                      lineto.Point.H, lineto.Point.V);
                }
                else if (segment is CurveTo)
                {
                    CurveTo curveto = segment as CurveTo;
                    Console.WriteLine("  CurveTo x1={0}, y1={1}, x2={2}, y2={3}, x3={4}, y3={5}",
                                      curveto.Point1.H, curveto.Point1.V,
                                      curveto.Point2.H, curveto.Point2.V,
                                      curveto.Point3.H, curveto.Point3.V);
                }
                else if (segment is CurveToV)
                {
                    CurveToV curveto = segment as CurveToV;
                    Console.WriteLine("  CurveToV x2={0}, y2={1}, x3={2}, y3={3}",
                                      curveto.Point2.H, curveto.Point2.V,
                                      curveto.Point3.H, curveto.Point3.V);
                }
                else if (segment is CurveToY)
                {
                    CurveToY curveto = segment as CurveToY;
                    Console.WriteLine("  CurveToV x1={0}, y1={1}, x3={2}, y3={3}",
                                      curveto.Point1.H, curveto.Point1.V,
                                      curveto.Point3.H, curveto.Point3.V);
                }
                else if (segment is RectSegment)
                {
                    RectSegment rect = segment as RectSegment;
                    Console.WriteLine("  Rectangle x={0}, y={1}, width={2}, height={3}",
                                      rect.Point.H, rect.Point.V,
                                      rect.Width, rect.Height);
                }
                else if (segment is ClosePath)
                {
                    Console.WriteLine("  ClosePath");
                }
            }
        }
示例#4
0
        public void TestLineTo()
        {
            var c        = new LineTo(10, 11);
            var offset   = new Point(1, 2);
            var previous = new Point(0, 0);

            // Test Shorthand()
            Assert.AreEqual("L 11,13", c.Shorthand(offset, previous));

            // Test Flip()
            Assert.AreEqual("L 11,-9", c.Flip(false).Shorthand(offset, previous));
            Assert.AreEqual("L -9,13", c.Flip(true).Shorthand(offset, previous));
        }
        public static void Run()
        {
            // ExStart:EditConnectorGeometry
            // The path to the documents directory.
            string dataDir = RunExamples.GetDataDir_GeometrySection();

            // Load source Visio diagram
            Diagram diagram = new Diagram(dataDir + "Drawing1.vsdx");
            // Set connector shape by page name and ID
            long  connectorId = 4;
            Shape connector   = diagram.Pages.GetPage("Page-1").Shapes.GetShape(connectorId);
            // Get connector geometry at index 0
            LineTo defaultLineTo = connector.Geoms[0].CoordinateCol.LineToCol[0];

            // Remove connector geometry from index 0
            connector.Geoms[0].CoordinateCol.LineToCol[0].Del = BOOL.True;

            // Initialize LineTo geometry object
            LineTo lineTo = new LineTo();

            // Set X value
            lineTo.X.Value = 0;
            // Set Y value
            lineTo.Y.Value = defaultLineTo.Y.Value / 2;
            // Add connector geometry
            connector.Geoms[0].CoordinateCol.Add(lineTo);

            // Initialize LineTo geometry object
            lineTo = new LineTo();
            // Set Y value
            lineTo.Y.Value = defaultLineTo.Y.Value / 2;
            // Set X value
            lineTo.X.Value = defaultLineTo.X.Value;
            // Add connector geometry
            connector.Geoms[0].CoordinateCol.Add(lineTo);

            // Initialize LineTo geometry object
            lineTo = new LineTo();
            // Set X value
            lineTo.X.Value = defaultLineTo.X.Value;
            // Set Y value
            lineTo.Y.Value = defaultLineTo.Y.Value;
            // Add connector geometry
            connector.Geoms[0].CoordinateCol.Add(lineTo);

            // Save diagram in VDX format
            diagram.Save(dataDir + "EditConnectorGeometry_Out.vsdx", SaveFileFormat.VSDX);
            // ExEnd:EditConnectorGeometry
        }
示例#6
0
        public static void Write(this IPathCommand command, System.IO.BinaryWriter writer)
        {
            switch (command.Type)
            {
            case CommandType.MoveTo:
            {
                MoveTo pCommand = command as MoveTo;
                writer.Write(pCommand.X);
                writer.Write(pCommand.Y);
            }
            break;

            case CommandType.LineTo:
            {
                LineTo pCommand = command as LineTo;
                writer.Write(pCommand.X);
                writer.Write(pCommand.Y);
            }
            break;

            case CommandType.CurveTo:
            {
                CurveTo pCommand = command as CurveTo;
                writer.Write(pCommand.ControlStart);
                writer.Write(pCommand.ControlEnd);
                writer.Write(pCommand.End);
            }
            break;

            case CommandType.SmoothCurveTo:
            {
                SmoothCurveTo pCommand = command as SmoothCurveTo;
                writer.Write(pCommand.ControlEnd);
                writer.Write(pCommand.End);
            }
            break;

            case CommandType.EllipticalArcTo:
            {
                EllipticalArcTo pCommand = command as EllipticalArcTo;
                writer.Write(pCommand.Size.Width);
                writer.Write(pCommand.Size.Height);
                writer.Write(pCommand.End);
                writer.Write(pCommand.RotationAngle);
                writer.Write(pCommand.IsLargeArc);
                writer.Write(pCommand.SweepDirection == SweepDirection.Clockwise);
            }
            break;

            case CommandType.QuadraticBeizerCurveTo:
            {
                QuadraticBeizerCurveTo pCommand = command as QuadraticBeizerCurveTo;
                writer.Write(pCommand.Control);
                writer.Write(pCommand.End);
            }
            break;

            case CommandType.SmoothQuadraticBeizerCurveTo:
            {
                SmoothQuadraticBeizerCurveTo pCommand = command as SmoothQuadraticBeizerCurveTo;
                writer.Write(pCommand.End);
            }
            break;

            case CommandType.ClosePath:
            {
                // Do nothing
            }
            break;
            }
        }
示例#7
0
        public static void Read(this IPathCommand command, System.IO.BinaryReader reader)
        {
            switch (command.Type)
            {
            case CommandType.MoveTo:
            {
                MoveTo pCommand = command as MoveTo;
                pCommand.X = reader.ReadDouble();
                pCommand.Y = reader.ReadDouble();
            }
            break;

            case CommandType.LineTo:
            {
                LineTo pCommand = command as LineTo;
                pCommand.X = reader.ReadDouble();
                pCommand.Y = reader.ReadDouble();
            }
            break;

            case CommandType.CurveTo:
            {
                CurveTo pCommand = command as CurveTo;
                pCommand.ControlStart = reader.ReadPoint();
                pCommand.ControlEnd   = reader.ReadPoint();
                pCommand.End          = reader.ReadPoint();
            }
            break;

            case CommandType.SmoothCurveTo:
            {
                SmoothCurveTo pCommand = command as SmoothCurveTo;
                pCommand.ControlEnd = reader.ReadPoint();
                pCommand.End        = reader.ReadPoint();
            }
            break;

            case CommandType.EllipticalArcTo:
            {
                EllipticalArcTo pCommand = command as EllipticalArcTo;
                pCommand.Size           = new System.Windows.Size(reader.ReadDouble(), reader.ReadDouble());
                pCommand.End            = reader.ReadPoint();
                pCommand.RotationAngle  = reader.ReadDouble();
                pCommand.IsLargeArc     = reader.ReadBoolean();
                pCommand.SweepDirection = (reader.ReadBoolean() == false ? SweepDirection.Counterclockwise : SweepDirection.Clockwise);
            }
            break;

            case CommandType.QuadraticBeizerCurveTo:
            {
                QuadraticBeizerCurveTo pCommand = command as QuadraticBeizerCurveTo;
                pCommand.Control = reader.ReadPoint();
                pCommand.End     = reader.ReadPoint();
            }
            break;

            case CommandType.SmoothQuadraticBeizerCurveTo:
            {
                SmoothQuadraticBeizerCurveTo pCommand = command as SmoothQuadraticBeizerCurveTo;
                pCommand.End = reader.ReadPoint();
            }
            break;

            case CommandType.ClosePath:
            {
                // Do nothing
            }
            break;
            }
        }
示例#8
0
        internal override void Read(System.IO.BinaryReader reader, BinaryReadInfo readInfo)
        {
            uint length = reader.ReadUInt32();

            ID = reader.ReadUInt32();
            uint numSections = reader.ReadUInt32();

            string componentName = null;
            bool   canResize     = false;
            bool   canFlip       = false;
            double minSize       = ComponentHelper.GridSize;
            List <ComponentProperty>          properties          = new List <ComponentProperty>();
            List <ConnectionGroup>            connections         = new List <ConnectionGroup>();
            List <RenderDescription>          renderDescriptions  = new List <RenderDescription>();
            List <Conditional <FlagOptions> > flagOptions         = new List <Conditional <FlagOptions> >();
            ComponentDescriptionMetadata      descriptionMetadata = new ComponentDescriptionMetadata();
            uint?iconResourceId = null;

            for (uint sectionCounter = 0; sectionCounter < numSections; sectionCounter++)
            {
                ushort sectionType   = reader.ReadUInt16();
                uint   sectionLength = reader.ReadUInt32();

                #region Metadata
                if (sectionType == (uint)BinaryConstants.ComponentSectionType.Metadata)
                {
                    componentName              = reader.ReadString();
                    canResize                  = reader.ReadBoolean();
                    canFlip                    = reader.ReadBoolean();
                    minSize                    = reader.ReadDouble();
                    descriptionMetadata.Type   = String.Format("Binary r{0} (*.cdcom)", readInfo.FormatVersion);
                    descriptionMetadata.GUID   = new Guid(reader.ReadBytes(16));
                    descriptionMetadata.Author = reader.ReadString();
                    if (readInfo.IsSignatureValid && readInfo.Certificate != null && readInfo.IsCertificateTrusted)
                    {
                        descriptionMetadata.Author = readInfo.Certificate.GetNameInfo(X509NameType.EmailName, false);
                    }
                    descriptionMetadata.Version = new Version(reader.ReadUInt16(), reader.ReadUInt16());
                    descriptionMetadata.AdditionalInformation          = reader.ReadString();
                    descriptionMetadata.ImplementSet                   = reader.ReadString();
                    descriptionMetadata.ImplementItem                  = reader.ReadString();
                    descriptionMetadata.Signature.IsHashValid          = readInfo.IsSignatureValid;
                    descriptionMetadata.Signature.Certificate          = readInfo.Certificate;
                    descriptionMetadata.Signature.IsCertificateTrusted = readInfo.IsCertificateTrusted;
                    int iconResource = reader.ReadInt32();
                    if (iconResource != -1)
                    {
                        iconResourceId = (uint)iconResource;
                    }
                    long created = reader.ReadInt64();
                }
                #endregion
                #region Flags
                else if (sectionType == (uint)BinaryConstants.ComponentSectionType.Flags)
                {
                    uint numFlagGroups = reader.ReadUInt32();
                    for (uint j = 0; j < numFlagGroups; j++)
                    {
                        IConditionTreeItem conditions;
                        if (readInfo.FormatVersion > 1)
                        {
                            conditions = reader.ReadConditionTree();
                        }
                        else
                        {
                            conditions = reader.ReadConditionCollection();
                        }

                        FlagOptions value = (FlagOptions)reader.ReadUInt32();
                        flagOptions.Add(new Conditional <FlagOptions>(value, conditions));
                    }
                }
                #endregion
                #region Properties
                else if (sectionType == (uint)BinaryConstants.ComponentSectionType.Properties)
                {
                    uint numProperties = reader.ReadUInt32();
                    for (uint j = 0; j < numProperties; j++)
                    {
                        string        propertyName   = reader.ReadString();
                        string        serializedName = reader.ReadString();
                        string        displayName    = reader.ReadString();
                        BinaryType    propType;
                        object        rawDefaultValue = reader.ReadType(out propType);
                        PropertyUnion defaultValue    = propType.ToPropertyUnion(rawDefaultValue);
                        string[]      enumOptions     = null;
                        if (propType == BinaryType.Enum)
                        {
                            enumOptions = new string[reader.ReadInt32()];
                            for (int k = 0; k < enumOptions.Length; k++)
                            {
                                enumOptions[k] = reader.ReadString();
                            }
                        }

                        // Format rules
                        List <ComponentPropertyFormat> formatRules = new List <ComponentPropertyFormat>();
                        uint numFormatRules = reader.ReadUInt32();
                        for (uint k = 0; k < numFormatRules; k++)
                        {
                            IConditionTreeItem conditions;
                            if (readInfo.FormatVersion > 1)
                            {
                                conditions = reader.ReadConditionTree();
                            }
                            else
                            {
                                conditions = reader.ReadConditionCollection();
                            }
                            string formatRule = reader.ReadString();
                            formatRules.Add(new ComponentPropertyFormat(formatRule, conditions));
                        }

                        // Other conditions
                        uint numOtherConditions = reader.ReadUInt32();
                        Dictionary <PropertyOtherConditionType, IConditionTreeItem> otherConditions = new Dictionary <PropertyOtherConditionType, IConditionTreeItem>((int)numOtherConditions);
                        for (uint k = 0; k < numOtherConditions; k++)
                        {
                            uint uintConditionType = reader.ReadUInt32();
                            IConditionTreeItem conditions;
                            if (readInfo.FormatVersion > 1)
                            {
                                conditions = reader.ReadConditionTree();
                            }
                            else
                            {
                                conditions = reader.ReadConditionCollection();
                            }
                            PropertyOtherConditionType conditionType = (PropertyOtherConditionType)uintConditionType;
                            otherConditions.Add(conditionType, conditions);
                        }

                        properties.Add(new ComponentProperty(propertyName, serializedName, displayName, BinaryIOExtentions.BinaryTypeToPropertyType(propType), defaultValue, formatRules.ToArray(), otherConditions, enumOptions));
                    }
                }
                #endregion
                #region Configurations
                else if (sectionType == (uint)BinaryConstants.ComponentSectionType.Configurations)
                {
                    uint numConfigurations = reader.ReadUInt32();
                    for (int j = 0; j < numConfigurations; j++)
                    {
                        string configurationName  = reader.ReadString();
                        string implementationName = reader.ReadString();

                        int numSetters = reader.ReadInt32();
                        var setters    = new Dictionary <string, PropertyUnion>(numSetters);
                        for (int k = 0; k < numSetters; k++)
                        {
                            BinaryType tempType;
                            string     name        = reader.ReadString();
                            var        setterValue = reader.ReadType(out tempType);
                            setters.Add(name, tempType.ToPropertyUnion(setterValue));
                        }

                        int iconID = reader.ReadInt32();

                        var configuration = new ComponentConfiguration(implementationName, configurationName, setters);
                        descriptionMetadata.Configurations.Add(configuration);

                        if (iconID != -1)
                        {
                            iconResources.Add(configuration, (uint)iconID);
                        }
                    }
                }
                #endregion
                #region Connections
                else if (sectionType == (uint)BinaryConstants.ComponentSectionType.Connections)
                {
                    uint numConnectionGroups = reader.ReadUInt32();
                    List <ConnectionGroup> connectionGroups = new List <ConnectionGroup>();
                    for (int j = 0; j < numConnectionGroups; j++)
                    {
                        IConditionTreeItem conditions;
                        if (readInfo.FormatVersion > 1)
                        {
                            conditions = reader.ReadConditionTree();
                        }
                        else
                        {
                            conditions = reader.ReadConditionCollection();
                        }

                        List <ConnectionDescription> tConnections = new List <ConnectionDescription>();
                        uint numConnections = reader.ReadUInt32();
                        for (uint k = 0; k < numConnections; k++)
                        {
                            tConnections.Add(new ConnectionDescription(reader.ReadComponentPoint(), reader.ReadComponentPoint(), (ConnectionEdge)reader.ReadInt32(), reader.ReadString()));
                        }

                        connections.Add(new ConnectionGroup(conditions, tConnections.ToArray()));
                    }
                }
                #endregion
                #region Render
                else if (sectionType == (uint)BinaryConstants.ComponentSectionType.Render)
                {
                    uint numRenderGroups = reader.ReadUInt32();
                    for (uint j = 0; j < numRenderGroups; j++)
                    {
                        IConditionTreeItem conditions;
                        if (readInfo.FormatVersion > 1)
                        {
                            conditions = reader.ReadConditionTree();
                        }
                        else
                        {
                            conditions = reader.ReadConditionCollection();
                        }

                        int numRenderCommands = (int)reader.ReadUInt32();
                        List <IRenderCommand> renderCommands = new List <IRenderCommand>(numRenderCommands);
                        for (int k = 0; k < numRenderCommands; k++)
                        {
                            RenderCommandType commandType = (RenderCommandType)reader.ReadUInt32();
                            switch (commandType)
                            {
                            case RenderCommandType.Line:
                            {
                                ComponentPoint start     = reader.ReadComponentPoint();
                                ComponentPoint end       = reader.ReadComponentPoint();
                                double         thickness = reader.ReadDouble();
                                renderCommands.Add(new Line(start, end, thickness));
                            }
                                continue;

                            case RenderCommandType.Rect:
                            {
                                ComponentPoint location  = reader.ReadComponentPoint();
                                double         width     = reader.ReadDouble();
                                double         height    = reader.ReadDouble();
                                double         thickness = reader.ReadDouble();
                                bool           fill      = (reader.ReadUInt32() == 0 ? false : true);
                                renderCommands.Add(new Rectangle(location, width, height, thickness, fill));
                            }
                                continue;

                            case RenderCommandType.Ellipse:
                            {
                                ComponentPoint centre    = reader.ReadComponentPoint();
                                double         radiusX   = reader.ReadDouble();
                                double         radiusY   = reader.ReadDouble();
                                double         thickness = reader.ReadDouble();
                                bool           fill      = (reader.ReadUInt32() == 0 ? false : true);
                                renderCommands.Add(new Ellipse(centre, radiusX, radiusY, thickness, fill));
                            }
                                continue;

                            case RenderCommandType.Path:
                            {
                                ComponentPoint start     = reader.ReadComponentPoint();
                                double         thickness = reader.ReadDouble();
                                bool           fill      = (reader.ReadUInt32() == 0 ? false : true);

                                int numCommands = reader.ReadInt32();
                                List <IPathCommand> pathCommands = new List <IPathCommand>(numCommands);
                                for (int l = 0; l < numCommands; l++)
                                {
                                    CommandType  pType      = (CommandType)reader.ReadInt32();
                                    IPathCommand theCommand = null;
                                    switch (pType)
                                    {
                                    case CommandType.MoveTo:
                                        theCommand = new MoveTo();
                                        break;

                                    case CommandType.LineTo:
                                        theCommand = new LineTo();
                                        break;

                                    case CommandType.CurveTo:
                                        theCommand = new CurveTo();
                                        break;

                                    case CommandType.EllipticalArcTo:
                                        theCommand = new EllipticalArcTo();
                                        break;

                                    case CommandType.QuadraticBeizerCurveTo:
                                        theCommand = new QuadraticBeizerCurveTo();
                                        break;

                                    case CommandType.SmoothCurveTo:
                                        theCommand = new SmoothCurveTo();
                                        break;

                                    case CommandType.SmoothQuadraticBeizerCurveTo:
                                        theCommand = new SmoothQuadraticBeizerCurveTo();
                                        break;

                                    default:
                                        theCommand = new ClosePath();
                                        break;
                                    }
                                    theCommand.Read(reader);
                                    pathCommands.Add(theCommand);
                                }

                                renderCommands.Add(new RenderPath(start, thickness, fill, pathCommands));
                            }
                                continue;

                            case RenderCommandType.Text:
                            {
                                byte           formattedTextVersion = reader.ReadByte();
                                ComponentPoint location             = reader.ReadComponentPoint();
                                TextAlignment  alignment            = (TextAlignment)reader.ReadUInt32();

                                uint           numTextRuns = reader.ReadUInt32();
                                List <TextRun> textRuns    = new List <TextRun>((int)numTextRuns);
                                for (uint l = 0; l < numTextRuns; l++)
                                {
                                    TextRunFormattingType formattingType = (TextRunFormattingType)reader.ReadUInt32();
                                    double runSize = reader.ReadDouble();
                                    string runText = reader.ReadString();
                                    textRuns.Add(new TextRun(runText, new TextRunFormatting(formattingType, runSize)));
                                }

                                renderCommands.Add(new Text(location, alignment, textRuns));
                            }
                                continue;
                            }
                        }

                        renderDescriptions.Add(new RenderDescription(conditions, renderCommands.ToArray()));
                    }
                }
                #endregion
                #region Skip
                else
                {
                    // Unknown type - skip
                    reader.BaseStream.Seek(sectionLength, SeekOrigin.Current);
                }
                #endregion
            }

            ComponentDescription = new ComponentDescription(ID.ToString(), componentName, canResize, canFlip, minSize, properties.ToArray(), connections.ToArray(), renderDescriptions.ToArray(), flagOptions.ToArray(), descriptionMetadata);

            if (iconResourceId.HasValue)
            {
                mainIconResource = iconResourceId.Value;
            }
        }
示例#9
0
        public static bool RenderFromXml(Stream xmlStream, IRenderContext renderContext, out Size imageSize)
        {
            XmlDocument doc = new XmlDocument();

            doc.Load(xmlStream);

            XmlNamespaceManager namespaceManager = new XmlNamespaceManager(doc.NameTable);

            namespaceManager.AddNamespace("p", PreviewNamespace);

            XmlNode previewNode = doc.SelectSingleNode("/p:preview", namespaceManager);

            imageSize = new Size(double.Parse(previewNode.Attributes["width"].InnerText), double.Parse(previewNode.Attributes["height"].InnerText));

            XmlNodeList renderNodes = previewNode.ChildNodes;

            foreach (XmlNode renderNode in renderNodes)
            {
                XmlElement renderElement = renderNode as XmlElement;

                if (renderElement == null)
                {
                    continue;
                }

                if (renderElement.Name == "line")
                {
                    Point  start     = Point.Parse(renderElement.Attributes["start"].InnerText);
                    Point  end       = Point.Parse(renderElement.Attributes["end"].InnerText);
                    double thickness = double.Parse(renderElement.Attributes["thickness"].InnerText);
                    renderContext.DrawLine(start, end, thickness);
                }
                else if (renderElement.Name == "rect")
                {
                    Point  start     = Point.Parse(renderElement.Attributes["start"].InnerText);
                    Size   size      = Size.Parse(renderElement.Attributes["size"].InnerText);
                    double thickness = double.Parse(renderElement.Attributes["thickness"].InnerText);
                    bool   fill      = bool.Parse(renderElement.Attributes["fill"].InnerText);
                    renderContext.DrawRectangle(start, size, thickness, fill);
                }
                else if (renderElement.Name == "ellipse")
                {
                    Point  centre    = Point.Parse(renderElement.Attributes["centre"].InnerText);
                    double radiusx   = double.Parse(renderElement.Attributes["rx"].InnerText);
                    double radiusy   = double.Parse(renderElement.Attributes["ry"].InnerText);
                    double thickness = double.Parse(renderElement.Attributes["thickness"].InnerText);
                    bool   fill      = bool.Parse(renderElement.Attributes["fill"].InnerText);
                    renderContext.DrawEllipse(centre, radiusx, radiusy, thickness, fill);
                }
                else if (renderElement.Name == "path")
                {
                    Point  start     = Point.Parse(renderElement.Attributes["start"].InnerText);
                    double thickness = double.Parse(renderElement.Attributes["thickness"].InnerText);
                    bool   fill      = bool.Parse(renderElement.Attributes["fill"].InnerText);
                    string data      = renderElement.InnerText;
                    List <IPathCommand> pathCommands = new List <IPathCommand>();
                    using (MemoryStream dataStream = new MemoryStream(Convert.FromBase64String(data)))
                    {
                        BinaryReader reader = new BinaryReader(dataStream);

                        int numCommands = reader.ReadInt32();

                        for (int l = 0; l < numCommands; l++)
                        {
                            CommandType  pType      = (CommandType)reader.ReadInt32();
                            IPathCommand theCommand = null;
                            switch (pType)
                            {
                            case CommandType.MoveTo:
                                theCommand = new MoveTo();
                                break;

                            case CommandType.LineTo:
                                theCommand = new LineTo();
                                break;

                            case CommandType.CurveTo:
                                theCommand = new CurveTo();
                                break;

                            case CommandType.EllipticalArcTo:
                                theCommand = new EllipticalArcTo();
                                break;

                            case CommandType.QuadraticBeizerCurveTo:
                                theCommand = new QuadraticBeizerCurveTo();
                                break;

                            case CommandType.SmoothCurveTo:
                                theCommand = new SmoothCurveTo();
                                break;

                            case CommandType.SmoothQuadraticBeizerCurveTo:
                                theCommand = new SmoothQuadraticBeizerCurveTo();
                                break;

                            default:
                                theCommand = new ClosePath();
                                break;
                            }
                            theCommand.Read(reader);
                            pathCommands.Add(theCommand);
                        }
                    }
                    renderContext.DrawPath(start, pathCommands, thickness, fill);
                }
                else if (renderElement.Name == "text")
                {
                    Point          anchor    = Point.Parse(renderElement.Attributes["anchor"].InnerText);
                    TextAlignment  alignment = (TextAlignment)Enum.Parse(typeof(TextAlignment), renderElement.Attributes["alignment"].InnerText);
                    List <TextRun> runs      = new List <TextRun>();
                    foreach (XmlNode runNode in renderElement.ChildNodes)
                    {
                        if (runNode.Name != "run")
                        {
                            continue;
                        }

                        double size = double.Parse(runNode.Attributes["size"].InnerText);
                        TextRunFormattingType formattingType = (TextRunFormattingType)Enum.Parse(typeof(TextRunFormattingType), runNode.Attributes["formatting"].InnerText);
                        string text = runNode.InnerText;
                        runs.Add(new TextRun(text, new TextRunFormatting(formattingType, size)));
                    }
                    renderContext.DrawText(anchor, alignment, runs);
                }
            }

            return(true);
        }
示例#10
0
        public static bool RenderFromXml(Stream xmlStream, IDrawingContext drawingContext, out Size imageSize)
        {
            var doc = XDocument.Load(xmlStream);

            var previewNode = doc.Elements().First(x => x.Name == PreviewNamespace + "preview");

            imageSize = new Size(double.Parse(previewNode.Attribute("width").Value),
                                 double.Parse(previewNode.Attribute("height").Value));

            var renderElements = previewNode.Elements();

            foreach (var renderElement in renderElements)
            {
                if (renderElement.Name == "line")
                {
                    Point  start     = Point.Parse(renderElement.Attribute("start").Value);
                    Point  end       = Point.Parse(renderElement.Attribute("end").Value);
                    double thickness = double.Parse(renderElement.Attribute("thickness").Value);
                    drawingContext.DrawLine(start, end, thickness);
                }
                else if (renderElement.Name == "rect")
                {
                    Point  start     = Point.Parse(renderElement.Attribute("start").Value);
                    Size   size      = Size.Parse(renderElement.Attribute("size").Value);
                    double thickness = double.Parse(renderElement.Attribute("thickness").Value);
                    bool   fill      = bool.Parse(renderElement.Attribute("fill").Value);
                    drawingContext.DrawRectangle(start, size, thickness, fill);
                }
                else if (renderElement.Name == "ellipse")
                {
                    Point  centre    = Point.Parse(renderElement.Attribute("centre").Value);
                    double radiusx   = double.Parse(renderElement.Attribute("rx").Value);
                    double radiusy   = double.Parse(renderElement.Attribute("ry").Value);
                    double thickness = double.Parse(renderElement.Attribute("thickness").Value);
                    bool   fill      = bool.Parse(renderElement.Attribute("fill").Value);
                    drawingContext.DrawEllipse(centre, radiusx, radiusy, thickness, fill);
                }
                else if (renderElement.Name == "path")
                {
                    Point  start     = Point.Parse(renderElement.Attribute("start").Value);
                    double thickness = double.Parse(renderElement.Attribute("thickness").Value);
                    bool   fill      = bool.Parse(renderElement.Attribute("fill").Value);
                    string data      = renderElement.Value;
                    List <IPathCommand> pathCommands = new List <IPathCommand>();
                    using (MemoryStream dataStream = new MemoryStream(Convert.FromBase64String(data)))
                    {
                        BinaryReader reader = new BinaryReader(dataStream);

                        int numCommands = reader.ReadInt32();

                        for (int l = 0; l < numCommands; l++)
                        {
                            CommandType  pType      = (CommandType)reader.ReadInt32();
                            IPathCommand theCommand = null;
                            switch (pType)
                            {
                            case CommandType.MoveTo:
                                theCommand = new MoveTo();
                                break;

                            case CommandType.LineTo:
                                theCommand = new LineTo();
                                break;

                            case CommandType.CurveTo:
                                theCommand = new CurveTo();
                                break;

                            case CommandType.EllipticalArcTo:
                                theCommand = new EllipticalArcTo();
                                break;

                            case CommandType.QuadraticBeizerCurveTo:
                                theCommand = new QuadraticBeizerCurveTo();
                                break;

                            case CommandType.SmoothCurveTo:
                                theCommand = new SmoothCurveTo();
                                break;

                            case CommandType.SmoothQuadraticBeizerCurveTo:
                                theCommand = new SmoothQuadraticBeizerCurveTo();
                                break;

                            default:
                                theCommand = new ClosePath();
                                break;
                            }
                            theCommand.Read(reader);
                            pathCommands.Add(theCommand);
                        }
                    }
                    drawingContext.DrawPath(start, pathCommands, thickness, fill);
                }
                else if (renderElement.Name == "text")
                {
                    Point          anchor    = Point.Parse(renderElement.Attribute("anchor").Value);
                    TextAlignment  alignment = (TextAlignment)Enum.Parse(typeof(TextAlignment), renderElement.Attribute("alignment").Value);
                    List <TextRun> runs      = new List <TextRun>();
                    foreach (var runNode in renderElement.Elements())
                    {
                        if (runNode.Name != "run")
                        {
                            continue;
                        }

                        double size = double.Parse(runNode.Attribute("size").Value);
                        TextRunFormattingType formattingType = (TextRunFormattingType)Enum.Parse(typeof(TextRunFormattingType), runNode.Attribute("formatting").Value);
                        string text = runNode.Value;
                        runs.Add(new TextRun(text, new TextRunFormatting(formattingType, size)));
                    }
                    drawingContext.DrawText(anchor, alignment, runs);
                }
            }

            return(true);
        }
        public static List <Base> ToPathList(this string pathDataText)
        {
            pathDataText = pathDataText.Trim();
            var strings = new List <string>();
            int start   = 0;

            for (int i = 1; i <= pathDataText.Length; i++)
            {
                if (i == pathDataText.Length || (char.ToLower(pathDataText[i]) != 'e' && char.IsLetter(pathDataText[i])))
                {
                    strings.Add(pathDataText.Substring(start, i - start));
                    start = i;
                }
            }

            var result = new List <PathElement.Base>();

            foreach (var str in strings)
            {
                if (string.IsNullOrWhiteSpace(str))
                {
                    continue;
                }
                var cmd = char.ToUpper(str[0]);
                switch (cmd)
                {
                case Arc.Symbol:
                    if (Arc.FromString(str) is List <Arc> arcList)
                    {
                        result.AddRange(arcList);
                    }
                    break;

                case BezierCurve.Symbol:
                    if (BezierCurve.FromString(str) is List <BezierCurve> bezierCurveList)
                    {
                        result.AddRange(bezierCurveList);
                    }
                    break;

                case ClosePath.Symbol:
                    result.Add(new ClosePath());
                    break;

                case HorzLineTo.Symbol:
                    if (HorzLineTo.FromString(str) is HorzLineTo horzLineTo)
                    {
                        result.Add(horzLineTo);
                    }
                    break;

                case LineTo.Symbol:
                    if (LineTo.FromString(str) is List <LineTo> lineList)
                    {
                        result.AddRange(lineList);
                    }
                    break;

                case MoveTo.Symbol:
                    if (MoveTo.FromString(str) is List <Base> moveAndLineList)
                    {
                        result.AddRange(moveAndLineList);
                    }
                    break;

                case QuadraticCurve.Symbol:
                    if (QuadraticCurve.FromString(str) is List <QuadraticCurve> quadraticCurveList)
                    {
                        result.AddRange(quadraticCurveList);
                    }
                    break;

                case ShortcutBezierCurve.Symbol:
                    if (ShortcutBezierCurve.FromString(str) is List <ShortcutBezierCurve> shortcutBezierCurveList)
                    {
                        result.AddRange(shortcutBezierCurveList);
                    }
                    break;

                case ShortcutQuadraticCurve.Symbol:
                    if (ShortcutQuadraticCurve.FromString(str) is List <ShortcutQuadraticCurve> shortcutQuadraticCurveList)
                    {
                        result.AddRange(shortcutQuadraticCurveList);
                    }
                    break;

                case VertLineTo.Symbol:
                    if (VertLineTo.FromString(str) is VertLineTo vertLineTo)
                    {
                        result.Add(vertLineTo);
                    }
                    break;

                default:
                    throw new Exception("Unrecognized Path Command : " + str);
                }
            }
            return(result);
        }