Skip to content

Commit

Permalink
New Font. Font is now embedded. Other changes.
Browse files Browse the repository at this point in the history
Other:
* Moved settings.ini to user's local app data directory
* Clickable Github link in settings
  • Loading branch information
phaselden committed Jan 21, 2021
1 parent bc7f8c2 commit 58c606a
Show file tree
Hide file tree
Showing 12 changed files with 278 additions and 191 deletions.
2 changes: 2 additions & 0 deletions src/FlipIt/FlipIt.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,8 @@
<DependentUpon>Settings.settings</DependentUpon>
<DesignTimeSharedInput>True</DesignTimeSharedInput>
</Compile>
<None Include="res\HelveticaLTStd-BoldCond.otf" />
<None Include="res\HelveticaLTStd-Cond.otf" />
</ItemGroup>
<ItemGroup>
<BootstrapperPackage Include=".NETFramework,Version=v4.0,Profile=Client">
Expand Down
43 changes: 22 additions & 21 deletions src/FlipIt/MainForm.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

91 changes: 76 additions & 15 deletions src/FlipIt/MainForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public partial class MainForm : Form
[DllImport("user32.dll")]
static extern bool GetClientRect(IntPtr hWnd, out Rectangle lpRect);

#endregion
#endregion

private const int splitWidth = 4;
private const int cityBoxSplitWidth = 2;
Expand All @@ -52,17 +52,29 @@ public partial class MainForm : Form
private Font smallCityFont;
private Font primarySmallFont;
private Graphics graphics;
private PrivateFontCollection _pfc;
private FontFamily _fontFamily;

// Alternative fonts:
// * league-gothic from https://github.com/theleagueof/league-gothic
// * http://tipotype.com/aileron/

private const string fontFamilyName = "Oswald"; //"Texgyreheroscn";"Bebas"
private FontFamily FontFamily
{
get
{
if (_fontFamily == null)
{
if (_pfc == null)
{
_pfc = InitFonts();
}
_fontFamily = _pfc.Families[0];
}
return _fontFamily ?? (_fontFamily = _pfc.Families[0]);
}
}

private Graphics Gfx => graphics ?? (graphics = CreateGraphics());
private Graphics Gfx => graphics ?? (graphics = CreateGraphics());

private Font PrimaryFont => primaryFont ?? (primaryFont = new Font(fontFamilyName, fontSize, FontStyle.Bold));
private Font PrimarySmallFont => primarySmallFont ?? (primarySmallFont = new Font(fontFamilyName, fontSize / 9, FontStyle.Bold));
private Font PrimaryFont => primaryFont ?? (primaryFont = new Font(FontFamily, fontSize, FontStyle.Bold));
private Font PrimarySmallFont => primarySmallFont ?? (primarySmallFont = new Font(FontFamily, fontSize / 9, FontStyle.Bold));

private static readonly Color backColorTop = Color.FromArgb(255, 15, 15, 15);
private static readonly Color backColorBottom = Color.FromArgb(255, 10, 10, 10);
Expand Down Expand Up @@ -97,10 +109,13 @@ public MainForm(Rectangle bounds, bool display24HourTime, ScreenSetting screenSe
InitializeComponent();
Bounds = bounds;
fontSize = bounds.Height / fontScaleFactor;
}
}

public MainForm(IntPtr previewWndHandle)
public MainForm(IntPtr previewWndHandle, bool display24HourTime, ScreenSetting screenSetting)
{
_display24HourTime = display24HourTime;
_screenSetting = screenSetting;

InitializeComponent();

// Set the preview window as the parent of this window
Expand Down Expand Up @@ -165,6 +180,33 @@ private void moveTimer_Tick(object sender, EventArgs e)
// }
}

private PrivateFontCollection InitFonts()
{
// We don't add both fonts at the same time because I can only get the private font collection
// to return the first one we add. If the first one is the non-bold one and we ask for a bold one
// then it seems to have a go at generating bold rather than using the one we gave it.
// The system font collection does not seem to have this problem.

var pfc = new PrivateFontCollection();
if (_screenSetting.DisplayType == DisplayType.CurrentTime)
{
AddFont(pfc, Properties.Resources.HelveticaLTStd_BoldCond);
}
else
{
AddFont(pfc, Properties.Resources.HelveticaLTStd_Cond);
}
return pfc;
}

private static void AddFont(PrivateFontCollection pfc, byte[] fontResource)
{
IntPtr ptr = Marshal.AllocCoTaskMem(fontResource.Length); // create an unsafe memory block for the font data
Marshal.Copy(fontResource, 0, ptr, fontResource.Length); // copy the bytes to the unsafe memory block
pfc.AddMemoryFont(ptr, fontResource.Length); // pass the font to the font collection
Marshal.FreeCoTaskMem(ptr);
}

private void DrawIt()
{
try
Expand All @@ -189,7 +231,7 @@ private void DrawIt()

private void DrawCurrentTime()
{
var height = PrimaryFont.Height*10/11;
var height = PrimaryFont.Height * 10/10;
var width = !showSeconds ? Convert.ToInt32(2.05*height) : Convert.ToInt32(3.1 * height);

var x = (Width - width)/2;
Expand Down Expand Up @@ -293,8 +335,8 @@ private void DrawCities()

if (cityFont == null)
{
cityFont = new Font(fontFamilyName, boxHeight.Percent(80), FontStyle.Regular, GraphicsUnit.Pixel);
smallCityFont = new Font(fontFamilyName, boxHeight.Percent(25), FontStyle.Regular, GraphicsUnit.Pixel);
cityFont = new Font(FontFamily, boxHeight.Percent(80), FontStyle.Regular, GraphicsUnit.Pixel);
smallCityFont = new Font(FontFamily, boxHeight.Percent(25), FontStyle.Regular, GraphicsUnit.Pixel);
}

var verticalGap = boxHeight.Percent(verticalGapBetweenBoxesPercent);
Expand Down Expand Up @@ -495,5 +537,24 @@ private void MainForm_Paint(object sender, PaintEventArgs e)
{
DrawIt();
}
}

private void MainForm_FormClosed(object sender, FormClosedEventArgs e)
{
DisposeFontResources();
}

private void DisposeFontResources()
{
if (_fontFamily != null)
{
_fontFamily.Dispose();
_fontFamily = null;
}
if (_pfc != null)
{
_pfc.Dispose();
_pfc = null;
}
}
}
}
4 changes: 2 additions & 2 deletions src/FlipIt/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ static void Main(string[] args)
}

IntPtr previewWndHandle = new IntPtr(long.Parse(secondArgument));
Application.Run(new MainForm(previewWndHandle));
Application.Run(new MainForm(previewWndHandle, settings.Display24HrTime, settings.ScreenSettings[0]));
}
else if (firstArgument == "/s") // Full-screen mode
{
Expand Down Expand Up @@ -89,7 +89,7 @@ private static FlipItSettings LoadSettings()

var settings = new FlipItSettings();

var settingsFolder = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData), "FlipIt");
var settingsFolder = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "FlipIt");
var iniFilePath = Path.Combine(settingsFolder, "Settings.ini");
if (File.Exists(iniFilePath))
{
Expand Down
22 changes: 21 additions & 1 deletion src/FlipIt/Properties/Resources.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 15 additions & 5 deletions src/FlipIt/Properties/Resources.resx
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
mimetype: application/x-microsoft.net.object.binary.base64
value : The object must be serialized with
: System.Serialization.Formatters.Binary.BinaryFormatter
: System.Runtime.Serialization.Formatters.Binary.BinaryFormatter
: and then encoded with base64 encoding.
mimetype: application/x-microsoft.net.object.soap.base64
Expand All @@ -60,6 +60,7 @@
: and then encoded with base64 encoding.
-->
<xsd:schema id="root" xmlns="" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
<xsd:import namespace="http://www.w3.org/XML/1998/namespace" />
<xsd:element name="root" msdata:IsDataSet="true">
<xsd:complexType>
<xsd:choice maxOccurs="unbounded">
Expand All @@ -68,9 +69,10 @@
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" />
</xsd:sequence>
<xsd:attribute name="name" type="xsd:string" />
<xsd:attribute name="name" use="required" type="xsd:string" />
<xsd:attribute name="type" type="xsd:string" />
<xsd:attribute name="mimetype" type="xsd:string" />
<xsd:attribute ref="xml:space" />
</xsd:complexType>
</xsd:element>
<xsd:element name="assembly">
Expand All @@ -85,9 +87,10 @@
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
<xsd:element name="comment" type="xsd:string" minOccurs="0" msdata:Ordinal="2" />
</xsd:sequence>
<xsd:attribute name="name" type="xsd:string" msdata:Ordinal="1" />
<xsd:attribute name="name" type="xsd:string" use="required" msdata:Ordinal="1" />
<xsd:attribute name="type" type="xsd:string" msdata:Ordinal="3" />
<xsd:attribute name="mimetype" type="xsd:string" msdata:Ordinal="4" />
<xsd:attribute ref="xml:space" />
</xsd:complexType>
</xsd:element>
<xsd:element name="resheader">
Expand All @@ -109,9 +112,16 @@
<value>2.0</value>
</resheader>
<resheader name="reader">
<value>System.Resources.ResXResourceReader, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
<value>System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
<resheader name="writer">
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
<assembly alias="System.Windows.Forms" name="System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
<data name="HelveticaLTStd_BoldCond" type="System.Resources.ResXFileRef, System.Windows.Forms">
<value>..\res\HelveticaLTStd-BoldCond.otf;System.Byte[], mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</data>
<data name="HelveticaLTStd_Cond" type="System.Resources.ResXFileRef, System.Windows.Forms">
<value>..\res\HelveticaLTStd-Cond.otf;System.Byte[], mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</data>
</root>
Loading

0 comments on commit 58c606a

Please sign in to comment.