|
11 | 11 | from behave import given, then, when |
12 | 12 |
|
13 | 13 | from docx import Document |
14 | | -from docx.enum.text import WD_ALIGN_PARAGRAPH, WD_BREAK, WD_UNDERLINE |
| 14 | +from docx.enum.text import ( |
| 15 | + WD_ALIGN_PARAGRAPH, WD_BREAK, WD_LINE_SPACING, WD_UNDERLINE |
| 16 | +) |
15 | 17 | from docx.oxml import parse_xml |
16 | 18 | from docx.oxml.ns import nsdecls, qn |
17 | 19 | from docx.shared import Pt |
@@ -66,6 +68,17 @@ def given_a_font_of_size(context, size): |
66 | 68 | context.font = document.styles[style_name].font |
67 | 69 |
|
68 | 70 |
|
| 71 | +@given('a paragraph format having {setting} line spacing') |
| 72 | +def given_a_paragraph_format_having_setting_line_spacing(context, setting): |
| 73 | + style_name = { |
| 74 | + 'inherited': 'Normal', |
| 75 | + '14 pt': 'Base', |
| 76 | + 'double': 'Citation', |
| 77 | + }[setting] |
| 78 | + document = Document(test_docx('sty-known-styles')) |
| 79 | + context.paragraph_format = document.styles[style_name].paragraph_format |
| 80 | + |
| 81 | + |
69 | 82 | @given('a paragraph format having {setting} space {side}') |
70 | 83 | def given_a_paragraph_format_having_setting_spacing(context, setting, side): |
71 | 84 | style_name = 'Normal' if setting == 'inherited' else 'Base' |
@@ -238,6 +251,31 @@ def when_I_assign_value_to_font_underline(context, value_key): |
238 | 251 | font.underline = value |
239 | 252 |
|
240 | 253 |
|
| 254 | +@when('I assign {value_key} to paragraph_format.line_spacing') |
| 255 | +def when_I_assign_value_to_paragraph_format_line_spacing(context, value_key): |
| 256 | + value = { |
| 257 | + 'Pt(14)': Pt(14), |
| 258 | + '2': 2, |
| 259 | + }.get(value_key) |
| 260 | + value = float(value_key) if value is None else value |
| 261 | + context.paragraph_format.line_spacing = value |
| 262 | + |
| 263 | + |
| 264 | +@when('I assign {value_key} to paragraph_format.line_spacing_rule') |
| 265 | +def when_I_assign_value_to_paragraph_format_line_rule(context, value_key): |
| 266 | + value = { |
| 267 | + 'None': None, |
| 268 | + 'WD_LINE_SPACING.EXACTLY': WD_LINE_SPACING.EXACTLY, |
| 269 | + 'WD_LINE_SPACING.MULTIPLE': WD_LINE_SPACING.MULTIPLE, |
| 270 | + 'WD_LINE_SPACING.SINGLE': WD_LINE_SPACING.SINGLE, |
| 271 | + 'WD_LINE_SPACING.DOUBLE': WD_LINE_SPACING.DOUBLE, |
| 272 | + 'WD_LINE_SPACING.AT_LEAST': WD_LINE_SPACING.AT_LEAST, |
| 273 | + 'WD_LINE_SPACING.ONE_POINT_FIVE': WD_LINE_SPACING.ONE_POINT_FIVE, |
| 274 | + }[value_key] |
| 275 | + paragraph_format = context.paragraph_format |
| 276 | + paragraph_format.line_spacing_rule = value |
| 277 | + |
| 278 | + |
241 | 279 | @when('I assign {value_key} to font.{sub_super}script') |
242 | 280 | def when_I_assign_value_to_font_sub_super(context, value_key, sub_super): |
243 | 281 | font = context.font |
@@ -381,6 +419,36 @@ def then_paragraph_format_alignment_is_value(context, value_key): |
381 | 419 | assert paragraph_format.alignment == value |
382 | 420 |
|
383 | 421 |
|
| 422 | +@then('paragraph_format.line_spacing is {value}') |
| 423 | +def then_paragraph_format_line_spacing_is_value(context, value): |
| 424 | + value = ( |
| 425 | + None if value == 'None' else |
| 426 | + float(value) if '.' in value else |
| 427 | + int(value) |
| 428 | + ) |
| 429 | + paragraph_format = context.paragraph_format |
| 430 | + |
| 431 | + if value is None or isinstance(value, int): |
| 432 | + assert paragraph_format.line_spacing == value |
| 433 | + else: |
| 434 | + assert abs(paragraph_format.line_spacing - value) < 0.001 |
| 435 | + |
| 436 | + |
| 437 | +@then('paragraph_format.line_spacing_rule is {value_key}') |
| 438 | +def then_paragraph_format_line_spacing_rule_is_value(context, value_key): |
| 439 | + value = { |
| 440 | + 'None': None, |
| 441 | + 'WD_LINE_SPACING.EXACTLY': WD_LINE_SPACING.EXACTLY, |
| 442 | + 'WD_LINE_SPACING.MULTIPLE': WD_LINE_SPACING.MULTIPLE, |
| 443 | + 'WD_LINE_SPACING.SINGLE': WD_LINE_SPACING.SINGLE, |
| 444 | + 'WD_LINE_SPACING.DOUBLE': WD_LINE_SPACING.DOUBLE, |
| 445 | + 'WD_LINE_SPACING.AT_LEAST': WD_LINE_SPACING.AT_LEAST, |
| 446 | + 'WD_LINE_SPACING.ONE_POINT_FIVE': WD_LINE_SPACING.ONE_POINT_FIVE, |
| 447 | + }[value_key] |
| 448 | + paragraph_format = context.paragraph_format |
| 449 | + assert paragraph_format.line_spacing_rule == value |
| 450 | + |
| 451 | + |
384 | 452 | @then('paragraph_format.space_{side} is {value}') |
385 | 453 | def then_paragraph_format_space_side_is_value(context, side, value): |
386 | 454 | expected_value = None if value == 'None' else int(value) |
|
0 commit comments