PeachDrawing.Win2D

Win2dRenderContext Class

The RenderContext for Win2dCanvas: where fonts, images and colours come from. It also owns, or borrows, the Microsoft.Graphics.Canvas.CanvasDevice that offscreen drawing and DirectWrite text need.

public class Win2dRenderContext : PeachDrawing.Core.RenderContext, System.IDisposable

Inheritance System.Object → RenderContext → Win2dRenderContext

Implements System.IDisposable

Remarks

The device. A device passed to the constructor is borrowed: the context never disposes it. With none given, the context makes its own the first time something needs one (Device) and disposes it in Dispose(). A Win2dCanvas over a caller’s drawing session never needs the context’s device at all, because it creates everything on the session’s own.

Lifetime. Dispose the context after every canvas and composer made from it. Disposing it releases the device it made, the font sets it loaded and the temporary font files it wrote; fonts and images it handed out stay valid objects, but a font built for DirectWrite can no longer measure text. It is not thread-safe to draw from two threads at once; the context’s own caches are.

Constructors  
Win2dRenderContext(CanvasDevice, Win2dTextEngine) Makes a context that draws with its own device, or device when one is given.
Properties  
Device The device offscreen drawing and DirectWrite text use: the one passed to the constructor, or the one this context made on first use.
LayoutUnitsPerPoint The factor between the layout units font sizes are requested in and the points the font behind them is built at (points = size / LayoutUnitsPerPoint). It is part of a cached font’s identity: the same requested size under a different scale is a different physical font, so the font handler keys every font cache by it. 1 for an adapter whose layout unit is the point.
PixelsPerPoint Layout units per point; see LayoutUnitsPerPoint. 1 means a layout unit is a point.
TextEngine Which text engine this context’s fonts use.
Methods  
AddFont(Stream, string) Adds a font from stream under fontFamilyName (or the family the file names).
AddFont(Stream, string, FontFaceDescriptors, IReadOnlyList<RuneInterval>) Adds a font from stream with the weight, width and style it is declared for.
CreateSoftware(Win2dTextEngine) Makes a context that draws with a software (WARP) device it owns: slower than the GPU but available without one, and the same on every machine, which is what a server, a test or a reproducible export wants.
Dispose() Releases the device this context made, the font sets it loaded and the font files it wrote. Safe to call more than once.
GetCssMediaType(IEnumerable<string>) The CSS media type (e.g. "print", "screen") this render context evaluates media queries against, chosen from mediaTypesAvailable.
Events  
DeviceLost Raised when the device this context draws with is lost (a driver reset, a removed adapter). Anything drawn must be drawn again.