|
| 1 | +import pangocffi |
| 2 | +from pangocffi import Layout, AttrList, Attribute, Underline, Rectangle, Style |
| 3 | +from tests.context_creator import ContextCreator |
| 4 | + |
| 5 | + |
| 6 | +def test_attributes(): |
| 7 | + width = 150 |
| 8 | + height = 50 |
| 9 | + # CFFIs and Contexts |
| 10 | + context_creator = ContextCreator.create_pdf( |
| 11 | + 'tests/acceptance/output/attributes.pdf', width, height |
| 12 | + ) |
| 13 | + pangocairo = context_creator.pangocairo |
| 14 | + cairo_context = context_creator.cairo_context |
| 15 | + |
| 16 | + text = 'Hi from Παν語! This test is to make sure that text is correctly ' \ |
| 17 | + 'annotated, and to also highlight functionality that is not ' \ |
| 18 | + 'available from methods like pango.set_markup(\'...\'). ' \ |
| 19 | + 'For instance, Attribute.from_shape() allows you to embed a ' \ |
| 20 | + 'picture or a widget in a layout. Example: Ø.' |
| 21 | + text_utf8 = text.encode('utf-8') |
| 22 | + |
| 23 | + # Using pangocairocffi, this would be `pangocairocffi.create_layout()` with |
| 24 | + # a cairocffi context object. |
| 25 | + layout = Layout.from_pointer( |
| 26 | + pangocairo.pango_cairo_create_layout(cairo_context) |
| 27 | + ) |
| 28 | + |
| 29 | + layout.set_width(pangocffi.units_from_double(width * 72 / 25.4)) |
| 30 | + layout.set_text(text) |
| 31 | + |
| 32 | + attr_list = AttrList() |
| 33 | + |
| 34 | + # Example: Handling byte indexes correctly. |
| 35 | + # When handling byte indexes, make sure to always use UTF-8 byte strings. |
| 36 | + # For example , while the string '語' has a length of 1 character in |
| 37 | + # python, in a UTF-8 byte string this is 3 bytes: `0xe8 0xaa 0x9e`. Pango |
| 38 | + # will not like byte indexes that occur in the middle of a character. |
| 39 | + text_to_match = 'Παν語!'.encode('utf-8') |
| 40 | + match_index = text_utf8.index(text_to_match) |
| 41 | + match_index_end = match_index + len(text_to_match) |
| 42 | + attr_list.insert(Attribute.from_style( |
| 43 | + Style.ITALIC, |
| 44 | + match_index, |
| 45 | + match_index_end |
| 46 | + )) |
| 47 | + |
| 48 | + # Example: Color alpha |
| 49 | + # `set_markup()` doesn't appear to have the ability to control color |
| 50 | + # alphas, but it is supported by Attributes! Note that the alpha color for |
| 51 | + # underline and foreground text cannot be controlled separately. |
| 52 | + text_to_match = 'correctly'.encode('utf-8') |
| 53 | + match_index = text_utf8.index(text_to_match) |
| 54 | + match_index_end = match_index + len(text_to_match) |
| 55 | + attr_list.insert(Attribute.from_underline( |
| 56 | + Underline.ERROR, match_index, match_index_end |
| 57 | + )) |
| 58 | + attr_list.insert(Attribute.from_foreground_color( |
| 59 | + 65535, 0, 0, match_index, match_index_end |
| 60 | + )) |
| 61 | + attr_list.insert(Attribute.from_underline_color( |
| 62 | + 0, 0, 65535, match_index, match_index_end |
| 63 | + )) |
| 64 | + attr_list.insert(Attribute.from_background_color( |
| 65 | + 0, 65535, 0, match_index, match_index_end |
| 66 | + )) |
| 67 | + attr_list.insert(Attribute.from_foreground_alpha( |
| 68 | + 32768, match_index, match_index_end |
| 69 | + )) |
| 70 | + attr_list.insert(Attribute.from_background_alpha( |
| 71 | + 8192, match_index, match_index_end |
| 72 | + )) |
| 73 | + |
| 74 | + # Example: Highlight multiple Monospace texts |
| 75 | + markup_texts = ('pango.set_markup(\'...\')', 'Attribute.from_shape()') |
| 76 | + for markup_text in markup_texts: |
| 77 | + text_to_match = markup_text.encode('utf-8') |
| 78 | + match_index = text_utf8.index(text_to_match) |
| 79 | + match_index_end = match_index + len(text_to_match) |
| 80 | + attr_list.insert(Attribute.from_family( |
| 81 | + 'monospace', |
| 82 | + match_index, |
| 83 | + match_index_end |
| 84 | + )) |
| 85 | + |
| 86 | + # Example: Shape placeholders |
| 87 | + # `Attribute.from_shape()` can be used to insert a box within the layout. |
| 88 | + # In this case, we're inserting a 1cm square that sits on the baseline of |
| 89 | + # the final line. Note the negative y coordinate of the rectangle. |
| 90 | + # Visually, the background color will appear a bit larger, that's because |
| 91 | + # it will also apply a background below the baseline of the font. |
| 92 | + # Once rendered, it should be possible to retrieve the position of the |
| 93 | + # shape using using LayoutIter so that an actual image can be rendered in |
| 94 | + # place should you need to. |
| 95 | + text_to_match = 'Ø'.encode('utf-8') |
| 96 | + match_index = text_utf8.index(text_to_match) |
| 97 | + match_index_end = match_index + len(text_to_match) |
| 98 | + glyph_length = pangocffi.units_from_double(10 * 72 / 25.4) # 10 mm |
| 99 | + ink = Rectangle( |
| 100 | + width=glyph_length, height=glyph_length, x=0, y=-glyph_length |
| 101 | + ) |
| 102 | + logical = Rectangle( |
| 103 | + width=glyph_length, height=glyph_length, x=0, y=-glyph_length |
| 104 | + ) |
| 105 | + attr_list.insert(Attribute.from_background_color( |
| 106 | + 65535, 0, 0, match_index, match_index_end |
| 107 | + )) |
| 108 | + attr_list.insert(Attribute.from_background_alpha( |
| 109 | + 8192, match_index, match_index_end |
| 110 | + )) |
| 111 | + attr_list.insert(Attribute.from_shape( |
| 112 | + ink, |
| 113 | + logical, |
| 114 | + match_index, |
| 115 | + match_index_end |
| 116 | + )) |
| 117 | + |
| 118 | + # Apply all the attributes to the layout |
| 119 | + layout.set_attributes(attr_list) |
| 120 | + |
| 121 | + # Using pangocairocffi, this would be `pangocairocffi.show_layout(layout)` |
| 122 | + pangocairo.pango_cairo_show_layout(cairo_context, layout.get_pointer()) |
| 123 | + |
| 124 | + context_creator.close() |
0 commit comments