Skip to content

Commit e538803

Browse files
committed
refactor: streamline color handling and text initialization in SlideBuilder
1 parent eb64379 commit e538803

File tree

4 files changed

+35
-18
lines changed

4 files changed

+35
-18
lines changed

src/tppt/pptx/dml/color.py

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@
33
from lxml.etree import _Element
44
from pptx.dml.color import ColorFormat as PptxColorFormat
55
from pptx.dml.color import RGBColor as PptxRGBColor
6+
from pptx.dml.color import _SRgbColor
67
from pptx.enum.dml import MSO_THEME_COLOR
8+
from pptx.oxml.ns import _nsmap as namespace
79
from pptx.oxml.xmlchemy import OxmlElement
810

911
from tppt.pptx.converter import PptxConvertible, to_pptx_rgb_color, to_tppt_rgb_color
@@ -41,13 +43,14 @@ def rgb(self) -> Color:
4143
def rgb(self, color: Color | LiteralColor):
4244
pptx_color, alpha = to_pptx_rgb_color(color)
4345
self._pptx.rgb = pptx_color
44-
if alpha:
45-
solid_fill = cast(
46-
_Element, self._pptx._xFill.solidFill.get_or_change_to_srgbClr()
47-
)
46+
srgbClr = cast(_Element, cast(_SRgbColor, self._pptx._color)._srgbClr)
47+
if alpha is not None:
4848
element = OxmlElement("a:alpha")
49-
element.attrib["val"] = str(alpha)
50-
solid_fill.append(element)
49+
element.attrib["val"] = str(int(100000 * (alpha / 255)))
50+
srgbClr.append(element)
51+
else:
52+
if alpha := srgbClr.find("a:alpha", namespace):
53+
srgbClr.remove(alpha)
5154

5255
@property
5356
def theme_color(self) -> MSO_THEME_COLOR:

src/tppt/pptx/shape/text.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,9 @@
33
from pptx.enum.text import MSO_ANCHOR, MSO_AUTO_SIZE, PP_ALIGN
44
from pptx.shapes.autoshape import Shape as PptxShape
55

6-
from tppt.pptx.converter import to_pptx_length
76
from tppt.pptx.text.text_frame import TextFrame
87
from tppt.types._color import Color, LiteralColor, to_color
9-
from tppt.types._length import Length, LiteralLength
8+
from tppt.types._length import Length, LiteralLength, to_length
109

1110
from . import RangeProps, Shape
1211

@@ -40,23 +39,23 @@ class Text(Shape):
4039

4140
def __init__(self, pptx_obj: PptxShape, data: TextData | None = None, /) -> None:
4241
if data and data["text"] != "":
43-
text_frame = pptx_obj.text_frame
42+
text_frame = TextFrame(pptx_obj.text_frame)
4443
p = text_frame.paragraphs[0]
4544
run = p.add_run()
4645
run.text = data["text"]
4746
font = run.font
4847
if size := data.get("size"):
49-
font.size = to_pptx_length(size)
48+
font.size = to_length(size)
5049
if (bold := data.get("bold")) is not None:
5150
font.bold = bold
5251
if (italic := data.get("italic")) is not None:
5352
font.italic = italic
5453
if (color := data.get("color")) is not None:
5554
font.color.rgb = to_color(color)
5655
if (margin_bottom := data.get("margin_bottom")) is not None:
57-
p.space_after = to_pptx_length(margin_bottom)
56+
p.space_after = to_length(margin_bottom)
5857
if (margin_left := data.get("margin_left")) is not None:
59-
p.space_before = to_pptx_length(margin_left)
58+
p.space_before = to_length(margin_left)
6059
if (vertical_anchor := data.get("vertical_anchor")) is not None:
6160
text_frame.vertical_anchor = vertical_anchor
6261
if (word_wrap := data.get("word_wrap")) is not None:

src/tppt/pptx/slide.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -126,10 +126,7 @@ def _register(slide: Slide) -> Text:
126126
data = TextData(
127127
type="text",
128128
text=text if isinstance(text, str) else "",
129-
top=kwargs["top"],
130-
left=kwargs["left"],
131-
width=kwargs["width"],
132-
height=kwargs["height"],
129+
**kwargs,
133130
)
134131

135132
text_obj = Text(

tests/test_text.py

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,33 @@ def test_text_with_options(output) -> None:
1515
lambda slide: slide.BlankLayout()
1616
.builder()
1717
.text(
18-
"サンプルテキスト",
18+
"サンプルテキスト1",
19+
left=(100, "pt"),
20+
top=(300, "pt"),
21+
width=(400, "pt"),
22+
height=(200, "pt"),
23+
size=(24, "pt"),
24+
bold=True,
25+
italic=True,
26+
color="#0C0",
27+
margin_bottom=(10, "pt"),
28+
margin_left=(10, "pt"),
29+
vertical_anchor=MSO_ANCHOR.MIDDLE,
30+
word_wrap=True,
31+
auto_size=MSO_AUTO_SIZE.TEXT_TO_FIT_SHAPE,
32+
alignment=PP_ALIGN.CENTER,
33+
level=1,
34+
)
35+
.text(
36+
"サンプルテキスト2",
1937
left=(100, "pt"),
2038
top=(100, "pt"),
2139
width=(400, "pt"),
2240
height=(200, "pt"),
2341
size=(24, "pt"),
2442
bold=True,
2543
italic=True,
26-
color="#0000FF99",
44+
color="#0000BB99",
2745
margin_bottom=(10, "pt"),
2846
margin_left=(10, "pt"),
2947
vertical_anchor=MSO_ANCHOR.MIDDLE,

0 commit comments

Comments
 (0)