def test_tts_writing_extent_when_body_has_extents(self): doc = model.ContentDocument() body = model.Body(doc) div = model.Div(doc) div.set_style( styles.StyleProperties.Extent, get_extent_from_dimensions(123, 456, styles.LengthType.Units.px)) p = model.P(doc) span = model.Span(doc) text = model.Text(doc) text.set_text("asdf") span.push_child(text) p.push_child(span) div.push_child(p) body.push_child(div) doc.set_body(body) r = model.Region("hello", doc) r.set_style( styles.StyleProperties.Extent, get_extent_from_dimensions(123, 456, styles.LengthType.Units.px)) doc.put_region(r) tree_from_model = imsc_writer.from_model(doc) extent = tree_from_model.getroot().attrib.get( f"{{{imsc_styles.StyleProperties.Extent.ns}}}{imsc_styles.StyleProperties.Extent.local_name}" ) self.assertEqual(extent, '1920px 1080px')
def get_extent(self) -> ExtentType: """Computes and returns the current paragraph extent, based on its content""" if len(self._caption_lines) == 0: return get_extent_from_dimensions(0, 0) paragraph_rows = self._caption_lines.keys() nb_lines = max(paragraph_rows) - min(paragraph_rows) + 1 max_text_lengths = max( [line.get_length() for line in self._caption_lines.values()]) return get_extent_from_dimensions(max_text_lengths, nb_lines)
def test_tts_writing_extent(self): self.assertEqual( _get_set_style( imsc_styles.StyleProperties.Extent, get_extent_from_dimensions(123, 456, styles.LengthType.Units.px)), '123px 456px')
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)
def _create_matching_region(self) -> Region: """Creates a new region based on paragraph needs""" doc_regions = list(self._doc.iter_regions()) paragraph_origin = self._paragraph.get_origin() region = Region(self._get_region_prefix() + str(len(doc_regions) + 1), self._doc) # Convert origin cells to percentages if self._paragraph.get_caption_style() is SccCaptionStyle.RollUp: # The region origin x offset region_origin = get_position_from_offsets(paragraph_origin.x.value, self._top) region_origin_pct = convert_cells_to_percentages( region_origin, self._doc.get_cell_resolution()) else: # The region origin matches with paragraph origin region_origin_pct = convert_cells_to_percentages( paragraph_origin, self._doc.get_cell_resolution()) region.set_style(StyleProperties.Origin, region_origin_pct) # The region width is initialized with he paragraph width (up to the right of the safe area) paragraph_extent = self._paragraph.get_extent() available_width = self._right - int(paragraph_origin.x.value) region_width = min(paragraph_extent.width.value, available_width) if self._paragraph.get_caption_style() is SccCaptionStyle.RollUp: # The region height extends from the top row to the bottom row of the safe area region_height = self._bottom - (self._top + 1) region.set_style(StyleProperties.DisplayAlign, DisplayAlignType.after) else: # The region height extends from its origin to the bottom of the safe area region_height = self._bottom - int(paragraph_origin.y.value) - 1 region.set_style(StyleProperties.DisplayAlign, DisplayAlignType.before) region_extent = get_extent_from_dimensions(region_width, region_height) # Convert extent cells to percentages region_extent_pct = convert_cells_to_percentages( region_extent, self._doc.get_cell_resolution()) region.set_style(StyleProperties.Extent, region_extent_pct) # Set default region style properties region.set_style(StyleProperties.ShowBackground, ShowBackgroundType.whenActive) self._doc.put_region(region) return region
def get_extent(self) -> ExtentType: """Computes and returns the current paragraph extent, based on its content""" lines: List[str] = [] last_line = [] separator = " " if self._caption_style is SccCaptionStyle.PaintOn else "" for caption_content in self._caption_contents: if isinstance(caption_content, SccCaptionLineBreak): lines.append(separator.join(last_line)) last_line = [] continue if isinstance(caption_content, SccCaptionText): last_line.append(caption_content.get_text()) if len(last_line) > 0: lines.append(separator.join(last_line)) nb_lines = len(lines) max_text_lengths = max([len(line) for line in lines]) if len(lines) > 0 else 0 return get_extent_from_dimensions(max_text_lengths, nb_lines)