Table of Contents

Struct CssPage

Namespace
Egami.Css
Assembly
Egami.Css.dll

A page being laid out, as page selectors see it: its page type (the page property of the content that starts it) and whether it is the first, a left or a blank page.

public readonly record struct CssPage : IEquatable<CssPage>
Implements
Inherited Members

Examples

CssStyleSheet sheet = CssStyleSheet.Parse("""
    @page { size: A4; margin: 2cm; font: 9pt serif;
      @bottom-center { content: counter(page) } }
    @page :first { margin-top: 5cm; @bottom-center { content: none } }
    @page :left { margin-left: 3cm }
    @page appendix { size: A4 landscape }
    h1.appendix { page: appendix; break-before: right }
    """);
CssPageRule first = (CssPageRule)sheet.Rules[1];
CssPageSelector selector = first.Selectors[0];               // :first, specificity (0, 1, 0)
CssPageMarginRule footer = ((CssPageRule)sheet.Rules[0]).MarginRules[0]; // CssMarginBox.BottomCenter

// Cascade the page rules for each page the layout makes, then compute the page context from the root element's
// computed style and each margin box from the page context's.
var cascade = new CssCascade([(sheet, CssOrigin.Author)]);
CssPageStyle pageStyle = cascade.CascadePage(new CssPage(Name: null, IsFirst: false, IsLeft: true));
CssComputedStyle root = cascade.Compute(new CssCascadedStyle(), null);
CssComputedStyle page = cascade.Compute(pageStyle.Page, root);
string? marginLeft = page.GetValueText("margin-left");                // "3cm"
bool isSize = CssPageSize.TryParse(page.GetValueText("size")!, out CssPageSize a4); // 210mm by 297mm
CssComputedStyle box = cascade.Compute(pageStyle.MarginBoxes[CssMarginBox.BottomCenter], page);
string? content = box.GetValueText("content");                        // "counter(page)"
string? fontSize = box.GetValueText("font-size");                     // "9pt": inherited from the page context

// A named page: the appendix pages turn A4 to landscape.
CssPageStyle appendix = cascade.CascadePage(new CssPage("appendix", IsFirst: false, IsLeft: false));
bool isTurned = CssPageSize.TryParse(appendix.Page.GetValueText("size")!, out CssPageSize turned);
bool landscape = turned.Orientation == CssPageOrientation.Landscape;  // true: 297mm by 210mm

Constructors

CssPage(string?, bool, bool, bool)

A page being laid out, as page selectors see it: its page type (the page property of the content that starts it) and whether it is the first, a left or a blank page.

public CssPage(string? Name, bool IsFirst, bool IsLeft, bool IsBlank = false)

Parameters

Name string

The page type name, or null for an unnamed page (page: auto throughout).

IsFirst bool

Whether this is the document's first page.

IsLeft bool

Whether this is a left page; otherwise a right page.

IsBlank bool

Whether the page has no content because a forced break skipped it.

Examples

CssStyleSheet sheet = CssStyleSheet.Parse("""
    @page { size: A4; margin: 2cm; font: 9pt serif;
      @bottom-center { content: counter(page) } }
    @page :first { margin-top: 5cm; @bottom-center { content: none } }
    @page :left { margin-left: 3cm }
    @page appendix { size: A4 landscape }
    h1.appendix { page: appendix; break-before: right }
    """);
CssPageRule first = (CssPageRule)sheet.Rules[1];
CssPageSelector selector = first.Selectors[0];               // :first, specificity (0, 1, 0)
CssPageMarginRule footer = ((CssPageRule)sheet.Rules[0]).MarginRules[0]; // CssMarginBox.BottomCenter

// Cascade the page rules for each page the layout makes, then compute the page context from the root element's
// computed style and each margin box from the page context's.
var cascade = new CssCascade([(sheet, CssOrigin.Author)]);
CssPageStyle pageStyle = cascade.CascadePage(new CssPage(Name: null, IsFirst: false, IsLeft: true));
CssComputedStyle root = cascade.Compute(new CssCascadedStyle(), null);
CssComputedStyle page = cascade.Compute(pageStyle.Page, root);
string? marginLeft = page.GetValueText("margin-left");                // "3cm"
bool isSize = CssPageSize.TryParse(page.GetValueText("size")!, out CssPageSize a4); // 210mm by 297mm
CssComputedStyle box = cascade.Compute(pageStyle.MarginBoxes[CssMarginBox.BottomCenter], page);
string? content = box.GetValueText("content");                        // "counter(page)"
string? fontSize = box.GetValueText("font-size");                     // "9pt": inherited from the page context

// A named page: the appendix pages turn A4 to landscape.
CssPageStyle appendix = cascade.CascadePage(new CssPage("appendix", IsFirst: false, IsLeft: false));
bool isTurned = CssPageSize.TryParse(appendix.Page.GetValueText("size")!, out CssPageSize turned);
bool landscape = turned.Orientation == CssPageOrientation.Landscape;  // true: 297mm by 210mm

Properties

IsBlank

Whether the page has no content because a forced break skipped it.

public bool IsBlank { get; init; }

Property Value

bool

IsFirst

Whether this is the document's first page.

public bool IsFirst { get; init; }

Property Value

bool

IsLeft

Whether this is a left page; otherwise a right page.

public bool IsLeft { get; init; }

Property Value

bool

Name

The page type name, or null for an unnamed page (page: auto throughout).

public string? Name { get; init; }

Property Value

string

Methods

ToString()

Formats the page as a record does, such as CssPage { Name = chapter, IsFirst = True, IsLeft = False, IsBlank = False }.

public override string ToString()

Returns

string

The text.