<commit_before>﻿// Copyright (c) <NAME>. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
using PdfSharp;
using PdfSharp.Drawing;
using PdfSharp.Pdf;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Test
{
    public class PdfRenderer : Test2d.IRenderer
    {
        public bool DrawPoints { get; set; }
        public double Zoom { get; set; }

        private Func<double, double> _scaleToPage;

        public void Save(string path, Test2d.Container container)
        {
            using (var doc = new PdfDocument())
            {
                Add(doc, container);
                doc.Save(path);
            }
        }

        public void Save(string path, IEnumerable<Test2d.Container> containers)
        {
            using (var doc = new PdfDocument())
            {
                foreach (var page in containers)
                {
                    Add(doc, page);
                }
                doc.Save(path);
            }
        }

        private void Add(PdfDocument doc, Test2d.Container container)
        {
            // create A4 page with landscape orientation
            PdfPage page = doc.AddPage();
            page.Size = PageSize.A4;
            page.Orientation = PageOrientation.Landscape;

            using (XGraphics gfx = XGraphics.FromPdfPage(page))
            {
                // calculate x and y page scale factors
                double scaleX = page.Width.Value / container.Width;
                double scaleY = page.Height.Value / container.Height;
                double scale = Math.Min(scaleX, scaleY);

                // set scaling function
                _scaleToPage = (value) => value * scale;

                // draw block contents to pdf graphics
                Draw(gfx, container);
            }
        }

        private static XColor ToXColor(Test2d.ArgbColor color)
        {
            return XColor.FromArgb(
                color.A,
                color.R,
                color.G,
                color.B);
        }

        private static XPen ToXPen(Test2d.ShapeStyle style, Func<double, double> scale)
        {
            return new XPen(
                ToXColor(style.Stroke),
                scale(style.Thickness))
            {
                LineCap = XLineCap.Flat
            };
        }

        private static XSolidBrush ToXSolidBrush(Test2d.ArgbColor color)
        {
            return new XSolidBrush(ToXColor(color));
        }

        private static System.Windows.Rect CreateRect(Test2d.XPoint tl, Test2d.XPoint br, double dx, double dy)
        {
            double tlx = Math.Min(tl.X, br.X);
            double tly = Math.Min(tl.Y, br.Y);
            double brx = Math.Max(tl.X, br.X);
            double bry = Math.Max(tl.Y, br.Y);
            return new System.Windows.Rect(
                new System.Windows.Point(tlx + dx, tly + dy),
                new System.Windows.Point(brx + dx, bry + dy));
        }

        public void ClearCache()
        {
        }

        public void Draw(object gfx, Test2d.Container container)
        {
            if (container.TemplateLayer.IsVisible)
            {
                Draw(gfx, container.TemplateLayer);
            }

            foreach (var layer in container.Layers)
            {
                if (layer.IsVisible)
                {
                    Draw(gfx, layer);
                }
            }
        }

        public void Draw(object gfx, Test2d.Layer layer)
        {
            foreach (var shape in layer.Shapes)
            {
                shape.Draw(gfx, this, 0, 0);
            }
        }

        private static void DrawLineInternal(
            XGraphics gfx,
            XPen pen,
            ref XPoint p0,
            ref XPoint p1)
        {
            gfx.DrawLine(pen, p0, p1);
        }

        private static void DrawRectangleInternal(
            XGraphics gfx,
            XSolidBrush brush,
            XPen pen,
            bool isFilled,
            ref XRect rect)
        {
            if (isFilled)
            {
                gfx.DrawRectangle(pen, brush, rect);
            }
            else
            {
                gfx.DrawRectangle(pen, rect);
            }
        }

        private static void DrawEllipseInternal(
            XGraphics gfx,
            XSolidBrush brush,
            XPen pen,
            bool isFilled,
            ref XRect rect)
        {
            if (isFilled)
            {
                gfx.DrawEllipse(pen, brush, rect);
            }
            else
            {
                gfx.DrawEllipse(pen, rect);
            }
        }

        public void Draw(object gfx, Test2d.XLine line, double dx, double dy)
        {
            var _gfx = gfx as XGraphics;

            XSolidBrush fill = ToXSolidBrush(line.Style.Fill);
            XPen stroke = ToXPen(line.Style, _scaleToPage);

            double x1 = _scaleToPage(line.Start.X + dx);
            double y1 = _scaleToPage(line.Start.Y + dy);
            double x2 = _scaleToPage(line.End.X + dx);
            double y2 = _scaleToPage(line.End.Y + dy);

            var sas = line.Style.LineStyle.StartArrowStyle;
            var eas = line.Style.LineStyle.EndArrowStyle;
            double a1 = Math.Atan2(y1 - y2, x1 - x2) * 180.0 / Math.PI;
            double a2 = Math.Atan2(y2 - y1, x2 - x1) * 180.0 / Math.PI;

            var t1 = new XMatrix();
            var c1 = new XPoint(x1, y1);
            t1.RotateAtPrepend(a1, c1);

            var t2 = new XMatrix();<commit_msg>Moved methods<commit_after>﻿// Copyright (c) <NAME>. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
using PdfSharp;
using PdfSharp.Drawing;
using PdfSharp.Pdf;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Test
{
    public class PdfRenderer : Test2d.IRenderer
    {
        public bool DrawPoints { get; set; }
        public double Zoom { get; set; }

        private Func<double, double> _scaleToPage;

        public void Save(string path, Test2d.Container container)
        {
            using (var doc = new PdfDocument())
            {
                Add(doc, container);
                doc.Save(path);
            }
        }

        public void Save(string path, IEnumerable<Test2d.Container> containers)
        {
            using (var doc = new PdfDocument())
            {
                foreach (var page in containers)
                {
                    Add(doc, page);
                }
                doc.Save(path);
            }
        }

        private void Add(PdfDocument doc, Test2d.Container container)
        {
            // create A4 page with landscape orientation
            PdfPage page = doc.AddPage();
            page.Size = PageSize.A4;
            page.Orientation = PageOrientation.Landscape;

            using (XGraphics gfx = XGraphics.FromPdfPage(page))
            {
                // calculate x and y page scale factors
                double scaleX = page.Width.Value / container.Width;
                double scaleY = page.Height.Value / container.Height;
                double scale = Math.Min(scaleX, scaleY);

                // set scaling function
                _scaleToPage = (value) => value * scale;

                // draw block contents to pdf graphics
                Draw(gfx, container);
            }
        }

        private static XColor ToXColor(Test2d.ArgbColor color)
        {
            return XColor.FromArgb(
                color.A,
                color.R,
                color.G,
                color.B);
        }

        private static XPen ToXPen(Test2d.ShapeStyle style, Func<double, double> scale)
        {
            return new XPen(
                ToXColor(style.Stroke),
                scale(style.Thickness))
            {
                LineCap = XLineCap.Flat
            };
        }

        private static XSolidBrush ToXSolidBrush(Test2d.ArgbColor color)
        {
            return new XSolidBrush(ToXColor(color));
        }

        private static System.Windows.Rect CreateRect(Test2d.XPoint tl, Test2d.XPoint br, double dx, double dy)
        {
            double tlx = Math.Min(tl.X, br.X);
            double tly = Math.Min(tl.Y, br.Y);
            double brx = Math.Max(tl.X, br.X);
            double bry = Math.Max(tl.Y, br.Y);
            return new System.Windows.Rect(
                new System.Windows.Point(tlx + dx, tly + dy),
                new System.Windows.Point(brx + dx, bry + dy));
        }

        private static void DrawLineInternal(
            XGraphics gfx,
            XPen pen,
            ref XPoint p0,
            ref XPoint p1)
        {
            gfx.DrawLine(pen, p0, p1);
        }

        private static void DrawRectangleInternal(
            XGraphics gfx,
            XSolidBrush brush,
            XPen pen,
            bool isFilled,
            ref XRect rect)
        {
            if (isFilled)
            {
                gfx.DrawRectangle(pen, brush, rect);
            }
            else
            {
                gfx.DrawRectangle(pen, rect);
            }
        }

        private static void DrawEllipseInternal(
            XGraphics gfx,
            XSolidBrush brush,
            XPen pen,
            bool isFilled,
            ref XRect rect)
        {
            if (isFilled)
            {
                gfx.DrawEllipse(pen, brush, rect);
            }
            else
            {
                gfx.DrawEllipse(pen, rect);
            }
        }

        public void ClearCache()
        {
        }

        public void Draw(object gfx, Test2d.Container container)
        {
            if (container.TemplateLayer.IsVisible)
            {
                Draw(gfx, container.TemplateLayer);
            }

            foreach (var layer in container.Layers)
            {
                if (layer.IsVisible)
                {
                    Draw(gfx, layer);
                }
            }
        }

        public void Draw(object gfx, Test2d.Layer layer)
        {
            foreach (var shape in layer.Shapes)
            {
                shape.Draw(gfx, this, 0, 0);
            }
        }

        public void Draw(object gfx, Test2d.XLine line, double dx, double dy)
        {
            var _gfx = gfx as XGraphics;

            XSolidBrush fill = ToXSolidBrush(line.Style.Fill);
            XPen stroke = ToXPen(line.Style, _scaleToPage);

            double x1 = _scaleToPage(line.Start.X + dx);
            double y1 = _scaleToPage(line.Start.Y + dy);
            double x2 = _scaleToPage(line.End.X + dx);
            double y2 = _scaleToPage(line.End.Y + dy);

            var sas = line.Style.LineStyle.StartArrowStyle;
            var eas = line.Style.LineStyle.EndArrowStyle;
            double a1 = Math.Atan2(y1 - y2, x1 - x2) * 180.0 / Math.PI;
            double a2 = Math.Atan2(y2 - y1, x2 - x1) * 180.0 / Math.PI;

            var t1 = new XMatrix();
            var c1 = new XPoint(x1, y1);
            t1.RotateAtPrepend(a1, c1);

            var t2 = new XMatrix();
{'fraction_non_alphanumeric': 0.08638049814520403, 'fraction_numerical': 0.009185656244479774, 'mean_word_length': 1.3014227642276424, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 13, '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': '4527041', 'n_tokens_mistral': 3281, 'n_tokens_neox': 3114, 'n_words': 796}