Ejemplo n.º 1
0
    def plain_text(self) -> str:
        """
        Returns text content without formatting codes.

        .. versionadded:: 0.13

        """
        return plain_text(self.dxf.text)
Ejemplo n.º 2
0
    def _convert_entity(self):
        """ Calculates the rough border path for a single line text.

        Calculation is based on a mono-spaced font and therefore the border
        path is just an educated guess.

        Vertical text generation and oblique angle is ignored.

        """
        def get_text_rotation() -> float:
            if alignment in ('FIT', 'ALIGNED') and not p1.isclose(p2):
                return (p2 - p1).angle
            else:
                return math.degrees(text.dxf.rotation)

        def get_insert() -> Vec3:
            if alignment == 'LEFT':
                return p1
            elif alignment in ('FIT', 'ALIGNED'):
                return p1.lerp(p2, factor=0.5)
            else:
                return p2

        text = cast('Text', self.entity)
        if text.dxftype() == 'ATTDEF':
            # ATTDEF outside of a BLOCK renders the tag rather than the value
            content = text.dxf.tag
        else:
            content = text.dxf.text

        content = plain_text(content)
        if len(content) == 0:
            # empty path - does not render any vertices!
            self._path = Path()
            return

        p1: Vec3 = text.dxf.insert
        p2: Vec3 = text.dxf.align_point
        font = fonts.make_font(get_font_name(text), text.dxf.height,
                               text.dxf.width)
        text_line = TextLine(content, font)
        alignment: str = text.get_align()
        if text.dxf.halign > 2:  # ALIGNED=3, MIDDLE=4, FIT=5
            text_line.stretch(alignment, p1, p2)
        halign, valign = unified_alignment(text)
        corner_vertices = text_line.corner_vertices(get_insert(),
                                                    halign, valign,
                                                    get_text_rotation())

        ocs = text.ocs()
        self._path = Path.from_vertices(
            ocs.points_to_wcs(corner_vertices),
            close=True,
        )
Ejemplo n.º 3
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)
Ejemplo n.º 4
0
def test_plain_text():
    assert plain_text('%%d') == '°'
    # underline
    assert plain_text('%%u') == ''
    assert plain_text('%%utext%%u') == 'text'
    # single %
    assert plain_text('%u%d%') == '%u%d%'
    t = Text.new(dxfattribs={'text': '45%%d'})
    assert t.plain_text() == '45°'

    assert plain_text('abc^a') == 'abc!'
    assert plain_text('abc^Jdef') == 'abcdef'
    assert plain_text('abc^@def') == 'abc\0def'
Ejemplo n.º 5
0
    def _convert_entity(self):
        """Calculates the rough border path for a single line text.

        Calculation is based on a mono-spaced font and therefore the border
        path is just an educated guess.

        Vertical text generation and oblique angle is ignored.

        """
        def text_rotation():
            if fit_or_aligned and not p1.isclose(p2):
                return (p2 - p1).angle
            else:
                return math.radians(text.dxf.rotation)

        def location():
            if fit_or_aligned:
                return p1.lerp(p2, factor=0.5)
            return p1

        text = cast("Text", self.entity)
        if text.dxftype() == "ATTDEF":
            # ATTDEF outside of a BLOCK renders the tag rather than the value
            content = text.dxf.tag
        else:
            content = text.dxf.text

        content = plain_text(content)
        if len(content) == 0:
            # empty path - does not render any vertices!
            self._path = Path()
            return

        font = fonts.make_font(get_font_name(text), text.dxf.height,
                               text.dxf.width)
        text_line = TextLine(content, font)
        alignment, p1, p2 = text.get_placement()
        if p2 is None:
            p2 = p1
        fit_or_aligned = (alignment == TextEntityAlignment.FIT
                          or alignment == TextEntityAlignment.ALIGNED)
        if text.dxf.halign > 2:  # ALIGNED=3, MIDDLE=4, FIT=5
            text_line.stretch(alignment, p1, p2)
        halign, valign = unified_alignment(text)
        mirror_x = -1 if text.is_backward else 1
        mirror_y = -1 if text.is_upside_down else 1
        oblique: float = math.radians(text.dxf.oblique)
        corner_vertices = text_line.corner_vertices(
            location(),
            halign,
            valign,
            angle=text_rotation(),
            scale=(mirror_x, mirror_y),
            oblique=oblique,
        )

        ocs = text.ocs()
        self._path = from_vertices(
            ocs.points_to_wcs(corner_vertices),
            close=True,
        )
Ejemplo n.º 6
0
 def plain_text(self) -> str:
     """ Returns text content without formatting codes. """
     return plain_text(self.dxf.text)
Ejemplo n.º 7
0
def test_plain_text():
    assert plain_text("%%C") == "Ø"  # alt-0216
    assert plain_text("%%D") == "°"  # alt-0176
    assert plain_text("%%P") == "±"  # alt-0177
    # underline
    assert plain_text("%%u") == ""
    assert plain_text("%%utext%%u") == "text"
    # overline
    assert plain_text("%%o") == ""
    # strike through
    assert plain_text("%%k") == ""

    # single %
    assert plain_text("%u%d%") == "%u%d%"
    t = Text.new(dxfattribs={"text": "45%%d"})
    assert t.plain_text() == "45°"

    assert plain_text("abc^a") == "abc!"
    assert plain_text("abc^Jdef") == "abcdef"
    assert plain_text("abc^@def") == "abc\0def"
Ejemplo n.º 8
0
def test_plain_text():
    assert plain_text('%%C') == 'Ø'  # alt-0216
    assert plain_text('%%D') == '°'  # alt-0176
    assert plain_text('%%P') == '±'  # alt-0177
    # underline
    assert plain_text('%%u') == ''
    assert plain_text('%%utext%%u') == 'text'
    # overline
    assert plain_text('%%o') == ''
    # strike through
    assert plain_text('%%k') == ''

    # single %
    assert plain_text('%u%d%') == '%u%d%'
    t = Text.new(dxfattribs={'text': '45%%d'})
    assert t.plain_text() == '45°'

    assert plain_text('abc^a') == 'abc!'
    assert plain_text('abc^Jdef') == 'abcdef'
    assert plain_text('abc^@def') == 'abc\0def'