Esempio n. 1
0
        /// <summary>
        /// Load a named font, ready for use.
        /// </summary>
        public Font GetFont(string familyName, int weight, bool bold, bool italic, bool underline, bool strikeout)
        {
            //	If we already have this font then don't duplicate it
            Font font = null;

            foreach (Font candidate in _fonts)
            {
                if (candidate.FamilyName.ToLower() != familyName.ToLower())
                {
                    continue;
                }
                if (candidate.Bold != bold)
                {
                    continue;
                }
                if (candidate.Italic != italic)
                {
                    continue;
                }

                //	If we get here then we've got a match
                font = candidate;
                break;
            }
            if (font != null)
            {
                return(font);
            }

            //	Search our font directories for a file that implements
            //	the specified font
            FontInfo info = FindFont(familyName, weight, bold, italic, underline, strikeout);

            if (info == null)
            {
                string msg = "Font '" + familyName + "'";
                if (bold)
                {
                    msg += " bold";
                }
                if (italic)
                {
                    msg += " italic";
                }
                if (underline)
                {
                    msg += " underline";
                }
                if (strikeout)
                {
                    msg += " strikeout";
                }
                msg += " not found, and no substitute found either.";
                throw new Exception(msg);
            }

            //	Create the real font
            switch (info.Type)
            {
            case FontType.TrueType:
                font = new TrueTypeFont(info.FileName);
                break;

            case FontType.Type1:
                font = new Type1Font(info.FileName);
                break;
            }
            _fonts.Add(font);
            return(font);
        }
Esempio n. 2
0
        /// <summary>
        /// Load information about the font, but don't load the glyph data.
        /// </summary>
        public static FontInfo GetFontInfo(string filename)
        {
            string familyName = null;
            string fontName   = null;
            bool   bold       = false;
            bool   italic     = false;

#pragma warning disable 0219 // variable assigned but never used
            bool underline = false;
            bool strikeout = false;
#pragma warning restore 0219

            using (StreamReader file = File.OpenText(filename))
            {
                //	Type 1 starts with "StartFontMetrics"
                string line = file.ReadLine();
                if (!line.StartsWith("StartFontMetrics"))
                {
                    return(null);
                }

                //	Find the font name line
                while (!file.EndOfStream)
                {
                    line = file.ReadLine();

                    MatchCollection mc = Regex.Matches(line, @"^FamilyName (.+)");
                    if (mc.Count == 1)
                    {
                        familyName = mc[0].Groups[1].Captures[0].Value;
                        continue;
                    }

                    mc = Regex.Matches(line, @"^FontName (.+)");
                    if (mc.Count == 1)
                    {
                        fontName = mc[0].Groups[1].Captures[0].Value;
                        continue;
                    }

                    mc = Regex.Matches(line, @"^Weight (\w+)");
                    if (mc.Count == 1)
                    {
                        int weight = 0;
                        switch (mc[0].Groups[1].Captures[0].Value.ToLower())
                        {
                        case "thin":        weight = 100; break;

                        case "extra-light": weight = 200; break;

                        case "light":           weight = 300; break;

                        case "normal":          weight = 400; break;

                        case "regular":         weight = 400; break;

                        case "medium":          weight = 500; break;

                        case "semi-bold":       weight = 600; break;

                        case "demi-bold":       weight = 600; break;

                        case "bold":            weight = 700; break;

                        case "extra-bold":      weight = 800; break;

                        case "ultra-bold":      weight = 800; break;

                        case "black":           weight = 900; break;

                        case "heavy":           weight = 900; break;

                        default:                        weight = 400; break;
                        }
                        bold = weight >= 600;
                        continue;
                    }

                    mc = Regex.Matches(line, @"^ItalicAngle (-?\d+)");
                    if (mc.Count == 1)
                    {
                        float angle;
                        float.TryParse(mc[0].Groups[1].Captures[0].Value, out angle);
                        italic = angle != 0.0;
                        continue;
                    }
                }
            }
            if (fontName == null)
            {
                return(null);
            }

            FontInfo info = new FontInfo(familyName, fontName, filename, FontType.Type1, bold, italic, false, false);
            return(info);
        }