Exemplo n.º 1
0
 public FormattedText(
     string textToFormat,
     CultureInfo culture,
     FlowDirection flowDirection,
     Typeface typeface,
     double emSize,
     Brush foreground)
 {
     this.Foreground = foreground;
     this.PlatformImpl = PlatformInterface.Instance.CreateFormattedText(
         textToFormat, 
         typeface, 
         emSize);
     this.Text = textToFormat;
 }
        public Direct2D1FormattedText(string text, Typeface typeface, double fontSize)
        {
            Factory factory = ((Direct2D1PlatformInterface)PlatformInterface.Instance).DirectWriteFactory;

            TextFormat format = new TextFormat(
                factory, 
                typeface.FontFamily.Source, 
                (float)fontSize);

            this.DirectWriteTextLayout = new TextLayout(
                factory,
                text ?? string.Empty,
                format,
                float.MaxValue,
                float.MaxValue);
        }
Exemplo n.º 3
0
        protected void UpdateTextGeometry()
        {
            BoundText = Text.GetProperty(nameof(TextShapeViewModel.Text)) is string boundText ? boundText : Text.Text;

            if (BoundText is null)
            {
                return;
            }

            if (Style.TextStyle.FontSize < 0.0)
            {
                return;
            }

            var fontStyle  = AM.FontStyle.Normal;
            var fontWeight = AM.FontWeight.Normal;

            if (Style.TextStyle.FontStyle.HasFlag(FontStyleFlags.Italic))
            {
                fontStyle |= AM.FontStyle.Italic;
            }

            if (Style.TextStyle.FontStyle.HasFlag(FontStyleFlags.Bold))
            {
                fontWeight |= AM.FontWeight.Bold;
            }

            // TODO: Cache Typeface
            // TODO: Cache FormattedText

            Typeface = new AM.Typeface(Style.TextStyle.FontName, fontStyle, fontWeight);

            var textAlignment = Style.TextStyle.TextHAlignment switch
            {
                TextHAlignment.Right => AM.TextAlignment.Right,
                TextHAlignment.Center => AM.TextAlignment.Center,
                _ => AM.TextAlignment.Left,
            };

            FormattedText = new AM.FormattedText()
            {
                Typeface      = Typeface,
                Text          = BoundText,
                TextAlignment = textAlignment,
                TextWrapping  = AM.TextWrapping.NoWrap,
                FontSize      = Style.TextStyle.FontSize,
                Constraint    = Rect.Size
            };

            var size = FormattedText.Bounds.Size;
            var rect = Rect;

            // NOTE: Using AM.TextAlignment
            var originX = rect.X;
            //var originX = Style.TextStyle.TextHAlignment switch
            //{
            //    TextHAlignment.Left => rect.X,
            //    TextHAlignment.Right => rect.Right - size.Width,
            //    _ => (rect.Left + rect.Width / 2.0) - (size.Width / 2.0)
            //};

            var originY = Style.TextStyle.TextVAlignment switch
            {
                TextVAlignment.Top => rect.Y,
                TextVAlignment.Bottom => rect.Bottom - size.Height,
                _ => (rect.Bottom - rect.Height / 2f) - (size.Height / 2f)
            };

            Origin = new A.Point(originX, originY);
        }
Exemplo n.º 4
0
        /// <inheritdoc/>
        public override void Draw(object dc, TextShape text, double dx, double dy, object db, object r)
        {
            var _gfx = dc as AM.DrawingContext;

            var properties = (ImmutableArray <Property>)db;
            var record     = (Record)r;
            var tbind      = text.BindText(properties, record);

            if (string.IsNullOrEmpty(tbind))
            {
                return;
            }

            AM.IBrush brush = ToBrush(text.Style.Stroke);

            var fontStyle  = AM.FontStyle.Normal;
            var fontWeight = AM.FontWeight.Normal;

            //var fontDecoration = PM.FontDecoration.None;

            if (text.Style.TextStyle.FontStyle != null)
            {
                if (text.Style.TextStyle.FontStyle.Flags.HasFlag(FontStyleFlags.Italic))
                {
                    fontStyle |= AM.FontStyle.Italic;
                }

                if (text.Style.TextStyle.FontStyle.Flags.HasFlag(FontStyleFlags.Bold))
                {
                    fontWeight |= AM.FontWeight.Bold;
                }

                // TODO: Implement font decoration after Avalonia adds support.

                /*
                 * if (text.Style.TextStyle.FontStyle.Flags.HasFlag(FontStyleFlags.Underline))
                 * {
                 *  fontDecoration |= PM.FontDecoration.Underline;
                 * }
                 *
                 * if (text.Style.TextStyle.FontStyle.Flags.HasFlag(FontStyleFlags.Strikeout))
                 * {
                 *  fontDecoration |= PM.FontDecoration.Strikethrough;
                 * }
                 */
            }

            if (text.Style.TextStyle.FontSize >= 0.0)
            {
                var tf = new AM.Typeface(
                    text.Style.TextStyle.FontName,
                    text.Style.TextStyle.FontSize * _textScaleFactor,
                    fontStyle,
                    fontWeight);

                var ft = new AM.FormattedText()
                {
                    Typeface      = tf,
                    Text          = tbind,
                    TextAlignment = AM.TextAlignment.Left,
                    Wrapping      = AM.TextWrapping.NoWrap
                };

                var rect   = CreateRect(text.TopLeft, text.BottomRight, dx, dy);
                var size   = ft.Measure();
                var origin = GetTextOrigin(text.Style, ref rect, ref size);

                _gfx.DrawText(brush, origin, ft);
            }
        }
Exemplo n.º 5
0
 /// <summary>
 ///     Tries to match a specified character to a <see cref="Typeface"/> that supports specified font properties.
 /// </summary>
 /// <param name="codepoint">The codepoint to match against.</param>
 /// <param name="fontStyle">The font style.</param>
 /// <param name="fontWeight">The font weight.</param>
 /// <param name="fontFamily">The font family. This is optional and used for fallback lookup.</param>
 /// <param name="culture">The culture.</param>
 /// <param name="typeface">The matching <see cref="Typeface"/>.</param>
 /// <returns>
 ///     <c>True</c>, if the <see cref="FontManager"/> could match the character to specified parameters, <c>False</c> otherwise.
 /// </returns>
 public bool TryMatchCharacter(int codepoint, FontStyle fontStyle,
                               FontWeight fontWeight,
                               FontFamily fontFamily, CultureInfo culture, out Typeface typeface) =>
 PlatformImpl.TryMatchCharacter(codepoint, fontStyle, fontWeight, fontFamily, culture, out typeface);
Exemplo n.º 6
0
 public GlyphTypeface(Typeface typeface)
 {
     PlatformImpl = FontManager.Current?.PlatformImpl.CreateGlyphTypeface(typeface);
 }
 public override IPlatformFormattedText CreateFormattedText(
     string text,
     Typeface typeface,
     double fontSize)
 {
     return new Direct2D1FormattedText(text, typeface, fontSize);
 }
Exemplo n.º 8
0
 public GlyphTypeface(Typeface typeface)
     : this(FontManager.Current?.PlatformImpl.CreateGlyphTypeface(typeface))
 {
 }
Exemplo n.º 9
0
 /// <summary>
 /// Create a new platform-specific <see cref="FormattedText"/>.
 /// </summary>
 /// <param name="textToFormat">The text.</param>
 /// <param name="typeface">The typeface.</param>
 /// <param name="fontSize">The font size.</param>
 /// <returns>The formatted text object.</returns>
 public abstract IPlatformFormattedText CreateFormattedText(
     string textToFormat,
     Typeface typeface,
     double fontSize);