16 |
17 |
18 |
--------------------------------------------------------------------------------
/Source/Demo/Common/Resources/Tooltip.html:
--------------------------------------------------------------------------------
1 | HtmlPanel control showing HTML Renderer capabilities
2 |
3 |
4 |
5 |
6 |
7 |
You can select html samples on the left or you can edit
8 |
9 | the HTML of any document using the editor on the bottom.
10 |
11 |
12 |
13 |
14 |
15 |
16 | This is an HtmlToolTip and it's very COOL!!!
17 | You can even click on the links!
18 |
--------------------------------------------------------------------------------
/Source/HtmlRenderer/Adapters/Entities/RDashStyle.cs:
--------------------------------------------------------------------------------
1 | // "Therefore those skilled at the unorthodox
2 | // are infinite as heaven and earth,
3 | // inexhaustible as the great rivers.
4 | // When they come to an end,
5 | // they begin again,
6 | // like the days and months;
7 | // they die and are reborn,
8 | // like the four seasons."
9 | //
10 | // - Sun Tsu,
11 | // "The Art of War"
12 |
13 | namespace TheArtOfDev.HtmlRenderer.Adapters.Entities
14 | {
15 | ///
16 | /// Specifies the style of dashed lines drawn with a object.
17 | ///
18 | public enum RDashStyle
19 | {
20 | Solid,
21 | Dash,
22 | Dot,
23 | DashDot,
24 | DashDotDot,
25 | Custom,
26 | }
27 | }
--------------------------------------------------------------------------------
/Source/HtmlRenderer/Adapters/Entities/RFontStyle.cs:
--------------------------------------------------------------------------------
1 | // "Therefore those skilled at the unorthodox
2 | // are infinite as heaven and earth,
3 | // inexhaustible as the great rivers.
4 | // When they come to an end,
5 | // they begin again,
6 | // like the days and months;
7 | // they die and are reborn,
8 | // like the four seasons."
9 | //
10 | // - Sun Tsu,
11 | // "The Art of War"
12 |
13 | using System;
14 |
15 | namespace TheArtOfDev.HtmlRenderer.Adapters.Entities
16 | {
17 | ///
18 | /// Specifies style information applied to text.
19 | ///
20 | [Flags]
21 | public enum RFontStyle
22 | {
23 | Regular = 0,
24 | Bold = 1,
25 | Italic = 2,
26 | Underline = 4,
27 | Strikeout = 8,
28 | }
29 | }
--------------------------------------------------------------------------------
/Source/Demo/Common/TestSamples/09.Inline.htm:
--------------------------------------------------------------------------------
1 |
2 |
3 |
26 |
27 |
--------------------------------------------------------------------------------
/Source/HtmlRenderer/Adapters/RBrush.cs:
--------------------------------------------------------------------------------
1 | // "Therefore those skilled at the unorthodox
2 | // are infinite as heaven and earth,
3 | // inexhaustible as the great rivers.
4 | // When they come to an end,
5 | // they begin again,
6 | // like the days and months;
7 | // they die and are reborn,
8 | // like the four seasons."
9 | //
10 | // - Sun Tsu,
11 | // "The Art of War"
12 |
13 | using System;
14 |
15 | namespace TheArtOfDev.HtmlRenderer.Adapters
16 | {
17 | ///
18 | /// Adapter for platform specific brush objects - used to fill graphics (rectangles, polygons and paths).
19 | /// The brush can be solid color, gradient or image.
20 | ///
21 | public abstract class RBrush : IDisposable
22 | {
23 | public abstract void Dispose();
24 | }
25 | }
--------------------------------------------------------------------------------
/Source/HtmlRenderer/Core/Entities/HtmlRenderErrorType.cs:
--------------------------------------------------------------------------------
1 | // "Therefore those skilled at the unorthodox
2 | // are infinite as heaven and earth,
3 | // inexhaustible as the great rivers.
4 | // When they come to an end,
5 | // they begin again,
6 | // like the days and months;
7 | // they die and are reborn,
8 | // like the four seasons."
9 | //
10 | // - Sun Tsu,
11 | // "The Art of War"
12 |
13 | namespace TheArtOfDev.HtmlRenderer.Core.Entities
14 | {
15 | ///
16 | /// Enum of possible error types that can be reported.
17 | ///
18 | public enum HtmlRenderErrorType
19 | {
20 | General = 0,
21 | CssParsing = 1,
22 | HtmlParsing = 2,
23 | Image = 3,
24 | Paint = 4,
25 | Layout = 5,
26 | KeyboardMouse = 6,
27 | Iframe = 7,
28 | ContextMenu = 8,
29 | }
30 | }
--------------------------------------------------------------------------------
/Source/HtmlRenderer/Core/Dom/CssUnit.cs:
--------------------------------------------------------------------------------
1 | // "Therefore those skilled at the unorthodox
2 | // are infinite as heaven and earth,
3 | // inexhaustible as the great rivers.
4 | // When they come to an end,
5 | // they begin again,
6 | // like the days and months;
7 | // they die and are reborn,
8 | // like the four seasons."
9 | //
10 | // - Sun Tsu,
11 | // "The Art of War"
12 |
13 | namespace TheArtOfDev.HtmlRenderer.Core.Dom
14 | {
15 | ///
16 | /// Represents the possible units of the CSS lengths
17 | ///
18 | ///
19 | /// http://www.w3.org/TR/CSS21/syndata.html#length-units
20 | ///
21 | internal enum CssUnit
22 | {
23 | None,
24 | Ems,
25 | Pixels,
26 | Ex,
27 | Inches,
28 | Centimeters,
29 | Milimeters,
30 | Points,
31 | Picas
32 | }
33 | }
--------------------------------------------------------------------------------
/Source/Directory.Build.props:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | Arthur Teplitzki
5 | Arthur Teplitzki
6 | README.md
7 | BSD-3-Clause
8 | https://codeplexarchive.org/project/HtmlRenderer
9 | html.png
10 | false
11 | See https://github.com/ArthurHub/HTML-Renderer/releases.
12 | 1.5.2-dev
13 | false
14 |
15 |
16 |
17 |
18 |
19 |
--------------------------------------------------------------------------------
/Source/Demo/WPF/GenerateImageWindow.xaml:
--------------------------------------------------------------------------------
1 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
--------------------------------------------------------------------------------
/Source/HtmlRenderer/Adapters/RFontFamily.cs:
--------------------------------------------------------------------------------
1 | // "Therefore those skilled at the unorthodox
2 | // are infinite as heaven and earth,
3 | // inexhaustible as the great rivers.
4 | // When they come to an end,
5 | // they begin again,
6 | // like the days and months;
7 | // they die and are reborn,
8 | // like the four seasons."
9 | //
10 | // - Sun Tsu,
11 | // "The Art of War"
12 |
13 | namespace TheArtOfDev.HtmlRenderer.Adapters
14 | {
15 | ///
16 | /// Adapter for platform specific font family object - define the available font families to use.
17 | /// Required for custom fonts handling: fonts that are not installed on the system.
18 | ///
19 | public abstract class RFontFamily
20 | {
21 | ///
22 | /// Gets the name of this Font Family.
23 | ///
24 | public abstract string Name { get; }
25 | }
26 | }
--------------------------------------------------------------------------------
/Source/Demo/Common/TestSamples/14.Iframes.htm:
--------------------------------------------------------------------------------
1 |
2 |
3 |
25 |
26 |
27 |
--------------------------------------------------------------------------------
/Source/HtmlRenderer/Adapters/RImage.cs:
--------------------------------------------------------------------------------
1 | // "Therefore those skilled at the unorthodox
2 | // are infinite as heaven and earth,
3 | // inexhaustible as the great rivers.
4 | // When they come to an end,
5 | // they begin again,
6 | // like the days and months;
7 | // they die and are reborn,
8 | // like the four seasons."
9 | //
10 | // - Sun Tsu,
11 | // "The Art of War"
12 |
13 | using System;
14 |
15 | namespace TheArtOfDev.HtmlRenderer.Adapters
16 | {
17 | ///
18 | /// Adapter for platform specific image object - used to render images.
19 | ///
20 | public abstract class RImage : IDisposable
21 | {
22 | ///
23 | /// Get the width, in pixels, of the image.
24 | ///
25 | public abstract double Width { get; }
26 |
27 | ///
28 | /// Get the height, in pixels, of the image.
29 | ///
30 | public abstract double Height { get; }
31 |
32 | public abstract void Dispose();
33 | }
34 | }
--------------------------------------------------------------------------------
/Source/HtmlRenderer/Core/Entities/HtmlGenerationStyle.cs:
--------------------------------------------------------------------------------
1 | // "Therefore those skilled at the unorthodox
2 | // are infinite as heaven and earth,
3 | // inexhaustible as the great rivers.
4 | // When they come to an end,
5 | // they begin again,
6 | // like the days and months;
7 | // they die and are reborn,
8 | // like the four seasons."
9 | //
10 | // - Sun Tsu,
11 | // "The Art of War"
12 |
13 | namespace TheArtOfDev.HtmlRenderer.Core.Entities
14 | {
15 | ///
16 | /// Controls the way styles are generated when html is generated.
17 | ///
18 | public enum HtmlGenerationStyle
19 | {
20 | ///
21 | /// styles are not generated at all
22 | ///
23 | None = 0,
24 |
25 | ///
26 | /// style are inserted in style attribute for each html tag
27 | ///
28 | Inline = 1,
29 |
30 | ///
31 | /// style section is generated in the head of the html
32 | ///
33 | InHeader = 2
34 | }
35 | }
--------------------------------------------------------------------------------
/Source/Demo/Common/TestSamples/01.Header.htm:
--------------------------------------------------------------------------------
1 |
2 |
3 | Stub simple text
4 |
5 |
5 | Lorem ipsum dolor sit amet, consectetur adipiscing elit. Curabitur ornare mollis
6 | elit. Integer sagittis. Fusce elementum commodo felis. Vivamus lacinia eleifend
7 | libero. Donec lacus. Nam sit amet urna. Nullam nulla. Donec accumsan porta magna.
8 | Mauris a dolor eu elit rutrum commodo. Nam iaculis turpis non augue. Nullam lobortis
9 | egestas risus. Nulla elementum dolor ac mauris. Ut tristique. In varius volutpat
10 | metus. Integer leo dolor, tristique a, dignissim ac, iaculis eget, elit. Donec arcu.
11 |
12 |
13 |
Limit block height with overflow: hidden
14 |
15 |
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Curabitur ornare mollis
16 | elit. Integer sagittis. Fusce elementum commodo felis. Vivamus lacinia eleifend
17 | libero. Donec lacus. Nam sit amet urna. Nullam nulla. Donec accumsan porta magna.
18 | Mauris a dolor eu elit rutrum commodo. Nam iaculis turpis non augue. Nullam lobortis
19 | egestas risus. Nulla elementum dolor ac mauris. Ut tristique. In varius volutpat
20 | metus. Integer leo dolor, tristique a, dignissim ac, iaculis eget, elit. Donec arcu.
21 |
22 |
23 |
24 |
--------------------------------------------------------------------------------
/Source/HtmlRenderer/Core/Dom/HoverBoxBlock.cs:
--------------------------------------------------------------------------------
1 | // "Therefore those skilled at the unorthodox
2 | // are infinite as heaven and earth,
3 | // inexhaustible as the great rivers.
4 | // When they come to an end,
5 | // they begin again,
6 | // like the days and months;
7 | // they die and are reborn,
8 | // like the four seasons."
9 | //
10 | // - Sun Tsu,
11 | // "The Art of War"
12 |
13 | using TheArtOfDev.HtmlRenderer.Core.Entities;
14 |
15 | namespace TheArtOfDev.HtmlRenderer.Core.Dom
16 | {
17 | ///
18 | /// CSS boxes that have ":hover" selector on them.
19 | ///
20 | internal sealed class HoverBoxBlock
21 | {
22 | ///
23 | /// the box that has :hover css on
24 | ///
25 | private readonly CssBox _cssBox;
26 |
27 | ///
28 | /// the :hover style block data
29 | ///
30 | private readonly CssBlock _cssBlock;
31 |
32 | ///
33 | /// Init.
34 | ///
35 | public HoverBoxBlock(CssBox cssBox, CssBlock cssBlock)
36 | {
37 | _cssBox = cssBox;
38 | _cssBlock = cssBlock;
39 | }
40 |
41 | ///
42 | /// the box that has :hover css on
43 | ///
44 | public CssBox CssBox
45 | {
46 | get { return _cssBox; }
47 | }
48 |
49 | ///
50 | /// the :hover style block data
51 | ///
52 | public CssBlock CssBlock
53 | {
54 | get { return _cssBlock; }
55 | }
56 | }
57 | }
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | Copyright (c) 2009, José Manuel Menéndez Poo
2 | Copyright (c) 2013, Arthur Teplitzki
3 | All rights reserved.
4 |
5 | Redistribution and use in source and binary forms, with or without modification,
6 | are permitted provided that the following conditions are met:
7 |
8 | Redistributions of source code must retain the above copyright notice, this
9 | list of conditions and the following disclaimer.
10 |
11 | Redistributions in binary form must reproduce the above copyright notice, this
12 | list of conditions and the following disclaimer in the documentation and/or
13 | other materials provided with the distribution.
14 |
15 | Neither the name of the menendezpoo.com, ArthurHub nor the names of its
16 | contributors may be used to endorse or promote products derived from
17 | this software without specific prior written permission.
18 |
19 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
20 | ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
21 | WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22 | DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
23 | ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24 | (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25 | LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
26 | ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28 | SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 |
--------------------------------------------------------------------------------
/Source/Demo/WPF/HtmlRenderer.Demo.WPF.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 | net462
4 | WinExe
5 | TheArtOfDev.HtmlRenderer.Demo.WPF
6 | HtmlRendererWpfDemo
7 | true
8 | false
9 | true
10 |
11 |
12 | html.ico
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 |
33 |
34 | %(ReferenceCopyLocalPaths.Filename)%(ReferenceCopyLocalPaths.Extension)
35 |
36 |
37 |
38 |
--------------------------------------------------------------------------------
/Source/HtmlRenderer.WinForms/Adapters/ImageAdapter.cs:
--------------------------------------------------------------------------------
1 | // "Therefore those skilled at the unorthodox
2 | // are infinite as heaven and earth,
3 | // inexhaustible as the great rivers.
4 | // When they come to an end,
5 | // they begin again,
6 | // like the days and months;
7 | // they die and are reborn,
8 | // like the four seasons."
9 | //
10 | // - Sun Tsu,
11 | // "The Art of War"
12 |
13 | using System.Drawing;
14 | using TheArtOfDev.HtmlRenderer.Adapters;
15 |
16 | namespace TheArtOfDev.HtmlRenderer.WinForms.Adapters
17 | {
18 | ///
19 | /// Adapter for WinForms Image object for core.
20 | ///
21 | internal sealed class ImageAdapter : RImage
22 | {
23 | ///
24 | /// the underline win-forms image.
25 | ///
26 | private readonly Image _image;
27 |
28 | ///
29 | /// Initializes a new instance of the class.
30 | ///
31 | public ImageAdapter(Image image)
32 | {
33 | _image = image;
34 | }
35 |
36 | ///
37 | /// the underline win-forms image.
38 | ///
39 | public Image Image
40 | {
41 | get { return _image; }
42 | }
43 |
44 | public override double Width
45 | {
46 | get { return _image.Width; }
47 | }
48 |
49 | public override double Height
50 | {
51 | get { return _image.Height; }
52 | }
53 |
54 | public override void Dispose()
55 | {
56 | _image.Dispose();
57 | }
58 | }
59 | }
--------------------------------------------------------------------------------
/Source/HtmlRenderer.PdfSharp/Adapters/ImageAdapter.cs:
--------------------------------------------------------------------------------
1 | // "Therefore those skilled at the unorthodox
2 | // are infinite as heaven and earth,
3 | // inexhaustible as the great rivers.
4 | // When they come to an end,
5 | // they begin again,
6 | // like the days and months;
7 | // they die and are reborn,
8 | // like the four seasons."
9 | //
10 | // - Sun Tsu,
11 | // "The Art of War"
12 |
13 | using TheArtOfDev.HtmlRenderer.Adapters;
14 | using PdfSharp.Drawing;
15 |
16 | namespace TheArtOfDev.HtmlRenderer.PdfSharp.Adapters
17 | {
18 | ///
19 | /// Adapter for WinForms Image object for core.
20 | ///
21 | internal sealed class ImageAdapter : RImage
22 | {
23 | ///
24 | /// the underline win-forms image.
25 | ///
26 | private readonly XImage _image;
27 |
28 | ///
29 | /// Initializes a new instance of the class.
30 | ///
31 | public ImageAdapter(XImage image)
32 | {
33 | _image = image;
34 | }
35 |
36 | ///
37 | /// the underline win-forms image.
38 | ///
39 | public XImage Image
40 | {
41 | get { return _image; }
42 | }
43 |
44 | public override double Width
45 | {
46 | get { return _image.PixelWidth; }
47 | }
48 |
49 | public override double Height
50 | {
51 | get { return _image.PixelHeight; }
52 | }
53 |
54 | public override void Dispose()
55 | {
56 | _image.Dispose();
57 | }
58 | }
59 | }
--------------------------------------------------------------------------------
/Source/HtmlRenderer.WPF/Adapters/ImageAdapter.cs:
--------------------------------------------------------------------------------
1 | // "Therefore those skilled at the unorthodox
2 | // are infinite as heaven and earth,
3 | // inexhaustible as the great rivers.
4 | // When they come to an end,
5 | // they begin again,
6 | // like the days and months;
7 | // they die and are reborn,
8 | // like the four seasons."
9 | //
10 | // - Sun Tsu,
11 | // "The Art of War"
12 |
13 | using System.Windows.Media.Imaging;
14 | using TheArtOfDev.HtmlRenderer.Adapters;
15 |
16 | namespace TheArtOfDev.HtmlRenderer.WPF.Adapters
17 | {
18 | ///
19 | /// Adapter for WPF Image object for core.
20 | ///
21 | internal sealed class ImageAdapter : RImage
22 | {
23 | ///
24 | /// the underline WPF image.
25 | ///
26 | private readonly BitmapImage _image;
27 |
28 | ///
29 | /// Init.
30 | ///
31 | public ImageAdapter(BitmapImage image)
32 | {
33 | _image = image;
34 | }
35 |
36 | ///
37 | /// the underline WPF image.
38 | ///
39 | public BitmapImage Image
40 | {
41 | get { return _image; }
42 | }
43 |
44 | public override double Width
45 | {
46 | get { return _image.PixelWidth; }
47 | }
48 |
49 | public override double Height
50 | {
51 | get { return _image.PixelHeight; }
52 | }
53 |
54 | public override void Dispose()
55 | {
56 | if (_image.StreamSource != null)
57 | _image.StreamSource.Dispose();
58 | }
59 | }
60 | }
--------------------------------------------------------------------------------
/Source/HtmlRenderer/Core/Entities/HtmlRefreshEventArgs.cs:
--------------------------------------------------------------------------------
1 | // "Therefore those skilled at the unorthodox
2 | // are infinite as heaven and earth,
3 | // inexhaustible as the great rivers.
4 | // When they come to an end,
5 | // they begin again,
6 | // like the days and months;
7 | // they die and are reborn,
8 | // like the four seasons."
9 | //
10 | // - Sun Tsu,
11 | // "The Art of War"
12 |
13 | using System;
14 |
15 | namespace TheArtOfDev.HtmlRenderer.Core.Entities
16 | {
17 | ///
18 | /// Raised when html renderer requires refresh of the control hosting (invalidation and re-layout).
19 | /// It can happen if some async event has occurred that requires re-paint and re-layout of the html.
20 | /// Example: async download of image is complete.
21 | ///
22 | public sealed class HtmlRefreshEventArgs : EventArgs
23 | {
24 | ///
25 | /// is re-layout is required for the refresh
26 | ///
27 | private readonly bool _layout;
28 |
29 | ///
30 | /// Init.
31 | ///
32 | /// is re-layout is required for the refresh
33 | public HtmlRefreshEventArgs(bool layout)
34 | {
35 | _layout = layout;
36 | }
37 |
38 | ///
39 | /// is re-layout is required for the refresh
40 | ///
41 | public bool Layout
42 | {
43 | get { return _layout; }
44 | }
45 |
46 | public override string ToString()
47 | {
48 | return string.Format("Layout: {0}", _layout);
49 | }
50 | }
51 | }
--------------------------------------------------------------------------------
/Source/Demo/Common/TestSamples/16.Borders.htm:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 | simple border 1px
7 |
8 |
9 |
10 |
11 | simple border 3px
12 |
13 |
14 |
15 |
16 | border 1px with corner-radius 5px
17 |
18 |
19 |
20 |
21 | border 2px with corner-radius 10px
22 |
23 |
24 |
25 |
26 | dotted border 1px
27 |
28 |
29 |
30 |
31 | dotted border 3px
32 |
33 |
34 |
35 |
36 | dashed border 1px
37 |
38 |
39 |
40 |
41 | dashed border 2px with corner-radius 10px
42 |
43 |
44 |
45 |
46 |
--------------------------------------------------------------------------------
/Source/HtmlRenderer.WinForms/MetafileExtensions.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Drawing.Imaging;
3 | using System.IO;
4 | using System.Runtime.InteropServices;
5 |
6 | namespace TheArtOfDev.HtmlRenderer.WinForms
7 | {
8 | public static class MetafileExtensions
9 | {
10 | public static void SaveAsEmf(Metafile me, string fileName)
11 | {
12 | /* https://social.msdn.microsoft.com/Forums/en-US/csharpgeneral/thread/12a1c749-b320-4ce9-aff7-9de0d7fd30ea
13 | How to save or serialize a Metafile: Solution found
14 | by : SWAT Team member _1
15 | Date : Friday, February 01, 2008 1:38 PM
16 | */
17 | int enfMetafileHandle = me.GetHenhmetafile().ToInt32();
18 | int bufferSize = GetEnhMetaFileBits(enfMetafileHandle, 0, null); // Get required buffer size.
19 | byte[] buffer = new byte[bufferSize]; // Allocate sufficient buffer
20 | if (GetEnhMetaFileBits(enfMetafileHandle, bufferSize, buffer) <= 0) // Get raw metafile data.
21 | throw new SystemException("Fail");
22 |
23 | FileStream ms = File.Open(fileName, FileMode.Create);
24 | ms.Write(buffer, 0, bufferSize);
25 | ms.Close();
26 | ms.Dispose();
27 | if (!DeleteEnhMetaFile(enfMetafileHandle)) //free handle
28 | throw new SystemException("Fail Free");
29 | }
30 |
31 | [DllImport("gdi32")]
32 | public static extern int GetEnhMetaFileBits(int hemf, int cbBuffer, byte[] lpbBuffer);
33 |
34 | [DllImport("gdi32")]
35 | public static extern bool DeleteEnhMetaFile(int hemfbitHandle);
36 | }
37 | }
38 |
--------------------------------------------------------------------------------
/Source/HtmlRenderer.WinForms/Adapters/BrushAdapter.cs:
--------------------------------------------------------------------------------
1 | // "Therefore those skilled at the unorthodox
2 | // are infinite as heaven and earth,
3 | // inexhaustible as the great rivers.
4 | // When they come to an end,
5 | // they begin again,
6 | // like the days and months;
7 | // they die and are reborn,
8 | // like the four seasons."
9 | //
10 | // - Sun Tsu,
11 | // "The Art of War"
12 |
13 | using System.Drawing;
14 | using TheArtOfDev.HtmlRenderer.Adapters;
15 |
16 | namespace TheArtOfDev.HtmlRenderer.WinForms.Adapters
17 | {
18 | ///
19 | /// Adapter for WinForms brushes objects for core.
20 | ///
21 | internal sealed class BrushAdapter : RBrush
22 | {
23 | ///
24 | /// The actual WinForms brush instance.
25 | ///
26 | private readonly Brush _brush;
27 |
28 | ///
29 | /// If to dispose the brush when is called.
30 | /// Ignore dispose for cached brushes.
31 | ///
32 | private readonly bool _dispose;
33 |
34 | ///
35 | /// Init.
36 | ///
37 | public BrushAdapter(Brush brush, bool dispose)
38 | {
39 | _brush = brush;
40 | _dispose = dispose;
41 | }
42 |
43 | ///
44 | /// The actual WinForms brush instance.
45 | ///
46 | public Brush Brush
47 | {
48 | get { return _brush; }
49 | }
50 |
51 | public override void Dispose()
52 | {
53 | if (_dispose)
54 | {
55 | _brush.Dispose();
56 | }
57 | }
58 | }
59 | }
--------------------------------------------------------------------------------
/Source/HtmlRenderer/Adapters/RGraphicsPath.cs:
--------------------------------------------------------------------------------
1 | // "Therefore those skilled at the unorthodox
2 | // are infinite as heaven and earth,
3 | // inexhaustible as the great rivers.
4 | // When they come to an end,
5 | // they begin again,
6 | // like the days and months;
7 | // they die and are reborn,
8 | // like the four seasons."
9 | //
10 | // - Sun Tsu,
11 | // "The Art of War"
12 |
13 | using System;
14 |
15 | namespace TheArtOfDev.HtmlRenderer.Adapters
16 | {
17 | ///
18 | /// Adapter for platform specific graphics path object - used to render (draw/fill) path shape.
19 | ///
20 | public abstract class RGraphicsPath : IDisposable
21 | {
22 | ///
23 | /// Start path at the given point.
24 | ///
25 | public abstract void Start(double x, double y);
26 |
27 | ///
28 | /// Add stright line to the given point from te last point.
29 | ///
30 | public abstract void LineTo(double x, double y);
31 |
32 | ///
33 | /// Add circular arc of the given size to the given point from the last point.
34 | ///
35 | public abstract void ArcTo(double x, double y, double size, Corner corner);
36 |
37 | ///
38 | /// Release path resources.
39 | ///
40 | public abstract void Dispose();
41 |
42 | ///
43 | /// The 4 corners that are handled in arc rendering.
44 | ///
45 | public enum Corner
46 | {
47 | TopLeft,
48 | TopRight,
49 | BottomLeft,
50 | BottomRight,
51 | }
52 | }
53 | }
--------------------------------------------------------------------------------
/Source/HtmlRenderer.WPF/Adapters/FontFamilyAdapter.cs:
--------------------------------------------------------------------------------
1 | // "Therefore those skilled at the unorthodox
2 | // are infinite as heaven and earth,
3 | // inexhaustible as the great rivers.
4 | // When they come to an end,
5 | // they begin again,
6 | // like the days and months;
7 | // they die and are reborn,
8 | // like the four seasons."
9 | //
10 | // - Sun Tsu,
11 | // "The Art of War"
12 |
13 | using System.Globalization;
14 | using System.Linq;
15 | using System.Windows.Markup;
16 | using System.Windows.Media;
17 | using TheArtOfDev.HtmlRenderer.Adapters;
18 |
19 | namespace TheArtOfDev.HtmlRenderer.WPF.Adapters
20 | {
21 | ///
22 | /// Adapter for WPF Font family object for core.
23 | ///
24 | internal sealed class FontFamilyAdapter : RFontFamily
25 | {
26 | ///
27 | /// Default language to get font family name by
28 | ///
29 | private static readonly XmlLanguage _xmlLanguage = XmlLanguage.GetLanguage(CultureInfo.CurrentCulture.IetfLanguageTag);
30 |
31 | ///
32 | /// the underline win-forms font.
33 | ///
34 | private readonly FontFamily _fontFamily;
35 |
36 | ///
37 | /// Init.
38 | ///
39 | public FontFamilyAdapter(FontFamily fontFamily)
40 | {
41 | _fontFamily = fontFamily;
42 | }
43 |
44 | ///
45 | /// the underline WPF font family.
46 | ///
47 | public FontFamily FontFamily => _fontFamily;
48 |
49 | public override string Name => _fontFamily.FamilyNames.TryGetValue(_xmlLanguage, out var name) ? name : _fontFamily.FamilyNames.FirstOrDefault().Value;
50 | }
51 | }
--------------------------------------------------------------------------------
/Source/HtmlRenderer/Core/Entities/HtmlScrollEventArgs.cs:
--------------------------------------------------------------------------------
1 | // "Therefore those skilled at the unorthodox
2 | // are infinite as heaven and earth,
3 | // inexhaustible as the great rivers.
4 | // When they come to an end,
5 | // they begin again,
6 | // like the days and months;
7 | // they die and are reborn,
8 | // like the four seasons."
9 | //
10 | // - Sun Tsu,
11 | // "The Art of War"
12 |
13 | using System;
14 | using TheArtOfDev.HtmlRenderer.Adapters.Entities;
15 |
16 | namespace TheArtOfDev.HtmlRenderer.Core.Entities
17 | {
18 | ///
19 | /// Raised when Html Renderer request scroll to specific location.
20 | /// This can occur on document anchor click.
21 | ///
22 | public sealed class HtmlScrollEventArgs : EventArgs
23 | {
24 | ///
25 | /// the location to scroll to
26 | ///
27 | private readonly RPoint _location;
28 |
29 | ///
30 | /// Init.
31 | ///
32 | /// the location to scroll to
33 | public HtmlScrollEventArgs(RPoint location)
34 | {
35 | _location = location;
36 | }
37 |
38 | ///
39 | /// the x location to scroll to
40 | ///
41 | public double X
42 | {
43 | get { return _location.X; }
44 | }
45 |
46 | ///
47 | /// the x location to scroll to
48 | ///
49 | public double Y
50 | {
51 | get { return _location.Y; }
52 | }
53 |
54 | public override string ToString()
55 | {
56 | return string.Format("Location: {0}", _location);
57 | }
58 | }
59 | }
--------------------------------------------------------------------------------
/Source/Demo/Common/TestSamples/18.Anchors.htm:
--------------------------------------------------------------------------------
1 |
2 |
3 |
51 |
52 |
53 | 1
54 |
55 | 2
56 |
57 |
58 | 3
59 |
60 |
61 |
62 | 4
63 |
64 |
65 |
--------------------------------------------------------------------------------
/Source/HtmlRenderer/Core/Entities/HtmlLinkClickedException.cs:
--------------------------------------------------------------------------------
1 | // "Therefore those skilled at the unorthodox
2 | // are infinite as heaven and earth,
3 | // inexhaustible as the great rivers.
4 | // When they come to an end,
5 | // they begin again,
6 | // like the days and months;
7 | // they die and are reborn,
8 | // like the four seasons."
9 | //
10 | // - Sun Tsu,
11 | // "The Art of War"
12 |
13 | using System;
14 |
15 | namespace TheArtOfDev.HtmlRenderer.Core.Entities
16 | {
17 | ///
18 | /// Exception thrown when client code subscribed to LinkClicked event thrown exception.
19 | ///
20 | public sealed class HtmlLinkClickedException : Exception
21 | {
22 | ///
23 | /// Initializes a new instance of the class.
24 | ///
25 | public HtmlLinkClickedException()
26 | { }
27 |
28 | ///
29 | /// Initializes a new instance of the class with a specified error message.
30 | ///
31 | /// The message that describes the error.
32 | public HtmlLinkClickedException(string message)
33 | : base(message)
34 | { }
35 |
36 | ///
37 | /// Initializes a new instance of the class with a specified error message and a reference to the inner exception that is the cause of this exception.
38 | ///
39 | /// The error message that explains the reason for the exception. The exception that is the cause of the current exception, or a null reference (Nothing in Visual Basic) if no inner exception is specified.
40 | public HtmlLinkClickedException(string message, Exception innerException)
41 | : base(message, innerException)
42 | { }
43 | }
44 | }
--------------------------------------------------------------------------------
/Source/HtmlRenderer/Adapters/Entities/RKeyEvent.cs:
--------------------------------------------------------------------------------
1 | // "Therefore those skilled at the unorthodox
2 | // are infinite as heaven and earth,
3 | // inexhaustible as the great rivers.
4 | // When they come to an end,
5 | // they begin again,
6 | // like the days and months;
7 | // they die and are reborn,
8 | // like the four seasons."
9 | //
10 | // - Sun Tsu,
11 | // "The Art of War"
12 |
13 | using TheArtOfDev.HtmlRenderer.Core;
14 |
15 | namespace TheArtOfDev.HtmlRenderer.Adapters.Entities
16 | {
17 | ///
18 | /// Even class for handling keyboard events in .
19 | ///
20 | public sealed class RKeyEvent
21 | {
22 | ///
23 | /// is control is pressed
24 | ///
25 | private readonly bool _control;
26 |
27 | ///
28 | /// is 'A' key is pressed
29 | ///
30 | private readonly bool _aKeyCode;
31 |
32 | ///
33 | /// is 'C' key is pressed
34 | ///
35 | private readonly bool _cKeyCode;
36 |
37 | ///
38 | /// Init.
39 | ///
40 | public RKeyEvent(bool control, bool aKeyCode, bool cKeyCode)
41 | {
42 | _control = control;
43 | _aKeyCode = aKeyCode;
44 | _cKeyCode = cKeyCode;
45 | }
46 |
47 | ///
48 | /// is control is pressed
49 | ///
50 | public bool Control
51 | {
52 | get { return _control; }
53 | }
54 |
55 | ///
56 | /// is 'A' key is pressed
57 | ///
58 | public bool AKeyCode
59 | {
60 | get { return _aKeyCode; }
61 | }
62 |
63 | ///
64 | /// is 'C' key is pressed
65 | ///
66 | public bool CKeyCode
67 | {
68 | get { return _cKeyCode; }
69 | }
70 | }
71 | }
--------------------------------------------------------------------------------
/Source/HtmlRenderer.WPF/RoutedEventArgs.cs:
--------------------------------------------------------------------------------
1 | // "Therefore those skilled at the unorthodox
2 | // are infinite as heaven and earth,
3 | // inexhaustible as the great rivers.
4 | // When they come to an end,
5 | // they begin again,
6 | // like the days and months;
7 | // they die and are reborn,
8 | // like the four seasons."
9 | //
10 | // - Sun Tsu,
11 | // "The Art of War"
12 |
13 | using System.Windows;
14 | using TheArtOfDev.HtmlRenderer.Core.Utils;
15 |
16 | namespace TheArtOfDev.HtmlRenderer.WPF
17 | {
18 | ///
19 | /// Handler for HTML renderer routed events.
20 | ///
21 | /// the event arguments object
22 | /// the type of the routed events args data
23 | public delegate void RoutedEventHandler(object sender, RoutedEventArgs args) where T : class;
24 |
25 | ///
26 | /// HTML Renderer routed event arguments containing event data.
27 | ///
28 | public sealed class RoutedEventArgs : RoutedEventArgs where T : class
29 | {
30 | ///
31 | /// the argument data of the routed event
32 | ///
33 | private readonly T _data;
34 |
35 | public RoutedEventArgs(RoutedEvent routedEvent, T data)
36 | : base(routedEvent)
37 | {
38 | ArgChecker.AssertArgNotNull(data, "args");
39 | _data = data;
40 | }
41 |
42 | public RoutedEventArgs(RoutedEvent routedEvent, object source, T data)
43 | : base(routedEvent, source)
44 | {
45 | ArgChecker.AssertArgNotNull(data, "args");
46 | _data = data;
47 | }
48 |
49 | ///
50 | /// the argument data of the routed event
51 | ///
52 | public T Data
53 | {
54 | get { return _data; }
55 | }
56 |
57 | public override string ToString()
58 | {
59 | return string.Format("RoutedEventArgs({0})", _data);
60 | }
61 | }
62 | }
--------------------------------------------------------------------------------
/Source/Demo/Common/Samples/06.Embeded video.htm:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 | Embeded video
8 |
9 |
10 |
11 | Embeded video is an 'iframe' with 'src' pointing to the video hosted on the server.
12 | Obviously the Html Renderer doesn't support iframes so don't expect to see ActiveX
13 | in the control that will stream the video, but the video thumbnail image, caption
14 | string and play visualization is shown. Clicking on it will open the video
15 | page on its site.
16 |
17 |
18 | How does it works
19 |
20 |
21 | The id of the video is extracted from the 'src' URL and using the server API gets
22 | the video data is extracted: thumbnail image url, caption string and link url.
23 | Then, on the video frame location, the thumbnail image is shown with the caption
24 | on top and a nice play button in the middle.
25 | When the user clicks on the video frame a link click events occures that opens the
26 | actual video page, instead of video source.
27 |
28 |
29 | Note: Because this technique requires server API it is currently supported
30 | only for YouTube and
31 | Vimeo.
32 |
33 | The code is under the New BSD License (BSD) license.
34 |
35 | If you use the code on a commercial application,
36 | please give the developers credit for the HTML Renderer code.
37 |
38 |
39 |
40 |
41 | Thanks for the HTML highlighting code on the demo app to
42 | Alun Evans
43 |
44 |
45 |
--------------------------------------------------------------------------------
/Source/HtmlRenderer.WPF/README.md:
--------------------------------------------------------------------------------
1 | # Welcome to the HTML Renderer WPF library!
2 |
3 | This library provides the rich formatting power of HTML in your WPF .NET applications using
4 | simple controls or static rendering code.
5 | For more info see HTML Renderer on GitHub: https://github.com/ArthurHub/HTML-Renderer
6 |
7 | ## DEMO APPLICATION
8 |
9 | HTML Renderer Demo application showcases HTML Renderer capabilities, use it to explore and learn
10 | on the library: https://codeplexarchive.org/ProjectTab/Wiki/HtmlRenderer/Documentation/Demo%20application
11 |
12 | ## FEEDBACK / RELEASE NOTES
13 |
14 | If you have problems, wish to report a bug, or have a suggestion, please open an issue on the
15 | HTML Renderer issue page: https://github.com/ArthurHub/HTML-Renderer/issues
16 |
17 | For full release notes and all versions see: https://github.com/ArthurHub/HTML-Renderer/releases
18 |
19 | ## QUICK START
20 |
21 | For more Quick Start see: https://codeplexarchive.org/ProjectTab/Wiki/HtmlRenderer/Documentation/Documentation
22 |
23 | ---
24 |
25 | ## Quick Start: Use HTML panel control on WPF window
26 |
27 | ```xaml
28 |
33 |
34 |
35 |
36 |
37 | ```
38 |
39 | ## Quick Start: Create image from HTML snippet
40 |
41 | ```csharp
42 | class Program
43 | {
44 | private static void Main(string[] args)
45 | {
46 | BitmapFrame image = HtmlRender.RenderToImage("
Hello World
This is html rendered text");
47 | var encoder = new PngBitmapEncoder();
48 | encoder.Frames.Add(image);
49 | using (FileStream stream = new FileStream("image.png", FileMode.OpenOrCreate))
50 | encoder.Save(stream);
51 | }
52 | }
53 | ```
--------------------------------------------------------------------------------
/Source/Demo/Common/TestSamples/02.Line break.htm:
--------------------------------------------------------------------------------
1 |
2 |
3 |
22 | this is a pre-line white-space style (double space)
23 | this is a pre-line white-space style this is a pre-line white-space style
24 |
25 |
26 | this is a pre-wrap white-space style (double space)
27 | this is a pre-wrap white-space style this is a pre-wrap white-space style
28 |
29 |
30 | this is a nowrap white-space style (double space)
31 | this is a nowrap white-space style this is a nowrap white-space style
32 |
33 |
34 | this is a pre white-space style (double space)
35 | this is a pre white-space style this is a pre white-space style
36 |
37 |
38 |
--------------------------------------------------------------------------------
/Source/Demo/WinForms/SampleForm.cs:
--------------------------------------------------------------------------------
1 | // "Therefore those skilled at the unorthodox
2 | // are infinite as heaven and earth,
3 | // inexhaustible as the great rivers.
4 | // When they come to an end,
5 | // they begin again,
6 | // like the days and months;
7 | // they die and are reborn,
8 | // like the four seasons."
9 | //
10 | // - Sun Tsu,
11 | // "The Art of War"
12 |
13 | using System;
14 | using System.Drawing;
15 | using System.Drawing.Drawing2D;
16 | using System.Windows.Forms;
17 | using TheArtOfDev.HtmlRenderer.Demo.Common;
18 |
19 | namespace TheArtOfDev.HtmlRenderer.Demo.WinForms
20 | {
21 | public partial class SampleForm : Form
22 | {
23 | private readonly Bitmap _background;
24 |
25 | public SampleForm()
26 | {
27 | InitializeComponent();
28 |
29 | Icon = DemoForm.GetIcon();
30 |
31 | _htmlLabel.Text = DemoUtils.SampleHtmlLabelText;
32 | _htmlPanel.Text = DemoUtils.SampleHtmlPanelText;
33 |
34 | _background = HtmlRenderingHelper.CreateImageForTransparentBackground();
35 | }
36 |
37 | private void OnHtmlLabelClick(object sender, EventArgs e)
38 | {
39 | _pGrid.SelectedObject = _htmlLabel;
40 | }
41 |
42 | private void OnHtmlPanelClick(object sender, EventArgs e)
43 | {
44 | _pGrid.SelectedObject = _htmlPanel;
45 | }
46 |
47 | private void OnHtmlLabelHostingPanelPaint(object sender, PaintEventArgs e)
48 | {
49 | using (var b = new TextureBrush(_background, WrapMode.Tile))
50 | {
51 | e.Graphics.FillRectangle(b, _htmlLabelHostingPanel.ClientRectangle);
52 | }
53 | }
54 |
55 | private void OnButtonClick(object sender, EventArgs e)
56 | {
57 | _htmlToolTip.SetToolTip(_changeTooltipButton, _htmlLabel.Text);
58 | }
59 |
60 | private void OnPropertyValueChanged(object s, PropertyValueChangedEventArgs e)
61 | {
62 | ((Control)_pGrid.SelectedObject).Refresh();
63 | Refresh();
64 | }
65 | }
66 | }
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | # Build Folders (you can keep bin if you'd like, to store dlls and pdbs)
2 | [Bb]in/
3 | [Oo]bj/
4 |
5 | # mstest test results
6 | TestResults
7 |
8 | ## Ignore Visual Studio temporary files, build results, and
9 | ## files generated by popular Visual Studio add-ons.
10 |
11 | # User-specific files
12 | *.suo
13 | *.user
14 | *.sln.docstates
15 |
16 | # Build results
17 | [Dd]ebug/
18 | [Rr]elease/
19 | x64/
20 | *_i.c
21 | *_p.c
22 | *.ilk
23 | *.meta
24 | *.obj
25 | *.pch
26 | *.pdb
27 | *.pgc
28 | *.pgd
29 | *.rsp
30 | *.sbr
31 | *.tlb
32 | *.tli
33 | *.tlh
34 | *.tmp
35 | *.log
36 | *.vspscc
37 | *.vssscc
38 | .builds
39 | .vs/
40 |
41 | # Visual C++ cache files
42 | ipch/
43 | *.aps
44 | *.ncb
45 | *.opensdf
46 | *.sdf
47 |
48 | # Visual Studio profiler
49 | *.psess
50 | *.vsp
51 | *.vspx
52 |
53 | # Guidance Automation Toolkit
54 | *.gpState
55 |
56 | # ReSharper is a .NET coding add-in
57 | _ReSharper*
58 |
59 | # NCrunch
60 | *.ncrunch*
61 | .*crunch*.local.xml
62 |
63 | # Installshield output folder
64 | [Ee]xpress
65 |
66 | # DocProject is a documentation generator add-in
67 | DocProject/buildhelp/
68 | DocProject/Help/*.HxT
69 | DocProject/Help/*.HxC
70 | DocProject/Help/*.hhc
71 | DocProject/Help/*.hhk
72 | DocProject/Help/*.hhp
73 | DocProject/Help/Html2
74 | DocProject/Help/html
75 |
76 | # Click-Once directory
77 | publish
78 |
79 | # Publish Web Output
80 | *.Publish.xml
81 |
82 | # NuGet Packages Directory
83 | packages
84 |
85 | # Windows Azure Build Output
86 | csx
87 | *.build.csdef
88 |
89 | # Windows Store app package directory
90 | AppPackages/
91 |
92 | # Others
93 | [Bb]in
94 | [Oo]bj
95 | sql
96 | TestResults
97 | [Tt]est[Rr]esult*
98 | *.Cache
99 | ClientBin
100 | [Ss]tyle[Cc]op.*
101 | ~$*
102 | *.dbmdl
103 | Generated_Code #added for RIA/Silverlight projects
104 |
105 | # Backup & report files from converting an old project file to a newer
106 | # Visual Studio version. Backup files are not needed, because we have git ;-)
107 | _UpgradeReport_Files/
108 | Backup*/
109 | UpgradeLog*.XML
110 |
111 | *.DotSettings
--------------------------------------------------------------------------------
/Source/Demo/WinForms/Program.cs:
--------------------------------------------------------------------------------
1 | // "Therefore those skilled at the unorthodox
2 | // are infinite as heaven and earth,
3 | // inexhaustible as the great rivers.
4 | // When they come to an end,
5 | // they begin again,
6 | // like the days and months;
7 | // they die and are reborn,
8 | // like the four seasons."
9 | //
10 | // - Sun Tsu,
11 | // "The Art of War"
12 |
13 | using System;
14 | using System.Globalization;
15 | using System.IO;
16 | using System.Reflection;
17 | using System.Windows.Forms;
18 |
19 | namespace TheArtOfDev.HtmlRenderer.Demo.WinForms
20 | {
21 | internal static class Program
22 | {
23 | ///
24 | /// The main entry point for the application.
25 | ///
26 | [STAThread]
27 | private static void Main()
28 | {
29 | AppDomain.CurrentDomain.AssemblyResolve += OnResolveAssembly;
30 |
31 | Application.EnableVisualStyles();
32 | Application.SetCompatibleTextRenderingDefault(false);
33 | Application.Run(new DemoForm());
34 |
35 | // Application.Run(new PerfForm());
36 | // PerfForm.Run();
37 | }
38 |
39 | private static Assembly OnResolveAssembly(object sender, ResolveEventArgs args)
40 | {
41 | Assembly executingAssembly = Assembly.GetExecutingAssembly();
42 | AssemblyName assemblyName = new AssemblyName(args.Name);
43 |
44 | string path = assemblyName.Name + ".dll";
45 | if (assemblyName.CultureInfo.Equals(CultureInfo.InvariantCulture) == false)
46 | {
47 | path = String.Format(@"{0}\{1}", assemblyName.CultureInfo, path);
48 | }
49 |
50 | using (Stream stream = executingAssembly.GetManifestResourceStream(path))
51 | {
52 | if (stream != null)
53 | {
54 | byte[] assemblyRawBytes = new byte[stream.Length];
55 | stream.Read(assemblyRawBytes, 0, assemblyRawBytes.Length);
56 | return Assembly.Load(assemblyRawBytes);
57 | }
58 | return null;
59 | }
60 | }
61 | }
62 | }
--------------------------------------------------------------------------------
/Source/HtmlRenderer.WPF/Adapters/GraphicsPathAdapter.cs:
--------------------------------------------------------------------------------
1 | // "Therefore those skilled at the unorthodox
2 | // are infinite as heaven and earth,
3 | // inexhaustible as the great rivers.
4 | // When they come to an end,
5 | // they begin again,
6 | // like the days and months;
7 | // they die and are reborn,
8 | // like the four seasons."
9 | //
10 | // - Sun Tsu,
11 | // "The Art of War"
12 |
13 | using System.Windows;
14 | using System.Windows.Media;
15 | using TheArtOfDev.HtmlRenderer.Adapters;
16 |
17 | namespace TheArtOfDev.HtmlRenderer.WPF.Adapters
18 | {
19 | ///
20 | /// Adapter for WPF graphics path object for core.
21 | ///
22 | internal sealed class GraphicsPathAdapter : RGraphicsPath
23 | {
24 | ///
25 | /// The actual WPF graphics geometry instance.
26 | ///
27 | private readonly StreamGeometry _geometry = new StreamGeometry();
28 |
29 | ///
30 | /// The context used in WPF geometry to render path
31 | ///
32 | private readonly StreamGeometryContext _geometryContext;
33 |
34 | public GraphicsPathAdapter()
35 | {
36 | _geometryContext = _geometry.Open();
37 | }
38 |
39 | public override void Start(double x, double y)
40 | {
41 | _geometryContext.BeginFigure(new Point(x, y), true, false);
42 | }
43 |
44 | public override void LineTo(double x, double y)
45 | {
46 | _geometryContext.LineTo(new Point(x, y), true, true);
47 | }
48 |
49 | public override void ArcTo(double x, double y, double size, Corner corner)
50 | {
51 | _geometryContext.ArcTo(new Point(x, y), new Size(size, size), 0, false, SweepDirection.Clockwise, true, true);
52 | }
53 |
54 | ///
55 | /// Close the geometry to so no more path adding is allowed and return the instance so it can be rendered.
56 | ///
57 | public StreamGeometry GetClosedGeometry()
58 | {
59 | _geometryContext.Close();
60 | _geometry.Freeze();
61 | return _geometry;
62 | }
63 |
64 | public override void Dispose()
65 | { }
66 | }
67 | }
--------------------------------------------------------------------------------
/Source/Demo/Common/Samples/08.Tooltip.htm:
--------------------------------------------------------------------------------
1 |
2 |
3 | HtmlToolTip
4 |
5 |
6 |
7 |
ToolTip
8 |
9 |
10 |
11 | Same as regular winforms tooltip but will render HTML and not just plain text. All HTML Renderer rules apply so you can use images, tables, styles, etc.
12 |
13 |
14 | Try the Sample Form and place the mouse over
15 | the button to change the ToolTip of the button.
16 |
17 |
18 |
19 | Example tooltip, all Tooltips are wrapped in a div with the style of this paragraph
20 |
35 | The size, width and height, of the tooltip is auto sized by the html shown, the longest line will set the width of the tooltip.
36 |
37 |
38 | Note: this is not the case here because those examples are embedded in HtmlPanel, but you can use Sample Form to play with it.
39 |
40 |
41 |
42 | You can add any complex html as you require, including images, table bullets, etc.
43 |
44 | Who said tooltip should be boring?
45 |
46 |
Not me!
47 |
Nope!
48 |
49 |
50 |
51 |
52 |
53 |
--------------------------------------------------------------------------------
/Source/HtmlRenderer/Adapters/RContextMenu.cs:
--------------------------------------------------------------------------------
1 | // "Therefore those skilled at the unorthodox
2 | // are infinite as heaven and earth,
3 | // inexhaustible as the great rivers.
4 | // When they come to an end,
5 | // they begin again,
6 | // like the days and months;
7 | // they die and are reborn,
8 | // like the four seasons."
9 | //
10 | // - Sun Tsu,
11 | // "The Art of War"
12 |
13 | using System;
14 | using TheArtOfDev.HtmlRenderer.Adapters.Entities;
15 |
16 | namespace TheArtOfDev.HtmlRenderer.Adapters
17 | {
18 | ///
19 | /// Adapter for platform specific context menu - used to create and show context menu at specific location.
20 | /// Not relevant for platforms that don't render HTML on UI element.
21 | ///
22 | public abstract class RContextMenu : IDisposable
23 | {
24 | ///
25 | /// The total number of items in the context menu
26 | ///
27 | public abstract int ItemsCount { get; }
28 |
29 | ///
30 | /// Add divider item to the context menu.
31 | /// The divider is a non clickable place holder used to separate items.
32 | ///
33 | public abstract void AddDivider();
34 |
35 | ///
36 | /// Add item to the context menu with the given text that will raise the given event when clicked.
37 | /// the text to set on the new context menu itemif to set the item as enabled or disabledthe event to raise when the item is clicked
38 | public abstract void AddItem(string text, bool enabled, EventHandler onClick);
39 |
40 | ///
41 | /// Remove the last item from the context menu iff it is a divider
42 | ///
43 | public abstract void RemoveLastDivider();
44 |
45 | ///
46 | /// Show the context menu in the given parent control at the given location.
47 | /// the parent control to show inthe location to show at relative to the parent control
48 | public abstract void Show(RControl parent, RPoint location);
49 |
50 | public abstract void Dispose();
51 | }
52 | }
--------------------------------------------------------------------------------
/Source/HtmlRenderer.WPF/HtmlRenderer.WPF.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 | net462
4 | Library
5 | TheArtOfDev.HtmlRenderer.WPF
6 | true
7 | false
8 | true
9 |
10 |
11 |
12 | HtmlRenderer.WPF
13 | HTML Renderer for WPF
14 | html render renderer draw control WPF
15 | Multipurpose (UI Controls / Image generation), 100% managed (C#), High performance HTML Rendering library for WPF.
16 |
17 | HTML UI in .NET WPF applications using controls or static rendering.
18 |
19 | Features and Benefits:
20 | ---
21 | * Controls: HtmlPanel, HtmlLabel.
22 | * Create images from HTML snippets.
23 | * 100% managed code and no external dependencies, no ActiveX, no MSHTML.
24 | * Extensive HTML 4.01 and CSS level 2 specifications support.
25 | * Support separating CSS from HTML by loading stylesheet code separately.
26 | * Support text selection, copy-paste and context menu.
27 | * Handles "real world" malformed HTML, it doesn't have to be XHTML.
28 | * Supports .NET 3.0 or higher including Client Profile.
29 | * Lightweight, only two DLLs (~300K).
30 | * High performance and low memory footprint.
31 | * Extendable and configurable.
32 | Multipurpose (UI Controls / Image generation), 100% managed (C#), High performance HTML Rendering library for WPF.
33 |
34 |
35 | bin\Release.WPF\
36 |
37 |
38 |
39 |
40 |
41 |
42 |
43 |
44 |
45 | Properties\SharedAssemblyInfo.cs
46 |
47 |
48 |
49 |
--------------------------------------------------------------------------------
/Source/HtmlRenderer/Core/Entities/CssBlockSelectorItem.cs:
--------------------------------------------------------------------------------
1 | // "Therefore those skilled at the unorthodox
2 | // are infinite as heaven and earth,
3 | // inexhaustible as the great rivers.
4 | // When they come to an end,
5 | // they begin again,
6 | // like the days and months;
7 | // they die and are reborn,
8 | // like the four seasons."
9 | //
10 | // - Sun Tsu,
11 | // "The Art of War"
12 |
13 | using TheArtOfDev.HtmlRenderer.Core.Utils;
14 |
15 | namespace TheArtOfDev.HtmlRenderer.Core.Entities
16 | {
17 | ///
18 | /// Holds single class selector in css block hierarchical selection (p class1 > div.class2)
19 | ///
20 | public struct CssBlockSelectorItem
21 | {
22 | #region Fields and Consts
23 |
24 | ///
25 | /// the name of the css class of the block
26 | ///
27 | private readonly string _class;
28 |
29 | ///
30 | /// is the selector item has to be direct parent
31 | ///
32 | private readonly bool _directParent;
33 |
34 | #endregion
35 |
36 |
37 | ///
38 | /// Creates a new block from the block's source
39 | ///
40 | /// the name of the css class of the block
41 | ///
42 | public CssBlockSelectorItem(string @class, bool directParent)
43 | {
44 | ArgChecker.AssertArgNotNullOrEmpty(@class, "@class");
45 |
46 | _class = @class;
47 | _directParent = directParent;
48 | }
49 |
50 | ///
51 | /// the name of the css class of the block
52 | ///
53 | public string Class
54 | {
55 | get { return _class; }
56 | }
57 |
58 | ///
59 | /// is the selector item has to be direct parent
60 | ///
61 | public bool DirectParent
62 | {
63 | get { return _directParent; }
64 | }
65 |
66 | ///
67 | /// Returns a that represents the current .
68 | ///
69 | public override string ToString()
70 | {
71 | return _class + (_directParent ? " > " : string.Empty);
72 | }
73 | }
74 | }
--------------------------------------------------------------------------------
/Source/Demo/Common/TestSamples/21.Bullets.htm:
--------------------------------------------------------------------------------
1 |
2 |
3 |
11 | For years, I (Jose) have been planning for a project like this. I prepared
12 | my self quite well. I went through the entire CSS Level 2 specification along with
13 | the HTML 4.01 specification.
14 |
15 |
16 | One of the most interesting things I found is this: Drawing HTML is no more than
17 | laying out a bunch of boxes with borders margins and padding's. Once you overpass
18 | this paradigm, everything else is to help the code actually place the boxes on the
19 | right place, and then paint the string each box contains.
20 |
21 |
22 | Imagine the power that drawing full-rich-formatted HTML on your controls can give
23 | to your applications. Use bold when you need it, italics on every message, and borders
24 | and fonts as you may like or need everywhere on the desktop application. One of
25 | the first projects where I will use it is on the tooltips of my Ribbon Project.
26 |
27 |
28 | Although I have not tested it on mono yet, there should be no problem at all, since
29 | all of the code on the library is managed code and the methods it use to paint are
30 | quite basic. It draws lines, rectangles, curves and text.
31 |
32 |
33 |
34 | In October 2012 I (Arthur) was looking to replace the usage of WinForms WebBrowser
35 | control by something that can render complex html and have good performance and stability
36 | characteristics. Obviously I was looking for fully managed solution preferably one that
37 | I will have full control over.
38 |
39 |
40 | HTML Renderer project showed great promise but had significant performance issues,
41 | lacked many features (primary text selection) and wasn't updated for more than 3 years.
42 | Realizing there is no alternative I embraced the project making it my baby.
43 |
44 |
45 |
46 |
47 |
48 |
49 |
--------------------------------------------------------------------------------
/Source/HtmlRenderer.WinForms/Adapters/ContextMenuAdapter.cs:
--------------------------------------------------------------------------------
1 | // "Therefore those skilled at the unorthodox
2 | // are infinite as heaven and earth,
3 | // inexhaustible as the great rivers.
4 | // When they come to an end,
5 | // they begin again,
6 | // like the days and months;
7 | // they die and are reborn,
8 | // like the four seasons."
9 | //
10 | // - Sun Tsu,
11 | // "The Art of War"
12 |
13 | using System;
14 | using System.Windows.Forms;
15 | using TheArtOfDev.HtmlRenderer.Adapters.Entities;
16 | using TheArtOfDev.HtmlRenderer.Core.Utils;
17 | using TheArtOfDev.HtmlRenderer.Adapters;
18 | using TheArtOfDev.HtmlRenderer.WinForms.Utilities;
19 |
20 | namespace TheArtOfDev.HtmlRenderer.WinForms.Adapters
21 | {
22 | ///
23 | /// Adapter for WinForms context menu for core.
24 | ///
25 | internal sealed class ContextMenuAdapter : RContextMenu
26 | {
27 | #region Fields and Consts
28 |
29 | ///
30 | /// the underline win forms context menu
31 | ///
32 | private readonly ContextMenuStrip _contextMenu;
33 |
34 | #endregion
35 |
36 |
37 | ///
38 | /// Init.
39 | ///
40 | public ContextMenuAdapter()
41 | {
42 | _contextMenu = new ContextMenuStrip();
43 | _contextMenu.ShowImageMargin = false;
44 | }
45 |
46 | public override int ItemsCount
47 | {
48 | get { return _contextMenu.Items.Count; }
49 | }
50 |
51 | public override void AddDivider()
52 | {
53 | _contextMenu.Items.Add("-");
54 | }
55 |
56 | public override void AddItem(string text, bool enabled, EventHandler onClick)
57 | {
58 | ArgChecker.AssertArgNotNullOrEmpty(text, "text");
59 | ArgChecker.AssertArgNotNull(onClick, "onClick");
60 |
61 | var item = _contextMenu.Items.Add(text, null, onClick);
62 | item.Enabled = enabled;
63 | }
64 |
65 | public override void RemoveLastDivider()
66 | {
67 | if (_contextMenu.Items[_contextMenu.Items.Count - 1].Text == string.Empty)
68 | _contextMenu.Items.RemoveAt(_contextMenu.Items.Count - 1);
69 | }
70 |
71 | public override void Show(RControl parent, RPoint location)
72 | {
73 | _contextMenu.Show(((ControlAdapter)parent).Control, Utils.ConvertRound(location));
74 | }
75 |
76 | public override void Dispose()
77 | {
78 | _contextMenu.Dispose();
79 | }
80 | }
81 | }
--------------------------------------------------------------------------------
/Source/Demo/Common/Samples/04.Links.htm:
--------------------------------------------------------------------------------
1 |
2 |
3 | Links
4 |
5 |
6 |
7 |
Links
8 |
9 |
10 |
11 | HTML Renderer supports all html hyperlinks specification and code interception.
12 |
13 |
14 |
URI href
15 | Any valid URI path will activate the default internet browser.
16 |
17 | Check the context menu options on the link as well by right clicking on it.
18 |
19 | This is a URI link to HTML Renderer (href="https://codeplexarchive.org/project/HtmlRenderer")
20 |
21 |
22 |
File path href
23 | Any file system path, It will be started as if you typed it on the Run Windows dialog.
24 |
25 | This is a link to hard drive(href="C:\")
26 |
27 |
28 |
Anchors href
29 | Link to elements on the page using element id will scroll to that element so it will be at the top.
30 |
31 | This is a link to anchor at the bottom of the page (href="#anchor")
32 |
33 |
34 |
Intercept
35 | Any link click can be intercepted by LinkClicked event, analyzed by link 'href' value or
36 | other attributes of the link element and cancel the default processing those allowing custom handling.
37 |
38 | This link is intercepted to show message box Hello! (href="SayHello")
39 |
40 |
41 |
42 |
43 |
44 |
45 |
46 |
47 |
48 |
49 |
50 |
51 |
52 |
53 |
54 |
55 |
56 |
57 |
58 |
59 |
60 |
61 |
62 |
63 |
8 | Lorem ipsum dolor sit amet, consectetur adipiscing elit. Curabitur ornare mollis
9 | elit. Integer sagittis. Fusce elementum commodo felis. Vivamus lacinia eleifend
10 | libero. Donec lacus. Nam sit amet urna. Nullam nulla. Donec accumsan porta magna.
11 | Mauris a dolor eu elit rutrum commodo. Nam iaculis turpis non augue. Nullam lobortis
12 | egestas risus. Nulla elementum dolor ac mauris. Ut tristique. In varius volutpat
13 | metus. Integer leo dolor, tristique a, dignissim ac, iaculis eget, elit. Donec arcu.
14 |
15 |
21 | Lorem ipsum dolor sit amet, consectetur adipiscing elit. Curabitur ornare mollis
22 | elit. Integer sagittis. Fusce elementum commodo felis. Vivamus lacinia eleifend
23 | libero. Donec lacus. Nam sit amet urna. Nullam nulla. Donec accumsan porta magna.
24 | Mauris a dolor eu elit rutrum commodo. Nam iaculis turpis non augue. Nullam lobortis
25 | egestas risus. Nulla elementum dolor ac mauris. Ut tristique. In varius volutpat
26 | metus. Integer leo dolor, tristique a, dignissim ac, iaculis eget, elit. Donec arcu.
27 |
28 |
The image should also be limited by size because it has: style="width:90%"
29 |
30 |
31 |
32 |
33 |
34 |
35 |
36 |
--------------------------------------------------------------------------------
/Source/HtmlRenderer.PdfSharp/Adapters/PenAdapter.cs:
--------------------------------------------------------------------------------
1 | // "Therefore those skilled at the unorthodox
2 | // are infinite as heaven and earth,
3 | // inexhaustible as the great rivers.
4 | // When they come to an end,
5 | // they begin again,
6 | // like the days and months;
7 | // they die and are reborn,
8 | // like the four seasons."
9 | //
10 | // - Sun Tsu,
11 | // "The Art of War"
12 |
13 | using TheArtOfDev.HtmlRenderer.Adapters;
14 | using TheArtOfDev.HtmlRenderer.Adapters.Entities;
15 | using PdfSharp.Drawing;
16 |
17 | namespace TheArtOfDev.HtmlRenderer.PdfSharp.Adapters
18 | {
19 | ///
20 | /// Adapter for WinForms pens objects for core.
21 | ///
22 | internal sealed class PenAdapter : RPen
23 | {
24 | ///
25 | /// The actual WinForms brush instance.
26 | ///
27 | private readonly XPen _pen;
28 |
29 | ///
30 | /// Init.
31 | ///
32 | public PenAdapter(XPen pen)
33 | {
34 | _pen = pen;
35 | }
36 |
37 | ///
38 | /// The actual WinForms brush instance.
39 | ///
40 | public XPen Pen
41 | {
42 | get { return _pen; }
43 | }
44 |
45 | public override double Width
46 | {
47 | get { return _pen.Width; }
48 | set { _pen.Width = value; }
49 | }
50 |
51 | public override RDashStyle DashStyle
52 | {
53 | set
54 | {
55 | switch (value)
56 | {
57 | case RDashStyle.Solid:
58 | _pen.DashStyle = XDashStyle.Solid;
59 | break;
60 | case RDashStyle.Dash:
61 | _pen.DashStyle = XDashStyle.Dash;
62 | if (Width < 2)
63 | _pen.DashPattern = new[] { 4, 4d }; // better looking
64 | break;
65 | case RDashStyle.Dot:
66 | _pen.DashStyle = XDashStyle.Dot;
67 | break;
68 | case RDashStyle.DashDot:
69 | _pen.DashStyle = XDashStyle.DashDot;
70 | break;
71 | case RDashStyle.DashDotDot:
72 | _pen.DashStyle = XDashStyle.DashDotDot;
73 | break;
74 | case RDashStyle.Custom:
75 | _pen.DashStyle = XDashStyle.Custom;
76 | break;
77 | default:
78 | _pen.DashStyle = XDashStyle.Solid;
79 | break;
80 | }
81 | }
82 | }
83 | }
84 | }
--------------------------------------------------------------------------------
/Source/HtmlRenderer/Core/Entities/LinkElementData.cs:
--------------------------------------------------------------------------------
1 | // "Therefore those skilled at the unorthodox
2 | // are infinite as heaven and earth,
3 | // inexhaustible as the great rivers.
4 | // When they come to an end,
5 | // they begin again,
6 | // like the days and months;
7 | // they die and are reborn,
8 | // like the four seasons."
9 | //
10 | // - Sun Tsu,
11 | // "The Art of War"
12 |
13 | namespace TheArtOfDev.HtmlRenderer.Core.Entities
14 | {
15 | ///
16 | /// Holds data on link element in HTML.
17 | /// Used to expose data outside of HTML Renderer internal structure.
18 | ///
19 | public sealed class LinkElementData
20 | {
21 | ///
22 | /// the id of the link element if present
23 | ///
24 | private readonly string _id;
25 |
26 | ///
27 | /// the href data of the link
28 | ///
29 | private readonly string _href;
30 |
31 | ///
32 | /// the rectangle of element as calculated by html layout
33 | ///
34 | private readonly T _rectangle;
35 |
36 | ///
37 | /// Init.
38 | ///
39 | public LinkElementData(string id, string href, T rectangle)
40 | {
41 | _id = id;
42 | _href = href;
43 | _rectangle = rectangle;
44 | }
45 |
46 | ///
47 | /// the id of the link element if present
48 | ///
49 | public string Id
50 | {
51 | get { return _id; }
52 | }
53 |
54 | ///
55 | /// the href data of the link
56 | ///
57 | public string Href
58 | {
59 | get { return _href; }
60 | }
61 |
62 | ///
63 | /// the rectangle of element as calculated by html layout
64 | ///
65 | public T Rectangle
66 | {
67 | get { return _rectangle; }
68 | }
69 |
70 | ///
71 | /// Is the link is directed to another element in the html
72 | ///
73 | public bool IsAnchor
74 | {
75 | get { return _href.Length > 0 && _href[0] == '#'; }
76 | }
77 |
78 | ///
79 | /// Return the id of the element this anchor link is referencing.
80 | ///
81 | public string AnchorId
82 | {
83 | get { return IsAnchor && _href.Length > 1 ? _href.Substring(1) : string.Empty; }
84 | }
85 |
86 | public override string ToString()
87 | {
88 | return string.Format("Id: {0}, Href: {1}, Rectangle: {2}", _id, _href, _rectangle);
89 | }
90 | }
91 | }
--------------------------------------------------------------------------------
/Source/HtmlRenderer.WPF/Adapters/PenAdapter.cs:
--------------------------------------------------------------------------------
1 | // "Therefore those skilled at the unorthodox
2 | // are infinite as heaven and earth,
3 | // inexhaustible as the great rivers.
4 | // When they come to an end,
5 | // they begin again,
6 | // like the days and months;
7 | // they die and are reborn,
8 | // like the four seasons."
9 | //
10 | // - Sun Tsu,
11 | // "The Art of War"
12 |
13 | using System.Windows.Media;
14 | using TheArtOfDev.HtmlRenderer.Adapters;
15 | using TheArtOfDev.HtmlRenderer.Adapters.Entities;
16 |
17 | namespace TheArtOfDev.HtmlRenderer.WPF.Adapters
18 | {
19 | ///
20 | /// Adapter for WPF pens objects for core.
21 | ///
22 | internal sealed class PenAdapter : RPen
23 | {
24 | ///
25 | /// The actual WPF brush instance.
26 | ///
27 | private readonly Brush _brush;
28 |
29 | ///
30 | /// the width of the pen
31 | ///
32 | private double _width;
33 |
34 | ///
35 | /// the dash style of the pen
36 | ///
37 | private DashStyle _dashStyle = DashStyles.Solid;
38 |
39 | ///
40 | /// Init.
41 | ///
42 | public PenAdapter(Brush brush)
43 | {
44 | _brush = brush;
45 | }
46 |
47 | public override double Width
48 | {
49 | get { return _width; }
50 | set { _width = value; }
51 | }
52 |
53 | public override RDashStyle DashStyle
54 | {
55 | set
56 | {
57 | switch (value)
58 | {
59 | case RDashStyle.Solid:
60 | _dashStyle = DashStyles.Solid;
61 | break;
62 | case RDashStyle.Dash:
63 | _dashStyle = DashStyles.Dash;
64 | break;
65 | case RDashStyle.Dot:
66 | _dashStyle = DashStyles.Dot;
67 | break;
68 | case RDashStyle.DashDot:
69 | _dashStyle = DashStyles.DashDot;
70 | break;
71 | case RDashStyle.DashDotDot:
72 | _dashStyle = DashStyles.DashDotDot;
73 | break;
74 | default:
75 | _dashStyle = DashStyles.Solid;
76 | break;
77 | }
78 | }
79 | }
80 |
81 | ///
82 | /// Create the actual WPF pen instance.
83 | ///
84 | public Pen CreatePen()
85 | {
86 | var pen = new Pen(_brush, _width);
87 | pen.DashStyle = _dashStyle;
88 | return pen;
89 | }
90 | }
91 | }
--------------------------------------------------------------------------------
/Source/HtmlRenderer.WinForms/HtmlRenderer.WinForms.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 | net462
4 | Library
5 | TheArtOfDev.HtmlRenderer.WinForms
6 | publish\
7 | true
8 | false
9 | true
10 |
11 |
12 |
13 | HtmlRenderer.WinForms
14 | HTML Renderer for WinForms
15 | html render renderer draw control winforms
16 | Multipurpose (UI Controls / Image generation), 100% managed (C#), High performance HTML Rendering library for WinForms.
17 |
18 | HTML UI in .NET WinForms applications using controls or static rendering.
19 |
20 | Features and Benefits:
21 | ---
22 | * Controls: HtmlPanel, HtmlLabel, HtmlToolTip.
23 | * Create images from HTML snippets.
24 | * 100% managed code and no external dependencies, no ActiveX, no MSHTML.
25 | * Extensive HTML 4.01 and CSS level 2 specifications support.
26 | * Support separating CSS from HTML by loading stylesheet code separately.
27 | * Support text selection, copy-paste and context menu.
28 | * Handles "real world" malformed HTML, it doesn't have to be XHTML.
29 | * Lightweight, only two DLLs (~300K).
30 | * High performance and low memory footprint.
31 | * Extendable and configurable.
32 | Multipurpose (UI Controls / Image generation), 100% managed (C#), High performance HTML Rendering library for WinForms.
33 |
34 |
35 | bin\Debug\
36 | AllRules.ruleset
37 |
38 |
39 | bin\Release\
40 | AllRules.ruleset
41 |
42 |
43 |
44 | Properties\SharedAssemblyInfo.cs
45 |
46 |
47 | Component
48 |
49 |
50 | Component
51 |
52 |
53 | Component
54 |
55 |
56 |
57 |
58 |
59 |
60 |
--------------------------------------------------------------------------------
/Source/HtmlRenderer.WPF/Adapters/ContextMenuAdapter.cs:
--------------------------------------------------------------------------------
1 | // "Therefore those skilled at the unorthodox
2 | // are infinite as heaven and earth,
3 | // inexhaustible as the great rivers.
4 | // When they come to an end,
5 | // they begin again,
6 | // like the days and months;
7 | // they die and are reborn,
8 | // like the four seasons."
9 | //
10 | // - Sun Tsu,
11 | // "The Art of War"
12 |
13 | using System;
14 | using System.Windows;
15 | using System.Windows.Controls;
16 | using TheArtOfDev.HtmlRenderer.Adapters;
17 | using TheArtOfDev.HtmlRenderer.Adapters.Entities;
18 | using TheArtOfDev.HtmlRenderer.Core.Utils;
19 | using TheArtOfDev.HtmlRenderer.WPF.Utilities;
20 |
21 | namespace TheArtOfDev.HtmlRenderer.WPF.Adapters
22 | {
23 | ///
24 | /// Adapter for WPF context menu for core.
25 | ///
26 | internal sealed class ContextMenuAdapter : RContextMenu
27 | {
28 | #region Fields and Consts
29 |
30 | ///
31 | /// the underline WPF context menu
32 | ///
33 | private readonly ContextMenu _contextMenu;
34 |
35 | #endregion
36 |
37 |
38 | ///
39 | /// Init.
40 | ///
41 | public ContextMenuAdapter()
42 | {
43 | _contextMenu = new ContextMenu();
44 | }
45 |
46 | public override int ItemsCount
47 | {
48 | get { return _contextMenu.Items.Count; }
49 | }
50 |
51 | public override void AddDivider()
52 | {
53 | _contextMenu.Items.Add(new Separator());
54 | }
55 |
56 | public override void AddItem(string text, bool enabled, EventHandler onClick)
57 | {
58 | ArgChecker.AssertArgNotNullOrEmpty(text, "text");
59 | ArgChecker.AssertArgNotNull(onClick, "onClick");
60 |
61 | var item = new MenuItem();
62 | item.Header = text;
63 | item.IsEnabled = enabled;
64 | item.Click += new RoutedEventHandler(onClick);
65 | _contextMenu.Items.Add(item);
66 | }
67 |
68 | public override void RemoveLastDivider()
69 | {
70 | if (_contextMenu.Items[_contextMenu.Items.Count - 1].GetType() == typeof(Separator))
71 | _contextMenu.Items.RemoveAt(_contextMenu.Items.Count - 1);
72 | }
73 |
74 | public override void Show(RControl parent, RPoint location)
75 | {
76 | _contextMenu.PlacementTarget = ((ControlAdapter)parent).Control;
77 | _contextMenu.PlacementRectangle = new Rect(Utils.ConvertRound(location), Size.Empty);
78 | _contextMenu.IsOpen = true;
79 | }
80 |
81 | public override void Dispose()
82 | {
83 | _contextMenu.IsOpen = false;
84 | _contextMenu.PlacementTarget = null;
85 | _contextMenu.Items.Clear();
86 | }
87 | }
88 | }
--------------------------------------------------------------------------------
/Source/HtmlRenderer.WinForms/Adapters/PenAdapter.cs:
--------------------------------------------------------------------------------
1 | // "Therefore those skilled at the unorthodox
2 | // are infinite as heaven and earth,
3 | // inexhaustible as the great rivers.
4 | // When they come to an end,
5 | // they begin again,
6 | // like the days and months;
7 | // they die and are reborn,
8 | // like the four seasons."
9 | //
10 | // - Sun Tsu,
11 | // "The Art of War"
12 |
13 | using System.Drawing;
14 | using TheArtOfDev.HtmlRenderer.Adapters.Entities;
15 | using TheArtOfDev.HtmlRenderer.Adapters;
16 |
17 | namespace TheArtOfDev.HtmlRenderer.WinForms.Adapters
18 | {
19 | ///
20 | /// Adapter for WinForms pens objects for core.
21 | ///
22 | internal sealed class PenAdapter : RPen
23 | {
24 | ///
25 | /// The actual WinForms brush instance.
26 | ///
27 | private readonly Pen _pen;
28 |
29 | ///
30 | /// Init.
31 | ///
32 | public PenAdapter(Pen pen)
33 | {
34 | _pen = pen;
35 | }
36 |
37 | ///
38 | /// The actual WinForms brush instance.
39 | ///
40 | public Pen Pen
41 | {
42 | get { return _pen; }
43 | }
44 |
45 | public override double Width
46 | {
47 | get { return _pen.Width; }
48 | set { _pen.Width = (float)value; }
49 | }
50 |
51 | public override RDashStyle DashStyle
52 | {
53 | set
54 | {
55 | switch (value)
56 | {
57 | case RDashStyle.Solid:
58 | _pen.DashStyle = System.Drawing.Drawing2D.DashStyle.Solid;
59 | break;
60 | case RDashStyle.Dash:
61 | _pen.DashStyle = System.Drawing.Drawing2D.DashStyle.Dash;
62 | if (Width < 2)
63 | _pen.DashPattern = new[] { 4, 4f }; // better looking
64 | break;
65 | case RDashStyle.Dot:
66 | _pen.DashStyle = System.Drawing.Drawing2D.DashStyle.Dot;
67 | break;
68 | case RDashStyle.DashDot:
69 | _pen.DashStyle = System.Drawing.Drawing2D.DashStyle.DashDot;
70 | break;
71 | case RDashStyle.DashDotDot:
72 | _pen.DashStyle = System.Drawing.Drawing2D.DashStyle.DashDotDot;
73 | break;
74 | case RDashStyle.Custom:
75 | _pen.DashStyle = System.Drawing.Drawing2D.DashStyle.Custom;
76 | break;
77 | default:
78 | _pen.DashStyle = System.Drawing.Drawing2D.DashStyle.Solid;
79 | break;
80 | }
81 | }
82 | }
83 | }
84 | }
--------------------------------------------------------------------------------
/Source/HtmlRenderer.WPF/Adapters/ControlAdapter.cs:
--------------------------------------------------------------------------------
1 | // "Therefore those skilled at the unorthodox
2 | // are infinite as heaven and earth,
3 | // inexhaustible as the great rivers.
4 | // When they come to an end,
5 | // they begin again,
6 | // like the days and months;
7 | // they die and are reborn,
8 | // like the four seasons."
9 | //
10 | // - Sun Tsu,
11 | // "The Art of War"
12 |
13 | using System.Windows;
14 | using System.Windows.Controls;
15 | using System.Windows.Input;
16 | using TheArtOfDev.HtmlRenderer.Adapters;
17 | using TheArtOfDev.HtmlRenderer.Adapters.Entities;
18 | using TheArtOfDev.HtmlRenderer.Core.Utils;
19 | using TheArtOfDev.HtmlRenderer.WPF.Utilities;
20 |
21 | namespace TheArtOfDev.HtmlRenderer.WPF.Adapters
22 | {
23 | ///
24 | /// Adapter for WPF Control for core.
25 | ///
26 | internal sealed class ControlAdapter : RControl
27 | {
28 | ///
29 | /// the underline WPF control.
30 | ///
31 | private readonly Control _control;
32 |
33 | ///
34 | /// Init.
35 | ///
36 | public ControlAdapter(Control control)
37 | : base(WpfAdapter.Instance)
38 | {
39 | ArgChecker.AssertArgNotNull(control, "control");
40 |
41 | _control = control;
42 | }
43 |
44 | ///
45 | /// Get the underline WPF control
46 | ///
47 | public Control Control
48 | {
49 | get { return _control; }
50 | }
51 |
52 | public override RPoint MouseLocation
53 | {
54 | get { return Utils.Convert(_control.PointFromScreen(Mouse.GetPosition(_control))); }
55 | }
56 |
57 | public override bool LeftMouseButton
58 | {
59 | get { return Mouse.LeftButton == MouseButtonState.Pressed; }
60 | }
61 |
62 | public override bool RightMouseButton
63 | {
64 | get { return Mouse.RightButton == MouseButtonState.Pressed; }
65 | }
66 |
67 | public override void SetCursorDefault()
68 | {
69 | _control.Cursor = Cursors.Arrow;
70 | }
71 |
72 | public override void SetCursorHand()
73 | {
74 | _control.Cursor = Cursors.Hand;
75 | }
76 |
77 | public override void SetCursorIBeam()
78 | {
79 | _control.Cursor = Cursors.IBeam;
80 | }
81 |
82 | public override void DoDragDropCopy(object dragDropData)
83 | {
84 | DragDrop.DoDragDrop(_control, dragDropData, DragDropEffects.Copy);
85 | }
86 |
87 | public override void MeasureString(string str, RFont font, double maxWidth, out int charFit, out double charFitWidth)
88 | {
89 | using (var g = new GraphicsAdapter())
90 | {
91 | g.MeasureString(str, font, maxWidth, out charFit, out charFitWidth);
92 | }
93 | }
94 |
95 | public override void Invalidate()
96 | {
97 | _control.InvalidateVisual();
98 | }
99 | }
100 | }
--------------------------------------------------------------------------------
/Source/HtmlRenderer.PdfSharp/Adapters/FontAdapter.cs:
--------------------------------------------------------------------------------
1 | // "Therefore those skilled at the unorthodox
2 | // are infinite as heaven and earth,
3 | // inexhaustible as the great rivers.
4 | // When they come to an end,
5 | // they begin again,
6 | // like the days and months;
7 | // they die and are reborn,
8 | // like the four seasons."
9 | //
10 | // - Sun Tsu,
11 | // "The Art of War"
12 |
13 | using TheArtOfDev.HtmlRenderer.Adapters;
14 | using PdfSharp.Drawing;
15 |
16 | namespace TheArtOfDev.HtmlRenderer.PdfSharp.Adapters
17 | {
18 | ///
19 | /// Adapter for WinForms Font object for core.
20 | ///
21 | internal sealed class FontAdapter : RFont
22 | {
23 | #region Fields and Consts
24 |
25 | ///
26 | /// the underline win-forms font.
27 | ///
28 | private readonly XFont _font;
29 |
30 | ///
31 | /// the vertical offset of the font underline location from the top of the font.
32 | ///
33 | private double _underlineOffset = -1;
34 |
35 | ///
36 | /// Cached font height.
37 | ///
38 | private double _height = -1;
39 |
40 | ///
41 | /// Cached font whitespace width.
42 | ///
43 | private double _whitespaceWidth = -1;
44 |
45 | #endregion
46 |
47 |
48 | ///
49 | /// Init.
50 | ///
51 | public FontAdapter(XFont font)
52 | {
53 | _font = font;
54 | }
55 |
56 | ///
57 | /// the underline win-forms font.
58 | ///
59 | public XFont Font
60 | {
61 | get { return _font; }
62 | }
63 |
64 | public override double Size
65 | {
66 | get { return _font.Size; }
67 | }
68 |
69 | public override double UnderlineOffset
70 | {
71 | get { return _underlineOffset; }
72 | }
73 |
74 | public override double Height
75 | {
76 | get { return _height; }
77 | }
78 |
79 | public override double LeftPadding
80 | {
81 | get { return _height / 6f; }
82 | }
83 |
84 |
85 | public override double GetWhitespaceWidth(RGraphics graphics)
86 | {
87 | if (_whitespaceWidth < 0)
88 | {
89 | _whitespaceWidth = graphics.MeasureString(" ", this).Width;
90 | }
91 | return _whitespaceWidth;
92 | }
93 |
94 | ///
95 | /// Set font metrics to be cached for the font for future use.
96 | ///
97 | /// the full height of the font
98 | /// the vertical offset of the font underline location from the top of the font.
99 | internal void SetMetrics(int height, int underlineOffset)
100 | {
101 | _height = height;
102 | _underlineOffset = underlineOffset;
103 | }
104 | }
105 | }
--------------------------------------------------------------------------------
/Source/Demo/WPF/MainControl.xaml:
--------------------------------------------------------------------------------
1 |
10 |
11 |
12 |
13 |
14 |
15 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 |
33 |
34 |
35 |
36 |
37 |
38 |
39 |
40 |
42 |
43 |
47 |
49 | Colored
50 |
51 |
52 |
53 |
--------------------------------------------------------------------------------
/Source/HtmlRenderer.PdfSharp/Utilities/Utils.cs:
--------------------------------------------------------------------------------
1 | // "Therefore those skilled at the unorthodox
2 | // are infinite as heaven and earth,
3 | // inexhaustible as the great rivers.
4 | // When they come to an end,
5 | // they begin again,
6 | // like the days and months;
7 | // they die and are reborn,
8 | // like the four seasons."
9 | //
10 | // - Sun Tsu,
11 | // "The Art of War"
12 |
13 | using PdfSharp.Drawing;
14 | using System.Drawing;
15 | using TheArtOfDev.HtmlRenderer.Adapters.Entities;
16 |
17 | namespace TheArtOfDev.HtmlRenderer.PdfSharp.Utilities
18 | {
19 | ///
20 | /// Utilities for converting WinForms entities to HtmlRenderer core entities.
21 | ///
22 | internal static class Utils
23 | {
24 | ///
25 | /// Convert from WinForms point to core point.
26 | ///
27 | public static RPoint Convert(XPoint p)
28 | {
29 | return new RPoint(p.X, p.Y);
30 | }
31 |
32 | ///
33 | /// Convert from WinForms point to core point.
34 | ///
35 | public static XPoint[] Convert(RPoint[] points)
36 | {
37 | XPoint[] myPoints = new XPoint[points.Length];
38 | for (int i = 0; i < points.Length; i++)
39 | myPoints[i] = Convert(points[i]);
40 | return myPoints;
41 | }
42 |
43 | ///
44 | /// Convert from core point to WinForms point.
45 | ///
46 | public static XPoint Convert(RPoint p)
47 | {
48 | return new XPoint(p.X, p.Y);
49 | }
50 |
51 | ///
52 | /// Convert from WinForms size to core size.
53 | ///
54 | public static RSize Convert(XSize s)
55 | {
56 | return new RSize(s.Width, s.Height);
57 | }
58 |
59 | ///
60 | /// Convert from core size to WinForms size.
61 | ///
62 | public static XSize Convert(RSize s)
63 | {
64 | return new XSize(s.Width, s.Height);
65 | }
66 |
67 | ///
68 | /// Convert from WinForms rectangle to core rectangle.
69 | ///
70 | public static RRect Convert(XRect r)
71 | {
72 | return new RRect(r.X, r.Y, r.Width, r.Height);
73 | }
74 |
75 | ///
76 | /// Convert from core rectangle to WinForms rectangle.
77 | ///
78 | public static XRect Convert(RRect r)
79 | {
80 | return new XRect(r.X, r.Y, r.Width, r.Height);
81 | }
82 |
83 | ///
84 | /// Convert from core color to WinForms color.
85 | ///
86 | public static XColor Convert(RColor c)
87 | {
88 | return XColor.FromArgb(c.A, c.R, c.G, c.B);
89 | }
90 |
91 | ///
92 | /// Convert from color to WinForms color.
93 | ///
94 | public static RColor Convert(Color c)
95 | {
96 | return RColor.FromArgb(c.A, c.R, c.G, c.B);
97 | }
98 |
99 | }
100 | }
--------------------------------------------------------------------------------
/Source/Demo/Common/Resources.cs:
--------------------------------------------------------------------------------
1 | // "Therefore those skilled at the unorthodox
2 | // are infinite as heaven and earth,
3 | // inexhaustible as the great rivers.
4 | // When they come to an end,
5 | // they begin again,
6 | // like the days and months;
7 | // they die and are reborn,
8 | // like the four seasons."
9 | //
10 | // - Sun Tsu,
11 | // "The Art of War"
12 |
13 | using System.IO;
14 |
15 | namespace TheArtOfDev.HtmlRenderer.Demo.Common
16 | {
17 | ///
18 | /// Get font, image and text resources for HtmlRenderer demo.
19 | ///
20 | public static class Resources
21 | {
22 | public static byte[] CustomFont
23 | {
24 | get
25 | {
26 | var stream = GetManifestResourceStream("CustomFont.ttf");
27 |
28 | byte[] buffer = new byte[16 * 1024];
29 | using (MemoryStream ms = new MemoryStream())
30 | {
31 | int read;
32 | while ((read = stream.Read(buffer, 0, buffer.Length)) > 0)
33 | {
34 | ms.Write(buffer, 0, read);
35 | }
36 | return ms.ToArray();
37 | }
38 | }
39 | }
40 |
41 | public static Stream Comment16
42 | {
43 | get { return GetManifestResourceStream("comment16.gif"); }
44 | }
45 |
46 | public static Stream Event16
47 | {
48 | get { return GetManifestResourceStream("Event16.png"); }
49 | }
50 |
51 | public static Stream Favorites32
52 | {
53 | get { return GetManifestResourceStream("favorites32.png"); }
54 | }
55 |
56 | public static Stream Font32
57 | {
58 | get { return GetManifestResourceStream("font32.png"); }
59 | }
60 |
61 | public static Stream Html32
62 | {
63 | get { return GetManifestResourceStream("html32.png"); }
64 | }
65 |
66 | public static Stream Image32
67 | {
68 | get { return GetManifestResourceStream("image32.png"); }
69 | }
70 |
71 | public static Stream Method16
72 | {
73 | get { return GetManifestResourceStream("method16.gif"); }
74 | }
75 |
76 | public static Stream Property16
77 | {
78 | get { return GetManifestResourceStream("property16.gif"); }
79 | }
80 |
81 | public static Stream WebPallete
82 | {
83 | get { return GetManifestResourceStream("web_pallete.gif"); }
84 | }
85 |
86 | public static string Tooltip
87 | {
88 | get
89 | {
90 | using (var reader = new StreamReader(GetManifestResourceStream("Tooltip.html")))
91 | {
92 | return reader.ReadToEnd();
93 | }
94 | }
95 | }
96 |
97 | private static Stream GetManifestResourceStream(string name)
98 | {
99 | return typeof(Resources).Assembly.GetManifestResourceStream("TheArtOfDev.HtmlRenderer.Demo.Common.Resources." + name);
100 | }
101 | }
102 | }
--------------------------------------------------------------------------------
/Source/HtmlRenderer.PdfSharp/Adapters/GraphicsPathAdapter.cs:
--------------------------------------------------------------------------------
1 | // "Therefore those skilled at the unorthodox
2 | // are infinite as heaven and earth,
3 | // inexhaustible as the great rivers.
4 | // When they come to an end,
5 | // they begin again,
6 | // like the days and months;
7 | // they die and are reborn,
8 | // like the four seasons."
9 | //
10 | // - Sun Tsu,
11 | // "The Art of War"
12 |
13 | using System;
14 | using TheArtOfDev.HtmlRenderer.Adapters;
15 | using TheArtOfDev.HtmlRenderer.Adapters.Entities;
16 | using PdfSharp.Drawing;
17 |
18 | namespace TheArtOfDev.HtmlRenderer.PdfSharp.Adapters
19 | {
20 | ///
21 | /// Adapter for WinForms graphics path object for core.
22 | ///
23 | internal sealed class GraphicsPathAdapter : RGraphicsPath
24 | {
25 | ///
26 | /// The actual PdfSharp graphics path instance.
27 | ///
28 | private readonly XGraphicsPath _graphicsPath = new XGraphicsPath();
29 |
30 | ///
31 | /// the last point added to the path to begin next segment from
32 | ///
33 | private RPoint _lastPoint;
34 |
35 | ///
36 | /// The actual PdfSharp graphics path instance.
37 | ///
38 | public XGraphicsPath GraphicsPath
39 | {
40 | get { return _graphicsPath; }
41 | }
42 |
43 | public override void Start(double x, double y)
44 | {
45 | _lastPoint = new RPoint(x, y);
46 | }
47 |
48 | public override void LineTo(double x, double y)
49 | {
50 | _graphicsPath.AddLine((float)_lastPoint.X, (float)_lastPoint.Y, (float)x, (float)y);
51 | _lastPoint = new RPoint(x, y);
52 | }
53 |
54 | public override void ArcTo(double x, double y, double size, Corner corner)
55 | {
56 | float left = (float)(Math.Min(x, _lastPoint.X) - (corner == Corner.TopRight || corner == Corner.BottomRight ? size : 0));
57 | float top = (float)(Math.Min(y, _lastPoint.Y) - (corner == Corner.BottomLeft || corner == Corner.BottomRight ? size : 0));
58 | _graphicsPath.AddArc(left, top, (float)size * 2, (float)size * 2, GetStartAngle(corner), 90);
59 | _lastPoint = new RPoint(x, y);
60 | }
61 |
62 | public override void Dispose()
63 | { }
64 |
65 | ///
66 | /// Get arc start angle for the given corner.
67 | ///
68 | private static int GetStartAngle(Corner corner)
69 | {
70 | int startAngle;
71 | switch (corner)
72 | {
73 | case Corner.TopLeft:
74 | startAngle = 180;
75 | break;
76 | case Corner.TopRight:
77 | startAngle = 270;
78 | break;
79 | case Corner.BottomLeft:
80 | startAngle = 90;
81 | break;
82 | case Corner.BottomRight:
83 | startAngle = 0;
84 | break;
85 | default:
86 | throw new ArgumentOutOfRangeException("corner");
87 | }
88 | return startAngle;
89 | }
90 | }
91 | }
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # HTML Renderer
2 |
3 | **Cross framework** (WinForms/WPF/PDF/Metro/Mono/etc.), **Multipurpose** (UI Controls / Image generation / PDF generation / etc.), **100% managed** (C#), High performance HTML Rendering library.
4 |
5 | The library is 100% managed **C#** code without any external dependencies (no WebBrowser control, ActiveX / COM or MSHTML dll), the only requirement is **.NET Framework 4.6.2 or higher**.
6 |
7 | 
8 |
9 |
10 | ## Issues & Documentation
11 |
12 | For questions and issues, use the official [GitHub repository](https://github.com/ArthurHub/HTML-Renderer).
13 |
14 | For documentation, check out the project on the [CodePlex Archive](https://codeplexarchive.org/project/HtmlRenderer) or the [personal blog](https://theartofdev.com/tag/html-renderer/).
15 |
16 |
17 | ## Download
18 |
19 | The release packages on [GitHub](https://github.com/ArthurHub/HTML-Renderer/releases) or the [CodePlex Archive](https://codeplexarchive.org/project/HtmlRenderer) also contains a Demo application to explore HML Renderer's capabilities.
20 |
21 | The latest NuGet packages can be found on NuGet.org:
22 |
23 | * [HtmlRenderer.WinForms](https://www.nuget.org/packages/HtmlRenderer.WinForms)
24 | * [HtmlRenderer.WPF](https://www.nuget.org/packages/HtmlRenderer.WPF)
25 | * [HtmlRenderer.Mono](https://www.nuget.org/packages/HtmlRenderer.Mono)
26 | * [HtmlRenderer.PdfSharp](https://www.nuget.org/packages/HtmlRenderer.PdfSharp)
27 | * [HtmlRenderer.Core](https://www.nuget.org/packages/HtmlRenderer.Core)
28 |
29 |
30 | ## Features and Benefits
31 |
32 | * Extensive HTML 4.01 and CSS level 2 specifications support.
33 | * Support separating CSS from HTML by loading stylesheet code separately.
34 | * Support text selection, copy-paste and context menu.
35 | * WinForms controls: HtmlPanel, HtmlLabel and HtmlToolTip.
36 | * WPF controls: HtmlPanel and HtmlLabel.
37 | * Works on Mono.
38 | * Create images/PDFs from HTML snippets.
39 | * Handles "real world" malformed HTML, it doesn't have to be XHTML.
40 | * 100% managed code and no external dependencies.
41 | * Lightweight, just two DLLs (~300K).
42 | * High performance and low memory footprint.
43 | * Extendable and configurable.
44 | * Powerful [Demo application](https://codeplexarchive.org/ProjectTab/Wiki/HtmlRenderer/Documentation/Demo%20application) to explore and learn the library.
45 |
46 |
47 | ## WinForms/WPF controls
48 |
49 | * *HtmlPanel* - The full power of HTML control build to replace WebBrowser control, accepts HTML, text selection, scrollbars, link click intercept, image load intercept and much more.
50 | * *HtmlLabel* - As WinForms label but accepts HTML, text selection, auto-size capabilities, transparent background and more.
51 | * *HtmlToolTip* - As WinForms ToolTip control but accepts HTML and ability to handle links (WinForms only).
52 |
53 |
54 | ## Sample application's
55 |
56 | * Render HTML content generated by rich web editors like forums, blogs, etc.
57 | * Render Office documents converted to HTML.
58 | * Create WinForms UI that requires text selection with clipboard support.
59 | * [Create images from HTML code snippets](https://codeplexarchive.org/ProjectTab/Wiki/HtmlRenderer/Documentation/Image%20generation).
60 | * Create PDF document from HTML code snippets.
61 |
--------------------------------------------------------------------------------
/Source/HtmlRenderer.WinForms/Adapters/GraphicsPathAdapter.cs:
--------------------------------------------------------------------------------
1 | // "Therefore those skilled at the unorthodox
2 | // are infinite as heaven and earth,
3 | // inexhaustible as the great rivers.
4 | // When they come to an end,
5 | // they begin again,
6 | // like the days and months;
7 | // they die and are reborn,
8 | // like the four seasons."
9 | //
10 | // - Sun Tsu,
11 | // "The Art of War"
12 |
13 | using System;
14 | using System.Drawing.Drawing2D;
15 | using TheArtOfDev.HtmlRenderer.Adapters;
16 | using TheArtOfDev.HtmlRenderer.Adapters.Entities;
17 |
18 | namespace TheArtOfDev.HtmlRenderer.WinForms.Adapters
19 | {
20 | ///
21 | /// Adapter for WinForms graphics path object for core.
22 | ///
23 | internal sealed class GraphicsPathAdapter : RGraphicsPath
24 | {
25 | ///
26 | /// The actual WinForms graphics path instance.
27 | ///
28 | private readonly GraphicsPath _graphicsPath = new GraphicsPath();
29 |
30 | ///
31 | /// the last point added to the path to begin next segment from
32 | ///
33 | private RPoint _lastPoint;
34 |
35 | ///
36 | /// The actual WinForms graphics path instance.
37 | ///
38 | public GraphicsPath GraphicsPath
39 | {
40 | get { return _graphicsPath; }
41 | }
42 |
43 | public override void Start(double x, double y)
44 | {
45 | _lastPoint = new RPoint(x, y);
46 | }
47 |
48 | public override void LineTo(double x, double y)
49 | {
50 | _graphicsPath.AddLine((float)_lastPoint.X, (float)_lastPoint.Y, (float)x, (float)y);
51 | _lastPoint = new RPoint(x, y);
52 | }
53 |
54 | public override void ArcTo(double x, double y, double size, Corner corner)
55 | {
56 | float left = (float)(Math.Min(x, _lastPoint.X) - (corner == Corner.TopRight || corner == Corner.BottomRight ? size : 0));
57 | float top = (float)(Math.Min(y, _lastPoint.Y) - (corner == Corner.BottomLeft || corner == Corner.BottomRight ? size : 0));
58 | _graphicsPath.AddArc(left, top, (float)size * 2, (float)size * 2, GetStartAngle(corner), 90);
59 | _lastPoint = new RPoint(x, y);
60 | }
61 |
62 | public override void Dispose()
63 | {
64 | _graphicsPath.Dispose();
65 | }
66 |
67 | ///
68 | /// Get arc start angle for the given corner.
69 | ///
70 | private static int GetStartAngle(Corner corner)
71 | {
72 | int startAngle;
73 | switch (corner)
74 | {
75 | case Corner.TopLeft:
76 | startAngle = 180;
77 | break;
78 | case Corner.TopRight:
79 | startAngle = 270;
80 | break;
81 | case Corner.BottomLeft:
82 | startAngle = 90;
83 | break;
84 | case Corner.BottomRight:
85 | startAngle = 0;
86 | break;
87 | default:
88 | throw new ArgumentOutOfRangeException("corner");
89 | }
90 | return startAngle;
91 | }
92 | }
93 | }
--------------------------------------------------------------------------------
/Source/HtmlRenderer.WinForms/Adapters/ControlAdapter.cs:
--------------------------------------------------------------------------------
1 | // "Therefore those skilled at the unorthodox
2 | // are infinite as heaven and earth,
3 | // inexhaustible as the great rivers.
4 | // When they come to an end,
5 | // they begin again,
6 | // like the days and months;
7 | // they die and are reborn,
8 | // like the four seasons."
9 | //
10 | // - Sun Tsu,
11 | // "The Art of War"
12 |
13 | using System.Windows.Forms;
14 | using TheArtOfDev.HtmlRenderer.Adapters.Entities;
15 | using TheArtOfDev.HtmlRenderer.Core.Utils;
16 | using TheArtOfDev.HtmlRenderer.Adapters;
17 | using TheArtOfDev.HtmlRenderer.WinForms.Utilities;
18 |
19 | namespace TheArtOfDev.HtmlRenderer.WinForms.Adapters
20 | {
21 | ///
22 | /// Adapter for WinForms Control for core.
23 | ///
24 | internal sealed class ControlAdapter : RControl
25 | {
26 | ///
27 | /// the underline win forms control.
28 | ///
29 | private readonly Control _control;
30 |
31 | ///
32 | /// Use GDI+ text rendering to measure/draw text.
33 | ///
34 | private readonly bool _useGdiPlusTextRendering;
35 |
36 | ///
37 | /// Init.
38 | ///
39 | public ControlAdapter(Control control, bool useGdiPlusTextRendering)
40 | : base(WinFormsAdapter.Instance)
41 | {
42 | ArgChecker.AssertArgNotNull(control, "control");
43 |
44 | _control = control;
45 | _useGdiPlusTextRendering = useGdiPlusTextRendering;
46 | }
47 |
48 | ///
49 | /// Get the underline win forms control
50 | ///
51 | public Control Control
52 | {
53 | get { return _control; }
54 | }
55 |
56 | public override RPoint MouseLocation
57 | {
58 | get { return Utils.Convert(_control.PointToClient(Control.MousePosition)); }
59 | }
60 |
61 | public override bool LeftMouseButton
62 | {
63 | get { return (Control.MouseButtons & MouseButtons.Left) != 0; }
64 | }
65 |
66 | public override bool RightMouseButton
67 | {
68 | get { return (Control.MouseButtons & MouseButtons.Right) != 0; }
69 | }
70 |
71 | public override void SetCursorDefault()
72 | {
73 | _control.Cursor = Cursors.Default;
74 | }
75 |
76 | public override void SetCursorHand()
77 | {
78 | _control.Cursor = Cursors.Hand;
79 | }
80 |
81 | public override void SetCursorIBeam()
82 | {
83 | _control.Cursor = Cursors.IBeam;
84 | }
85 |
86 | public override void DoDragDropCopy(object dragDropData)
87 | {
88 | _control.DoDragDrop(dragDropData, DragDropEffects.Copy);
89 | }
90 |
91 | public override void MeasureString(string str, RFont font, double maxWidth, out int charFit, out double charFitWidth)
92 | {
93 | using (var g = new GraphicsAdapter(_control.CreateGraphics(), _useGdiPlusTextRendering, true))
94 | {
95 | g.MeasureString(str, font, maxWidth, out charFit, out charFitWidth);
96 | }
97 | }
98 |
99 | public override void Invalidate()
100 | {
101 | _control.Invalidate();
102 | }
103 | }
104 | }
--------------------------------------------------------------------------------
/.github/workflows/build.yml:
--------------------------------------------------------------------------------
1 | name: Build
2 |
3 | on: [push, pull_request, workflow_dispatch]
4 |
5 | env:
6 | DOTNET_SKIP_FIRST_TIME_EXPERIENCE: 1
7 | DOTNET_NOLOGO: true
8 | DOTNET_CLI_TELEMETRY_OPTOUT: 1
9 | NuGetDirectory: ${{ github.workspace }}/nuget
10 |
11 | defaults:
12 | run:
13 | shell: pwsh
14 |
15 | jobs:
16 | build:
17 | name: ${{ matrix.platform.name }} ${{ matrix.dotnet.name }}
18 | runs-on: ${{ matrix.platform.os }}
19 | permissions:
20 | id-token: write
21 |
22 | strategy:
23 | fail-fast: false
24 | matrix:
25 | platform:
26 | - { name: Windows x64, os: windows-2025 }
27 | - { name: Ubuntu x64, os: ubuntu-24.04 }
28 | - { name: macOS arm64, os: macos-15 }
29 | dotnet:
30 | - { name: .NET 8, version: '8.0.x' }
31 | - { name: .NET 9, version: '9.0.x' }
32 | - { name: .NET 10, version: '10.0.x' }
33 |
34 | steps:
35 | - name: Checkout HTML Renderer
36 | uses: actions/checkout@v6
37 |
38 | - name: Setup .NET ${{ matrix.dotnet.version }} SDK
39 | id: setup-dotnet
40 | uses: actions/setup-dotnet@v5
41 | with:
42 | dotnet-version: ${{ matrix.dotnet.version }}
43 | - name: Enforce SDK Version
44 | run: dotnet new globaljson --sdk-version ${{ steps.setup-dotnet.outputs.dotnet-version }} --force
45 | - name: Verify SDK Installation
46 | run: dotnet --info
47 |
48 | - name: Restore Dependencies
49 | run: dotnet restore Source/HtmlRenderer.sln
50 |
51 | - name: Build
52 | run: dotnet build Source/HtmlRenderer.sln --configuration Release --no-restore
53 |
54 | - name: Create HtmlRenderer.Core NuGet package
55 | run: dotnet pack Source/HtmlRenderer/HtmlRenderer.csproj --configuration Release --include-symbols -p:SymbolPackageFormat=snupkg --no-build --verbosity normal --output ${{ env.NuGetDirectory }}
56 |
57 | - name: Create HtmlRenderer.PdfSharp NuGet package
58 | run: dotnet pack Source/HtmlRenderer.PdfSharp/HtmlRenderer.PdfSharp.csproj --configuration Release --include-symbols -p:SymbolPackageFormat=snupkg --no-build --verbosity normal --output ${{ env.NuGetDirectory }}
59 |
60 | - name: Create HtmlRenderer.WinForms NuGet package
61 | run: dotnet pack Source/HtmlRenderer.WinForms/HtmlRenderer.WinForms.csproj --configuration Release --include-symbols -p:SymbolPackageFormat=snupkg --no-build --verbosity normal --output ${{ env.NuGetDirectory }}
62 |
63 | - name: Create HtmlRenderer.WPF NuGet package
64 | run: dotnet pack Source/HtmlRenderer.WPF/HtmlRenderer.WPF.csproj --configuration Release --include-symbols -p:SymbolPackageFormat=snupkg --no-build --verbosity normal --output ${{ env.NuGetDirectory }}
65 |
66 | - name: Upload NuGet package artifacts
67 | uses: actions/upload-artifact@v5
68 | with:
69 | name: "HTML Renderer (${{ matrix.platform.name }} ${{ matrix.dotnet.name }})"
70 | path: ${{ env.NuGetDirectory }}/*.*nupkg
71 |
72 | - name: NuGet Login
73 | if: startsWith(github.ref, 'refs/tags/') && matrix.dotnet.name == '.NET 8' && runner.os == 'Windows'
74 | uses: NuGet/login@v1
75 | id: login
76 | with:
77 | user: eXpl0it3r
78 |
79 | - name: NuGet Push
80 | if: startsWith(github.ref, 'refs/tags/') && matrix.dotnet.name == '.NET 8' && runner.os == 'Windows'
81 | run: |
82 | foreach ($file in (Get-ChildItem ${{ env.NuGetDirectory }} -Recurse -Include *.*nupkg)) {
83 | dotnet nuget push $file --api-key ${{steps.login.outputs.NUGET_API_KEY}} --source https://api.nuget.org/v3/index.json
84 | }
85 |
--------------------------------------------------------------------------------
/Source/HtmlRenderer.WinForms/Adapters/FontAdapter.cs:
--------------------------------------------------------------------------------
1 | // "Therefore those skilled at the unorthodox
2 | // are infinite as heaven and earth,
3 | // inexhaustible as the great rivers.
4 | // When they come to an end,
5 | // they begin again,
6 | // like the days and months;
7 | // they die and are reborn,
8 | // like the four seasons."
9 | //
10 | // - Sun Tsu,
11 | // "The Art of War"
12 |
13 | using System;
14 | using System.Drawing;
15 | using TheArtOfDev.HtmlRenderer.Adapters;
16 |
17 | namespace TheArtOfDev.HtmlRenderer.WinForms.Adapters
18 | {
19 | ///
20 | /// Adapter for WinForms Font object for core.
21 | ///
22 | internal sealed class FontAdapter : RFont
23 | {
24 | #region Fields and Consts
25 |
26 | ///
27 | /// the underline win-forms font.
28 | ///
29 | private readonly Font _font;
30 |
31 | ///
32 | /// a handle to this Font.
33 | ///
34 | private IntPtr _hFont;
35 |
36 | ///
37 | /// the vertical offset of the font underline location from the top of the font.
38 | ///
39 | private float _underlineOffset = -1;
40 |
41 | ///
42 | /// Cached font height.
43 | ///
44 | private float _height = -1;
45 |
46 | ///
47 | /// Cached font whitespace width.
48 | ///
49 | private double _whitespaceWidth = -1;
50 |
51 | #endregion
52 |
53 |
54 | ///
55 | /// Init.
56 | ///
57 | public FontAdapter(Font font)
58 | {
59 | _font = font;
60 | }
61 |
62 | ///
63 | /// the underline win-forms font.
64 | ///
65 | public Font Font
66 | {
67 | get { return _font; }
68 | }
69 |
70 | ///
71 | /// Get the handle to this Font.
72 | ///
73 | public IntPtr HFont
74 | {
75 | get
76 | {
77 | if (_hFont == IntPtr.Zero)
78 | _hFont = _font.ToHfont();
79 | return _hFont;
80 | }
81 | }
82 |
83 | public override double Size
84 | {
85 | get { return _font.Size; }
86 | }
87 |
88 | public override double UnderlineOffset
89 | {
90 | get { return _underlineOffset; }
91 | }
92 |
93 | public override double Height
94 | {
95 | get { return _height; }
96 | }
97 |
98 | public override double LeftPadding
99 | {
100 | get { return _height / 6f; }
101 | }
102 |
103 | public override double GetWhitespaceWidth(RGraphics graphics)
104 | {
105 | if (_whitespaceWidth < 0)
106 | {
107 | _whitespaceWidth = graphics.MeasureString(" ", this).Width;
108 | }
109 | return _whitespaceWidth;
110 | }
111 |
112 | ///
113 | /// Set font metrics to be cached for the font for future use.
114 | ///
115 | /// the full height of the font
116 | /// the vertical offset of the font underline location from the top of the font.
117 | internal void SetMetrics(int height, int underlineOffset)
118 | {
119 | _height = height;
120 | _underlineOffset = underlineOffset;
121 | }
122 | }
123 | }
--------------------------------------------------------------------------------
/Source/HtmlRenderer.WPF/Adapters/FontAdapter.cs:
--------------------------------------------------------------------------------
1 | // "Therefore those skilled at the unorthodox
2 | // are infinite as heaven and earth,
3 | // inexhaustible as the great rivers.
4 | // When they come to an end,
5 | // they begin again,
6 | // like the days and months;
7 | // they die and are reborn,
8 | // like the four seasons."
9 | //
10 | // - Sun Tsu,
11 | // "The Art of War"
12 |
13 | using System.Windows.Media;
14 | using TheArtOfDev.HtmlRenderer.Adapters;
15 |
16 | namespace TheArtOfDev.HtmlRenderer.WPF.Adapters
17 | {
18 | ///
19 | /// Adapter for WPF Font.
20 | ///
21 | internal sealed class FontAdapter : RFont
22 | {
23 | #region Fields and Consts
24 |
25 | ///
26 | /// the underline win-forms font.
27 | ///
28 | private readonly Typeface _font;
29 |
30 | ///
31 | /// The glyph font for the font
32 | ///
33 | private readonly GlyphTypeface _glyphTypeface;
34 |
35 | ///
36 | /// the size of the font
37 | ///
38 | private readonly double _size;
39 |
40 | ///
41 | /// the vertical offset of the font underline location from the top of the font.
42 | ///
43 | private readonly double _underlineOffset = -1;
44 |
45 | ///
46 | /// Cached font height.
47 | ///
48 | private readonly double _height = -1;
49 |
50 | ///
51 | /// Cached font whitespace width.
52 | ///
53 | private double _whitespaceWidth = -1;
54 |
55 | #endregion
56 |
57 |
58 | ///
59 | /// Init.
60 | ///
61 | public FontAdapter(Typeface font, double size)
62 | {
63 | _font = font;
64 | _size = size;
65 | _height = 96d / 72d * _size * _font.FontFamily.LineSpacing;
66 | _underlineOffset = 96d / 72d * _size * (_font.FontFamily.LineSpacing + font.UnderlinePosition);
67 |
68 | GlyphTypeface typeface;
69 | if (font.TryGetGlyphTypeface(out typeface))
70 | {
71 | _glyphTypeface = typeface;
72 | }
73 | else
74 | {
75 | foreach (var sysTypeface in Fonts.SystemTypefaces)
76 | {
77 | if (sysTypeface.TryGetGlyphTypeface(out typeface))
78 | break;
79 | }
80 | }
81 | }
82 |
83 | ///
84 | /// the underline win-forms font.
85 | ///
86 | public Typeface Font
87 | {
88 | get { return _font; }
89 | }
90 |
91 | public GlyphTypeface GlyphTypeface
92 | {
93 | get { return _glyphTypeface; }
94 | }
95 |
96 | public override double Size
97 | {
98 | get { return _size; }
99 | }
100 |
101 | public override double UnderlineOffset
102 | {
103 | get { return _underlineOffset; }
104 | }
105 |
106 | public override double Height
107 | {
108 | get { return _height; }
109 | }
110 |
111 | public override double LeftPadding
112 | {
113 | get { return _height / 6f; }
114 | }
115 |
116 | public override double GetWhitespaceWidth(RGraphics graphics)
117 | {
118 | if (_whitespaceWidth < 0)
119 | {
120 | _whitespaceWidth = graphics.MeasureString(" ", this).Width;
121 | }
122 | return _whitespaceWidth;
123 | }
124 | }
125 | }
--------------------------------------------------------------------------------
/Source/HtmlRenderer/Adapters/RControl.cs:
--------------------------------------------------------------------------------
1 | // "Therefore those skilled at the unorthodox
2 | // are infinite as heaven and earth,
3 | // inexhaustible as the great rivers.
4 | // When they come to an end,
5 | // they begin again,
6 | // like the days and months;
7 | // they die and are reborn,
8 | // like the four seasons."
9 | //
10 | // - Sun Tsu,
11 | // "The Art of War"
12 |
13 | using TheArtOfDev.HtmlRenderer.Adapters.Entities;
14 | using TheArtOfDev.HtmlRenderer.Core.Utils;
15 |
16 | namespace TheArtOfDev.HtmlRenderer.Adapters
17 | {
18 | ///
19 | /// Adapter for platform specific control object - used to handle updating the control that the html is rendered on.
20 | /// Not relevant for platforms that don't render HTML on UI element.
21 | ///
22 | public abstract class RControl
23 | {
24 | ///
25 | /// The platform adapter.
26 | ///
27 | private readonly RAdapter _adapter;
28 |
29 | ///
30 | /// Init control with platform adapter.
31 | ///
32 | protected RControl(RAdapter adapter)
33 | {
34 | ArgChecker.AssertArgNotNull(adapter, "adapter");
35 | _adapter = adapter;
36 | }
37 |
38 | ///
39 | /// The platform adapter.
40 | ///
41 | public RAdapter Adapter
42 | {
43 | get { return _adapter; }
44 | }
45 |
46 | ///
47 | /// Is the left mouse button is currently in pressed state
48 | ///
49 | public abstract bool LeftMouseButton { get; }
50 |
51 | ///
52 | /// Is the right mouse button is currently in pressed state
53 | ///
54 | public abstract bool RightMouseButton { get; }
55 |
56 | ///
57 | /// Get the current location of the mouse relative to the control
58 | ///
59 | public abstract RPoint MouseLocation { get; }
60 |
61 | ///
62 | /// Set the cursor over the control to default cursor
63 | ///
64 | public abstract void SetCursorDefault();
65 |
66 | ///
67 | /// Set the cursor over the control to hand cursor
68 | ///
69 | public abstract void SetCursorHand();
70 |
71 | ///
72 | /// Set the cursor over the control to I beam cursor
73 | ///
74 | public abstract void SetCursorIBeam();
75 |
76 | ///
77 | /// Do drag-drop copy operation for the given data object.
78 | ///
79 | /// the drag-drop data object
80 | public abstract void DoDragDropCopy(object dragDropData);
81 |
82 | ///
83 | /// Measure the width of string under max width restriction calculating the number of characters that can fit and the width those characters take.
84 | ///
85 | /// the string to measure
86 | /// the font to measure string with
87 | /// the max width to calculate fit characters
88 | /// the number of characters that will fit under restriction
89 | /// the width that only the characters that fit into max width take
90 | public abstract void MeasureString(string str, RFont font, double maxWidth, out int charFit, out double charFitWidth);
91 |
92 | ///
93 | /// Invalidates the entire surface of the control and causes the control to be redrawn.
94 | ///
95 | public abstract void Invalidate();
96 | }
97 | }
--------------------------------------------------------------------------------