Exemplo n.º 1
0
def _split_into_lines(entity: AnyText, box_width: Optional[float],
                      get_text_width: Callable[[str], float]) -> List[str]:
    if isinstance(entity, AttDef):
        # ATTDEF outside of an Insert renders the tag rather than the value
        text = plain_text(entity.dxf.tag)
    else:
        text = entity.plain_text()
    if isinstance(entity, (Text, Attrib, AttDef)):
        assert '\n' not in text
        return [text]
    else:
        return text_wrap(text, box_width, get_text_width)
Exemplo n.º 2
0
 def get_content() -> List[str]:
     text = mtext.plain_text(split=False)
     return text_wrap(text, box_width, font.text_width)
Exemplo n.º 3
0
def test_text_wrapping():
    def get_text_width(s: str) -> float:
        return len(s)

    assert text_wrap('', 0, get_text_width) == []
    assert text_wrap('   \n    ', 1, get_text_width) == []

    assert text_wrap('abc', 0, get_text_width) == ['abc']
    assert text_wrap(' abc', 6,
                     get_text_width) == [' abc'], "preserve leading spaces"
    assert text_wrap('abc ', 1,
                     get_text_width) == ['abc'], "do not wrap too long words"
    assert text_wrap(' abc ', 6,
                     get_text_width) == [' abc'], "remove trailing spaces"

    assert text_wrap('abc\ndef', 1,
                     get_text_width) == ['abc',
                                         'def'], "do not wrap too long words"
    assert text_wrap('   abc\ndef', 1,
                     get_text_width) == ['', 'abc', 'def'
                                         ], "leading spaces can cause wrapping"
    assert text_wrap('   abc\ndef', 6, get_text_width) == ['   abc', 'def']
    assert text_wrap('abc    \n    def', 1, get_text_width) == ['abc', 'def']

    assert text_wrap(' a ', 1, get_text_width) == ['', 'a']
    assert text_wrap('\na ', 2, get_text_width) == ['', 'a']
    assert text_wrap(' \na ', 1, get_text_width) == ['', 'a']
    assert text_wrap(' \n \n ', 1, get_text_width) == []
    assert text_wrap(' \n \n a', 1, get_text_width) == ['', '', 'a']

    assert text_wrap('  abc', 6, get_text_width) == ['  abc']
    assert text_wrap('  abc def', 6, get_text_width) == ['  abc', 'def']
    assert text_wrap('  abc def  ', 6, get_text_width) == ['  abc', 'def']
    assert text_wrap('  abc def', 1, get_text_width) == ['', 'abc', 'def']
    assert text_wrap('  abc def', 6, get_text_width) == ['  abc', 'def']
Exemplo n.º 4
0
def test_text_wrapping():
    def get_text_width(s: str) -> float:
        return len(s)

    assert text_wrap("", 0, get_text_width) == []
    assert text_wrap("   \n    ", 1, get_text_width) == []

    assert text_wrap("abc", 0, get_text_width) == ["abc"]
    assert text_wrap(" abc", 6, get_text_width) == [
        " abc"
    ], "preserve leading spaces"
    assert text_wrap("abc ", 1, get_text_width) == [
        "abc"
    ], "do not wrap too long words"
    assert text_wrap(" abc ", 6, get_text_width) == [
        " abc"
    ], "remove trailing spaces"

    assert text_wrap("abc\ndef", 1, get_text_width) == [
        "abc",
        "def",
    ], "do not wrap too long words"
    assert text_wrap("   abc\ndef", 1, get_text_width) == [
        "",
        "abc",
        "def",
    ], "leading spaces can cause wrapping"
    assert text_wrap("   abc\ndef", 6, get_text_width) == ["   abc", "def"]
    assert text_wrap("abc    \n    def", 1, get_text_width) == ["abc", "def"]

    assert text_wrap(" a ", 1, get_text_width) == ["", "a"]
    assert text_wrap("\na ", 2, get_text_width) == ["", "a"]
    assert text_wrap(" \na ", 1, get_text_width) == ["", "a"]
    assert text_wrap(" \n \n ", 1, get_text_width) == []
    assert text_wrap(" \n \n a", 1, get_text_width) == ["", "", "a"]

    assert text_wrap("  abc", 6, get_text_width) == ["  abc"]
    assert text_wrap("  abc def", 6, get_text_width) == ["  abc", "def"]
    assert text_wrap("  abc def  ", 6, get_text_width) == ["  abc", "def"]
    assert text_wrap("  abc def", 1, get_text_width) == ["", "abc", "def"]
    assert text_wrap("  abc def", 6, get_text_width) == ["  abc", "def"]