|
| 1 | +"""Step implementations for features/iss-16-advanced-text.feature (issue #16). |
| 2 | +
|
| 3 | +Self-contained: every scenario builds an in-memory blank presentation. |
| 4 | +""" |
| 5 | + |
| 6 | +import io |
| 7 | +import os |
| 8 | + |
| 9 | +from behave import given, then, when |
| 10 | + |
| 11 | +from pptx import Presentation |
| 12 | +from pptx.dml.color import RGBColor |
| 13 | +from pptx.enum.text import MSO_AUTO_SIZE, MSO_TEXT_DIRECTION, MSO_TEXT_STRIKE_TYPE |
| 14 | +from pptx.util import Inches, Pt |
| 15 | + |
| 16 | +TEST_FONT = os.path.join( |
| 17 | + os.path.dirname(os.path.dirname(__file__)), "..", "tests", "test_files", "calibriz.ttf" |
| 18 | +) |
| 19 | +TEST_FONT = os.path.abspath(TEST_FONT) |
| 20 | + |
| 21 | + |
| 22 | +def _blank_run(context): |
| 23 | + prs = Presentation() |
| 24 | + s = prs.slides.add_slide(prs.slide_layouts[6]) |
| 25 | + tf = s.shapes.add_textbox(Inches(1), Inches(1), Inches(4), Inches(2)).text_frame |
| 26 | + r = tf.paragraphs[0].add_run() |
| 27 | + r.text = "Sample" |
| 28 | + context.prs = prs |
| 29 | + context.tf = tf |
| 30 | + context.run = r |
| 31 | + |
| 32 | + |
| 33 | +def _roundtrip(context): |
| 34 | + buf = io.BytesIO() |
| 35 | + context.prs.save(buf) |
| 36 | + buf.seek(0) |
| 37 | + context.prs2 = Presentation(buf) |
| 38 | + context.tf2 = list(context.prs2.slides[0].shapes)[0].text_frame |
| 39 | + return context.tf2 |
| 40 | + |
| 41 | + |
| 42 | +@given("a blank slide text frame with one run") |
| 43 | +def given_blank_run(context): |
| 44 | + _blank_run(context) |
| 45 | + |
| 46 | + |
| 47 | +@given("a tiny text frame stuffed with text") |
| 48 | +def given_tiny_stuffed(context): |
| 49 | + prs = Presentation() |
| 50 | + s = prs.slides.add_slide(prs.slide_layouts[6]) |
| 51 | + tf = s.shapes.add_textbox(Inches(0), Inches(0), Inches(1), Inches(0.3)).text_frame |
| 52 | + tf.text = "Supercalifragilistic " * 14 |
| 53 | + for r in tf.paragraphs[0].runs: |
| 54 | + r.font.size = Pt(18) |
| 55 | + context.prs = prs |
| 56 | + context.tf = tf |
| 57 | + |
| 58 | + |
| 59 | +@given("a tiny text frame with one very long word") |
| 60 | +def given_tiny_longword(context): |
| 61 | + prs = Presentation() |
| 62 | + s = prs.slides.add_slide(prs.slide_layouts[6]) |
| 63 | + tf = s.shapes.add_textbox(Inches(0), Inches(0), Inches(0.5), Inches(0.5)).text_frame |
| 64 | + tf.text = "Supercalifragilisticexpialidocious" |
| 65 | + context.prs = prs |
| 66 | + context.tf = tf |
| 67 | + |
| 68 | + |
| 69 | +@when("I set the run superscript") |
| 70 | +def when_superscript(context): |
| 71 | + context.run.font.superscript = True |
| 72 | + |
| 73 | + |
| 74 | +@then("the run reports superscript true") |
| 75 | +def then_superscript(context): |
| 76 | + assert context.run.font.superscript is True |
| 77 | + |
| 78 | + |
| 79 | +@when("I set the run strike to double") |
| 80 | +def when_strike_double(context): |
| 81 | + context.run.font.strike = MSO_TEXT_STRIKE_TYPE.DOUBLE |
| 82 | + |
| 83 | + |
| 84 | +@then("the run reports strike double after round-trip") |
| 85 | +def then_strike_double(context): |
| 86 | + f2 = _roundtrip(context).paragraphs[0].runs[0].font |
| 87 | + assert f2.strike == MSO_TEXT_STRIKE_TYPE.DOUBLE |
| 88 | + |
| 89 | + |
| 90 | +@when("I set the run highlight to FFFF00") |
| 91 | +def when_highlight(context): |
| 92 | + context.run.font.highlight.rgb = RGBColor(0xFF, 0xFF, 0x00) |
| 93 | + |
| 94 | + |
| 95 | +@then("the run reports highlight FFFF00 after round-trip") |
| 96 | +def then_highlight(context): |
| 97 | + f2 = _roundtrip(context).paragraphs[0].runs[0].font |
| 98 | + assert f2.highlight.rgb == RGBColor(0xFF, 0xFF, 0x00) |
| 99 | + |
| 100 | + |
| 101 | +@when("I set the run character spacing to 2 points") |
| 102 | +def when_spacing(context): |
| 103 | + context.run.font.character_spacing = Pt(2) |
| 104 | + |
| 105 | + |
| 106 | +@then("the run reports character spacing 2 points") |
| 107 | +def then_spacing(context): |
| 108 | + assert context.run.font.character_spacing.pt == 2.0 |
| 109 | + |
| 110 | + |
| 111 | +@when("I set east_asian to MS Gothic and name to Calibri") |
| 112 | +def when_trio(context): |
| 113 | + context.run.font.east_asian = "MS Gothic" |
| 114 | + context.run.font.name = "Calibri" |
| 115 | + |
| 116 | + |
| 117 | +@then("latin is Calibri and east_asian is MS Gothic and they are independent") |
| 118 | +def then_trio(context): |
| 119 | + f2 = _roundtrip(context).paragraphs[0].runs[0].font |
| 120 | + assert f2.name == "Calibri" and f2.east_asian == "MS Gothic" |
| 121 | + assert f2.latin == "Calibri" |
| 122 | + |
| 123 | + |
| 124 | +@when("I set the text frame to 2 columns spaced 36 points") |
| 125 | +def when_columns(context): |
| 126 | + context.tf.columns = 2 |
| 127 | + context.tf.column_spacing = Pt(36) |
| 128 | + |
| 129 | + |
| 130 | +@then("the text frame reports 2 columns after round-trip") |
| 131 | +def then_columns(context): |
| 132 | + assert _roundtrip(context).columns == 2 |
| 133 | + |
| 134 | + |
| 135 | +@when("I set the text direction to east asian vertical") |
| 136 | +def when_direction(context): |
| 137 | + context.tf.text_direction = MSO_TEXT_DIRECTION.EAST_ASIAN_VERTICAL |
| 138 | + |
| 139 | + |
| 140 | +@then("the text frame reports east asian vertical after round-trip") |
| 141 | +def then_direction(context): |
| 142 | + assert _roundtrip(context).text_direction == MSO_TEXT_DIRECTION.EAST_ASIAN_VERTICAL |
| 143 | + |
| 144 | + |
| 145 | +@when("I set the paragraph to Arabic right-to-left") |
| 146 | +def when_rtl(context): |
| 147 | + p = context.tf.paragraphs[0] |
| 148 | + p.text = "اللغة العربية" |
| 149 | + p.rtl = True |
| 150 | + |
| 151 | + |
| 152 | +@then("the paragraph reports rtl true after round-trip") |
| 153 | +def then_rtl(context): |
| 154 | + p2 = _roundtrip(context).paragraphs[0] |
| 155 | + assert p2.rtl is True |
| 156 | + |
| 157 | + |
| 158 | +@then("will_overflow reports true") |
| 159 | +def then_will_overflow(context): |
| 160 | + assert context.tf.will_overflow(font_file=TEST_FONT) is True |
| 161 | + |
| 162 | + |
| 163 | +@when("I call fit_text on it") |
| 164 | +def when_fit_text(context): |
| 165 | + context.tf.fit_text(font_file=TEST_FONT) |
| 166 | + |
| 167 | + |
| 168 | +@then("no error is raised and auto_size is set") |
| 169 | +def then_fit_ok(context): |
| 170 | + assert context.tf.auto_size is not None |
| 171 | + |
| 172 | + |
| 173 | +@when("I call shrink_text_to_fit") |
| 174 | +def when_shrink(context): |
| 175 | + context.tf.shrink_text_to_fit(font_file=TEST_FONT) |
| 176 | + |
| 177 | + |
| 178 | +@then("the normAutofit fontScale is below 100") |
| 179 | +def then_shrink(context): |
| 180 | + na = context.tf._txBody.bodyPr.normAutofit |
| 181 | + assert na is not None and na.fontScale < 100 |
| 182 | + assert context.tf.auto_size == MSO_AUTO_SIZE.TEXT_TO_FIT_SHAPE |
0 commit comments