Krita Source Code Documentation
Loading...
Searching...
No Matches
KoSvgText::CssLengthPercentage Struct Reference

#include <KoSvgText.h>

+ Inheritance diagram for KoSvgText::CssLengthPercentage:

Public Types

enum  UnitType {
  Absolute , Percentage , Em , Ex ,
  Cap , Ch , Ic , Lh
}
 

Public Member Functions

void convertToAbsolute (const KoSvgText::FontMetrics metrics, const qreal fontSize, const UnitType percentageUnit=Em)
 
 CssLengthPercentage ()
 
 CssLengthPercentage (qreal value, UnitType unit=Absolute)
 
bool operator== (const CssLengthPercentage &other) const
 

Public Attributes

UnitType unit = Absolute
 
qreal value = 0.0
 

Detailed Description

CssLengthPercentage is a struct that represents the CSS length-percentage, which is Css' way of saying that an attribute can be either a length (which are unit-based, including font-relative units) or a percentage (which is relative to a value on a case-by-case basis).

Most percentages in SVG are viewport based, which we just resolve during parsing, because KoShapes do not know anything about the doc they're in. A handful are font-size based, which we resolve to em, and this is also what this struct defaults to. A very small subset (text-indent and text path start-offset) are relative to other things, and these are solved during text-layout.

We currently use this for both Lengths and LengthPercentages (always saving as EM when it is a Length, in the case of TabSize). Whether and how a percentage should be resolved is defined in the CSS specs in the section a given property is specified.

Definition at line 406 of file KoSvgText.h.

Member Enumeration Documentation

◆ UnitType

Enumerator
Absolute 

Pt, everything needs to be converted to pt for this to work.

Percentage 
Em 

0 to 1.0

Ex 

multiply by Font-size

Cap 

multiply by font-x-height.

Ch 

multiply by font cap height

Ic 

multiply by width of "0", represents average proportional script advance.

Lh 

multiply by width of "U+6C34", represents average full width script advance.

Definition at line 407 of file KoSvgText.h.

407 {
408 Absolute,
409 Percentage,
410 Em,
411 Ex,
412 Cap,
413 Ch,
414 Ic,
415 Lh,
416 };
@ Cap
multiply by font-x-height.
Definition KoSvgText.h:412
@ Ch
multiply by font cap height
Definition KoSvgText.h:413
@ Absolute
Pt, everything needs to be converted to pt for this to work.
Definition KoSvgText.h:408
@ Lh
multiply by width of "U+6C34", represents average full width script advance.
Definition KoSvgText.h:415
@ Ex
multiply by Font-size
Definition KoSvgText.h:411
@ Ic
multiply by width of "0", represents average proportional script advance.
Definition KoSvgText.h:414

Constructor & Destructor Documentation

◆ CssLengthPercentage() [1/2]

KoSvgText::CssLengthPercentage::CssLengthPercentage ( )
inline

Definition at line 418 of file KoSvgText.h.

418{}

◆ CssLengthPercentage() [2/2]

KoSvgText::CssLengthPercentage::CssLengthPercentage ( qreal value,
UnitType unit = Absolute )
inline

Definition at line 419 of file KoSvgText.h.

Member Function Documentation

◆ convertToAbsolute()

void KoSvgText::CssLengthPercentage::convertToAbsolute ( const KoSvgText::FontMetrics metrics,
const qreal fontSize,
const UnitType percentageUnit = Em )

Definition at line 949 of file KoSvgText.cpp.

949 {
950 UnitType u = unit;
951 if (u == Percentage) {
952 u = percentageUnit;
953 }
954
955 const qreal ftMultiplier = fontSize / metrics.fontSize;
956 if (u == Em) {
957 value = value * fontSize;
958 } else if (u == Ex) {
959 value = value * metrics.xHeight * ftMultiplier;
960 } else if (u == Cap) {
961 value = value * metrics.capHeight * ftMultiplier;
962 } else if (u == Ch) {
963 value = value * metrics.zeroAdvance * ftMultiplier;
964 } else if (u == Ic) {
965 value = value * metrics.ideographicAdvance * ftMultiplier;
966 } else if (u == Lh) {
967 value = value * (metrics.ascender - metrics.descender + metrics.lineGap) * ftMultiplier;
968 }
969 unit = Absolute;
970}
qreal u
qint32 xHeight
height of X, defaults to 0.5 fontsize.
Definition KoSvgText.h:334
qint32 lineGap
additional linegap between consecutive lines.
Definition KoSvgText.h:341
qint32 zeroAdvance
Advance of the character '0', CSS Unit 'ch', defaults to 0.5 em in horizontal and 1....
Definition KoSvgText.h:330
qint32 ideographicAdvance
Advance of the character '水' (U+6C34), CSS Unit ic, defaults to 1 em.
Definition KoSvgText.h:332
qint32 fontSize
Currently set size, CSS unit 'em'.
Definition KoSvgText.h:329
qint32 descender
distance for origin to bottom.
Definition KoSvgText.h:340
qint32 ascender
distance from origin to top.
Definition KoSvgText.h:339
qint32 capHeight
Height of capital letters, defaults to ascender.
Definition KoSvgText.h:335

References Absolute, KoSvgText::FontMetrics::ascender, Cap, KoSvgText::FontMetrics::capHeight, Ch, KoSvgText::FontMetrics::descender, Em, Ex, KoSvgText::FontMetrics::fontSize, Ic, KoSvgText::FontMetrics::ideographicAdvance, Lh, KoSvgText::FontMetrics::lineGap, Percentage, u, unit, value, KoSvgText::FontMetrics::xHeight, and KoSvgText::FontMetrics::zeroAdvance.

◆ operator==()

bool KoSvgText::CssLengthPercentage::operator== ( const CssLengthPercentage & other) const
inline

Definition at line 426 of file KoSvgText.h.

426 {
427 return qFuzzyCompare(value, other.value) && unit == other.unit;
428 }
static bool qFuzzyCompare(half p1, half p2)

References qFuzzyCompare(), unit, and value.

Member Data Documentation

◆ unit

UnitType KoSvgText::CssLengthPercentage::unit = Absolute

Definition at line 422 of file KoSvgText.h.

◆ value

qreal KoSvgText::CssLengthPercentage::value = 0.0

Definition at line 421 of file KoSvgText.h.


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