// Token: 0x06006882 RID: 26754 RVA: 0x001D7B84 File Offset: 0x001D5D84
        private void AddGlyphRunRecursive(Drawing drawing, IList glyphRunsCollection, ref int cchGlyphRuns)
        {
            DrawingGroup drawingGroup = drawing as DrawingGroup;

            if (drawingGroup != null)
            {
                using (DrawingCollection.Enumerator enumerator = drawingGroup.Children.GetEnumerator())
                {
                    while (enumerator.MoveNext())
                    {
                        Drawing drawing2 = enumerator.Current;
                        this.AddGlyphRunRecursive(drawing2, glyphRunsCollection, ref cchGlyphRuns);
                    }
                    return;
                }
            }
            GlyphRunDrawing glyphRunDrawing = drawing as GlyphRunDrawing;

            if (glyphRunDrawing != null)
            {
                GlyphRun glyphRun = glyphRunDrawing.GlyphRun;
                if (glyphRun != null)
                {
                    cchGlyphRuns += ((glyphRun.Characters == null) ? 0 : glyphRun.Characters.Count);
                    glyphRunsCollection.Add(glyphRun);
                }
            }
        }
Esempio n. 2
0
        public GlyphRunDrawingExample()
        {
            // <SnippetGlyphRunDrawingExampleInline>
            GlyphRun theGlyphRun = new GlyphRun(
                new GlyphTypeface(new Uri(@"C:\WINDOWS\Fonts\TIMES.TTF")),
                0,
                false,
                13.333333333333334,
                new ushort[] { 43, 72, 79, 79, 82, 3, 58, 82, 85, 79, 71 },
                new Point(0, 12.29),
                new double[] {
                9.62666666666667, 7.41333333333333, 2.96,
                2.96, 7.41333333333333, 3.70666666666667,
                12.5866666666667, 7.41333333333333,
                4.44, 2.96, 7.41333333333333
            },
                null,
                null,
                null,
                null,
                null,
                null

                );

            GlyphRunDrawing gDrawing = new GlyphRunDrawing(Brushes.Black, theGlyphRun);
            // </SnippetGlyphRunDrawingExampleInline>

            //
            // Use a DrawingImage and an Image control
            // to display the drawing.
            //
            DrawingImage theDrawingImage = new DrawingImage(gDrawing);

            // Freeze the DrawingImage for performance benefits.
            theDrawingImage.Freeze();

            Image anImage = new Image();

            anImage.Source              = theDrawingImage;
            anImage.Stretch             = Stretch.None;
            anImage.HorizontalAlignment = HorizontalAlignment.Left;

            //
            // Place the image inside a border and
            // add it to the page.
            //
            Border exampleBorder = new Border();

            exampleBorder.Child               = anImage;
            exampleBorder.BorderBrush         = Brushes.Gray;
            exampleBorder.BorderThickness     = new Thickness(1);
            exampleBorder.HorizontalAlignment = HorizontalAlignment.Left;
            exampleBorder.VerticalAlignment   = VerticalAlignment.Top;
            exampleBorder.Margin              = new Thickness(10);

            this.Margin     = new Thickness(20);
            this.Background = Brushes.White;
            this.Content    = exampleBorder;
        }
Esempio n. 3
0
        /// <summary>
        /// To the image source.
        /// </summary>
        /// <param name="icon">The icon.</param>
        /// <param name="size">The size.</param>
        /// <param name="color">The color.</param>
        /// <returns></returns>
        public static ImageSource ToImageSource(this IIcon icon, Xamarin.Forms.Color color)
        {
            var character = $"{icon.Character}";
            var module    = Iconize.FindModuleOf(icon);

            var typeface = new Typeface(new FontFamily(new Uri("pack://application:,,,/"), $"/#{module.FontFamily}"), FontStyles.Normal, FontWeights.Regular, FontStretches.Normal);

            if (!typeface.TryGetGlyphTypeface(out var glyphTypeface))
            {
                throw new InvalidOperationException("No glyphtypeface found");
            }

            var glyphIndexes  = new UInt16[character.Length];
            var advanceWidths = new Double[character.Length];

            for (int n = 0; n < character.Length; n++)
            {
                ushort glyphIndex = glyphTypeface.CharacterToGlyphMap[character[n]];
                glyphIndexes[n] = glyphIndex;
                double width = glyphTypeface.AdvanceWidths[glyphIndex] * 1.0;
                advanceWidths[n] = width;
            }

            var gr = new GlyphRun(glyphTypeface, 0, false, 1.0, glyphIndexes,
                                  new System.Windows.Point(0, 0), advanceWidths,
                                  null, null, null, null, null, null);

            var glyphRunDrawing = new GlyphRunDrawing(new SolidColorBrush(ToUIColor(color)), gr);

            return(new DrawingImage(glyphRunDrawing));
        }
Esempio n. 4
0
        private static ImageSource CreateGlyph(string text, FontFamily fontFamily, FontStyle fontStyle, FontWeight fontWeight, FontStretch fontStretch, Brush foreBrush)
        {
            if (fontFamily != null && !string.IsNullOrEmpty(text))
            {
                var typeface = new Typeface(fontFamily, fontStyle, fontWeight, fontStretch);

                GlyphTypeface glyphTypeface;
                if (!typeface.TryGetGlyphTypeface(out glyphTypeface))
                {
                    throw Log.ErrorAndCreateException <InvalidOperationException>("No glyph type face found");
                }

                var glyphIndexes  = new ushort[text.Length];
                var advanceWidths = new double[text.Length];

                for (var i = 0; i < text.Length; i++)
                {
                    ushort glyphIndex;

                    try
                    {
                        var key = text[i];

                        if (!glyphTypeface.CharacterToGlyphMap.TryGetValue(key, out glyphIndex))
                        {
                            glyphIndex = 42;
                        }
                    }
                    catch (Exception)
                    {
                        glyphIndex = 42;
                    }

                    glyphIndexes[i] = glyphIndex;

                    var width = glyphTypeface.AdvanceWidths[glyphIndex] * 1.0;
                    advanceWidths[i] = width;
                }

                try
                {
                    var glyphRun        = new GlyphRun(glyphTypeface, 0, false, RenderingEmSize, glyphIndexes, new Point(0, 0), advanceWidths, null, null, null, null, null, null);
                    var glyphRunDrawing = new GlyphRunDrawing(foreBrush, glyphRun);

                    //TextOptions.SetTextRenderingMode(glyphRunDrawing, TextRenderingMode.Aliased);

                    var drawingImage = new DrawingImage(glyphRunDrawing);

                    //TextOptions.SetTextRenderingMode(drawingImage, TextRenderingMode.Aliased);

                    return(drawingImage);
                }
                catch (Exception ex)
                {
                    Log.Error(ex, "Error in generating Glyphrun");
                }
            }

            return(null);
        }
        public static ImageSource ToFontAwesomeIcon(this string text, Brush foreBrush, FontStyle fontStyle, FontWeight fontWeight, FontStretch fontStretch)
        {
            var fontFamily = new FontFamily("/GitWorkItems;component/Resources/#FontAwesome");

            if (fontFamily != null && !String.IsNullOrEmpty(text))
            {
                //premier essai, on charge la police directement
                Typeface typeface = new Typeface(fontFamily, fontStyle, fontWeight, fontStretch);

                GlyphTypeface glyphTypeface;
                if (!typeface.TryGetGlyphTypeface(out glyphTypeface))
                {
                    //si ça ne fonctionne pas (et pour le mode design dans certains cas) on ajoute l'uri pack://application
                    typeface = new Typeface(new FontFamily(new Uri("pack://application:,,,"), fontFamily.Source), fontStyle, fontWeight, fontStretch);
                    if (!typeface.TryGetGlyphTypeface(out glyphTypeface))
                    {
                        throw new InvalidOperationException("No glyphtypeface found");
                    }
                }

                //détermination des indices/tailles des caractères dans la police
                ushort[] glyphIndexes  = new ushort[text.Length];
                double[] advanceWidths = new double[text.Length];

                for (int n = 0; n < text.Length; n++)
                {
                    ushort glyphIndex;
                    try
                    {
                        glyphIndex = glyphTypeface.CharacterToGlyphMap[text[n]];
                    }
                    catch (Exception)
                    {
                        glyphIndex = 42;
                    }
                    glyphIndexes[n] = glyphIndex;

                    double width = glyphTypeface.AdvanceWidths[glyphIndex] * 1.0;
                    advanceWidths[n] = width;
                }

                try
                {
                    //création de l'objet DrawingImage (compatible avec Imagesource) à partir d'un glyphrun
                    GlyphRun gr = new GlyphRun(glyphTypeface, 0, false, 1.0, glyphIndexes,
                                               new Point(0, 0), advanceWidths, null, null, null, null, null, null);

                    GlyphRunDrawing glyphRunDrawing = new GlyphRunDrawing(foreBrush, gr);
                    return(new DrawingImage(glyphRunDrawing));
                }
                catch (Exception ex)
                {
                    // ReSharper disable LocalizableElement
                    Console.WriteLine("Error in generating Glyphrun : " + ex.Message);
                    // ReSharper restore LocalizableElement
                }
            }
            return(null);
        }
Esempio n. 6
0
        private bool HitTestDrawing(GlyphRunDrawing drawing, Point pt)
        {
            if (drawing != null && drawing.Bounds.Contains(pt))
            {
                return(true);
            }

            return(false);
        }
Esempio n. 7
0
        private bool HitTestDrawing(DrawingGroup group, Geometry geomDisplay, out Drawing hitDrawing, IntersectionDetail detail)
        {
            hitDrawing = null;

            var geomBounds = new RectangleGeometry(group.Bounds);

            if (geomBounds.FillContainsWithDetail(geomDisplay) == detail)
            {
                DrawingGroup    groupDrawing    = null;
                GlyphRunDrawing glyRunDrawing   = null;
                GeometryDrawing geometryDrawing = null;

                DrawingCollection drawings = group.Children;
                for (int i = drawings.Count - 1; i >= 0; i--)
                {
                    Drawing drawing = drawings[i];
                    if (TryCast.Cast(drawing, out geometryDrawing))
                    {
                        if (HitTestDrawing(geometryDrawing, geomDisplay, detail))
                        {
                            hitDrawing = geometryDrawing;
                            return(true);
                        }
                    }
                    else if (TryCast.Cast(drawing, out groupDrawing))
                    {
                        SvgObjectType objectType = SvgObject.GetType(groupDrawing);
                        if (objectType == SvgObjectType.Text)
                        {
                            var textBounds = new RectangleGeometry(groupDrawing.Bounds);
                            if (textBounds.FillContainsWithDetail(geomDisplay) == detail)
                            {
                                hitDrawing = groupDrawing;
                                return(true);
                            }
                        }
                        if (HitTestDrawing(groupDrawing, geomDisplay, out hitDrawing, detail))
                        {
                            return(true);
                        }
                    }
                    else if (TryCast.Cast(drawing, out glyRunDrawing))
                    {
                        if (HitTestDrawing(glyRunDrawing, geomDisplay, detail))
                        {
                            hitDrawing = glyRunDrawing;
                            return(true);
                        }
                    }
                }

                return(true);
            }

            return(false);
        }
Esempio n. 8
0
        static System.Windows.Media.ImageSource CreateGlyph(
            string text,
            FontFamily fontFamily,
            FontStyle fontStyle,
            FontWeight fontWeight,
            FontStretch fontStretch,
            double fontSize,
            WBrush foreBrush)
        {
            if (fontFamily == null || string.IsNullOrEmpty(text))
            {
                return(null);
            }
            var typeface = new Typeface(fontFamily, fontStyle, fontWeight, fontStretch);

            if (!typeface.TryGetGlyphTypeface(out GlyphTypeface glyphTypeface))
            {
                //if it does not work
                return(null);
            }

            var glyphIndexes  = new ushort[text.Length];
            var advanceWidths = new double[text.Length];

            for (int n = 0; n < text.Length; n++)
            {
                var glyphIndex = glyphTypeface.CharacterToGlyphMap[text[n]];
                glyphIndexes[n] = glyphIndex;
                var width = glyphTypeface.AdvanceWidths[glyphIndex] * 1.0;
                advanceWidths[n] = width;
            }

#if NETCOREAPP3_1
            var dpi = VisualTreeHelper.GetDpi(System.Windows.Application.Current.MainWindow).PixelsPerDip;
            var gr  = new GlyphRun(glyphTypeface,
                                   0,
                                   false,
                                   fontSize,
                                   (float)dpi,
                                   glyphIndexes,
                                   new System.Windows.Point(0, 0),
                                   advanceWidths,
                                   null, null, null, null, null, null);
#else
            var gr = new GlyphRun(glyphTypeface,
                                  0, false,
                                  fontSize,
                                  glyphIndexes,
                                  new System.Windows.Point(0, 0),
                                  advanceWidths,
                                  null, null, null, null, null, null);
#endif
            var glyphRunDrawing = new GlyphRunDrawing(foreBrush, gr);
            return(new DrawingImage(glyphRunDrawing));
        }
Esempio n. 9
0
        private static ImageSource ToImageSource(Brush foregroundBrush, double size, GlyphTypeface gt, ushort glyphIndex)
        {
            var fontSize = PixelsToPoints(size);
            var width    = gt.AdvanceWidths[glyphIndex];
            var glyphRun = new GlyphRun(gt, 0, false, fontSize,
                                        new[] { glyphIndex }, new Point(0, 0), new[] { width },
                                        null, null, null, null, null, null);
            var glyphRunDrawing = new GlyphRunDrawing(foregroundBrush ?? DefaultBrush, glyphRun);

            return(new DrawingImage(glyphRunDrawing));
        }
Esempio n. 10
0
        private bool HitTestDrawing(DrawingGroup group, Point pt, out Drawing hitDrawing)
        {
            hitDrawing = null;

            if (group.Bounds.Contains(pt))
            {
                DrawingGroup    groupDrawing    = null;
                GlyphRunDrawing glyRunDrawing   = null;
                GeometryDrawing geometryDrawing = null;

                DrawingCollection drawings = group.Children;
                for (int i = drawings.Count - 1; i >= 0; i--)
                {
                    Drawing drawing = drawings[i];
                    if (TryCast.Cast(drawing, out geometryDrawing))
                    {
                        if (HitTestDrawing(geometryDrawing, pt))
                        {
                            hitDrawing = drawing;
                            return(true);
                        }
                    }
                    else if (TryCast.Cast(drawing, out groupDrawing))
                    {
                        SvgObjectType objectType = SvgObject.GetType(groupDrawing);
                        //if (objectType == SvgObjectType.Text && groupDrawing.Bounds.Contains(pt))
                        if (objectType == SvgObjectType.Text)
                        {
                            hitDrawing = drawing;
                            return(true);
                        }
                        if (HitTestDrawing(groupDrawing, pt, out hitDrawing))
                        {
                            return(true);
                        }
                    }
                    else if (TryCast.Cast(drawing, out glyRunDrawing))
                    {
                        if (HitTestDrawing(glyRunDrawing, pt))
                        {
                            hitDrawing = glyRunDrawing;
                            return(true);
                        }
                    }
                }
                string uniqueId = SvgObject.GetUniqueId(group);
                if (!string.IsNullOrWhiteSpace(uniqueId))
                {
                    _hitGroup = group;
                }
            }

            return(false);
        }
Esempio n. 11
0
        private bool HitTestDrawing(GlyphRunDrawing drawing, Point pt)
        {
            GeometryGroup geomGroup = (GeometryGroup)drawing.GlyphRun.BuildGeometry();

            if (drawing.Bounds.Contains(pt) || geomGroup.Bounds.Contains(pt) ||
                geomGroup.FillContains(pt, 1, ToleranceType.Absolute))
            {
                return(true);
            }
            return(false);
        }
 private static Drawing SimplifyGlyphRunDrawing(GlyphRunDrawing glyphRunDrawing)
 {
     if (glyphRunDrawing.ForegroundBrush == null || glyphRunDrawing.GlyphRun == null)
     {
         return((Drawing)null);
     }
     System.Windows.Media.Geometry geometry = glyphRunDrawing.GlyphRun.BuildGeometry();
     if (geometry == null || geometry.IsEmpty())
     {
         return((Drawing)null);
     }
     return(MakeDrawingBrushCommand.SimplifyGeometryDrawing(new GeometryDrawing(glyphRunDrawing.ForegroundBrush, (Pen)null, geometry)));
 }
Esempio n. 13
0
        private bool HitTestDrawing(GlyphRunDrawing drawing, Geometry geomDisplay, IntersectionDetail detail)
        {
            if (drawing != null)
            {
                var textBounds = new RectangleGeometry(drawing.Bounds);
                if (textBounds.FillContainsWithDetail(geomDisplay) == detail)
                {
                    return(true);
                }
            }

            return(false);
        }
Esempio n. 14
0
        private void AddTextDrawing(DrawingGroup group, string name)
        {
            bool isHyperlink = false;

            if (name == "x11201_1_")
            {
                isHyperlink = true;
            }

            SolidColorBrush textBrush = null;

            if (name.StartsWith("XMLID_", StringComparison.OrdinalIgnoreCase))
            {
                textBrush = new SolidColorBrush(_colorText);
            }
            else
            {
                isHyperlink = true;
                textBrush   = new SolidColorBrush(_colorLink);
            }
            string brushName = name + "_Brush";

            SvgObject.SetName(textBrush, brushName);

            DrawingCollection drawings = group.Children;
            int itemCount = drawings != null ? drawings.Count : 0;

            for (int i = 0; i < itemCount; i++)
            {
                DrawingGroup drawing = drawings[i] as DrawingGroup;
                if (drawing != null)
                {
                    for (int j = 0; j < drawing.Children.Count; j++)
                    {
                        GlyphRunDrawing glyphDrawing = drawing.Children[j] as GlyphRunDrawing;
                        if (glyphDrawing != null)
                        {
                            glyphDrawing.ForegroundBrush = textBrush;
                        }
                    }
                }
            }

            if (isHyperlink)
            {
                _visualBrushes[name] = textBrush;

                _linkObjects.Add(group);
            }
        }
Esempio n. 15
0
        public static ImageSource CreateGlyph(string text, FontFamily fontFamily, FontStyle fontStyle, FontWeight fontWeight, FontStretch fontStretch, Brush foreBrush)
        {
            if (fontFamily != null && !String.IsNullOrEmpty(text))
            {
                var typeface = new Typeface(fontFamily, fontStyle, fontWeight, fontStretch);

                GlyphTypeface glyphTypeface;
                if (!typeface.TryGetGlyphTypeface(out glyphTypeface))
                {
                    typeface = new Typeface(new FontFamily(new Uri("pack://application:,,,"), fontFamily.Source), fontStyle, fontWeight, fontStretch);
                    if (!typeface.TryGetGlyphTypeface(out glyphTypeface))
                    {
                        throw new InvalidOperationException("No glyphtypeface found");
                    }
                }

                var glyphIndexes  = new ushort[text.Length];
                var advanceWidths = new double[text.Length];

                for (var n = 0; n < text.Length; n++)
                {
                    ushort glyphIndex;
                    try {
                        glyphIndex = glyphTypeface.CharacterToGlyphMap[text[n]];
                    }
                    catch (Exception) {
                        glyphIndex = 42;
                    }
                    glyphIndexes[n] = glyphIndex;

                    var width = glyphTypeface.AdvanceWidths[glyphIndex] * 1.0;
                    advanceWidths[n] = width;
                }

                try {
                    var gr = new GlyphRun(glyphTypeface, 0, false, 1.0, glyphIndexes,
                                          new Point(0, 0), advanceWidths, null, null, null, null, null, null);

                    var glyphRunDrawing = new GlyphRunDrawing(foreBrush, gr);
                    return(new DrawingImage(glyphRunDrawing));
                }
                catch (Exception ex) {
                    // ReSharper disable LocalizableElement
                    Console.WriteLine("Error in generating Glyphrun : " + ex.Message);
                    // ReSharper restore LocalizableElement
                }
            }
            return(null);
        }
Esempio n. 16
0
        static void RenderTo(this GlyphRunDrawing drawing, d.Graphics graphics, double opacity)
        {
            var geo   = drawing.GlyphRun.BuildGeometry();
            var brush = drawing.ForegroundBrush;

            if (geo != null && brush != null)
            {
                if (!Utility.IsZero(opacity - 1))
                {
                    brush          = brush.Clone();
                    brush.Opacity *= opacity;
                }
                graphics.FillPath(brush.ToGdiPlus(geo.Bounds), geo.ToGdiPlus());
            }
        }
Esempio n. 17
0
        private static ImageSource CreateGlyph(string text, FontFamily fontFamily, FontStyle fontStyle, FontWeight fontWeight, FontStretch fontStretch, Brush foreBrush)
        {
            if (fontFamily != null && !string.IsNullOrEmpty(text))
            {
                var typeface = new Typeface(fontFamily, fontStyle, fontWeight, fontStretch);

                GlyphTypeface glyphTypeface;
                if (!typeface.TryGetGlyphTypeface(out glyphTypeface))
                {
                    Log.ErrorAndThrowException <InvalidOperationException>("No glyph type face found");
                }

                var glyphIndexes  = new ushort[text.Length];
                var advanceWidths = new double[text.Length];
                for (var i = 0; i < text.Length; i++)
                {
                    ushort glyphIndex;

                    try
                    {
                        glyphIndex = glyphTypeface.CharacterToGlyphMap[text[i]];
                    }
                    catch (Exception)
                    {
                        glyphIndex = 42;
                    }

                    glyphIndexes[i] = glyphIndex;

                    var width = glyphTypeface.AdvanceWidths[glyphIndex] * 1.0;
                    advanceWidths[i] = width;
                }

                try
                {
                    var gr = new GlyphRun(glyphTypeface, 0, false, 1.0, glyphIndexes, new Point(0, 0), advanceWidths, null, null, null, null, null, null);
                    var glyphRunDrawing = new GlyphRunDrawing(foreBrush, gr);

                    return(new DrawingImage(glyphRunDrawing));
                }
                catch (Exception ex)
                {
                    Log.Error(ex, "Error in generating Glyphrun");
                }
            }

            return(null);
        }
Esempio n. 18
0
        public static ImageSource ToImageSource(this IconChar iconChar,
                                                Brush foregroundBrush = null, double size = DefaultSize)
        {
            if (TypefaceFor(iconChar.ToChar(), out var gt, out var glyphIndex) == null)
            {
                return(null);
            }
            var fontSize = PixelsToPoints(size);
            var width    = gt.AdvanceWidths[glyphIndex];
            var glyphRun = new GlyphRun(gt, 0, false, fontSize,
                                        new[] { glyphIndex }, new Point(0, 0), new[] { width },
                                        null, null, null, null, null, null);
            var glyphRunDrawing = new GlyphRunDrawing(foregroundBrush ?? DefaultBrush, glyphRun);

            return(new DrawingImage(glyphRunDrawing));
        }
Esempio n. 19
0
 private static void SwapColorsWithoutCloning(Drawing drawing, ColorCallback colorCallback)
 {
     if (drawing != null)
     {
         DrawingGroup group = drawing as DrawingGroup;
         if (group != null)
         {
             for (int i = 0; i < group.Children.Count; i++)
             {
                 SwapColorsWithoutCloning(group.Children[i], colorCallback);
             }
         }
         else
         {
             GeometryDrawing drawing2 = drawing as GeometryDrawing;
             if (drawing2 != null)
             {
                 SwapColorsWithoutCloning(drawing2.Brush, colorCallback);
                 if (drawing2.Pen != null)
                 {
                     SwapColorsWithoutCloning(drawing2.Pen.Brush, colorCallback);
                 }
             }
             else
             {
                 GlyphRunDrawing drawing3 = drawing as GlyphRunDrawing;
                 if (drawing3 != null)
                 {
                     SwapColorsWithoutCloning(drawing3.ForegroundBrush, colorCallback);
                 }
                 else
                 {
                     ImageDrawing drawing4 = drawing as ImageDrawing;
                     if (drawing4 != null)
                     {
                         drawing4.ImageSource = SwapColorsWithoutCloningIfPossible(drawing4.ImageSource, colorCallback);
                     }
                     else if (!(drawing is VideoDrawing))
                     {
                         throw new ArgumentException(string.Format(CultureInfo.CurrentCulture, "UnexpectedDrawingType", new object[] { drawing.GetType().Name }));
                     }
                 }
             }
         }
     }
 }
Esempio n. 20
0
        private static ImageSource CreateGlyph(string text, FontFamily fontFamily, FontStyle fontStyle, FontWeight fontWeight, FontStretch fontStretch, Brush foreBrush)
        {
            if (string.IsNullOrEmpty(text))
            {
                throw new ArgumentException("Text must be specified");
            }
            if (fontFamily == null)
            {
                throw new ArgumentException("FontFamiliy must be specified");
            }

            var typeface = new Typeface(fontFamily, fontStyle, fontWeight, fontStretch);

            if (!typeface.TryGetGlyphTypeface(out var glyphTypeface))
            {
                typeface = new Typeface(new FontFamily(new Uri("pack://application:,,,"), fontFamily.Source), fontStyle, fontWeight, fontStretch);
                if (!typeface.TryGetGlyphTypeface(out glyphTypeface))
                {
                    throw new InvalidOperationException("No glyphtypeface found");
                }
            }

            ushort[] glyphIndexes  = new ushort[text.Length];
            double[] advanceWidths = new double[text.Length];

            for (int n = 0; n < text.Length; n++)
            {
                ushort glyphIndex;
                try
                {
                    glyphIndex = glyphTypeface.CharacterToGlyphMap[text[n]];
                }
                catch (Exception)
                {
                    glyphIndex = 42;
                }
                glyphIndexes[n]  = glyphIndex;
                advanceWidths[n] = glyphTypeface.AdvanceWidths[glyphIndex] * 1.0;
            }

            var glyphRun        = new GlyphRun(glyphTypeface, 0, false, 1.0, glyphIndexes, new Point(0, 0), advanceWidths, null, null, null, null, null, null);
            var glyphRunDrawing = new GlyphRunDrawing(foreBrush, glyphRun);

            return(new DrawingImage(glyphRunDrawing));
        }
Esempio n. 21
0
        static GraphViewWpf()
        {
            var glyphRun        = BuildGlyphRun(" Busy with rendering...");
            var glyphRunDrawing = new GlyphRunDrawing(Brushes.Lavender, glyphRun);
            var tileBrush       = new DrawingBrush
            {
                Drawing       = glyphRunDrawing,
                Viewbox       = new Rect(0, 0, glyphRunDrawing.Bounds.Right * 1.1, glyphRunDrawing.Bounds.Bottom * 1.5),
                ViewboxUnits  = BrushMappingMode.Absolute,
                Viewport      = glyphRunDrawing.Bounds,
                ViewportUnits = BrushMappingMode.Absolute,
                TileMode      = TileMode.Tile
            };
            var drawingRectangle = new RectangleGeometry(new Rect(0, 0, 2000, 2000));
            var drawing          = new GeometryDrawing(tileBrush, null, drawingRectangle);

            _busyWithRenderingDrawing = new DrawingImage(drawing);
        }
        private void UpdateGlyphRun()
        {
            var c = (char)_rand.Next(65, 90);

            if (_fontSize + _direction > 200)
            {
                _direction = -10;
            }

            if (_fontSize + _direction < 20)
            {
                _direction = 10;
            }

            _fontSize += _direction;

            _glyphIndices[0] = _glyphTypeface.GetGlyph(c);

            _characters[0] = c;

            var scale = (double)_fontSize / _glyphTypeface.DesignEmHeight;

            var drawingGroup = new DrawingGroup();

            var glyphRunDrawing = new GlyphRunDrawing
            {
                Foreground = Brushes.Black,
                GlyphRun   = new GlyphRun(_glyphTypeface, _fontSize, _characters, _glyphIndices)
            };

            drawingGroup.Children.Add(glyphRunDrawing);

            var geometryDrawing = new GeometryDrawing
            {
                Pen      = new Pen(Brushes.Black),
                Geometry = new RectangleGeometry {
                    Rect = new Rect(glyphRunDrawing.GlyphRun.Size)
                }
            };

            drawingGroup.Children.Add(geometryDrawing);

            (_imageControl.Source as DrawingImage).Drawing = drawingGroup;
        }
Esempio n. 23
0
        // ReSharper disable once MemberCanBePrivate.Global
        public static ImageSource ToImageSource(string text,
                                                Brush foregroundBrush = null, double size = DefaultSize)
        {
            if (string.IsNullOrWhiteSpace(text))
            {
                return(null);
            }

            var glyphIndexes  = new ushort[text.Length];
            var advanceWidths = new double[text.Length];

            for (var n = 0; n < text.Length; n++)
            {
                ushort glyphIndex;
                try
                {
                    glyphIndex = GlyphTypeface.CharacterToGlyphMap[text[n]];
                }
                catch (Exception)
                {
                    glyphIndex = 42;
                }
                glyphIndexes[n] = glyphIndex;

                var width = GlyphTypeface.AdvanceWidths[glyphIndex] * 1.0;
                advanceWidths[n] = width;
            }

            try
            {
                var fontSize = PixelsToPoints(size);
                var glyphRun = new GlyphRun(GlyphTypeface, 0, false, fontSize, glyphIndexes,
                                            new Point(0, 0), advanceWidths, null, null, null, null, null, null);

                var glyphRunDrawing = new GlyphRunDrawing(foregroundBrush ?? DefaultBrush, glyphRun);
                return(new DrawingImage(glyphRunDrawing));
            }
            catch (Exception ex)
            {
                Trace.TraceError($"Error generating GlyphRun : {ex.Message}");
            }
            return(null);
        }
Esempio n. 24
0
        private bool HitTestDrawing(DrawingGroup group, Point pt)
        {
            if (group.Bounds.Contains(pt))
            {
                DrawingGroup      groupDrawing    = null;
                GlyphRunDrawing   glyRunDrawing   = null;
                GeometryDrawing   geometryDrawing = null;
                DrawingCollection drawings        = group.Children;

                for (int i = 0; i < drawings.Count; i++)
                {
                    Drawing drawing = drawings[i];
                    if (TryCast.Cast(drawing, out geometryDrawing))
                    {
                        if (HitTestDrawing(geometryDrawing, pt))
                        {
                            return(true);
                        }
                    }
                    else if (TryCast.Cast(drawing, out groupDrawing))
                    {
                        if (HitTestDrawing(groupDrawing, pt))
                        {
                            return(true);
                        }
                        SvgObjectType objectType = SvgObject.GetType(groupDrawing);
                        if (objectType == SvgObjectType.Text && groupDrawing.Bounds.Contains(pt))
                        {
                            return(true);
                        }
                    }
                    else if (TryCast.Cast(drawing, out glyRunDrawing))
                    {
                        if (HitTestDrawing(glyRunDrawing, pt))
                        {
                            return(true);
                        }
                    }
                }
            }

            return(false);
        }
Esempio n. 25
0
        private void UpdateGlyphRun()
        {
            var c = (uint)_rand.Next(65, 90);

            if (_fontSize + _direction > 200)
            {
                _direction = -10;
            }

            if (_fontSize + _direction < 20)
            {
                _direction = 10;
            }

            _fontSize += _direction;

            _glyphIndices[0] = _glyphTypeface.GetGlyph(c);

            var scale = (double)_fontSize / _glyphTypeface.DesignEmHeight;

            var drawingGroup = new DrawingGroup();

            var glyphRunDrawing = new GlyphRunDrawing
            {
                Foreground     = Brushes.Black,
                GlyphRun       = new GlyphRun(_glyphTypeface, _fontSize, _glyphIndices),
                BaselineOrigin = new Point(0, -_glyphTypeface.Ascent * scale)
            };

            drawingGroup.Children.Add(glyphRunDrawing);

            var geometryDrawing = new GeometryDrawing
            {
                Pen      = new Pen(Brushes.Black),
                Geometry = new RectangleGeometry {
                    Rect = glyphRunDrawing.GlyphRun.Bounds
                }
            };

            drawingGroup.Children.Add(geometryDrawing);

            _drawingPresenter.Drawing = drawingGroup;
        }
Esempio n. 26
0
        static System.Windows.Media.ImageSource CreateGlyph(
            string text,
            FontFamily fontFamily,
            FontStyle fontStyle,
            FontWeight fontWeight,
            FontStretch fontStretch,
            double fontSize,
            Brush foreBrush)
        {
            if (fontFamily == null || string.IsNullOrEmpty(text))
            {
                return(null);
            }
            var typeface = new Typeface(fontFamily, fontStyle, fontWeight, fontStretch);

            if (!typeface.TryGetGlyphTypeface(out GlyphTypeface glyphTypeface))
            {
                //if it does not work
                return(null);
            }

            var glyphIndexes  = new ushort[text.Length];
            var advanceWidths = new double[text.Length];

            for (int n = 0; n < text.Length; n++)
            {
                var glyphIndex = glyphTypeface.CharacterToGlyphMap[text[n]];
                glyphIndexes[n] = glyphIndex;
                var width = glyphTypeface.AdvanceWidths[glyphIndex] * 1.0;
                advanceWidths[n] = width;
            }

            var gr = new GlyphRun(glyphTypeface,
                                  0, false,
                                  fontSize,
                                  glyphIndexes,
                                  new System.Windows.Point(0, 0),
                                  advanceWidths,
                                  null, null, null, null, null, null);
            var glyphRunDrawing = new GlyphRunDrawing(foreBrush, gr);

            return(new DrawingImage(glyphRunDrawing));
        }
Esempio n. 27
0
        private void AddGlyphRunDrawing(ConvertibleDrawing.DrawingStackEntry entry)
        {
            GlyphRunDrawing glyphRunDrawing = entry.Drawing as GlyphRunDrawing;

            System.Windows.Media.Geometry geometry = (System.Windows.Media.Geometry)null;
            if (glyphRunDrawing.GlyphRun != null)
            {
                geometry = glyphRunDrawing.GlyphRun.BuildGeometry();
            }
            Path path = new Path();

            ConvertibleDrawing.SetValueIfNotDefault(Shape.FillProperty, (DependencyObject)path, (object)glyphRunDrawing.ForegroundBrush);
            ConvertibleDrawing.SetValueIfNotDefault(Path.DataProperty, (DependencyObject)path, (object)geometry);
            ConvertibleDrawing.SetPositionInCanvas((UIElement)path, entry.Offset);
            if (entry.Transform != Transform.Identity)
            {
                ConvertibleDrawing.SetValueIfNotDefault(UIElement.RenderTransformProperty, (DependencyObject)path, (object)entry.Transform);
            }
            entry.Canvas.Children.Add((UIElement)path);
        }
Esempio n. 28
0
 /// <summary>
 /// Enumerates through the drawing group of a visual to extract the geometries.
 /// </summary>
 /// <param name="dg"></param>
 /// <param name="currentParent"></param>
 private static void EnumerateDrawingGroup(DrawingGroup dg, GeometryGroup currentParent)
 {
     for (int i = 0; i < dg.Children.Count; i++)
     {
         if (dg.Children[i] is GeometryDrawing)
         {
             GeometryDrawing geometryDrawing = dg.Children[i] as GeometryDrawing;
             if (geometryDrawing != null)
             {
                 Geometry geometry = geometryDrawing.Geometry;
                 currentParent.Children.Add(geometry);
             }
         }
         else if (dg.Children[i] is DrawingGroup)
         {
             EnumerateDrawingGroup((DrawingGroup)dg.Children[i], currentParent);
         }
         else if (dg.Children[i] is GlyphRunDrawing)
         {
             GlyphRunDrawing glyphrunDrawing = dg.Children[i] as GlyphRunDrawing;
             GlyphRun        glyphrun        = glyphrunDrawing.GlyphRun;
             if (glyphrun != null)
             {
                 Geometry geometry = glyphrun.BuildGeometry();
                 currentParent.Children.Add(geometry);
             }
         }
         else if (dg.Children[i] is ImageDrawing)
         {
             ImageDrawing      imagedrawing = dg.Children[i] as ImageDrawing;
             RectangleGeometry rg           = new RectangleGeometry(imagedrawing.Rect);
             currentParent.Children.Add(rg);
         }
         else if (dg.Children[i] is VideoDrawing)
         {
             VideoDrawing      videodrawing = dg.Children[i] as VideoDrawing;
             RectangleGeometry rg           = new RectangleGeometry(videodrawing.Rect);
             currentParent.Children.Add(rg);
         }
     }
 }
Esempio n. 29
0
        private void CreateVisuals(ITextViewLine line)
        {
            var textViewLines = this.view.TextViewLines;

            for (int charIndex = line.Start; charIndex < line.End; charIndex++)
            {
                if (this.view.TextSnapshot[charIndex] == '=')
                {
                    var span     = new SnapshotSpan(this.view.TextSnapshot, Span.FromBounds(charIndex, charIndex + 1));
                    var geometry = textViewLines.GetMarkerGeometry(span);

                    if (geometry != null)
                    {
                        var props = view.FormattedLineSource.DefaultTextProperties;
                        props.Typeface.TryGetGlyphTypeface(out var glyphTypeface);
                        var fontSize   = props.Typeface.XHeight;
                        var glyphIndex = glyphTypeface.CharacterToGlyphMap['='];
                        var width      = glyphTypeface.AdvanceWidths[glyphIndex] * fontSize;
#pragma warning disable CS0618 // Type or member is obsolete
                        var glyphRun = new GlyphRun(glyphTypeface, 0, false, props.FontHintingEmSize, new ushort[] { glyphIndex },
                                                    new Point(0, props.FontHintingEmSize * 0.66), new double[] { width }, null, null, null, null, null, null);
#pragma warning restore CS0618 // Type or member is obsolete
                        var drawing = new GlyphRunDrawing(this.brush, glyphRun);
                        drawing.Freeze();

                        var drawingImage = new DrawingImage(drawing);
                        drawingImage.Freeze();

                        var image = new Image {
                            Source = drawingImage
                        };

                        // Align the image with the top of the bounds of the text geometry
                        Canvas.SetLeft(image, geometry.Bounds.Left);
                        Canvas.SetTop(image, geometry.Bounds.Top);

                        this.layer.AddAdornment(AdornmentPositioningBehavior.TextRelative, span, null, image, null);
                    }
                }
            }
        }
Esempio n. 30
0
        private void button1_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            Glyphs gp = new Glyphs();

            gp.FontUri             = new Uri(@"C:\WINDOWS\Fonts\SIMSUN.TTC"); //宋体字
            gp.UnicodeString       = "GlyphRunDrawing绘制文本!";
            gp.FontRenderingEmSize = 30;                                      //大小
            gp.IsSideways          = false;                                   //是否旋转
            gp.StyleSimulations    = StyleSimulations.BoldSimulation;         //粗体字
            GlyphRunDrawing grd = new GlyphRunDrawing();

            grd.ForegroundBrush = Brushes.Red;            //字颜色
            grd.GlyphRun        = gp.ToGlyphRun();        //创建GlyphRun字符
            DrawingImage di      = new DrawingImage(grd); //绘制文字用于显示
            Image        myimage = new Image();           //定义图像对象

            myimage.Source = di;                          //作为图像对象
            Canvas.SetLeft(myimage, 20);                  //绘图位置坐标
            Canvas.SetTop(myimage, 100);
            this.canvas1.Children.Add(myimage);           //装入容器显示
        }