コード例 #1
0
    def test_insert_text(self, _init_pygame, default_ui_manager: UIManager):
        the_font = pygame.freetype.Font(None, 20)
        input_data = deque([
            TextLineChunkFTFont(text='hello ',
                                font=the_font,
                                underlined=False,
                                colour=pygame.Color('#FFFFFF'),
                                using_default_text_colour=False,
                                bg_colour=pygame.Color('#FF0000')),
            TextLineChunkFTFont(text='this is a',
                                font=the_font,
                                underlined=False,
                                colour=pygame.Color('#FFFFFF'),
                                using_default_text_colour=True,
                                bg_colour=pygame.Color('#FF0000')),
            TextLineChunkFTFont(text=' test',
                                font=the_font,
                                underlined=False,
                                colour=pygame.Color('#FFFFFF'),
                                using_default_text_colour=False,
                                bg_colour=pygame.Color('#FF0000')),
        ])

        layout = TextBoxLayout(input_data_queue=input_data,
                               layout_rect=pygame.Rect(0, 0, 500, 300),
                               view_rect=pygame.Rect(0, 0, 500, 150),
                               line_spacing=1.0)

        layout.insert_text('nother insertion', 15)

        row = layout.layout_rows[0]
        chunk = row.items[1]

        assert chunk.text == 'this is another insertion'
コード例 #2
0
    def test_split_index(self, _init_pygame, default_ui_manager: UIManager):

        the_font = pygame.freetype.Font(None, 30)
        the_font.origin = True
        the_font.pad = True

        chunk = TextLineChunkFTFont(text='test this',
                                    font=the_font,
                                    underlined=False,
                                    colour=pygame.Color('#FFFFFF'),
                                    using_default_text_colour=False,
                                    bg_colour=pygame.Color('#00000000'))

        with pytest.raises(ValueError, match='Line width is too narrow'):
            chunk.split(requested_x=0, line_width=0, row_start_x=0)

        assert len(chunk.split_points) == 1
        assert chunk.split_points[0] == 5
        original_chunk_width = chunk.width

        new_chunk = chunk.split_index(index=5)

        assert chunk.width == 62
        assert new_chunk.width == 53
        assert original_chunk_width == (chunk.width + new_chunk.width)
        assert chunk.height == new_chunk.height
コード例 #3
0
    def test_set_cursor_from_click_pos(self, _init_pygame,
                                       default_ui_manager: UIManager):
        the_font = pygame.freetype.Font(None, 20)
        input_data = deque([
            TextLineChunkFTFont(text='hello ',
                                font=the_font,
                                underlined=False,
                                colour=pygame.Color('#FFFFFF'),
                                using_default_text_colour=False,
                                bg_colour=pygame.Color('#FF0000')),
            TextLineChunkFTFont(text='this is a',
                                font=the_font,
                                underlined=False,
                                colour=pygame.Color('#FFFFFF'),
                                using_default_text_colour=False,
                                bg_colour=pygame.Color('#FF0000')),
            TextLineChunkFTFont(text=' test',
                                font=the_font,
                                underlined=False,
                                colour=pygame.Color('#FFFFFF'),
                                using_default_text_colour=False,
                                bg_colour=pygame.Color('#FF0000')),
        ])

        layout = TextBoxLayout(input_data_queue=input_data,
                               layout_rect=pygame.Rect(0, 0, 100, 300),
                               view_rect=pygame.Rect(0, 0, 100, 150),
                               line_spacing=1.0)

        layout.set_cursor_from_click_pos((17, 24))
        assert layout.cursor_text_row is not None
        assert layout.cursor_text_row.cursor_index == 2
        assert layout.cursor_text_row.cursor_draw_width == 17
コード例 #4
0
    def test_vert_align_bottom_all_rows(self, _init_pygame,
                                        default_ui_manager: UIManager):
        the_font = pygame.freetype.Font(None, 20)
        input_data = deque([
            TextLineChunkFTFont(text='hello ',
                                font=the_font,
                                underlined=False,
                                colour=pygame.Color('#FFFFFF'),
                                using_default_text_colour=False,
                                bg_colour=pygame.Color('#FF0000')),
            TextLineChunkFTFont(text='this is a',
                                font=the_font,
                                underlined=False,
                                colour=pygame.Color('#FFFFFF'),
                                using_default_text_colour=False,
                                bg_colour=pygame.Color('#FF0000')),
            TextLineChunkFTFont(text=' test',
                                font=the_font,
                                underlined=False,
                                colour=pygame.Color('#FFFFFF'),
                                using_default_text_colour=False,
                                bg_colour=pygame.Color('#FF0000')),
        ])

        layout = TextBoxLayout(input_data_queue=input_data,
                               layout_rect=pygame.Rect(0, 0, 500, 300),
                               view_rect=pygame.Rect(0, 0, 500, 150),
                               line_spacing=1.0)

        row = layout.layout_rows[0]
        assert row.y == 0
        layout.vert_align_bottom_all_rows(y_padding=8)
        assert row.bottom == 292
コード例 #5
0
    def test_delete_selected_text(self, _init_pygame,
                                  default_ui_manager: UIManager):
        the_font = pygame.freetype.Font(None, 20)
        input_data = deque([
            TextLineChunkFTFont(text='hello ',
                                font=the_font,
                                underlined=False,
                                colour=pygame.Color('#FFFFFF'),
                                using_default_text_colour=False,
                                bg_colour=pygame.Color('#FF0000')),
            TextLineChunkFTFont(text='this is a',
                                font=the_font,
                                underlined=False,
                                colour=pygame.Color('#FFFFFF'),
                                using_default_text_colour=True,
                                bg_colour=pygame.Color('#FF0000')),
            TextLineChunkFTFont(text=' test',
                                font=the_font,
                                underlined=False,
                                colour=pygame.Color('#FFFFFF'),
                                using_default_text_colour=False,
                                bg_colour=pygame.Color('#FF0000')),
        ])

        layout = TextBoxLayout(input_data_queue=input_data,
                               layout_rect=pygame.Rect(0, 0, 100, 300),
                               view_rect=pygame.Rect(0, 0, 100, 150),
                               line_spacing=1.0)

        layout.set_text_selection(5, 17)

        assert len(layout.selected_rows) == 2
        assert len(layout.selected_chunks) == 4

        selected_text = ""
        for chunk in layout.selected_chunks:
            selected_text += chunk.text

        assert selected_text == ' this is a t'

        layout.delete_selected_text()

        assert len(layout.selected_rows) == 0
        assert len(layout.selected_chunks) == 0

        selected_text = ""
        for chunk in layout.selected_chunks:
            selected_text += chunk.text

        assert selected_text == ''

        remaining_text = ''
        for row in layout.layout_rows:
            for chunk in row.items:
                remaining_text += chunk.text

        assert remaining_text == 'helloest'
コード例 #6
0
    def test_vert_align_items_to_row(self, _init_pygame,
                                     default_ui_manager: UIManager):
        input_data = deque([])
        line_spacing = 1.25
        text_box_layout = TextBoxLayout(input_data_queue=input_data,
                                        layout_rect=pygame.Rect(
                                            0, 0, 200, 300),
                                        view_rect=pygame.Rect(0, 0, 200, 150),
                                        line_spacing=line_spacing)
        layout_row = TextBoxLayoutRow(row_start_x=0,
                                      row_start_y=0,
                                      row_index=0,
                                      line_spacing=line_spacing,
                                      layout=text_box_layout)

        font_1 = pygame.freetype.Font(None, 30)
        font_2 = pygame.freetype.Font(None, 20)
        font_3 = pygame.freetype.Font(None, 10)
        text_chunk_1 = TextLineChunkFTFont(text='hello ',
                                           font=font_1,
                                           underlined=False,
                                           colour=pygame.Color('#FFFFFF'),
                                           using_default_text_colour=False,
                                           bg_colour=pygame.Color('#FF0000'))
        text_chunk_2 = TextLineChunkFTFont(text='this is a',
                                           font=font_2,
                                           underlined=False,
                                           colour=pygame.Color('#FFFFFF'),
                                           using_default_text_colour=False,
                                           bg_colour=pygame.Color('#FF0000'))
        text_chunk_3 = TextLineChunkFTFont(text=' test',
                                           font=font_3,
                                           underlined=False,
                                           colour=pygame.Color('#FFFFFF'),
                                           using_default_text_colour=False,
                                           bg_colour=pygame.Color('#FF0000'))
        layout_row.add_item(text_chunk_1)
        layout_row.add_item(text_chunk_2)
        layout_row.add_item(text_chunk_3)

        # not sure this is right, need to do some more visual testing of vertical
        # alignment of text rects with different height text on a single row.
        assert layout_row.items[0].y == 0
        assert layout_row.items[1].y == 7
        assert layout_row.items[2].y == 15

        layout_row.vert_align_items_to_row()

        assert layout_row.items[0].y == 0
        assert layout_row.items[1].y == 7
        assert layout_row.items[2].y == 15
コード例 #7
0
    def test_redraw(self, _init_pygame, default_ui_manager: UIManager):

        the_font = pygame.freetype.Font(None, 30)
        the_font.origin = True
        the_font.pad = True

        chunk = TextLineChunkFTFont(text='test this',
                                    font=the_font,
                                    underlined=False,
                                    colour=pygame.Color('#FFFFFF'),
                                    using_default_text_colour=False,
                                    bg_colour=pygame.Color('#FF00FF'))

        layout_surface = pygame.Surface((200, 300),
                                        depth=32,
                                        flags=pygame.SRCALPHA)
        layout_surface.fill((0, 0, 0, 0))
        chunk.finalise(layout_surface, pygame.Rect(0, 0, 200, 300),
                       chunk.y_origin, chunk.height, chunk.height)

        assert layout_surface.get_at((10, 10)) == pygame.Color('#FF00FF')

        chunk.clear()

        assert layout_surface.get_at((10, 10)) == pygame.Color('#00000000')

        chunk.redraw()

        assert layout_surface.get_at((10, 10)) == pygame.Color('#FF00FF')
コード例 #8
0
    def test_insert_text(self):
        input_data = deque([])
        line_spacing = 1.25
        text_box_layout = TextBoxLayout(input_data_queue=input_data,
                                        layout_rect=pygame.Rect(
                                            0, 0, 200, 300),
                                        view_rect=pygame.Rect(0, 0, 200, 150),
                                        line_spacing=line_spacing)
        layout_row = TextBoxLayoutRow(row_start_x=0,
                                      row_start_y=0,
                                      row_index=0,
                                      line_spacing=line_spacing,
                                      layout=text_box_layout)

        font_1 = pygame.freetype.Font(None, 30)
        font_1.origin = True
        font_1.pad = True
        text_chunk_1 = TextLineChunkFTFont(text='test',
                                           font=font_1,
                                           underlined=False,
                                           colour=pygame.Color('#FFFFFF'),
                                           using_default_text_colour=False,
                                           bg_colour=pygame.Color('#00000000'))

        layout_row.add_item(text_chunk_1)

        layout_row.insert_text(' this', 4)

        assert layout_row.items[0].text == 'test this'
コード例 #9
0
    def test_add_chunks_to_hover_group(self, _init_pygame,
                                       default_ui_manager: UIManager):
        input_data = deque([
            TextLineChunkFTFont(text='hello',
                                font=pygame.freetype.Font(None, 20),
                                underlined=False,
                                colour=pygame.Color('#FFFFFF'),
                                using_default_text_colour=False,
                                bg_colour=pygame.Color('#FF0000')),
            HyperlinkTextChunk(href='test',
                               text='a link',
                               font=pygame.freetype.Font(None, 20),
                               underlined=False,
                               colour=pygame.Color('#FFFFFF'),
                               bg_colour=pygame.Color('#FF0000'),
                               hover_colour=pygame.Color('#0000FF'),
                               active_colour=pygame.Color('#FFFF00'),
                               hover_underline=False)
        ])

        layout = TextBoxLayout(input_data_queue=input_data,
                               layout_rect=pygame.Rect(0, 0, 200, 300),
                               view_rect=pygame.Rect(0, 0, 200, 150),
                               line_spacing=1.0)

        links_found = []
        layout.add_chunks_to_hover_group(links_found)
        assert len(links_found) == 1
コード例 #10
0
    def test_clear(self, _init_pygame, default_ui_manager: UIManager):
        input_data = deque([])
        line_spacing = 1.25
        text_box_layout = TextBoxLayout(input_data_queue=input_data,
                                        layout_rect=pygame.Rect(
                                            0, 0, 200, 300),
                                        view_rect=pygame.Rect(0, 0, 200, 150),
                                        line_spacing=line_spacing)
        layout_row = TextBoxLayoutRow(row_start_x=0,
                                      row_start_y=0,
                                      row_index=0,
                                      line_spacing=line_spacing,
                                      layout=text_box_layout)

        font_1 = pygame.freetype.Font(None, 30)
        font_1.origin = True
        font_1.pad = True
        text_chunk_1 = TextLineChunkFTFont(text='D',
                                           font=font_1,
                                           underlined=False,
                                           colour=pygame.Color('#FFFFFF'),
                                           using_default_text_colour=False,
                                           bg_colour=pygame.Color('#00000000'))

        layout_row.add_item(text_chunk_1)
        layout_surface = pygame.Surface((200, 300),
                                        depth=32,
                                        flags=pygame.SRCALPHA)
        layout_surface.fill((0, 0, 0, 0))
        layout_row.finalise(layout_surface)
        assert layout_surface.get_at((18, 18)) == pygame.Color('#FFFFFF')

        layout_row.clear()
        assert layout_surface.get_at((18, 18)) == pygame.Color('#00000000')
コード例 #11
0
    def test_insert_text(self, _init_pygame, default_ui_manager: UIManager):

        the_font = pygame.freetype.Font(None, 30)
        the_font.origin = True
        the_font.pad = True

        chunk = TextLineChunkFTFont(text='test this',
                                    font=the_font,
                                    underlined=False,
                                    colour=pygame.Color('#FFFFFF'),
                                    using_default_text_colour=False,
                                    bg_colour=pygame.Color('#FF00FF'))

        assert chunk.text == 'test this'

        chunk.insert_text(input_text=' inserting all of', index=4)

        assert chunk.text == 'test inserting all of this'
コード例 #12
0
    def test_add_text(self, _init_pygame, default_ui_manager: UIManager):

        the_font = pygame.freetype.Font(None, 30)
        the_font.origin = True
        the_font.pad = True

        chunk = TextLineChunkFTFont(text='test this',
                                    font=the_font,
                                    underlined=False,
                                    colour=pygame.Color('#FFFFFF'),
                                    using_default_text_colour=False,
                                    bg_colour=pygame.Color('#FF00FF'))

        assert chunk.text == 'test this'

        chunk.add_text(' for great justice')

        assert chunk.text == 'test this for great justice'
コード例 #13
0
    def test_merge_adjacent_compatible_chunks(self, _init_pygame,
                                              default_ui_manager: UIManager):
        input_data = deque([])
        line_spacing = 1.25
        text_box_layout = TextBoxLayout(input_data_queue=input_data,
                                        layout_rect=pygame.Rect(
                                            0, 0, 200, 300),
                                        view_rect=pygame.Rect(0, 0, 200, 150),
                                        line_spacing=line_spacing)
        layout_row = TextBoxLayoutRow(row_start_x=0,
                                      row_start_y=0,
                                      row_index=0,
                                      line_spacing=line_spacing,
                                      layout=text_box_layout)

        font_1 = pygame.freetype.Font(None, 30)
        font_2 = pygame.freetype.Font(None, 20)
        text_chunk_1 = TextLineChunkFTFont(text='hello ',
                                           font=font_1,
                                           underlined=False,
                                           colour=pygame.Color('#FFFFFF'),
                                           using_default_text_colour=False,
                                           bg_colour=pygame.Color('#FF0000'))
        text_chunk_2 = TextLineChunkFTFont(text='this is a',
                                           font=font_1,
                                           underlined=False,
                                           colour=pygame.Color('#FFFFFF'),
                                           using_default_text_colour=False,
                                           bg_colour=pygame.Color('#FF0000'))
        text_chunk_3 = TextLineChunkFTFont(text=' test',
                                           font=font_2,
                                           underlined=False,
                                           colour=pygame.Color('#FFFFFF'),
                                           using_default_text_colour=False,
                                           bg_colour=pygame.Color('#FF0000'))
        layout_row.add_item(text_chunk_1)
        layout_row.add_item(text_chunk_2)
        layout_row.add_item(text_chunk_3)

        assert len(layout_row.items) == 3

        layout_row.merge_adjacent_compatible_chunks()

        assert len(layout_row.items) == 2
コード例 #14
0
    def test_backspace_at_cursor(self, _init_pygame,
                                 default_ui_manager: UIManager):
        the_font = pygame.freetype.Font(None, 20)
        input_data = deque([
            TextLineChunkFTFont(text='hello ',
                                font=the_font,
                                underlined=False,
                                colour=pygame.Color('#FFFFFF'),
                                using_default_text_colour=False,
                                bg_colour=pygame.Color('#FF0000')),
            TextLineChunkFTFont(text='this is a',
                                font=the_font,
                                underlined=False,
                                colour=pygame.Color('#FFFFFF'),
                                using_default_text_colour=True,
                                bg_colour=pygame.Color('#FF0000')),
            TextLineChunkFTFont(text=' test',
                                font=the_font,
                                underlined=False,
                                colour=pygame.Color('#FFFFFF'),
                                using_default_text_colour=False,
                                bg_colour=pygame.Color('#FF0000')),
        ])

        layout = TextBoxLayout(input_data_queue=input_data,
                               layout_rect=pygame.Rect(0, 0, 100, 300),
                               view_rect=pygame.Rect(0, 0, 100, 150),
                               line_spacing=1.0)

        layout.set_cursor_position(16)
        layout.backspace_at_cursor()
        layout.backspace_at_cursor()

        remaining_text = ''
        for row in layout.layout_rows:
            for chunk in row.items:
                remaining_text += chunk.text

        assert remaining_text == 'hello this is test'
コード例 #15
0
    def test_creation(self, _init_pygame, default_ui_manager: UIManager):

        the_font = pygame.freetype.Font(None, 30)
        the_font.origin = True
        the_font.pad = True

        chunk = TextLineChunkFTFont(text='test',
                                    font=the_font,
                                    underlined=False,
                                    colour=pygame.Color('#FFFFFF'),
                                    using_default_text_colour=False,
                                    bg_colour=pygame.Color('#00000000'))

        assert chunk.text == 'test'
コード例 #16
0
    def test_set_cursor_position(self):
        input_data = deque([])
        line_spacing = 1.25
        text_box_layout = TextBoxLayout(input_data_queue=input_data,
                                        layout_rect=pygame.Rect(
                                            0, 0, 200, 300),
                                        view_rect=pygame.Rect(0, 0, 200, 150),
                                        line_spacing=line_spacing)
        layout_row = TextBoxLayoutRow(row_start_x=0,
                                      row_start_y=0,
                                      row_index=0,
                                      line_spacing=line_spacing,
                                      layout=text_box_layout)

        font_1 = pygame.freetype.Font(None, 30)
        font_1.origin = True
        font_1.pad = True
        text_chunk_1 = TextLineChunkFTFont(text='test',
                                           font=font_1,
                                           underlined=False,
                                           colour=pygame.Color('#FFFFFF'),
                                           using_default_text_colour=False,
                                           bg_colour=pygame.Color('#00000000'))

        layout_row.add_item(text_chunk_1)

        layout_surface = pygame.Surface((200, 300),
                                        depth=32,
                                        flags=pygame.SRCALPHA)
        layout_surface.fill((0, 0, 0, 0))
        layout_row.finalise(layout_surface)
        layout_row.toggle_cursor()

        assert layout_row.edit_cursor_active is True
        assert layout_surface.get_at((1, 5)) == pygame.Color('#FFFFFF')
        assert layout_row.cursor_index == 0
        assert layout_row.cursor_draw_width == 0

        layout_row.set_cursor_position(3)

        layout_row.toggle_cursor()
        layout_row.toggle_cursor()

        assert layout_row.edit_cursor_active is True
        assert layout_surface.get_at((1, 5)) == pygame.Color('#00000000')
        assert layout_row.cursor_index == 3
        assert layout_row.cursor_draw_width == 44
コード例 #17
0
    def test_update_text_with_new_text_end_pos(self, _init_pygame,
                                               default_ui_manager: UIManager):
        input_data = deque([
            TextLineChunkFTFont(text='hello',
                                font=pygame.freetype.Font(None, 20),
                                underlined=False,
                                colour=pygame.Color('#FFFFFF'),
                                using_default_text_colour=False,
                                bg_colour=pygame.Color('#FF0000'))
        ])

        layout = TextBoxLayout(input_data_queue=input_data,
                               layout_rect=pygame.Rect(0, 0, 200, 300),
                               view_rect=pygame.Rect(0, 0, 200, 150),
                               line_spacing=1.0)

        layout_surface = layout.finalise_to_new()
        layout.update_text_with_new_text_end_pos(
            0)  # this does nothing unless we pass in text
        assert layout_surface.get_at((10, 10)) == pygame.Color(0, 0, 0, 0)

        layout.update_text_with_new_text_end_pos(3)
        assert layout_surface.get_at((10, 10)) == pygame.Color(255, 0, 0, 255)
コード例 #18
0
    def test_x_pos_to_letter_index(self):
        the_font = pygame.freetype.Font(None, 30)
        the_font.origin = True
        the_font.pad = True

        chunk = TextLineChunkFTFont(text='test this',
                                    font=the_font,
                                    underlined=False,
                                    colour=pygame.Color('#FFFFFF'),
                                    using_default_text_colour=False,
                                    bg_colour=pygame.Color('#FF00FF'))

        letter_index = chunk.x_pos_to_letter_index(x_pos=62)
        assert letter_index == 5

        letter_index = chunk.x_pos_to_letter_index(x_pos=37)
        assert letter_index == 3

        letter_index = chunk.x_pos_to_letter_index(x_pos=0)
        assert letter_index == 0

        letter_index = chunk.x_pos_to_letter_index(x_pos=100)
        assert letter_index == 8
コード例 #19
0
    def test_backspace_letter_at_index(self, _init_pygame,
                                       default_ui_manager: UIManager):

        the_font = pygame.freetype.Font(None, 30)
        the_font.origin = True
        the_font.pad = True

        chunk = TextLineChunkFTFont(text='test this',
                                    font=the_font,
                                    underlined=False,
                                    colour=pygame.Color('#FFFFFF'),
                                    using_default_text_colour=False,
                                    bg_colour=pygame.Color('#FF00FF'))

        assert chunk.text == 'test this'

        chunk.backspace_letter_at_index(index=7)

        assert chunk.text == 'test tis'

        chunk.backspace_letter_at_index(index=7)

        assert chunk.text == 'test ts'

        chunk.backspace_letter_at_index(index=7)

        assert chunk.text == 'test t'

        chunk.backspace_letter_at_index(index=100)

        assert chunk.text == 'test t'

        chunk.backspace_letter_at_index(index=0)

        assert chunk.text == 'test t'

        chunk.backspace_letter_at_index(index=0)

        assert chunk.text == 'test t'

        chunk.backspace_letter_at_index(index=0)
        chunk.backspace_letter_at_index(index=0)
        chunk.backspace_letter_at_index(index=0)
        chunk.backspace_letter_at_index(index=0)

        assert chunk.text == 'test t'

        chunk.backspace_letter_at_index(index=6)
        chunk.backspace_letter_at_index(index=5)
        chunk.backspace_letter_at_index(index=4)
        chunk.backspace_letter_at_index(index=3)
        chunk.backspace_letter_at_index(index=2)
        chunk.backspace_letter_at_index(index=1)

        text_rect = the_font.get_rect('A')
        text_width = sum(
            [char_metric[4] for char_metric in the_font.get_metrics('')])
        text_height = text_rect.height

        assert chunk.text == ''
        assert chunk.size == (text_width, text_height)
コード例 #20
0
    def test_style_match(self, _init_pygame, default_ui_manager: UIManager):

        the_font = pygame.freetype.Font(None, 30)
        the_font.origin = True
        the_font.pad = True

        the_second_font = pygame.freetype.Font(None, 14)
        the_second_font.origin = True
        the_second_font.pad = True

        chunk_1 = TextLineChunkFTFont(text='test',
                                      font=the_font,
                                      underlined=False,
                                      colour=pygame.Color('#FFFFFF'),
                                      using_default_text_colour=False,
                                      bg_colour=pygame.Color('#00000000'))

        # only text differs so the styles should match
        chunk_2 = TextLineChunkFTFont(text='a match',
                                      font=the_font,
                                      underlined=False,
                                      colour=pygame.Color('#FFFFFF'),
                                      using_default_text_colour=False,
                                      bg_colour=pygame.Color('#00000000'))

        assert chunk_1.style_match(chunk_2)

        # no underline so should be no style match
        chunk_3 = TextLineChunkFTFont(text='not a match',
                                      font=the_font,
                                      underlined=True,
                                      colour=pygame.Color('#FFFFFF'),
                                      using_default_text_colour=False,
                                      bg_colour=pygame.Color('#00000000'))

        assert not chunk_1.style_match(chunk_3)

        # different font so should be no style match
        chunk_4 = TextLineChunkFTFont(text='not a match',
                                      font=the_second_font,
                                      underlined=False,
                                      colour=pygame.Color('#FFFFFF'),
                                      using_default_text_colour=False,
                                      bg_colour=pygame.Color('#00000000'))

        assert not chunk_1.style_match(chunk_4)

        # different font colour so should be no style match
        chunk_5 = TextLineChunkFTFont(text='not a match',
                                      font=the_font,
                                      underlined=False,
                                      colour=pygame.Color('#FF8080'),
                                      using_default_text_colour=False,
                                      bg_colour=pygame.Color('#00000000'))

        assert not chunk_1.style_match(chunk_5)

        # different background colour so should be no style match
        chunk_6 = TextLineChunkFTFont(text='not a match',
                                      font=the_font,
                                      underlined=False,
                                      colour=pygame.Color('#FFFFFF'),
                                      using_default_text_colour=False,
                                      bg_colour=pygame.Color('#5050F0'))

        assert not chunk_1.style_match(chunk_6)
コード例 #21
0
    def test_finalise(self, _init_pygame, default_ui_manager: UIManager):
        the_font = pygame.freetype.Font(None, 30)
        the_font.origin = True
        the_font.pad = True

        chunk_1 = TextLineChunkFTFont(text='test',
                                      font=the_font,
                                      underlined=False,
                                      colour=pygame.Color('#FFFFFF'),
                                      using_default_text_colour=False,
                                      bg_colour=pygame.Color('#FF0000'))

        layout_surface = pygame.Surface((200, 300),
                                        depth=32,
                                        flags=pygame.SRCALPHA)
        layout_surface.fill((0, 0, 0, 0))
        chunk_1.finalise(layout_surface, pygame.Rect(0, 0, 200, 300),
                         chunk_1.y_origin, chunk_1.height, chunk_1.height)

        assert layout_surface.get_at((10, 10)) == pygame.Color('#FF0000')

        chunk_2 = TextLineChunkFTFont(text='test',
                                      font=the_font,
                                      underlined=False,
                                      colour=ColourGradient(
                                          0, pygame.Color('#FFFF00'),
                                          pygame.Color('#FF0000')),
                                      using_default_text_colour=False,
                                      bg_colour=pygame.Color('#FF0000'))

        layout_surface_2 = pygame.Surface((200, 300),
                                          depth=32,
                                          flags=pygame.SRCALPHA)
        layout_surface_2.fill((0, 0, 0, 0))
        chunk_2.finalise(layout_surface_2, pygame.Rect(0, 0, 200, 300),
                         chunk_2.y_origin, chunk_2.height, chunk_2.height)

        assert layout_surface_2.get_at((10, 10)) == pygame.Color('#FF0000')

        chunk_3 = TextLineChunkFTFont(
            text='test',
            font=the_font,
            underlined=False,
            colour=ColourGradient(0, pygame.Color('#FFFF00'),
                                  pygame.Color('#FF0000')),
            using_default_text_colour=False,
            bg_colour=ColourGradient(0, pygame.Color('#FFFF00'),
                                     pygame.Color('#FF0000')))

        layout_surface_3 = pygame.Surface((200, 300),
                                          depth=32,
                                          flags=pygame.SRCALPHA)
        layout_surface_3.fill((0, 0, 0, 0))
        chunk_3.finalise(layout_surface_3, pygame.Rect(0, 0, 200, 300),
                         chunk_3.y_origin, chunk_3.height, chunk_3.height)

        assert layout_surface_3.get_at((10, 10)) != pygame.Color('#00000000')

        chunk_4 = TextLineChunkFTFont(text='test',
                                      font=the_font,
                                      underlined=False,
                                      colour=pygame.Color('#FFFFFF'),
                                      using_default_text_colour=False,
                                      bg_colour=ColourGradient(
                                          0, pygame.Color('#FFFF00'),
                                          pygame.Color('#FF0000')))

        layout_surface_4 = pygame.Surface((200, 300),
                                          depth=32,
                                          flags=pygame.SRCALPHA)
        layout_surface_4.fill((0, 0, 0, 0))
        chunk_4.finalise(layout_surface_4, pygame.Rect(0, 0, 200, 300),
                         chunk_4.y_origin, chunk_4.height, chunk_4.height)

        assert layout_surface_4.get_at((10, 10)) != pygame.Color('#00000000')
コード例 #22
0
    def build_text_layout(self):
        """
        Build a text box layout for this drawable shape if it has some text.
        """
        containing_rect_when_text_built = self.containing_rect.copy()
        # Draw any text
        if 'text' in self.theming and 'font' in self.theming and self.theming[
                'text'] is not None:
            # we need two rectangles for the text. One is has actual area the
            # text surface takes up, which may be larger than the displayed area,
            # and its position on the final surface. The other is the amount of
            # area of the text surface which we blit from, which may be much smaller
            # than the total text area.

            horiz_padding = 0
            if 'text_horiz_alignment_padding' in self.theming:
                horiz_padding = self.theming['text_horiz_alignment_padding']

            vert_padding = 0
            if 'text_vert_alignment_padding' in self.theming:
                vert_padding = self.theming['text_vert_alignment_padding']

            total_text_buffer = self.shadow_width + self.border_width + self.rounded_corner_offset
            self.text_view_rect = self.containing_rect.copy()
            self.text_view_rect.x = 0
            self.text_view_rect.y = 0
            if self.text_view_rect.width != -1:
                self.text_view_rect.width -= ((total_text_buffer * 2) +
                                              (2 * horiz_padding))
            if self.text_view_rect.height != -1:
                self.text_view_rect.height -= ((total_text_buffer * 2) +
                                               (2 * vert_padding))

            text_actual_area_rect = self.text_view_rect.copy()
            text_actual_area_rect.x = total_text_buffer + horiz_padding
            text_actual_area_rect.y = total_text_buffer + vert_padding
            if 'text_width' in self.theming:
                text_actual_area_rect.width = self.theming['text_width']
            if 'text_height' in self.theming:
                text_actual_area_rect.height = self.theming['text_height']

            text_shadow_data = (0, 0, 0, pygame.Color('#10101070'), False)
            if 'text_shadow' in self.theming:
                text_shadow_data = self.theming['text_shadow']
            text_chunk = TextLineChunkFTFont(
                self.theming['text'],
                self.theming['font'],
                underlined=False,
                colour=pygame.Color('#FFFFFFFF'),
                using_default_text_colour=True,
                bg_colour=pygame.Color('#00000000'),
                text_shadow_data=text_shadow_data,
                max_dimensions=(text_actual_area_rect.width,
                                text_actual_area_rect.height))
            text_chunk.should_centre_from_baseline = True
            self.text_box_layout = TextBoxLayout(deque([text_chunk]),
                                                 text_actual_area_rect,
                                                 self.text_view_rect,
                                                 line_spacing=1.25)
            if 'text_cursor_colour' in self.theming:
                self.text_box_layout.set_cursor_colour(
                    self.theming['text_cursor_colour'])
            self.align_all_text_rows()
        return containing_rect_when_text_built