refactor: rename CssStyle properties to match CSS naming

- TextAlign enum → CssTextAlign (reordered to match settings: Justify=0, Left=1, Center=2, Right=3)
- alignment → textAlign
- indent → textIndent
- decoration → textDecoration
- Update CssPropertyFlags field names to match
- Remove TextAlign::None; default to CssTextAlign::Left

This aligns internal naming with actual CSS property names for clarity.
This commit is contained in:
Jake Kenneally 2026-02-03 19:41:54 -05:00
parent 53931b3693
commit 0fa4bb4100
4 changed files with 70 additions and 81 deletions

View File

@ -204,15 +204,15 @@ std::vector<std::string> CssParser::splitWhitespace(const std::string& s) {
// Property value interpreters // Property value interpreters
TextAlign CssParser::interpretAlignment(const std::string& val) { CssTextAlign CssParser::interpretAlignment(const std::string& val) {
const std::string v = normalized(val); const std::string v = normalized(val);
if (v == "left" || v == "start") return TextAlign::Left; if (v == "left" || v == "start") return CssTextAlign::Left;
if (v == "right" || v == "end") return TextAlign::Right; if (v == "right" || v == "end") return CssTextAlign::Right;
if (v == "center") return TextAlign::Center; if (v == "center") return CssTextAlign::Center;
if (v == "justify") return TextAlign::Justify; if (v == "justify") return CssTextAlign::Justify;
return TextAlign::None; return CssTextAlign::Left;
} }
CssFontStyle CssParser::interpretFontStyle(const std::string& val) { CssFontStyle CssParser::interpretFontStyle(const std::string& val) {
@ -352,11 +352,8 @@ CssStyle CssParser::parseDeclarations(const std::string& declBlock) {
// Match property and set value // Match property and set value
if (propName == "text-align") { if (propName == "text-align") {
const TextAlign align = interpretAlignment(propValue); style.textAlign = interpretAlignment(propValue);
if (align != TextAlign::None) { style.defined.textAlign = 1;
style.alignment = align;
style.defined.alignment = 1;
}
} else if (propName == "font-style") { } else if (propName == "font-style") {
style.fontStyle = interpretFontStyle(propValue); style.fontStyle = interpretFontStyle(propValue);
style.defined.fontStyle = 1; style.defined.fontStyle = 1;
@ -364,11 +361,11 @@ CssStyle CssParser::parseDeclarations(const std::string& declBlock) {
style.fontWeight = interpretFontWeight(propValue); style.fontWeight = interpretFontWeight(propValue);
style.defined.fontWeight = 1; style.defined.fontWeight = 1;
} else if (propName == "text-decoration" || propName == "text-decoration-line") { } else if (propName == "text-decoration" || propName == "text-decoration-line") {
style.decoration = interpretDecoration(propValue); style.textDecoration = interpretDecoration(propValue);
style.defined.decoration = 1; style.defined.textDecoration = 1;
} else if (propName == "text-indent") { } else if (propName == "text-indent") {
style.indent = interpretLength(propValue); style.textIndent = interpretLength(propValue);
style.defined.indent = 1; style.defined.textIndent = 1;
} else if (propName == "margin-top") { } else if (propName == "margin-top") {
style.marginTop = interpretLength(propValue); style.marginTop = interpretLength(propValue);
style.defined.marginTop = 1; style.defined.marginTop = 1;

View File

@ -85,7 +85,7 @@ class CssParser {
static CssStyle parseDeclarations(const std::string& declBlock); static CssStyle parseDeclarations(const std::string& declBlock);
// Individual property value parsers // Individual property value parsers
static TextAlign interpretAlignment(const std::string& val); static CssTextAlign interpretAlignment(const std::string& val);
static CssFontStyle interpretFontStyle(const std::string& val); static CssFontStyle interpretFontStyle(const std::string& val);
static CssFontWeight interpretFontWeight(const std::string& val); static CssFontWeight interpretFontWeight(const std::string& val);
static CssTextDecoration interpretDecoration(const std::string& val); static CssTextDecoration interpretDecoration(const std::string& val);

View File

@ -2,10 +2,8 @@
#include <cstdint> #include <cstdint>
// Text alignment options matching CSS text-align property // Matches order of PARAGRAPH_ALIGNMENT in CrossPointSettings
enum class TextAlign : uint8_t { None = 0, Left = 1, Right = 2, Center = 3, Justify = 4 }; enum class CssTextAlign : uint8_t { Justify = 0, Left = 1, Center = 2, Right = 3 };
// CSS length unit types
enum class CssUnit : uint8_t { Pixels = 0, Em = 1, Rem = 2, Points = 3 }; enum class CssUnit : uint8_t { Pixels = 0, Em = 1, Rem = 2, Points = 3 };
// Represents a CSS length value with its unit, allowing deferred resolution to pixels // Represents a CSS length value with its unit, allowing deferred resolution to pixels
@ -47,11 +45,11 @@ enum class CssTextDecoration : uint8_t { None = 0, Underline = 1 };
// Bitmask for tracking which properties have been explicitly set // Bitmask for tracking which properties have been explicitly set
struct CssPropertyFlags { struct CssPropertyFlags {
uint16_t alignment : 1; uint16_t textAlign : 1;
uint16_t fontStyle : 1; uint16_t fontStyle : 1;
uint16_t fontWeight : 1; uint16_t fontWeight : 1;
uint16_t decoration : 1; uint16_t textDecoration : 1;
uint16_t indent : 1; uint16_t textIndent : 1;
uint16_t marginTop : 1; uint16_t marginTop : 1;
uint16_t marginBottom : 1; uint16_t marginBottom : 1;
uint16_t marginLeft : 1; uint16_t marginLeft : 1;
@ -60,14 +58,13 @@ struct CssPropertyFlags {
uint16_t paddingBottom : 1; uint16_t paddingBottom : 1;
uint16_t paddingLeft : 1; uint16_t paddingLeft : 1;
uint16_t paddingRight : 1; uint16_t paddingRight : 1;
uint16_t reserved : 3;
CssPropertyFlags() CssPropertyFlags()
: alignment(0), : textAlign(0),
fontStyle(0), fontStyle(0),
fontWeight(0), fontWeight(0),
decoration(0), textDecoration(0),
indent(0), textIndent(0),
marginTop(0), marginTop(0),
marginBottom(0), marginBottom(0),
marginLeft(0), marginLeft(0),
@ -75,16 +72,15 @@ struct CssPropertyFlags {
paddingTop(0), paddingTop(0),
paddingBottom(0), paddingBottom(0),
paddingLeft(0), paddingLeft(0),
paddingRight(0), paddingRight(0) {}
reserved(0) {}
[[nodiscard]] bool anySet() const { [[nodiscard]] bool anySet() const {
return alignment || fontStyle || fontWeight || decoration || indent || marginTop || marginBottom || marginLeft || return textAlign || fontStyle || fontWeight || textDecoration || textIndent || marginTop || marginBottom ||
marginRight || paddingTop || paddingBottom || paddingLeft || paddingRight; marginLeft || marginRight || paddingTop || paddingBottom || paddingLeft || paddingRight;
} }
void clearAll() { void clearAll() {
alignment = fontStyle = fontWeight = decoration = indent = 0; textAlign = fontStyle = fontWeight = textDecoration = textIndent = 0;
marginTop = marginBottom = marginLeft = marginRight = 0; marginTop = marginBottom = marginLeft = marginRight = 0;
paddingTop = paddingBottom = paddingLeft = paddingRight = 0; paddingTop = paddingBottom = paddingLeft = paddingRight = 0;
} }
@ -94,12 +90,12 @@ struct CssPropertyFlags {
// Only stores properties relevant to e-ink text rendering // Only stores properties relevant to e-ink text rendering
// Length values are stored as CssLength (value + unit) for deferred resolution // Length values are stored as CssLength (value + unit) for deferred resolution
struct CssStyle { struct CssStyle {
TextAlign alignment = TextAlign::None; CssTextAlign textAlign = CssTextAlign::Left;
CssFontStyle fontStyle = CssFontStyle::Normal; CssFontStyle fontStyle = CssFontStyle::Normal;
CssFontWeight fontWeight = CssFontWeight::Normal; CssFontWeight fontWeight = CssFontWeight::Normal;
CssTextDecoration decoration = CssTextDecoration::None; CssTextDecoration textDecoration = CssTextDecoration::None;
CssLength indent; // First-line indent (deferred resolution) CssLength textIndent; // First-line indent (deferred resolution)
CssLength marginTop; // Vertical spacing before block CssLength marginTop; // Vertical spacing before block
CssLength marginBottom; // Vertical spacing after block CssLength marginBottom; // Vertical spacing after block
CssLength marginLeft; // Horizontal spacing left of block CssLength marginLeft; // Horizontal spacing left of block
@ -114,66 +110,65 @@ struct CssStyle {
// Apply properties from another style, only overwriting if the other style // Apply properties from another style, only overwriting if the other style
// has that property explicitly defined // has that property explicitly defined
void applyOver(const CssStyle& base) { void applyOver(const CssStyle& base) {
if (base.defined.alignment) { if (base.hasTextAlign()) {
alignment = base.alignment; textAlign = base.textAlign;
defined.alignment = 1; defined.textAlign = 1;
} }
if (base.defined.fontStyle) { if (base.hasFontStyle()) {
fontStyle = base.fontStyle; fontStyle = base.fontStyle;
defined.fontStyle = 1; defined.fontStyle = 1;
} }
if (base.defined.fontWeight) { if (base.hasFontWeight()) {
fontWeight = base.fontWeight; fontWeight = base.fontWeight;
defined.fontWeight = 1; defined.fontWeight = 1;
} }
if (base.defined.decoration) { if (base.hasTextDecoration()) {
decoration = base.decoration; textDecoration = base.textDecoration;
defined.decoration = 1; defined.textDecoration = 1;
} }
if (base.defined.indent) { if (base.hasTextIndent()) {
indent = base.indent; textIndent = base.textIndent;
defined.indent = 1; defined.textIndent = 1;
} }
if (base.defined.marginTop) { if (base.hasMarginTop()) {
marginTop = base.marginTop; marginTop = base.marginTop;
defined.marginTop = 1; defined.marginTop = 1;
} }
if (base.defined.marginBottom) { if (base.hasMarginBottom()) {
marginBottom = base.marginBottom; marginBottom = base.marginBottom;
defined.marginBottom = 1; defined.marginBottom = 1;
} }
if (base.defined.marginLeft) { if (base.hasMarginLeft()) {
marginLeft = base.marginLeft; marginLeft = base.marginLeft;
defined.marginLeft = 1; defined.marginLeft = 1;
} }
if (base.defined.marginRight) { if (base.hasMarginRight()) {
marginRight = base.marginRight; marginRight = base.marginRight;
defined.marginRight = 1; defined.marginRight = 1;
} }
if (base.defined.paddingTop) { if (base.hasPaddingTop()) {
paddingTop = base.paddingTop; paddingTop = base.paddingTop;
defined.paddingTop = 1; defined.paddingTop = 1;
} }
if (base.defined.paddingBottom) { if (base.hasPaddingBottom()) {
paddingBottom = base.paddingBottom; paddingBottom = base.paddingBottom;
defined.paddingBottom = 1; defined.paddingBottom = 1;
} }
if (base.defined.paddingLeft) { if (base.hasPaddingLeft()) {
paddingLeft = base.paddingLeft; paddingLeft = base.paddingLeft;
defined.paddingLeft = 1; defined.paddingLeft = 1;
} }
if (base.defined.paddingRight) { if (base.hasPaddingRight()) {
paddingRight = base.paddingRight; paddingRight = base.paddingRight;
defined.paddingRight = 1; defined.paddingRight = 1;
} }
} }
// Compatibility accessors for existing code that uses hasX pattern [[nodiscard]] bool hasTextAlign() const { return defined.textAlign; }
[[nodiscard]] bool hasTextAlign() const { return defined.alignment; }
[[nodiscard]] bool hasFontStyle() const { return defined.fontStyle; } [[nodiscard]] bool hasFontStyle() const { return defined.fontStyle; }
[[nodiscard]] bool hasFontWeight() const { return defined.fontWeight; } [[nodiscard]] bool hasFontWeight() const { return defined.fontWeight; }
[[nodiscard]] bool hasTextDecoration() const { return defined.decoration; } [[nodiscard]] bool hasTextDecoration() const { return defined.textDecoration; }
[[nodiscard]] bool hasTextIndent() const { return defined.indent; } [[nodiscard]] bool hasTextIndent() const { return defined.textIndent; }
[[nodiscard]] bool hasMarginTop() const { return defined.marginTop; } [[nodiscard]] bool hasMarginTop() const { return defined.marginTop; }
[[nodiscard]] bool hasMarginBottom() const { return defined.marginBottom; } [[nodiscard]] bool hasMarginBottom() const { return defined.marginBottom; }
[[nodiscard]] bool hasMarginLeft() const { return defined.marginLeft; } [[nodiscard]] bool hasMarginLeft() const { return defined.marginLeft; }
@ -183,15 +178,12 @@ struct CssStyle {
[[nodiscard]] bool hasPaddingLeft() const { return defined.paddingLeft; } [[nodiscard]] bool hasPaddingLeft() const { return defined.paddingLeft; }
[[nodiscard]] bool hasPaddingRight() const { return defined.paddingRight; } [[nodiscard]] bool hasPaddingRight() const { return defined.paddingRight; }
// Merge another style (alias for applyOver for compatibility)
void merge(const CssStyle& other) { applyOver(other); }
void reset() { void reset() {
alignment = TextAlign::None; textAlign = CssTextAlign::Left;
fontStyle = CssFontStyle::Normal; fontStyle = CssFontStyle::Normal;
fontWeight = CssFontWeight::Normal; fontWeight = CssFontWeight::Normal;
decoration = CssTextDecoration::None; textDecoration = CssTextDecoration::None;
indent = CssLength{}; textIndent = CssLength{};
marginTop = marginBottom = marginLeft = marginRight = CssLength{}; marginTop = marginBottom = marginLeft = marginRight = CssLength{};
paddingTop = paddingBottom = paddingLeft = paddingRight = CssLength{}; paddingTop = paddingBottom = paddingLeft = paddingRight = CssLength{};
defined.clearAll(); defined.clearAll();

View File

@ -64,8 +64,8 @@ BlockStyle createBlockStyleFromCss(const CssStyle& cssStyle, const float emSize)
blockStyle.paddingLeft = cssStyle.paddingLeft.toPixelsInt16(emSize); blockStyle.paddingLeft = cssStyle.paddingLeft.toPixelsInt16(emSize);
blockStyle.paddingRight = cssStyle.paddingRight.toPixelsInt16(emSize); blockStyle.paddingRight = cssStyle.paddingRight.toPixelsInt16(emSize);
// Text indent // Text indent
blockStyle.textIndent = cssStyle.indent.toPixelsInt16(emSize); blockStyle.textIndent = cssStyle.textIndent.toPixelsInt16(emSize);
blockStyle.textIndentDefined = cssStyle.defined.indent; blockStyle.textIndentDefined = cssStyle.defined.textIndent;
return blockStyle; return blockStyle;
} }
@ -75,7 +75,7 @@ void ChapterHtmlSlimParser::updateEffectiveInlineStyle() {
effectiveBold = currentBlockStyle.hasFontWeight() && currentBlockStyle.fontWeight == CssFontWeight::Bold; effectiveBold = currentBlockStyle.hasFontWeight() && currentBlockStyle.fontWeight == CssFontWeight::Bold;
effectiveItalic = currentBlockStyle.hasFontStyle() && currentBlockStyle.fontStyle == CssFontStyle::Italic; effectiveItalic = currentBlockStyle.hasFontStyle() && currentBlockStyle.fontStyle == CssFontStyle::Italic;
effectiveUnderline = effectiveUnderline =
currentBlockStyle.hasTextDecoration() && currentBlockStyle.decoration == CssTextDecoration::Underline; currentBlockStyle.hasTextDecoration() && currentBlockStyle.textDecoration == CssTextDecoration::Underline;
// Apply inline style stack in order // Apply inline style stack in order
for (const auto& entry : inlineStyleStack) { for (const auto& entry : inlineStyleStack) {
@ -255,7 +255,7 @@ void XMLCALL ChapterHtmlSlimParser::startElement(void* userData, const XML_Char*
// Merge inline style (highest priority) // Merge inline style (highest priority)
if (!styleAttr.empty()) { if (!styleAttr.empty()) {
CssStyle inlineStyle = CssParser::parseInlineStyle(styleAttr); CssStyle inlineStyle = CssParser::parseInlineStyle(styleAttr);
cssStyle.merge(inlineStyle); cssStyle.applyOver(inlineStyle);
} }
} }
@ -263,17 +263,17 @@ void XMLCALL ChapterHtmlSlimParser::startElement(void* userData, const XML_Char*
// Headers: center aligned, bold, apply CSS overrides // Headers: center aligned, bold, apply CSS overrides
TextBlock::Style alignment = TextBlock::CENTER_ALIGN; TextBlock::Style alignment = TextBlock::CENTER_ALIGN;
if (cssStyle.hasTextAlign()) { if (cssStyle.hasTextAlign()) {
switch (cssStyle.alignment) { switch (cssStyle.textAlign) {
case TextAlign::Left: case CssTextAlign::Left:
alignment = TextBlock::LEFT_ALIGN; alignment = TextBlock::LEFT_ALIGN;
break; break;
case TextAlign::Right: case CssTextAlign::Right:
alignment = TextBlock::RIGHT_ALIGN; alignment = TextBlock::RIGHT_ALIGN;
break; break;
case TextAlign::Center: case CssTextAlign::Center:
alignment = TextBlock::CENTER_ALIGN; alignment = TextBlock::CENTER_ALIGN;
break; break;
case TextAlign::Justify: case CssTextAlign::Justify:
alignment = TextBlock::JUSTIFIED; alignment = TextBlock::JUSTIFIED;
break; break;
default: default:
@ -296,17 +296,17 @@ void XMLCALL ChapterHtmlSlimParser::startElement(void* userData, const XML_Char*
// Determine alignment from CSS or default // Determine alignment from CSS or default
auto alignment = static_cast<TextBlock::Style>(self->paragraphAlignment); auto alignment = static_cast<TextBlock::Style>(self->paragraphAlignment);
if (cssStyle.hasTextAlign()) { if (cssStyle.hasTextAlign()) {
switch (cssStyle.alignment) { switch (cssStyle.textAlign) {
case TextAlign::Left: case CssTextAlign::Left:
alignment = TextBlock::LEFT_ALIGN; alignment = TextBlock::LEFT_ALIGN;
break; break;
case TextAlign::Right: case CssTextAlign::Right:
alignment = TextBlock::RIGHT_ALIGN; alignment = TextBlock::RIGHT_ALIGN;
break; break;
case TextAlign::Center: case CssTextAlign::Center:
alignment = TextBlock::CENTER_ALIGN; alignment = TextBlock::CENTER_ALIGN;
break; break;
case TextAlign::Justify: case CssTextAlign::Justify:
alignment = TextBlock::JUSTIFIED; alignment = TextBlock::JUSTIFIED;
break; break;
default: default:
@ -352,7 +352,7 @@ void XMLCALL ChapterHtmlSlimParser::startElement(void* userData, const XML_Char*
} }
if (cssStyle.hasTextDecoration()) { if (cssStyle.hasTextDecoration()) {
entry.hasUnderline = true; entry.hasUnderline = true;
entry.underline = cssStyle.decoration == CssTextDecoration::Underline; entry.underline = cssStyle.textDecoration == CssTextDecoration::Underline;
} }
self->inlineStyleStack.push_back(entry); self->inlineStyleStack.push_back(entry);
self->updateEffectiveInlineStyle(); self->updateEffectiveInlineStyle();
@ -369,7 +369,7 @@ void XMLCALL ChapterHtmlSlimParser::startElement(void* userData, const XML_Char*
} }
if (cssStyle.hasTextDecoration()) { if (cssStyle.hasTextDecoration()) {
entry.hasUnderline = true; entry.hasUnderline = true;
entry.underline = cssStyle.decoration == CssTextDecoration::Underline; entry.underline = cssStyle.textDecoration == CssTextDecoration::Underline;
} }
self->inlineStyleStack.push_back(entry); self->inlineStyleStack.push_back(entry);
self->updateEffectiveInlineStyle(); self->updateEffectiveInlineStyle();
@ -388,7 +388,7 @@ void XMLCALL ChapterHtmlSlimParser::startElement(void* userData, const XML_Char*
} }
if (cssStyle.hasTextDecoration()) { if (cssStyle.hasTextDecoration()) {
entry.hasUnderline = true; entry.hasUnderline = true;
entry.underline = cssStyle.decoration == CssTextDecoration::Underline; entry.underline = cssStyle.textDecoration == CssTextDecoration::Underline;
} }
self->inlineStyleStack.push_back(entry); self->inlineStyleStack.push_back(entry);
self->updateEffectiveInlineStyle(); self->updateEffectiveInlineStyle();