Exemple #1
0
 /// <summary>
 /// Initializes a new instance of the <see cref="FontCache"/> class.
 /// </summary>
 /// <param name="factory">DirectWrite factory.</param>
 public FontCache(DWrite.Factory factory)
 {
     this.factory = factory;
     using (var factoryDWrite = factory.QueryInterface <DWrite.Factory2>())
     {
         this.fontCollection     = factoryDWrite.GetSystemFontCollection(false);
         this.systemFontFallback = factoryDWrite.SystemFontFallback;
         this.SetPrimaryFontFamily(DefaultFontFamilyName);
     }
 }
Exemple #2
0
        private static sw.FontCollection FontCollection()
        {
            if (fontCollection == null)
            {
                fontCollection =
                    Factory.GetSystemFontCollection(
                        checkForUpdates: false);
            }

            return(fontCollection);
        }
Exemple #3
0
 public static bool TryCreate(DWrite.FontCollection collection, string fontFamilyName, out FontFamilyCacheItem item)
 {
     if (collection.FindFamilyName(fontFamilyName, out var index))
     {
         var fontFamily = collection.GetFontFamily(index);
         item = new FontFamilyCacheItem(fontFamily);
         return(true);
     }
     else
     {
         item = null;
         return(false);
     }
 }
        /// <summary>
        /// Gets a collection containing all available font familily names.
        /// </summary>
        public IEnumerable <string> GetFontFamilyNames(string localeName = "en-us")
        {
            localeName.EnsureNotNullOrEmpty(nameof(localeName));
            localeName = localeName.ToLower();

            // Query for all available FontFamilies installed on the system
            List <string> result = null;

            using (DWrite.FontCollection fontCollection = this.FactoryDWrite.GetSystemFontCollection(false))
            {
                int fontFamilyCount = fontCollection.FontFamilyCount;
                result = new List <string>(fontFamilyCount);

                for (int loop = 0; loop < fontFamilyCount; loop++)
                {
                    using (DWrite.FontFamily actFamily = fontCollection.GetFontFamily(loop))
                        using (DWrite.LocalizedStrings actLocalizedStrings = actFamily.FamilyNames)
                        {
                            int localeIndex = -1;
                            if ((bool)actLocalizedStrings.FindLocaleName(localeName, out localeIndex))
                            {
                                string actName = actLocalizedStrings.GetString(0);
                                if (!string.IsNullOrWhiteSpace(actName))
                                {
                                    result.Add(actName);
                                }
                            }
                            else if ((bool)actLocalizedStrings.FindLocaleName("en-us", out localeIndex))
                            {
                                string actName = actLocalizedStrings.GetString(0);
                                if (!string.IsNullOrWhiteSpace(actName))
                                {
                                    result.Add(actName);
                                }
                            }
                        }
                }
            }

            // Sort the list finally
            result.Sort();

            return(result);
        }
        private static Font GetFont(string fontName, float fontSize)
        {
            var fontFamily = GetFontFamily(fontName);

            if (fontFamily == null)
            {
                fontFamily = GetFontFamily(fontName);
            }

            if (fontFamily == null)
            {
                _currentFontCollection = FactoryDWrite.GetSystemFontCollection(true);

                CCLog.Log("{0} not found.  Defaulting to {1}.", fontName, GenericSanSerif().FontFamily.FamilyNames.GetString(0));
                return GenericSanSerif();
            }

            // This is generic right now.  We should be able to handle different styles in the future
            var font = fontFamily.GetFirstMatchingFont(FontWeight.Regular, FontStretch.Normal, FontStyle.Normal);

            return font;
        }
        //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;
        }
 /// <summary>
 /// Inits the custom font.
 /// </summary>
 private void InitCustomFont()
 {
     CurrentResourceFontLoader = new ResourceFontLoader(FactoryDWrite);
     CurrentFontCollection = new FontCollection(FactoryDWrite, CurrentResourceFontLoader, CurrentResourceFontLoader.Key);
     comboBoxFonts.Items.Add("Pericles");
     comboBoxFonts.Items.Add("Kootenay");
     comboBoxFonts.SelectedIndex = 0;
 }
Exemple #8
0
        public static void Initialize()
        {
            directWriteFactory = new SharpDX.DirectWrite.Factory();
            resourceFontLoader = new ResourceFontLoader(directWriteFactory);
            fontCollection     = new SharpDX.DirectWrite.FontCollection(directWriteFactory, resourceFontLoader, resourceFontLoader.Key);

            FontSmall  = new TextFormat(directWriteFactory, "Quicksand", fontCollection, FontWeight.Regular, FontStyle.Normal, FontStretch.Normal, 10.0f);
            FontMedium = new TextFormat(directWriteFactory, "Quicksand", fontCollection, FontWeight.Regular, FontStyle.Normal, FontStretch.Normal, 12.0f);
            FontBig    = new TextFormat(directWriteFactory, "Quicksand", fontCollection, FontWeight.Regular, FontStyle.Normal, FontStretch.Normal, 20.0f);
            FontHuge   = new TextFormat(directWriteFactory, "Quicksand", fontCollection, FontWeight.Regular, FontStyle.Normal, FontStretch.Normal, 28.0f);

            FontSmallBold  = new TextFormat(directWriteFactory, "Quicksand", fontCollection, FontWeight.Bold, FontStyle.Normal, FontStretch.Normal, 10.0f);
            FontMediumBold = new TextFormat(directWriteFactory, "Quicksand", fontCollection, FontWeight.Bold, FontStyle.Normal, FontStretch.Normal, 12.0f);
            FontBigBold    = new TextFormat(directWriteFactory, "Quicksand", fontCollection, FontWeight.Bold, FontStyle.Normal, FontStretch.Normal, 20.0f);

            FontSmallCenter = new TextFormat(directWriteFactory, "Quicksand", fontCollection, FontWeight.Regular, FontStyle.Normal, FontStretch.Normal, 10.0f);
            FontSmallCenter.TextAlignment = TextAlignment.Center;
            FontSmallCenter.WordWrapping  = WordWrapping.NoWrap;

            FontSmallRight = new TextFormat(directWriteFactory, "Quicksand", fontCollection, FontWeight.Regular, FontStyle.Normal, FontStretch.Normal, 10.0f);
            FontSmallRight.TextAlignment = TextAlignment.Trailing;
            FontSmallRight.WordWrapping  = WordWrapping.NoWrap;

            FontMediumRight = new TextFormat(directWriteFactory, "Quicksand", fontCollection, FontWeight.Regular, FontStyle.Normal, FontStretch.Normal, 12.0f);
            FontMediumRight.TextAlignment = TextAlignment.Trailing;
            FontMediumRight.WordWrapping  = WordWrapping.NoWrap;

            FontMediumCenter = new TextFormat(directWriteFactory, "Quicksand", fontCollection, FontWeight.Regular, FontStyle.Normal, FontStretch.Normal, 12.0f);
            FontMediumCenter.TextAlignment = TextAlignment.Center;
            FontMediumCenter.WordWrapping  = WordWrapping.NoWrap;

            FontMediumBoldCenter = new TextFormat(directWriteFactory, "Quicksand", fontCollection, FontWeight.Bold, FontStyle.Normal, FontStretch.Normal, 12.0f);
            FontMediumBoldCenter.TextAlignment = TextAlignment.Center;
            FontMediumBoldCenter.WordWrapping  = WordWrapping.NoWrap;

            FontMediumBoldCenterEllipsis = new TextFormat(directWriteFactory, "Quicksand", fontCollection, FontWeight.Bold, FontStyle.Normal, FontStretch.Normal, 12.0f);
            FontMediumBoldCenterEllipsis.TextAlignment = TextAlignment.Center;
            FontMediumBoldCenterEllipsis.WordWrapping  = WordWrapping.NoWrap;
            var trimmingSign = new EllipsisTrimming(directWriteFactory, FontMediumBoldCenterEllipsis);

            FontMediumBoldCenterEllipsis.SetTrimming(new Trimming()
            {
                Delimiter = (int)')', Granularity = TrimmingGranularity.Character, DelimiterCount = 1
            }, trimmingSign);



            PrivateFontCollection = new PrivateFontCollection();
            AddFontFromMemory(PrivateFontCollection, "FamiStudio.Resources.Quicksand-Regular.ttf");
            AddFontFromMemory(PrivateFontCollection, "FamiStudio.Resources.Quicksand-Bold.ttf");

            for (int j = 0; j < CustomColors.GetLength(1); j++)
            {
                for (int i = 0; i < CustomColors.GetLength(0); i++)
                {
                    if (i == CustomColors.GetLength(0) - 1 &&
                        j == CustomColors.GetLength(1) - 1)
                    {
                        continue;
                    }

                    var color = CustomColors[i, j];
                    CustomColors[i, j] = System.Drawing.Color.FromArgb(
                        Math.Min(255, color.R + 60),
                        Math.Min(255, color.G + 60),
                        Math.Min(255, color.B + 60));
                }
            }

            OleLibrary       = LoadLibrary("ole32.dll");
            DragCursorHandle = LoadCursor(OleLibrary, 2);
            CopyCursorHandle = LoadCursor(OleLibrary, 3);
            DragCursor       = new Cursor(DragCursorHandle);
            CopyCursor       = new Cursor(CopyCursorHandle);
        }
        private static Font GetFont(string fontName, float fontSize)
        {
            var fontFamily = GetFontFamily(fontName);

            if (fontFamily == null)
            {
                if (privateFontLoader == null)
                {
                    privateFontLoader = new PrivateFontLoader(FactoryDWrite);
                    privateFontCollection = new FontCollection(FactoryDWrite, privateFontLoader, privateFontLoader.Key);
                }

                _currentFontCollection = privateFontCollection;
                fontFamily = GetFontFamily(fontName);

            }

            if (fontFamily == null)
            {
                _currentFontCollection = FactoryDWrite.GetSystemFontCollection(true);
                return GenericSanSerif();
            }

            // This is generic right now.  We should be able to handle different styles in the future
            var font = fontFamily.GetFirstMatchingFont(FontWeight.Regular, FontStretch.Normal, FontStyle.Normal);
            
            return font;
        }
        private string CreateFont(string fontName, float fontSize, CCRawList<char> charset)
        {

            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();
                //_defaultDIP = ConvertPointSizeToDIP(_defaultFontSizeEm);
            }

            FontFamily fontFamily = GetFontFamily(fontName);


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

                _currentFont = _defaultFont;

                //if (!String.IsNullOrEmpty(ext) && ext.ToLower() == ".ttf")
                //{

                //    //var appPath = AppDomain.CurrentDomain.BaseDirectory;
                //    //var contentPath = Path.Combine(appPath, CCContentManager.SharedContentManager.RootDirectory);
                //    //var fontPath = Path.Combine(contentPath, fontName);

                //    //if (File.Exists(fontPath))
                //    //{
                //    //    try
                //    //    {
                //    //        if (privateFontLoader == null)
                //    //        {
                //    //            privateFontLoader = new PrivateFontLoader(FactoryDWrite, fontName);
                //    //            privateFontCollection = new FontCollection(FactoryDWrite, privateFontLoader, privateFontLoader.Key);
                //    //        }

                //    //        _currentFontCollection = privateFontCollection;

                //    //        var family = _currentFontCollection.GetFontFamily(0);
                //    //        // This is generic right now.  We should be able to handle different styles in the future
                //    //        var font = family.GetFirstMatchingFont(FontWeight.Regular, FontStretch.Normal, FontStyle.Normal);
                //    //        _currentFont = font;
                //    //        _currentFontSizeEm = fontSize;
                //    //        _currentDIP = ConvertPointSizeToDIP(fontSize);
                //    //    }
                //    //    catch
                //    //    {
                //    //        _currentFont = _defaultFont;
                //    //        _currentFontSizeEm = fontSize;
                //    //        _currentDIP = ConvertPointSizeToDIP(fontSize);
                //    //    }
                //    //}

                //    //else
                //    //{
                //    //    _currentFont = _defaultFont;
                //    //    _currentFontSizeEm = fontSize;
                //    //    _currentDIP = ConvertPointSizeToDIP(fontSize);
                //    //}
                //}
                //else
                //{
                    _currentFont = GetFont(fontName, fontSize);
                    _currentFontSizeEm = fontSize;
                    _currentDIP = ConvertPointSizeToDIP(fontSize);
                //}

                _fontFamilyCache.Add(fontName, _currentFont.FontFamily);
            }
            else
            {
                _currentFont = fontFamily.GetFirstMatchingFont(FontWeight.Regular, FontStretch.Normal, FontStyle.Normal);
                _currentFontSizeEm = fontSize;
                _currentDIP = ConvertPointSizeToDIP(fontSize);
            }

            fontName = _currentFont.FontFamily.FamilyNames.GetString(0); 
            textFormat = new TextFormat(FactoryDWrite, fontName, 
                _currentFontCollection, FontWeight.Regular, FontStyle.Normal, FontStretch.Normal, _currentDIP);
            
            GetKerningInfo(charset);

            return _currentFont.ToString();
        }
        private static void LoadCustomFonts()
        {
            FontLoader currentResourceFontLoader = new FontLoader(Factories.FactoryWrite);
            FontCollection currentFontCollection = new FontCollection(Factories.FactoryWrite, currentResourceFontLoader, currentResourceFontLoader.Key);

            SmallFont = new TextFormat(Factories.FactoryWrite, "Braciola MS", currentFontCollection, FontWeight.Normal, FontStyle.Normal, FontStretch.Normal, 24);
            SmallFont.TextAlignment = TextAlignment.Center;
            SmallFont.ParagraphAlignment = ParagraphAlignment.Center;

            RegularFont = new TextFormat(Factories.FactoryWrite, "Braciola MS", currentFontCollection, FontWeight.Normal, FontStyle.Normal, FontStretch.Normal, 48);
            RegularFont.TextAlignment = TextAlignment.Center;
            RegularFont.ParagraphAlignment = ParagraphAlignment.Center;

            BoldFont = new TextFormat(Factories.FactoryWrite, "Braciola MS", currentFontCollection, FontWeight.ExtraBold, FontStyle.Normal, FontStretch.Expanded, 128);
            BoldFont.TextAlignment = TextAlignment.Center;
            BoldFont.ParagraphAlignment = ParagraphAlignment.Center;

            BoldStencilFont = new TextFormat(Factories.FactoryWrite, "Braciola MS ExB Stencil", currentFontCollection, FontWeight.Normal, FontStyle.Normal, FontStretch.Normal, 128);
            BoldStencilFont.TextAlignment = TextAlignment.Center;
            BoldStencilFont.ParagraphAlignment = ParagraphAlignment.Far;
        }
Exemple #12
0
 private void _loadFontResources()
 {
     _fontLoader = new ResourceFontCollectionLoader(_dwriteFactory);
     _embeddedFonts = new FontCollection(_dwriteFactory, _fontLoader, _fontLoader.Key);
     _systemFonts = _dwriteFactory.GetSystemFontCollection(true);
 }