예제 #1
0
파일: draw.py 프로젝트: vincsdev/gamera
    def __call__(self,
                 p,
                 text,
                 color,
                 size=10,
                 font_family=0,
                 italic=False,
                 bold=False,
                 halign=0):
        from gamera.core import Dim, RGB, ONEBIT, Image
        from gamera.plugins import string_io
        try:
            import wx
        except ImportError:
            raise RuntimeError("Drawing text requires wxPython.")
        try:
            dc = wx.MemoryDC()
        except wx._core.PyNoAppError:
            app = wx.App()
            dc = wx.MemoryDC()
        if font_family < 0 or font_family > 2:
            raise ValueError("font_family must be in range 0-2.")
        font_family = [wx.ROMAN, wx.SWISS, wx.MODERN][font_family]
        italic = (italic and wx.ITALIC) or wx.NORMAL
        bold = (bold and wx.BOLD) or wx.NORMAL
        if type(text) == str:
            encoding = wx.FONTENCODING_SYSTEM
        elif type(text) == unicode:
            encoding = wx.FONTENCODING_UNICODE
        else:
            raise ValueError("text must be a string or unicode string.")
        font = wx.Font(size, font_family, italic, bold, encoding=encoding)
        font.SetPixelSize(wx.Size(size * 2, size * 2))
        dc.SetFont(font)
        w, h = dc.GetTextExtent(text)

        # Do the actual drawing
        bmp = wx.EmptyBitmap(w, h, -1)
        dc.SelectObject(bmp)
        dc.SetPen(wx.TRANSPARENT_PEN)
        dc.SetBrush(wx.WHITE_BRUSH)
        dc.DrawRectangle(0, 0, w, h)
        dc.SetBrush(wx.BLACK_BRUSH)
        dc.DrawText(text, 0, 0)
        img = bmp.ConvertToImage()
        img_str = img.GetData()

        text_image = string_io._from_raw_string((0, 0), Dim(w, h), RGB, 0,
                                                img_str)
        text_image = text_image.to_onebit()
        if halign == 1:
            p = (p[0] - w / 2, p[1])
        elif halign == 2:
            p = (p[0] - w, p[1])

        ul = (max(p[0], self.ul_x), max(p[1], self.ul_y))
        lr = (min(p[0] + w, self.lr_x), min(p[1] + h, self.lr_y))
        w = lr[0] - ul[0]
        h = lr[1] - ul[1]
        if w < 0 or h < 0:
            return

        text_image = text_image.subimage(
            (max(self.ul_x - p[0], 0), max(self.ul_y - p[1], 0)), Dim(w, h))
        if self.data.pixel_type == ONEBIT:
            subimage = self.subimage(ul, Dim(w, h))
            if color:
                subimage.or_image(text_image, in_place=True)
            else:
                text_image.invert()
                subimage.and_image(text_image, in_place=True)
        elif self.data.pixel_type == RGB:
            subimage = Image(
                (max(ul[0] - self.ul_x, p[0]), max(ul[1] - self.ul_y, p[1])),
                Dim(w, h), ONEBIT)
            subimage.or_image(text_image, in_place=True)
            self.highlight(subimage, color)
예제 #2
0
파일: draw.py 프로젝트: hsnr-gamera/gamera
  def __call__(self, p, text, color, size=10, font_family=0,
               italic=False, bold=False, halign=0):
    from gamera.core import Dim, RGB, ONEBIT, Image
    from gamera.plugins import string_io
    try:
      import wx
      from gamera.gui import compat_wx
    except ImportError:
      raise RuntimeError("Drawing text requires wxPython.")
    try:
      dc = wx.MemoryDC()
    except wx._core.PyNoAppError:
      app = wx.App()
      dc = wx.MemoryDC()
    if font_family < 0 or font_family > 2:
      raise ValueError("font_family must be in range 0-2.")
    font_family = [wx.ROMAN, wx.SWISS, wx.MODERN][font_family]
    italic = (italic and wx.ITALIC) or wx.NORMAL
    bold = (bold and wx.BOLD) or wx.NORMAL
    if type(text) == str:
      encoding = wx.FONTENCODING_SYSTEM
    elif type(text) == unicode:
      encoding = wx.FONTENCODING_UNICODE
    else:
      raise ValueError("text must be a string or unicode string.")
    font = wx.Font(size, font_family, italic, bold,
                   encoding = encoding)
    font.SetPixelSize(wx.Size(size * 2, size * 2))
    dc.SetFont(font)
    w, h = dc.GetTextExtent(text)

    # Do the actual drawing
    bmp = compat_wx.create_empty_bitmap(w, h, -1)
    dc.SelectObject(bmp)
    dc.SetPen(wx.TRANSPARENT_PEN)
    dc.SetBrush(wx.WHITE_BRUSH)
    dc.DrawRectangle(0, 0, w, h)
    dc.SetBrush(wx.BLACK_BRUSH)
    dc.DrawText(text, 0, 0)
    img = bmp.ConvertToImage()
    img_str = img.GetData()
    
    text_image = string_io._from_raw_string(
      (0, 0), Dim(w, h), RGB, 0, img_str)
    text_image = text_image.to_onebit()
    if halign == 1:
      p = (p[0] - w / 2, p[1])
    elif halign == 2:
      p = (p[0] - w, p[1])

    ul = (max(p[0], self.ul_x), max(p[1], self.ul_y))
    lr = (min(p[0] + w, self.lr_x), min(p[1] + h, self.lr_y))
    w = lr[0] - ul[0]
    h = lr[1] - ul[1]
    if w < 0 or h < 0:
      return
    
    text_image = text_image.subimage(
      (max(self.ul_x - p[0], 0), max(self.ul_y - p[1], 0)),
      Dim(w, h))
    if self.data.pixel_type == ONEBIT:
      subimage = self.subimage(ul, Dim(w, h))
      if color:
        subimage.or_image(text_image, in_place=True)
      else:
        text_image.invert()
        subimage.and_image(text_image, in_place=True)
    elif self.data.pixel_type == RGB:
      subimage = Image((max(ul[0] - self.ul_x, p[0]),
                        max(ul[1] - self.ul_y, p[1])),
                       Dim(w, h), ONEBIT)
      subimage.or_image(text_image, in_place=True)
      self.highlight(subimage, color)