Exemple #1
0
 public FontWidths(Demon.Font.FontWidths underlying)
 {
     _underlying = underlying;
 }
        protected virtual void Load(string filename)
        {
            //TODO: Load the actual font, not just its AFM file. The AFM file
            //is only useful to us for the standard 14 fonts.

            ushort[] widths     = new ushort[256];
            ushort   widthIndex = 0;
            int      sumWidths  = 0;

            using (StreamReader file = File.OpenText(filename))
            {
                while (!file.EndOfStream)
                {
                    string 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)
                    {
                        _postScriptName = mc[0].Groups[1].Captures[0].Value;
                        continue;
                    }

                    mc = Regex.Matches(line, @"^Weight (\w+)");
                    if (mc.Count == 1)
                    {
                        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.TryParse(mc[0].Groups[1].Captures[0].Value, out _italicAngle);
                        _italic = _italicAngle != 0.0;
                        continue;
                    }

                    mc = Regex.Matches(line, @"^IsFixedPitch (\w+)");
                    if (mc.Count == 1)
                    {
                        bool.TryParse(mc[0].Groups[1].Captures[0].Value, out _isFixedPitch);
                        continue;
                    }

                    mc = Regex.Matches(line, @"^UnderlinePosition (-?\d+)");
                    if (mc.Count == 1)
                    {
                        short.TryParse(mc[0].Groups[1].Captures[0].Value, out _underlinePosition);
                        continue;
                    }

                    mc = Regex.Matches(line, @"^UnderlineThickness (-?\d+)");
                    if (mc.Count == 1)
                    {
                        short.TryParse(mc[0].Groups[1].Captures[0].Value, out _underlineThickness);
                        continue;
                    }

                    mc = Regex.Matches(line, @"^FontBBox ([0-9\-]+) ([0-9\-]+) ([0-9\-]+) ([0-9\-]+)");
                    if (mc.Count == 1)
                    {
                        int left   = 0;
                        int bottom = 0;
                        int right  = 0;
                        int top    = 0;

                        int.TryParse(mc[0].Groups[1].Captures[0].Value, out left);
                        int.TryParse(mc[0].Groups[2].Captures[0].Value, out bottom);
                        int.TryParse(mc[0].Groups[3].Captures[0].Value, out right);
                        int.TryParse(mc[0].Groups[4].Captures[0].Value, out top);

                        _bbox = new BoundingBox(left, bottom, right, bottom);

                        continue;
                    }

                    mc = Regex.Matches(line, @"^CapHeight (\d+)");
                    if (mc.Count == 1)
                    {
                        short.TryParse(mc[0].Groups[1].Captures[0].Value, out _capHeight);
                        continue;
                    }

                    mc = Regex.Matches(line, @"^XHeight (\d+)");
                    if (mc.Count == 1)
                    {
                        short.TryParse(mc[0].Groups[1].Captures[0].Value, out _xHeight);
                        continue;
                    }

                    mc = Regex.Matches(line, @"^Ascender (\d+)");
                    if (mc.Count == 1)
                    {
                        short.TryParse(mc[0].Groups[1].Captures[0].Value, out _ascender);
                        continue;
                    }

                    mc = Regex.Matches(line, @"^Descender (-?\d+)");
                    if (mc.Count == 1)
                    {
                        short.TryParse(mc[0].Groups[1].Captures[0].Value, out _descender);
                        continue;
                    }

                    //	Widths
                    mc = Regex.Matches(line, @"^C (\d+) ; WX (\d+)");
                    if (mc.Count == 1)
                    {
                        ushort c;
                        ushort w;
                        ushort.TryParse(mc[0].Groups[1].Captures[0].Value, out c);
                        ushort.TryParse(mc[0].Groups[2].Captures[0].Value, out w);

                        if (_firstChar == 0)
                        {
                            _firstChar = c;
                        }
                        _lastChar = c;

                        widths[widthIndex] = w;
                        sumWidths         += w;
                        _avgCharWidth      = (short)(sumWidths / (widthIndex + 1));
                        if (w > _advanceWidthMax)
                        {
                            _advanceWidthMax = w;
                        }

                        ++widthIndex;
                    }
                }
            }

            _widths = new FontWidths(widths);
        }