Skip to content

FormattedTextArea

src.text_layers.FormattedTextArea

Bases: FormattedTextField

A FormattedTextField where the text is required to fit within a given area.

  • Reduces font size until the text fits within the reference layer's bounds.
  • Properly separates and formats flavor text with respect to an optional divider layer.
  • Centers the formatted text vertically with respect to the reference layer's bounds.
Source code in src\text_layers.py
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
class FormattedTextArea (FormattedTextField):
    """A FormattedTextField where the text is required to fit within a given area.

    * Reduces font size until the text fits within the reference layer's bounds.
    * Properly separates and formats flavor text with respect to an optional divider layer.
    * Centers the formatted text vertically with respect to the reference layer's bounds.
    """

    """
    * Properties
    """

    @cached_property
    def pt_reference(self) -> Optional[ReferenceLayer]:
        return self.kwargs.get('pt_reference', None)

    @cached_property
    def divider(self) -> Optional[Union[ArtLayer, LayerSet]]:
        """Divider layer, if provided and flavor text exists."""
        if (divider := self.kwargs.get('divider')) and all([self.flavor_text, self.contents, CFG.flavor_divider]):
            divider.visible = True
            return divider
        return

    @cached_property
    def scale_height(self) -> bool:
        """Scale text to fit reference height (Default: True)."""
        if scale_height := self.kwargs.get('scale_height'):
            return scale_height
        return True

    @cached_property
    def scale_width(self) -> bool:
        """Scale text to fit reference width (Default: False)."""
        if scale_width := self.kwargs.get('scale_width'):
            return scale_width
        return False

    @cached_property
    def fix_overflow_width(self) -> bool:
        """Scale text to fit bounding box width (Default: False)."""
        if fix_overflow_width := self.kwargs.get('fix_overflow_width'):
            return fix_overflow_width
        return True

    @cached_property
    def fix_overflow_height(self) -> bool:
        """Scale text to fit bounding box height (Default: If it overflows the bounds)."""
        if len(self.contents + self.flavor_text) > 280:
            return True
        if fix_overflow_height := self.kwargs.get('fix_overflow_height'):
            return fix_overflow_height
        return False

    """
    * Methods
    """

    def insert_divider(self):
        """Inserts and correctly positions flavor text divider."""

        # Create a reference layer with no effects
        flavor = self.layer.duplicate()
        rules = flavor.duplicate()
        flavor.rasterize(RasterizeType.EntireLayer)
        remove_trailing_text(rules, self.flavor_start)
        select_layer_bounds(rules, self.doc_selection)
        self.docref.activeLayer = flavor
        self.doc_selection.expand(2)
        self.doc_selection.clear()

        # Move flavor text to bottom, then position divider
        flavor.translate(0, self.layer.bounds[3] - flavor.bounds[3])
        position_between_layers(self.divider, rules, flavor)
        self.doc_selection.deselect()
        flavor.remove()
        rules.remove()

    def pre_scale_to_fit(self) -> Optional[float]:
        """Fix height overflow before formatting text."""
        contents = self.contents if not self.flavor_text else str(
            self.contents + "\r" + self.flavor_text)
        self.TI.contents = contents
        return scale_text_to_height(
            layer=self.layer,
            height=int(self.reference_dims['height']*1.1))

    def scale_to_fit(self, font_size: Optional[float] = None) -> None:
        """Scale font size to fit within any references."""

        # Scale layer to reference
        if self.reference_dims:

            # Resize the text until it fits the reference vertically
            if self.scale_height:
                font_size = scale_text_to_height(
                    layer=self.layer,
                    height=self.reference_dims['height'])

            # Resize the text until it fits the reference horizontally
            if self.scale_width:
                font_size = scale_text_to_width(
                    layer=self.layer,
                    width=self.reference_dims['width'],
                    step=0.2, font_size=font_size)

        # Resize the text until it fits the TextLayer bounding box
        if self.fix_overflow_width:
            scale_text_to_width_textbox(
                layer=self.layer, font_size=font_size)

    def position_within_reference(self):
        """Positions the layer with respect to the reference, if required."""

        # Ensure the layer is centered vertically
        dims = get_layer_dimensions(self.layer)
        self.layer.translate(0, self.reference_dims['center_y'] - dims['center_y'])

        # Ensure the layer is centered horizontally if needed
        if self.contents_centered and self.flavor_centered:
            self.layer.translate(0, self.reference_dims['center_x'] - dims['center_x'])

    def execute(self):

        # Skip if both are empty
        if not self.input:
            return
        font_size = None

        # Pre-scaling to prevent text overflow errors
        if self.fix_overflow_height and self.reference_dims:
            font_size = self.pre_scale_to_fit()

        # Execute text formatting
        super().execute()

        # Scale layer to fit reference
        self.scale_to_fit(font_size)

        # Position the layer properly
        if self.reference_dims:
            self.position_within_reference()

        # Insert flavor divider if needed
        if self.divider:
            self.insert_divider()

        # Shift vertically if the text overlaps the PT box
        if self.pt_reference:

            # Use newer methodology if top reference not provided
            delta = clear_reference_vertical(
                layer=self.layer,
                ref=self.pt_reference,
                docsel=self.doc_selection)

            # Shift the divider if layer was moved
            if delta < 0 and self.divider:
                self.divider.translate(0, delta)

Attributes

divider: Optional[Union[ArtLayer, LayerSet]]

Divider layer, if provided and flavor text exists.

fix_overflow_height: bool

Scale text to fit bounding box height (Default: If it overflows the bounds).

fix_overflow_width: bool

Scale text to fit bounding box width (Default: False).

scale_height: bool

Scale text to fit reference height (Default: True).

scale_width: bool

Scale text to fit reference width (Default: False).

Functions

insert_divider()

Inserts and correctly positions flavor text divider.

Source code in src\text_layers.py
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
def insert_divider(self):
    """Inserts and correctly positions flavor text divider."""

    # Create a reference layer with no effects
    flavor = self.layer.duplicate()
    rules = flavor.duplicate()
    flavor.rasterize(RasterizeType.EntireLayer)
    remove_trailing_text(rules, self.flavor_start)
    select_layer_bounds(rules, self.doc_selection)
    self.docref.activeLayer = flavor
    self.doc_selection.expand(2)
    self.doc_selection.clear()

    # Move flavor text to bottom, then position divider
    flavor.translate(0, self.layer.bounds[3] - flavor.bounds[3])
    position_between_layers(self.divider, rules, flavor)
    self.doc_selection.deselect()
    flavor.remove()
    rules.remove()

position_within_reference()

Positions the layer with respect to the reference, if required.

Source code in src\text_layers.py
782
783
784
785
786
787
788
789
790
791
def position_within_reference(self):
    """Positions the layer with respect to the reference, if required."""

    # Ensure the layer is centered vertically
    dims = get_layer_dimensions(self.layer)
    self.layer.translate(0, self.reference_dims['center_y'] - dims['center_y'])

    # Ensure the layer is centered horizontally if needed
    if self.contents_centered and self.flavor_centered:
        self.layer.translate(0, self.reference_dims['center_x'] - dims['center_x'])

pre_scale_to_fit() -> Optional[float]

Fix height overflow before formatting text.

Source code in src\text_layers.py
749
750
751
752
753
754
755
756
def pre_scale_to_fit(self) -> Optional[float]:
    """Fix height overflow before formatting text."""
    contents = self.contents if not self.flavor_text else str(
        self.contents + "\r" + self.flavor_text)
    self.TI.contents = contents
    return scale_text_to_height(
        layer=self.layer,
        height=int(self.reference_dims['height']*1.1))

scale_to_fit(font_size: Optional[float] = None) -> None

Scale font size to fit within any references.

Source code in src\text_layers.py
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
def scale_to_fit(self, font_size: Optional[float] = None) -> None:
    """Scale font size to fit within any references."""

    # Scale layer to reference
    if self.reference_dims:

        # Resize the text until it fits the reference vertically
        if self.scale_height:
            font_size = scale_text_to_height(
                layer=self.layer,
                height=self.reference_dims['height'])

        # Resize the text until it fits the reference horizontally
        if self.scale_width:
            font_size = scale_text_to_width(
                layer=self.layer,
                width=self.reference_dims['width'],
                step=0.2, font_size=font_size)

    # Resize the text until it fits the TextLayer bounding box
    if self.fix_overflow_width:
        scale_text_to_width_textbox(
            layer=self.layer, font_size=font_size)