private void WriteStitchingFunction(DocumentWriter writer) { const int stitchingFunctionType = 3; var stops = _shadingColor.Stops; writer.WriteDictionaryOpen(); { writer.WriteName(NameFunctionTypeBytes); writer.WriteNumber(stitchingFunctionType); WriteDomain0To1NameAndArray(writer); writer.WriteName(NameFunctionsBytes); writer.WriteArrayOpen(); { for (var i = 0; i < stops.Count - 1; i++) { var stop = stops[i]; var next = stops[i + 1]; WriteLinearColorInterpolationFunction(writer, stop.Color, next.Color); } } writer.WriteArrayClose(); writer.WriteName(NameBoundsBytes); writer.WriteArrayOpen(); { for (var i = 1; i < stops.Count - 1; i++) { writer.WriteNumber(stops[i].Offset); } } writer.WriteArrayClose(); writer.WriteName(NameEncodeBytes); writer.WriteArrayOpen(); { for (var i = 0; i < stops.Count - 1; i++) { writer.WriteNumber(0); writer.WriteNumber(1); } } writer.WriteArrayClose(); } writer.WriteDictionaryClose(); }
private static void WriteDomain0To1NameAndArray(DocumentWriter writer) { writer.WriteName(NameDomainBytes); writer.WriteArrayOpen(); { writer.WriteNumber0(); writer.WriteNumber1(); } writer.WriteArrayClose(); }
private static void WriteLinearColorInterpolationFunction(DocumentWriter writer, PdfColor color1, PdfColor color2) { const int interpolationFunctionType = 2; void WriteColorArray(PdfColor color) { writer.WriteArrayOpen(); switch (color) { case RgbColor rgb: writer.WriteNumberColor(rgb.R); writer.WriteNumberColor(rgb.G); writer.WriteNumberColor(rgb.B); break; case CmykColor cmyk: writer.WriteNumberColor(cmyk.C); writer.WriteNumberColor(cmyk.M); writer.WriteNumberColor(cmyk.Y); writer.WriteNumberColor(cmyk.K); break; default: throw new NotSupportedException(); } writer.WriteArrayClose(); } writer.WriteDictionaryOpen(); { writer.WriteName(NameFunctionTypeBytes); writer.WriteNumber(interpolationFunctionType); writer.WriteName(NameNBytes); writer.WriteNumber1(); WriteDomain0To1NameAndArray(writer); writer.WriteName(NameC0Bytes); WriteColorArray(color1); writer.WriteName(NameC1Bytes); WriteColorArray(color2); } writer.WriteDictionaryClose(); }
public override void Draw(DocumentWriter writer) { const int shadingPatternType = 2; // tiling pattern = 1 void WriteNumberHighPrecision(float v) { // WriteNumber isn't high precision enough, only supports 2 decimal places :( var bytes = Encoding.ASCII.GetBytes($"{v} "); writer.Write(bytes); } writer.WriteBeginObject(); writer.WriteDictionaryOpen(); { writer.WriteName(NamePatternTypeBytes); writer.WriteNumber(shadingPatternType); writer.WriteName(NameMatrixBytes); writer.WriteArrayOpen(); { _shadingColor.DrawMatrix(WriteNumberHighPrecision); } writer.WriteArrayClose(); writer.WriteName(NameShadingBytes); writer.WriteDictionaryOpen(); { writer.WriteName(NameShadingTypeBytes); writer.WriteNumber((int)_shadingColor.ShadingType); writer.WriteName(NameColorSpaceBytes); _shadingColor.Stops[0].Color.ColorSpace.DrawColorSpace(writer); writer.WriteName(NameCoordsBytes); writer.WriteArrayOpen(); { _shadingColor.DrawCoordinates(WriteNumberHighPrecision); } writer.WriteArrayClose(); writer.WriteName(NameExtendBytes); writer.WriteArrayOpen(); { writer.WriteBoolean(true); writer.WriteBoolean(true); } writer.WriteArrayClose(); writer.WriteName(NameFunctionBytes); if (_shadingColor.Stops.Count > 2) { WriteStitchingFunction(writer); } else if (_shadingColor.Stops.Count == 2) { var stop1 = _shadingColor.Stops[0]; var stop2 = _shadingColor.Stops[1]; WriteLinearColorInterpolationFunction(writer, stop1.Color, stop2.Color); } else { throw new InvalidOperationException($"Expected two or more color stops"); } } writer.WriteDictionaryClose(); } writer.WriteDictionaryClose(); writer.WriteEndObject(); }