def test_equal(self): a = AttrList() b = a.copy() try: assert a == b except AttributeError: warnings.warn(("AttrList can't be compared." "Pango version more that 1.46.0 is required.")) with self.assertRaises(NotImplementedError): assert a == 2
def test_set_attributes(self): context = Context() layout = Layout(context) layout.set_text("Working?") attr = Attribute.from_size(5, 1, 4) attr_list = AttrList() attr_list.insert(attr) layout.set_attributes(attr_list) layout.get_attributes() # Resetting the attributes layout.set_attributes(None) layout.get_attributes()
def test_copy(self): a = AttrList() b = copy.copy(a) try: assert a == b except AttributeError: warnings.warn(("AttrList can't be compared." "Pango version more that 1.46.0 is required.")) c = copy.deepcopy(a) try: assert a == c except AttributeError: warnings.warn(("AttrList can't be compared." "Pango version more that 1.46.0 is required."))
def test_from_pointer(self): a = AttrList() a.copy() b = AttrList.from_pointer(a.get_pointer()) try: assert a == b except AttributeError: warnings.warn(("AttrList can't be compared." "Pango version more that 1.46.0 is required."))
def get_text_and_attributes(parsed_text, style: TextStyle, styles, text_scale: float) -> Tuple[str, AttrList]: attr_list = AttrList() base_style = [styles["default"].compose(style)] start = 0 text = "" def push_attrs(style, start_index, end_index): for attr in style_to_attributes(style, text_scale, start_index, end_index): attr_list.insert(attr) def active_style(): return [style for style in reversed(base_style) if style is not None][0] line = "" for (token, value) in parsed_text: if token == "text": line += value text += value elif token == "newline": for _ in range(value): if not line: # This fixes the height of empty lines text += INVISIBLE_SPACE text += "\n" line = "" elif token == "begin": push_attrs(active_style(), start, byte_length(text)) start = byte_length(text) if value.startswith("#"): base_style.append(None) else: base_style.append(active_style().compose(styles[value])) elif token == "end": push_attrs(active_style(), start, byte_length(text)) start = byte_length(text) base_style.pop() if start < byte_length(text): push_attrs(base_style[0], start, byte_length(text)) assert len(base_style) == 1 assert base_style[0] is not None return text, attr_list
def test_init(self): a = AttrList() a.get_pointer()
def test_splice(self): # Idk how this works # copied from # https://gitlab.gnome.org/GNOME/pango/-/blob/master/tests/testattributes.c#L249 base = AttrList() attr = Attribute.from_size(10, 0, -1) base.insert(attr) list = base.copy() other = AttrList() list.splice(other, 11, 5) list = base.copy() other = AttrList() attr = Attribute.from_size(20) attr.start_index = 0 attr.end_index = 3 other.insert(attr) attr1 = Attribute.from_stretch(Stretch.SEMI_CONDENSED) attr1.start_index = 2 attr1.end_index = 4 other.insert(attr1) list.splice(other, 11, 5)
def test_change(self): a = AttrList() b = Attribute.from_size(5, 1, 6) a.change(b)
def test_insert(self): a = AttrList() b = Attribute.from_size(5, 1, 4) a.insert(b) c = Attribute.from_foreground_alpha(6, 2, 4) a.insert_before(c)
def test_ref_and_unref(self): a = AttrList() a._ref() a._unref()
def test_attr_list_returns_null_from_null_pointer(self): with self.assertRaises(ValueError): AttrList.from_pointer(ffi.NULL)
def test_attributes(): width = 150 height = 50 # CFFIs and Contexts context_creator = ContextCreator.create_pdf( 'tests/acceptance/output/attributes.pdf', width, height) pangocairo = context_creator.pangocairo cairo_context = context_creator.cairo_context text = 'Hi from Παν語! This test is to make sure that text is correctly ' \ 'annotated, and to also highlight functionality that is not ' \ 'available from methods like pango.set_markup(\'...\'). ' \ 'For instance, Attribute.from_shape() allows you to embed a ' \ 'picture or a widget in a layout. Example: Ø.' text_utf8 = text.encode('utf-8') # Using pangocairocffi, this would be `pangocairocffi.create_layout()` with # a cairocffi context object. layout = Layout.from_pointer( pangocairo.pango_cairo_create_layout(cairo_context)) layout.set_width(pangocffi.units_from_double(width * 72 / 25.4)) layout.set_text(text) attr_list = AttrList() # Example: Handling byte indexes correctly. # When handling byte indexes, make sure to always use UTF-8 byte strings. # For example , while the string '語' has a length of 1 character in # python, in a UTF-8 byte string this is 3 bytes: `0xe8 0xaa 0x9e`. Pango # will not like byte indexes that occur in the middle of a character. text_to_match = 'Παν語!'.encode('utf-8') match_index = text_utf8.index(text_to_match) match_index_end = match_index + len(text_to_match) attr_list.insert( Attribute.from_style(Style.ITALIC, match_index, match_index_end)) # Example: Color alpha # `set_markup()` doesn't appear to have the ability to control color # alphas, but it is supported by Attributes! Note that the alpha color for # underline and foreground text cannot be controlled separately. text_to_match = 'correctly'.encode('utf-8') match_index = text_utf8.index(text_to_match) match_index_end = match_index + len(text_to_match) attr_list.insert( Attribute.from_underline(Underline.ERROR, match_index, match_index_end)) attr_list.insert( Attribute.from_foreground_color(65535, 0, 0, match_index, match_index_end)) attr_list.insert( Attribute.from_underline_color(0, 0, 65535, match_index, match_index_end)) attr_list.insert( Attribute.from_background_color(0, 65535, 0, match_index, match_index_end)) attr_list.insert( Attribute.from_foreground_alpha(32768, match_index, match_index_end)) attr_list.insert( Attribute.from_background_alpha(8192, match_index, match_index_end)) # Example: Highlight multiple Monospace texts markup_texts = ('pango.set_markup(\'...\')', 'Attribute.from_shape()') for markup_text in markup_texts: text_to_match = markup_text.encode('utf-8') match_index = text_utf8.index(text_to_match) match_index_end = match_index + len(text_to_match) attr_list.insert( Attribute.from_family('monospace', match_index, match_index_end)) # Example: Shape placeholders # `Attribute.from_shape()` can be used to insert a box within the layout. # In this case, we're inserting a 1cm square that sits on the baseline of # the final line. Note the negative y coordinate of the rectangle. # Visually, the background color will appear a bit larger, that's because # it will also apply a background below the baseline of the font. # Once rendered, it should be possible to retrieve the position of the # shape using using LayoutIter so that an actual image can be rendered in # place should you need to. text_to_match = 'Ø'.encode('utf-8') match_index = text_utf8.index(text_to_match) match_index_end = match_index + len(text_to_match) glyph_length = pangocffi.units_from_double(10 * 72 / 25.4) # 10 mm ink = Rectangle(width=glyph_length, height=glyph_length, x=0, y=-glyph_length) logical = Rectangle(width=glyph_length, height=glyph_length, x=0, y=-glyph_length) attr_list.insert( Attribute.from_background_color(65535, 0, 0, match_index, match_index_end)) attr_list.insert( Attribute.from_background_alpha(8192, match_index, match_index_end)) attr_list.insert( Attribute.from_shape(ink, logical, match_index, match_index_end)) # Apply all the attributes to the layout layout.set_attributes(attr_list) # Using pangocairocffi, this would be `pangocairocffi.show_layout(layout)` pangocairo.pango_cairo_show_layout(cairo_context, layout.get_pointer()) context_creator.close()