예제 #1
0
  def _extend_region_to_paragraph(self, region: Region):
    """Extends region dimensions based on paragraph extent"""
    if not self._has_same_origin_as_region(region):
      raise ValueError("Paragraph origin does not match with region.")

    region_extent = region.get_style(StyleProperties.Extent)

    paragraph_extent = self._paragraph.get_extent()

    # Convert extent cells to percentages
    paragraph_extent_pct = convert_cells_to_percentages(paragraph_extent, self._doc.get_cell_resolution())

    if paragraph_extent_pct.width.value > region_extent.width.value:
      # Resets the region width on the paragraph line width (up to the right of the safe area)
      # The region height always remains the same (depending on the region origin)
      region_origin: PositionType = region.get_style(StyleProperties.Origin)

      # Convert right cells coordinate to percentages
      right_pct = self._right * 100 / self._doc.get_cell_resolution().columns
      available_width_pct = right_pct - region_origin.x.value

      if int(paragraph_extent_pct.width.value) > available_width_pct:
        LOGGER.warning("The paragraph width overflows from the safe area (at %s)", self._paragraph.get_begin())

      max_width_pct = round(min(paragraph_extent_pct.width.value, available_width_pct))

      region_extent_pct = get_extent_from_dimensions(max_width_pct, region_extent.height.value, LengthType.Units.pct)
      region.set_style(StyleProperties.Extent, region_extent_pct)
예제 #2
0
  def _has_same_origin_as_region(self, region: Region) -> bool:
    """Checks whether the region origin is the same as the paragraph origin"""
    region_origin: Optional[PositionType] = region.get_style(StyleProperties.Origin)

    # Convert paragraph origin units into percentages
    paragraph_origin = convert_cells_to_percentages(self._paragraph.get_origin(), self._doc.get_cell_resolution())

    if self._paragraph.get_caption_style() in (SccCaptionStyle.RollUp, SccCaptionStyle.PaintOn):
      return region_origin \
             and region_origin.x.units is paragraph_origin.x.units \
             and region_origin.y.units is paragraph_origin.y.units \
             and math.isclose(region_origin.x.value, paragraph_origin.x.value, abs_tol=0.001) \
             and region_origin.y.value <= paragraph_origin.y.value

    return region_origin \
       and region_origin.x.units is paragraph_origin.x.units \
       and region_origin.y.units is paragraph_origin.y.units \
       and math.isclose(region_origin.x.value, paragraph_origin.x.value, abs_tol=0.001) \
       and math.isclose(region_origin.y.value, paragraph_origin.y.value, abs_tol=0.001)
예제 #3
0
    def test_process_isd(self):
        supported_style_properties = SupportedStylePropertiesFilter({
            StyleProperties.BackgroundColor: [NamedColors.red.value],
            StyleProperties.Extent: []
        })

        doc = ContentDocument()

        r1 = Region("r1", doc)
        r1.set_style(StyleProperties.BackgroundColor, NamedColors.red.value)
        r1.set_style(StyleProperties.LuminanceGain, 2.0)
        doc.put_region(r1)

        b = Body(doc)
        b.set_begin(Fraction(1))
        b.set_end(Fraction(10))
        doc.set_body(b)

        div1 = Div(doc)
        div1.set_region(r1)
        b.push_child(div1)

        p1 = P(doc)
        p1.set_style(StyleProperties.BackgroundColor, NamedColors.white.value)
        p1.set_style(StyleProperties.Direction, DirectionType.rtl)
        div1.push_child(p1)

        span1 = Span(doc)
        span1.set_style(StyleProperties.BackgroundColor, NamedColors.red.value)
        span1.set_style(StyleProperties.FontStyle, FontStyleType.italic)
        span1.set_style(StyleProperties.Direction, DirectionType.ltr)
        p1.push_child(span1)

        t1 = Text(doc, "hello")
        span1.push_child(t1)

        significant_times = sorted(ISD.significant_times(doc))
        self.assertEqual(3, len(significant_times))

        isd = ISD.from_model(doc, significant_times[1])

        r1 = isd.get_region("r1")

        self.assertEqual(len(Region._applicableStyles), len(r1._styles))
        self.assertEqual(NamedColors.red.value,
                         r1.get_style(StyleProperties.BackgroundColor))
        self.assertEqual(2.0, r1.get_style(StyleProperties.LuminanceGain))

        body1 = list(r1)[0]
        div1 = list(body1)[0]
        p1 = list(div1)[0]
        span1 = list(p1)[0]

        self.assertEqual(len(P._applicableStyles), len(p1._styles))
        self.assertEqual(NamedColors.white.value,
                         p1.get_style(StyleProperties.BackgroundColor))
        self.assertEqual(DirectionType.rtl,
                         p1.get_style(StyleProperties.Direction))

        self.assertEqual(len(Span._applicableStyles), len(span1._styles))
        self.assertEqual(NamedColors.red.value,
                         span1.get_style(StyleProperties.BackgroundColor))
        self.assertEqual(FontStyleType.italic,
                         span1.get_style(StyleProperties.FontStyle))
        self.assertEqual(DirectionType.ltr,
                         span1.get_style(StyleProperties.Direction))

        supported_style_properties.process(isd)

        self.assertEqual(2, len(r1._styles))
        self.assertEqual(NamedColors.red.value,
                         r1.get_style(StyleProperties.BackgroundColor))
        self.assertEqual(
            ExtentType(height=LengthType(value=0.0, units=LengthType.Units.rh),
                       width=LengthType(value=0.0, units=LengthType.Units.rw)),
            r1.get_style(StyleProperties.Extent))

        self.assertEqual(0, len(p1._styles))
        self.assertIsNone(p1.get_style(StyleProperties.BackgroundColor))
        self.assertIsNone(p1.get_style(StyleProperties.Direction))

        self.assertEqual(1, len(span1._styles))
        self.assertEqual(NamedColors.red.value,
                         span1.get_style(StyleProperties.BackgroundColor))
        self.assertIsNone(span1.get_style(StyleProperties.FontStyle))
        self.assertIsNone(span1.get_style(StyleProperties.Direction))
예제 #4
0
    def test_process_isd(self):
        default_style_value_filter = DefaultStylePropertyValuesFilter({
            StyleProperties.BackgroundColor:
            NamedColors.red.value,
            StyleProperties.Direction:
            DirectionType.ltr
        })

        doc = ContentDocument()

        r1 = Region("r1", doc)
        r1.set_style(StyleProperties.BackgroundColor, NamedColors.red.value)
        r1.set_style(StyleProperties.LuminanceGain, 2.0)
        doc.put_region(r1)

        b = Body(doc)
        b.set_begin(Fraction(1))
        b.set_end(Fraction(10))
        doc.set_body(b)

        div1 = Div(doc)
        div1.set_region(r1)
        b.push_child(div1)

        p1 = P(doc)
        p1.set_style(StyleProperties.BackgroundColor, NamedColors.white.value)
        p1.set_style(StyleProperties.Direction, DirectionType.rtl)
        div1.push_child(p1)

        span1 = Span(doc)
        span1.set_style(StyleProperties.BackgroundColor, NamedColors.red.value)
        span1.set_style(StyleProperties.FontStyle, FontStyleType.italic)
        span1.set_style(StyleProperties.Direction, DirectionType.ltr)
        p1.push_child(span1)

        t1 = Text(doc, "hello")
        span1.push_child(t1)

        significant_times = sorted(ISD.significant_times(doc))
        self.assertEqual(3, len(significant_times))

        isd = ISD.from_model(doc, significant_times[1])

        r1 = isd.get_region("r1")

        self.assertEqual(len(Region._applicableStyles), len(r1._styles))
        self.assertEqual(NamedColors.red.value,
                         r1.get_style(StyleProperties.BackgroundColor))
        self.assertEqual(2.0, r1.get_style(StyleProperties.LuminanceGain))

        body1 = list(r1)[0]
        div1 = list(body1)[0]
        p1 = list(div1)[0]
        span1 = list(p1)[0]

        self.assertEqual(len(P._applicableStyles), len(p1._styles))
        self.assertEqual(NamedColors.white.value,
                         p1.get_style(StyleProperties.BackgroundColor))
        self.assertEqual(DirectionType.rtl,
                         p1.get_style(StyleProperties.Direction))

        self.assertEqual(len(Span._applicableStyles), len(span1._styles))
        self.assertEqual(NamedColors.red.value,
                         span1.get_style(StyleProperties.BackgroundColor))
        self.assertEqual(FontStyleType.italic,
                         span1.get_style(StyleProperties.FontStyle))
        self.assertEqual(DirectionType.ltr,
                         span1.get_style(StyleProperties.Direction))

        default_style_value_filter.process(isd)

        self.assertEqual(len(Region._applicableStyles) - 1, len(r1._styles))
        self.assertIsNone(r1.get_style(StyleProperties.BackgroundColor))

        self.assertEqual(len(P._applicableStyles), len(p1._styles))
        self.assertEqual(NamedColors.white.value,
                         p1.get_style(StyleProperties.BackgroundColor))
        self.assertEqual(DirectionType.rtl,
                         p1.get_style(StyleProperties.Direction))

        self.assertEqual(len(Span._applicableStyles) - 1, len(span1._styles))
        self.assertIsNone(span1.get_style(StyleProperties.BackgroundColor))
        self.assertEqual(FontStyleType.italic,
                         span1.get_style(StyleProperties.FontStyle))
        self.assertEqual(DirectionType.ltr,
                         span1.get_style(StyleProperties.Direction))