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 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 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 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 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 stacking(msp, location): attribs = dict(ATTRIBS) attribs["char_height"] = 0.25 attribs["width"] = 4 editor = MTextEditor("Stacked text:" + NP) # place fraction with down scaled text height in a group: stack = MTextEditor().scale_height(0.6).stack("1", "2", "^") editor.append("over: ").group(str(stack)).append(NP) stack = MTextEditor().scale_height(0.6).stack("1", "2", "/") editor.append("fraction: ").group(str(stack)).append(NP) stack = MTextEditor().scale_height(0.6).stack("1", "2", "#") editor.append("slanted: ").group(str(stack)).append(NP) # additional formatting in numerator and denominator is not supported # by AutoCAD or BricsCAD. # switching colors inside the fraction to red does not work: numerator = MTextEditor().color("red").append("1") stack = MTextEditor().scale_height(0.6).stack(str(numerator), "2", "#") editor.append("color red: ").group(str(stack)).append(NP) msp.add_mtext(str(editor), attribs).set_location(insert=location)
def changing_fonts(msp, location): attribs = dict(ATTRIBS) attribs["width"] = 15.0 editor = MTextEditor("changing fonts:" + NP) editor.append("Default: Hello World!" + NP) editor.append("SimSun: ") # The font name for changing MTEXT fonts inline is the font family name! # The font family name is the name shown in font selection widgets in # desktop applications: "Arial", "Times New Roman", "Comic Sans MS" # # change font in a group to revert back to the default font at the end: simsun_editor = MTextEditor().font("SimSun").append("你好,世界" + NP) # reverts the font back at the end of the group: editor.group(str(simsun_editor)) # back to default font OpenSans: editor.append("Times New Roman: ") # change font outside of a group until next font change: editor.font("Times New Roman").append("Привет мир!" + NP) # If the font does not exist, a replacement font will be used: editor.font("Does not exist").append("This is the replacement font!") msp.add_mtext(str(editor), attribs).set_location(insert=location)
def test_append_text(): m = MTextEditor() m.append("TEXT") assert str(m) == "TEXT"