Example #1
0
            /// <summary>
            /// Creates a run reflecting the current context settings.
            /// </summary>
            /// <returns></returns>
            public Run CreateRun(string text)
            {
                var run = new Run {
                    Text = text
                };

                if (FontSize.HasValue)
                {
                    run.FontSize = FontSize.Value;
                }
                if (FontWeight.HasValue)
                {
                    run.FontWeight = FontWeight.Value;
                }
                if (FontStyle.HasValue)
                {
                    run.FontStyle = FontStyle.Value;
                }
                if (FontVariants.HasValue)
                {
                    Typography.SetVariants(run, FontVariants.Value);
                }
                if (Foreground != null)
                {
                    run.Foreground = Foreground;
                }
                if (FontFamily != null)
                {
                    run.FontFamily = FontFamily;
                }
                run.TextDecorations = TextDecorations;
                return(run);
            }
Example #2
0
        public override List <Run> DisplayText()
        {
            List <Run> display = new List <Run>();

            display.Add(new Run()
            {
                Text = "("
            });
            foreach (Element e in Elements)
            {
                display.AddRange(e.DisplayText());
            }
            display.Add(new Run()
            {
                Text = ")"
            });
            if (Subscript > 1)
            {
                Run r = new Run()
                {
                    Text = Subscript.ToString()
                };
                Typography.SetVariants(r, Windows.UI.Xaml.FontVariants.Subscript);
                display.Add(r);
            }

            return(display);
        }
Example #3
0
        private Inline CreateSubscript(HtmlNode node)
        {
            if (string.IsNullOrEmpty(node.InnerText.Trim()))
            {
                return(null);
            }
            var run = new Run()
            {
                Text = node.InnerText.Trim()
            };

            Typography.SetVariants(run, FontVariants.Subscript);
            return(run);
        }
Example #4
0
 private Inline CreateSuperscript(HtmlNode node)
 {
     if (node.Descendants().Any(p => p.Name == "a"))
     {
         var linkNode = node.Descendants().Where(p => p.Name == "a").FirstOrDefault();
         var hyp      = CreateHyperLink(linkNode, true);
         return(hyp);
     }
     else
     {
         if (string.IsNullOrEmpty(node.InnerText.Trim()))
         {
             return(null);
         }
         var run = new Run()
         {
             Text = node.InnerText.Trim()
         };
         Typography.SetVariants(run, FontVariants.Superscript);
         return(run);
     }
 }
Example #5
0
        public override List <Run> DisplayText()
        {
            List <Run> display = new List <Run>();

            display.Add(new Run()
            {
                Text = Symbol
            });
            if (Subscript > 1)
            {
                Run r = new Run()
                {
                    Text = Subscript.ToString()
                };
                Typography.SetVariants(r, Windows.UI.Xaml.FontVariants.Subscript);
                display.Add(r);
            }
            if (Charge != 0)
            {
                if (Charge < 0)
                {
                    //This won't convert a - to a ⁻ for some reason?
                    Run r1 = new Run()
                    {
                        Text = "⁻"
                    };
                    display.Add(r1);
                }

                Run r = new Run()
                {
                    Text = Math.Abs(Charge).ToString()
                };
                Typography.SetVariants(r, Windows.UI.Xaml.FontVariants.Superscript);
                display.Add(r);
            }

            return(display);
        }
        static void AddInline(TextBlock textBlock, Forms9Patch.Label label, MetaFont metaFont, string text, int startIndex, int length)
        {
            var run = new Run();

            run.Text       = text.Substring(startIndex, length);
            run.FontSize   = metaFont.Size;
            run.FontWeight = metaFont.Bold ? Windows.UI.Text.FontWeights.Bold : Windows.UI.Text.FontWeights.Normal;
            run.FontStyle  = metaFont.Italic ? Windows.UI.Text.FontStyle.Italic : Windows.UI.Text.FontStyle.Normal;


            if (TextDecorationsPresent && metaFont.Strikethrough)
            {
                ApplyTextDecorations(run, Decoration.Strikethrough);
            }

            switch (metaFont.Baseline)
            {
            case FontBaseline.Numerator:
                run.FontFamily = new Windows.UI.Xaml.Media.FontFamily("Cambria");
                Typography.SetVariants(run, Windows.UI.Xaml.FontVariants.Superscript);
                break;

            case FontBaseline.Superscript:
                run.FontFamily = new Windows.UI.Xaml.Media.FontFamily("Cambria");
                Typography.SetVariants(run, Windows.UI.Xaml.FontVariants.Superscript);
                break;

            case FontBaseline.Denominator:
                run.FontFamily = new Windows.UI.Xaml.Media.FontFamily("Cambria");
                Typography.SetVariants(run, Windows.UI.Xaml.FontVariants.Subscript);
                break;

            case FontBaseline.Subscript:
                run.FontFamily = new Windows.UI.Xaml.Media.FontFamily("Cambria");
                Typography.SetVariants(run, Windows.UI.Xaml.FontVariants.Subscript);
                break;

            default:
                if (metaFont.Family != null)
                {
                    run.FontFamily = new Windows.UI.Xaml.Media.FontFamily(FontService.ReconcileFontFamily(metaFont.Family));
                }
                break;
            }

            if (!metaFont.BackgroundColor.IsDefaultOrTransparent())
            {
                try
                {
                    textBlock.ApplyBackgroundColor(metaFont.BackgroundColor, startIndex, length);
                }
                catch (Exception)
                {
                    //throw new Exception("It appears that this Xamarin.Forms.UWP app was built with a Windows TargetVersion < 10.0.16299.0 (Windows 10 Fall Creators Update).  10.0.16299.0 is needed to support Forms9Patch.Label.HtmlText background color attributes.", e);
                }
            }

            if (metaFont.IsActionEmpty())
            {
                run.Foreground = new Windows.UI.Xaml.Media.SolidColorBrush(metaFont.TextColor.ToWindowsColor());
                if (TextDecorationsPresent && metaFont.Underline)
                {
                    ApplyTextDecorations(run, Decoration.Underline);
                }
                textBlock.Inlines.Add(run);
            }
            else
            {
                run.Foreground = new Windows.UI.Xaml.Media.SolidColorBrush(Xamarin.Forms.Color.Blue.ToWindowsColor());
                if (TextDecorationsPresent)
                {
                    ApplyTextDecorations(run, Decoration.Underline);
                }
                var hyperlink = new Hyperlink();
                hyperlink.Inlines.Add(run);
                hyperlink.Click += (Hyperlink sender, HyperlinkClickEventArgs args) => label.Tap(metaFont.Action.Id, metaFont.Action.Href);
                textBlock.Inlines.Add(hyperlink);
            }
        }
Example #7
0
        private static IEnumerable <Inline> Parse(IEnumerable <XNode> nodes, FontFamily fontFamily, Brush fontColor, FontWeight fontWeight, FontStyle fontStyle, TextDecorations textDecoration, FontVariants fontVariants)
        {
            foreach (var current in nodes)
            {
                switch (current)
                {
                case XText xText:
                    var run = new Run()
                    {
                        Text = xText.Value, FontWeight = fontWeight, FontStyle = fontStyle, TextDecorations = textDecoration
                    };
                    if (fontFamily != null)
                    {
                        run.FontFamily = fontFamily;
                    }
                    if (fontColor != null)
                    {
                        run.Foreground = fontColor;
                    }
                    Typography.SetVariants(run, fontVariants);
                    yield return(run);

                    break;

                case XElement xElement:
                {
                    switch (xElement.Name.LocalName.ToLower())
                    {
                    case "b":
                    case "strong":
                    {
                        FontWeight newFontWeight = FontWeights.Bold;
                        var        weight        = xElement.Attribute("weight")?.Value;
                        if (!string.IsNullOrEmpty(weight))
                        {
                            switch (weight.ToLower())
                            {
                            case "950":
                            case "extrablack":
                                newFontWeight = FontWeights.ExtraBlack;
                                break;

                            case "900":
                            case "black":
                                newFontWeight = FontWeights.Black;
                                break;

                            case "800":
                            case "extrabold":
                                newFontWeight = FontWeights.ExtraBold;
                                break;

                            case "700":
                            case "bold":
                                newFontWeight = FontWeights.Bold;
                                break;

                            case "600":
                            case "semibold":
                                newFontWeight = FontWeights.SemiBold;
                                break;

                            case "500":
                            case "medium":
                                newFontWeight = FontWeights.Medium;
                                break;

                            case "400":
                            case "normal":
                                newFontWeight = FontWeights.Normal;
                                break;

                            case "350":
                            case "semilight":
                                newFontWeight = FontWeights.SemiLight;
                                break;

                            case "300":
                            case "light":
                                newFontWeight = FontWeights.Light;
                                break;

                            case "200":
                            case "extralight":
                                newFontWeight = FontWeights.ExtraLight;
                                break;

                            case "100":
                            case "thin":
                                newFontWeight = FontWeights.Thin;
                                break;
                            }
                        }
                        foreach (var item in Parse(xElement.Nodes(), fontFamily, fontColor, newFontWeight, fontStyle, textDecoration, fontVariants))
                        {
                            yield return(item);
                        }
                        break;
                    }

                    case "em":
                    case "i":
                    case "cite":
                    case "dfn":
                        foreach (var item in Parse(xElement.Nodes(), fontFamily, fontColor, fontWeight, FontStyle.Italic, textDecoration, fontVariants))
                        {
                            yield return(item);
                        }
                        break;

                    case "u":
                        foreach (var item in Parse(xElement.Nodes(), fontFamily, fontColor, fontWeight, fontStyle, TextDecorations.Underline, fontVariants))
                        {
                            yield return(item);
                        }
                        break;

                    case "s":
                    case "strike":
                    case "del":
                        foreach (var item in Parse(xElement.Nodes(), fontFamily, fontColor, fontWeight, fontStyle, TextDecorations.Strikethrough, fontVariants))
                        {
                            yield return(item);
                        }
                        break;

                    case "font":
                    {
                        Brush      newFontColor  = null;
                        FontFamily newFontFamily = null;

                        var colorStr = xElement.Attribute("color")?.Value;
                        if (!string.IsNullOrEmpty(colorStr))
                        {
                            try
                            {
                                var color = XamlBindingHelper.ConvertValue(typeof(Windows.UI.Color), colorStr) as Color?;
                                if (color.HasValue)
                                {
                                    newFontColor = new SolidColorBrush()
                                    {
                                        Color = color.Value
                                    };
                                }
                            }
                            catch { }
                        }

                        var faceStr = xElement.Attribute("face")?.Value;
                        if (!string.IsNullOrEmpty(faceStr))
                        {
                            newFontFamily = new FontFamily(faceStr);
                        }

                        foreach (var item in Parse(xElement.Nodes(), newFontFamily ?? fontFamily, newFontColor ?? fontColor, fontWeight, fontStyle, textDecoration, fontVariants))
                        {
                            yield return(item);
                        }
                    }
                    break;

                    case "tt":
                    {
                        foreach (var item in Parse(xElement.Nodes(), new FontFamily("Consolas"), fontColor, fontWeight, fontStyle, textDecoration, fontVariants))
                        {
                            yield return(item);
                        }
                    }
                    break;

                    case "sup":
                    {
                        foreach (var item in Parse(xElement.Nodes(), fontFamily, fontColor, fontWeight, fontStyle, textDecoration, FontVariants.Superscript))
                        {
                            yield return(item);
                        }
                    }
                    break;

                    case "sub":
                    {
                        foreach (var item in Parse(xElement.Nodes(), fontFamily, fontColor, fontWeight, fontStyle, textDecoration, FontVariants.Subscript))
                        {
                            yield return(item);
                        }
                    }
                    break;

                    case "br":
                    {
                        yield return(new LineBreak());
                    }
                    break;

                    case "a":
                    {
                        var href = xElement.Attribute("href")?.Value;
                        if (!string.IsNullOrEmpty(href))
                        {
                            var hyperlink = new Hyperlink()
                            {
                                NavigateUri    = new Uri(href),
                                UnderlineStyle = UnderlineStyle.None
                            };
                            foreach (var item in Parse(xElement.Nodes(), fontFamily, fontColor, fontWeight, fontStyle, textDecoration, fontVariants))
                            {
                                hyperlink.Inlines.Add(item);
                            }
                            yield return(hyperlink);
                        }
                        else
                        {
                            //ignore the hyperlink
                            foreach (var item in Parse(xElement.Nodes(), fontFamily, fontColor, fontWeight, fontStyle, textDecoration, fontVariants))
                            {
                                yield return(item);
                            }
                        }
                    }
                    break;

                    default:
                        //ignore the element
                        foreach (var item in Parse(xElement.Nodes(), fontFamily, fontColor, fontWeight, fontStyle, textDecoration, fontVariants))
                        {
                            yield return(item);
                        }
                        break;
                    }
                }
                break;
                }
            }
        }
Example #8
0
 public void Typo3(object sender, RoutedEventArgs e)
 {
     Typography.SetVariants(fd1, FontVariants.Normal);
     Typography.SetCapitals(fd1, FontCapitals.Normal);
     Typography.SetHistoricalForms(fd1, true);
 }
Example #9
0
 public void typo2(object sender, RoutedEventArgs e)
 {
     Typography.SetHistoricalForms(fd1, false);
     Typography.SetCapitals(fd1, FontCapitals.Normal);
     Typography.SetVariants(fd1, FontVariants.Subscript);
 }