def changing_text_height_absolute(msp, location): attribs = dict(ATTRIBS) attribs["width"] = 40.0 # need mor space to avoid text wrapping editor = MTextEditor( "changing text height absolute: default height is 0.7" + NP) # doubling the default height = 1.4 editor.height(1.4) editor.append("text height: 1.4" + NP) editor.height(3.5).append("text height: 3.5" + NP) editor.height(0.7).append("back to default height: 0.7" + NP) msp.add_mtext(str(editor), attribs).set_location(insert=location)
def changing_text_height_absolute(msp, location): attribs = dict(ATTRIBS) attribs["width"] = 40.0 # need mor space to avoid text wrapping editor = MTextEditor( "changing text height absolute: default height is 0.7" + NP) # this is the default text height in the beginning: # The text height can only be changed by a factor: editor.height(1.4) # scale by 2 = 1.4 editor.append("text height: 1.4" + NP) editor.height(3.5).append("text height: 3.5" + NP) editor.height(0.7).append("back to default height: 0.7" + NP) msp.add_mtext(str(editor), attribs).set_location(insert=location)
def using_colors(msp, location): attribs = dict(ATTRIBS) attribs["width"] = 10.0 editor = MTextEditor("using colors:" + NP) # Change colors by name: red, green, blue, yellow, cyan, magenta, white editor.color("red").append("RED" + NP) # The color stays the same until changed editor.append("also RED" + NP) # Change color by ACI (AutoCAD Color Index) editor.aci(3).append("GREEN" + NP) # Change color by RGB tuples editor.rgb((0, 0, 255)).append("BLUE" + NP) msp.add_mtext(str(editor), attribs).set_location(insert=location)
def test_change_color_name(): m = MTextEditor() m.color("red") assert str(m) == r"\C1;" m.clear() m.aci(0) assert str(m) == r"\C0;"
def test_bullet_lists(): result = MTextEditor().bullet_list( indent=4, # left indentation of the list items bullets=["-", "+"], # bullets - mark in front of the list item content=["first", "second"], # list items ) assert str(result) == r"{\pxi-3,l4,t4;-^Ifirst\P+^Isecond\P}"
def recreate_mtext_py_example(msp, location): # replicate example "mtext.py": attribs = dict(ATTRIBS) attribs["width"] = 15.0 editor = MTextEditor(f"recreate mtext.py result:{NP}normal ").overline( "over line").append(" normal" + NP + "normal ").strike_through( "strike through").append(" normal" + NP).underline( "under line").append(" normal") msp.add_mtext(str(editor), attribs).set_location(insert=location)
def numbered_list(msp, location): attribs = dict(ATTRIBS) attribs["char_height"] = 0.25 attribs["width"] = 7.5 # There are no special commands to build numbered list, the list is build of # indentation and a tabulator stop. There is no automatic numbering, # but therefore the absolute freedom for using any string as list marker: editor = MTextEditor("Numbered List:" + NP) editor.bullet_list(indent=1, bullets=["1.", "2.", "3."], content=[ "First item", "Second item", " ".join(lorem_ipsum(30)), ]) # Indentation and tab stops are multiples of the default text height (MTEXT # char_height)! msp.add_mtext(str(editor), attribs).set_location(insert=location)
def bullet_list(msp, location): attribs = dict(ATTRIBS) attribs["char_height"] = 0.25 attribs["width"] = 7.5 # There are no special commands to build bullet list, the list is build of # indentation and a tabulator stop. Each list item needs a marker as an # arbitrary string. bullet = "•" # alt + numpad 7 editor = MTextEditor("Bullet List:" + NP) editor.bullet_list( indent=1, bullets=[bullet] * 3, # each list item needs a marker content=[ "First item", "Second item", " ".join(lorem_ipsum(30)), ]) msp.add_mtext(str(editor), attribs).set_location(insert=location)
def changing_text_height_relative(msp, location): attribs = dict(ATTRIBS) attribs["width"] = 40.0 # need mor space to avoid text wrapping editor = MTextEditor( "changing text height relative: default height is 0.7" + NP) # this is the default text height in the beginning: current_height = attribs["char_height"] # The text height can only be changed by a factor: editor.scale_height(2) # scale by 2 = 1.4 # keep track of the actual height: current_height *= 2 editor.append("text height: 1.4" + NP) # to set an absolute height, calculate the required factor: desired_height = 3.5 factor = desired_height / current_height editor.scale_height(factor).append("text height: 3.5" + NP) current_height = desired_height # and back to 0.7 editor.scale_height( 0.7 / current_height).append("back to default height: 0.7" + NP) msp.add_mtext(str(editor), attribs).set_location(insert=location)
def indent_first_line(msp, location): # Indentation is a multiple of the default text height (MTEXT char_height) attribs = dict(ATTRIBS) attribs["char_height"] = 0.25 attribs["width"] = 7.5 editor = MTextEditor("Indent the first line:" + NP) props = ParagraphProperties( indent=1, # indent first line = 1x0.25 drawing units align=MTextParagraphAlignment.JUSTIFIED) editor.paragraph(props) editor.append(" ".join(lorem_ipsum(100))) msp.add_mtext(str(editor), attribs).set_location(insert=location)
def indent_except_fist_line(msp, location): # Indentation is a multiple of the default text height (MTEXT char_height) attribs = dict(ATTRIBS) attribs["char_height"] = 0.25 attribs["width"] = 7.5 editor = MTextEditor("Indent left paragraph side:" + NP) indent = 0.7 # 0.7 * 0.25 = 0.175 drawing units props = ParagraphProperties( # first line indentation is relative to "left", this reverses the # left indentation: indent=-indent, # first line # indent left paragraph side: left=indent, align=MTextParagraphAlignment.JUSTIFIED) editor.paragraph(props) editor.append(" ".join(lorem_ipsum(100))) msp.add_mtext(str(editor), attribs).set_location(insert=location)
def test_change_font(): m = MTextEditor() m.font("Arial", bold=False, italic=False) assert str(m) == r"\fArial|b0|i0;"
def test_change_to_blue_by_rgb(): m = MTextEditor().rgb((0, 0, 255)) assert str(m) == r"\c16711680;"
def test_append_text(): m = MTextEditor() m.append("TEXT") assert str(m) == "TEXT"
def test_change_to_green_by_rgb(): m = MTextEditor().rgb((0, 255, 0)) assert str(m) == r"\c65280;"
def test_change_to_red_by_rgb(): m = MTextEditor().rgb((255, 0, 0)) assert str(m) == r"\c255;"
def test_aci_color_raises_value_error(aci): with pytest.raises(ValueError): MTextEditor().aci(aci)
def test_change_aci_color(): m = MTextEditor() m.aci(0).aci(256) assert str(m) == r"\C0;\C256;"
def test_underline_text(): assert str(MTextEditor().underline("TEXT")) == r"\LTEXT\l"
def test_invalid_divider_char_raises_value_error(): m = MTextEditor() pytest.raises(ValueError, m.stack, "1", "2", "x")
def test_stacked_text_with_slanted_divider_line(): m = MTextEditor().stack("1", "2", "#") assert str(m) == r"\S1#2;" # no space after "#" required
def test_change_width_factor(): assert str(MTextEditor().width_factor(2)) == r"\W2;" assert str(MTextEditor().width_factor(1.6666)) == r"\W1.667;"
def test_scale_height_factor(): assert str(MTextEditor().scale_height(2)) == r"\H2x;" assert str(MTextEditor().scale_height(1.6666)) == r"\H1.667x;"
def test_iadd_text(): m = MTextEditor() m += "TEXT" assert str(m) == "TEXT"
def test_strike_through_text(): assert str(MTextEditor().strike_through("TEXT")) == r"\KTEXT\k"
def test_overline_text(): assert str(MTextEditor().overline("TEXT")) == r"\OTEXT\o"
def test_absolute_text_height(): assert str(MTextEditor().height(2)) == r"\H2;" assert str(MTextEditor().height(1.6666)) == r"\H1.667;"
def test_stacked_text_limits_style(): m = MTextEditor().stack("1", "2") assert str(m) == r"\S1^ 2;"
def test_change_char_tracking_factor(): assert str(MTextEditor().char_tracking_factor(2)) == r"\T2;" assert str(MTextEditor().char_tracking_factor(1.6666)) == r"\T1.667;"
def test_stacked_text_with_horizontal_divider_line(): m = MTextEditor().stack("1", "2", "/") assert str(m) == r"\S1/2;" # no space after "/" required