|
| 1 | +using Synercoding.FileFormats.Pdf.LowLevel; |
| 2 | +using Synercoding.FileFormats.Pdf.LowLevel.Graphics; |
| 3 | +using Synercoding.FileFormats.Pdf.LowLevel.Operators.Color; |
| 4 | +using Synercoding.FileFormats.Pdf.LowLevel.Operators.Pathing.Construction; |
| 5 | +using Synercoding.FileFormats.Pdf.LowLevel.Operators.Pathing.Painting; |
| 6 | +using Synercoding.FileFormats.Pdf.LowLevel.Operators.State; |
| 7 | +using Synercoding.Primitives; |
| 8 | +using System; |
| 9 | + |
| 10 | +namespace Synercoding.FileFormats.Pdf.Internals |
| 11 | +{ |
| 12 | + internal class Path : IPath |
| 13 | + { |
| 14 | + private readonly ShapeContext _context; |
| 15 | + private readonly ContentStream _contentStream; |
| 16 | + |
| 17 | + public Path(ShapeContext context, ContentStream contentStream, GraphicsState graphicsState) |
| 18 | + { |
| 19 | + _context = context; |
| 20 | + _contentStream = contentStream; |
| 21 | + GraphicsState = graphicsState; |
| 22 | + |
| 23 | + _startPath(); |
| 24 | + } |
| 25 | + |
| 26 | + internal GraphicsState GraphicsState { get; } |
| 27 | + |
| 28 | + private void _startPath() |
| 29 | + { |
| 30 | + _contentStream.SaveState(); |
| 31 | + |
| 32 | + _contentStream |
| 33 | + .Write(new LineWidthOperator(GraphicsState.LineWidth)) |
| 34 | + .Write(new LineCapOperator(GraphicsState.LineCap)) |
| 35 | + .Write(new LineJoinOperator(GraphicsState.LineJoin)) |
| 36 | + .Write(new MiterLimitOperator(GraphicsState.MiterLimit)) |
| 37 | + .Write(new DashOperator(GraphicsState.Dash.Array, GraphicsState.Dash.Phase)); |
| 38 | + |
| 39 | + // write graphic state to stream |
| 40 | + } |
| 41 | + |
| 42 | + internal void FinishPath() |
| 43 | + { |
| 44 | + // TODO: set colorspace for stroke and/or fill |
| 45 | + |
| 46 | + if (GraphicsState.Stroke is not null && GraphicsState.Fill is not null) |
| 47 | + { |
| 48 | + if (GraphicsState.Stroke is GrayColor gs) |
| 49 | + _contentStream.Write(new GrayStrokingColorOperator(gs)); |
| 50 | + else if (GraphicsState.Stroke is RgbColor rs) |
| 51 | + _contentStream.Write(new RgbStrokingColorOperator(rs)); |
| 52 | + else if (GraphicsState.Stroke is CmykColor cs) |
| 53 | + _contentStream.Write(new CmykStrokingColorOperator(cs)); |
| 54 | + else |
| 55 | + throw new NotImplementedException($"The color type {GraphicsState.Stroke.GetType().Name} is not implemented."); |
| 56 | + |
| 57 | + if (GraphicsState.Fill is GrayColor gf) |
| 58 | + _contentStream.Write(new GrayNonStrokingColorOperator(gf)); |
| 59 | + else if (GraphicsState.Fill is RgbColor rf) |
| 60 | + _contentStream.Write(new RgbNonStrokingColorOperator(rf)); |
| 61 | + else if (GraphicsState.Fill is CmykColor cf) |
| 62 | + _contentStream.Write(new CmykNonStrokingColorOperator(cf)); |
| 63 | + else |
| 64 | + throw new NotImplementedException($"The color type {GraphicsState.Fill.GetType().Name} is not implemented."); |
| 65 | + |
| 66 | + _contentStream.Write(new FillAndStrokeOperator(GraphicsState.FillRule)); |
| 67 | + } |
| 68 | + else if (GraphicsState.Fill is not null) |
| 69 | + { |
| 70 | + if (GraphicsState.Fill is GrayColor gf) |
| 71 | + _contentStream.Write(new GrayNonStrokingColorOperator(gf)); |
| 72 | + else if (GraphicsState.Fill is RgbColor rf) |
| 73 | + _contentStream.Write(new RgbNonStrokingColorOperator(rf)); |
| 74 | + else if (GraphicsState.Fill is CmykColor cf) |
| 75 | + _contentStream.Write(new CmykNonStrokingColorOperator(cf)); |
| 76 | + else |
| 77 | + throw new NotImplementedException($"The color type {GraphicsState.Fill.GetType().Name} is not implemented."); |
| 78 | + |
| 79 | + _contentStream.Write(new FillOperator(GraphicsState.FillRule)); |
| 80 | + } |
| 81 | + else if (GraphicsState.Stroke is not null) |
| 82 | + { |
| 83 | + if (GraphicsState.Stroke is GrayColor gs) |
| 84 | + _contentStream.Write(new GrayStrokingColorOperator(gs)); |
| 85 | + else if (GraphicsState.Stroke is RgbColor rs) |
| 86 | + _contentStream.Write(new RgbStrokingColorOperator(rs)); |
| 87 | + else if (GraphicsState.Stroke is CmykColor cs) |
| 88 | + _contentStream.Write(new CmykStrokingColorOperator(cs)); |
| 89 | + else |
| 90 | + throw new NotImplementedException($"The color type {GraphicsState.Stroke.GetType().Name} is not implemented."); |
| 91 | + |
| 92 | + _contentStream.Write(new StrokeOperator()); |
| 93 | + } |
| 94 | + else |
| 95 | + { |
| 96 | + _contentStream.Write(new EndPathOperator()); |
| 97 | + } |
| 98 | + |
| 99 | + _contentStream.RestoreState(); |
| 100 | + } |
| 101 | + |
| 102 | + public IShapeContext Close() |
| 103 | + { |
| 104 | + _contentStream.Write(new CloseOperator()); |
| 105 | + |
| 106 | + return _context; |
| 107 | + } |
| 108 | + |
| 109 | + public IPath CurveTo(double cpX1, double cpY1, double cpX2, double cpY2, double finalX, double finalY) |
| 110 | + { |
| 111 | + _contentStream.Write(new CubicBezierCurveDualControlPointsOperator(cpX1, cpY1, cpX2, cpY2, finalX, finalY)); |
| 112 | + return this; |
| 113 | + } |
| 114 | + |
| 115 | + public IPath CurveTo(Point cp1, Point cp2, Point final) |
| 116 | + { |
| 117 | + _contentStream.Write(new CubicBezierCurveDualControlPointsOperator(cp1, cp2, final)); |
| 118 | + return this; |
| 119 | + } |
| 120 | + |
| 121 | + public IPath CurveToWithEndAnker(double cpX, double cpY, double finalX, double finalY) |
| 122 | + { |
| 123 | + _contentStream.Write(new CubicBezierCurveFinalControlPointsOperator(cpX, cpY, finalX, finalY)); |
| 124 | + return this; |
| 125 | + } |
| 126 | + |
| 127 | + public IPath CurveToWithEndAnker(Point cp, Point final) |
| 128 | + { |
| 129 | + _contentStream.Write(new CubicBezierCurveFinalControlPointsOperator(cp, final)); |
| 130 | + return this; |
| 131 | + } |
| 132 | + |
| 133 | + public IPath CurveToWithStartAnker(double cpX, double cpY, double finalX, double finalY) |
| 134 | + { |
| 135 | + _contentStream.Write(new CubicBezierCurveInitialControlPointsOperator(cpX, cpY, finalX, finalY)); |
| 136 | + return this; |
| 137 | + } |
| 138 | + |
| 139 | + public IPath CurveToWithStartAnker(Point cp, Point final) |
| 140 | + { |
| 141 | + _contentStream.Write(new CubicBezierCurveInitialControlPointsOperator(cp, final)); |
| 142 | + return this; |
| 143 | + } |
| 144 | + |
| 145 | + public IPath LineTo(double x, double y) |
| 146 | + { |
| 147 | + _contentStream.Write(new LineOperator(x, y)); |
| 148 | + return this; |
| 149 | + } |
| 150 | + |
| 151 | + public IPath LineTo(Point point) |
| 152 | + { |
| 153 | + _contentStream.Write(new LineOperator(point)); |
| 154 | + return this; |
| 155 | + } |
| 156 | + |
| 157 | + public IPath Move(double x, double y) |
| 158 | + { |
| 159 | + _contentStream.Write(new MoveOperator(x, y)); |
| 160 | + return this; |
| 161 | + } |
| 162 | + |
| 163 | + public IPath Move(Point point) |
| 164 | + { |
| 165 | + _contentStream.Write(new MoveOperator(point)); |
| 166 | + return this; |
| 167 | + } |
| 168 | + |
| 169 | + public IPath Rectangle(double x, double y, double width, double height) |
| 170 | + { |
| 171 | + _contentStream.Write(new RectangleOperator(x, y, width, height)); |
| 172 | + return this; |
| 173 | + } |
| 174 | + |
| 175 | + public IPath Rectangle(Rectangle rectangle) |
| 176 | + { |
| 177 | + _contentStream.Write(new RectangleOperator(rectangle)); |
| 178 | + return this; |
| 179 | + } |
| 180 | + } |
| 181 | +} |
0 commit comments