Refactor Odt text style class
This commit is contained in:
+27
-17
@@ -128,7 +128,7 @@ class ToOdt(Tokenizer):
|
|||||||
|
|
||||||
self._mainPara: dict[str, ODTParagraphStyle] = {} # User-accessible paragraph styles
|
self._mainPara: dict[str, ODTParagraphStyle] = {} # User-accessible paragraph styles
|
||||||
self._autoPara: dict[str, ODTParagraphStyle] = {} # Auto-generated paragraph styles
|
self._autoPara: dict[str, ODTParagraphStyle] = {} # Auto-generated paragraph styles
|
||||||
self._autoText = {} # Auto-generated text styles
|
self._autoText: dict[int, ODTTextStyle] = {} # Auto-generated text styles
|
||||||
|
|
||||||
self._errData = [] # List of errors encountered
|
self._errData = [] # List of errors encountered
|
||||||
|
|
||||||
@@ -513,12 +513,11 @@ class ToOdt(Tokenizer):
|
|||||||
return
|
return
|
||||||
|
|
||||||
def closeDocument(self) -> None:
|
def closeDocument(self) -> None:
|
||||||
"""Pack the styles of the XML document."""
|
"""Pack the automatic styles of the XML document."""
|
||||||
# Build the auto-generated styles
|
|
||||||
for style in self._autoPara.values():
|
for style in self._autoPara.values():
|
||||||
style.packXML(self._xAuto)
|
style.packXML(self._xAuto)
|
||||||
for styleName, style in self._autoText.values():
|
for style in self._autoText.values():
|
||||||
style.packXML(self._xAuto, styleName)
|
style.packXML(self._xAuto)
|
||||||
return
|
return
|
||||||
|
|
||||||
def saveFlatXML(self, path: str | Path) -> None:
|
def saveFlatXML(self, path: str | Path) -> None:
|
||||||
@@ -711,10 +710,9 @@ class ToOdt(Tokenizer):
|
|||||||
def _textStyle(self, hFmt: int) -> str:
|
def _textStyle(self, hFmt: int) -> str:
|
||||||
"""Return a text style for a given style code."""
|
"""Return a text style for a given style code."""
|
||||||
if hFmt in self._autoText:
|
if hFmt in self._autoText:
|
||||||
return self._autoText[hFmt][0]
|
return self._autoText[hFmt].name
|
||||||
|
|
||||||
name = "T%d" % (len(self._autoText) + 1)
|
style = ODTTextStyle(f"T{len(self._autoText)+1:d}")
|
||||||
style = ODTTextStyle()
|
|
||||||
if hFmt & X_BLD:
|
if hFmt & X_BLD:
|
||||||
style.setFontWeight("bold")
|
style.setFontWeight("bold")
|
||||||
if hFmt & X_ITA:
|
if hFmt & X_ITA:
|
||||||
@@ -727,15 +725,14 @@ class ToOdt(Tokenizer):
|
|||||||
style.setUnderlineWidth("auto")
|
style.setUnderlineWidth("auto")
|
||||||
style.setUnderlineColour("font-color")
|
style.setUnderlineColour("font-color")
|
||||||
if hFmt & X_MRK:
|
if hFmt & X_MRK:
|
||||||
style.setBackgroundColor(self._markText)
|
style.setBackgroundColour(self._markText)
|
||||||
if hFmt & X_SUP:
|
if hFmt & X_SUP:
|
||||||
style.setTextPosition("super")
|
style.setTextPosition("super")
|
||||||
if hFmt & X_SUB:
|
if hFmt & X_SUB:
|
||||||
style.setTextPosition("sub")
|
style.setTextPosition("sub")
|
||||||
|
self._autoText[hFmt] = style
|
||||||
|
|
||||||
self._autoText[hFmt] = (name, style)
|
return style.name
|
||||||
|
|
||||||
return name
|
|
||||||
|
|
||||||
def _emToCm(self, value: float) -> str:
|
def _emToCm(self, value: float) -> str:
|
||||||
"""Converts an em value to centimetres."""
|
"""Converts an em value to centimetres."""
|
||||||
@@ -1279,8 +1276,8 @@ class ODTTextStyle:
|
|||||||
VALID_LWIDTH = ["auto"]
|
VALID_LWIDTH = ["auto"]
|
||||||
VALID_LCOL = ["font-color"]
|
VALID_LCOL = ["font-color"]
|
||||||
|
|
||||||
def __init__(self) -> None:
|
def __init__(self, name: str) -> None:
|
||||||
# Text Attributes
|
self._name = name
|
||||||
self._tAttr = {
|
self._tAttr = {
|
||||||
"font-weight": ["fo", None],
|
"font-weight": ["fo", None],
|
||||||
"font-style": ["fo", None],
|
"font-style": ["fo", None],
|
||||||
@@ -1294,11 +1291,16 @@ class ODTTextStyle:
|
|||||||
}
|
}
|
||||||
return
|
return
|
||||||
|
|
||||||
|
@property
|
||||||
|
def name(self) -> str:
|
||||||
|
return self._name
|
||||||
|
|
||||||
##
|
##
|
||||||
# Setters
|
# Setters
|
||||||
##
|
##
|
||||||
|
|
||||||
def setFontWeight(self, value: str | None) -> None:
|
def setFontWeight(self, value: str | None) -> None:
|
||||||
|
"""Set text font weight."""
|
||||||
if value in self.VALID_WEIGHT:
|
if value in self.VALID_WEIGHT:
|
||||||
self._tAttr["font-weight"][1] = value
|
self._tAttr["font-weight"][1] = value
|
||||||
else:
|
else:
|
||||||
@@ -1306,13 +1308,15 @@ class ODTTextStyle:
|
|||||||
return
|
return
|
||||||
|
|
||||||
def setFontStyle(self, value: str | None) -> None:
|
def setFontStyle(self, value: str | None) -> None:
|
||||||
|
"""Set text font style."""
|
||||||
if value in self.VALID_STYLE:
|
if value in self.VALID_STYLE:
|
||||||
self._tAttr["font-style"][1] = value
|
self._tAttr["font-style"][1] = value
|
||||||
else:
|
else:
|
||||||
self._tAttr["font-style"][1] = None
|
self._tAttr["font-style"][1] = None
|
||||||
return
|
return
|
||||||
|
|
||||||
def setBackgroundColor(self, value: str | None) -> None:
|
def setBackgroundColour(self, value: str | None) -> None:
|
||||||
|
"""Set text background colour."""
|
||||||
if value and len(value) == 7 and value[0] == "#":
|
if value and len(value) == 7 and value[0] == "#":
|
||||||
self._tAttr["background-color"][1] = value
|
self._tAttr["background-color"][1] = value
|
||||||
else:
|
else:
|
||||||
@@ -1320,6 +1324,7 @@ class ODTTextStyle:
|
|||||||
return
|
return
|
||||||
|
|
||||||
def setTextPosition(self, value: str | None) -> None:
|
def setTextPosition(self, value: str | None) -> None:
|
||||||
|
"""Set text vertical position."""
|
||||||
if value in self.VALID_POS:
|
if value in self.VALID_POS:
|
||||||
self._tAttr["text-position"][1] = f"{value} 58%"
|
self._tAttr["text-position"][1] = f"{value} 58%"
|
||||||
else:
|
else:
|
||||||
@@ -1327,6 +1332,7 @@ class ODTTextStyle:
|
|||||||
return
|
return
|
||||||
|
|
||||||
def setStrikeStyle(self, value: str | None) -> None:
|
def setStrikeStyle(self, value: str | None) -> None:
|
||||||
|
"""Set text line-trough style."""
|
||||||
if value in self.VALID_LSTYLE:
|
if value in self.VALID_LSTYLE:
|
||||||
self._tAttr["text-line-through-style"][1] = value
|
self._tAttr["text-line-through-style"][1] = value
|
||||||
else:
|
else:
|
||||||
@@ -1334,6 +1340,7 @@ class ODTTextStyle:
|
|||||||
return
|
return
|
||||||
|
|
||||||
def setStrikeType(self, value: str | None) -> None:
|
def setStrikeType(self, value: str | None) -> None:
|
||||||
|
"""Set text line-through type."""
|
||||||
if value in self.VALID_LTYPE:
|
if value in self.VALID_LTYPE:
|
||||||
self._tAttr["text-line-through-type"][1] = value
|
self._tAttr["text-line-through-type"][1] = value
|
||||||
else:
|
else:
|
||||||
@@ -1341,6 +1348,7 @@ class ODTTextStyle:
|
|||||||
return
|
return
|
||||||
|
|
||||||
def setUnderlineStyle(self, value: str | None) -> None:
|
def setUnderlineStyle(self, value: str | None) -> None:
|
||||||
|
"""Set text underline style."""
|
||||||
if value in self.VALID_LSTYLE:
|
if value in self.VALID_LSTYLE:
|
||||||
self._tAttr["text-underline-style"][1] = value
|
self._tAttr["text-underline-style"][1] = value
|
||||||
else:
|
else:
|
||||||
@@ -1348,6 +1356,7 @@ class ODTTextStyle:
|
|||||||
return
|
return
|
||||||
|
|
||||||
def setUnderlineWidth(self, value: str | None) -> None:
|
def setUnderlineWidth(self, value: str | None) -> None:
|
||||||
|
"""Set text underline width."""
|
||||||
if value in self.VALID_LWIDTH:
|
if value in self.VALID_LWIDTH:
|
||||||
self._tAttr["text-underline-width"][1] = value
|
self._tAttr["text-underline-width"][1] = value
|
||||||
else:
|
else:
|
||||||
@@ -1355,6 +1364,7 @@ class ODTTextStyle:
|
|||||||
return
|
return
|
||||||
|
|
||||||
def setUnderlineColour(self, value: str | None) -> None:
|
def setUnderlineColour(self, value: str | None) -> None:
|
||||||
|
"""Set text underline colour."""
|
||||||
if value in self.VALID_LCOL:
|
if value in self.VALID_LCOL:
|
||||||
self._tAttr["text-underline-color"][1] = value
|
self._tAttr["text-underline-color"][1] = value
|
||||||
else:
|
else:
|
||||||
@@ -1365,10 +1375,10 @@ class ODTTextStyle:
|
|||||||
# Methods
|
# Methods
|
||||||
##
|
##
|
||||||
|
|
||||||
def packXML(self, xParent: ET.Element, name: str) -> None:
|
def packXML(self, xParent: ET.Element) -> None:
|
||||||
"""Pack the content into an xml element."""
|
"""Pack the content into an xml element."""
|
||||||
xEntry = ET.SubElement(xParent, _mkTag("style", "style"), attrib={
|
xEntry = ET.SubElement(xParent, _mkTag("style", "style"), attrib={
|
||||||
_mkTag("style", "name"): name,
|
_mkTag("style", "name"): self._name,
|
||||||
_mkTag("style", "family"): "text",
|
_mkTag("style", "family"): "text",
|
||||||
})
|
})
|
||||||
if attr := {_mkTag(n, m): v for m, (n, v) in self._tAttr.items() if v is not None}:
|
if attr := {_mkTag(n, m): v for m, (n, v) in self._tAttr.items() if v is not None}:
|
||||||
|
|||||||
@@ -1080,7 +1080,7 @@ def testCoreToOdt_ODTParagraphStyle():
|
|||||||
@pytest.mark.core
|
@pytest.mark.core
|
||||||
def testCoreToOdt_ODTTextStyle():
|
def testCoreToOdt_ODTTextStyle():
|
||||||
"""Test the ODTTextStyle class."""
|
"""Test the ODTTextStyle class."""
|
||||||
txtStyle = ODTTextStyle()
|
txtStyle = ODTTextStyle("test")
|
||||||
|
|
||||||
# Font Weight
|
# Font Weight
|
||||||
assert txtStyle._tAttr["font-weight"] == ["fo", None]
|
assert txtStyle._tAttr["font-weight"] == ["fo", None]
|
||||||
@@ -1110,13 +1110,13 @@ def testCoreToOdt_ODTTextStyle():
|
|||||||
|
|
||||||
# Background Color
|
# Background Color
|
||||||
assert txtStyle._tAttr["background-color"] == ["fo", None]
|
assert txtStyle._tAttr["background-color"] == ["fo", None]
|
||||||
txtStyle.setBackgroundColor("stuff")
|
txtStyle.setBackgroundColour("stuff")
|
||||||
assert txtStyle._tAttr["background-color"] == ["fo", None]
|
assert txtStyle._tAttr["background-color"] == ["fo", None]
|
||||||
txtStyle.setBackgroundColor("012345")
|
txtStyle.setBackgroundColour("012345")
|
||||||
assert txtStyle._tAttr["background-color"] == ["fo", None]
|
assert txtStyle._tAttr["background-color"] == ["fo", None]
|
||||||
txtStyle.setBackgroundColor("#012345")
|
txtStyle.setBackgroundColour("#012345")
|
||||||
assert txtStyle._tAttr["background-color"] == ["fo", "#012345"]
|
assert txtStyle._tAttr["background-color"] == ["fo", "#012345"]
|
||||||
txtStyle.setBackgroundColor("stuff")
|
txtStyle.setBackgroundColour("stuff")
|
||||||
assert txtStyle._tAttr["background-color"] == ["fo", None]
|
assert txtStyle._tAttr["background-color"] == ["fo", None]
|
||||||
|
|
||||||
# Text Position
|
# Text Position
|
||||||
@@ -1191,7 +1191,7 @@ def testCoreToOdt_ODTTextStyle():
|
|||||||
txtStyle.setStrikeStyle("solid")
|
txtStyle.setStrikeStyle("solid")
|
||||||
txtStyle.setStrikeType("single")
|
txtStyle.setStrikeType("single")
|
||||||
xStyle = ET.Element("test")
|
xStyle = ET.Element("test")
|
||||||
txtStyle.packXML(xStyle, "test")
|
txtStyle.packXML(xStyle)
|
||||||
assert xmlToText(xStyle) == (
|
assert xmlToText(xStyle) == (
|
||||||
'<test><style:style style:name="test" style:family="text"><style:text-properties '
|
'<test><style:style style:name="test" style:family="text"><style:text-properties '
|
||||||
'fo:font-weight="bold" fo:font-style="italic" style:text-position="super 58%" '
|
'fo:font-weight="bold" fo:font-style="italic" style:text-position="super 58%" '
|
||||||
|
|||||||
Reference in New Issue
Block a user