<commit_before>using System;
using System.Collections.Generic;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using GeoAPI.Geometries;
using NetTopologySuite.Geometries;
using NUnit.Framework;
using SharpMap;
using SharpMap.Data;
using SharpMap.Data.Providers;
using SharpMap.Layers;
using SharpMap.Rendering.Decoration;
using LabelStyle = SharpMap.Styles.LabelStyle;

namespace UnitTests.Layers
{
    [TestFixture, Category("RequiresWindows")]
    public class Layer_AffectedAreaTest
    {
        public enum LabelLayerMode
        {
            BasicLabel,
            BasicLabelRot,
            TextOnPath,
            PathOnLabel,
            SineCurve,
            SineCurveClipped,
            SineCurveExtended
        }

        public enum SymbolizerMode
        {
            Rps,
            Cps,
            Pps,
            Lps
        }

        public enum PointAlignment
        {
            Horizontal,
            Vertical,
            Diagonal
        }

        private readonly float[] _rotations = new float[]
            {0f, 30f, 60f, 90f, 120f, 150f, 180f, 210f, 240f, 270f, 310f, 330f};

        /// <summary>
        /// Validate calculated affectedArea on 3 primary code paths of LabelLayer
        /// </summary>
        /// <param name="mode"></param>
        /// <param name="testRotations"></param>
        [NUnit.Framework.TestCase(LabelLayerMode.BasicLabel, true)]
        [NUnit.Framework.TestCase(LabelLayerMode.BasicLabelRot, true)]
        [NUnit.Framework.TestCase(LabelLayerMode.TextOnPath, true)]
        [NUnit.Framework.TestCase(LabelLayerMode.PathOnLabel, true)]
        public void LabelLayer_AffectedArea(LabelLayerMode mode, bool testRotations)
        {
            using (var map = new Map())
            {
                ConfigureMap(map);

                Envelope extents = null;
                switch (mode)
                {
                    case LabelLayerMode.BasicLabel:
                    case LabelLayerMode.BasicLabelRot:
                        AddBasicLabelLayers(map, mode);
                        extents = map.GetExtents();
                        extents.ExpandBy(0.125);
                        break;
                    case LabelLayerMode.TextOnPath:
                        AddTextOnPathLayers(map);
                        extents = map.GetExtents();
                        break;
                    case LabelLayerMode.PathOnLabel:
                        AddPathOnLabelLayers(map);
                        extents = map.GetExtents();
                        extents.ExpandBy(0.125);
                        break;
                }

                foreach (var rot in _rotations)
                {
                    SetMapTransform(map, rot);
                    map.ZoomToBox(extents, true);

                    var affectedArea = GetAffectedArea(map, (Layer) map.Layers[1]);
                    AddAffectedAreaLayer(map, affectedArea);

                    using (var img = map.GetMap())
                        img.Save(
                            Path.Combine(UnitTestsFixture.GetImageDirectory(this),
                                $"LabelLayer_{mode}_{rot:000}.png"),
                            System.Drawing.Imaging.ImageFormat.Png);

                    // remove affected area layer
                    map.Layers.RemoveAt(2);
                    if (!testRotations) break;
                }
            }
        }

        private void ConfigureMap(Map map)
        {
            map.Size = new Size(800, 640);
            map.BackColor = Color.AliceBlue;
            map.SRID = 4326;
            map.Decorations.Add(new NorthArrow());
            map.Decorations.Add(new Disclaimer()
            {
                Text = "Affected Area envelope should surround label"
            });
        }

        private void SetMapTransform(Map map, float rotationDeg)
        {
            if (rotationDeg.Equals(0f))
            {
                map.MapTransform = null;
            }
            else
            {
                var matrix = new System.Drawing.Drawing2D.Matrix();
                matrix.RotateAt(rotationDeg, new PointF(map.Size.Width * 0.5f, map.Size.Height * 0.5f));
                map.MapTransform = matrix;
            }
        }

        private Envelope GetAffectedArea(Map map, Layer layer)
        {
            using (var img = new Bitmap(map.Size.Width, map.Size.Height))
            using (var g = Graphics.FromImage(img))
            {
                layer.Render(g, (MapViewport) map, out var affectedArea);
                return affectedArea;
            }
        }

        private void AddAffectedAreaLayer(Map map, Envelope affectedAreaEnv)
        {
            var coords = new Coordinate[]
            {
                new Coordinate(affectedAreaEnv.TopLeft()),
                new Coordinate(affectedAreaEnv.TopRight()),
                new Coordinate(affectedAreaEnv.BottomRight()),
                new Coordinate(affectedAreaEnv.BottomLeft()),
                new Coordinate(affectedAreaEnv.TopLeft())
            };
            
            var gp = new GeometryProvider(new Polygon(new LinearRing(coords)));
            var vLayer = new VectorLayer("Affected Area")
            {
                DataSource = gp,
                SRID = map.SRID
            };
            vLayer.Style.Fill = null;
            vLayer.Style.EnableOutline = true;

            map.Layers.Add(vLayer);
        }

        private void AddBasicLabelLayers(Map map, LabelLayerMode mode)<commit_msg>Update UnitTest for ILayerEx signature change<commit_after>using System;
using System.Collections.Generic;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using GeoAPI.Geometries;
using NetTopologySuite.Geometries;
using NUnit.Framework;
using SharpMap;
using SharpMap.Data;
using SharpMap.Data.Providers;
using SharpMap.Layers;
using SharpMap.Rendering.Decoration;
using LabelStyle = SharpMap.Styles.LabelStyle;

namespace UnitTests.Layers
{
    [TestFixture, Category("RequiresWindows")]
    public class Layer_AffectedAreaTest
    {
        public enum LabelLayerMode
        {
            BasicLabel,
            BasicLabelRot,
            TextOnPath,
            PathOnLabel,
            SineCurve,
            SineCurveClipped,
            SineCurveExtended
        }

        public enum SymbolizerMode
        {
            Rps,
            Cps,
            Pps,
            Lps
        }

        public enum PointAlignment
        {
            Horizontal,
            Vertical,
            Diagonal
        }

        private readonly float[] _rotations = new float[]
            {0f, 30f, 60f, 90f, 120f, 150f, 180f, 210f, 240f, 270f, 310f, 330f};

        /// <summary>
        /// Validate calculated affectedArea on 3 primary code paths of LabelLayer
        /// </summary>
        /// <param name="mode"></param>
        /// <param name="testRotations"></param>
        [NUnit.Framework.TestCase(LabelLayerMode.BasicLabel, true)]
        [NUnit.Framework.TestCase(LabelLayerMode.BasicLabelRot, true)]
        [NUnit.Framework.TestCase(LabelLayerMode.TextOnPath, true)]
        [NUnit.Framework.TestCase(LabelLayerMode.PathOnLabel, true)]
        public void LabelLayer_AffectedArea(LabelLayerMode mode, bool testRotations)
        {
            using (var map = new Map())
            {
                ConfigureMap(map);

                Envelope extents = null;
                switch (mode)
                {
                    case LabelLayerMode.BasicLabel:
                    case LabelLayerMode.BasicLabelRot:
                        AddBasicLabelLayers(map, mode);
                        extents = map.GetExtents();
                        extents.ExpandBy(0.125);
                        break;
                    case LabelLayerMode.TextOnPath:
                        AddTextOnPathLayers(map);
                        extents = map.GetExtents();
                        break;
                    case LabelLayerMode.PathOnLabel:
                        AddPathOnLabelLayers(map);
                        extents = map.GetExtents();
                        extents.ExpandBy(0.125);
                        break;
                }

                foreach (var rot in _rotations)
                {
                    SetMapTransform(map, rot);
                    map.ZoomToBox(extents, true);

                    var affectedArea = GetAffectedArea(map, (Layer) map.Layers[1]);
                    AddAffectedAreaLayer(map, affectedArea);

                    using (var img = map.GetMap())
                        img.Save(
                            Path.Combine(UnitTestsFixture.GetImageDirectory(this),
                                $"LabelLayer_{mode}_{rot:000}.png"),
                            System.Drawing.Imaging.ImageFormat.Png);

                    // remove affected area layer
                    map.Layers.RemoveAt(2);
                    if (!testRotations) break;
                }
            }
        }

        private void ConfigureMap(Map map)
        {
            map.Size = new Size(800, 640);
            map.BackColor = Color.AliceBlue;
            map.SRID = 4326;
            map.Decorations.Add(new NorthArrow());
            map.Decorations.Add(new Disclaimer()
            {
                Text = "Affected Area envelope should surround label"
            });
        }

        private void SetMapTransform(Map map, float rotationDeg)
        {
            if (rotationDeg.Equals(0f))
            {
                map.MapTransform = null;
            }
            else
            {
                var matrix = new System.Drawing.Drawing2D.Matrix();
                matrix.RotateAt(rotationDeg, new PointF(map.Size.Width * 0.5f, map.Size.Height * 0.5f));
                map.MapTransform = matrix;
            }
        }

        private Envelope GetAffectedArea(Map map, Layer layer)
        {
            using (var img = new Bitmap(map.Size.Width, map.Size.Height))
            using (var g = Graphics.FromImage(img))
            {
//                layer.Render(g, (MapViewport) map, out var affectedArea);
//                return affectedArea;
                return ((ILayerEx) layer).Render(g, (MapViewport)map);
            }
        }

        private void AddAffectedAreaLayer(Map map, Envelope affectedAreaEnv)
        {
            var coords = new Coordinate[]
            {
                new Coordinate(affectedAreaEnv.TopLeft()),
                new Coordinate(affectedAreaEnv.TopRight()),
                new Coordinate(affectedAreaEnv.BottomRight()),
                new Coordinate(affectedAreaEnv.BottomLeft()),
                new Coordinate(affectedAreaEnv.TopLeft())
            };
            
            var gp = new GeometryProvider(new Polygon(new LinearRing(coords)));
            var vLayer = new VectorLayer("Affected Area")
            {
                DataSource = gp,
                SRID = map.SRID
            };
            vLayer.Style.Fill = null;
            vLayer.Style.EnableOutline = true;

            map.Layers.Add(vLayer);
        }

        private void AddBasicLabelLayers(Map map, LabelLayerMode mode)
{'fraction_non_alphanumeric': 0.08573967125721901, 'fraction_numerical': 0.01083962683251888, 'mean_word_length': 1.4263849967665445, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4727966', 'n_tokens_mistral': 3109, 'n_tokens_neox': 2800, 'n_words': 594}