Skip to content

Commit 972b9dd

Browse files
committed
Add HtmlPanel zoom (Ctrl+wheel) and middle-mouse pan
Implement browser-style zoom with anchor-to-cursor on Ctrl+MouseWheel, optional Ctrl+/−/0 shortcuts, and middle-button drag panning. Scale paint via Graphics transform and force GDI+ text when zoomed so glyphs scale (GDI TextOut ignores ScaleTransform).
1 parent 981bb4c commit 972b9dd

2 files changed

Lines changed: 336 additions & 25 deletions

File tree

‎Source/HtmlRenderer.WinForms/Adapters/GraphicsAdapter.cs‎

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,12 @@ internal sealed class GraphicsAdapter : RGraphics
6262
/// </summary>
6363
private readonly bool _useGdiPlusTextRendering;
6464

65+
/// <summary>
66+
/// True when <see cref="_g"/> has a non-identity world transform (e.g. zoom ScaleTransform).
67+
/// GDI TextOut ignores the GDI+ world transform, so drawing must use GDI+ in that case.
68+
/// </summary>
69+
private readonly bool _hasWorldTransform;
70+
6571
/// <summary>
6672
/// the initialized HDC used
6773
/// </summary>
@@ -105,7 +111,13 @@ public GraphicsAdapter(Graphics g, bool useGdiPlusTextRendering, bool releaseGra
105111
_g = g;
106112
_releaseGraphics = releaseGraphics;
107113

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

111123
public override void PopClip()
@@ -148,7 +160,9 @@ public override void ReturnPreviousSmoothingMode(Object prevMode)
148160

149161
public override RSize MeasureString(string str, RFont font)
150162
{
151-
if (_useGdiPlusTextRendering)
163+
// Prefer GDI metrics when layout has no world transform, even if draw uses GDI+ for zoom.
164+
// Measuring with an active ScaleTransform would inflate sizes and break layout.
165+
if (_useGdiPlusTextRendering && !_hasWorldTransform)
152166
{
153167
ReleaseHdc();
154168
var fontAdapter = (FontAdapter)font;
@@ -187,7 +201,7 @@ public override void MeasureString(string str, RFont font, double maxWidth, out
187201
{
188202
charFit = 0;
189203
charFitWidth = 0;
190-
if (_useGdiPlusTextRendering)
204+
if (_useGdiPlusTextRendering && !_hasWorldTransform)
191205
{
192206
ReleaseHdc();
193207

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

217231
public override void DrawString(string str, RFont font, RColor color, RPoint point, RSize size, bool rtl)
218232
{
219-
if (_useGdiPlusTextRendering)
233+
// GDI TextOut ignores Graphics.ScaleTransform; use GDI+ whenever a world transform is active (zoom).
234+
if (_useGdiPlusTextRendering || _hasWorldTransform)
220235
{
221236
ReleaseHdc();
222237
SetRtlAlignGdiPlus(rtl);

0 commit comments

Comments
 (0)