// Spludlow Software // Copyright © Samuel P. Ludlow 2020 All Rights Reserved // Distributed under the terms of the GNU General Public License version 3 // Distributed WITHOUT ANY WARRANTY; without implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE // https://www.spludlow.co.uk/LICENCE.TXT // The Spludlow logo is a registered trademark of Samuel P. Ludlow and may not be used without permission // v1.14 using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Data; using System.Drawing; namespace Spludlow.Printing { public class PageSizes { public static DataTable PageSizeTable = Spludlow.Data.TextTable.ReadText(new string[] { "Name Short Long", "String* Single Single", "A8 52 74", "BC 55 85", "A7 74 105", "A6 105 148", "DL 99 210", "Junior 127 203", "Memo 140 216", "A5 148 210", "Letter 216 279", "A4 210 297", "RA4 215 305", "SRA4 225 320", "Legal 216 356", "Tabloid 279 432", "A3 297 420", "RA3 305 430", "SRA3 320 450", "A2 420 594", "RA2 430 610", "SRA2 450 640", "A1 594 841", "RA1 610 860", "SRA1 640 900", "A0 841 1189", "RA0 860 1220", "SRA0 900 1280", "2A0 1189 1682", "4A0 1682 2378", }); public static Dictionary PageSizeLookup; public static Dictionary PageSizeNameLookup; static PageSizes() { PageSizeLookup = new Dictionary(); PageSizeNameLookup = new Dictionary(); foreach (DataRow row in PageSizeTable.Rows) { string name = (string)row["Name"]; float shortLength = (float)row["Short"]; float longLength = (float)row["Long"]; PageSizeLookup.Add(name, new SizeF(shortLength, longLength)); PageSizeNameLookup.Add(shortLength + " X " + longLength, name); } } public const float PointsInMm = 2.83464567F; public static float PointsToMm(float points) { return points / PointsInMm; } public static float MmToPoints(float mm) { return mm * PointsInMm; } public static void LogPageSizes() { DataTable table = PageSizeTable.Copy(); table.Columns.Add("AreaMM", typeof(Single)); table.Columns.Add("AreaCM", typeof(Single)); table.Columns.Add("AreaM", typeof(Single)); foreach (DataRow row in table.Rows) { float shortLength = (float)row["Short"]; float longLength = (float)row["Long"]; float mm = shortLength * longLength; row["AreaMM"] = mm; row["AreaCM"] = mm / 100; row["AreaM"] = mm / 1000000; } DataView view = new DataView(table); view.Sort = "AreaMM"; Spludlow.Log.Report("Page Sizes", view); } public static float MillimetersToDpi(float mm, int targetPixles) { decimal inches = (decimal)mm * 0.0393700787M; decimal dpi = targetPixles / inches; return (float)Math.Round(dpi, 1); } public static int MillimetersToPixels(float mm, float dpi) { decimal inches = (decimal)mm * 0.0393700787M; decimal pixels = inches * (decimal)dpi; pixels = Math.Round(pixels, 0); return (int)pixels; } public static SizeF PixelsToMillimeters(Size pixels, float dpi) { float width = PixelsToMillimeters(pixels.Width, dpi); float height = PixelsToMillimeters(pixels.Height, dpi); return new SizeF(width, height); } public static float PixelsToMillimeters(int pixels, float dpi) { decimal inches = (decimal)pixels / (decimal)dpi; decimal mm = inches * 25.4M; mm = Math.Round(mm, 3); return (float)mm; } public static Size PageSizePixels(string isoPaperSize, int dpi) { bool landscape = false; if (isoPaperSize.EndsWith("*") == true) { isoPaperSize = isoPaperSize.Substring(0, isoPaperSize.Length - 1); landscape = true; } return PageSizePixels(isoPaperSize, landscape, dpi); } public static Size PageSizePixels(string isoPaperSize, bool landscape, int dpi) { SizeF sizeMm = PageSizeMillimeters(isoPaperSize, landscape); int width = MillimetersToPixels(sizeMm.Width, dpi); int height = MillimetersToPixels(sizeMm.Height, dpi); return new Size(width, height); } public static SizeF PageSizeMillimeters(string isoPaperSize) { bool landscape = false; if (isoPaperSize.EndsWith("*") == true) { isoPaperSize = isoPaperSize.Substring(0, isoPaperSize.Length - 1); landscape = true; } return PageSizeMillimeters(isoPaperSize, landscape); } public static SizeF PageSizeMillimeters(string isoPaperSize, bool landscape) { isoPaperSize = isoPaperSize.ToUpper(); if (isoPaperSize.Contains("X") == true) { string[] parts = Spludlow.Text.Split(isoPaperSize, 'X'); if (parts.Length != 2) throw new ApplicationException("Paper size bad dimensions: " + isoPaperSize); SizeF size = new SizeF(Single.Parse(parts[0]), Single.Parse(parts[1])); if (landscape == true) return SetAspect(size, true); return size; } if (PageSizeLookup.ContainsKey(isoPaperSize) == false) throw new ApplicationException("Paper Size not found: " + isoPaperSize); SizeF sourceSize = PageSizeLookup[isoPaperSize]; if (landscape == false) return new SizeF(sourceSize.Width, sourceSize.Height); else return new SizeF(sourceSize.Height, sourceSize.Width); } public static SizeF SetAspect(SizeF size, bool landscape) { float shortLength = Math.Min(size.Width, size.Height); float longLength = Math.Max(size.Width, size.Height); if (landscape == false) return new SizeF(shortLength, longLength); else return new SizeF(longLength, shortLength); } public static string PaperName(float width, float height) { return PaperName(new SizeF(width, height)); } public static string PaperName(SizeF size) // Always retuens "short X long" for cusom with * if landacpe { bool landscape = false; if (size.Width > size.Height) landscape = true; SizeF portaitSize = SetAspect(size, false); string key = portaitSize.Width + " X " + portaitSize.Height; if (PageSizeNameLookup.ContainsKey(key) == true) key = PageSizeNameLookup[key]; return key + (landscape == true ? "*" : ""); } } }