Пример #1
0
    def update_layout(self, rectangle=None):
        if rectangle is None:
            rectangle = self.allocation
        # print 'updating layout'
        self.available_width = rectangle[2] - 2 * BORDER_SIZE
        self.cell_width = self.thumbnail_width + 2 * CELL_BORDER_WIDTH
        self.cell_height = self.thumbnail_height + 2 * CELL_BORDER_WIDTH
        self.cells_per_row = max(int(self.available_width / self.cell_width),
                                 1)
        self.cell_width += \
            (self.available_width - self.cells_per_row * self.cell_width) \
            / self.cells_per_row

        if True:  # display date
            metrics = self.get_pango_context().\
                get_metrics(self.style.font_desc, pango.Language('en_US'))
            self.cell_height += pango.PIXELS(metrics.get_ascent() +
                                             metrics.get_descent())

        self.num_thumbnails = len(self.items)
        self.num_rows = int(self.num_thumbnails / self.cells_per_row)
        if self.num_thumbnails % self.cells_per_row:
            self.num_rows += 1

        self.height = int(self.num_rows * self.cell_height + 2 * BORDER_SIZE)

        vadjustment = self.get_vadjustment()
        hadjustment = self.get_hadjustment()
        vadjustment.step_increment = self.cell_height
        x = hadjustment.value
        y = self.height * self.scroll_value
        self.set_size(x, y, self.allocation.width, self.height)
Пример #2
0
def draw_on_surface(surface, text, params):
    """Draw the string on a pre-created surface and return height."""
    pangocairo_ctx = pangocairo.CairoContext(cairo.Context(surface))
    layout = pangocairo_ctx.create_layout()

    pango_ctx = layout.get_context()
    if params.language is not None:
        pango_ctx.set_language(pango.Language(params.language))

    if params.rtl:
        if params.vertical:
            base_dir = pango.DIRECTION_TTB_RTL
        else:
            base_dir = pango.DIRECTION_RTL
        alignment = pango.ALIGN_RIGHT
    else:
        if params.vertical:
            base_dir = pango.DIRECTION_TTB_LTR
        else:
            base_dir = pango.DIRECTION_LTR
        alignment = pango.ALIGN_LEFT

    pango_ctx.set_base_dir(base_dir)
    layout.set_alignment(alignment)

    layout.set_width(params.width * pango.SCALE)
    layout.set_spacing((params.line_spacing - params.font_size) * pango.SCALE)

    font = pango.FontDescription()
    font.set_family(params.family)
    font.set_size(params.font_size * pango.SCALE)
    font.set_style(params.style)
    font.set_weight(params.weight)

    layout.set_font_description(font)

    layout.set_text(text)

    #    # Doesn't work for some reason
    #    pango_ctx.set_base_gravity(pango.GRAVITY_AUTO)
    #    matrix = pango_ctx.get_matrix()
    #    matrix.rotate(90)
    #    pango_ctx.set_matrix(matrix)
    #    layout.context_changed()

    extents = layout.get_pixel_extents()
    top_usage = min(extents[0][1], extents[1][1], 0)
    bottom_usage = max(extents[0][3], extents[1][3])

    pangocairo_ctx.set_antialias(cairo.ANTIALIAS_GRAY)
    pangocairo_ctx.set_source_rgb(1, 1, 1)  # White background
    pangocairo_ctx.paint()

    pangocairo_ctx.translate(0, -top_usage)
    pangocairo_ctx.set_source_rgb(0, 0, 0)  # Black text color
    pangocairo_ctx.show_layout(layout)

    return bottom_usage - top_usage
Пример #3
0
def draw_on_surface(surface, text, params):
    """Draw the string on a pre-created surface and return height."""
    pangocairo_ctx = pangocairo.CairoContext(cairo.Context(surface))
    layout = pangocairo_ctx.create_layout()

    pango_ctx = layout.get_context()
    if params.language is not None:
        pango_ctx.set_language(pango.Language(params.language))

    if params.rtl:
        if params.vertical:
            base_dir = pango.DIRECTION_TTB_RTL
        else:
            base_dir = pango.DIRECTION_RTL
        alignment = pango.ALIGN_RIGHT
    else:
        if params.vertical:
            base_dir = pango.DIRECTION_TTB_LTR
        else:
            base_dir = pango.DIRECTION_LTR
        alignment = pango.ALIGN_LEFT

    # The actual meaning of alignment is confusing.
    #
    # In an RTL context, RTL text aligns to the right by default.  So
    # setting right alignment and an RTL context means asking for
    # 'default alignment' (just as does setting left alignment and an
    # LTR context).
    #
    # What actually happens depends on the directionality of the actual
    # text in the paragraph. If the text is Arabic this will be RTL, so
    # it is aligned to the right, the default alignment for RTL text.
    # And if the text is English this will be LTR, so it is aligned to
    # the left, the default alignment for LTR text.
    #
    # This is reversed when the context and the alignment disagree:
    # setting left alignment in an RTL context (or right alignment in an
    # LTR context) means asking for 'opposite alignment'.  Arabic text
    # is aligned to the left, and English text to the right.
    #
    # pango layout set_auto_dir controls whether the text direction
    # is based on the text itself, or influenced by the context.  By
    # default it is off so the text direction is completely independent
    # of the setting of the context: Arabic text is RTL and English text
    # is LTR.  However, the algorithm depends on the first 'strongly
    # directional' character encountered in a paragraph.  If you have
    # text that is largly Arabic but happens to start with English
    # (e.g. brand names) it will be assigned LTR, the wrong direction.
    # Either you force the correct direction by munging the text or you
    # tell pango to use the context.
    #
    # The text will be reordered based on the unicode bidi attributes
    # of the characters, and this is only as good as your unicode data.
    # Newly-encoded scripts can be newer than your libraries and will
    # likely order LTR if you implementation doesn't know about them.

    font = pango.FontDescription()
    font.set_family(params.family)
    font.set_size(params.font_size * pango.SCALE)
    font.set_style(params.style)
    font.set_weight(params.weight)
    font.set_stretch(params.stretch)

    layout.set_font_description(font)
    layout.set_alignment(alignment)
    layout.set_width(params.width * pango.SCALE)
    layout.set_wrap(pango.WRAP_WORD_CHAR)
    layout.set_spacing((params.line_spacing - params.font_size) * pango.SCALE)

    pango_ctx.set_base_dir(base_dir)
    layout.context_changed()
    layout.set_text(text)

    extents = layout.get_pixel_extents()
    top_usage = min(extents[0][1], extents[1][1], 0)
    bottom_usage = max(extents[0][3], extents[1][3])

    pangocairo_ctx.set_antialias(cairo.ANTIALIAS_GRAY)
    pangocairo_ctx.set_source_rgb(1, 1, 1)  # White background
    pangocairo_ctx.paint()

    pangocairo_ctx.translate(0, -top_usage)
    pangocairo_ctx.set_source_rgb(0, 0, 0)  # Black text color
    pangocairo_ctx.show_layout(layout)

    return bottom_usage - top_usage
Пример #4
0
context.fill()
#get font families:

font_map = pangocairo.cairo_font_map_get_default()
families = font_map.list_families()
print families[0]

# to see family names:
print sorted([f.get_name() for f in font_map.list_families()])

font_name = [
    'Noto Sans Mono CJK SC', 'WenQuanYi Micro Hei Mono',
    'YaHei Consolas Hybrid', 'Menlo Regular'
][1]
pc = pango.Context()
pc.set_language(pango.Language("zh_CN.UTF-8"))

font = pango.FontDescription(' '.join([font_name, str(15)]))

context.translate(50, 25)

f_o = cairo.FontOptions()
f_o.set_antialias(cairo.ANTIALIAS_NONE)
f_o.set_hint_style(cairo.HINT_STYLE_SLIGHT)
f_o.set_hint_metrics(cairo.HINT_METRICS_ON)

context.set_font_options(f_o)

p_c = pangocairo_context = pangocairo.CairoContext(context)
#pangocairo_context.set_antialias(cairo.ANTIALIAS_SUBPIXEL)
Пример #5
0
def get_language(code):
    """Provide a pango.Language and keep it for reuse."""
    global _languages
    if not code in _languages:
        _languages[code] = pango.Language(code)
    return _languages[code]