Beispiel #1
0
    def _do_draw_label(self, context, rect):
        angle = 2 * math.pi * self._rotation / 360.0

        if self._context == None:
            label = gtk.Label()
            self._context = label.create_pango_context()
        pango_context = self._context

        attrs = pango.AttrList()
        attrs.insert(pango.AttrWeight(self._weight, 0, len(self._text)))
        attrs.insert(pango.AttrStyle(self._slant, 0, len(self._text)))
        attrs.insert(pango.AttrUnderline(self._underline, 0, len(self._text)))
        if self._size != None:
            attrs.insert(pango.AttrSize(1000 * self._size, 0, len(self._text)))

        if self._layout == None:
            self._layout = pango.Layout(pango_context)
        layout = self._layout
        layout.set_text(self._text)
        layout.set_attributes(attrs)

        #find out where to draw the layout and calculate the maximum width
        width = rect.width
        if self._anchor in [
                ANCHOR_BOTTOM_LEFT, ANCHOR_TOP_LEFT, ANCHOR_LEFT_CENTER
        ]:
            width = rect.width - self._position[0]
        elif self._anchor in [
                ANCHOR_BOTTOM_RIGHT, ANCHOR_TOP_RIGHT, ANCHOR_RIGHT_CENTER
        ]:
            width = self._position[0]

        text_width, text_height = layout.get_pixel_size()
        width = width * math.cos(angle)
        width = min(width, self._max_width)

        if self._wrap:
            layout.set_wrap(pango.WRAP_WORD_CHAR)
        layout.set_width(int(1000 * width))

        x, y = get_text_pos(layout, self._position, self._anchor, angle)

        if not self._fixed:
            #Find already drawn labels that would intersect with the current one
            #and adjust position to avoid intersection.
            text_width, text_height = layout.get_pixel_size()
            real_width = abs(text_width * math.cos(angle)) + abs(
                text_height * math.sin(angle))
            real_height = abs(text_height * math.cos(angle)) + abs(
                text_width * math.sin(angle))

            other_labels = get_registered_labels()
            this_rect = gtk.gdk.Rectangle(int(x), int(y), int(real_width),
                                          int(real_height))
            for label in other_labels:
                label_rect = label.get_allocation()
                intersection = this_rect.intersect(label_rect)
                if intersection.width == 0 and intersection.height == 0:
                    continue

                y_diff = 0
                if label_rect.y <= y and label_rect.y + label_rect.height >= y:
                    y_diff = y - label_rect.y + label_rect.height
                elif label_rect.y > y and label_rect.y < y + real_height:
                    y_diff = label_rect.y - real_height - y
                y += y_diff

        #draw layout
        context.move_to(x, y)
        context.rotate(angle)
        context.set_source_rgb(*basics.color_gdk_to_cairo(self._color))
        context.show_layout(layout)
        context.rotate(-angle)
        context.stroke()

        #calculate the real dimensions
        text_width, text_height = layout.get_pixel_size()
        real_width = abs(text_width * math.cos(angle)) + abs(
            text_height * math.sin(angle))
        real_height = abs(text_height * math.cos(angle)) + abs(
            text_width * math.sin(angle))
        self._real_dimensions = real_width, real_height
        self._real_position = x, y
        self._line_count = layout.get_line_count()

        register_label(self)
Beispiel #2
0
 def _do_draw_label(self, context, rect):
     angle = 2 * math.pi * self._rotation / 360.0
     
     if self._context == None:
         label = gtk.Label()
         self._context = label.create_pango_context()          
     pango_context = self._context
     
     attrs = pango.AttrList()
     attrs.insert(pango.AttrWeight(self._weight, 0, len(self._text)))
     attrs.insert(pango.AttrStyle(self._slant, 0, len(self._text)))
     attrs.insert(pango.AttrUnderline(self._underline, 0,
                     len(self._text)))
     if self._size != None:
         attrs.insert(pango.AttrSize(1000 * self._size, 0,
                         len(self._text)))
     
     if self._layout == None:
         self._layout = pango.Layout(pango_context)
     layout = self._layout
     if self._markup:
         layout.set_markup(self._text)
     else:
         layout.set_text(self._text)
         layout.set_attributes(attrs)
     
     #find out where to draw the layout and calculate the maximum width
     width = rect.width * math.cos(angle) + rect.height * math.sin(angle)
     if self._anchor in [ANCHOR_BOTTOM_LEFT, ANCHOR_TOP_LEFT,
                         ANCHOR_LEFT_CENTER]:
         width = rect.width - self._position[0]
     elif self._anchor in [ANCHOR_BOTTOM_RIGHT, ANCHOR_TOP_RIGHT,
                             ANCHOR_RIGHT_CENTER]:
         width = self._position[0]
     
     text_width, text_height = layout.get_pixel_size()
     width = min(width, self._max_width)
     
     if self._wrap:
         layout.set_wrap(pango.WRAP_WORD_CHAR)
     layout.set_width(int(1000 * width))
     
     text_width, text_height = layout.get_pixel_size()
     anchor = self._anchor
     x, y = self._position
 
     #anchor translation
     tx, ty = 0, 0
     ttx, tty = 0, 0
     if anchor == ANCHOR_BOTTOM_LEFT:
         tx = text_height * math.sin(angle)
         ty = text_height * math.cos(angle)
         ttx = 0
         tty = text_height
     elif anchor == ANCHOR_TOP_LEFT:
         tx = 0
         ty = 0
     elif anchor ==  ANCHOR_TOP_RIGHT:
         tx = text_width * math.cos(angle)
         ty = -text_width * math.sin(angle)
         ttx = text_width
         tty = 0
     elif anchor == ANCHOR_BOTTOM_RIGHT:
         tx = text_width * math.cos(angle) + text_height * math.sin(angle)
         ty = -text_width * math.sin(angle) + text_height * math.cos(angle)
         ttx = text_width
         tty = text_height
     elif anchor == ANCHOR_CENTER:
         tx = (text_width * math.cos(angle) + text_height * math.sin(angle)) / 2
         ty = (-text_width * math.sin(angle) + text_height * math.cos(angle)) / 2
         ttx = text_width / 2
         tty = text_height / 2
     elif anchor == ANCHOR_TOP_CENTER:
         tx = text_width * math.cos(angle) / 2
         ty = -text_width * math.sin(angle) / 2
         ttx = text_width / 2
         tty = 0
     elif anchor == ANCHOR_BOTTOM_CENTER:
         tx = text_width * math.cos(angle) / 2 + text_height * math.sin(angle)
         ty = -text_width * math.sin(angle) / 2 + text_height * math.cos(angle)
         ttx = text_width / 2
         tty = text_height
     elif anchor == ANCHOR_LEFT_CENTER:
         tx = text_height * math.sin(angle) / 2
         ty = text_height * math.cos(angle) / 2
         ttx = 0
         tty = text_height / 2
     elif anchor == ANCHOR_RIGHT_CENTER:
         tx = text_width * math.cos(angle) + text_height * math.sin(angle) / 2
         ty = -text_width * math.sin(angle) + text_height * math.cos(angle) / 2
         ttx = text_width
         tty = text_height / 2
         
     context.translate(-tx, -ty)
     context.move_to(x, y)
     context.rotate(-angle)
     context.set_source_rgb(*basics.color_gdk_to_cairo(self._color))
     context.show_layout(layout)
     context.rotate(angle)
     context.stroke()
     context.translate(tx, ty)
     
     #calculate the bounding rect
     real_x = x - ttx
     real_y = y - tty
     top_left = real_x, real_y
     bottom_left = real_x , real_y + text_height
     bottom_right = real_x + text_width, real_y + text_height
     top_right = real_x + text_width, real_y
     
     offset = x, y
     
     n_top_left = rotate_vector(offset, top_left, angle)
     n_bottom_left = rotate_vector(offset, bottom_left, angle)
     n_bottom_right = rotate_vector(offset, bottom_right, angle)
     n_top_right = rotate_vector(offset, top_right, angle)
     
     alloc_x_min = int(min(n_top_left[0], n_bottom_left[0], n_bottom_right[0],
                         n_top_right[0]))
     alloc_x_max = int(max(n_top_left[0], n_bottom_left[0], n_bottom_right[0],
                         n_top_right[0]))
     alloc_y_min = int(min(n_top_left[1], n_bottom_left[1], n_bottom_right[1],
                         n_top_right[1]))
     alloc_y_max = int(max(n_top_left[1], n_bottom_left[1], n_bottom_right[1],
                         n_top_right[1]))
                         
     rect = gtk.gdk.Rectangle(alloc_x_min, alloc_y_min,
                                 alloc_x_max - alloc_x_min,
                                 alloc_y_max - alloc_y_min)       
     
     self._real_dimensions = rect.width, rect.height
     self._real_position = rect.x, rect.y
     self._line_count = layout.get_line_count()
     
     register_label(self)
Beispiel #3
0
 def _do_draw_label(self, context, rect):
     angle = 2 * math.pi * self._rotation / 360.0
     
     if self._context == None:
         label = Gtk.Label()
         self._context = label.create_pango_context()          
     pango_context = self._context
     
     attrs = Pango.AttrList()
     attrs.insert(Pango.AttrWeight(self._weight, 0, len(self._text)))
     attrs.insert(Pango.AttrStyle(self._slant, 0, len(self._text)))
     attrs.insert(Pango.AttrUnderline(self._underline, 0,
                     len(self._text)))
     if self._size != None:
         attrs.insert(Pango.AttrSize(1000 * self._size, 0,
                         len(self._text)))
     
     if self._layout == None:
         self._layout = Pango.Layout(pango_context)
     layout = self._layout
     layout.set_text(self._text)
     layout.set_attributes(attrs)
     
     #find out where to draw the layout and calculate the maximum width
     width = rect.width
     if self._anchor in [ANCHOR_BOTTOM_LEFT, ANCHOR_TOP_LEFT,
                         ANCHOR_LEFT_CENTER]:
         width = rect.width - self._position[0]
     elif self._anchor in [ANCHOR_BOTTOM_RIGHT, ANCHOR_TOP_RIGHT,
                             ANCHOR_RIGHT_CENTER]:
         width = self._position[0]
     
     text_width, text_height = layout.get_pixel_size()
     width = width * math.cos(angle)
     width = min(width, self._max_width)
     
     if self._wrap:
         layout.set_wrap(Pango.WRAP_WORD_CHAR)
     layout.set_width(int(1000 * width))
     
     x, y = get_text_pos(layout, self._position, self._anchor, angle)
     
     if not self._fixed:
         #Find already drawn labels that would intersect with the current one
         #and adjust position to avoid intersection.
         text_width, text_height = layout.get_pixel_size()
         real_width = abs(text_width * math.cos(angle)) + abs(text_height * math.sin(angle))
         real_height = abs(text_height * math.cos(angle)) + abs(text_width * math.sin(angle))
         
         other_labels = get_registered_labels()
         this_rect = Gdk.Rectangle(int(x), int(y), int(real_width), int(real_height))
         for label in other_labels:
             label_rect = label.get_allocation()
             intersection = this_rect.intersect(label_rect)
             if intersection.width == 0 and intersection.height == 0:
                 continue
             
             y_diff = 0
             if label_rect.y <= y and label_rect.y + label_rect.height >= y:
                 y_diff = y - label_rect.y + label_rect.height
             elif label_rect.y > y and label_rect.y < y + real_height:
                 y_diff = label_rect.y - real_height - y
             y += y_diff
     
     #draw layout
     context.move_to(x, y)
     context.rotate(angle)
     context.set_source_rgb(*basics.color_gdk_to_cairo(self._color))
     context.show_layout(layout)
     context.rotate(-angle)
     context.stroke()
     
     #calculate the real dimensions
     text_width, text_height = layout.get_pixel_size()
     real_width = abs(text_width * math.cos(angle)) + abs(text_height * math.sin(angle))
     real_height = abs(text_height * math.cos(angle)) + abs(text_width * math.sin(angle))
     self._real_dimensions = real_width, real_height
     self._real_position = x, y
     self._line_count = layout.get_line_count()
     
     register_label(self)