Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 19 additions & 4 deletions Source/HtmlRenderer.WinForms/Adapters/GraphicsAdapter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,12 @@ internal sealed class GraphicsAdapter : RGraphics
/// </summary>
private readonly bool _useGdiPlusTextRendering;

/// <summary>
/// True when <see cref="_g"/> has a non-identity world transform (e.g. zoom ScaleTransform).
/// GDI TextOut ignores the GDI+ world transform, so drawing must use GDI+ in that case.
/// </summary>
private readonly bool _hasWorldTransform;

/// <summary>
/// the initialized HDC used
/// </summary>
Expand Down Expand Up @@ -105,7 +111,13 @@ public GraphicsAdapter(Graphics g, bool useGdiPlusTextRendering, bool releaseGra
_g = g;
_releaseGraphics = releaseGraphics;

_useGdiPlusTextRendering = useGdiPlusTextRendering;
// When a world transform is active (HtmlPanel zoom), force GDI+ for DrawString so text scales.
// Layout measurement still prefers GDI when requested (transform is identity on layout Graphics).
using (var transform = g.Transform)
{
_hasWorldTransform = !transform.IsIdentity;
}
_useGdiPlusTextRendering = useGdiPlusTextRendering || _hasWorldTransform;
}

public override void PopClip()
Expand Down Expand Up @@ -148,7 +160,9 @@ public override void ReturnPreviousSmoothingMode(Object prevMode)

public override RSize MeasureString(string str, RFont font)
{
if (_useGdiPlusTextRendering)
// Prefer GDI metrics when layout has no world transform, even if draw uses GDI+ for zoom.
// Measuring with an active ScaleTransform would inflate sizes and break layout.
if (_useGdiPlusTextRendering && !_hasWorldTransform)
{
ReleaseHdc();
var fontAdapter = (FontAdapter)font;
Expand Down Expand Up @@ -187,7 +201,7 @@ public override void MeasureString(string str, RFont font, double maxWidth, out
{
charFit = 0;
charFitWidth = 0;
if (_useGdiPlusTextRendering)
if (_useGdiPlusTextRendering && !_hasWorldTransform)
{
ReleaseHdc();

Expand Down Expand Up @@ -216,7 +230,8 @@ public override void MeasureString(string str, RFont font, double maxWidth, out

public override void DrawString(string str, RFont font, RColor color, RPoint point, RSize size, bool rtl)
{
if (_useGdiPlusTextRendering)
// GDI TextOut ignores Graphics.ScaleTransform; use GDI+ whenever a world transform is active (zoom).
if (_useGdiPlusTextRendering || _hasWorldTransform)
{
ReleaseHdc();
SetRtlAlignGdiPlus(rtl);
Expand Down
2 changes: 1 addition & 1 deletion Source/HtmlRenderer.WinForms/Adapters/WinFormsAdapter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ protected override RBrush CreateLinearGradientBrush(RRect rect, RColor color1, R
return new BrushAdapter(new LinearGradientBrush(Utils.Convert(rect), Utils.Convert(color1), Utils.Convert(color2), (float)angle), true);
}

protected override RImage ConvertImageInt(object image)
protected override RImage? ConvertImageInt(object? image)
{
return image != null ? new ImageAdapter((Image)image) : null;
}
Expand Down
12 changes: 6 additions & 6 deletions Source/HtmlRenderer.WinForms/HtmlContainer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -286,15 +286,15 @@ public SizeF ActualSize
/// <summary>
/// Get the currently selected text segment in the html.
/// </summary>
public string SelectedText
public string? SelectedText
{
get { return _htmlContainerInt.SelectedText; }
}

/// <summary>
/// Copy the currently selected html segment with style.
/// </summary>
public string SelectedHtml
public string? SelectedHtml
{
get { return _htmlContainerInt.SelectedHtml; }
}
Expand All @@ -312,7 +312,7 @@ public void ClearSelection()
/// </summary>
/// <param name="htmlSource">the html to init with, init empty if not given</param>
/// <param name="baseCssData">optional: the stylesheet to init with, init default if not given</param>
public void SetHtml(string htmlSource, CssData baseCssData = null)
public void SetHtml(string? htmlSource, CssData? baseCssData = null)
{
_htmlContainerInt.SetHtml(htmlSource, baseCssData);
}
Expand All @@ -322,7 +322,7 @@ public void SetHtml(string htmlSource, CssData baseCssData = null)
/// </summary>
/// <param name="styleGen">Optional: controls the way styles are generated when html is generated (default: <see cref="HtmlGenerationStyle.Inline"/>)</param>
/// <returns>generated html</returns>
public string GetHtml(HtmlGenerationStyle styleGen = HtmlGenerationStyle.Inline)
public string? GetHtml(HtmlGenerationStyle styleGen = HtmlGenerationStyle.Inline)
{
return _htmlContainerInt.GetHtml(styleGen);
}
Expand All @@ -334,7 +334,7 @@ public string GetHtml(HtmlGenerationStyle styleGen = HtmlGenerationStyle.Inline)
/// <param name="location">the location to find the attribute at</param>
/// <param name="attribute">the attribute key to get value by</param>
/// <returns>found attribute value or null if not found</returns>
public string GetAttributeAt(Point location, string attribute)
public string? GetAttributeAt(Point location, string attribute)
{
return _htmlContainerInt.GetAttributeAt(Utils.Convert(location), attribute);
}
Expand All @@ -358,7 +358,7 @@ public List<LinkElementData<RectangleF>> GetLinks()
/// </summary>
/// <param name="location">the location to find the link at</param>
/// <returns>css link href if exists or null</returns>
public string GetLinkAt(Point location)
public string? GetLinkAt(Point location)
{
return _htmlContainerInt.GetLinkAt(Utils.Convert(location));
}
Expand Down
24 changes: 10 additions & 14 deletions Source/HtmlRenderer.WinForms/HtmlLabel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ public class HtmlLabel : Control
/// <summary>
/// Underline html container instance.
/// </summary>
protected HtmlContainer _htmlContainer;
protected HtmlContainer? _htmlContainer;

/// <summary>
/// The current border style of the control
Expand All @@ -85,17 +85,17 @@ public class HtmlLabel : Control
/// <summary>
/// the raw base stylesheet data used in the control
/// </summary>
protected string _baseRawCssData;
protected string? _baseRawCssData;

/// <summary>
/// the base stylesheet data used in the panel
/// </summary>
protected CssData _baseCssData;
protected CssData? _baseCssData;

/// <summary>
/// the current html text set in the control
/// </summary>
protected string _text;
protected string? _text;

/// <summary>
/// is to handle auto size of the control height only
Expand Down Expand Up @@ -296,7 +296,7 @@ public virtual bool IsContextMenuEnabled
[Description("Set base stylesheet to be used by html rendered in the control.")]
[Category("Appearance")]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
[Editor("System.ComponentModel.Design.MultilineStringEditor, System.Design, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a", "System.Drawing.Design.UITypeEditor, System.Drawing, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a")]
[Editor("System.ComponentModel.Design.MultilineStringEditor, System.Windows.Forms.Design, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089", "System.Drawing.Design.UITypeEditor, System.Windows.Forms, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089")]
public virtual string BaseStylesheet
{
get { return _baseRawCssData; }
Expand Down Expand Up @@ -488,21 +488,17 @@ protected override void OnLayout(LayoutEventArgs levent)
{
if (_htmlContainer != null)
{
Graphics g = CreateGraphics();
if (g != null)
using (var g = CreateGraphics())
using (var ig = new GraphicsAdapter(g, _htmlContainer.UseGdiPlusTextRendering))
{
using (g)
using (var ig = new GraphicsAdapter(g, _htmlContainer.UseGdiPlusTextRendering))
{
var newSize = HtmlRendererUtils.Layout(ig,
var newSize = HtmlRendererUtils.Layout(ig,
_htmlContainer.HtmlContainerInt,
new RSize(ClientSize.Width - Padding.Horizontal, ClientSize.Height - Padding.Vertical),
new RSize(MinimumSize.Width - Padding.Horizontal, MinimumSize.Height - Padding.Vertical),
new RSize(MaximumSize.Width - Padding.Horizontal, MaximumSize.Height - Padding.Vertical),
AutoSize,
AutoSizeHeightOnly);
ClientSize = Utils.ConvertRound(new RSize(newSize.Width + Padding.Horizontal, newSize.Height + Padding.Vertical));
}
ClientSize = Utils.ConvertRound(new RSize(newSize.Width + Padding.Horizontal, newSize.Height + Padding.Vertical));
}
}
base.OnLayout(levent);
Expand Down Expand Up @@ -659,7 +655,7 @@ protected override void WndProc(ref Message m)
try
{
// Replace .NET's hand cursor with the OS cursor
Win32Utils.SetCursor(Win32Utils.LoadCursor(0, Win32Utils.IdcHand));
Win32Utils.SetCursor(Win32Utils.LoadCursor(IntPtr.Zero, Win32Utils.IdcHand));
m.Result = IntPtr.Zero;
return;
}
Expand Down
Loading