Table of Contents

Class DefaultGraphicEngine

Namespace
SlapKit.Excel.Graphics
Assembly
SlapKit.Excel.dll
public class DefaultGraphicEngine : IXLGraphicEngine
Inheritance
DefaultGraphicEngine
Implements
Inherited Members
Extension Methods

Constructors

DefaultGraphicEngine(string)

Initialize a new instance of the engine.

public DefaultGraphicEngine(string fallbackFont)

Parameters

fallbackFont string

A name of a font that is used when a font in a workbook is not available.

Properties

Instance

Get a singleton instance of the engine that uses Microsoft Sans Serif as a fallback font.

public static Lazy<DefaultGraphicEngine> Instance { get; }

Property Value

Lazy<DefaultGraphicEngine>

Methods

CreateOnlyWithFonts(Stream, params Stream[])

Create a default graphic engine that uses only fallback font and additional fonts passed as streams. It ignores all system fonts and that can lead to decrease of initialization time.

public static IXLGraphicEngine CreateOnlyWithFonts(Stream fallbackFontStream, params Stream[] fontStreams)

Parameters

fallbackFontStream Stream

A stream that contains a fallback font.

fontStreams Stream[]

Fonts that should be loaded to the engine.

Returns

IXLGraphicEngine

Remarks

Font is determined by a name and style in the worksheet, but the font name must be mapped to a font file/stream. System fonts on Windows contain hundreds of font files that have to be checked to find the correct font file for the font name and style. That means to read hundreds of files and parse data inside them. Even though SixLabors.Fonts does this only once (lazily too) and stores data in a static variable, it is an overhead that can be avoided.

This factory method is useful in several scenarios:

  • Client side Blazor doesn't have access to any system fonts.
  • Worksheet contains only limited number of fonts. It might be sufficient to just load few fonts we are

CreateWithFontsAndSystemFonts(Stream, params Stream[])

Create a default graphic engine that uses only fallback font and additional fonts passed as streams. It also uses system fonts.

public static IXLGraphicEngine CreateWithFontsAndSystemFonts(Stream fallbackFontStream, params Stream[] fontStreams)

Parameters

fallbackFontStream Stream

A stream that contains a fallback font.

fontStreams Stream[]

Fonts that should be loaded to the engine.

Returns

IXLGraphicEngine

GetDescent(IXLFontBase, double)

Get font descent in pixels (positive value).

public double GetDescent(IXLFontBase font, double dpiY)

Parameters

font IXLFontBase
dpiY double

Returns

double

Remarks

Excel is using OS/2 WinAscent/WinDescent for TrueType fonts (e.g. Calibri), not a correct font ascent/descent.

GetGlyphBox(ReadOnlySpan<int>, IXLFontBase, Dpi)

Get a glyph bounding box for a grapheme cluster.

public GlyphBox GetGlyphBox(ReadOnlySpan<int> graphemeCluster, IXLFontBase font, Dpi dpi)

Parameters

graphemeCluster ReadOnlySpan<int>

A part of a string in code points (or runes in C# terminology, not UTF-16 code units) that together form a grapheme. Multiple unicode codepoints can form a single glyph, e.g. family grapheme is a single glyph created from 6 codepoints (man, zero-width-join, woman, zero-width-join and a girl). A string can be split into a grapheme clusters through GetTextElementEnumerator(string).

font IXLFontBase

Font used to determine size of a glyph for the grapheme cluster.

dpi Dpi

A resolution used to determine pixel size of a glyph. Font might be rendered differently at different resolutions.

Returns

GlyphBox

Bounding box containing the glyph.

Remarks

In 99+%, grapheme cluster will be just a codepoint. Method uses grapheme instead, so it can be future-proof signature and have less braking changes. Implementing method by adding widths of individual code points is acceptable.

GetMaxDigitWidth(IXLFontBase, double)

The width of the widest 0-9 digit in pixels.

public double GetMaxDigitWidth(IXLFontBase fontBase, double dpiX)

Parameters

fontBase IXLFontBase
dpiX double

Returns

double

Remarks

OOXML measures width of a column in multiples of widest 0-9 digit character in a normal style font.

GetPictureInfo(Stream, XLPictureFormat)

Get the info about a picture (mostly dimensions).

public XLPictureInfo GetPictureInfo(Stream stream, XLPictureFormat expectedFormat)

Parameters

stream Stream
expectedFormat XLPictureFormat

The expected format of the image. Use Unknown for auto detection.

Returns

XLPictureInfo

Exceptions

ArgumentException

Unable to determine picture dimensions or format doesn't match the stream.

GetTextHeight(IXLFontBase, double)

Get the height of a text with the font in pixels. Should be EMHeight+descent.

public double GetTextHeight(IXLFontBase font, double dpiY)

Parameters

font IXLFontBase
dpiY double

Returns

double

GetTextWidth(string, IXLFontBase, double)

Get the width of a text in pixels. Do not add any padding, there can be multiple spans of a texts with different fonts in a line.

public double GetTextWidth(string text, IXLFontBase fontBase, double dpiX)

Parameters

text string
fontBase IXLFontBase
dpiX double

Returns

double