Donner
C++20 SVG rendering library
Loading...
Searching...
No Matches
donner::Length< T > Struct Template Reference

Parses a CSS <length-percentage> type as defined by https://www.w3.org/TR/css-values-3/#typedef-length-percentage. More...

#include "donner/base/Length.h"

Public Types

enum class  Extent {
  X ,
  Y ,
  Mixed
}
 Selects which extent of the viewbox to use for percentage and viewbox-relative length conversions, see toPixels(). More...
 
using Unit = LengthUnit
 The unit identifier for the length.
 

Public Member Functions

 Length ()=default
 Default constructor, initializes to unitless 0.
 
 Length (T value, Unit unit=Unit::None)
 Construct a length from a value and unit.
 
bool operator== (const Length &other) const
 Equality operator, using near-equals comparison for the value.
 
std::partial_ordering operator<=> (const Length &other) const
 Spaceship operator, first ordered by the unit if they are not the same, then by the value (with a near-equals comparison).
 
bool isAbsoluteSize () const
 Returns true if the length is an absolute dimension (not a percentage or relative unit).
 
toPixels (const Box< T > &viewbox, const FontMetrics &fontMetrics, Extent extent=Extent::Mixed) const
 Convert the length to pixels, following the ratios at https://www.w3.org/TR/css-values/#absolute-lengths and https://www.w3.org/TR/css-values/#relative-lengths.
 

Public Attributes

value = T(0)
 The numeric value of the length.
 
Unit unit = Unit::None
 The unit identifier of the length.
 

Friends

std::ostream & operator<< (std::ostream &os, const Length< T > &length)
 ostream-output operator for Length.
 

Detailed Description

template<typename T>
struct donner::Length< T >

Parses a CSS <length-percentage> type as defined by https://www.w3.org/TR/css-values-3/#typedef-length-percentage.

A length is composed of a number followed by a unit identifier.

Unit identifiers are split into two categories, absolute and relative:

The unit may be omitted for '0', which is unitless.

For a percentage, the number is followed by the '%' character.

Examples:

  • 0
  • 10px
  • 50%
Template Parameters
TValue storage type, typically double.

Member Enumeration Documentation

◆ Extent

template<typename T >
enum class donner::Length::Extent
strong

Selects which extent of the viewbox to use for percentage and viewbox-relative length conversions, see toPixels().

Enumerator

Use X component of viewbox for percentage calculations.

Use Y component of viewbox for percentage calculations.

Mixed 

Use diagonal extent of viewbox.

Constructor & Destructor Documentation

◆ Length()

template<typename T >
donner::Length< T >::Length ( T value,
Unit unit = Unit::None )
inlineexplicit

Construct a length from a value and unit.

Parameters
valueThe numeric value of the length.
unitThe unit identifier of the length.

Member Function Documentation

◆ toPixels()

template<typename T >
T donner::Length< T >::toPixels ( const Box< T > & viewbox,
const FontMetrics & fontMetrics,
Extent extent = Extent::Mixed ) const
inline

Convert the length to pixels, following the ratios at https://www.w3.org/TR/css-values/#absolute-lengths and https://www.w3.org/TR/css-values/#relative-lengths.

Parameters
viewboxViewbox of the element for computing viewbox-relative conversions.
fontMetricsFont size information for font-relative sizes.
extentWhich extent of the viewbox to use for percentage and viewbox-relative length.
Returns
Length in pixels.

Friends And Related Symbol Documentation

◆ operator<<

template<typename T >
std::ostream & operator<< ( std::ostream & os,
const Length< T > & length )
friend

ostream-output operator for Length.

Parameters
osThe ostream to write to.
lengthLength class to output.
Returns
The ostream that was written to, for chaining.

The documentation for this struct was generated from the following file: