Browse Source

Fixed HtmlLabel measure

pull/136/head
Nikita Tsukanov 11 years ago
parent
commit
104b84ba76
  1. 18
      samples/TestApplication/Program.cs
  2. 55
      src/Perspex.HtmlRenderer/Adapters/GraphicsAdapter.cs
  3. 30
      src/Perspex.HtmlRenderer/HtmlLabel.cs

18
samples/TestApplication/Program.cs

@ -377,22 +377,14 @@ namespace TestApplication
Header = "Html", Header = "Html",
Content = new ScrollViewer() Content = new ScrollViewer()
{ {
Width = 900, Width = 600,
HorizontalAlignment = HorizontalAlignment.Center,
CanScrollHorizontally = false,
VerticalScrollBarVisibility = ScrollBarVisibility.Visible, VerticalScrollBarVisibility = ScrollBarVisibility.Visible,
Content = Content =
new Border new HtmlLabel()
{ {
Height = 2500, Text = htmlText
Child =
new HtmlLabel()
{
Text = htmlText,
AutoSize = false,
MaxWidth = 900,
MaxHeight = 2500
}
} }
} }
}; };

55
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) public override void MeasureString(string str, RFont font, double maxWidth, out int charFit, out double charFitWidth)
{ {
var text = GetText(str, font); var text = GetText(str, font);
charFit = str.Length; var fullLength = text.Measure().Width;
charFitWidth = 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<int, int> 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) public override void DrawString(string str, RFont font, RColor color, RPoint point, RSize size, bool rtl)
{ {
var text = GetText(str, font); var text = GetText(str, font);

30
src/Perspex.HtmlRenderer/HtmlLabel.cs

@ -48,29 +48,6 @@ namespace Perspex.Controls
//BackgroundProperty.OverrideDefaultValue<HtmlLabel>(Brushes.Transparent); //BackgroundProperty.OverrideDefaultValue<HtmlLabel>(Brushes.Transparent);
} }
/// <summary>
/// Automatically sets the size of the label by content size
/// </summary>
[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); }
}
/// <summary>
/// Automatically sets the height of the label by content height (width is not effected).
/// </summary>
[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 #region Private methods
/// <summary> /// <summary>
@ -86,11 +63,12 @@ namespace Perspex.Controls
var vertical = Padding.Top + Padding.Bottom + BorderThickness.Top + BorderThickness.Bottom; 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 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); 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); constraint = new Size(newSize.Width + horizontal, newSize.Height + vertical);
} }
} }

Loading…
Cancel
Save