public static Bitmap RenderToBitmap(int nTwipSize, ArrayList commandList, ArrayList fillStyles, ArrayList lineStyles, out EPoint ptOffset) { EPoint pntCurrentLoc = new EPoint(); ArrayList pathInfos = new ArrayList(); PathInfo pathInfoCurrent = new PathInfo(); pathInfos.Add(pathInfoCurrent); bool hasDrawnSinceLastStyleChange = false; EPointF ptScale = new EPointF(1, 1) / nTwipSize; string sDebug = ""; foreach (ShapeCommand.Base cmd in commandList) { if (cmd.MovesTurtle) { if (cmd.Draws) { ((ShapeCommand.Draw)cmd).AddToPath(pathInfoCurrent.Path, pntCurrentLoc, ptScale.X); if (Shape.Debug) { ArrayList pts = ((ShapeCommand.Draw)cmd).GeneratePoints(pntCurrentLoc); if (cmd is ShapeCommand.Curve) { sDebug += "Curve"; } else { sDebug += "Line"; } sDebug += "\r\n"; foreach (EPointF pt in pts) { sDebug += pt.ToString() + "\r\n"; } } hasDrawnSinceLastStyleChange = true; } else { pathInfoCurrent.Path.CloseAllFigures(); pathInfoCurrent.Path.StartFigure(); } pntCurrentLoc = cmd.GetNewLoc(pntCurrentLoc); } else { if (hasDrawnSinceLastStyleChange) { pathInfoCurrent = new PathInfo(); pathInfos.Add(pathInfoCurrent); } if (cmd is ShapeCommand.FillStyle) { ShapeCommand.FillStyle fs = (ShapeCommand.FillStyle)cmd; Brush brush = null; if (fs.StyleId > 0) { brush = ((Style.FillStyle)fillStyles[fs.StyleId - 1]).GetBrush(); } if (fs.Side == 0) { pathInfoCurrent.Brush0 = brush; } else { pathInfoCurrent.Brush1 = brush; } } else if (cmd is ShapeCommand.LineStyle) { ShapeCommand.LineStyle ls = (ShapeCommand.LineStyle)cmd; Pen pen = null; if (ls.StyleId > 0) { pen = ((Style.LineStyle)lineStyles[ls.StyleId - 1]).GetPen(); } pathInfoCurrent.Pen = pen; if (pen != null) { pen.Width *= ptScale.X; } } hasDrawnSinceLastStyleChange = false; } } Matrix transform = new Matrix(); transform.Scale(ptScale.X, ptScale.Y); ERectangle bounds = ERectangle.FromLTRB(99999, 99999, -99999, -99999); foreach (PathInfo pathInfo in pathInfos) { //pathInfo.Path.Transform(transform); bounds.Expand(pathInfo.GetBounds().ToERectangle()); } ptOffset = bounds.TopLeft; if (bounds.Width == 0 || bounds.Height == 0) { return(null); } //this.Bounds = bounds; if (Shape.Debug) { Endogine.Files.FileReadWrite.Write("__s.txt", sDebug); } transform = new Matrix(); transform.Translate(-bounds.X, -bounds.Y); foreach (PathInfo pathInfo in pathInfos) { pathInfo.Path.Transform(transform); } Bitmap bmp = new Bitmap(bounds.Width, bounds.Height, PixelFormat.Format32bppArgb); Endogine.BitmapHelpers.Canvas canvas = Endogine.BitmapHelpers.Canvas.Create(bmp); canvas.Locked = true; canvas.Fill(Color.FromArgb(0, 255, 255, 255)); canvas.Dispose(); Graphics g = Graphics.FromImage(bmp); g.SmoothingMode = SmoothingMode.HighQuality; foreach (PathInfo pathInfo in pathInfos) { if (pathInfo.Pen != null) { g.DrawPath(pathInfo.Pen, pathInfo.Path); } if (pathInfo.Brush0 != null) { g.FillPath(pathInfo.Brush0, pathInfo.Path); } if (pathInfo.Brush1 != null) //TODO: can GDI+ handle Flash's two different fills (0 and 1)? { g.FillPath(pathInfo.Brush1, pathInfo.Path); } } return(bmp); }
public ArrayList CreateMorphedShape(float ratio) { ArrayList morphedCommands = new ArrayList(); int targetIndex = -1; EPoint ptCurrent = new EPoint(); foreach (ShapeCommand.Base cmd in this.CommandList) { if (!(cmd is ShapeCommand.Style)) { targetIndex++; } ShapeCommand.Base cmdTo = (ShapeCommand.Base) this._morphCommandList[targetIndex]; if (cmd is ShapeCommand.Move) { EPoint pt1 = ((ShapeCommand.Move)cmd).Target; EPoint pt2 = ((ShapeCommand.Move)cmdTo).Target; ptCurrent = this.GetPointBetween(pt1, pt2, ratio); morphedCommands.Add(new ShapeCommand.Move(ptCurrent)); } else if ((cmd is ShapeCommand.Line) || (cmd is ShapeCommand.Curve)) { ShapeCommand.Draw draw = (ShapeCommand.Draw)cmd; ShapeCommand.Curve curve = draw.GetAsCurve(); if ((cmdTo is ShapeCommand.Line) || (cmdTo is ShapeCommand.Curve)) { draw = (ShapeCommand.Draw)cmdTo; ShapeCommand.Curve curveTo = draw.GetAsCurve(); EPoint ptControl = this.GetPointBetween(curve.Control, curveTo.Control, ratio); EPoint ptAnchor = this.GetPointBetween(curve.Anchor, curveTo.Anchor, ratio); morphedCommands.Add(new ShapeCommand.Curve(ptControl, ptAnchor)); ptCurrent += ptControl + ptAnchor; } else { ShapeCommand.Move move = (ShapeCommand.Move)cmdTo; EPoint ptFrom = ptCurrent + curve.Control + curve.Anchor; EPoint ptTo = move.Target; ptCurrent = this.GetPointBetween(ptFrom, ptTo, ratio); morphedCommands.Add(new ShapeCommand.Move(ptCurrent.Copy())); } } else if (cmd is ShapeCommand.LineStyle) { ShapeCommand.LineStyle ls = (ShapeCommand.LineStyle)cmd; if (ls.StyleId > 0) { Style.LineStyle style = (Style.LineStyle) this.LineStyles[ls.StyleId - 1]; style.MorphPosition = ratio; morphedCommands.Add(ls); //.GetMorphed(ratio)); } } else if (cmd is ShapeCommand.FillStyle) { ShapeCommand.FillStyle fs = (ShapeCommand.FillStyle)cmd; if (fs.StyleId > 0) { Style.FillStyle style = (Style.FillStyle) this.FillStyles[fs.StyleId - 1]; style.MorphPosition = ratio; morphedCommands.Add(fs); //.GetMorphed(ratio)); } } } return(morphedCommands); }