Beispiel #1
0
        /// <summary>
        /// Builds the text geometry for the given string.
        /// </summary>
        /// <param name="stringToBuild">The string to build within the geometry.</param>
        /// <param name="geometryOptions">Some configuration for geometry creation.</param>
        public void BuildTextGeometry(string stringToBuild, TextGeometryOptions geometryOptions)
        {
            DWrite.Factory writeFactory = GraphicsCore.Current.FactoryDWrite;

            //TODO: Cache font objects

            //Get DirectWrite font weight
            DWrite.FontWeight fontWeight = DWrite.FontWeight.Normal;
            switch (geometryOptions.FontWeight)
            {
            case FontGeometryWeight.Bold:
                fontWeight = DWrite.FontWeight.Bold;
                break;

            default:
                fontWeight = DWrite.FontWeight.Normal;
                break;
            }

            //Get DirectWrite font style
            DWrite.FontStyle fontStyle = DWrite.FontStyle.Normal;
            switch (geometryOptions.FontStyle)
            {
            case FontGeometryStyle.Italic:
                fontStyle = DWrite.FontStyle.Italic;
                break;

            case FontGeometryStyle.Oblique:
                fontStyle = DWrite.FontStyle.Oblique;
                break;

            default:
                fontStyle = DWrite.FontStyle.Normal;
                break;
            }

            //Create the text layout object
            try
            {
                DWrite.TextLayout textLayout = new DWrite.TextLayout(
                    writeFactory, stringToBuild,
                    new DWrite.TextFormat(
                        writeFactory, geometryOptions.FontFamily, fontWeight, fontStyle, geometryOptions.FontSize),
                    float.MaxValue, float.MaxValue, 1f, true);

                //Render the text using the vertex structure text renderer
                using (VertexStructureTextRenderer textRenderer = new VertexStructureTextRenderer(this, geometryOptions))
                {
                    textLayout.Draw(textRenderer, 0f, 0f);
                }
            }
            catch (SharpDX.SharpDXException)
            {
                //TODO: Display some error
            }
        }
        void textbox_FontChanged(object sender, EventArgs e)
        {
            FooTextBox textbox = (FooTextBox)sender;
            Font       font    = textbox.Font;

            this.fontName = font.Name;
            this.fontSize = font.Size;
            DW.FontWeight weigth = font.Bold ? DW.FontWeight.Bold : DW.FontWeight.Normal;
            DW.FontStyle  style  = font.Italic ? DW.FontStyle.Italic : DW.FontStyle.Normal;
            this.InitTextFormat(font.Name, font.Size, weigth, style);
        }
Beispiel #3
0
            public DWrite.FontFace GetFontFace(int codePoint, DWrite.FontWeight weight, DWrite.FontStyle style)
            {
                var(font, fontFace) = this.GetOrAdd(weight, style);

                if (font.HasCharacter(codePoint))
                {
                    return(fontFace);
                }

                return(null);
            }
Beispiel #4
0
            private (DWrite.Font font, DWrite.FontFace fontFace) GetOrAdd(DWrite.FontWeight weight, DWrite.FontStyle style)
            {
                if (!this.fontCache.TryGetValue((weight, style), out var v))
                {
                    var font     = this.fontFamily.GetFirstMatchingFont(weight, DWrite.FontStretch.Normal, style);
                    var fontFace = new DWrite.FontFace(font);
                    v = (font, fontFace);
                    this.fontCache.Add((weight, style), v);
                }

                return(v);
            }
Beispiel #5
0
 /// <inheritdoc />
 protected TextElement()
 {
     _font            = "Roboto";
     _text            = "Label";
     _fontSize        = 16.0f;
     _fontWeight      = SharpDX.DirectWrite.FontWeight.Normal;
     _textAlignmentX  = AlignmentX.Center;
     _textAlignmentY  = AlignmentY.Middle;
     _wordWrapping    = WordWrapping.NoWrap;
     TextColour       = Colour.Black;
     TextStrokeColour = new Colour(0, 0, 0, 0);
     _textReady       = true;
 }
Beispiel #6
0
        public static void Convert(FontStyle value, out sw.FontStyle fontStyle, out sw.FontWeight fontWeight)
        {
            fontStyle  = sw.FontStyle.Normal;
            fontWeight = sw.FontWeight.Normal;

            if (value.HasFlag(FontStyle.Italic))
            {
                fontStyle = sw.FontStyle.Italic;
            }

            if (value.HasFlag(FontStyle.Bold))
            {
                fontWeight = sw.FontWeight.Bold;
            }
        }
Beispiel #7
0
        public static void Convert(
            FontStyle value,
            out sw.FontStyle fontStyle,
            out sw.FontWeight fontWeight)
        {
            fontStyle  = sw.FontStyle.Normal;
            fontWeight = sw.FontWeight.Normal;

            if ((value & FontStyle.Italic) == FontStyle.Italic)
            {
                fontStyle = sw.FontStyle.Italic;
            }

            if ((value & FontStyle.Bold) == FontStyle.Bold)
            {
                fontWeight = sw.FontWeight.Bold;
            }
        }
Beispiel #8
0
        /// <summary>
        /// Get font face for specified codepoint, weight and style.
        /// </summary>
        /// <param name="codePoint">The code point.</param>
        /// <param name="weight">The font weight.</param>
        /// <param name="style">The font style.</param>
        /// <returns>Selected font face.</returns>
        public DWrite.FontFace GetFontFace(int codePoint, DWrite.FontWeight weight, DWrite.FontStyle style)
        {
            foreach (var f in this.fontFamilies)
            {
                var face = f.GetFontFace(codePoint, weight, style);
                if (face != null)
                {
                    return(face);
                }
            }

            using (var source = new SingleCharTextSource(this.factory, codePoint))
            {
                this.systemFontFallback.MapCharacters(
                    source,
                    0,
                    1,
                    this.fontCollection,
                    this.baseFontFamilyName,
                    weight,
                    style,
                    DWrite.FontStretch.Normal,
                    out var mappedLength,
                    out var font,
                    out var scale);

                if (font != null)
                {
                    FontFamilyCacheItem.TryCreate(this.fontCollection, font.FontFamily.FamilyNames.GetString(0), out var familyCache);
                    this.fontFamilies.Add(familyCache);
                    font.Dispose();

                    var fontFace = familyCache.GetFontFace(codePoint, weight, style);
                    if (fontFace != null)
                    {
                        return(fontFace);
                    }
                }
            }

            // OK we don't have the glyph for this codepoint in the fallbacks.
            // Use primary font to show something like '?'.
            return(this.fontFamilies[0].GetFontFace(weight, style));
        }
Beispiel #9
0
        public static FontStyle Convert(sw.FontStyle fontStyle, sw.FontWeight fontWeight)
        {
            var style = FontStyle.Normal;

            if (fontStyle == sw.FontStyle.Italic)
            {
                style |= FontStyle.Italic;
            }
            if (fontStyle == sw.FontStyle.Oblique)
            {
                style |= FontStyle.Italic;
            }

            if (fontWeight == sw.FontWeight.Bold)
            {
                style |= FontStyle.Bold;
            }
            return(style);
        }
Beispiel #10
0
        public static bool IsBold(this sw.FontWeight weight)
        {
            switch (weight)
            {
            case SharpDX.DirectWrite.FontWeight.Bold:
            case SharpDX.DirectWrite.FontWeight.DemiBold:
            case SharpDX.DirectWrite.FontWeight.ExtraBold:
            case SharpDX.DirectWrite.FontWeight.Heavy:
                return(true);

            case SharpDX.DirectWrite.FontWeight.ExtraBlack:
            case SharpDX.DirectWrite.FontWeight.ExtraLight:
            case SharpDX.DirectWrite.FontWeight.Light:
            case SharpDX.DirectWrite.FontWeight.Medium:
            case SharpDX.DirectWrite.FontWeight.Normal:
            case SharpDX.DirectWrite.FontWeight.Thin:
            default:
                return(false);
            }
        }
        public const int MiniumeWidth = 40;    //これ以上ないと誤操作が起こる

        public void InitTextFormat(string fontName, float fontSize, DW.FontWeight fontWeigth = DW.FontWeight.Normal, DW.FontStyle fontStyle = DW.FontStyle.Normal)
        {
            if (this.format != null)
            {
                this.format.Dispose();
            }

            float dpix, dpiy;

            this.GetDpi(out dpix, out dpiy);

            float fontSizeInDIP = fontSize * (dpix / 72.0f);

            this.format = this._factory.GetTextFormat(fontName, fontSizeInDIP, fontWeigth, fontStyle);
            this.format.WordWrapping     = DW.WordWrapping.NoWrap;
            this.format.ReadingDirection = GetDWRightDirect(_RightToLeft);

            MyTextLayout layout = this._factory.GetTextLayout("0", this.format, float.MaxValue, float.MaxValue, dpix, false);

            layout.RightToLeft = false;
            this.emSize        = new Size(layout.Width, layout.Height);
            layout.Dispose();

            this.TabWidthChar = this.TabWidthChar;

            this.hasCache = false;

            layout             = this._factory.GetTextLayout("+", this.format, float.MaxValue, float.MaxValue, dpix, false);
            layout.RightToLeft = false;
#if METRO
            this.FoldingWidth = Math.Max(D2DRenderCommon.MiniumeWidth, layout.Width);
#else
            this.FoldingWidth = layout.Width;
#endif
            layout.Dispose();

            this._factory.Clear();

            this.OnChangedRenderResource(this, new ChangedRenderRsourceEventArgs(ResourceType.Font));
        }
Beispiel #12
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);
        }
        /// <summary>
        /// Called when [draw texture].
        /// </summary>
        /// <param name="deviceResources">The device resources.</param>
        protected override void OnUpdateTextureAndBillboardVertices(IDeviceResources deviceResources)
        {
            if (TextInfo != null && !string.IsNullOrEmpty(TextInfo.Text))
            {
                var w = Width;
                var h = Height;
#if CORE
                Texture = TextInfo.Text.ToBitmapStream(FontSize, Color.White, Color.Black, FontFamily, FontWeight, FontStyle,
                                                       new Vector4((float)Padding.Left, (float)Padding.Top, (float)Padding.Right, (float)Padding.Bottom), ref w, ref h, predefinedSize, deviceResources);
#else
                Texture = TextInfo.Text.ToBitmapStream(FontSize, Color.White, Color.Black, FontFamily, FontWeight.ToDXFontWeight(), FontStyle.ToDXFontStyle(),
                                                       new Vector4((float)Padding.Left, (float)Padding.Top, (float)Padding.Right, (float)Padding.Bottom), ref w, ref h, predefinedSize, deviceResources);
#endif
                if (!predefinedSize)
                {
                    Width  = w;
                    Height = h;
                }
                DrawCharacter(TextInfo.Text, TextInfo.Origin, Width, Height, TextInfo);
            }
            else
            {
                Texture = null;
                if (!predefinedSize)
                {
                    Width  = 0;
                    Height = 0;
                }
            }
            TextInfo?.UpdateTextInfo(Width, Height);
        }
 public DW.TextFormat GetTextFormat(string fontName, double fontSize, DW.FontWeight fontWeigth = DW.FontWeight.Normal, DW.FontStyle fontStyle = DW.FontStyle.Normal)
 {
     return(new DW.TextFormat(this._DWFactory, fontName, fontWeigth, fontStyle, (float)fontSize));
 }
Beispiel #15
0
 /// <summary>
 /// Get font face from primary font family using specified weight and style.
 /// </summary>
 /// <param name="weight">Font weight.</param>
 /// <param name="style">Font style.</param>
 /// <returns>Selected font face.</returns>
 public DWrite.FontFace GetPrimaryFontFace(DWrite.FontWeight weight, DWrite.FontStyle style)
 {
     return(this.fontFamilies[0].GetFontFace(weight, style));
 }
Beispiel #16
0
 public DWrite.FontFace GetFontFace(DWrite.FontWeight weight, DWrite.FontStyle style)
 {
     return(this.GetOrAdd(weight, style).fontFace);
 }