Skip to content

Commit

Permalink
Refactored
Browse files Browse the repository at this point in the history
  • Loading branch information
DavidLazarescu committed Oct 19, 2023
1 parent 45d4ec0 commit 407f949
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 30 deletions.
5 changes: 1 addition & 4 deletions src/application/core/page_generator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -105,10 +105,7 @@ mupdf::FzPixmap PageGenerator::renderPage(float zoom)
for(auto& link : m_links)
{
auto newLinkRect =
mupdf::fz_make_rect(link.rect().x0 - m_pageXOffset,
link.rect().y0 - m_pageYOffset,
link.rect().x1 - m_pageXOffset,
link.rect().y1 - m_pageYOffset);
utils::moveRect(link.rect(), m_pageXOffset, m_pageYOffset);
link.fz_set_link_rect(newLinkRect);
}
}
Expand Down
20 changes: 20 additions & 0 deletions src/application/core/utils/fz_utils.hpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
#pragma once
#include <mupdf/classes.h>
#include <mupdf/classes2.h>
#include <QImage>
#include <QPointF>
#include <QRectF>
#include "mupdf/fitz/geometry.h"

namespace application::core::utils
{
Expand Down Expand Up @@ -143,4 +145,22 @@ inline void restoreQRect(QRectF& rect, float zoom)
rect = fzRectToQRectF(restoredFzRect);
}

inline fz_quad moveQuad(const fz_quad& quad, int x, int y)
{
return fz_make_quad(quad.ul.x - x, quad.ul.y - y, quad.ur.x - x,
quad.ur.y - y, quad.ll.x - x, quad.ll.y - y,
quad.lr.x - x, quad.lr.y - y);
}

inline mupdf::FzRect moveRect(const mupdf::FzRect& rect, int x, int y)
{
return mupdf::fz_make_rect(rect.x0 - x, rect.y0 - y, rect.x1 - x,
rect.y1 - y);
}

inline mupdf::FzPoint movePoint(const mupdf::FzPoint& point, int x, int y)
{
return fz_make_point(point.x + x, point.y + y);
}

} // namespace application::core::utils
41 changes: 15 additions & 26 deletions src/application/core/utils/text_selector.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "text_selector.hpp"
#include <mupdf/functions.h>
#include <QDebug>
#include "fz_utils.hpp"
#include "mupdf/fitz/geometry.h"

namespace application::core::utils
Expand All @@ -15,9 +16,8 @@ void TextSelector::generateSelectionRects(QList<mupdf::FzQuad>& container,
mupdf::FzPoint start,
mupdf::FzPoint end)
{
start = mupdf::ll_fz_make_point(start.x + m_pageXOffset,
start.y + m_pageYOffset);
end = mupdf::ll_fz_make_point(end.x + m_pageXOffset, end.y + m_pageYOffset);
start = utils::movePoint(start, m_pageXOffset, m_pageYOffset);
end = utils::movePoint(end, m_pageXOffset, m_pageYOffset);

const int max = 1000;
fz_quad hits[max];
Expand All @@ -27,59 +27,48 @@ void TextSelector::generateSelectionRects(QList<mupdf::FzQuad>& container,
for(int i = 0; i < n; ++i)
{
fz_quad hit = hits[i];
// apply offsets to create new fz_quad
hit = fz_make_quad(hit.ul.x - m_pageXOffset, hit.ul.y - m_pageYOffset,
hit.ur.x - m_pageXOffset, hit.ur.y - m_pageYOffset,
hit.ll.x - m_pageXOffset, hit.ll.y - m_pageYOffset,
hit.lr.x - m_pageXOffset, hit.lr.y - m_pageYOffset);
hit = utils::moveQuad(hit, m_pageXOffset, m_pageYOffset);

container.append(hit);
}
}

FzPointPair TextSelector::getPositionsForWordSelection(mupdf::FzPoint start,
mupdf::FzPoint end)
{
start = mupdf::ll_fz_make_point(start.x + m_pageXOffset,
start.y + m_pageYOffset);
end = mupdf::ll_fz_make_point(end.x + m_pageXOffset, end.y + m_pageYOffset);
start = utils::movePoint(start, m_pageXOffset, m_pageYOffset);
end = utils::movePoint(end, m_pageXOffset, m_pageYOffset);

// This modifies the fzBegin and fzEnd.
mupdf::ll_fz_snap_selection(m_textPage->m_internal, start.internal(),
end.internal(), FZ_SELECT_WORDS);

start = mupdf::ll_fz_make_point(start.x - m_pageXOffset,
start.y - m_pageYOffset);
end = mupdf::ll_fz_make_point(end.x - m_pageXOffset, end.y - m_pageYOffset);
start = utils::movePoint(start, -m_pageXOffset, -m_pageYOffset);
end = utils::movePoint(end, -m_pageXOffset, -m_pageYOffset);

return { start, end };
}

FzPointPair TextSelector::getPositionsForLineSelection(mupdf::FzPoint point)
{
mupdf::FzPoint start(point.x, point.y);
mupdf::FzPoint end = start;

start = mupdf::ll_fz_make_point(start.x + m_pageXOffset,
start.y + m_pageYOffset);
end = mupdf::ll_fz_make_point(end.x + m_pageXOffset, end.y + m_pageYOffset);
auto start = utils::movePoint(point, m_pageXOffset, m_pageYOffset);
auto end = utils::movePoint(point, m_pageXOffset, m_pageYOffset);

// This modifies the fzPoint
mupdf::ll_fz_snap_selection(m_textPage->m_internal, start.internal(),
end.internal(), FZ_SELECT_LINES);

start = mupdf::ll_fz_make_point(start.x - m_pageXOffset,
start.y - m_pageYOffset);
end = mupdf::ll_fz_make_point(end.x - m_pageXOffset, end.y - m_pageYOffset);
start = utils::movePoint(start, -m_pageXOffset, -m_pageYOffset);
end = utils::movePoint(end, -m_pageXOffset, -m_pageYOffset);

return { start, end };
}

std::string TextSelector::getTextFromSelection(mupdf::FzPoint start,
mupdf::FzPoint end)
{
start = mupdf::ll_fz_make_point(start.x + m_pageXOffset,
start.y + m_pageYOffset);
end = mupdf::ll_fz_make_point(end.x + m_pageXOffset, end.y + m_pageYOffset);
start = utils::movePoint(start, m_pageXOffset, m_pageYOffset);
end = utils::movePoint(end, m_pageXOffset, m_pageYOffset);

auto text = m_textPage->fz_copy_selection(start, end, 1);
return text;
Expand Down

0 comments on commit 407f949

Please sign in to comment.