From 96f773c308f4378275e76f047407ad75528aca7f Mon Sep 17 00:00:00 2001 From: Sergiusz Zalewski <38229504+KeterSCP@users.noreply.github.com> Date: Thu, 9 Nov 2023 21:23:28 +0100 Subject: [PATCH] EMR_CREATEBRUSHINDIRECT --- SharpEmf.sln.DotSettings | 12 ++++ src/SharpEmf/Enums/BrushStyle.cs | 21 +++++++ src/SharpEmf/Enums/EmfRecordType.cs | 5 ++ src/SharpEmf/Enums/HatchStyle.cs | 17 ++++++ src/SharpEmf/Objects/ColorRef.cs | 8 +-- src/SharpEmf/Objects/LogBrushEx.cs | 61 +++++++++++++++++++ .../Records/EnhancedMetafileRecord.cs | 2 + .../ObjectCreation/EmrCreateBrushIndirect.cs | 39 ++++++++++++ .../SharpEmf.UnitTests.csproj | 5 +- 9 files changed, 160 insertions(+), 10 deletions(-) create mode 100644 src/SharpEmf/Enums/BrushStyle.cs create mode 100644 src/SharpEmf/Enums/HatchStyle.cs create mode 100644 src/SharpEmf/Objects/LogBrushEx.cs create mode 100644 src/SharpEmf/Records/ObjectCreation/EmrCreateBrushIndirect.cs diff --git a/SharpEmf.sln.DotSettings b/SharpEmf.sln.DotSettings index 3f3684a..8dd08cb 100644 --- a/SharpEmf.sln.DotSettings +++ b/SharpEmf.sln.DotSettings @@ -10,7 +10,14 @@ True True True + True True + True + True + True + True + True + True True True True @@ -57,6 +64,8 @@ True True True + True + True True True True @@ -98,6 +107,9 @@ True True True + True + True + True True True True diff --git a/src/SharpEmf/Enums/BrushStyle.cs b/src/SharpEmf/Enums/BrushStyle.cs new file mode 100644 index 0000000..8bd14a8 --- /dev/null +++ b/src/SharpEmf/Enums/BrushStyle.cs @@ -0,0 +1,21 @@ +using JetBrains.Annotations; + +namespace SharpEmf.Enums; + +/// +/// Specifies the different possible brush types that can be used in graphics operations +/// +[PublicAPI] +public enum BrushStyle : uint +{ + BS_SOLID = 0x0000, + BS_NULL = 0x0001, + BS_HATCHED = 0x0002, + BS_PATTERN = 0x0003, + BS_INDEXED = 0x0004, + BS_DIBPATTERN = 0x0005, + BS_DIBPATTERNPT = 0x0006, + BS_PATTERN8X8 = 0x0007, + BS_DIBPATTERN8X8 = 0x0008, + BS_MONOPATTERN = 0x0009 +} \ No newline at end of file diff --git a/src/SharpEmf/Enums/EmfRecordType.cs b/src/SharpEmf/Enums/EmfRecordType.cs index 0094326..1944c40 100644 --- a/src/SharpEmf/Enums/EmfRecordType.cs +++ b/src/SharpEmf/Enums/EmfRecordType.cs @@ -115,6 +115,11 @@ public enum EmfRecordType : uint /// EMR_SELECTOBJECT = 0x00000025, + /// + /// Defines a logical brush for filling figures in graphics operations + /// + EMR_CREATEBRUSHINDIRECT = 0x00000027, + /// /// Deletes a graphics object, clearing its index in the EMF object table /// diff --git a/src/SharpEmf/Enums/HatchStyle.cs b/src/SharpEmf/Enums/HatchStyle.cs new file mode 100644 index 0000000..1356abb --- /dev/null +++ b/src/SharpEmf/Enums/HatchStyle.cs @@ -0,0 +1,17 @@ +using JetBrains.Annotations; + +namespace SharpEmf.Enums; + +/// +/// Specifies the hatch pattern +/// +[PublicAPI] +public enum HatchStyle : uint +{ + HS_SOLIDCLR = 0x0006, + HS_DITHEREDCLR = 0x0007, + HS_SOLIDTEXTCLR = 0x0008, + HS_DITHEREDTEXTCLR = 0x0009, + HS_SOLIDBKCLR = 0x000A, + HS_DITHEREDBKCLR = 0x000B +} \ No newline at end of file diff --git a/src/SharpEmf/Objects/ColorRef.cs b/src/SharpEmf/Objects/ColorRef.cs index 8a2cb9e..aaef974 100644 --- a/src/SharpEmf/Objects/ColorRef.cs +++ b/src/SharpEmf/Objects/ColorRef.cs @@ -1,4 +1,5 @@ -using JetBrains.Annotations; +using System.Diagnostics; +using JetBrains.Annotations; using SharpEmf.Exceptions; namespace SharpEmf.Objects; @@ -42,11 +43,6 @@ public static ColorRef Parse(Stream stream) var blue = stream.ReadByte(); var reserved = stream.ReadByte(); - if (reserved != 0x00) - { - throw new EmfParseException($"Reserved byte must be 0x00, but was {reserved}"); - } - return new ColorRef((byte)red, (byte)green, (byte)blue, (byte)reserved); } } \ No newline at end of file diff --git a/src/SharpEmf/Objects/LogBrushEx.cs b/src/SharpEmf/Objects/LogBrushEx.cs new file mode 100644 index 0000000..f6134c2 --- /dev/null +++ b/src/SharpEmf/Objects/LogBrushEx.cs @@ -0,0 +1,61 @@ +using JetBrains.Annotations; +using SharpEmf.Enums; +using SharpEmf.Exceptions; +using SharpEmf.Extensions; + +namespace SharpEmf.Objects; + +/// +/// Defines the style, color, and pattern of a device-independent brush +/// +/// +/// The following table shows the relationship between the BrushStyle, Color, and BrushHatch fields: +/// +/// | BrushStyle | Color | BrushHatch +/// |--------------|-----------------------------------------------------|-------------------------------------------------------------| +/// | BS_SOLID | Specifies the color of the brush | Not used and SHOULD be ignored | +/// | BS_NULL | Not used and SHOULD be ignored | Not used and SHOULD be ignored | +/// | BS_HATCHED | Specifies the foreground color of the hatch pattern | Specifies the orientation of lines used to create the hatch | +/// +/// +[PublicAPI] +public readonly struct LogBrushEx +{ + /// + /// Specifies the brush style. + /// Values of this field MUST be , , or + /// + public BrushStyle BrushStyle { get; } + + /// + /// Specifies a color + /// + public ColorRef Color { get; } + + /// + /// Brush hatch data + /// + public HatchStyle HatchStyle { get; } + + private LogBrushEx(BrushStyle brushStyle, ColorRef color, HatchStyle hatchStyle) + { + BrushStyle = brushStyle; + Color = color; + HatchStyle = hatchStyle; + } + + public static LogBrushEx Parse(Stream stream) + { + var brushStyle = stream.ReadEnum(); + + if (brushStyle is not (BrushStyle.BS_SOLID or BrushStyle.BS_HATCHED or BrushStyle.BS_NULL)) + { + throw new EmfParseException($"Invalid {nameof(BrushStyle)} value: {brushStyle}"); + } + + var color = ColorRef.Parse(stream); + var hatchStyle = stream.ReadEnum(); + + return new LogBrushEx(brushStyle, color, hatchStyle); + } +} \ No newline at end of file diff --git a/src/SharpEmf/Records/EnhancedMetafileRecord.cs b/src/SharpEmf/Records/EnhancedMetafileRecord.cs index 5d8b677..48c3974 100644 --- a/src/SharpEmf/Records/EnhancedMetafileRecord.cs +++ b/src/SharpEmf/Records/EnhancedMetafileRecord.cs @@ -8,6 +8,7 @@ using SharpEmf.Records.Control.Header; using SharpEmf.Records.Drawing; using SharpEmf.Records.Escape; +using SharpEmf.Records.ObjectCreation; using SharpEmf.Records.ObjectManipulation; using SharpEmf.Records.PathBracket; using SharpEmf.Records.State; @@ -56,6 +57,7 @@ public static EnhancedMetafileRecord Parse(Stream stream) EmfRecordType.EMR_EXCLUDECLIPRECT => EmrExcludeClipRect.Parse, EmfRecordType.EMR_INTERSECTCLIPRECT => EmrIntersectClipRect.Parse, EmfRecordType.EMR_SELECTOBJECT => EmrSelectObject.Parse, + EmfRecordType.EMR_CREATEBRUSHINDIRECT => EmrCreateBrushIndirect.Parse, EmfRecordType.EMR_DELETEOBJECT => EmrDeleteObject.Parse, EmfRecordType.EMR_ANGLEARC => EmrAngleArc.Parse, EmfRecordType.EMR_ELLIPSE => EmrEllipse.Parse, diff --git a/src/SharpEmf/Records/ObjectCreation/EmrCreateBrushIndirect.cs b/src/SharpEmf/Records/ObjectCreation/EmrCreateBrushIndirect.cs new file mode 100644 index 0000000..40be84d --- /dev/null +++ b/src/SharpEmf/Records/ObjectCreation/EmrCreateBrushIndirect.cs @@ -0,0 +1,39 @@ +using JetBrains.Annotations; +using SharpEmf.Enums; +using SharpEmf.Extensions; +using SharpEmf.Interfaces; +using SharpEmf.Objects; + +namespace SharpEmf.Records.ObjectCreation; + +/// +[PublicAPI] +public record EmrCreateBrushIndirect : EnhancedMetafileRecord, IEmfParsable +{ + /// + /// Specifies the index of the logical brush object in the EMF object table + /// + /// + /// This index is used to refer to the object, so it can be reused or modified + /// + public uint IHBrush { get; } + + /// + /// Specifies the style, color, and pattern of the logical brush + /// + public LogBrushEx LogBrush { get; } + + private EmrCreateBrushIndirect(EmfRecordType Type, uint Size, uint ihBrush, LogBrushEx logBrush) : base(Type, Size) + { + IHBrush = ihBrush; + LogBrush = logBrush; + } + + public static EmrCreateBrushIndirect Parse(Stream stream, EmfRecordType recordType, uint size) + { + var ihBrush = stream.ReadUInt32(); + var logBrush = LogBrushEx.Parse(stream); + + return new EmrCreateBrushIndirect(recordType, size, ihBrush, logBrush); + } +} \ No newline at end of file diff --git a/tests/SharpEmf.UnitTests/SharpEmf.UnitTests.csproj b/tests/SharpEmf.UnitTests/SharpEmf.UnitTests.csproj index ffdc003..34fe91a 100644 --- a/tests/SharpEmf.UnitTests/SharpEmf.UnitTests.csproj +++ b/tests/SharpEmf.UnitTests/SharpEmf.UnitTests.csproj @@ -24,10 +24,7 @@ - - PreserveNewest - - + PreserveNewest