예제 #1
0
파일: split_lib.py 프로젝트: acordts/area54
def split_to_line_objects_v2(text, line_cnt, delimiter=' ', opt_len=0):
    '''
    @summary: split text objects in almost equal chunks
    @param text: text to split
    @type text: str
    @param line_cnt: amount of required lines
    @type line_cnt: int
    @param delimiter: delimiter element
    @type delimiter: char
    @param opt_len: optimal line length in pixel
    @type opt_len: float
    @return: splitted text lines
    @rtype: list of TextObject 
    @note: unused spacer will be removed, line length will be equal for all lines
           lines contain as many words as possible, thats why the last line could be
           nearly empty
    '''
    if not opt_len:
        deleted_spacer = len(TextObject(10 * delimiter))
        opt_len = (len(TextObject(text)) - deleted_spacer) / line_cnt

    delimiter_width = len(TextObject(delimiter))
    words = text.split(delimiter)

    line = TextObject()

    for word_nr in xrange(len(words)):
        new_text = str(line) + words[word_nr]
        line.text_str(new_text)

        if len(line) + delimiter_width >= opt_len:
            break
        line.text_str(str(line) + delimiter)

    text = delimiter.join(words[word_nr + 1:])
    
    if line_cnt == 1:
        return [line]
    elif line_cnt == 2:
        return [line, TextObject(text)]

    lines = [line]
    lines.extend(split_to_line_objects_v2(text, line_cnt - 1, delimiter, opt_len))
    return lines
예제 #2
0
파일: split_lib.py 프로젝트: acordts/area54
def opt_line_cnt(box_w, box_h, text):
    '''
    @param box_w: width of text box in pixel
    @type box_w: int
    @param box_h: height of text box in pixel
    @type box_h: int
    @param text: text to scale
    @type text: str
    @return: best line length and line count to fit in box 
    @rtype: int
    '''
    txt_obj = TextObject(text)
    txt_width = len(txt_obj)
    line_height = txt_obj.get_line_height()

    box_ratio = 1.0 * box_h / box_w
    max_lines = int(math.floor(box_h/line_height))
        
    for i in xrange(1, max_lines + 1):
        new_ratio = 1.0 * (line_height * i) / (txt_width / i)
        if new_ratio > box_ratio:
            break
    return i
예제 #3
0
파일: split_lib.py 프로젝트: acordts/area54
def split_to_line_objects(text, line_cnt, delimiter=' '):
    '''
    @summary: split text objects in almost equal chunks
    @param text: text to split
    @type text: str
    @param line_cnt: amount of required lines
    @type line_cnt: int
    @param delimiter: delimiter element
    @type delimiter: char
    @param opt_len: optimal line length in pixel
    @type opt_len: float
    @return: splitted text lines
    @rtype: list of TextObject
    @note: text is more homogene than in the 2nd implementation
    '''
    opt_len = len(TextObject(text)) / line_cnt

    delimiter_width = len(TextObject(delimiter))
    words = text.split(delimiter)

    line = TextObject()

    for word_nr in xrange(len(words)):
        new_text = str(line) + words[word_nr]
        line.text_str(new_text)

        if len(line) + delimiter_width >= opt_len:
            break
        line.text_str(str(line) + delimiter)

    text = delimiter.join(words[word_nr + 1:])
    if line_cnt == 2:
        return [line, TextObject(text)]

    lines = [line]
    lines.extend(split_to_line_objects(text, line_cnt - 1, delimiter))
    return lines