1 /*
2   KeePass Password Safe - The Open-Source Password Manager
3   Copyright (C) 2003-2021 Dominik Reichl <dominik.reichl@t-online.de>
4 
5   This program is free software; you can redistribute it and/or modify
6   it under the terms of the GNU General Public License as published by
7   the Free Software Foundation; either version 2 of the License, or
8   (at your option) any later version.
9 
10   This program is distributed in the hope that it will be useful,
11   but WITHOUT ANY WARRANTY; without even the implied warranty of
12   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13   GNU General Public License for more details.
14 
15   You should have received a copy of the GNU General Public License
16   along with this program; if not, write to the Free Software
17   Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
18 */
19 
20 using System;
21 using System.Collections.Generic;
22 using System.Diagnostics;
23 using System.Drawing;
24 using System.Text;
25 using System.Windows.Forms;
26 
27 namespace KeePass.UI
28 {
29 	public static class FontUtil
30 	{
CreateFont(string strFamily, float fEmSize, FontStyle fs)31 		public static Font CreateFont(string strFamily, float fEmSize, FontStyle fs)
32 		{
33 			return CreateFont(strFamily, fEmSize, fs, GraphicsUnit.Point);
34 		}
35 
CreateFont(string strFamily, float fEmSize, FontStyle fs, GraphicsUnit gu)36 		public static Font CreateFont(string strFamily, float fEmSize, FontStyle fs,
37 			GraphicsUnit gu)
38 		{
39 			try { return new Font(strFamily, fEmSize, fs, gu); }
40 			catch(Exception) { Debug.Assert(false); } // Style unsupported?
41 
42 			return new Font(strFamily, fEmSize, gu); // Regular style
43 		}
44 
CreateFont(FontFamily ff, float fEmSize, FontStyle fs)45 		public static Font CreateFont(FontFamily ff, float fEmSize, FontStyle fs)
46 		{
47 			try { return new Font(ff, fEmSize, fs); }
48 			catch(Exception) { Debug.Assert(false); } // Style unsupported?
49 
50 			return new Font(ff, fEmSize);
51 		}
52 
CreateFont(Font fBase, FontStyle fs)53 		public static Font CreateFont(Font fBase, FontStyle fs)
54 		{
55 			try { return new Font(fBase, fs); }
56 			catch(Exception) { Debug.Assert(false); } // Style unsupported?
57 
58 			return new Font(fBase, fBase.Style); // Clone
59 		}
60 
Assign(Control c, Font f)61 		private static void Assign(Control c, Font f)
62 		{
63 			if(c == null) { Debug.Assert(false); return; }
64 			if(f == null) { Debug.Assert(false); return; }
65 
66 			try
67 			{
68 				using(RtlAwareResizeScope r = new RtlAwareResizeScope(c))
69 				{
70 					c.Font = f;
71 				}
72 			}
73 			catch(Exception) { Debug.Assert(false); }
74 		}
75 
76 		private static Font m_fontDefault = null;
77 		/// <summary>
78 		/// Get the default UI font. This might be <c>null</c>!
79 		/// </summary>
80 		public static Font DefaultFont
81 		{
82 			get { return m_fontDefault; }
83 		}
84 
SetDefaultFont(Control c)85 		public static void SetDefaultFont(Control c)
86 		{
87 			if(c == null) { Debug.Assert(false); return; }
88 
89 			// Allow specifying the default font once only
90 			if(m_fontDefault == null) m_fontDefault = c.Font;
91 		}
92 
AssignDefault(Control c)93 		public static void AssignDefault(Control c)
94 		{
95 			Assign(c, m_fontDefault);
96 		}
97 
98 		private static Font m_fontBold = null;
AssignDefaultBold(Control c)99 		public static void AssignDefaultBold(Control c)
100 		{
101 			if(c == null) { Debug.Assert(false); return; }
102 
103 			if(m_fontBold == null)
104 			{
105 				try { m_fontBold = new Font(c.Font, FontStyle.Bold); }
106 				catch(Exception) { Debug.Assert(false); m_fontBold = c.Font; }
107 			}
108 
109 			Assign(c, m_fontBold);
110 		}
111 
112 		private static Font m_fontItalic = null;
AssignDefaultItalic(Control c)113 		public static void AssignDefaultItalic(Control c)
114 		{
115 			if(c == null) { Debug.Assert(false); return; }
116 
117 			if(m_fontItalic == null)
118 			{
119 				try { m_fontItalic = new Font(c.Font, FontStyle.Italic); }
120 				catch(Exception) { Debug.Assert(false); m_fontItalic = c.Font; }
121 			}
122 
123 			Assign(c, m_fontItalic);
124 		}
125 
126 		private static Font m_fontMono = null;
127 		/// <summary>
128 		/// Get the default UI monospace font. This might be <c>null</c>!
129 		/// </summary>
130 		public static Font MonoFont
131 		{
132 			get { return m_fontMono; }
133 		}
134 
AssignDefaultMono(Control c, bool bIsPasswordBox)135 		public static void AssignDefaultMono(Control c, bool bIsPasswordBox)
136 		{
137 			if(c == null) { Debug.Assert(false); return; }
138 
139 			if(m_fontMono == null)
140 			{
141 				try
142 				{
143 					m_fontMono = new Font(FontFamily.GenericMonospace,
144 						c.Font.SizeInPoints);
145 
146 					Debug.Assert(c.Font.Height == m_fontMono.Height);
147 				}
148 				catch(Exception) { Debug.Assert(false); m_fontMono = c.Font; }
149 			}
150 
151 			if(bIsPasswordBox && Program.Config.UI.PasswordFont.OverrideUIDefault)
152 				Assign(c, Program.Config.UI.PasswordFont.ToFont());
153 			else Assign(c, m_fontMono);
154 		}
155 
156 		/* private const string FontPartsSeparator = @"/:/";
157 
158 		public static Font FontIDToFont(string strFontID)
159 		{
160 			Debug.Assert(strFontID != null); if(strFontID == null) return null;
161 
162 			string[] vParts = strFontID.Split(new string[] { FontPartsSeparator },
163 				StringSplitOptions.None);
164 			if((vParts == null) || (vParts.Length != 6)) return null;
165 
166 			float fSize;
167 			if(!float.TryParse(vParts[1], out fSize)) { Debug.Assert(false); return null; }
168 
169 			FontStyle fs = FontStyle.Regular;
170 			if(vParts[2] == "1") fs |= FontStyle.Bold;
171 			if(vParts[3] == "1") fs |= FontStyle.Italic;
172 			if(vParts[4] == "1") fs |= FontStyle.Underline;
173 			if(vParts[5] == "1") fs |= FontStyle.Strikeout;
174 
175 			return FontUtil.CreateFont(vParts[0], fSize, fs);
176 		}
177 
178 		public static string FontToFontID(Font f)
179 		{
180 			Debug.Assert(f != null); if(f == null) return string.Empty;
181 
182 			StringBuilder sb = new StringBuilder();
183 
184 			sb.Append(f.Name);
185 			sb.Append(FontPartsSeparator);
186 			sb.Append(f.SizeInPoints.ToString());
187 			sb.Append(FontPartsSeparator);
188 			sb.Append(f.Bold ? "1" : "0");
189 			sb.Append(FontPartsSeparator);
190 			sb.Append(f.Italic ? "1" : "0");
191 			sb.Append(FontPartsSeparator);
192 			sb.Append(f.Underline ? "1" : "0");
193 			sb.Append(FontPartsSeparator);
194 			sb.Append(f.Strikeout ? "1" : "0");
195 
196 			return sb.ToString();
197 		} */
198 	}
199 }
200