コード例 #1
0
        // Opposite of the Format function above, parsing a
        // string into a text block
        TextBlock Parse(string str)
        {
            var   tb           = new TextBlock();
            Style currentStyle = new DummyStyle("A");
            var   runStart     = 0;

            for (int i = 0; i < str.Length; i++)
            {
                if (str[i] == '\\')
                {
                    if (runStart < i)
                    {
                        tb.AddText(str.AsSpan(runStart, i - runStart), currentStyle);
                    }

                    i++;
                    currentStyle = new DummyStyle(new string(str[i], 1));
                    runStart     = i + 1;
                    continue;
                }
            }

            if (runStart < str.Length)
            {
                tb.AddText(str.AsSpan(runStart), currentStyle);
            }

            return(tb);
        }
コード例 #2
0
        // Draw image to surface
        private static void Draw(SKSurface surface, string message)
        {
            // Create a blank canvas
            var canvas = surface.Canvas;

            canvas.Clear(SKColors.LightGray);

            // Find the canvas bounds
            var canvasBounds = canvas.DeviceClipBounds;

            // Create the text block
            var tb = new TextBlock();

            // Configure layout properties
            tb.MaxWidth  = canvasBounds.Width * 0.8f;
            tb.Alignment = TextAlignment.Left;

            var style = new Style()
            {
                FontSize = 24
            };

            // Add text to the text block
            tb.AddText(message, style);

            // Paint the text block
            tb.Paint(canvas, new SKPoint(canvasBounds.Width * 0.1f, canvasBounds.Height * 0.1f));
        }
コード例 #3
0
        internal static TextBlock CreateTextBlock(string text, SKTypeface font, int fontSize, Size maxSize, TextAlignment alignment = TextAlignment.Auto, SKColor color = new SKColor(), int?maxLines = null)
        {
            if (maxLines == 1)
            {
                text = text.Replace("\n\r", "»");
                text = text.Replace("\n", "»");
                text = text.Replace("\r", "»");
            }

            var tb = new TextBlock {
                MaxWidth  = maxLines.GetValueOrDefault() == 1 ? (float?)null : maxSize.Width,
                MaxHeight = maxSize.Height == int.MaxValue ? (int?)null : maxSize.Height,
                Alignment = alignment,
                MaxLines  = maxLines
            };

            var styleNormal = new Style {
                FontFamily = font.FamilyName,
                FontSize   = fontSize,
                TextColor  = color
            };

            tb.AddText(text, styleNormal);

            return(tb);
        }
コード例 #4
0
        public static int GetMaxCaretIndex(string text)
        {
            var tb = new TextBlock();

            tb.AddText(text, new Style());

            return(tb.CaretIndicies[tb.CaretIndicies.Count - 1]);
        }
コード例 #5
0
        public static SizeF GetTextSize(string text, FontAttributes fontAttributes, TextAlignment alignment, LineBreakMode lineBreakMode, float maxWidth, float height = -1)
        {
            var tb = new TextBlock();

            tb.AddText(text, fontAttributes.ToStyle());
            tb.Alignment = alignment.ToTextAlignment();
            tb.MaxWidth  = maxWidth;

            tb.MaxLines = null;
            tb.Layout();
            return(new SizeF(tb.MeasuredWidth, tb.MeasuredHeight));
        }
コード例 #6
0
        internal static TextBlock CreateTextBlock(string text, SKTypeface font, int fontSize, Size maxSize, TextAlignment alignment = TextAlignment.Auto, SKColor color = new SKColor(), int?maxLines = null, bool ellipsis = false)
        {
            if (maxLines == 1)
            {
                text = text.Replace("\n\r", "»");
                text = text.Replace("\n", "»");
                text = text.Replace("\r", "»");
            }

            var tb = new TextBlock {
                MaxWidth  = maxLines.GetValueOrDefault() == 1 ? (float?)null : maxSize.Width,
                MaxHeight = maxSize.Height == int.MaxValue ? (int?)null : maxSize.Height,
                Alignment = alignment,
                MaxLines  = maxLines
            };

            var styleNormal = new Style {
                FontFamily = font.FamilyName,
                FontSize   = fontSize,
                TextColor  = color
            };

            tb.AddText(text, styleNormal);

            // Above, for a single line, we did not limit the width, and we need to fix it here
            // - If it fits, we need to set MaxWidth so Left/Center/Right alignment will work
            // - If it doesn't fit and we need an ellipsis, set MaxWidth to trigger the ellipsis
            if (maxLines.GetValueOrDefault() == 1)
            {
                if (tb.MeasuredWidth <= maxSize.Width || ellipsis)
                {
                    tb.MaxWidth = maxSize.Width;
                }
            }
            else if (maxLines.GetValueOrDefault() == 0)
            {
                if (tb.Lines.LastOrDefault()?.Runs.Count > 1 && !ellipsis)
                {
                    tb.MaxHeight *= 2;
                }
            }

            return(tb);
        }
コード例 #7
0
        public static bool Run()
        {
            Console.WriteLine("Text Block Load Test");
            Console.WriteLine("--------------------");
            Console.WriteLine();

            string typefaceName = "Arial";
            float  Scale        = 1.5f;
            var    styleSmall   = new Style()
            {
                FontFamily = typefaceName, FontSize = 12 * Scale
            };
            var styleScript = new Style()
            {
                FontFamily = "Segoe Script", FontSize = 18 * Scale
            };
            var styleHeading = new Style()
            {
                FontFamily = typefaceName, FontSize = 24 * Scale, FontWeight = 700
            };
            var styleNormal = new Style()
            {
                FontFamily = typefaceName, FontSize = 18 * Scale, LineHeight = 1.0f
            };
            var styleBold = new Style()
            {
                FontFamily = typefaceName, FontSize = 18 * Scale, FontWeight = 700
            };
            var styleUnderline = new Style()
            {
                FontFamily = typefaceName, FontSize = 18 * Scale, Underline = UnderlineStyle.Gapped
            };
            var styleStrike = new Style()
            {
                FontFamily = typefaceName, FontSize = 18 * Scale, StrikeThrough = StrikeThroughStyle.Solid
            };
            var styleSubScript = new Style()
            {
                FontFamily = typefaceName, FontSize = 18 * Scale, FontVariant = FontVariant.SubScript
            };
            var styleSuperScript = new Style()
            {
                FontFamily = typefaceName, FontSize = 18 * Scale, FontVariant = FontVariant.SuperScript
            };
            var styleItalic = new Style()
            {
                FontFamily = typefaceName, FontItalic = true, FontSize = 18 * Scale
            };
            var styleBoldLarge = new Style()
            {
                FontFamily = typefaceName, FontSize = 28 * Scale, FontWeight = 700
            };
            var styleRed = new Style()
            {
                FontFamily = typefaceName, FontSize = 18 * Scale                         /*, TextColor = new SKColor(0xFFFF0000) */
            };
            var styleBlue = new Style()
            {
                FontFamily = typefaceName, FontSize = 18 * Scale                          /*, TextColor = new SKColor(0xFF0000FF) */
            };


            var tr = new TestResults();
            var tb = new TextBlock();

            for (int i = 0; i < 1000; i++)
            {
                tr.EnterTest();

                tb.Clear();
                tb.MaxWidth = 1000;
                tb.AddText("Welcome to RichTextKit!\n", styleHeading);
                tb.AddText("\nRichTextKit is a rich text layout, rendering and measurement library for SkiaSharp.\n\nIt supports normal, ", styleNormal);
                tb.AddText("bold", styleBold);
                tb.AddText(", ", styleNormal);
                tb.AddText("italic", styleItalic);
                tb.AddText(", ", styleNormal);
                tb.AddText("underline", styleUnderline);
                tb.AddText(" (including ", styleNormal);
                tb.AddText("gaps over descenders", styleUnderline);
                tb.AddText("), ", styleNormal);
                tb.AddText("strikethrough", styleStrike);
                tb.AddText(", superscript (E=mc", styleNormal);
                tb.AddText("2", styleSuperScript);
                tb.AddText("), subscript (H", styleNormal);
                tb.AddText("2", styleSubScript);
                tb.AddText("O), ", styleNormal);
                tb.AddText("colored ", styleRed);
                tb.AddText("text", styleBlue);
                tb.AddText(" and ", styleNormal);
                tb.AddText("mixed ", styleNormal);
                tb.AddText("sizes", styleSmall);
                tb.AddText(" and ", styleNormal);
                tb.AddText("fonts", styleScript);
                tb.AddText(".\n\n", styleNormal);
                tb.AddText("Font fallback means emojis work: 🌐 🍪 🍕 🚀 and ", styleNormal);
                tb.AddText("text shaping and bi-directional text support means complex scripts and languages like Arabic: مرحبا بالعالم, Japanese: ハローワールド, Chinese: 世界您好 and Hindi: हैलो वर्ल्ड are rendered correctly!\n\n", styleNormal);
                tb.AddText("RichTextKit also supports left/center/right text alignment, word wrapping, truncation with ellipsis place-holder, text measurement, hit testing, painting a selection range, caret position & shape helpers.", styleNormal);
                tb.Layout();
                tr.LeaveTest();
                tr.TestPassed(true);
            }

            tr.Dump();
            return(tr.AllPassed);
        }
コード例 #8
0
        public void Render(SKCanvas canvas, float canvasWidth, float canvasHeight)
        {
            canvas.Clear(new SKColor(0xFFFFFFFF));

            const float margin = 80;


            float?height = (float)(canvasHeight - margin * 2);
            float?width  = (float)(canvasWidth - margin * 2);

            //width = 25;

            if (!UseMaxHeight)
            {
                height = null;
            }
            if (!UseMaxWidth)
            {
                width = null;
            }

            using (var gridlinePaint = new SKPaint()
            {
                Color = new SKColor(0xFFFF0000), StrokeWidth = 1
            })
            {
                canvas.DrawLine(new SKPoint(margin, 0), new SKPoint(margin, (float)canvasHeight), gridlinePaint);
                if (width.HasValue)
                {
                    canvas.DrawLine(new SKPoint(margin + width.Value, 0), new SKPoint(margin + width.Value, (float)canvasHeight), gridlinePaint);
                }
                canvas.DrawLine(new SKPoint(0, margin), new SKPoint((float)canvasWidth, margin), gridlinePaint);
                if (height.HasValue)
                {
                    canvas.DrawLine(new SKPoint(0, margin + height.Value), new SKPoint((float)canvasWidth, margin + height.Value), gridlinePaint);
                }
            }

            //string typefaceName = "Times New Roman";
            string typefaceName = "Segoe UI";
            //string typefaceName = "Segoe Script";

            var styleSmall = new Style()
            {
                FontFamily = typefaceName, FontSize = 12 * Scale
            };
            var styleScript = new Style()
            {
                FontFamily = "Segoe Script", FontSize = 18 * Scale
            };
            var styleHeading = new Style()
            {
                FontFamily = typefaceName, FontSize = 24 * Scale, FontWeight = 700
            };
            var styleNormal = new Style()
            {
                FontFamily = typefaceName, FontSize = 18 * Scale, LineHeight = 1.0f
            };
            var styleLTR = new Style()
            {
                FontFamily = typefaceName, FontSize = 18 * Scale, TextDirection = TextDirection.LTR
            };
            var styleBold = new Style()
            {
                FontFamily = typefaceName, FontSize = 18 * Scale, FontWeight = 700
            };
            var styleUnderline = new Style()
            {
                FontFamily = typefaceName, FontSize = 18 * Scale, Underline = UnderlineStyle.Gapped
            };
            var styleStrike = new Style()
            {
                FontFamily = typefaceName, FontSize = 18 * Scale, StrikeThrough = StrikeThroughStyle.Solid
            };
            var styleSubScript = new Style()
            {
                FontFamily = typefaceName, FontSize = 18 * Scale, FontVariant = FontVariant.SubScript
            };
            var styleSuperScript = new Style()
            {
                FontFamily = typefaceName, FontSize = 18 * Scale, FontVariant = FontVariant.SuperScript
            };
            var styleItalic = new Style()
            {
                FontFamily = typefaceName, FontItalic = true, FontSize = 18 * Scale
            };
            var styleBoldLarge = new Style()
            {
                FontFamily = typefaceName, FontSize = 28 * Scale, FontWeight = 700
            };
            var styleRed = new Style()
            {
                FontFamily = typefaceName, FontSize = 18 * Scale, TextColor = new SKColor(0xFFFF0000)
            };
            var styleBlue = new Style()
            {
                FontFamily = typefaceName, FontSize = 18 * Scale, TextColor = new SKColor(0xFF0000FF)
            };
            var styleFontAwesome = new Style()
            {
                FontFamily = "FontAwesome", FontSize = 24
            };


            _textBlock.Clear();
            _textBlock.MaxWidth  = width;
            _textBlock.MaxHeight = height;

            _textBlock.BaseDirection = BaseDirection;
            _textBlock.Alignment     = TextAlignment;

            switch (ContentMode)
            {
            case 0:
                _textBlock.AddText("Welcome to RichTextKit!\n", styleHeading);
                _textBlock.AddText("\nRichTextKit is a rich text layout, rendering and measurement library for SkiaSharp.\n\nIt supports normal, ", styleNormal);
                _textBlock.AddText("bold", styleBold);
                _textBlock.AddText(", ", styleNormal);
                _textBlock.AddText("italic", styleItalic);
                _textBlock.AddText(", ", styleNormal);
                _textBlock.AddText("underline", styleUnderline);
                _textBlock.AddText(" (including ", styleNormal);
                _textBlock.AddText("gaps over descenders", styleUnderline);
                _textBlock.AddText("), ", styleNormal);
                _textBlock.AddText("strikethrough", styleStrike);
                _textBlock.AddText(", superscript (E=mc", styleNormal);
                _textBlock.AddText("2", styleSuperScript);
                _textBlock.AddText("), subscript (H", styleNormal);
                _textBlock.AddText("2", styleSubScript);
                _textBlock.AddText("O), ", styleNormal);
                _textBlock.AddText("colored ", styleRed);
                _textBlock.AddText("text", styleBlue);
                _textBlock.AddText(" and ", styleNormal);
                _textBlock.AddText("mixed ", styleNormal);
                _textBlock.AddText("sizes", styleSmall);
                _textBlock.AddText(" and ", styleNormal);
                _textBlock.AddText("fonts", styleScript);
                _textBlock.AddText(".\n\n", styleNormal);
                _textBlock.AddText("Font fallback means emojis work: 🌐 🍪 🍕 🚀 and ", styleNormal);
                _textBlock.AddText("text shaping and bi-directional text support means complex scripts and languages like Arabic: مرحبا بالعالم, Japanese: ハローワールド, Chinese: 世界您好 and Hindi: हैलो वर्ल्ड are rendered correctly!\n\n", styleNormal);
                _textBlock.AddText("RichTextKit also supports left/center/right text alignment, word wrapping, truncation with ellipsis place-holder, text measurement, hit testing, painting a selection range, caret position & shape helpers.", styleNormal);
                break;

            case 1:
                _textBlock.AddText("Hello Wor", styleNormal);
                _textBlock.AddText("ld", styleRed);
                _textBlock.AddText(". This is normal 18px. These are emojis: 🌐 🍪 🍕 🚀 ", styleNormal);
                _textBlock.AddText("This is ", styleNormal);
                _textBlock.AddText("bold 28px", styleBoldLarge);
                _textBlock.AddText(". ", styleNormal);
                _textBlock.AddText("This is italic", styleItalic);
                _textBlock.AddText(". This is ", styleNormal);
                _textBlock.AddText("red", styleRed);
                _textBlock.AddText(". This is Arabic: (", styleNormal);
                _textBlock.AddText("تسجّل ", styleNormal);
                _textBlock.AddText("يتكلّم", styleNormal);
                _textBlock.AddText("), Hindi: ", styleNormal);
                _textBlock.AddText("हालाँकि प्रचलित रूप पूज", styleNormal);
                _textBlock.AddText(", Han: ", styleNormal);
                _textBlock.AddText("緳 踥踕", styleNormal);
                break;

            case 2:
                _textBlock.AddText("Hello Wor", styleNormal);
                _textBlock.AddText("ld", styleRed);
                _textBlock.AddText(".\nThis is normal 18px.\nThese are emojis: 🌐 🍪 🍕 🚀\n", styleNormal);
                _textBlock.AddText("This is ", styleNormal);
                _textBlock.AddText("bold 28px", styleBoldLarge);
                _textBlock.AddText(".\n", styleNormal);
                _textBlock.AddText("This is italic", styleItalic);
                _textBlock.AddText(".\nThis is ", styleNormal);
                _textBlock.AddText("red", styleRed);

                /*
                 * tle.AddText(".\nThis is Arabic: (", styleNormal);
                 * tle.AddText("تسجّل ", styleNormal);
                 * tle.AddText("يتكلّم", styleNormal);
                 * tle.AddText("), Hindi: ", styleNormal);
                 */
                _textBlock.AddText(".\nThis is Arabic: (تسجّل يتكلّم), Hindi: ", styleNormal);
                _textBlock.AddText("हालाँकि प्रचलित रूप पूज", styleNormal);
                _textBlock.AddText(", Han: ", styleNormal);
                _textBlock.AddText("緳 踥踕", styleNormal);
                break;

            case 3:
                _textBlock.AddText("Lorem ipsum dolor sit amet, consectetur adipiscing elit. Phasellus semper, sapien vitae placerat sollicitudin, lorem diam aliquet quam, id finibus nisi quam eget lorem.\nDonec facilisis sem nec rhoncus elementum. Cras laoreet porttitor malesuada.\n\nVestibulum sed lacinia diam. Mauris a mollis enim. Cras in rhoncus mauris, at vulputate nisl. Sed nec lobortis dolor, hendrerit posuere quam. Vivamus malesuada sit amet nunc ac cursus. Praesent volutpat euismod efficitur. Nam eu ante.", styleNormal);
                break;

            case 4:
                _textBlock.AddText("مرحبا بالعالم.  هذا هو اختبار التفاف \nالخط للتأكد من أنه يعمل للغات من اليمين إلى اليسار.", styleNormal);
                break;

            case 5:
                //_textBlock.AddText("مرحبا بالعالم.  هذا هو اختبار التفاف الخط للتأكد من \u2066ACME Inc.\u2069 أنه يعمل للغات من اليمين إلى اليسار.", styleNormal);
                _textBlock.AddText("مرحبا بالعالم.  هذا هو اختبار التفاف الخط للتأكد من ", styleNormal);
                _textBlock.AddText("ACME Inc.", styleLTR);
                _textBlock.AddText(" أنه يعمل للغات من اليمين إلى اليسار.", styleNormal);
                break;

            case 6:
                _textBlock.AddText("Subscript: H", styleNormal);
                _textBlock.AddText("2", styleSubScript);
                _textBlock.AddText("O  Superscript: E=mc", styleNormal);
                _textBlock.AddText("2", styleSuperScript);
                _textBlock.AddText("  Key: C", styleNormal);
                _textBlock.AddText("♯", styleSuperScript);
                _textBlock.AddText(" B", styleNormal);
                _textBlock.AddText("♭", styleSubScript);
                break;

            case 7:
                _textBlock.AddText("The quick brown fox jumps over the lazy dog.", styleUnderline);
                _textBlock.AddText(" ", styleNormal);
                _textBlock.AddText("Strike Through", styleStrike);
                _textBlock.AddText(" something ends in wooooooooq", styleNormal);
                break;

            case 8:
                _textBlock.AddText("Apples and Bananas\r\n", styleNormal);
                _textBlock.AddText("Pears\r\n", styleNormal);
                _textBlock.AddText("Bananas\r\n", styleNormal);
                break;

            case 9:
                _textBlock.AddText("Hello World", styleNormal);
                break;

            case 10:
                _textBlock.AddText("", styleNormal);
                break;

            case 11:
                _textBlock.AddText("\uF06B", styleFontAwesome);
                break;
            }

            var sw = new Stopwatch();

            sw.Start();
            _textBlock.Layout();
            var elapsed = sw.ElapsedMilliseconds;

            var options = new TextPaintOptions()
            {
                SelectionColor = new SKColor(0x60FF0000),
            };

            HitTestResult?htr = null;
            CaretInfo?    ci  = null;

            if (_showHitTest)
            {
                htr = _textBlock.HitTest(_hitTestX - margin, _hitTestY - margin);
                if (htr.Value.OverCodePointIndex >= 0)
                {
                    options.SelectionStart = htr.Value.OverCodePointIndex;
                    options.SelectionEnd   = _textBlock.CaretIndicies[_textBlock.LookupCaretIndex(htr.Value.OverCodePointIndex) + 1];
                }

                ci = _textBlock.GetCaretInfo(htr.Value.ClosestCodePointIndex);
            }

            if (ShowMeasuredSize)
            {
                using (var paint = new SKPaint()
                {
                    Color = new SKColor(0x1000FF00),
                    IsStroke = false,
                })
                {
                    var rect = new SKRect(margin + _textBlock.MeasuredPadding.Left, margin, margin + _textBlock.MeasuredWidth + _textBlock.MeasuredPadding.Left, margin + _textBlock.MeasuredHeight);
                    canvas.DrawRect(rect, paint);
                }
            }

            if (_textBlock.MeasuredOverhang.Left > 0)
            {
                using (var paint = new SKPaint()
                {
                    Color = new SKColor(0xFF00fff0), StrokeWidth = 1
                })
                {
                    canvas.DrawLine(new SKPoint(margin - _textBlock.MeasuredOverhang.Left, 0), new SKPoint(margin - _textBlock.MeasuredOverhang.Left, (float)canvasHeight), paint);
                }
            }

            if (_textBlock.MeasuredOverhang.Right > 0)
            {
                using (var paint = new SKPaint()
                {
                    Color = new SKColor(0xFF00ff00), StrokeWidth = 1
                })
                {
                    float x;
                    if (_textBlock.MaxWidth.HasValue)
                    {
                        x = margin + _textBlock.MaxWidth.Value;
                    }
                    else
                    {
                        x = margin + _textBlock.MeasuredWidth;
                    }
                    x += _textBlock.MeasuredOverhang.Right;
                    canvas.DrawLine(new SKPoint(x, 0), new SKPoint(x, (float)canvasHeight), paint);
                }
            }

            _textBlock.Paint(canvas, new SKPoint(margin, margin), options);

            if (ci != null)
            {
                using (var paint = new SKPaint()
                {
                    Color = new SKColor(0xFF000000),
                    IsStroke = true,
                    IsAntialias = true,
                    StrokeWidth = Scale,
                })
                {
                    var rect = ci.Value.CaretRectangle;
                    rect.Offset(margin, margin);
                    canvas.DrawLine(rect.Right, rect.Top, rect.Left, rect.Bottom, paint);
                }
            }

            var state = $"Size: {width} x {height} Base Direction: {BaseDirection} Alignment: {TextAlignment} Content: {ContentMode} scale: {Scale} time: {elapsed}";

            canvas.DrawText(state, margin, 20, new SKPaint()
            {
                Typeface    = SKTypeface.FromFamilyName("Arial"),
                TextSize    = 12,
                IsAntialias = true,
            });

            state = $"Selection: {options.SelectionStart}-{options.SelectionEnd} Closest: {(htr.HasValue ? htr.Value.ClosestCodePointIndex.ToString() : "-")}";
            canvas.DrawText(state, margin, 40, new SKPaint()
            {
                Typeface    = SKTypeface.FromFamilyName("Arial"),
                TextSize    = 12,
                IsAntialias = true,
            });
        }
コード例 #9
0
ファイル: CalloutStyleRenderer.cs プロジェクト: Mapsui/Mapsui
        /// <summary>
        /// Update content for single and detail
        /// </summary>
        public static void UpdateContent(CalloutStyle callout)
        {
            if (callout.Type == CalloutType.Custom)
            {
                return;
            }

            if (callout.Title == null)
            {
                callout.Content = -1;
                return;
            }

            var styleSubtitle     = new Topten.RichTextKit.Style();
            var styleTitle        = new Topten.RichTextKit.Style();
            var textBlockTitle    = new TextBlock();
            var textBlockSubtitle = new TextBlock();

            if (callout.Type == CalloutType.Detail)
            {
                styleSubtitle.FontFamily = callout.SubtitleFont.FontFamily;
                styleSubtitle.FontSize   = (float)callout.SubtitleFont.Size;
                styleSubtitle.FontItalic = callout.SubtitleFont.Italic;
                styleSubtitle.FontWeight = callout.SubtitleFont.Bold ? 700 : 400;
                styleSubtitle.TextColor  = callout.SubtitleFontColor.ToSkia();

                textBlockSubtitle.AddText(callout.Subtitle, styleSubtitle);
                textBlockSubtitle.Alignment = callout.SubtitleTextAlignment.ToRichTextKit();
            }
            styleTitle.FontFamily = callout.TitleFont.FontFamily;
            styleTitle.FontSize   = (float)callout.TitleFont.Size;
            styleTitle.FontItalic = callout.TitleFont.Italic;
            styleTitle.FontWeight = callout.TitleFont.Bold ? 700 : 400;
            styleTitle.TextColor  = callout.TitleFontColor.ToSkia();

            textBlockTitle.Alignment = callout.TitleTextAlignment.ToRichTextKit();
            textBlockTitle.AddText(callout.Title, styleTitle);

            textBlockTitle.MaxWidth = textBlockSubtitle.MaxWidth = (float)callout.MaxWidth;
            // Layout TextBlocks
            textBlockTitle.Layout();
            textBlockSubtitle.Layout();
            // Get sizes
            var width  = Math.Max(textBlockTitle.MeasuredWidth, textBlockSubtitle.MeasuredWidth);
            var height = textBlockTitle.MeasuredHeight + (callout.Type == CalloutType.Detail ? textBlockSubtitle.MeasuredHeight + (float)callout.Spacing : 0f);

            // Now we have the correct width, so make a new layout cycle for text alignment
            textBlockTitle.MaxWidth = textBlockSubtitle.MaxWidth = width;
            textBlockTitle.Layout();
            textBlockSubtitle.Layout();
            // Create bitmap from TextBlock
            using (var rec = new SKPictureRecorder())
                using (var canvas = rec.BeginRecording(new SKRect(0, 0, width, height)))
                {
                    // Draw text to canvas
                    textBlockTitle.Paint(canvas, new TextPaintOptions()
                    {
                        IsAntialias = true
                    });
                    if (callout.Type == CalloutType.Detail)
                    {
                        textBlockSubtitle.Paint(canvas, new SKPoint(0, textBlockTitle.MeasuredHeight + (float)callout.Spacing), new TextPaintOptions()
                        {
                            IsAntialias = true
                        });
                    }
                    // Create a SKPicture from canvas
                    var picture = rec.EndRecording();
                    if (callout.InternalContent >= 0)
                    {
                        BitmapRegistry.Instance.Set(callout.InternalContent, picture);
                    }
                    else
                    {
                        callout.InternalContent = BitmapRegistry.Instance.Register(picture);
                    }
                    callout.Content = callout.InternalContent;
                }
        }
コード例 #10
0
ファイル: TextParagraph.cs プロジェクト: ywscr/RichTextKit
 /// <summary>
 /// Constructs a new TextParagraph
 /// </summary>
 public TextParagraph(IStyle style)
 {
     _textBlock = new TextBlock();
     _textBlock.AddText("\u2029", style);
 }
コード例 #11
0
        /// <summary>
        /// Update content for single and detail
        /// </summary>
        public static void UpdateContent(CalloutStyle callout)
        {
            if (callout.Type == CalloutType.Custom)
            {
                return;
            }

            if (callout.Title == null)
            {
                return;
            }

            var _styleSubtitle     = new Topten.RichTextKit.Style();
            var _styleTitle        = new Topten.RichTextKit.Style();
            var _textBlockTitle    = new TextBlock();
            var _textBlockSubtitle = new TextBlock();

            if (callout.Type == CalloutType.Detail)
            {
                _styleSubtitle.FontFamily = callout.SubtitleFont.FontFamily;
                _styleSubtitle.FontSize   = (float)callout.SubtitleFont.Size;
                _styleSubtitle.FontItalic = callout.SubtitleFont.Italic;
                _styleSubtitle.FontWeight = callout.SubtitleFont.Bold ? 700 : 400;
                _styleSubtitle.TextColor  = callout.SubtitleFontColor.ToSkia();

                _textBlockSubtitle.AddText(callout.Subtitle, _styleSubtitle);
                _textBlockSubtitle.Alignment = callout.SubtitleTextAlignment.ToRichTextKit();
            }
            _styleTitle.FontFamily = callout.TitleFont.FontFamily;
            _styleTitle.FontSize   = (float)callout.TitleFont.Size;
            _styleTitle.FontItalic = callout.TitleFont.Italic;
            _styleTitle.FontWeight = callout.TitleFont.Bold ? 700 : 400;
            _styleTitle.TextColor  = callout.TitleFontColor.ToSkia();

            _textBlockTitle.Alignment = callout.TitleTextAlignment.ToRichTextKit();
            _textBlockTitle.AddText(callout.Title, _styleTitle);

            _textBlockTitle.MaxWidth = _textBlockSubtitle.MaxWidth = (float)callout.MaxWidth;
            // Layout TextBlocks
            _textBlockTitle.Layout();
            _textBlockSubtitle.Layout();
            // Get sizes
            var width  = Math.Max(_textBlockTitle.MeasuredWidth, _textBlockSubtitle.MeasuredWidth);
            var height = _textBlockTitle.MeasuredHeight + (callout.Type == CalloutType.Detail ? _textBlockSubtitle.MeasuredHeight + callout.Spacing : 0);

            // Now we have the correct width, so make a new layout cycle for text alignment
            _textBlockTitle.MaxWidth = _textBlockSubtitle.MaxWidth = width;
            _textBlockTitle.Layout();
            _textBlockSubtitle.Layout();
            // Create bitmap from TextBlock
            var info = new SKImageInfo((int)width, (int)height, SKImageInfo.PlatformColorType, SKAlphaType.Premul);

            using (var surface = SKSurface.Create(info))
            {
                var canvas    = surface.Canvas;
                var memStream = new MemoryStream();

                canvas.Clear(SKColors.Transparent);
                // surface.Canvas.Scale(DeviceDpi / 96.0f);
                _textBlockTitle.Paint(canvas, new TextPaintOptions()
                {
                    IsAntialias = true
                });
                _textBlockSubtitle.Paint(canvas, new SKPoint(0, _textBlockTitle.MeasuredHeight + (float)callout.Spacing), new TextPaintOptions()
                {
                    IsAntialias = true
                });
                // Create image from canvas
                var image = surface.Snapshot();
                var data  = image.Encode(SKEncodedImageFormat.Png, 100);
                if (callout.InternalContent >= 0)
                {
                    BitmapRegistry.Instance.Set(callout.InternalContent, data.AsStream(true));
                }
                else
                {
                    callout.InternalContent = BitmapRegistry.Instance.Register(data.AsStream(true));
                }
                callout.Content = callout.InternalContent;
            }
        }