Created
August 16, 2018 16:49
-
-
Save end2endzone/0de46dc5621b1db61f63318a5a8506d0 to your computer and use it in GitHub Desktop.
A C# class for quickly exporting the dimentions of all characters of a specific font to a c++ header file. Prevents c++ project to have a dependency on FreeType.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Text; | |
using System.IO; | |
namespace WindowsFormsApplication1 | |
{ | |
class FontSizeExport | |
{ | |
private System.Windows.Forms.Form MyForm; | |
private System.Windows.Forms.Label MyLabel; | |
public FontSizeExport() | |
{ | |
MyForm = new System.Windows.Forms.Form(); | |
MyLabel = new System.Windows.Forms.Label(); | |
MyForm.SuspendLayout(); | |
// | |
// MyLabel | |
// | |
MyLabel.Anchor = System.Windows.Forms.AnchorStyles.None; | |
MyLabel.AutoSize = true; | |
MyLabel.Font = new System.Drawing.Font("Verdana", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0))); | |
MyLabel.Location = new System.Drawing.Point(9, 94); | |
MyLabel.Margin = new System.Windows.Forms.Padding(0, 0, 0, 0); | |
MyLabel.Name = "MyLabel"; | |
MyLabel.Size = new System.Drawing.Size(74, 18); | |
MyLabel.TabIndex = 0; | |
MyLabel.Text = "AAAAAA"; | |
// | |
// MyForm | |
// | |
MyForm.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); | |
MyForm.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; | |
MyForm.ClientSize = new System.Drawing.Size(284, 262); | |
MyForm.Controls.Add(MyLabel); | |
MyForm.Name = "MyForm"; | |
MyForm.Text = "MyForm"; | |
MyForm.ResumeLayout(false); | |
MyForm.PerformLayout(); | |
} | |
/// <summary> | |
/// Quick and dirty function to get the width of a character at a specific font size | |
/// </summary> | |
/// <param name="c"></param> | |
/// <param name="FontSize"></param> | |
/// <returns></returns> | |
private int GetCharacterWidth(char c, float FontSize) | |
{ | |
this.MyLabel.Font = new System.Drawing.Font("Verdana", FontSize, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Pixel, ((byte)(0))); | |
this.MyLabel.Text = c.ToString(); | |
int BaseWidth = this.MyLabel.Size.Width; | |
MyLabel.Text += c.ToString(); | |
int CharWidth = this.MyLabel.Size.Width - BaseWidth; | |
return CharWidth; | |
} | |
/// <summary> | |
/// Quick and dirty function to get the maximum height of all characters at a specific font size | |
/// </summary> | |
/// <param name="FontSize"></param> | |
/// <returns></returns> | |
private int GetCharacterHeight(float FontSize) | |
{ | |
this.MyLabel.Font = new System.Drawing.Font("Verdana", FontSize, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Pixel, ((byte)(0))); | |
this.MyLabel.Text = "0123456789 ABCDEFGHIJKLMNOPQRSTUVWXYZ abcdefghijklmnopqrstuvwxyz !?[]"; | |
int CharHeight = this.MyLabel.Size.Height; | |
return CharHeight; | |
} | |
public void GenerateVerdanaCppHeader(string FilePath) | |
{ | |
StringBuilder code = new StringBuilder(); | |
const int VERDANA_NUM_FONT_SIZES = 100; | |
const int VERDANA_NUM_CHARACTERS = 128; | |
//add header | |
code.Append(String.Format( | |
"#ifndef VERDANA_H\n" + | |
"#define VERDANA_H\n" + | |
"\n" + | |
"const int VERDANA_NUM_FONT_SIZES = {0};\n" + | |
"const int VERDANA_NUM_CHARACTERS = {1};\n" + | |
"const int VERDANA_CHAR_WIDTH[VERDANA_NUM_FONT_SIZES][VERDANA_NUM_CHARACTERS] = {{\n", VERDANA_NUM_FONT_SIZES, VERDANA_NUM_CHARACTERS)); | |
for (int FontSize = 1; FontSize < VERDANA_NUM_FONT_SIZES; FontSize++) | |
{ | |
code.Append(String.Format(" /*Font size {0}: */ {{", FontSize)); | |
//insert the array values | |
for (int i = 0; i < VERDANA_NUM_CHARACTERS; i++) | |
{ | |
char c = (char)i; | |
int CharWidth = GetCharacterWidth(c, FontSize); | |
code.Append(CharWidth.ToString()); | |
if (i != (VERDANA_NUM_CHARACTERS - 1)) | |
code.Append(", "); | |
} | |
code.Append("},\n"); | |
} | |
//end array | |
code.Append("};\n"); | |
//character height | |
code.Append(String.Format("const int VERDANA_CHAR_HEIGHT[VERDANA_NUM_FONT_SIZES] = {{", VERDANA_NUM_FONT_SIZES)); | |
for (int FontSize = 1; FontSize < VERDANA_NUM_FONT_SIZES; FontSize++) | |
{ | |
int CharHeight = GetCharacterHeight(FontSize); | |
code.Append(CharHeight.ToString()); | |
if (FontSize != (VERDANA_NUM_FONT_SIZES - 1)) | |
code.Append(", "); | |
} | |
//end array | |
code.Append("};\n"); | |
//add footer | |
code.Append("\n" + | |
"#endif //VERDANA_H\n"); | |
File.WriteAllText(FilePath, code.ToString()); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment