def set_style(self, style): if style == self.last_style: return self.last_style_tab style_tab = self.dc_info.styles[style].split(";") self.last_style_tab = style_tab style = "" if style_tab[3] == "1": style += "I" if style_tab[4] == "1": style += "B" if style_tab[1] in self.surf.fonts_map: font_name = self.surf.fonts_map[style_tab[1]] else: font_name = "sans-serif" self.dc.set_font( font_name, style, int((self.scale * self.base_font_size * int(style_tab[2])) / 100), ) (r, g, b) = self.rgbfromhex(style_tab[0]) self.dc.set_text_color(r, g, b) BaseDc.set_style(self, style) return style_tab
def draw_image(self, x, y, dx, dy, scale, png_data): png_stream = io.StringIO(png_data) image = wx.ImageFromStream(png_stream) w = image.GetWidth() h = image.GetHeight() (x_scale, y_scale) = self._scale_image(x, y, dx, dy, scale, w, h) if scale < 4: image.Rescale(w * x_scale, h * y_scale) bmp = image.ConvertToBitmap() w = image.GetWidth() h = image.GetHeight() self.ctx.DrawBitmap(bmp, x, y, w, h) else: delta_x = 0 delta_y = 0 while delta_y < dy: if scale == 4 and delta_y > 0: break delta_x = 0 bmp = image.ConvertToBitmap() while delta_x < dx: if scale == 5 and delta_x > 0: break self.ctx.DrawBitmap(bmp, x + delta_x, y + delta_y, w, h) delta_x += w delta_y += h BaseDc.draw_image(self, x, y, dx, dy, scale, png_data)
def add_spline(self, xytab, close): tabpoints = [] for pos in xytab: tabpoints.append(wx.Point(pos[0] * self.scale, pos[1] * self.scale)) self._add(self._spline, (self, tabpoints)) BaseDc.add_spline(self, xytab)
def __init__(self, dc=None, calc_only=False, width=-1, height=-1, output_name=None, scale=1.0): BaseDc.__init__(self, calc_only, width, height, output_name, scale) self.dc_info = DcDcinfo(self) if self.width >= 0: width2 = self.width else: width2 = self.default_width if self.height >= 0: height2 = self.height else: height2 = self.default_height self.type = None if self.calc_only: self.surf = wx.Bitmap(10, 10, 32) self.dc = wx.MemoryDC(self.surf) self.dc.Clear() if width < 0: self.width = -1 if height < 0: self.height = 1000000000 else: if dc: self.surf = None self.dc = dc else: if output_name: name = output_name.lower() self.surf = wx.EmptyBitmap(width2, height2, 32) self.dc = wx.MemoryDC(self.surf) self.dc.Clear() if ".jpg" in name or ".jpeg" in name: self.type = "jpg" else: self.type = "png" else: self.surf = wx.EmptyBitmap(10, 10, 32) self.dc = wx.MemoryDC(self.surf) self.last_style_tab = None self._color = (0, 0, 0, 255) self._line_width = 0 self._fun_stack = [] self._fill = False self._draw = False self._preserve = False self.transparent_brush = wx.Brush(wx.Colour(255, 255, 255), style=wx.TRANSPARENT) self.transparent_pen = wx.Pen(wx.Colour(255, 255, 255), width=0, style=wx.TRANSPARENT) self._last_pen = None self._last_brush = wx.Brush(wx.Colour(255, 255, 255), style=wx.SOLID) self._last_pen_color = (0, 0, 0, 255) self._last_line_width = -1 self._last_brush_color = (255, 255, 255, 255)
def add_polygon(self, xytab): tabpoints = [] for pos in xytab: tabpoints.append(wx.Point(pos[0] * self.scale, pos[1] * self.scale)) self._add(self.dc.DrawPolygon, (tabpoints, )) BaseDc.add_polygon(self, xytab)
def add_line(self, x, y, dx, dy): self._add( self.dc.line, ( x * self.scale, y * self.scale, (x + dx) * self.scale, (y + dy) * self.scale, ), ) BaseDc.add_line(self, x, y, dx, dy)
def add_rounded_rectangle(self, x, y, dx, dy, radius): self._add( self.dc.DrawRoundedRectangle, ( x * self.scale, y * self.scale, dx * self.scale, dy * self.scale, radius * self.scale, ), ) BaseDc.add_rounded_rectangle(self, x, y, dx, dy, radius)
def __init__(self, dc=None, calc_only=False, width=-1, height=-1, output_name=None, scale=1.0): BaseDc.__init__(self, calc_only, width, height, output_name, scale) if self.width >= 0: width2 = self.width else: width2 = self.default_width if self.height >= 0: height2 = self.height else: height2 = self.default_height self.dc_info = PdfDcInfo(self) self.type = None if self.calc_only: self.surf = PDFSurface(None, 10, 10) if width < 0: self.width = -1 if height < 0: self.height = 1000000000 self.dc = self.surf.get_dc() else: if dc: self.surf = None self.dc = dc else: if output_name: name = output_name.lower() self.surf = PDFSurface(output_name, width2, height2) else: self.surf = PDFSurface(None, width2, height2) self.dc = self.surf.get_dc() self.last_style_tab = None self._color = (0, 0, 0, 255) self._line_width = 0 self._fun_stack = [] self._fill = False self._draw = False self._preserve = False self._last_pen = None self._last_brush = None self._last_pen_color = (0, 0, 0, 255) self._last_line_width = -1 self._last_brush_color = (255, 255, 255, 255) self.start_page()
def add_arc(self, x, y, radius, angle1, angle2): self._add( self.dc.DrawEllipticArc, ( (x + radius) * self.scale, (y + radius) * self.scale, radius * 2 * self.scale, radius * 2 * self.scale, (360 - angle1) * self.scale, (360 - angle2) * self.scale, ), ) BaseDc.add_arc(self, x, y, radius, angle1, angle2)
def __init__(self, ctx=None, calc_only=False, width=-1, height=-1, output_name=None): BaseDc.__init__(self, calc_only, width, height, output_name) self.dc_info = GraphicsContextDcinfo(self) self.type = None if self.calc_only: self.surf = wx.EmptyBitmap(10, 10, 32) dc = wx.MemoryDC(self.surf) dc.Clear() self.ctx = self._make_gc(dc) if width < 0: self.width = -1 if height < 0: self.height = 1000000000 else: if ctx: self.surf = None self.ctx = ctx else: if self.width >= 0: width2 = self.width else: width2 = self.default_width if self.height >= 0: height2 = self.height else: height2 = self.default_height self.surf = wx.EmptyBitmap(width2, height2, 32) if output_name: self.type = "png" dc = wx.MemoryDC(self.surf) dc.Clear() self.ctx = self._make_gc(dc) self.path = None self.colour = wx.Colour(0, 0, 0) self.line_width = 1 self._move_x = 0 self._move_y = 0 self.last_style_tab = None
def set_style(self, style): if style == self.last_style: return self.last_style_tab style_tab = self.dc_info.styles[style].split(";") self.last_style_tab = style_tab font = wx.SystemSettings.GetFont(wx.SYS_DEFAULT_GUI_FONT) if style_tab[3] == "1": slant = wx.ITALIC font.SetStyle(wx.ITALIC) else: slant = wx.NORMAL if style_tab[4] == "1": weight = wx.BOLD font.SetWeight(wx.BOLD) else: weight = wx.FONTWEIGHT_NORMAL if style_tab[1] == "serif": font_style = wx.ROMAN font.SetFamily(wx.ROMAN) elif style_tab[1] == "sans-serif": font_style = wx.SWISS font.SetFamily(wx.SWISS) elif style_tab[1] == "monospace": font_style = wx.MODERN font.SetFamily(wx.MODERN) elif style_tab[1] == "cursive": font_style = wx.SCRIPT font.SetFamily(wx.SCRIPT) elif style_tab[1] == "fantasy": font_style = wx.DECORATIVE font.SetFamily(wx.DECORATIVE) else: font_style = wx.DEFAULT font.SetPointSize( (self.scale * (self.base_font_size * 72) * int(style_tab[2])) / (96 * 100.0)) self.dc.SetFont(font) (r, g, b) = self.rgbfromhex(style_tab[0]) self.dc.SetTextForeground(wx.Colour(r, g, b)) self.set_color(r, g, b) BaseDc.set_style(self, style) return style_tab
def fill(self, preserve=False): self._draw = False self._fill = True if self._last_brush_color != self._color: self.dc.set_fill_color(self._color[0], self._color[1], self._color[2]) self._last_brush_color = self._color self._draw_and_fill() self._fill = False if not preserve: self._fun_stack = [] return BaseDc.fill(self, preserve)
def draw_image(self, x, y, dx, dy, scale, png_data): png_stream = io.BytesIO(png_data) image = PIL.Image.open(png_stream) w, h = image.size (x_scale, y_scale) = self._scale_image( x, y, dx, dy, scale, w, h, ) if scale < 4: if scale != 0 and x_scale < 0.25 and y_scale < 0.25: image.thumbnail((4 * w * x_scale, 4 * h * y_scale), PIL.Image.ANTIALIAS) file_name = get_temp_filename("temp.png") image.save(file_name, "PNG") self.dc.image(file_name, x, y, w * x_scale, h * y_scale) os.remove(file_name) else: pass BaseDc.draw_image(self, x, y, dx, dy, scale, png_data)
def fill(self, preserve=False): self._draw = False self._fill = True self.dc.SetPen(self.transparent_pen) if self._last_brush_color != self._color: self._last_brush = wx.Brush( wx.Colour(self._color[0], self._color[1], self._color[2]), style=wx.SOLID, ) self._last_brush_color = self._color self.dc.SetBrush(self._last_brush) self._draw_and_fill() self._fill = False if not preserve: self._fun_stack = [] return BaseDc.fill(self, preserve)
def draw(self, preserve=False): self._draw = True self._fill = False if (self._last_pen_color != self._color or self._last_line_width != self._line_width): self.dc.set_draw_color(self._color[0], self._color[1], self._color[2]) self.dc.set_text_color(self._color[0], self._color[1], self._color[2]) self.dc.set_line_width(self._line_width) self._last_pen_color = self._color self._last_line_width = self._line_width self._draw_and_fill() self._draw = False if not preserve: self._fun_stack = [] return BaseDc.draw(self, preserve)
def draw(self, preserve=False): self._draw = True self._fill = False self.dc.SetBrush(self.transparent_brush) if (self._last_pen_color != self._color or self._last_line_width != self._line_width): self._last_pen = wx.Pen( wx.Colour(self._color[0], self._color[1], self._color[2]), (self._line_width + 0.49) * self.scale, style=wx.SOLID, ) self._last_pen_color = self._color self._last_line_width = self._line_width self.dc.SetPen(self._last_pen) self._draw_and_fill() self._draw = False if not preserve: self._fun_stack = [] return BaseDc.draw(self, preserve)
def new_page(self): BaseDc.new_page(self)
def new_path(self): self.path = self.ctx.CreatePath() BaseDc.new_path(self)
def draw_rotated_text(self, x, y, txt, angle): (w, h, d, e) = self.dc_info.get_extents(txt) BaseDc.draw_rotated_text(self, x, y, txt)
def rectangle(self, x, y, dx, dy): self.path.AddRectangle(x, y, dx, dy) BaseDc.rectangle(self, x, y, dx, dy)
def draw_line(self, x, y, dx, dy): self.path.MoveToPoint(x, y) self.path.AddLineToPoint(x + dx, y + dy) BaseDc.draw_line(self, x, y, dx, dy)
def set_pen(self, r, g, b, a=255, width=1): pen = wx.Pen(wx.Colour(r, g, b, a)) self.ctx.SetPen(pen) BaseDc.set_pen(self, r, g, b, a, width)
def set_brush(self, r, g, b, a=255): brush = wx.Brush(wx.Colour(r, g, b, a)) self.ctx.SetBrush(brush) BaseDc.set_brush(self, r, g, b, a)
def show_text(self, txt): (w, h, d, e) = self.ctx.GetFullTextExtent(txt) dy_up = h - d self.ctx.DrawText(txt, self._move_x, self._move_y - dy_up) BaseDc.show_text(self, txt)
def line_to(self, x, y): self.path.AddLineToPoint(x, y) BaseDc.move_to(self, x, y)
def move_to(self, x, y): self._move_x = x self._move_y = y if self.path: self.path.MoveToPoint(x, y) BaseDc.move_to(self, x, y)
def draw(self): if not self.calc_only: self.ctx.DrawPath(self.path) self.path = None BaseDc.draw(self)
def fill(self): if not self.calc_only: self.ctx.FillPath(self.path) self.path = None BaseDc.fill(self)
def stroke(self): if not self.calc_only: self.ctx.StrokePath(self.path) self.path = None BaseDc.stroke(self)
def __init__( self, dc=None, dc_info=None, base_url=None, url=None, calc_only=False, parse_only=False, init_css_str=None, css_type=CSS_TYPE_STANDARD, ): """Constructor Args: dc - Device context onto which graphics and text can be drawn dc_info - information related to device context, dervied from BaseDcInfo. If None embeded in dc info is used. calc_only - do not render - calc only. parse_only - do not render - parse only. init_css_str - css for rendered html. If None: standard css is used (INIT_CSS_STR_BASE variable) css_type - if CSS_TYPE_STANDARD: simplified version of css, if CSS_TYPE_INDENT: simplified and in icss format ( brackets replaced with indentations ) """ self.tag_parser = None self.url = url self.base_url = base_url self.parse_only = parse_only self.obj_id_dict = {} self.obj_action_dict = {} self.parent_window = None self._max_width = 0 self._max_height = 0 self.lp = 1 self.table_lp = 0 self.http = None self.tdata_tab = [] self.debug = False if self.parse_only: self.calc_only = True else: self.calc_only = calc_only if dc != None: self.dc = dc else: self.dc = BaseDc() if self.calc_only: self.dc = NullDc(self.dc) if dc_info: self.dc_info = dc_info else: self.dc_info = self.dc.get_dc_info() self.css = Css() if init_css_str: if css_type == self.CSS_TYPE_STANDARD: self.css.parse_str(init_css_str) else: self.css.parse_indent_str(init_css_str) else: if css_type == self.CSS_TYPE_STANDARD: self.css.parse_str(INIT_CSS_STR_BASE) else: self.css.parse_indent_str(INIT_CSS_STR_BASE) HtmlModParser.__init__(self, url)