Skip to content

Commit

Permalink
Revert css page size support
Browse files Browse the repository at this point in the history
  • Loading branch information
sammycage committed Jun 21, 2024
1 parent 902cc31 commit f3030ae
Show file tree
Hide file tree
Showing 11 changed files with 44 additions and 68 deletions.
1 change: 0 additions & 1 deletion include/plutobook.h
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,6 @@ PLUTOBOOK_API plutobook_page_size_t plutobook_get_page_size(const plutobook_t* b
PLUTOBOOK_API plutobook_page_margins_t plutobook_get_page_margins(const plutobook_t* book);
PLUTOBOOK_API plutobook_page_size_t plutobook_get_page_size_at(const plutobook_t* book, unsigned int index);
PLUTOBOOK_API plutobook_media_type_t plutobook_get_media_type(const plutobook_t* book);
PLUTOBOOK_API float plutobook_get_page_scale(const plutobook_t* book);
PLUTOBOOK_API unsigned int plutobook_get_page_count(const plutobook_t* book);

PLUTOBOOK_API plutobook_status_t plutobook_load_url(plutobook_t* book, const char* url, const char* user_style, const char* user_script);
Expand Down
13 changes: 0 additions & 13 deletions include/plutobook.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -786,18 +786,6 @@ class PLUTOBOOK_API Book {
*/
MediaType mediaType() const { return m_mediaType; }

/**
* @brief pageScale
* @return
*/
float pageScale() const { return m_pageScale; }

/**
* @brief setPageScale
* @param scale
*/
void setPageScale(float scale) { m_pageScale = scale; }

/**
* @brief pageCount
* @return
Expand Down Expand Up @@ -1030,7 +1018,6 @@ class PLUTOBOOK_API Book {
PageSize m_pageSize;
PageMargins m_pageMargins;
MediaType m_mediaType;
float m_pageScale{0};

mutable bool m_needsBuild{true};
mutable bool m_needsLayout{true};
Expand Down
4 changes: 2 additions & 2 deletions source/document.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -826,12 +826,12 @@ void Document::renderPage(GraphicsContext& context, uint32_t pageIndex)
if(pageIndex >= m_pages.size())
return;
const auto& page = m_pages[pageIndex];
Rect pageRect(0, page->pageTop(), box()->pageWidth(), page->pageBottom() - page->pageTop());
Rect pageRect(0, page->pageTop(), (page->width() - page->marginWidth()) / page->pageScale(), page->pageBottom() - page->pageTop());
if(pageRect.isEmpty())
return;
context.save();
context.translate(page->marginLeft(), page->marginTop());
context.scale(box()->pageScale(), box()->pageScale());
context.scale(page->pageScale(), page->pageScale());
context.translate(-pageRect.x, -pageRect.y);
context.clipRect(pageRect);
box()->layer()->paint(context, pageRect);
Expand Down
5 changes: 5 additions & 0 deletions source/layout/boxstyle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,11 @@ Heap* BoxStyle::heap() const
return m_node->heap();
}

Book* BoxStyle::book() const
{
return document()->book();
}

void BoxStyle::setFont(RefPtr<Font> font)
{
m_font = std::move(font);
Expand Down
2 changes: 2 additions & 0 deletions source/layout/boxstyle.h
Original file line number Diff line number Diff line change
Expand Up @@ -616,6 +616,7 @@ class Font;
class Image;
class Node;
class Document;
class Book;
class CSSValue;

enum class CSSValueID : uint16_t;
Expand Down Expand Up @@ -653,6 +654,7 @@ class BoxStyle : public HeapMember, public RefCounted<BoxStyle> {

Document* document() const;
Heap* heap() const;
Book* book() const;

Node* node() const { return m_node; }
PseudoType pseudoType() const { return m_pseudoType; }
Expand Down
27 changes: 3 additions & 24 deletions source/layout/boxview.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,41 +19,20 @@ bool BoxView::isPrintMedia() const

void BoxView::computeWidth(float& x, float& width, float& marginLeft, float& marginRight) const
{
width = pageWidth();
width = document()->viewportWidth();
}

void BoxView::computeHeight(float& y, float& height, float& marginTop, float& marginBottom) const
{
height = pageHeight();
height = document()->viewportHeight();
}

void BoxView::layoutContents()
void BoxView::layout()
{
BlockFlowBox::layout();
layer()->layout();
}

void BoxView::layout()
{
auto pageScale = isPrintMedia() ? document()->book()->pageScale() : 1.f;
m_pageWidth = document()->viewportWidth();
m_pageHeight = document()->viewportHeight();
m_pageScale = 1.f;
if(pageScale > 0.f && isPrintMedia()) {
m_pageScale = pageScale / 100.f;
m_pageWidth /= m_pageScale;
m_pageHeight /= m_pageScale;
}

layoutContents();
if(pageScale <= 0.f && isPrintMedia() && m_pageWidth < document()->width()) {
m_pageScale = m_pageWidth / document()->width();
m_pageWidth /= m_pageScale;
m_pageHeight /= m_pageScale;
layoutContents();
}
}

void BoxView::build()
{
m_backgroundStyle = document()->rootStyle();
Expand Down
8 changes: 0 additions & 8 deletions source/layout/boxview.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,25 +13,17 @@ class BoxView final : public BlockFlowBox {
bool requiresLayer() const final { return true; }
BoxStyle* backgroundStyle() const { return m_backgroundStyle; }

float pageWidth() const { return m_pageWidth; }
float pageHeight() const { return m_pageHeight; }
float pageScale() const { return m_pageScale; }

bool isPrintMedia() const;

void computeWidth(float& x, float& width, float& marginLeft, float& marginRight) const final;
void computeHeight(float& y, float& height, float& marginTop, float& marginBottom) const final;
void layoutContents();
void layout() final;
void build() final;

const char* name() const final { return "BoxView"; }

private:
BoxStyle* m_backgroundStyle{nullptr};
float m_pageWidth{0};
float m_pageHeight{0};
float m_pageScale{1};
};

template<>
Expand Down
28 changes: 15 additions & 13 deletions source/layout/pagebox.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,6 @@ std::unique_ptr<PageBox> PageBox::create(const RefPtr<BoxStyle>& style, const Gl
return std::unique_ptr<PageBox>(new (style->heap()) PageBox(style, pageName, pageIndex));
}

const PageSize& PageBox::pageSize() const
{
return document()->book()->pageSize();
}

void PageBox::updateOverflowRect()
{
BlockBox::updateOverflowRect();
Expand All @@ -28,18 +23,24 @@ void PageBox::computePreferredWidths(float& minWidth, float& maxWidth) const

void PageBox::computeWidth(float& x, float& width, float& marginLeft, float& marginRight) const
{
auto book = document()->book();
width = book->pageSize().height() / units::px;
marginLeft = book->pageMargins().left() / units::px;
marginRight = book->pageMargins().right() / units::px;
auto marginLeftLength = style()->marginLeft();
auto marginRightLength = style()->marginRight();

const auto& deviceMargins = document()->book()->pageMargins();
marginLeft = marginLeftLength.isAuto() ? deviceMargins.left() / units::px : marginLeftLength.calcMin(m_pageSize.width() / units::px);
marginRight = marginRightLength.isAuto() ? deviceMargins.right() / units::px : marginRightLength.calcMin(m_pageSize.width() / units::px);
width = m_pageSize.width() / units::px;
}

void PageBox::computeHeight(float& y, float& height, float& marginTop, float& marginBottom) const
{
auto book = document()->book();
height = book->pageSize().height() / units::px;
marginTop = book->pageMargins().top() / units::px;
marginBottom = book->pageMargins().bottom() / units::px;
auto marginTopLength = style()->marginTop();
auto marginBottomLength = style()->marginBottom();

const auto& deviceMargins = document()->book()->pageMargins();
marginTop = marginTopLength.isAuto() ? deviceMargins.top() / units::px : marginTopLength.calcMin(m_pageSize.height() / units::px);
marginBottom = marginBottomLength.isAuto() ? deviceMargins.bottom() / units::px : marginBottomLength.calcMin(m_pageSize.height() / units::px);
height = m_pageSize.height() / units::px;
}

void PageBox::layout()
Expand All @@ -55,6 +56,7 @@ void PageBox::build()

PageBox::PageBox(const RefPtr<BoxStyle>& style, const GlobalString& pageName, uint32_t pageIndex)
: BlockBox(nullptr, style)
, m_pageSize(style->getPageSize(style->book()->pageSize()))
, m_pageName(pageName)
, m_pageIndex(pageIndex)
{
Expand Down
6 changes: 5 additions & 1 deletion source/layout/pagebox.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,17 @@ class PageBox final : public BlockBox {

bool isPageBox() const final { return true; }
bool requiresLayer() const final { return false; }
const PageSize& pageSize() const { return m_pageSize; }
const GlobalString& pageName() const { return m_pageName; }
uint32_t pageIndex() const { return m_pageIndex; }
const PageSize& pageSize() const;

float pageTop() const { return m_pageTop; }
float pageBottom() const { return m_pageBottom; }
float pageScale() const { return m_pageScale; }

void setPageTop(float pageTop) { m_pageTop = pageTop; }
void setPageBottom(float pageBottom) { m_pageBottom = pageBottom; }
void setPageScale(float pageScale) { m_pageScale = pageScale; }

void updateOverflowRect() final;
void computePreferredWidths(float& minWidth, float& maxWidth) const final;
Expand All @@ -34,10 +36,12 @@ class PageBox final : public BlockBox {

private:
PageBox(const RefPtr<BoxStyle>& style, const GlobalString& pageName, uint32_t pageIndex);
PageSize m_pageSize;
GlobalString m_pageName;
uint32_t m_pageIndex;
float m_pageTop{0};
float m_pageBottom{0};
float m_pageScale{1};
};

template<>
Expand Down
13 changes: 12 additions & 1 deletion source/layout/pagebuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,19 @@ void PageBuilder::newPage(const BoxFrame* box, float top)
pageBox->build();
pageBox->layout();

auto pageWidth = std::max(1.f, pageBox->width() - pageBox->marginWidth());
auto pageHeight = std::max(1.f, pageBox->height() - pageBox->marginHeight());
if(auto pageScale = pageStyle->pageScale()) {
pageBox->setPageScale(pageScale.value());
} else if(pageWidth < m_document->width()) {
pageBox->setPageScale(pageWidth / m_document->width());
} else {
pageBox->setPageScale(1.f);
}

pageBox->setPageTop(top);
pageBox->setPageBottom(top + m_document->box()->pageHeight());
pageBox->setPageBottom(top + (pageHeight / pageBox->pageScale()));

m_currentPage = pageBox.get();
m_pages.push_back(std::move(pageBox));
}
Expand Down
5 changes: 0 additions & 5 deletions source/plutobook.cc
Original file line number Diff line number Diff line change
Expand Up @@ -524,11 +524,6 @@ plutobook_page_margins_t plutobook_get_page_margins(const plutobook_t* book)
return getBook(book).pageMargins();
}

float plutobook_get_page_scale(const plutobook_t* book)
{
return getBook(book).pageScale();
}

unsigned int plutobook_get_page_count(const plutobook_t* book)
{
return getBook(book).pageCount();
Expand Down

0 comments on commit f3030ae

Please sign in to comment.