예제 #1
0
        private static FontFamily GetFontFamily(string familyName)
        {
            int fontIndex = 0;

            _currentFontCollection.FindFamilyName(familyName, out fontIndex);
            if (fontIndex < 0)
            {
                return(null);
            }

            var fontFamily = _currentFontCollection.GetFontFamily(fontIndex);

            return(fontFamily);
        }
예제 #2
0
 public static IEnumerable <string> GetAllFontFamilyName()
 {
     using (Factory Fact = new Factory())
         using (FontCollection Collection = Fact.GetSystemFontCollection(new RawBool(false)))
         {
             for (int Index = 0; Index < Collection.FontFamilyCount; Index++)
             {
                 using (FontFamily Font = Collection.GetFontFamily(Index))
                 {
                     if (Font.FamilyNames.FindLocaleName(ApplicationLanguages.PrimaryLanguageOverride, out int NameStringIndex))
                     {
                         yield return(Font.FamilyNames.GetString(NameStringIndex));
                     }
                     else if (Font.FamilyNames.FindLocaleName("en-us", out int NameStringIndex1))
                     {
                         yield return(Font.FamilyNames.GetString(NameStringIndex1));
                     }
                     else
                     {
                         yield return(Font.FamilyNames.GetString(0));
                     }
                 }
             }
         }
 }
 public static IEnumerable <FontFamily> GetFontFamilies(this FontCollection fontCollection)
 {
     for (int i = 0; i < fontCollection.FontFamilyCount; i++)
     {
         yield return(fontCollection.GetFontFamily(i));
     }
 }
예제 #4
0
        private async Task <List <string> > LoadCustomFontCharacters(string fontfamilyname, string fontFilePath)
        {
            var uri         = new Uri(fontFilePath);
            var storageFile = await StorageFile.GetFileFromApplicationUriAsync(uri);

            CurrentResourceFontLoader = new ResourceFontLoader(FactoryDWrite, await storageFile.OpenStreamForReadAsync());
            CurrentFontCollection     = new FontCollection(FactoryDWrite, CurrentResourceFontLoader,
                                                           CurrentResourceFontLoader.Key);

            var character = new List <string>();

            CurrentFontCollection.FindFamilyName(fontfamilyname, out var familyIndex);
            if (familyIndex == -1)
            {
                return(character);
            }

            using (var fontFamily = CurrentFontCollection.GetFontFamily(familyIndex))
            {
                var font  = fontFamily.GetFont(0);
                var count = 65536 * 4 - 1;
                for (var i = 0; i < count; i++)
                {
                    if (font.HasCharacter(i))
                    {
                        character.Add(char.ConvertFromUtf32(i));
                    }
                }
            }

            return(character);
        }
예제 #5
0
        protected static Font CompileFontData(string a_fontFamily, float a_fontSize)
        {
            if (!string.IsNullOrEmpty(a_fontFamily))
            {
                FontFamily fontFamily = FontCollection.GetFontFamily(a_fontFamily);

                if (fontFamily != null)
                {
                    return(new Font(fontFamily, a_fontSize, GraphicsUnit.Pixel));
                }

                InstalledFontCollection fontCollection = new InstalledFontCollection();

                FontFamily[] fontFamilies = fontCollection.Families;

                foreach (FontFamily font in fontFamilies)
                {
                    if (font.Name == a_fontFamily)
                    {
                        return(new Font(font, a_fontSize, GraphicsUnit.Pixel));
                    }
                }
            }

            return(new Font(SystemFonts.DefaultFont.FontFamily, a_fontSize));
        }
예제 #6
0
        void Create(string familyName, float sizeInPoints, FontStyle style, FontDecoration decoration)
        {
            Size           = sizeInPoints;
            FontStyle      = style;
            FontDecoration = decoration;
            int index;

            if (FontCollection.FindFamilyName(familyName, out index))
            {
                sw.FontStyle  fontStyle;
                sw.FontWeight fontWeight;
                Conversions.Convert(style, out fontStyle, out fontWeight);
                Control = FontCollection.GetFontFamily(index).GetFirstMatchingFont(fontWeight, sw.FontStretch.Normal, fontStyle);
            }
        }
예제 #7
0
        private Font GetSystemFont(string fontFamilyName)
        {
            int  fontFamilyIndex;
            bool found = _systemFonts.FindFamilyName(fontFamilyName, out fontFamilyIndex);

            if (!found)
            {
                throw new ArgumentException("Couldn't find the specified font.");
            }

            using (var fontFamily = _systemFonts.GetFontFamily(fontFamilyIndex))
            {
                return(fontFamily.GetFirstMatchingFont(FontWeight.Normal, FontStretch.Normal, FontStyle.Normal));
            }
        }
        public static List <InstalledFont> GetFonts()
        {
            var fontList = new List <InstalledFont>();

            using (var factory = new Factory())
            {
                FontCollection = factory.GetSystemFontCollection(false);
                var familyCount = FontCollection.FontFamilyCount;

                for (int i = 0; i < familyCount; i++)
                {
                    try
                    {
                        using (var fontFamily = FontCollection.GetFontFamily(i))
                        {
                            var familyNames = fontFamily.FamilyNames;

                            if (!familyNames.FindLocaleName(CultureInfo.CurrentCulture.Name, out var index))
                            {
                                familyNames.FindLocaleName("en-us", out index);
                            }

                            bool isSymbolFont = fontFamily.GetFont(index).IsSymbolFont;

                            string name = familyNames.GetString(index);
                            fontList.Add(new InstalledFont()
                            {
                                Name         = name,
                                FamilyIndex  = i,
                                Index        = index,
                                IsSymbolFont = isSymbolFont
                            });
                        }
                    }
                    catch (Exception e)
                    {
                        // Corrupted font files throw an exception
                    }
                }
            }

            return(fontList);
        }
예제 #9
0
    async Task LoadCustomFonts()
    {
        await Task.Run(() =>
        {
            // Font Families
            FontFamilyNames = new List <string> {
                "Aileron", "Grundschrift"
            };
            // Character codes to check for:
            int[] codes               = { 0x41, 0x6f, 0x7c, 0xc2aa, 0xD7A3 };
            FactoryDWrite             = new SharpDX.DirectWrite.Factory();
            CurrentResourceFontLoader = new ResourceFontLoader(FactoryDWrite, customFontStreams);
            CurrentFontCollection     = new FontCollection(FactoryDWrite, CurrentResourceFontLoader, CurrentResourceFontLoader.Key);

            foreach (var fontfamilyname in FontFamilyNames)
            {
                int familyIndex;
                CurrentFontCollection.FindFamilyName(fontfamilyname, out familyIndex);

                using (var fontFamily = CurrentFontCollection.GetFontFamily(familyIndex))
                {
                    var font = fontFamily.GetFont(0);

                    using (var fontface = new FontFace(font))
                    {
                        var results = fontface.GetGlyphIndices(codes);
                        for (int i = 0; i < codes.Length - 1; i++)
                        {
                            if (results[i] > 0)
                            {
                                Debug.WriteLine("Contains the unicode character " + codes[i]);
                            }
                            else
                            {
                                Debug.WriteLine("Does not contain the unicode character " + codes[i]);
                            }
                        }
                    }
                }
            }
        });
    }
예제 #10
0
        public void SetTextFormat(TextFormat textFormat)
        {
            // Initializes properties using a text format, like font family, font size,
            // and reading direction. For simplicity, this custom layout supports
            // minimal formatting. No mixed formatting or alignment modes are supported.
            readingDirection_ = textFormat.ReadingDirection;
            fontEmSize_       = textFormat.FontSize;
            localName_        = textFormat.LocaleName;

            // Map font and style to fontFace.
            FontCollection fontCollection = textFormat.FontCollection;// Need the font collection to map from font name to actual font.

            if (fontCollection == null)
            {
                fontCollection = dwriteFactory_.GetSystemFontCollection(false);// No font collection was set in the format, so use the system default.
            }
            // Find matching family name in collection.
            String fontFamilyName = textFormat.FontFamilyName;

            int fontIndex;

            // If the given font does not exist, take what we can get
            // (displaying something instead nothing), choosing the foremost
            // font in the collection.
            if (!fontCollection.FindFamilyName(fontFamilyName, out fontIndex))
            {
                fontIndex = 0;
            }

            FontFamily fontFamily = fontCollection.GetFontFamily(fontIndex);
            Font       font       = fontFamily.GetFirstMatchingFont(textFormat.FontWeight, textFormat.FontStretch, textFormat.FontStyle);

            fontFace_ = new FontFace(font);

            font.Dispose();
            fontFamily.Dispose();
            fontCollection.Dispose();
        }
        public static Font GetFont(Typeface typeface)
        {
            var fontFamily     = typeface.FontFamily;
            var fontCollection = GetOrAddFontCollection(fontFamily);

            foreach (var familyName in fontFamily.FamilyNames)
            {
                if (fontCollection.FindFamilyName(familyName, out var index))
                {
                    return(fontCollection.GetFontFamily(index).GetFirstMatchingFont(
                               (FontWeight)typeface.Weight,
                               FontStretch.Normal,
                               (FontStyle)typeface.Style));
                }
            }

            InstalledFontCollection.FindFamilyName(FontFamily.Default.Name, out var i);

            return(InstalledFontCollection.GetFontFamily(i).GetFirstMatchingFont(
                       (FontWeight)typeface.Weight,
                       FontStretch.Normal,
                       (FontStyle)typeface.Style));
        }
예제 #12
0
        private void LoadFonts()
        {
            Task.Run(() =>
            {
                var x       = new List <string>();
                var factory = new Factory();
                FontCollection fontCollection = factory.GetSystemFontCollection(false);
                int familyCount = fontCollection.FontFamilyCount;
                for (int i = 0; i < familyCount; i++)
                {
                    FontFamily fontFamily        = fontCollection.GetFontFamily(i);
                    LocalizedStrings familyNames = fontFamily.FamilyNames;
                    int index;
                    if (!familyNames.FindLocaleName(CultureInfo.CurrentCulture.Name, out index))
                    {
                        familyNames.FindLocaleName("en-us", out index);
                    }

                    string name = familyNames.GetString(index);
                    x.Add(name);
                }
                Fonts = new ObservableCollection <string>(x.OrderBy(y => y));
            });
        }
        public static Font GetFont(Typeface typeface)
        {
            var fontFamily     = typeface.FontFamily;
            var fontCollection = GetOrAddFontCollection(fontFamily);
            int index;

            foreach (var name in fontFamily.FamilyNames)
            {
                if (fontCollection.FindFamilyName(name, out index))
                {
                    return(fontCollection.GetFontFamily(index).GetFirstMatchingFont(
                               (FontWeight)typeface.Weight,
                               (FontStretch)typeface.Stretch,
                               (FontStyle)typeface.Style));
                }
            }

            InstalledFontCollection.FindFamilyName("Segoe UI", out index);

            return(InstalledFontCollection.GetFontFamily(index).GetFirstMatchingFont(
                       (FontWeight)typeface.Weight,
                       (FontStretch)typeface.Stretch,
                       (FontStyle)typeface.Style));
        }
예제 #14
0
        public static SharpDX.DirectWrite.Font GetDxFont(string fontFamily, SharpDX.DirectWrite.FontWeight weight, SharpDX.DirectWrite.FontStretch stretch, SharpDX.DirectWrite.FontStyle style)
        {
            //Windows.UI.Xaml.Media.FontFamily fontFamily = textBlock.FontFamily;

            if (fontFamily == null)
            {
                fontFamily = LabeLRenderer._defaultTextBlock.FontFamily.Source;
            }

            var fontKey = fontFamily + "-" + weight + "-" + stretch + "-" + style;

            if (_loadedFonts.TryGetValue(fontKey, out SharpDX.DirectWrite.Font font))
            {
                return(font);
            }

            using (var factory = new Factory())
            {
                if (fontFamily.StartsWith("ms-appdata:///"))
                {
                    var    fontFamilyFilePath = fontFamily.Substring(14);
                    string dir = null;
                    if (fontFamilyFilePath.StartsWith("local/"))
                    {
                        dir = ApplicationData.Current.LocalFolder.Path;
                        fontFamilyFilePath = fontFamilyFilePath.Substring(6);
                    }
                    else if (fontFamilyFilePath.StartsWith("localcache/"))
                    {
                        dir = ApplicationData.Current.LocalCacheFolder.Path;
                        fontFamilyFilePath = fontFamilyFilePath.Substring(11);
                    }
                    else if (fontFamilyFilePath.StartsWith("roaming/"))
                    {
                        dir = ApplicationData.Current.RoamingFolder.Path;
                        fontFamilyFilePath = fontFamilyFilePath.Substring(8);
                    }
                    else if (fontFamilyFilePath.StartsWith("temp/"))
                    {
                        dir = ApplicationData.Current.TemporaryFolder.Path;
                        fontFamilyFilePath = fontFamilyFilePath.Substring(5);
                    }
                    else
                    {
                        System.Console.WriteLine("Unknown StorageFolder for " + fontFamily);
                        return(null);
                    }
                    var path = System.IO.Path.Combine(dir, fontFamilyFilePath.Split('#')[0]);
                    path = path.Replace('/', '\\');
                    var fontFile = new SharpDX.DirectWrite.FontFile(factory, path);

                    var loader = fontFile.Loader;

                    var key = fontFile.GetReferenceKey();

                    using (var fontCollectionLoader = new FontCollectionLoader(fontFile))
                    {
                        factory.RegisterFontCollectionLoader(fontCollectionLoader);

                        using (var fontCollection = new FontCollection(factory, fontCollectionLoader, key))
                        {
                            var family = fontCollection.GetFontFamily(0);


                            var familyNames = family.FamilyNames;

                            font = family.GetFirstMatchingFont(weight, stretch, style);

                            _loadedFonts[fontKey] = font;
                        }
                    }
                    return(font);
                }
                using (var fontCollection = factory.GetSystemFontCollection(false))
                {
                    var familyCount = fontCollection.FontFamilyCount;
                    for (int i = 0; i < familyCount; i++)
                    {
                        try
                        {
                            using (var dxFontFamily = fontCollection.GetFontFamily(i))
                            {
                                var familyNames = dxFontFamily.FamilyNames;

                                if (!familyNames.FindLocaleName(System.Globalization.CultureInfo.CurrentCulture.Name, out int index))
                                {
                                    familyNames.FindLocaleName("en-us", out index);
                                }

                                var name = familyNames.GetString(index);

                                var display = name;
                                using (var dxFont = dxFontFamily.GetFont(index))
                                {
                                    if (dxFont.IsSymbolFont)
                                    {
                                        display = "Segoe UI";
                                    }
                                }

                                //fontList.Add(new InstalledFont { Name = name, DisplayFont = display });
                                if ((fontFamily == name || fontFamily == display) && dxFontFamily.GetFirstMatchingFont(weight, stretch, style) is SharpDX.DirectWrite.Font systemFont)
                                {
                                    _loadedFonts[fontKey] = systemFont;
                                    return(systemFont);
                                }
                            }
                        }
#pragma warning disable CC0004    // Catch block cannot be empty
                        catch { } // Corrupted font files throw an exception - ignore them
#pragma warning restore CC0004    // Catch block cannot be empty
                    }
                }
            }
            return(null);
        }
예제 #15
0
        //private string CreateFont(string fontName, float fontSize, CCRawList<char> charset)
        private Font CreateFont(string fontName, float fontSize)
        {
            Font _currentFont;

            if (Factory2D == null)
            {
                Factory2D      = new SharpDX.Direct2D1.Factory();
                FactoryDWrite  = new SharpDX.DirectWrite.Factory();
                FactoryImaging = new SharpDX.WIC.ImagingFactory();

                dpi      = Factory2D.DesktopDpi;
                dpiScale = dpi.Height / 72f;
            }

            _currentFontCollection = FactoryDWrite.GetSystemFontCollection(true);

            if (_defaultFont == null)
            {
                _defaultFont = GenericSanSerif();
            }

            FontFamily fontFamily = GetFontFamily(fontName);


            FontCollection fontCollection;

            if (!_fontCollectionCache.TryGetValue(fontName, out fontCollection))
            {
                var ext = Path.GetExtension(fontName);

                if (!String.IsNullOrEmpty(ext) && ext.ToLower() == ".ttf")
                {
                    try
                    {
                        var fileFontLoader     = new FileFontLoader(FactoryDWrite, fontName);
                        var fileFontCollection = new FontCollection(FactoryDWrite, fileFontLoader, fileFontLoader.Key);
                        _currentFontCollection = fileFontCollection;
                        fontFamily             = _currentFontCollection.GetFontFamily(0);
                        var ffn = fontFamily.FamilyNames;
                        _currentFont = fontFamily.GetFirstMatchingFont(FontWeight.Regular, FontStretch.Normal, FontStyle.Normal);

                        _fontCollectionCache.Add(fontName, fileFontCollection);
                        _currentFontCollection = fileFontCollection;
                    }
                    catch
                    {
                        _currentFont = GetFont(fontName, fontSize);
                        CCLog.Log("{0} not found.  Defaulting to {1}.", fontName, _currentFont.FontFamily.FamilyNames.GetString(0));
                    }
                }
                else
                {
                    _currentFont = GetFont(fontName, fontSize);
                }
            }
            else
            {
                fontFamily = fontCollection.GetFontFamily(0);

                _currentFont           = fontFamily.GetFirstMatchingFont(FontWeight.Regular, FontStretch.Normal, FontStyle.Normal);
                _currentFontCollection = fontCollection;
            }

            return(_currentFont);
        }