Advertisement
tolikpunkoff

FileFont

Mar 16th, 2018
436
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.47 KB | None | 0 0
  1. public class FileFont
  2.     {        
  3.         private string fontfilename = "";
  4.         private Font fnt = null;
  5.         private FontStyle fstyle = FontStyle.Regular;
  6.         private float size = 10;
  7.  
  8.         private Dictionary<string, FontStyle> styleset = new Dictionary<string, FontStyle>();
  9.        
  10.         private bool f_bold = false;
  11.         private bool f_italic = false;
  12.         private bool f_underline = false;
  13.         private bool f_strikeout = false;
  14.  
  15.         public float Size
  16.         {
  17.             get
  18.             {
  19.                 return size;
  20.             }
  21.             set
  22.             {
  23.                 size = value;
  24.             }
  25.         }
  26.  
  27.         public bool Regular
  28.         {
  29.             get
  30.             {
  31.                 return IsRegular();
  32.             }
  33.             set
  34.             {
  35.                 if (value)
  36.                 {
  37.                     SetRegular();
  38.                 }
  39.             }
  40.         }
  41.  
  42.         public bool Bold
  43.         {
  44.             get
  45.             {
  46.                 return f_bold;
  47.             }
  48.             set
  49.             {
  50.                 SetStyle("Bold", value);
  51.                 f_bold = value;
  52.             }
  53.         }
  54.  
  55.         public bool Italic
  56.         {
  57.             get
  58.             {
  59.                 return f_italic;
  60.             }
  61.             set
  62.             {
  63.                 SetStyle("Italic", value);
  64.                 f_italic = value;
  65.             }
  66.         }
  67.  
  68.         public bool Underline
  69.         {
  70.             get
  71.             {
  72.                 return f_underline;
  73.             }
  74.             set
  75.             {
  76.                 SetStyle("Underline", value);
  77.                 f_underline = value;
  78.             }
  79.         }
  80.  
  81.         public bool Strikeout
  82.         {
  83.             get
  84.             {
  85.                 return f_strikeout;
  86.             }
  87.             set
  88.             {
  89.                 SetStyle("Strikeout", value);
  90.                 f_strikeout = value;
  91.             }
  92.         }
  93.  
  94.         public Font Font
  95.         {
  96.             get
  97.             {
  98.                 BuildFont();
  99.                 return fnt;
  100.             }            
  101.         }
  102.  
  103.         public string FileName
  104.         {
  105.             get
  106.             {
  107.                 return fontfilename;
  108.             }
  109.             set
  110.             {
  111.                 fontfilename = value;
  112.             }
  113.         }
  114.  
  115.         public FileFont(string filename)
  116.         {
  117.             FileName = filename;            
  118.             styleset.Add("Bold", FontStyle.Regular);
  119.             styleset.Add("Italic", FontStyle.Regular);
  120.             styleset.Add("Strikeout", FontStyle.Regular);
  121.             styleset.Add("Underline", FontStyle.Regular);
  122.  
  123.  
  124.             GlobalFontCollection.AddFont(FileName);                
  125.         }
  126.  
  127.         private void BuildFont()
  128.         {
  129.             fstyle = FontStyle.Regular;
  130.             foreach (string key in styleset.Keys)
  131.             {
  132.                 fstyle = fstyle | styleset[key];
  133.             }
  134.             int idx = GlobalFontCollection.GetIndex(FileName);
  135.             fnt = new Font(GlobalFontCollection.PFCollection.Families[idx],
  136.                 size, fstyle);
  137.         }
  138.  
  139.         private void SetRegular()
  140.         {
  141.             string[] keys = new string[styleset.Keys.Count];
  142.             styleset.Keys.CopyTo(keys, 0);
  143.            
  144.             foreach (string key in keys)
  145.             {
  146.                 styleset[key] = FontStyle.Regular;
  147.             }
  148.            
  149.             f_bold = false;
  150.             f_italic = false;
  151.             f_strikeout = false;
  152.             f_underline = false;
  153.         }
  154.  
  155.         private void SetStyle(string StyleName, bool On)
  156.         {
  157.             if (!On)
  158.             {
  159.                 styleset[StyleName] = FontStyle.Regular;
  160.             }
  161.             else
  162.             {
  163.                 switch (StyleName)
  164.                 {
  165.                     case "Bold": styleset[StyleName] = FontStyle.Bold; break;
  166.                     case "Italic": styleset[StyleName] = FontStyle.Italic; break;
  167.                     case "Strikeout": styleset[StyleName] = FontStyle.Strikeout; break;
  168.                     case "Underline": styleset[StyleName] = FontStyle.Underline; break;
  169.  
  170.                 }
  171.             }
  172.         }
  173.  
  174.         private bool IsRegular()
  175.         {
  176.             foreach (string key in styleset.Keys)
  177.             {
  178.                 if (styleset[key] != FontStyle.Regular) return false;
  179.             }
  180.  
  181.             return true;
  182.         }
  183.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement