<commit_before>﻿using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Threading;

namespace PreviewIo
{
	internal class PreviewContext
	{
		public event EventHandler ViewPortChanged;
		public event EventHandler PreviewRequired;

		public Rectangle ViewPort { get; private set; }
		public bool DisplayPreview { get; private set; }
		public Stream FileStream { get; private set; }
		public PreviewSettings Settings { get; }
		public CancellationTokenSource TokenSource { get; private set; }
		public Size? DrawingSize { get; set; }

		public PreviewContext()
		{
			TokenSource = new CancellationTokenSource();
			Settings = new PreviewSettings
			{
				RenderingFormat = ImageFormat.Png,
				UpScaleForPrint = 4,
			};
		}

		public void OnViewPortChanged(Rectangle newSize)
		{
			ViewPort = newSize;

			ViewPortChanged?.Invoke(this, EventArgs.Empty);
		}

		public void OnPreviewRequired(Stream stream)
		{
			TokenSource.Cancel();
			TokenSource = new CancellationTokenSource();
			FileStream = stream;
			DisplayPreview = true;
			PreviewRequired?.Invoke(this, EventArgs.Empty);
		}

		public Size GetPreviewSize()
		{
			return _IncreaseSizeForPrint(DrawingSize) ?? Size.Empty;
		}

		private Size? _IncreaseSizeForPrint(Size? drawingSize)
		{
			if (drawingSize == null)
				return null;

			var size = drawingSize.Value;
			return new Size(size.Width * Settings.UpScaleForPrint, size.Height * Settings.UpScaleForPrint);
		}

		public void RecalculateDrawingSize(Size upscaledPreviewSize)
		{
			var actualSize = new Size(
				upscaledPreviewSize.Width / Settings.UpScaleForPrint,
				upscaledPreviewSize.Height / Settings.UpScaleForPrint);
			DrawingSize = actualSize;
		}
	}
}<commit_msg>PreviewContext: Updated to adjust DrawingSize when the aspect ratios are incorrect; this could result in a preview being over-downscaled from the actual preview image<commit_after>﻿using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Threading;

namespace PreviewIo
{
	internal class PreviewContext
	{
		public event EventHandler ViewPortChanged;
		public event EventHandler PreviewRequired;

		public Rectangle ViewPort { get; private set; }
		public bool DisplayPreview { get; private set; }
		public Stream FileStream { get; private set; }
		public PreviewSettings Settings { get; }
		public CancellationTokenSource TokenSource { get; private set; }
		public Size? DrawingSize { get; set; }

		public PreviewContext()
		{
			TokenSource = new CancellationTokenSource();
			Settings = new PreviewSettings();
		}

		public void OnViewPortChanged(Rectangle newSize)
		{
			ViewPort = newSize;

			ViewPortChanged?.Invoke(this, EventArgs.Empty);
		}

		public void OnPreviewRequired(Stream stream)
		{
			TokenSource.Cancel();
			TokenSource = new CancellationTokenSource();
			FileStream = stream;
			DisplayPreview = true;
			PreviewRequired?.Invoke(this, EventArgs.Empty);
		}

		public Size GetPreviewSize()
		{
			return _IncreaseSizeForPrint(DrawingSize) ?? Size.Empty;
		}

		private Size? _IncreaseSizeForPrint(Size? drawingSize)
		{
			if (drawingSize == null)
				return null;

			var size = drawingSize.Value;
			return new Size(size.Width * Settings.UpScaleForPrint, size.Height * Settings.UpScaleForPrint);
		}

		public void RecalculateDrawingSize(Size upscaledPreviewSize)
		{
			var actualSize = new Size(
				upscaledPreviewSize.Width / Settings.UpScaleForPrint,
				upscaledPreviewSize.Height / Settings.UpScaleForPrint);

			var previousDrawingSize = DrawingSize;
			DrawingSize = actualSize;

			if (previousDrawingSize == null)
				return;

			//work out the actual scale of the preview compared to the requested size
			var scale = new SizeF(
				((float)actualSize.Width / previousDrawingSize.Value.Width) * Settings.UpScaleForPrint,
				((float)actualSize.Height / previousDrawingSize.Value.Height) * Settings.UpScaleForPrint);

			var mostAppropriateScale = _GetMostAppropriateScale(scale);

			//reset the drawing size to that of the preview
			DrawingSize = new Size(
				upscaledPreviewSize.Width / (int)mostAppropriateScale,
				upscaledPreviewSize.Height / (int)mostAppropriateScale);
		}

		private float _GetMostAppropriateScale(SizeF scale)
		{
			var widthScale = Math.Abs(Settings.UpScaleForPrint - scale.Width);
			var heightScale = Math.Abs(Settings.UpScaleForPrint - scale.Height);

			if (widthScale < heightScale)
				return scale.Height;

			return scale.Width;
		}
	}
}
{'fraction_non_alphanumeric': 0.08327766644399912, 'fraction_numerical': 0.00022266755733689602, 'mean_word_length': 4.266119577960141, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 3, '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': '6689186', 'n_tokens_mistral': 1502, 'n_tokens_neox': 1300, 'n_words': 343}