コード例 #1
0
        private void tableList_SelectedIndexChanged(object sender, EventArgs e)
        {
            // TODO: load and show the data in the selected font table.
            var tableData = _font.GetTable(tableList.Text.Substring(0, 4));

            tableProperties.SelectedObject = tableData;
        }
コード例 #2
0
ファイル: PortableFont.cs プロジェクト: i-e-b/Form8sn
        private static void ReadAvailableFonts(string basePath)
        {
            if (!Directory.Exists(basePath))
            {
                return;
            }

            // Font file names usually bear little resemblance to the family and style names.
            // Our portable GDI reads the whole font library and jumps into headers to find the names.
            // This is time and memory expensive, so we cache the results aggressively
            var allFiles = Directory.EnumerateFiles(basePath, "*.ttf", SearchOption.AllDirectories).ToList();

            foreach (var file in allFiles)
            {
                try
                {
                    var ttf       = new TrueTypeFont(file);
                    var nameTable = ttf.GetTable("name") as TtfTableName;
                    if (nameTable?.Names == null)
                    {
                        continue;
                    }

                    var names = nameTable.Names
                                .Where(n => n.NameId == 1 || (n.NameId == 4 && EnglishLanguage(n))).ToList(); // 1 = 'family name' (like "Courier New"); 4 = style name (like "Courier New Bold");

                    foreach (var name in names)
                    {
                        var key = name.PlatformId == 3 && name.EncodingId == 1
                            ? Encoding.BigEndianUnicode.GetString(name.ByteStringValue)
                            : Encoding.UTF8.GetString(name.ByteStringValue);

                        if (LooksLikeBadName(key))
                        {
                            continue;
                        }

                        if (!_fontCache.ContainsKey(key))
                        {
                            _fontCache.Add(key, file);
                        }
                    }
                }
                catch (BadImageFormatException)
                {
                    Console.WriteLine($"Not a true-type file: {file}");
                }
                catch (Exception ex)
                {
                    var str = ex.ToString();
                    Console.WriteLine(str);
                    // ignoring
                }
            }
        }