/// <summary> /// Measure the size (width and height) required to draw the given html under given max width restriction.<br/> /// If no max width restriction is given the layout will use the maximum possible width required by the content, /// it can be the longest text line or full image width.<br/> /// Use GDI+ text rending, use <see cref="Graphics.TextRenderingHint"/> to control text rendering. /// </summary> /// <param name="g">Device to use for measure</param> /// <param name="html">HTML source to render</param> /// <param name="maxWidth">optional: bound the width of the html to render in (default - 0, unlimited)</param> /// <param name="cssData">optional: the style to use for html rendering (default - use W3 default style)</param> /// <param name="stylesheetLoad">optional: can be used to overwrite stylesheet resolution logic</param> /// <param name="imageLoad">optional: can be used to overwrite image resolution logic</param> /// <returns>the size required for the html</returns> public static SizeF MeasureGdiPlus(Graphics g, IResourceServer resourceServer, float maxWidth = 0 ) { ArgChecker.AssertArgNotNull(g, "g"); return(Measure(g, resourceServer, maxWidth, true)); }
/// <summary> /// Renders the specified HTML source on the specified location and max size restriction.<br/> /// Use GDI+ text rending, use <see cref="Graphics.TextRenderingHint"/> to control text rendering.<br/> /// If <paramref name="maxWidth"/> is zero the html will use all the required width, otherwise it will perform line /// wrap as specified in the html<br/> /// Returned is the actual width and height of the rendered html.<br/> /// </summary> /// <param name="g">Device to render with</param> /// <param name="html">HTML source to render</param> /// <param name="left">optional: the left most location to start render the html at (default - 0)</param> /// <param name="top">optional: the top most location to start render the html at (default - 0)</param> /// <param name="maxWidth">optional: bound the width of the html to render in (default - 0, unlimited)</param> /// <param name="cssData">optional: the style to use for html rendering (default - use W3 default style)</param> /// <param name="stylesheetLoad">optional: can be used to overwrite stylesheet resolution logic</param> /// <param name="imageLoad">optional: can be used to overwrite image resolution logic</param> /// <returns>the actual size of the rendered html</returns> public static SizeF RenderGdiPlus(Graphics g, IResourceServer resouerceServer, float left = 0, float top = 0, float maxWidth = 0 ) { ArgChecker.AssertArgNotNull(g, "g"); return(RenderClip(g, resouerceServer, new PointF(left, top), new SizeF(maxWidth, 0), true)); }
/// <summary> /// Renders the specified HTML on top of the given image.<br/> /// <paramref name="image"/> will contain the rendered html in it on top of original content.<br/> /// <paramref name="image"/> must not contain transparent pixels as it will corrupt the rendered html text.<br/> /// See "Rendering to image" remarks section on <see cref="HtmlRender"/>.<br/> /// </summary> /// <param name="image">the image to render the html on</param> /// <param name="html">HTML source to render</param> /// <param name="location">the top-left most location to start render the html at</param> /// <param name="maxSize">the max size of the rendered html (if height above zero it will be clipped)</param> /// <param name="cssData">optional: the style to use for html rendering (default - use W3 default style)</param> /// <param name="stylesheetLoad">optional: can be used to overwrite stylesheet resolution logic</param> /// <param name="imageLoad">optional: can be used to overwrite image resolution logic</param> public static void RenderToImage(Image image, IResourceServer resourceServer, PointF location, SizeF maxSize ) { ArgChecker.AssertArgNotNull(image, "image"); if (!string.IsNullOrEmpty(resourceServer.Html)) { // create memory buffer from desktop handle that supports alpha channel IntPtr dib; var memoryHdc = Win32Utils.CreateMemoryHdc(IntPtr.Zero, image.Width, image.Height, out dib); try { // create memory buffer graphics to use for HTML rendering using (var memoryGraphics = Graphics.FromHdc(memoryHdc)) { // draw the image to the memory buffer to be the background of the rendered html memoryGraphics.DrawImageUnscaled(image, 0, 0); // render HTML into the memory buffer RenderHtml(memoryGraphics, resourceServer, location, maxSize, false); } // copy from memory buffer to image CopyBufferToImage(memoryHdc, image); } finally { Win32Utils.ReleaseMemoryHdc(memoryHdc, dib); } } }
public void SetResoureServer(IResourceServer resourceServer) { Clear(); _resourceServer = resourceServer; if (!string.IsNullOrEmpty(resourceServer.Html)) { _loadComplete = false; var baseCssData = resourceServer.CssData; _cssData = baseCssData ?? _adapter.DefaultCssData; DomParser parser = new DomParser(_cssParser); var cssData = new DomParser.CssDataWithChanged { cssData = _cssData }; _root = parser.GenerateCssTree(resourceServer.Html, this, cssData); if (cssData.cssDataChanged) { _cssData = cssData.cssData; } if (_root != null) { _selectionHandler = new SelectionHandler(_root); } } }
/// <summary> /// Renders the specified HTML into a new image of unknown size that will be determined by min/max width/height and HTML layout.<br/> /// If <paramref name="maxSize.Width"/> is zero the html will use all the required width, otherwise it will perform line /// wrap as specified in the html<br/> /// If <paramref name="maxSize.Height"/> is zero the html will use all the required height, otherwise it will clip at the /// given max height not rendering the html below it.<br/> /// If <paramref name="minSize"/> (Width/Height) is above zero the rendered image will not be smaller than the given min size.<br/> /// The generated image have transparent background that the html is rendered on.<br/> /// GDI+ text rending can be controlled by providing <see cref="TextRenderingHint"/>.<br/> /// See "Rendering to image" remarks section on <see cref="HtmlRender"/>.<br/> /// </summary> /// <param name="html">HTML source to render</param> /// <param name="minSize">optional: the min size of the rendered html (zero - not limit the width/height)</param> /// <param name="maxSize">optional: the max size of the rendered html, if not zero and html cannot be layout within the limit it will be clipped (zero - not limit the width/height)</param> /// <param name="textRenderingHint">optional: (default - SingleBitPerPixelGridFit)</param> /// <param name="cssData">optional: the style to use for html rendering (default - use W3 default style)</param> /// <param name="stylesheetLoad">optional: can be used to overwrite stylesheet resolution logic</param> /// <param name="imageLoad">optional: can be used to overwrite image resolution logic</param> /// <returns>the generated image of the html</returns> public static Image RenderToImageGdiPlus(IResourceServer resourceServer, Size minSize, Size maxSize, TextRenderingHint textRenderingHint = TextRenderingHint.AntiAlias, CssData cssData = null ) { if (string.IsNullOrEmpty(resourceServer.Html)) { return(new Bitmap(0, 0, PixelFormat.Format32bppArgb)); } using (var container = new HtmlContainer()) { container.UseGdiPlusTextRendering = true; container.SetResourceServer(resourceServer); var finalSize = MeasureHtmlByRestrictions(container, minSize, maxSize); container.MaxSize = finalSize; // create the final image to render into by measured size var image = new Bitmap(finalSize.Width, finalSize.Height, PixelFormat.Format32bppArgb); // render HTML into the image using (var g = Graphics.FromImage(image)) { g.TextRenderingHint = textRenderingHint; container.PerformPaint(g); } return(image); } }
/// <summary> /// Renders the specified HTML source on the specified location and max size restriction.<br/> /// Use GDI+ text rending, use <see cref="Graphics.TextRenderingHint"/> to control text rendering.<br/> /// If <paramref name="maxSize"/>.Width is zero the html will use all the required width, otherwise it will perform line /// wrap as specified in the html<br/> /// If <paramref name="maxSize"/>.Height is zero the html will use all the required height, otherwise it will clip at the /// given max height not rendering the html below it.<br/> /// Returned is the actual width and height of the rendered html.<br/> /// </summary> /// <param name="g">Device to render with</param> /// <param name="html">HTML source to render</param> /// <param name="location">the top-left most location to start render the html at</param> /// <param name="maxSize">the max size of the rendered html (if height above zero it will be clipped)</param> /// <param name="cssData">optional: the style to use for html rendering (default - use W3 default style)</param> /// <param name="stylesheetLoad">optional: can be used to overwrite stylesheet resolution logic</param> /// <param name="imageLoad">optional: can be used to overwrite image resolution logic</param> /// <returns>the actual size of the rendered html</returns> public static SizeF RenderGdiPlus(Graphics g, IResourceServer resourceServer, PointF location, SizeF maxSize ) { ArgChecker.AssertArgNotNull(g, "g"); return(RenderClip(g, resourceServer, location, maxSize, true)); }
public MtaServer( Action <ServerBuilder> builderAction, Func <uint, INetWrapper, Client>?clientCreationMethod = null ) { this.netWrappers = new(); this.clients = new(); this.clientCreationMethod = clientCreationMethod; this.root = new(); this.serviceCollection = new(); var builder = new ServerBuilder(); builderAction(builder); this.configuration = builder.Configuration; this.Password = this.configuration.Password; this.SetupDependencies(services => builder.LoadDependencies(services)); this.serviceProvider = this.serviceCollection.BuildServiceProvider(); this.packetReducer = new(this.serviceProvider.GetRequiredService <ILogger>()); this.resourceServer = this.serviceProvider.GetRequiredService <IResourceServer>(); this.resourceServer.Start(); this.elementRepository = this.serviceProvider.GetRequiredService <IElementRepository>(); this.elementIdGenerator = this.serviceProvider.GetService <IElementIdGenerator>(); this.root.AssociateWith(this); builder.ApplyTo(this); }
public ServerTestLogic(Server.MtaServer server, IElementRepository elementRepository, RootElement root, IResourceServer resourceServer) { this.server = server; this.elementRepository = elementRepository; this.root = root; this.resourceServer = resourceServer; this.SetupTestLogic(); }
/// <summary> /// Renders the specified HTML on top of the given image.<br/> /// <paramref name="image"/> will contain the rendered html in it on top of original content.<br/> /// <paramref name="image"/> must not contain transparent pixels as it will corrupt the rendered html text.<br/> /// The HTML will be layout by the given image size but may be clipped if cannot fit.<br/> /// See "Rendering to image" remarks section on <see cref="HtmlRender"/>.<br/> /// </summary> /// <param name="image">the image to render the html on</param> /// <param name="html">HTML source to render</param> /// <param name="location">optional: the top-left most location to start render the html at (default - 0,0)</param> /// <param name="cssData">optional: the style to use for html rendering (default - use W3 default style)</param> /// <param name="stylesheetLoad">optional: can be used to overwrite stylesheet resolution logic</param> /// <param name="imageLoad">optional: can be used to overwrite image resolution logic</param> public static void RenderToImage(Image image, IResourceServer resourceServer, PointF location = new PointF() ) { ArgChecker.AssertArgNotNull(image, "image"); var maxSize = new SizeF(image.Size.Width - location.X, image.Size.Height - location.Y); RenderToImage(image, resourceServer, location, maxSize); }
/// <summary> /// Create PDF pages from given HTML and appends them to the provided PDF document.<br/> /// </summary> /// <param name="document">PDF document to append pages to</param> /// <param name="html">HTML source to create PDF from</param> /// <param name="pageSize">the page size to use for each page in the generated pdf </param> /// <param name="margin">the margin to use between the HTML and the edges of each page</param> /// <param name="cssData">optional: the style to use for html rendering (default - use W3 default style)</param> /// <param name="stylesheetLoad">optional: can be used to overwrite stylesheet resolution logic</param> /// <param name="imageLoad">optional: can be used to overwrite image resolution logic</param> /// <returns>the generated image of the html</returns> public static Task AddPdfPages(PdfDocument document, IResourceServer resourceServer, PageSize pageSize, int margin = 20 ) { var config = new PdfGenerateConfig(); config.PageSize = pageSize; config.SetMargins(margin); return(AddPdfPages(document, resourceServer, config)); }
/// <summary> /// Create PDF document from given HTML.<br/> /// </summary> /// <param name="html">HTML source to create PDF from</param> /// <param name="pageSize">the page size to use for each page in the generated pdf </param> /// <param name="margin">the margin to use between the HTML and the edges of each page</param> /// <param name="cssData">optional: the style to use for html rendering (default - use W3 default style)</param> /// <param name="stylesheetLoad">optional: can be used to overwrite stylesheet resolution logic</param> /// <param name="imageLoad">optional: can be used to overwrite image resolution logic</param> /// <returns>the generated image of the html</returns> public static Task <PdfDocument> GeneratePdf(IResourceServer resourceServer, PageSize pageSize, int margin = 20 ) { var config = new PdfGenerateConfig(); config.PageSize = pageSize; config.SetMargins(margin); return(GeneratePdf(resourceServer, config)); }
static UPnPError OnGetResourceServerBaseURL(DvAction action, IList <object> inParams, out IList <object> outParams, CallContext context) { IResourceServer resourceServer = ServiceRegistration.Get <IResourceServer>(); string baseURL = resourceServer.GetServiceUrl(context.Endpoint.EndPointIPAddress) + ResourceHttpAccessUrlUtils.RESOURCE_ACCESS_PATH; outParams = new List <object> { baseURL }; return(null); }
/// <summary> /// Create PDF document from given HTML.<br/> /// </summary> /// <param name="html">HTML source to create PDF from</param> /// <param name="config">the configuration to use for the PDF generation (page size/page orientation/margins/etc.)</param> /// <param name="cssData">optional: the style to use for html rendering (default - use W3 default style)</param> /// <param name="stylesheetLoad">optional: can be used to overwrite stylesheet resolution logic</param> /// <param name="imageLoad">optional: can be used to overwrite image resolution logic</param> /// <returns>the generated image of the html</returns> public static async Task <PdfDocument> GeneratePdf(IResourceServer resourceServer, PdfGenerateConfig config ) { // create PDF document to render the HTML into var document = new PdfDocument(); // add rendered PDF pages to document AddPdfPages(document, resourceServer, config); return(document); }
static UPnPError OnGetResourceServerBaseURL(DvAction action, IList <object> inParams, out IList <object> outParams, CallContext context) { IResourceServer resourceServer = ServiceRegistration.Get <IResourceServer>(); string baseURL = "http://" + NetworkHelper.IPEndPointToString(context.Endpoint.EndPointIPAddress, resourceServer.GetPortForIP(context.Endpoint.EndPointIPAddress)) + ResourceHttpAccessUrlUtils.RESOURCE_ACCESS_PATH; outParams = new List <object> { baseURL }; return(null); }
/// <summary> /// Renders the specified HTML into a new image of the requested size.<br/> /// The HTML will be layout by the given size but will be clipped if cannot fit.<br/> /// The generated image have transparent background that the html is rendered on.<br/> /// GDI+ text rending can be controlled by providing <see cref="TextRenderingHint"/>.<br/> /// See "Rendering to image" remarks section on <see cref="HtmlRender"/>.<br/> /// </summary> /// <param name="html">HTML source to render</param> /// <param name="size">The size of the image to render into, layout html by width and clipped by height</param> /// <param name="textRenderingHint">optional: (default - SingleBitPerPixelGridFit)</param> /// <param name="cssData">optional: the style to use for html rendering (default - use W3 default style)</param> /// <param name="stylesheetLoad">optional: can be used to overwrite stylesheet resolution logic</param> /// <param name="imageLoad">optional: can be used to overwrite image resolution logic</param> /// <returns>the generated image of the html</returns> public static Image RenderToImageGdiPlus(IResourceServer resourceServer, Size size, TextRenderingHint textRenderingHint = TextRenderingHint.AntiAlias) { var image = new Bitmap(size.Width, size.Height, PixelFormat.Format32bppArgb); using (var g = Graphics.FromImage(image)) { g.TextRenderingHint = textRenderingHint; RenderHtml(g, resourceServer, PointF.Empty, size, true); } return(image); }
public void Activated(PluginRuntime pluginRuntime) { var meta = pluginRuntime.Metadata; Logger.Info(string.Format("{0} v{1} [{2}] by {3}", meta.Name, meta.PluginVersion, meta.Description, meta.Author)); IResourceServer server = ServiceRegistration.Get <IResourceServer>(); if (server != null) { ServiceRegistration.Set <IFanArtService>(new FanArtService()); _fanartModule = new FanartAccessModule(); server.AddHttpModule(_fanartModule); } }
/// <summary> /// Renders the specified HTML into a new image of unknown size that will be determined by min/max width/height and HTML layout.<br/> /// If <paramref name="maxSize.Width"/> is zero the html will use all the required width, otherwise it will perform line /// wrap as specified in the html<br/> /// If <paramref name="maxSize.Height"/> is zero the html will use all the required height, otherwise it will clip at the /// given max height not rendering the html below it.<br/> /// If <paramref name="minSize"/> (Width/Height) is above zero the rendered image will not be smaller than the given min size.<br/> /// <p> /// Limitation: The image cannot have transparent background, by default it will be white.<br/> /// See "Rendering to image" remarks section on <see cref="HtmlRender"/>.<br/> /// </p> /// </summary> /// <param name="html">HTML source to render</param> /// <param name="minSize">optional: the min size of the rendered html (zero - not limit the width/height)</param> /// <param name="maxSize">optional: the max size of the rendered html, if not zero and html cannot be layout within the limit it will be clipped (zero - not limit the width/height)</param> /// <param name="backgroundColor">optional: the color to fill the image with (default - white)</param> /// <param name="cssData">optional: the style to use for html rendering (default - use W3 default style)</param> /// <param name="stylesheetLoad">optional: can be used to overwrite stylesheet resolution logic</param> /// <param name="imageLoad">optional: can be used to overwrite image resolution logic</param> /// <returns>the generated image of the html</returns> /// <exception cref="ArgumentOutOfRangeException">if <paramref name="backgroundColor"/> is <see cref="Color.Transparent"/></exception>. public static Image RenderToImage(IResourceServer resourceServer, Size minSize, Size maxSize, Color backgroundColor = new Color(), CssData cssData = null ) { if (backgroundColor == Color.Transparent) { throw new ArgumentOutOfRangeException("backgroundColor", "Transparent background in not supported"); } if (string.IsNullOrEmpty(resourceServer.Html)) { return(new Bitmap(0, 0, PixelFormat.Format32bppArgb)); } using (var container = new HtmlContainer()) { container.SetResourceServer(resourceServer); var finalSize = MeasureHtmlByRestrictions(container, minSize, maxSize); container.MaxSize = finalSize; // create the final image to render into by measured size var image = new Bitmap(finalSize.Width, finalSize.Height, PixelFormat.Format32bppArgb); // create memory buffer from desktop handle that supports alpha channel IntPtr dib; var memoryHdc = Win32Utils.CreateMemoryHdc(IntPtr.Zero, image.Width, image.Height, out dib); try { // render HTML into the memory buffer using (var memoryGraphics = Graphics.FromHdc(memoryHdc)) { memoryGraphics.Clear(backgroundColor != Color.Empty ? backgroundColor : Color.White); container.PerformPaint(memoryGraphics); } // copy from memory buffer to image CopyBufferToImage(memoryHdc, image); } finally { Win32Utils.ReleaseMemoryHdc(memoryHdc, dib); } return(image); } }
/// <summary> /// Renders the specified HTML source on the specified location and max size restriction.<br/> /// If <paramref name="maxSize"/>.Width is zero the html will use all the required width, otherwise it will perform line /// wrap as specified in the html<br/> /// If <paramref name="maxSize"/>.Height is zero the html will use all the required height, otherwise it will clip at the /// given max height not rendering the html below it.<br/> /// Clip the graphics so the html will not be rendered outside the max height bound given.<br/> /// Returned is the actual width and height of the rendered html.<br/> /// </summary> /// <param name="g">Device to render with</param> /// <param name="html">HTML source to render</param> /// <param name="location">the top-left most location to start render the html at</param> /// <param name="maxSize">the max size of the rendered html (if height above zero it will be clipped)</param> /// <param name="cssData">optional: the style to use for html rendering (default - use W3 default style)</param> /// <param name="useGdiPlusTextRendering">true - use GDI+ text rendering, false - use GDI text rendering</param> /// <param name="stylesheetLoad">optional: can be used to overwrite stylesheet resolution logic</param> /// <param name="imageLoad">optional: can be used to overwrite image resolution logic</param> /// <returns>the actual size of the rendered html</returns> private static SizeF RenderClip(Graphics g, IResourceServer resourceServer, PointF location, SizeF maxSize, bool useGdiPlusTextRendering ) { Region prevClip = null; if (maxSize.Height > 0) { prevClip = g.Clip; g.SetClip(new RectangleF(location, maxSize)); } var actualSize = RenderHtml(g, resourceServer, location, maxSize, useGdiPlusTextRendering); if (prevClip != null) { g.SetClip(prevClip, CombineMode.Replace); } return(actualSize); }
/// <summary> /// Measure the size (width and height) required to draw the given html under given width and height restrictions.<br/> /// </summary> /// <param name="g">Device to use for measure</param> /// <param name="html">HTML source to render</param> /// <param name="maxWidth">optional: bound the width of the html to render in (default - 0, unlimited)</param> /// <param name="cssData">optional: the style to use for html rendering (default - use W3 default style)</param> /// <param name="useGdiPlusTextRendering">true - use GDI+ text rendering, false - use GDI text rendering</param> /// <param name="stylesheetLoad">optional: can be used to overwrite stylesheet resolution logic</param> /// <param name="imageLoad">optional: can be used to overwrite image resolution logic</param> /// <returns>the size required for the html</returns> private static SizeF Measure(Graphics g, IResourceServer resourceServer, float maxWidth, bool useGdiPlusTextRendering ) { SizeF actualSize = SizeF.Empty; if (!string.IsNullOrEmpty(resourceServer.Html)) { using (var container = new HtmlContainer()) { container.MaxSize = new SizeF(maxWidth, 0); container.UseGdiPlusTextRendering = useGdiPlusTextRendering; container.SetResourceServer(resourceServer); container.PerformLayout(g); actualSize = container.ActualSize; } } return(actualSize); }
/// <summary> /// Clear the content of the HTML container releasing any resources used to render previously existing content. /// </summary> public void Clear() { if (_root != null) { _root.Dispose(); _root = null; if (_selectionHandler != null) { _selectionHandler.Dispose(); } _selectionHandler = null; if (_resourceServer != null) { _resourceServer.Dispose(); } _resourceServer = null; _hoverBoxes = null; } }
/// <summary> /// Renders the specified HTML into a new image of the requested size.<br/> /// The HTML will be layout by the given size but will be clipped if cannot fit.<br/> /// <p> /// Limitation: The image cannot have transparent background, by default it will be white.<br/> /// See "Rendering to image" remarks section on <see cref="HtmlRender"/>.<br/> /// </p> /// </summary> /// <param name="html">HTML source to render</param> /// <param name="size">The size of the image to render into, layout html by width and clipped by height</param> /// <param name="backgroundColor">optional: the color to fill the image with (default - white)</param> /// <param name="cssData">optional: the style to use for html rendering (default - use W3 default style)</param> /// <param name="stylesheetLoad">optional: can be used to overwrite stylesheet resolution logic</param> /// <param name="imageLoad">optional: can be used to overwrite image resolution logic</param> /// <returns>the generated image of the html</returns> /// <exception cref="ArgumentOutOfRangeException">if <paramref name="backgroundColor"/> is <see cref="Color.Transparent"/></exception>. public static Image RenderToImage(IResourceServer resourceServer, Size size, Color backgroundColor = new Color() ) { if (backgroundColor == Color.Transparent) { throw new ArgumentOutOfRangeException("backgroundColor", "Transparent background in not supported"); } // create the final image to render into var image = new Bitmap(size.Width, size.Height, PixelFormat.Format32bppArgb); if (!string.IsNullOrEmpty(resourceServer.Html)) { // create memory buffer from desktop handle that supports alpha channel IntPtr dib; var memoryHdc = Win32Utils.CreateMemoryHdc(IntPtr.Zero, image.Width, image.Height, out dib); try { // create memory buffer graphics to use for HTML rendering using (var memoryGraphics = Graphics.FromHdc(memoryHdc)) { memoryGraphics.Clear(backgroundColor != Color.Empty ? backgroundColor : Color.White); // render HTML into the memory buffer RenderHtml(memoryGraphics, resourceServer, PointF.Empty, size, true); } // copy from memory buffer to image CopyBufferToImage(memoryHdc, image); } finally { Win32Utils.ReleaseMemoryHdc(memoryHdc, dib); } } return(image); }
/// <summary> /// Renders the specified HTML source on the specified location and max size restriction.<br/> /// If <paramref name="maxSize"/>.Width is zero the html will use all the required width, otherwise it will perform line /// wrap as specified in the html<br/> /// If <paramref name="maxSize"/>.Height is zero the html will use all the required height, otherwise it will clip at the /// given max height not rendering the html below it.<br/> /// Returned is the actual width and height of the rendered html.<br/> /// </summary> /// <param name="g">Device to render with</param> /// <param name="html">HTML source to render</param> /// <param name="location">the top-left most location to start render the html at</param> /// <param name="maxSize">the max size of the rendered html (if height above zero it will be clipped)</param> /// <param name="cssData">optional: the style to use for html rendering (default - use W3 default style)</param> /// <param name="useGdiPlusTextRendering">true - use GDI+ text rendering, false - use GDI text rendering</param> /// <param name="stylesheetLoad">optional: can be used to overwrite stylesheet resolution logic</param> /// <param name="imageLoad">optional: can be used to overwrite image resolution logic</param> /// <returns>the actual size of the rendered html</returns> private static SizeF RenderHtml(Graphics g, IResourceServer resourceServer, PointF location, SizeF maxSize, bool useGdiPlusTextRendering ) { SizeF actualSize = SizeF.Empty; if (!string.IsNullOrEmpty(resourceServer.Html)) { using (var container = new HtmlContainer()) { container.Location = location; container.MaxSize = maxSize; container.UseGdiPlusTextRendering = useGdiPlusTextRendering; container.SetResourceServer(resourceServer); container.PerformLayout(g); container.PerformPaint(g); actualSize = container.ActualSize; } } return(actualSize); }
public static async Task <Metafile> RenderToMetafile(IResourceServer resourceServer, float left = 0, float top = 0, float maxWidth = 0 ) { Metafile image; IntPtr dib; var memoryHdc = Win32Utils.CreateMemoryHdc(IntPtr.Zero, 1, 1, out dib); try { image = new Metafile(memoryHdc, EmfType.EmfPlusDual, ".."); using (var g = Graphics.FromImage(image)) { Render(g, resourceServer, left, top, maxWidth); } } finally { Win32Utils.ReleaseMemoryHdc(memoryHdc, dib); } return(image); }
public MtaServer( Configuration?configuration = null, Action <ServiceCollection>?dependencyCallback = null, Func <uint, INetWrapper, Client>?clientCreationMethod = null ) { this.netWrappers = new(); this.clients = new(); this.clientCreationMethod = clientCreationMethod; this.configuration = configuration ?? new(); this.Password = configuration?.Password; this.root = new(); this.serviceCollection = new(); var validationResults = new List <ValidationResult>(); if (!Validator.TryValidateObject(this.configuration, new ValidationContext(this.configuration), validationResults, true)) { string invalidProperties = string.Join("\r\n\t", validationResults.Select(r => r.ErrorMessage)); throw new Exception($"An error has occurred while parsing configuration parameters:\r\n {invalidProperties}"); } this.SetupDependencies(dependencyCallback); this.serviceProvider = this.serviceCollection.BuildServiceProvider(); this.resourceServer = this.serviceProvider.GetRequiredService <IResourceServer>(); this.resourceServer.Start(); this.elementRepository = this.serviceProvider.GetRequiredService <IElementRepository>(); this.elementIdGenerator = this.serviceProvider.GetService <IElementIdGenerator>(); this.root.AssociateWith(this); this.packetReducer = new(this.serviceProvider.GetRequiredService <ILogger>()); }
/// <summary> /// 检查会议室等资源是否被占用 /// </summary> /// <param name="dailyWork">会议信息</param> /// <param name="error">错误时输出错误信息</param> /// <returns>检查是否通过的标志</returns> private bool CheckMeetingResource(PRJ_DailyWork dailyWork, out string error) { error = null; TaskManagementDataContext ctx = CommentParameter.TaskManagementDataContext; var resourceID = (from r in ctx.PRJ_ResourceUsageList where r.TaskID == dailyWork.ID select r.ResourceID).ToList(); StringBuilder sb = new StringBuilder(); sb.AppendLine("下列会议资源已经被占用,当前会议无法使用:"); int i = 0; IResourceServer resourceServer = TaskObjectFactory.GetOperator <IResourceServer>(); List <View_PRJ_Resource> resources = resourceServer.GetMeetingResource(dailyWork.BeginTime, dailyWork.EndTime); foreach (var item in resourceID) { if (!resourceServer.IsIdle(item, dailyWork.BeginTime, dailyWork.EndTime)) { sb.AppendLine(string.Format("{0}. {1};", ++i, resources.Find(p => p.资源编号 == item).资源名称)); } } if (i > 0) { error = sb.ToString(); return(false); } else { return(true); } }
public MtaServer( string directory, string netDllPath, Configuration?configuration = null, Action <ServiceCollection>?dependencyCallback = null ) { this.configuration = configuration ?? new Configuration(); var validationResults = new List <ValidationResult>(); if (!Validator.TryValidateObject(this.configuration, new ValidationContext(this.configuration), validationResults, true)) { string invalidProperties = string.Join("\r\n\t", validationResults.Select(r => r.ErrorMessage)); throw new Exception($"An error has occurred while parsing configuration parameters:\r\n {invalidProperties}"); } this.root = new RootElement(); this.serviceCollection = new ServiceCollection(); this.SetupDependencies(dependencyCallback); this.serviceProvider = this.serviceCollection.BuildServiceProvider(); this.resourceServer = this.serviceProvider.GetRequiredService <IResourceServer>(); this.resourceServer.Start(); this.elementRepository = this.serviceProvider.GetRequiredService <IElementRepository>(); this.elementIdGenerator = this.serviceProvider.GetService <IElementIdGenerator>(); this.elementRepository.Add(this.root); this.packetReducer = new PacketReducer(); this.clients = new Dictionary <NetWrapper, Dictionary <uint, Client> >(); this.netWrapper = CreateNetWrapper(directory, netDllPath, this.configuration.Host, this.configuration.Port); }
/// <summary> /// Create PDF pages from given HTML and appends them to the provided PDF document.<br/> /// </summary> /// <param name="document">PDF document to append pages to</param> /// <param name="html">HTML source to create PDF from</param> /// <param name="config">the configuration to use for the PDF generation (page size/page orientation/margins/etc.)</param> /// <param name="cssData">optional: the style to use for html rendering (default - use W3 default style)</param> /// <param name="stylesheetLoad">optional: can be used to overwrite stylesheet resolution logic</param> /// <param name="imageLoad">optional: can be used to overwrite image resolution logic</param> /// <returns>the generated image of the html</returns> public static async Task AddPdfPages(PdfDocument document, IResourceServer resourceServer, PdfGenerateConfig config ) { XSize orgPageSize; // get the size of each page to layout the HTML in if (config.PageSize != PageSize.Undefined) { orgPageSize = PageSizeConverter.ToSize(config.PageSize); } else { orgPageSize = config.ManualPageSize; } if (config.PageOrientation == PageOrientation.Landscape) { // invert pagesize for landscape orgPageSize = new XSize(orgPageSize.Height, orgPageSize.Width); } var pageSize = new XSize(orgPageSize.Width - config.MarginLeft - config.MarginRight, orgPageSize.Height - config.MarginTop - config.MarginBottom); var html = await resourceServer.GetHtmlAsync(); if (!string.IsNullOrEmpty(html)) { using (var container = new HtmlContainer()) { container.Location = new XPoint(config.MarginLeft, config.MarginTop); container.MaxSize = new XSize(pageSize.Width, 0); await container.SetResourceServerAsync(resourceServer); container.PageSize = pageSize; container.MarginBottom = config.MarginBottom; container.MarginLeft = config.MarginLeft; container.MarginRight = config.MarginRight; container.MarginTop = config.MarginTop; // layout the HTML with the page width restriction to know how many pages are required using (var measure = XGraphics.CreateMeasureContext(pageSize, XGraphicsUnit.Point, XPageDirection.Downwards)) { container.PerformLayout(measure); } // while there is un-rendered HTML, create another PDF page and render with proper offset for the next page double scrollOffset = 0; while (scrollOffset > -container.ActualSize.Height) { var page = document.AddPage(); page.Height = orgPageSize.Height; page.Width = orgPageSize.Width; using (var g = XGraphics.FromPdfPage(page)) { //g.IntersectClip(new XRect(config.MarginLeft, config.MarginTop, pageSize.Width, pageSize.Height)); g.IntersectClip(new XRect(0, 0, page.Width, page.Height)); container.ScrollOffset = new XPoint(0, scrollOffset); container.PerformPaint(g); } scrollOffset -= pageSize.Height; } // add web links and anchors HandleLinks(document, container, orgPageSize, pageSize); } } }
/// <summary> /// Renders the specified HTML into a new image of unknown size that will be determined by max width/height and HTML layout.<br/> /// If <paramref name="maxWidth"/> is zero the html will use all the required width, otherwise it will perform line /// wrap as specified in the html<br/> /// If <paramref name="maxHeight"/> is zero the html will use all the required height, otherwise it will clip at the /// given max height not rendering the html below it.<br/> /// The generated image have transparent background that the html is rendered on.<br/> /// GDI+ text rending can be controlled by providing <see cref="TextRenderingHint"/>.<br/> /// See "Rendering to image" remarks section on <see cref="HtmlRender"/>.<br/> /// </summary> /// <param name="html">HTML source to render</param> /// <param name="maxWidth">optional: the max width of the rendered html, if not zero and html cannot be layout within the limit it will be clipped</param> /// <param name="maxHeight">optional: the max height of the rendered html, if not zero and html cannot be layout within the limit it will be clipped</param> /// <param name="textRenderingHint">optional: (default - SingleBitPerPixelGridFit)</param> /// <param name="cssData">optional: the style to use for html rendering (default - use W3 default style)</param> /// <param name="stylesheetLoad">optional: can be used to overwrite stylesheet resolution logic</param> /// <param name="imageLoad">optional: can be used to overwrite image resolution logic</param> /// <returns>the generated image of the html</returns> public static Image RenderToImageGdiPlus(IResourceServer resourceServer, int maxWidth = 0, int maxHeight = 0, TextRenderingHint textRenderingHint = TextRenderingHint.AntiAlias, CssData cssData = null ) { return(RenderToImageGdiPlus(resourceServer, Size.Empty, new Size(maxWidth, maxHeight), textRenderingHint, cssData)); }
/// <summary> /// Renders the specified HTML into a new image of unknown size that will be determined by max width/height and HTML layout.<br/> /// If <paramref name="maxWidth"/> is zero the html will use all the required width, otherwise it will perform line /// wrap as specified in the html<br/> /// If <paramref name="maxHeight"/> is zero the html will use all the required height, otherwise it will clip at the /// given max height not rendering the html below it.<br/> /// <p> /// Limitation: The image cannot have transparent background, by default it will be white.<br/> /// See "Rendering to image" remarks section on <see cref="HtmlRender"/>.<br/> /// </p> /// </summary> /// <param name="html">HTML source to render</param> /// <param name="maxWidth">optional: the max width of the rendered html, if not zero and html cannot be layout within the limit it will be clipped</param> /// <param name="maxHeight">optional: the max height of the rendered html, if not zero and html cannot be layout within the limit it will be clipped</param> /// <param name="backgroundColor">optional: the color to fill the image with (default - white)</param> /// <param name="cssData">optional: the style to use for html rendering (default - use W3 default style)</param> /// <param name="stylesheetLoad">optional: can be used to overwrite stylesheet resolution logic</param> /// <param name="imageLoad">optional: can be used to overwrite image resolution logic</param> /// <returns>the generated image of the html</returns> /// <exception cref="ArgumentOutOfRangeException">if <paramref name="backgroundColor"/> is <see cref="Color.Transparent"/></exception>. public static Image RenderToImage(IResourceServer resourceServer, int maxWidth = 0, int maxHeight = 0, Color backgroundColor = new Color() ) { return(RenderToImage(resourceServer, Size.Empty, new Size(maxWidth, maxHeight), backgroundColor)); }
public void AddResourceServer(IResourceServer resourceServer) { this.resourceServers.Add(resourceServer); resourceServer.Start(); }