summaryrefslogtreecommitdiff
path: root/Userland/Applications/PDFViewer/PDFViewer.cpp
blob: d2e018cb50a147c184a4ec85de34dc324be1375a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
/*
 * Copyright (c) 2021, Matthew Olsson <mattco@serenityos.org>
 *
 * SPDX-License-Identifier: BSD-2-Clause
 */

#include "PDFViewer.h"
#include <LibGUI/Action.h>
#include <LibGUI/Painter.h>
#include <LibPDF/Renderer.h>

PDFViewer::PDFViewer()
{
    set_should_hide_unnecessary_scrollbars(true);
    set_focus_policy(GUI::FocusPolicy::StrongFocus);
    set_scrollbars_enabled(true);

    start_timer(30'000);
}

void PDFViewer::set_document(RefPtr<PDF::Document> document)
{
    m_document = document;
    m_current_page_index = document->get_first_page_index();
    m_zoom_level = initial_zoom_level;
    m_rendered_page_list.clear();

    m_rendered_page_list.ensure_capacity(document->get_page_count());
    for (u32 i = 0; i < document->get_page_count(); i++)
        m_rendered_page_list.unchecked_append(HashMap<u32, RefPtr<Gfx::Bitmap>>());

    update();
}

RefPtr<Gfx::Bitmap> PDFViewer::get_rendered_page(u32 index)
{
    auto& rendered_page_map = m_rendered_page_list[index];
    auto existing_rendered_page = rendered_page_map.get(m_zoom_level);
    if (existing_rendered_page.has_value())
        return existing_rendered_page.value();

    auto rendered_page = render_page(m_document->get_page(index));
    rendered_page_map.set(m_zoom_level, rendered_page);
    return rendered_page;
}

void PDFViewer::paint_event(GUI::PaintEvent& event)
{
    GUI::Frame::paint_event(event);

    GUI::Painter painter(*this);
    painter.add_clip_rect(widget_inner_rect());
    painter.add_clip_rect(event.rect());
    painter.fill_rect(event.rect(), Color(0x80, 0x80, 0x80));

    if (!m_document)
        return;

    auto page = get_rendered_page(m_current_page_index);
    set_content_size(page->size());

    painter.translate(frame_thickness(), frame_thickness());
    painter.translate(-horizontal_scrollbar().value(), -vertical_scrollbar().value());

    int x = max(0, (width() - page->width()) / 2);
    int y = max(0, (height() - page->height()) / 2);

    painter.blit({ x, y }, *page, page->rect());
}

void PDFViewer::mousewheel_event(GUI::MouseEvent& event)
{
    bool scrolled_down = event.wheel_delta() > 0;

    if (event.ctrl()) {
        if (scrolled_down) {
            zoom_out();
        } else {
            zoom_in();
        }
    } else {
        auto& scrollbar = event.shift() ? horizontal_scrollbar() : vertical_scrollbar();

        if (scrolled_down) {
            if (scrollbar.value() == scrollbar.max()) {
                if (m_current_page_index < m_document->get_page_count() - 1) {
                    m_current_page_index++;
                    scrollbar.set_value(0);
                }
            } else {
                scrollbar.set_value(scrollbar.value() + 20);
            }
        } else {
            if (scrollbar.value() == 0) {
                if (m_current_page_index > 0) {
                    m_current_page_index--;
                    scrollbar.set_value(scrollbar.max());
                }
            } else {
                scrollbar.set_value(scrollbar.value() - 20);
            }
        }
    }

    update();
}

void PDFViewer::timer_event(Core::TimerEvent&)
{
    // Clear the bitmap vector of all pages except the current page
    for (size_t i = 0; i < m_rendered_page_list.size(); i++) {
        if (i != m_current_page_index)
            m_rendered_page_list[i].clear();
    }
}

void PDFViewer::zoom_in()
{
    if (m_zoom_level < number_of_zoom_levels - 1)
        m_zoom_level++;
}

void PDFViewer::zoom_out()
{
    if (m_zoom_level > 0)
        m_zoom_level--;
}

RefPtr<Gfx::Bitmap> PDFViewer::render_page(const PDF::Page& page)
{
    auto zoom_scale_factor = static_cast<float>(zoom_levels[m_zoom_level]) / 100.0f;

    float page_width = page.media_box.upper_right_x - page.media_box.lower_left_x;
    float page_height = page.media_box.upper_right_y - page.media_box.lower_left_y;
    float page_scale_factor = page_height / page_width;

    float width = 300.0f * zoom_scale_factor;
    float height = width * page_scale_factor;
    auto bitmap = Gfx::Bitmap::create(Gfx::BitmapFormat::BGRA8888, { width, height });

    PDF::Renderer::render(*m_document, page, bitmap);

    if (page.rotate != 0) {
        int rotation_count = (page.rotate / 90) % 4;
        if (rotation_count == 3) {
            bitmap = bitmap->rotated(Gfx::RotationDirection::CounterClockwise);
        } else {
            for (int i = 0; i < rotation_count; i++)
                bitmap = bitmap->rotated(Gfx::RotationDirection::Clockwise);
        }
    }

    return bitmap;
}