def tr_textout(self, chunk): length = get_data('<h', chunk[:2])[0] encoding = self.get_encoding() txt = chunk[8:8 + length].decode(encoding) txt_length = len(txt) txt = txt.encode('utf-8') y, x, = get_data('<hhhh', chunk[8 + length:16 + length]) p = apply_trafo_to_point([x, y], self.get_trafo()) cfg = self.layer.config sk2_style, tags = self.get_text_style() markup = [[tags, (0, txt_length)]] tr = [] + libgeom.NORMAL_TRAFO text = sk2_model.Text(cfg, self.layer, p, txt, -1, tr, sk2_style) text.markup = markup rect = None if self.dc.opacity: bg_style = [[], [], [], []] clr = [] + self.dc.bgcolor clr = [uc2const.COLOR_RGB, clr, 1.0, '', ''] bg_style[0] = [sk2const.FILL_EVENODD, sk2const.FILL_SOLID, clr] text.update() bbox = [] + text.cache_bbox rect = bbox[:2] + [bbox[2] - bbox[0], bbox[3] - bbox[1]] rect = sk2_model.Rectangle(cfg, self.layer, rect, style=bg_style) self.layer.childs.append(rect) if self.dc.font[-2]: tr = libgeom.trafo_rotate_grad(self.dc.font[-2], p[0], p[1]) text.trafo = libgeom.multiply_trafo(text.trafo, tr) if self.dc.opacity: rect.trafo = libgeom.multiply_trafo(rect.trafo, tr) self.layer.childs.append(text)
def _rectangle(self, element): ll, chunk = self.read_point(element.params) ur, chunk = self.read_point(chunk) w, h = ur[0] - ll[0], ur[1] - ll[1] rect = sk2_model.Rectangle(self.layer.config, self.layer, ll + [w, h], self.get_trafo(), self.get_style(fill=True)) self.layer.childs.append(rect)
def create_rectangle(self, obj): config = self.pdxf_doc.config parent = self.parent_stack[-1] w, h = obj.rect_size x = y = 0.0 if w < 0: x = w w = -w if h < 0: y = h h = -h mr = min(w, h) / 2.0 r1, r2, r3, r4 = obj.radiuses radiuses = [r1 / mr, r2 / mr, r3 / mr, r4 / mr] trafo = deepcopy(obj.trafo) if not obj.style_id is None and obj.fill_id is None and obj.outl_id is None: style = [[], deepcopy(config.default_stroke), [], []] else: if obj.fill_id is None: fill = [] else: fill = self.get_fill_prop(obj.fill_id) if obj.outl_id is None: stroke = [] else: stroke = self.get_stroke_prop(obj.outl_id) style = [fill, stroke, [], []] rect = sk2_model.Rectangle(config, parent, [x, y, w, h], trafo, style, radiuses) self.methods.append_object(rect, parent)
def translate_to_sk2(self, sk2_doc): ncells = len(self.model.colors) if not ncells: return cellsize = self.config.large_cell if ncells > self.config.short_palette_size: cellsize = self.config.small_cell if self.model.columns < 2: columns = int(self.config.palette_width / cellsize) else: columns = self.model.columns rows = 1.0 * ncells / columns if rows > int(rows): rows = int(rows) + 1 else: rows = int(rows) ext = uc2const.FORMAT_EXTENSION[uc2const.SK2][0] sk2_doc.doc_file = self.model.name + "." + ext sk2_doc.methods.set_doc_origin(sk2const.DOC_ORIGIN_LU) sk2_doc.methods.set_doc_units(uc2const.UNIT_PX) page = sk2_doc.methods.get_page() orient = uc2const.PORTRAIT w = columns * cellsize + 1 h = rows * cellsize + 1 if w > h: orient = uc2const.LANDSCAPE sk2_doc.methods.set_page_format(page, ['Custom', (w, h), orient]) sk2_doc.methods.set_default_page_format(['Custom', (w, h), orient]) grid_layer = sk2_doc.methods.get_grid_layer() grid_layer.grid = [0, 0, uc2const.px_to_pt, uc2const.px_to_pt] grid_layer.properties = [1, 0, 0] layer = sk2_doc.methods.get_layer(page) cfg = sk2_doc.config x_orign = -(w / 2.0) + 0.5 - cellsize y = h / 2.0 - 0.5 stroke = deepcopy(cfg.default_stroke) stroke[1] = 1.0 index = 0 for row in range(rows): y -= cellsize x = x_orign for column in range(columns): x += cellsize trafo = [cellsize, 0, 0, cellsize, x, y] color = deepcopy(self.model.colors[index]) fill = [sk2const.FILL_EVENODD, sk2const.FILL_SOLID, color] style = [fill, deepcopy(stroke), [], []] layer.childs.append(sk2_model.Rectangle(cfg, layer, trafo=trafo, style=style)) index += 1 if index == ncells: break sk2_doc.update()
def _draw_point(self, point): style = [[], [], [], []] clr = [] + sk2const.RGB_BLACK clr[1] = [1.0, 0.0, 0.0] style[0] = [sk2const.FILL_EVENODD, sk2const.FILL_SOLID, clr] rect = point + [3.0, 3.0] rect = sk2_model.Rectangle(self.layer.config, self.layer, rect, style=style) self.layer.childs.append(rect)
def tr_rectangle(self, chunk): bottom, right, top, left = get_data('<hhhh', chunk) left, top = apply_trafo_to_point([left, top], self.get_trafo()) right, bottom = apply_trafo_to_point([right, bottom], self.get_trafo()) cfg = self.layer.config sk2_style = self.get_style() rect = [left, top, right - left, bottom - top] rect = sk2_model.Rectangle(cfg, self.layer, rect, style=sk2_style) self.layer.childs.append(rect)
def tr_round_rectangle(self, chunk): eh, ew, bottom, right, top, left = get_data('<hhhhhh', chunk) corners = 4 * [0.0, ] if eh and ew: coef = max(ew / abs(right - left), eh / abs(bottom - top)) corners = 4 * [coef, ] left, top = apply_trafo_to_point([left, top], self.get_trafo()) right, bottom = apply_trafo_to_point([right, bottom], self.get_trafo()) cfg = self.layer.config sk2_style = self.get_style() rect = [left, top, right - left, bottom - top] rect = sk2_model.Rectangle(cfg, self.layer, rect, style=sk2_style, corners=corners) self.layer.childs.append(rect)
def set_page(self, extend): left, bottom = extend[0] right, top = extend[1] width = right - left height = top - bottom scale = 841 / float(max(abs(width), abs(height))) if self.cgm['scale.mode'] == 1: scale = self.cgm['scale.metric'] * 72 / 25.4 w, h = width * scale, height * scale orient = uc2const.PORTRAIT if w < h else uc2const.LANDSCAPE page_format = [_('Custom size'), (w, h), orient] self.sk2_mtds.set_page_format(self.page, page_format) if self.cgm['color.bg']: style = [self.get_fill_style(self.cgm['color.bg']), [], [], []] rect = sk2_model.Rectangle(self.layer.config, self.layer, [-w / 2.0, -h / 2.0, w, h], style=style) self.layer.childs.append(rect)
def translate_rect(self, dest_parent, source_rect): trafo = self.get_sk2_trafo(source_rect) corners = [0.0, 0.0, 0.0, 0.0] rect = [0.0, 0.0, 1.0, 1.0] if source_rect.radius1 and source_rect.radius2: radius = min(source_rect.radius1, source_rect.radius2) corners = [radius * 2.0] * 4 if source_rect.radius1 > source_rect.radius2: coef = source_rect.radius1 / source_rect.radius2 rect = [0.0, 0.0, 1.0, coef] tr = [1.0, 0.0, 0.0, 1.0 / coef, 0.0, 0.0] else: coef = source_rect.radius2 / source_rect.radius1 rect = [0.0, 0.0, coef, 1.0] tr = [1.0 / coef, 0.0, 0.0, 1.0, 0.0, 0.0] trafo = libgeom.multiply_trafo(tr, trafo) dest_rect = sk2_model.Rectangle(dest_parent.config, dest_parent, rect, trafo, corners=corners) dest_rect.style = get_sk2_style(source_rect.properties) return dest_rect
def translate_polyline(self, obj, cfg): tr = self.trafo style = self.get_style(obj) if obj.sub_type in (fig_const.T_ARC_BOX, fig_const.T_PIC_BOX): bbox = libgeom.bbox_for_points(obj.points) bbox_size = libgeom.bbox_size(bbox) rect = [bbox[0], bbox[1], bbox_size[0], bbox_size[1]] corners = sk2const.CORNERS if obj.sub_type == fig_const.T_ARC_BOX: try: wide_side = max(bbox_size) * tr[0] c = obj.radius * 2.0 * self.thickness / wide_side corners = [c, c, c, c] except ZeroDivisionError: pass else: try: pic = self.translate_pic(obj, cfg) if pic: return pic except Exception: pass props = dict(rect=rect, trafo=tr[:], style=style, corners=corners[:]) new_obj = sk2_model.Rectangle(cfg, **props) else: start_point, points = obj.points[0], obj.points[1:] end_marker = points and start_point == points[-1] paths = [[start_point, points, end_marker]] props = dict(paths=paths, trafo=tr[:], style=style) new_obj = sk2_model.Curve(cfg, **props) return new_obj