Example #1
0
        internal static void InitializeDirect2D()
        {
            lock (s_initLock)
            {
                if (s_initialized)
                {
                    return;
                }
#if DEBUG
                try
                {
                    Direct2D1Factory = new SharpDX.Direct2D1.Factory1(
                        SharpDX.Direct2D1.FactoryType.MultiThreaded,
                        SharpDX.Direct2D1.DebugLevel.Error);
                }
                catch
                {
                    //
                }
#endif
                if (Direct2D1Factory == null)
                {
                    Direct2D1Factory = new SharpDX.Direct2D1.Factory1(
                        SharpDX.Direct2D1.FactoryType.MultiThreaded,
                        SharpDX.Direct2D1.DebugLevel.None);
                }

                using (var factory = new SharpDX.DirectWrite.Factory())
                {
                    DirectWriteFactory = factory.QueryInterface <SharpDX.DirectWrite.Factory1>();
                }

                ImagingFactory = new SharpDX.WIC.ImagingFactory();

                var featureLevels = new[]
                {
                    SharpDX.Direct3D.FeatureLevel.Level_11_1,
                    SharpDX.Direct3D.FeatureLevel.Level_11_0,
                    SharpDX.Direct3D.FeatureLevel.Level_10_1,
                    SharpDX.Direct3D.FeatureLevel.Level_10_0,
                    SharpDX.Direct3D.FeatureLevel.Level_9_3,
                    SharpDX.Direct3D.FeatureLevel.Level_9_2,
                    SharpDX.Direct3D.FeatureLevel.Level_9_1,
                };

                Direct3D11Device = new SharpDX.Direct3D11.Device(
                    SharpDX.Direct3D.DriverType.Hardware,
                    SharpDX.Direct3D11.DeviceCreationFlags.BgraSupport | SharpDX.Direct3D11.DeviceCreationFlags.VideoSupport,
                    featureLevels);

                DxgiDevice = Direct3D11Device.QueryInterface <SharpDX.DXGI.Device1>();

                Direct2D1Device = new SharpDX.Direct2D1.Device(Direct2D1Factory, DxgiDevice);

                s_initialized = true;
            }
        }
Example #2
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);
     }
 }
Example #3
0
        private void TextByGlyph(RectangleF rect, string text, TextInfo info, SolidColorBrush brush)
        {
            FontFace fontFace;

            if (!m_faceMap.TryGetValue(info.Font, out fontFace))
            {
                using (var f = new SharpDX.DirectWrite.Factory())
                    using (var collection = f.GetSystemFontCollection(false))
                    {
                        int familyIndex;
                        if (!collection.FindFamilyName(info.Font.FamilylName, out familyIndex))
                        {
                            return;
                        }

                        using (var family = collection.GetFontFamily(familyIndex))
                            using (var font = family.GetFont(0))
                            {
                                fontFace = new FontFace(font);
                                m_faceMap.Add(info.Font, fontFace);
                            }
                    }
            }

            var codePoints = EnumCodePoints(text).ToArray();
            var indices    = fontFace.GetGlyphIndices(codePoints);

            // Get glyph
            var metrices = fontFace.GetDesignGlyphMetrics(indices, false);

            // draw
            var glyphRun = new GlyphRun
            {
                FontFace = fontFace,
                Indices  = indices,
                FontSize = info.Font.Size,
            };

            bool done = false;

            using (var f = new SharpDX.DirectWrite.Factory())
                using (var ff = f.QueryInterface <SharpDX.DirectWrite.Factory4>())
                {
                    var desc = new GlyphRunDescription
                    {
                    };
                    ColorGlyphRunEnumerator it;
                    var result = ff.TryTranslateColorGlyphRun(0, 0, glyphRun,
                                                              null, MeasuringMode.Natural, null, 0, out it);
                    if (result.Code == DWRITE_E_NOCOLORLOR)
                    {
                        m_device.D2DDeviceContext.DrawGlyphRun(rect.TopLeft + new Vector2(0, info.Font.Size), glyphRun, brush, MeasuringMode.Natural);
                    }
                    else
                    {
                        while (true)
                        {
                            var colorBrush = GetOrCreateBrush(new Color4(
                                                                  it.CurrentRun.RunColor.R,
                                                                  it.CurrentRun.RunColor.G,
                                                                  it.CurrentRun.RunColor.B,
                                                                  it.CurrentRun.RunColor.A));
                            m_device.D2DDeviceContext.DrawGlyphRun(rect.TopLeft + new Vector2(0, info.Font.Size),
                                                                   it.CurrentRun.GlyphRun, colorBrush, MeasuringMode.Natural);
                            done = true;

                            SharpDX.Mathematics.Interop.RawBool hasNext;
                            it.MoveNext(out hasNext);
                            if (!hasNext)
                            {
                                break;;
                            }
                        }
                    }
                }
        }