diff --git a/samples/TestApplication/Program.cs b/samples/TestApplication/Program.cs index 3fcdcd4e80..08adbc8a84 100644 --- a/samples/TestApplication/Program.cs +++ b/samples/TestApplication/Program.cs @@ -377,22 +377,14 @@ namespace TestApplication Header = "Html", Content = new ScrollViewer() { - Width = 900, + Width = 600, + HorizontalAlignment = HorizontalAlignment.Center, + CanScrollHorizontally = false, VerticalScrollBarVisibility = ScrollBarVisibility.Visible, Content = - new Border + new HtmlLabel() { - Height = 2500, - Child = - new HtmlLabel() - { - - Text = htmlText, - AutoSize = false, - MaxWidth = 900, - MaxHeight = 2500 - - } + Text = htmlText } } }; diff --git a/src/Perspex.HtmlRenderer/Adapters/GraphicsAdapter.cs b/src/Perspex.HtmlRenderer/Adapters/GraphicsAdapter.cs index 6b3aacd500..dedb4c9e48 100644 --- a/src/Perspex.HtmlRenderer/Adapters/GraphicsAdapter.cs +++ b/src/Perspex.HtmlRenderer/Adapters/GraphicsAdapter.cs @@ -123,10 +123,59 @@ namespace TheArtOfDev.HtmlRenderer.Perspex.Adapters public override void MeasureString(string str, RFont font, double maxWidth, out int charFit, out double charFitWidth) { var text = GetText(str, font); - charFit = str.Length; - charFitWidth = text.Measure().Width; + var fullLength = text.Measure().Width; + if (fullLength < maxWidth) + { + charFitWidth = fullLength; + charFit = str.Length; + return; + } + + int lastLen = 0; + double lastMeasure = 0; + BinarySearch(len => + { + text = GetText(str.Substring(0, len), font); + var size = text.Measure().Width; + lastMeasure = size; + lastLen = len; + if (size <= maxWidth) + return -1; + return 1; + + }, 0, str.Length); + if (lastMeasure > maxWidth) + { + lastLen--; + lastMeasure = GetText(str.Substring(0, lastLen), font).Measure().Width; + } + charFit = lastLen; + charFitWidth = lastMeasure; + } - + + private static int BinarySearch(Func condition, int start, int end) + { + do + { + int ind = start + (end - start)/2; + int res = condition(ind); + if (res == 0) + return ind; + else if (res > 0) + { + if (start != ind) + start = ind; + else + start = ind + 1; + } + else + end = ind; + + } while (end > start); + return -1; + } + public override void DrawString(string str, RFont font, RColor color, RPoint point, RSize size, bool rtl) { var text = GetText(str, font); diff --git a/src/Perspex.HtmlRenderer/HtmlLabel.cs b/src/Perspex.HtmlRenderer/HtmlLabel.cs index 7d9b4c0489..f4c90cf05e 100644 --- a/src/Perspex.HtmlRenderer/HtmlLabel.cs +++ b/src/Perspex.HtmlRenderer/HtmlLabel.cs @@ -48,29 +48,6 @@ namespace Perspex.Controls //BackgroundProperty.OverrideDefaultValue(Brushes.Transparent); } - /// - /// Automatically sets the size of the label by content size - /// - [Category("Layout")] - [Description("Automatically sets the size of the label by content size.")] - public bool AutoSize - { - get { return (bool)GetValue(AutoSizeProperty); } - set { SetValue(AutoSizeProperty, value); } - } - - /// - /// Automatically sets the height of the label by content height (width is not effected). - /// - [Category("Layout")] - [Description("Automatically sets the height of the label by content height (width is not effected)")] - public virtual bool AutoSizeHeightOnly - { - get { return (bool)GetValue(AutoSizeHeightOnlyProperty); } - set { SetValue(AutoSizeHeightOnlyProperty, value); } - } - - #region Private methods /// @@ -86,11 +63,12 @@ namespace Perspex.Controls var vertical = Padding.Top + Padding.Bottom + BorderThickness.Top + BorderThickness.Bottom; var size = new RSize(constraint.Width < Double.PositiveInfinity ? constraint.Width - horizontal : 0, constraint.Height < Double.PositiveInfinity ? constraint.Height - vertical : 0); - var minSize = new RSize(MinWidth < Double.PositiveInfinity ? MinWidth - horizontal : 0, MinHeight < Double.PositiveInfinity ? MinHeight - vertical : 0); + //var minSize = new RSize(MinWidth < Double.PositiveInfinity ? MinWidth - horizontal : 0, MinHeight < Double.PositiveInfinity ? MinHeight - vertical : 0); var maxSize = new RSize(MaxWidth < Double.PositiveInfinity ? MaxWidth - horizontal : 0, MaxHeight < Double.PositiveInfinity ? MaxHeight - vertical : 0); + _htmlContainer.HtmlContainerInt.MaxSize = maxSize; - var newSize = HtmlRendererUtils.Layout(ig, _htmlContainer.HtmlContainerInt, size, minSize, maxSize, AutoSize, AutoSizeHeightOnly); - + _htmlContainer.HtmlContainerInt.PerformLayout(ig); + var newSize = _htmlContainer.ActualSize; constraint = new Size(newSize.Width + horizontal, newSize.Height + vertical); } }