Ejemplo n.º 1
0
def newLimitedWidthTextLayout(widget, text, width, font=None, truncate=True, markup=True):
    if not font:
        font = ui.getFont()
    layout = widget.create_pango_layout("")
    if markup:
        layout.set_markup(text)
    else:
        layout.set_text(text)
    layout.set_font_description(pfontEncode(font))
    if not layout:
        return None
    layoutW, layoutH = layout.get_pixel_size()
    if layoutW > width:
        if truncate:
            char_w = layoutW / len(text)
            char_num = int(width // char_w)
            while layoutW > width:
                text = cutText(text, char_num)
                layout = widget.create_pango_layout(text)
                layout.set_font_description(pfontEncode(font))
                layoutW, layoutH = layout.get_pixel_size()
                char_num -= max(int((layoutW - width) // char_w), 1)
                if char_num < 0:
                    layout = None
                    break
        else:  ## use smaller font
            font2 = list(font)
            while layoutW > width:
                font2[3] = 0.9 * font2[3] * width / layoutW
                layout.set_font_description(pfontEncode(font2))
                layoutW, layoutH = layout.get_pixel_size()
                # print layoutW, width
            # print
    return layout
Ejemplo n.º 2
0
def newTextLayout(widget, text='', font=None, maxSize=None, truncate=False):
    layout = widget.create_pango_layout('') ## a pango.Layout object
    if not font:
        font = ui.getFont()
    layout.set_font_description(pfontEncode(font))
    if text:
        layout.set_markup(text)
        if maxSize:
            (maxW, maxH) = maxSize
            layoutW, layoutH = layout.get_pixel_size()
            if layoutH > maxH:
                font[3] = int(font[3]*maxH//layoutH)
                layout.set_font_description(pfontEncode(font))
            layoutW, layoutH = layout.get_pixel_size()
            if layoutW > maxW:
                if truncate:
                    char_w = layoutW/len(text)
                    char_num = int(maxW//char_w)
                    while layoutW > maxW:
                        text = cutText(text, char_num)
                        layout.set_markup(text)
                        layout.set_font_description(pfontEncode(font))
                        layoutW, layoutH = layout.get_pixel_size()
                        char_num -= max(int((layoutW-maxW)//char_w), 1)
                        if char_num<0:
                            layout = None
                            break
                else:## use smaller font, don't truncate text
                    font[3] = int(font[3]*float(maxW)/layoutW)
                    layout.set_font_description(pfontEncode(font))
    return layout
Ejemplo n.º 3
0
def newTextLayout(
	widget,
	text='',
	font=None,
	maxSize=None,
	maximizeScale=0.6,
	truncate=False,
):
	'''
		None return value should be expected and handled, only if maxSize is given
	'''
	layout = widget.create_pango_layout('') ## a Pango.Layout object
	if font:
		font = list(font)
	else:
		font = ui.getFont()
	layout.set_font_description(pfontEncode(font))
	if text:
		layout.set_markup(text)
		if maxSize:
			layoutW, layoutH = layout.get_pixel_size()
			##
			maxW, maxH = maxSize
			maxW = float(maxW)
			maxH = float(maxH)
			if maxW <= 0:
				return
			if maxH <= 0:
				minRat = 1.0
			else:
				minRat = 1.01 * layoutH/maxH ## FIXME
			if truncate:
				if minRat > 1:
					font[3] = int(font[3]/minRat)
				layout.set_font_description(pfontEncode(font))
				layoutW, layoutH = layout.get_pixel_size()
				if layoutW > 0:
					char_w = float(layoutW)/len(text)
					char_num = int(maxW//char_w)
					while layoutW > maxW:
						text = cutText(text, char_num)
						if not text:
							break
						layout = widget.create_pango_layout(text)
						layout.set_font_description(pfontEncode(font))
						layoutW, layoutH = layout.get_pixel_size()
						char_num -= max(int((layoutW-maxW)//char_w), 1)
						if char_num<0:
							layout = None
							break
			else:
				if maximizeScale > 0:
					minRat = minRat/maximizeScale
				if minRat < layoutW/maxW:
					minRat = layoutW/maxW
				if minRat > 1:
					font[3] = int(font[3]/minRat)
				layout.set_font_description(pfontEncode(font))
	return layout
Ejemplo n.º 4
0
def newTextLayout(
    widget,
    text='',
    font=None,
    maxSize=None,
    maximizeScale=0.6,
    truncate=False,
):
    '''
        None return value should be expected and handled, only if maxSize is given
    '''
    layout = widget.create_pango_layout('') ## a pango.Layout object
    if not font:
        font = ui.getFont()
    layout.set_font_description(pfontEncode(font))
    if text:
        layout.set_markup(text)
        if maxSize:
            layoutW, layoutH = layout.get_pixel_size()
            ##
            maxW, maxH = maxSize
            maxW = float(maxW)
            maxH = float(maxH)
            if maxW <= 0:
                return
            if maxH <= 0:
                minRat = 1.0
            else:
                minRat = 1.01 * layoutH/maxH ## FIXME
            if truncate:
                if minRat > 1:
                    font[3] = int(font[3]/minRat)
                layout.set_font_description(pfontEncode(font))
                layoutW, layoutH = layout.get_pixel_size()
                if layoutW > 0:
                    char_w = float(layoutW)/len(text)
                    char_num = int(maxW//char_w)
                    while layoutW > maxW:
                        text = cutText(text, char_num)
                        if not text:
                            break
                        layout = widget.create_pango_layout(text)
                        layout.set_font_description(pfontEncode(font))
                        layoutW, layoutH = layout.get_pixel_size()
                        char_num -= max(int((layoutW-maxW)//char_w), 1)
                        if char_num<0:
                            layout = None
                            break
            else:
                if maximizeScale > 0:
                    minRat = minRat/maximizeScale
                if minRat < layoutW/maxW:
                    minRat = layoutW/maxW
                if minRat > 1:
                    font[3] = int(font[3]/minRat)
                layout.set_font_description(pfontEncode(font))
    return layout