using RothenburgAR.Common; using System.Collections; using System.Collections.Generic; using UnityEngine; namespace RothenburgAR.UI { public class FontsizeManager : Singleton { public FontsizeSetting CurrentFontsizeSetting = FontsizeSetting.Default; private const float InchPerPt = 1f / 72f; private const float PtPerPixel = 0.75f; // font size in pt relative to dpi specific logical inch private readonly Dictionary> sizes = new Dictionary> { { FontsizeSetting.Default, new Dictionary { {FontsizeClass.Header, 40f}, {FontsizeClass.Body, 25f}, {FontsizeClass.ExhibitTitle, 1f} } }, { FontsizeSetting.Large, new Dictionary { {FontsizeClass.Header, 48}, {FontsizeClass.Body, 30f}, {FontsizeClass.ExhibitTitle, 1.2f} } } }; public float GetFontsize(FontsizeClass sizeClass, FontsizeSetting sizeSetting) { return sizes[sizeSetting][sizeClass]; } /// /// Calculates font size that is roughly equal in size on "all" displays /// public float GetFontsize(FontsizeClass sizeClass) { return GetFontsize(sizeClass, CurrentFontsizeSetting); var dpi = Screen.dpi; var res = Screen.currentResolution; //TODO remove, just a hack to get readable fonts in the unity preview window dpi = dpi == 96 ? 320 : dpi; // Screen diagonal in px var diagonal = Mathf.Sqrt(Mathf.Pow(res.height, 2) + Mathf.Pow(res.width, 2)); // Screen diagonal in logical inches var diagonalIn = diagonal / dpi; // ratio of font size to screen size var ratio = sizes[CurrentFontsizeSetting][sizeClass] * InchPerPt / diagonalIn; // final font size in pt var fontsizePt = diagonal * ratio * PtPerPixel; return fontsizePt; } } public enum FontsizeSetting { Default, Large } public enum FontsizeClass { Header, Body, ExhibitTitle } }