def add_atom(self, a): """adds atom item to SVG document""" if a.show: item = a.selector x1, y1 = a.get_xy() x, y, x2, y2 = self.paper.bbox(item) if a.area_color != '': # it is not needed to export the rectangle in case its transparent dom_extensions.elementUnder( self.group, 'rect', (('x', self.convert(x)), ('y', self.convert(y)), ('width', self.convert(x2 - x)), ('height', self.convert(y2 - y)), ('fill', self.cc(a.area_color)), ('stroke', self.cc(a.area_color)))) # some fine tuning y1 += a.font.metrics('descent') + Tuning.SVG.text_y_shift x += Tuning.SVG.text_x_shift ## hack to compensate for the wrong measuring of text text = ftext_to_svg_dom(a.xml_ftext) dom_extensions.setAttributes( text, (("x", self.convert(x)), ("y", self.convert(y1)), ("font-family", a.font_family), ("font-size", '%d%s' % (a.font_size, pt_or_px)), ('fill', self.cc(a.line_color)))) # set the text length but only for text longer than threshold if (x2 - x) > 50: text.setAttribute('textLength', "%.1f" % (x2 - x)) self.group.appendChild(text) if hasattr(a, "marks"): for m in a.marks: self.group.appendChild(m.get_svg_element(self.document))
def add_text(self, t): """Add text item to SVG document. """ x1, y1 = t.get_xy() x, y, x2, y2 = t.ftext.bbox( complete=True) _x, y, _x2, y2 = t.ftext.bbox( complete=False) if t.area_color: # it is not needed to export the rectangle in case its transparent dom_extensions.elementUnder( self.group, 'rect', (( 'x', self.convert( x)), ( 'y', self.convert( y)), ( 'width', self.convert( x2-x)), ( 'height', self.convert( y2-y)), ( 'fill', self.cc( t.area_color)), ( 'stroke', self.cc( t.area_color)))) y1 += (y2-y)/4.0 x += 2 ## hack to compensate for the wrong measuring of text text = ftext_dom_to_svg_dom(dom.parseString(t.ftext.sanitized_text().encode('utf-8')), self.document, replace_minus=t.paper.get_paper_property('replace_minus')) dom_extensions.setAttributes( text, (( "x", self.convert( x)), ( "y", self.convert( y1)), ( "font-family", t.font_family), ( "font-size", '%d%s' % (t.font_size, pt_or_px)), ( 'fill', self.cc( t.line_color)), ( 'textLength', "%d" % (x2-x+len( [1 for i in t.xml_ftext if i=="-" and t.paper.get_paper_property( 'replace_minus')]))))) self.group.appendChild( text)
def add_atom( self, a): """adds atom item to SVG document""" if a.show: item = a.selector x1, y1 = a.get_xy() x, y, x2, y2 = self.paper.bbox( item) if a.area_color != '': # it is not needed to export the rectangle in case its transparent dom_extensions.elementUnder( self.group, 'rect', (( 'x', self.convert( x)), ( 'y', self.convert( y)), ( 'width', self.convert( x2-x)), ( 'height', self.convert( y2-y)), ( 'fill', self.cc( a.area_color)), ( 'stroke', self.cc( a.area_color)))) # some fine tuning y1 += a.font.metrics('descent') + Tuning.SVG.text_y_shift x += Tuning.SVG.text_x_shift ## hack to compensate for the wrong measuring of text text = ftext_to_svg_dom( a.xml_ftext) dom_extensions.setAttributes( text, (( "x", self.convert( x)), ( "y", self.convert( y1)), ( "font-family", a.font_family), ( "font-size", '%d%s' % (a.font_size, pt_or_px)), ( 'fill', self.cc( a.line_color)))) # set the text length but only for text longer than threshold if (x2-x) > 50: text.setAttribute( 'textLength', "%.1f" % (x2-x)) self.group.appendChild( text) if hasattr( a, "marks"): for m in a.marks: self.group.appendChild( m.get_svg_element( self.document))
def add_text(self, t): """Add text item to SVG document. """ x1, y1 = t.get_xy() x, y, x2, y2 = t.ftext.bbox(complete=True) _x, y, _x2, y2 = t.ftext.bbox(complete=False) if t.area_color: # it is not needed to export the rectangle in case its transparent dom_extensions.elementUnder(self.group, 'rect', (('x', self.convert(x)), ('y', self.convert(y)), ('width', self.convert(x2 - x)), ('height', self.convert(y2 - y)), ('fill', self.cc(t.area_color)), ('stroke', self.cc(t.area_color)))) y1 += (y2 - y) / 4.0 x += 2 ## hack to compensate for the wrong measuring of text text = ftext_dom_to_svg_dom( dom.parseString(t.ftext.sanitized_text().encode('utf-8')), self.document, replace_minus=t.paper.get_paper_property('replace_minus')) dom_extensions.setAttributes( text, (("x", self.convert(x)), ("y", self.convert(y1)), ("font-family", t.font_family), ("font-size", '%d%s' % (t.font_size, pt_or_px)), ('fill', self.cc(t.line_color)), ('textLength', "%d" % (x2 - x + len([ 1 for i in t.xml_ftext if i == "-" and t.paper.get_paper_property('replace_minus') ]))))) self.group.appendChild(text)
def get_package(self, doc): """returns a DOM element describing the object in CDML, doc is the parent document which is used for element creation (the returned element is not inserted into the document)""" ret = doc.createElement('standard') dom_extensions.setAttributes( ret, (('line_width', str(self.line_width)), ('font_size', str(self.font_size)), ('font_family', str(self.font_family)), ('line_color', self.line_color), ('area_color', self.area_color), ('paper_type', self.paper_type), ('paper_orientation', self.paper_orientation), ('paper_crop_svg', str(self.paper_crop_svg)), ('paper_crop_margin', str(self.paper_crop_margin)))) dom_extensions.elementUnder( ret, 'bond', (('length', str(self.bond_length)), ('width', str(self.bond_width)), ('wedge-width', str(self.wedge_width)), ('double-ratio', str(self.double_length_ratio)))) dom_extensions.elementUnder(ret, 'arrow', (('length', str(self.arrow_length)), )) dom_extensions.elementUnder( ret, 'atom', (('show_hydrogens', str(int(self.show_hydrogens))), )) return ret
def get_package( self, doc): """returns a DOM element describing the object in CDML, doc is the parent document which is used for element creation (the returned element is not inserted into the document)""" b = ['no', 'yes'] bnd = doc.createElement('bond') dom_extensions.setAttributes( bnd, (('type', "%s%d" % (self.type, self.order)), ('line_width', str( self.line_width)), ('start', self.atom1.id), ('end', self.atom2.id), ('id', str( self.id)), ('double_ratio', str( self.double_length_ratio)))) if hasattr( self, 'equithick') and self.equithick: bnd.setAttribute( 'equithick', str(1)) if self.order != 1: bnd.setAttribute( 'bond_width', str( self.bond_width * self.paper.screen_to_real_ratio())) if self.order == 2: bnd.setAttribute( 'center', b[ int( self.center)]) if self.auto_bond_sign != 1: bnd.setAttribute( 'auto_sign', str( self.auto_bond_sign)) if self.type != 'n': bnd.setAttribute( 'wedge_width', str( self.wedge_width * self.paper.screen_to_real_ratio())) if self.line_color != '#000': bnd.setAttribute( 'color', self.line_color) if self.type != 'n' and self.order != 1: bnd.setAttribute( 'simple_double', str( int( self.simple_double))) return bnd
def to_dom( self, doc): style = doc.createElement( 'style:style') dom_extensions.setAttributes( style, (('style:family', self.family), ('style:name', self.name))) prop = dom_extensions.elementUnder( style, 'style:properties', (( 'fo:font-size', self.font_size), ( 'fo:font-family', self.font_family), ( 'fo:font-style', self.font_style), ( 'fo:font-weight', self.font_weight))) return style
def to_dom( self, doc): style = doc.createElement( 'style:style') dom_extensions.setAttributes( style, (('style:family', self.family), ('style:name', self.name))) dom_extensions.elementUnder( style, 'style:properties', (( 'fo:font-size', self.font_size), ( 'fo:font-family', self.font_family), ( 'fo:text-align', 'center'), ( 'fo:color', self.color))) return style
def to_dom( self, doc): style = doc.createElement( 'style:style') dom_extensions.setAttributes( style, (('style:family', self.family), ('style:name', self.name))) prop = dom_extensions.elementUnder( style, 'style:text-properties', (( 'fo:font-size', self.font_size), ( 'fo:font-family', self.font_family), ( 'fo:font-style', self.font_style), ( 'fo:font-weight', self.font_weight))) return style
def get_package(self, doc): """returns a DOM element describing the object in CDML, doc is the parent document which is used for element creation (the returned element is not inserted into the document)""" pnt = doc.createElement('point') x, y = map(Screen.px_to_text_with_unit, self.paper.screen_to_real_coords((self.x, self.y))) dom_extensions.setAttributes(pnt, (('x', x), ('y', y))) return pnt
def get_package( self, doc): """returns a DOM element describing the object in CDML, doc is the parent document which is used for element creation (the returned element is not inserted into the document)""" pnt = doc.createElement('point') x, y = map( Screen.px_to_text_with_unit, self.paper.screen_to_real_coords( (self.x, self.y))) dom_extensions.setAttributes( pnt, (('x', x), ('y', y))) return pnt
def to_dom(self, doc): style = doc.createElement('style:style') dom_extensions.setAttributes(style, (('style:family', self.family), ('style:name', self.name))) dom_extensions.elementUnder(style, 'style:properties', (('fo:font-size', self.font_size), ('fo:font-family', self.font_family), ('fo:text-align', 'center'), ('fo:color', self.color))) return style
def get_package( self, doc): """returns a DOM element describing the object in CDML, doc is the parent document which is used for element creation (the returned element is not inserted into the document)""" pack = doc.createElement( 'polygon') dom_extensions.setAttributes( pack, (('area_color', self.area_color), ('line_color', self.line_color), ('width', str( self.line_width)))) for p in self.points: pack.appendChild( p.get_package( doc)) return pack
def get_svg_element( self, doc): e = doc.createElement( 'ellipse') dom_extensions.setAttributes( e, (( 'cx', str( self.x)), ( 'cy', str( self.y)), ( 'rx', str( self.size /2)), ( 'ry', str( self.size /2)), ( 'fill', self.line_color), ( 'stroke', self.line_color), ( 'stroke-width', '1'))) return e
def transform_CML_bond( self, bond, doc): """called by transform_bond in order to do the transform from CML_bond instance to CDML""" if bond.not_enough_data(): # bond does not have sufficient data to be safely converted to cdml raise plugin.import_exception( "missing "+str( bond.not_enough_data())+" in bond specification") out = doc.createElement( 'bond') type = (bond.stereo.lower() or 'n') + str( bond.order) dom_ext.setAttributes( out, (('type', type), ('start', bond.atom1), ('end', bond.atom2))) return out
def to_dom( self, doc): style = doc.createElement( 'style:style') dom_extensions.setAttributes( style, (('style:family', self.family), ('style:name', self.name))) dom_extensions.elementUnder( style, 'style:paragraph-properties', (#( 'fo:font-size', self.font_size), #( 'fo:font-family', self.font_family), ( 'fo:text-align', 'center'), #( 'fo:color', self.color), ( 'fo:margin-left', "0cm"), ( 'fo:margin-right', "0cm"), ( 'fo:text-indent', "0cm"))) return style
def get_svg_element( self, doc): e = doc.createElement( 'line') x1, y1, x2, y2 = self.paper.coords( self.items[0]) dom_extensions.setAttributes( e, (( 'x1', str( x1)), ( 'y1', str( y1)), ( 'x2', str( x2)), ( 'y2', str( y2)), ( 'stroke-width', str( self.line_width)), ( 'fill', self.line_color), ( 'stroke', self.line_color))) return e
def get_package(self, doc): """returns a DOM element describing the object in CDML, doc is the parent document which is used for element creation (the returned element is not inserted into the document)""" pack = doc.createElement('circle') x1, y1, x2, y2 = Screen.px_to_text_with_unit( self.paper.screen_to_real_coords(self.coords)) dom_extensions.setAttributes( pack, (('x1', x1), ('y1', y1), ('x2', x2), ('y2', y2), ('area_color', self.area_color), ('line_color', self.line_color), ('width', str(self.line_width)))) return pack
def to_dom(self, doc): style = doc.createElement('style:style') dom_extensions.setAttributes(style, (('style:family', self.family), ('style:name', self.name))) prop = dom_extensions.elementUnder(style, 'style:properties') if self.font_style: prop.setAttribute('fo:font-style', self.font_style) if self.font_weight: prop.setAttribute('fo:font-weight', self.font_weight) if self.text_position: prop.setAttribute('style:text-position', self.text_position) return style
def to_dom( self, doc): style = doc.createElement( 'style:style') dom_extensions.setAttributes( style, (('style:family', self.family), ('style:name', self.name))) prop = dom_extensions.elementUnder( style, 'style:properties') if self.font_style: prop.setAttribute( 'fo:font-style', self.font_style) if self.font_weight: prop.setAttribute( 'fo:font-weight', self.font_weight) if self.text_position: prop.setAttribute( 'style:text-position', self.text_position) return style
def get_package( self, doc): """returns a DOM element describing the object in CDML, doc is the parent document which is used for element creation (the returned element is not inserted into the document)""" pack = doc.createElement( 'circle') x1, y1, x2, y2 = Screen.px_to_text_with_unit( self.paper.screen_to_real_coords( self.coords)) dom_extensions.setAttributes( pack, (('x1', x1), ('y1', y1), ('x2', x2), ('y2', y2), ('area_color', self.area_color), ('line_color', self.line_color), ('width', str( self.line_width)))) return pack
def ftext_dom_to_svg_dom( ftext, doc, add_to=None, replace_minus=False): if not add_to: element = doc.createElement( 'text') else: element = add_to if not ftext.nodeValue: name = ftext.nodeName # check if to add attributes to already existing element or create a new one if (not element.lastChild and element.nodeName == "tspan") or name == "ftext": my_svg = element else: my_svg = doc.createElement( 'tspan') element.appendChild( my_svg) # now put the attributes inside if name == 'b': dom_extensions.setAttributes( my_svg, (('font-weight', 'bold'),)) elif name == 'i': dom_extensions.setAttributes( my_svg, (('font-style', 'italic'),)) elif name == 'sup': dom_extensions.setAttributes( my_svg, (('baseline-shift', 'super'),('font-size','75%'))) elif name == 'sub': dom_extensions.setAttributes( my_svg, (('baseline-shift', 'sub'),('font-size','75%'))) # continue with the children for el in ftext.childNodes: ftext_dom_to_svg_dom( el, doc, add_to=my_svg) else: if replace_minus: element.appendChild( doc.createTextNode( ftext.nodeValue.replace( "-", unichr( 8722)))) else: element.appendChild( doc.createTextNode( ftext.nodeValue)) return element
def get_package( self, doc): a = doc.createElement('mark') x ,y = map( Screen.px_to_text_with_unit, (self.x, self.y)) dom_extensions.setAttributes( a, (('type', self.__class__.__name__), ('x', x), ('y', y), ('auto', str( int( self.auto))), ('size', str( self.size)))) for (attr, typ) in self.meta__save_attrs.items(): val = getattr( self, attr) if typ == bool: value = data.booleans[ int( val)] else: value = str( val) a.setAttribute( attr, value) return a
def get_CML_dom(self, doc): if self.not_enough_data(): return None # raise cml_exception( "missing "+str( self.not_enough_data())+" in atom specification") out = doc.createElement('atom') #id if self.z is None: dom_ext.setAttributes(out, (('id', str(self.id)), ('x2', str(self.x)), ('y2', str(self.y)), ('elementType', self.symbol))) else: dom_ext.setAttributes(out, (('id', str(self.id)), ('x3', str(self.x)), ('y3', str(self.y)), ('z3', str(self.z)), ('elementType', self.symbol))) if self.charge: out.setAttribute('formalCharge', str(self.charge)) return out
def get_CML_dom( self, doc): if self.not_enough_data(): return None # raise cml_exception( "missing "+str( self.not_enough_data())+" in atom specification") out = doc.createElement( 'atom') #id if self.z is None: dom_ext.setAttributes( out, (('id', str( self.id)), ('x2', str( self.x)), ('y2', str( self.y)), ('elementType', self.symbol))) else: dom_ext.setAttributes( out, (('id', str( self.id)), ('x3', str( self.x)), ('y3', str( self.y)), ('z3', str( self.z)), ('elementType', self.symbol))) if self.charge: out.setAttribute( 'formalCharge', str( self.charge)) return out
def get_package(self, doc): """returns a DOM element describing the object in CDML, doc is the parent document which is used for element creation (the returned element is not inserted into the document)""" a = ['no', 'yes'] arr = doc.createElement('arrow') arr.setAttribute('id', self.id) start, end = 0, 0 if self.pin == 2 or self.pin == 3: start = 1 if self.pin == 1 or self.pin == 3: end = 1 dom_extensions.setAttributes( arr, (('shape', str(self.shape)), ('spline', a[self.spline]), ('width', str(self.line_width)), ('start', a[start]), ('end', a[end]), ('color', str(self.line_color)), ('type', self.type))) for p in self.points: arr.appendChild(p.get_package(doc)) return arr
def transform_atom( self, a, doc): out = doc.createElement( 'atom') try: atom = self.CML_atom( cml=a) except cml_exception as detail: # problems converting cml to atom raise plugin.import_exception( detail) if atom.not_enough_data(): # atom does not have sufficient data to be safely converted to cdml raise plugin.import_exception( "missing "+str( atom.not_enough_data())+" in atom specification") self.atoms.append( atom) dom_ext.setAttributes( out, (('name', atom.symbol), ('id', atom.id))) pnt = dom_ext.elementUnder( out, 'point', (('x', str( atom.x)), ('y', str( atom.y)))) if atom.z is not None: pnt.setAttribute( 'z', str( atom.z)) self.xs.append( atom.x) self.ys.append( atom.y) return out
def get_package( self, doc): """returns a DOM element describing the object in CDML, doc is the parent document which is used for element creation (the returned element is not inserted into the document)""" ret = doc.createElement( 'standard') dom_extensions.setAttributes( ret, (('line_width', str( self.line_width)), ('font_size', str( self.font_size)), ('font_family', str( self.font_family)), ('line_color', self.line_color), ('area_color', self.area_color), ('paper_type', self.paper_type), ('paper_orientation', self.paper_orientation), ('paper_crop_svg', str( self.paper_crop_svg)), ('paper_crop_margin', str( self.paper_crop_margin)))) dom_extensions.elementUnder( ret, 'bond', (('length', str( self.bond_length)), ('width', str( self.bond_width)), ('wedge-width', str( self.wedge_width)), ('double-ratio', str( self.double_length_ratio)))) dom_extensions.elementUnder( ret, 'arrow', (('length', str( self.arrow_length)),)) dom_extensions.elementUnder( ret, 'atom', (('show_hydrogens', str( int( self.show_hydrogens))),)) return ret
def get_package( self, doc): """returns a DOM element describing the object in CDML, doc is the parent document which is used for element creation (the returned element is not inserted into the document)""" a = ['no', 'yes'] arr = doc.createElement('arrow') arr.setAttribute( 'id', self.id) start, end = 0, 0 if self.pin == 2 or self.pin == 3: start = 1 if self.pin == 1 or self.pin ==3: end = 1 dom_extensions.setAttributes( arr, (('shape', str( self.shape)), ('spline', a[self.spline]), ('width', str( self.line_width)), ('start', a[start]), ('end', a[end]), ('color', str( self.line_color)), ('type', self.type))) for p in self.points: arr.appendChild( p.get_package( doc)) return arr
def to_dom( self, doc): style = doc.createElement( 'style:style') dom_extensions.setAttributes( style, (('style:family', self.family), ('style:name', self.name), ('style:parent-style-name','standard'))) pad_top = "%scm" % self.padding_top pad_bot = "%scm" % self.padding_bottom pad_left = "%scm" % self.padding_left pad_right = "%scm" % self.padding_right prop = dom_extensions.elementUnder( style, 'style:properties', (( 'draw:fill', self.fill), ( 'svg:stroke-color', self.stroke_color), ( 'draw:fill-color', self.fill_color), ( 'svg:stroke-width', '%fcm' % self.stroke_width), ( 'draw:auto-grow-width', 'true'), ( 'draw:auto-grow-height', 'true'), ( 'draw:textarea-horizontal-align','middle'), ( 'draw:textarea-vertical-align','middle'), ( 'fo:padding-top', pad_top), ( 'fo:padding-bottom', pad_bot), ( 'fo:padding-left', pad_left), ( 'fo:padding-right', pad_right))) if self.marker_end: prop.setAttribute( 'draw:marker-end', 'Arrow') if self.marker_end_width: prop.setAttribute( 'draw:marker-end-width', "%dcm" % self.marker_end_width) if self.marker_start: prop.setAttribute( 'draw:marker-start', 'Arrow') if self.marker_start_width: prop.setAttribute( 'draw:marker-start-width', "%dcm" % self.marker_start_width) if not self.stroke_width: prop.setAttribute('draw:stroke', 'none') else: prop.setAttribute('draw:stroke', self.stroke_color) return style
def to_dom(self, doc): style = doc.createElement('style:style') dom_extensions.setAttributes(style, (('style:family', self.family), ('style:name', self.name), ('style:parent-style-name', 'standard'))) pad_top = "%scm" % self.padding_top pad_bot = "%scm" % self.padding_bottom pad_left = "%scm" % self.padding_left pad_right = "%scm" % self.padding_right prop = dom_extensions.elementUnder( style, 'style:properties', (('draw:fill', self.fill), ('svg:stroke-color', self.stroke_color), ('draw:fill-color', self.fill_color), ('svg:stroke-width', '%fcm' % self.stroke_width), ('draw:auto-grow-width', 'true'), ('draw:auto-grow-height', 'true'), ('draw:textarea-horizontal-align', 'middle'), ('draw:textarea-vertical-align', 'middle'), ('fo:padding-top', pad_top), ('fo:padding-bottom', pad_bot), ('fo:padding-left', pad_left), ('fo:padding-right', pad_right))) if self.marker_end: prop.setAttribute('draw:marker-end', 'Arrow') if self.marker_end_width: prop.setAttribute('draw:marker-end-width', "%dcm" % self.marker_end_width) if self.marker_start: prop.setAttribute('draw:marker-start', 'Arrow') if self.marker_start_width: prop.setAttribute('draw:marker-start-width', "%dcm" % self.marker_start_width) if not self.stroke_width: prop.setAttribute('draw:stroke', 'none') else: prop.setAttribute('draw:stroke', self.stroke_color) return style
def ftext_dom_to_svg_dom(ftext, doc, add_to=None, replace_minus=False): if not add_to: element = doc.createElement('text') else: element = add_to if not ftext.nodeValue: name = ftext.nodeName # check if to add attributes to already existing element or create a new one if (not element.lastChild and element.nodeName == "tspan") or name == "ftext": my_svg = element else: my_svg = doc.createElement('tspan') element.appendChild(my_svg) # now put the attributes inside if name == 'b': dom_extensions.setAttributes(my_svg, (('font-weight', 'bold'), )) elif name == 'i': dom_extensions.setAttributes(my_svg, (('font-style', 'italic'), )) elif name == 'sup': dom_extensions.setAttributes(my_svg, (('baseline-shift', 'super'), ('font-size', '75%'))) elif name == 'sub': dom_extensions.setAttributes(my_svg, (('baseline-shift', 'sub'), ('font-size', '75%'))) # continue with the children for el in ftext.childNodes: ftext_dom_to_svg_dom(el, doc, add_to=my_svg) else: if replace_minus: element.appendChild( doc.createTextNode(ftext.nodeValue.replace("-", unichr(8722)))) else: element.appendChild(doc.createTextNode(ftext.nodeValue)) return element
def construct_dom_tree( self, top_levels): """constructs the SVG dom from all top_levels""" # the constants border_size = self.paper.get_paper_property( 'crop_margin') # converter px_to_cm_txt = lambda x: Screen.px_to_text_with_unit( x, unit="cm", round_to=5) px_to_mm_txt = lambda x: Screen.px_to_text_with_unit( x, unit="mm", round_to=5) # the code self._id = 0 doc = self.document self.top = dom_extensions.elementUnder( doc, "svg", attributes=(("xmlns", "http://www.w3.org/2000/svg"), ("version", "1.0"))) if self.full_size: sx = self.paper.get_paper_property( 'size_x') sy = self.paper.get_paper_property( 'size_y') dom_extensions.setAttributes( self.top, (("width", '%fmm' % sx), ("height", '%fmm' % sy), ('viewBox', '0 0 %d %d' % (Screen.mm_to_px(sx), Screen.mm_to_px(sy))))) else: items = list( self.paper.find_all()) items.remove( self.paper.background) x1, y1, x2, y2 = self.paper.list_bbox( items) w = px_to_mm_txt( x2 -x1 +2*border_size) h = px_to_mm_txt( y2 -y1 +2*border_size) bx2, by2 = x2-x1+2*border_size, y2-y1+2*border_size dom_extensions.setAttributes( self.top, (("width", w), ("height", h), ("viewBox",'0 0 %d %d' % ( bx2, by2)))) self.group = dom_extensions.elementUnder( self.top, 'g', (('font-size', '12pt'), ('font-family', 'Helvetica'), ('stroke-linecap', 'round'))) if not self.full_size: self.group.setAttribute( 'transform', 'translate(%d,%d)' % (-x1+border_size, -y1+border_size)) # sort the top_levels according to paper.stack cs = [] for c in self.paper.stack: if c in top_levels: cs.append( c) for o in cs: if o.object_type == 'molecule': for b in o.bonds: self.add_bond( b) for a in o.atoms: self.add_atom( a) elif o.object_type == 'arrow': self.add_arrow( o) elif o.object_type == 'text': self.add_text( o) elif o.object_type == 'plus': self.add_plus( o) elif o.object_type in data.vector_graphics_types: if o.object_type == 'rect': self.add_rect( o) elif o.object_type == 'oval': self.add_oval( o) elif o.object_type == 'polygon': self.add_polygon( o) elif o.object_type == 'polyline': self.add_polyline( o)
class CML_importer( plugin.importer): """Imports a CML (Chemical Markup Language) document, uses version 1.0 of the CML standard.""" doc_string = _("Imports a CML (Chemical Markup Language) document, uses version 1.0 of the CML standard.") def __init__( self): # this makes implementing CML2 much easier - just supply different CML_atom and CML_bond self.CML_atom = CML_atom self.CML_bond = CML_bond self.xs = [] self.ys = [] def on_begin( self): self.xs = [] self.ys = [] self.atoms = [] return 1 def get_cdml_dom( self, file_name): tree = dom.parse( file_name) out = dom.Document() root = dom_ext.elementUnder( out, 'cdml', (('version','0.11'),)) for m in tree.getElementsByTagName( 'molecule'): out_m = self.transform_molecule( m, out) if out_m: root.appendChild( out_m) view = self.on_end_set_viewport() if view: viewport = out.createElement( 'viewport') viewport.setAttribute( 'viewport', '%f %f %f %f' % view) root.insertBefore( viewport, root.firstChild) return root def transform_molecule( self, mol, doc): #atoms out = doc.createElement( 'molecule') for a in mol.getElementsByTagName( 'atom'): out_a = self.transform_atom( a, doc) if out_a: out.appendChild( out_a) # bonds ar = mol.getElementsByTagName( 'bondArray') if ar: ar = ar[0] for b in ar.getElementsByTagName( 'bond'): out_b = self.transform_bond( b, doc) if out_b: out.appendChild( out_b) else: for b in self.add_nonexisting_bonds(): out.appendChild( b) return out def transform_atom( self, a, doc): out = doc.createElement( 'atom') try: atom = self.CML_atom( cml=a) except cml_exception, detail: # problems converting cml to atom raise plugin.import_exception( detail) if atom.not_enough_data(): # atom does not have sufficient data to be safely converted to cdml raise plugin.import_exception( "missing "+str( atom.not_enough_data())+" in atom specification") self.atoms.append( atom) dom_ext.setAttributes( out, (('name', atom.symbol), ('id', atom.id))) pnt = dom_ext.elementUnder( out, 'point', (('x', str( atom.x)), ('y', str( atom.y)))) if atom.z != None: pnt.setAttribute( 'z', str( atom.z)) self.xs.append( atom.x) self.ys.append( atom.y) return out
def construct_dom_tree(self, top_levels): """Construct the SVG dom from all top_levels. """ # the constants border_size = self.paper.get_paper_property('crop_margin') # converter px_to_cm_txt = lambda x: Screen.px_to_text_with_unit( x, unit="cm", round_to=5) px_to_mm_txt = lambda x: Screen.px_to_text_with_unit( x, unit="mm", round_to=5) # the code self._id = 0 doc = self.document self.top = dom_extensions.elementUnder( doc, "svg", attributes=(("xmlns", "http://www.w3.org/2000/svg"), ("version", "1.0"))) if self.full_size: sx = self.paper.get_paper_property('size_x') sy = self.paper.get_paper_property('size_y') dom_extensions.setAttributes( self.top, (("width", '%fmm' % sx), ("height", '%fmm' % sy), ('viewBox', '0 0 %d %d' % (Screen.mm_to_px(sx), Screen.mm_to_px(sy))))) else: items = list(self.paper.find_all()) items.remove(self.paper.background) x1, y1, x2, y2 = self.paper.list_bbox(items) w = px_to_mm_txt(x2 - x1 + 2 * border_size) h = px_to_mm_txt(y2 - y1 + 2 * border_size) bx2, by2 = x2 - x1 + 2 * border_size, y2 - y1 + 2 * border_size dom_extensions.setAttributes( self.top, (("width", w), ("height", h), ("viewBox", '0 0 %d %d' % (bx2, by2)))) self.group = dom_extensions.elementUnder(self.top, 'g', (('font-size', '12pt'), ('font-family', 'Helvetica'), ('stroke-linecap', 'round'))) if not self.full_size: self.group.setAttribute( 'transform', 'translate(%d,%d)' % (-x1 + border_size, -y1 + border_size)) # sort the top_levels according to paper.stack cs = [] for c in self.paper.stack: if c in top_levels: cs.append(c) for o in cs: if o.object_type == 'molecule': for b in o.bonds: self.add_bond(b) for a in o.atoms: self.add_atom(a) elif o.object_type == 'arrow': self.add_arrow(o) elif o.object_type == 'text': self.add_text(o) elif o.object_type == 'plus': self.add_plus(o) elif o.object_type in data.vector_graphics_types: if o.object_type == 'rect': self.add_rect(o) elif o.object_type == 'oval': self.add_oval(o) elif o.object_type == 'polygon': self.add_polygon(o) elif o.object_type == 'polyline': self.add_polyline(o)