This repository was archived by the owner on May 6, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 162
Fix page breaks in reflowable Epub books #229
Open
olivierkorner
wants to merge
5
commits into
readium:develop
Choose a base branch
from
ArtBookMagazine:feature/page_breaks
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
6ee6857
First working prototype of the CSS content filter, with page-break su…
olivierkorner d001d5f
Refactor CSSPreprocessor to make it a generic CSS rewrite engine
olivierkorner 9d5381f
Remove comments and debug code
olivierkorner 1eb58c6
Revert PassThroughFilter.cpp to previous version
olivierkorner 2403761
Merge remote-tracking branch 'upstream/develop' into feature/page_breaks
olivierkorner File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,157 @@ | ||
// | ||
// css_preprocessor.cpp | ||
// ePub3 | ||
// | ||
// Created by Olivier Körner on 2016-03-08. | ||
// Copyright (c) 2016 Readium Foundation and/or its licensees. All rights reserved. | ||
// | ||
// This program is distributed in the hope that it will be useful, but WITHOUT ANY | ||
// WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | ||
// | ||
// Licensed under Gnu Affero General Public License Version 3 (provided, notwithstanding this notice, | ||
// Readium Foundation reserves the right to license this material under a different separate license, | ||
// and if you have done so, the terms of that separate license control and the following references | ||
// to GPL do not apply). | ||
// | ||
// This program is free software: you can redistribute it and/or modify it under the terms of the GNU | ||
// Affero General Public License as published by the Free Software Foundation, either version 3 of | ||
// the License, or (at your option) any later version. You should have received a copy of the GNU | ||
// Affero General Public License along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
|
||
#include "css_preprocessor.h" | ||
#include "package.h" | ||
#include "filter_manager.h" | ||
|
||
static const REGEX_NS::regex::flag_type regexFlags(REGEX_NS::regex::ECMAScript|REGEX_NS::regex::optimize|REGEX_NS::regex::icase); | ||
|
||
EPUB3_BEGIN_NAMESPACE | ||
|
||
static REGEX_NS::regex CSSMatcher("page\\-break\\-(after|before|inside) *\\: *(always|avoid|left|right)", regexFlags); | ||
static REGEX_NS::regex StyleAttributeMatcher("<[^>]+style=\\\"([^\\\"]*)\"", regexFlags); | ||
static REGEX_NS::regex StyleTagMatcher("<style[^>]*>((.|\n|\r)*?)<\\/style>", regexFlags); | ||
|
||
static const std::string PageBreakReplacement = "-webkit-column-break-$1: $2; column-break-$&: $2"; | ||
|
||
|
||
bool CSSPreprocessor::ShouldApply(ConstManifestItemPtr item) | ||
{ | ||
auto mediaType = item->MediaType(); | ||
bool itemPrepaginated = false; | ||
auto iprop = item->PropertyMatching("layout", "rendition"); | ||
if (iprop != nullptr) { | ||
auto ilayout = iprop->Value(); | ||
itemPrepaginated = (ilayout == "pre-paginated"); | ||
} | ||
bool pkgPrepaginated = false; | ||
auto prop = item->GetPackage()->PropertyMatching("layout", "rendition"); | ||
if (prop != nullptr) { | ||
auto layout = prop->Value(); | ||
pkgPrepaginated = (layout == "pre-paginated"); | ||
} | ||
|
||
if (itemPrepaginated || pkgPrepaginated) | ||
return false; | ||
|
||
return (mediaType == "application/xhtml+xml" || mediaType == "text/html" || mediaType == "text/css"); | ||
} | ||
|
||
ContentFilterPtr CSSPreprocessor::CSSFilterFactory(ConstPackagePtr package) | ||
{ | ||
CSSSubstitution pageBreakSub(CSSMatcher, PageBreakReplacement); | ||
std::vector<CSSSubstitution> substitutions { pageBreakSub }; | ||
return New(package, substitutions); | ||
} | ||
|
||
void CSSPreprocessor::Register() | ||
{ | ||
FilterManager::Instance()->RegisterFilter("CSSPreprocessor", ValidationComplete, CSSFilterFactory); | ||
} | ||
|
||
CSSPreprocessor::CSSPreprocessor(ConstPackagePtr pkg, CSSSubstitutionList substitutions) : ContentFilter(ShouldApply), m_substitutions(substitutions) | ||
{ | ||
} | ||
|
||
|
||
void* CSSPreprocessor::FilterData(FilterContext* context, void *data, size_t len, size_t *outputLen) | ||
{ | ||
CSSFilterContext* p = dynamic_cast<CSSFilterContext*>(context); | ||
bool isCSS = p->isCSS(); | ||
char* input = reinterpret_cast<char*>(data); | ||
|
||
std::string output; | ||
if (isCSS) { | ||
// Data is pure CSS, proceed with substitutions | ||
output.assign(input, len); | ||
for (CSSSubstitution& substitution: m_substitutions) { | ||
output = REGEX_NS::regex_replace(output, substitution.GetSearchRegex(), substitution.GetReplaceFormat()); | ||
} | ||
} | ||
else | ||
{ | ||
// Data is HTML, look for <style> elements and style attributes | ||
|
||
std::string toutput; | ||
// find each `style` tags | ||
REGEX_NS::cregex_iterator pos(input, input+len, StyleTagMatcher); | ||
REGEX_NS::cregex_iterator end; | ||
if ( pos == end ) | ||
{ | ||
toutput.assign(input, len); // no match | ||
} | ||
else | ||
{ | ||
while (pos != end) { | ||
toutput += pos->prefix(); | ||
|
||
std::string str = pos->str(); | ||
for (CSSSubstitution& substitution: m_substitutions) { | ||
str = REGEX_NS::regex_replace(str, substitution.GetSearchRegex(), substitution.GetReplaceFormat()); | ||
} | ||
toutput += str; | ||
|
||
auto here = pos++; | ||
if ( pos == end ) | ||
toutput += here->suffix(); | ||
} | ||
} | ||
|
||
// find each `style` attributes | ||
REGEX_NS::sregex_iterator apos(toutput.begin(), toutput.end(), StyleAttributeMatcher); | ||
REGEX_NS::sregex_iterator aend; | ||
if ( apos == aend ) | ||
{ | ||
output = toutput; // no match | ||
} | ||
else { | ||
while (apos != aend) { | ||
output += apos->prefix(); | ||
|
||
std::string str = apos->str(); | ||
for (CSSSubstitution& substitution: m_substitutions) { | ||
str = REGEX_NS::regex_replace(str, substitution.GetSearchRegex(), substitution.GetReplaceFormat()); | ||
} | ||
output += str; | ||
|
||
auto here = apos++; | ||
if ( apos == aend ) | ||
output += here->suffix(); | ||
} | ||
} | ||
|
||
} | ||
|
||
*outputLen = output.size(); | ||
if ( output.size() < len ) | ||
{ | ||
// use the incoming buffer | ||
output.copy(input, output.size()); | ||
return input; | ||
} | ||
|
||
// allocate a new buffer and copy | ||
char* result = new char[output.size()]; | ||
output.copy(result, output.size()); | ||
return result; | ||
} | ||
|
||
EPUB3_END_NAMESPACE |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,142 @@ | ||
// | ||
// css_preprocessor.hpp | ||
// ePub3 | ||
// | ||
// Created by Olivier Körner on 03/03/2016. | ||
// Copyright (c) 2016 The Readium Foundation and contributors. All rights reserved. | ||
// | ||
// This program is distributed in the hope that it will be useful, but WITHOUT ANY | ||
// WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | ||
// | ||
// Licensed under Gnu Affero General Public License Version 3 (provided, notwithstanding this notice, | ||
// Readium Foundation reserves the right to license this material under a different separate license, | ||
// and if you have done so, the terms of that separate license control and the following references | ||
// to GPL do not apply). | ||
// | ||
// This program is free software: you can redistribute it and/or modify it under the terms of the GNU | ||
// Affero General Public License as published by the Free Software Foundation, either version 3 of | ||
// the License, or (at your option) any later version. You should have received a copy of the GNU | ||
// Affero General Public License along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
|
||
#ifndef __ePub3__css_preprocessor_hpp | ||
#define __ePub3__css_preprocessor_hpp | ||
|
||
#include <ePub3/epub3.h> | ||
#include <ePub3/filter.h> | ||
#include <ePub3/utilities/iri.h> | ||
#include <ePub3/content_handler.h> | ||
#include REGEX_INCLUDE | ||
|
||
EPUB3_BEGIN_NAMESPACE | ||
|
||
class Package; | ||
|
||
/** | ||
Implements a filter for reading content documents which statically replaces `object` | ||
elements with `iframe` elements referencing the appropriate DHTML handler. | ||
@ingroup filters | ||
*/ | ||
class CSSPreprocessor : public ContentFilter, public PointerType<CSSPreprocessor> | ||
{ | ||
protected: | ||
/// | ||
/// Matches only mnifest items with a media-type of "application/xhtml+xml" or "text/html" or "text/css". | ||
static bool ShouldApply(ConstManifestItemPtr item); | ||
|
||
/// The factory routine | ||
static ContentFilterPtr CSSFilterFactory(ConstPackagePtr package); | ||
|
||
private: | ||
|
||
/// | ||
/// No default constructor. | ||
//CSSPreprocessor() : ContentFilter(ShouldApply) { } | ||
CSSPreprocessor() _DELETED_; | ||
|
||
// Filter context for CSS preprocessing: is the stream CSS or HTML? | ||
class CSSFilterContext : public FilterContext | ||
{ | ||
private: | ||
bool _isCSS; | ||
|
||
public: | ||
CSSFilterContext() : FilterContext(), _isCSS(false) {} | ||
CSSFilterContext(ConstManifestItemPtr item) { _isCSS = (item->MediaType().compare("text/css") == 0); } | ||
|
||
bool isCSS() const { return _isCSS; } | ||
void setCSS(bool val) { _isCSS = val; } | ||
|
||
}; | ||
|
||
public: | ||
class CSSSubstitution | ||
{ | ||
public: | ||
CSSSubstitution(const REGEX_NS::regex& search_re, const std::string& replace) : m_search_re(search_re), m_replace(replace) {} | ||
CSSSubstitution(const CSSSubstitution &o) : m_search_re(o.m_search_re), m_replace(o.m_replace) {} | ||
CSSSubstitution(CSSSubstitution &&o) : m_search_re(std::move(o.m_search_re)), m_replace(std::move(o.m_replace)) {} | ||
virtual ~CSSSubstitution() {} | ||
|
||
const REGEX_NS::regex& GetSearchRegex() const | ||
{ | ||
return m_search_re; | ||
} | ||
const std::string& GetReplaceFormat() const | ||
{ | ||
return m_replace; | ||
} | ||
|
||
private: | ||
|
||
REGEX_NS::regex m_search_re; | ||
std::string m_replace; | ||
|
||
}; | ||
|
||
typedef std::vector<CSSSubstitution> CSSSubstitutionList; | ||
|
||
/** | ||
Initializes a preprocessor and associates it with a Package object, from which | ||
it can obtain foreign media handler details. | ||
@param pkg The package to which this filter will apply. | ||
*/ | ||
EPUB3_EXPORT | ||
CSSPreprocessor(ConstPackagePtr pkg, CSSSubstitutionList substitutions); | ||
//CSSPreprocessor(ConstPackagePtr pkg) : ContentFilter(ShouldApply); | ||
|
||
/// | ||
/// Standard copy constructor. | ||
CSSPreprocessor(const CSSPreprocessor& o) : ContentFilter(o), m_substitutions(o.m_substitutions) {} | ||
|
||
/// | ||
/// C++11 'move' constructor. | ||
CSSPreprocessor(CSSPreprocessor&& o) : ContentFilter(std::move(o)), m_substitutions(std::move(o.m_substitutions)) {} | ||
|
||
/// | ||
/// Destructor. | ||
|
||
/// | ||
/// This preprocessor requires access to the entire content document at once. | ||
virtual OperatingMode GetOperatingMode() const OVERRIDE { return OperatingMode::RequiresCompleteData; } | ||
|
||
/** | ||
Performs the static replacement of `object` tags whose `type` attribute | ||
identifies a media-type for which the Publication provides a media handler. | ||
|
||
*/ | ||
virtual void* FilterData(FilterContext* context, void* data, size_t len, size_t* outputLen) OVERRIDE; | ||
|
||
// register with the filter manager | ||
static void Register(); | ||
|
||
private: | ||
CSSSubstitutionList m_substitutions; | ||
|
||
protected: | ||
|
||
virtual FilterContext *InnerMakeFilterContext(ConstManifestItemPtr item) const OVERRIDE { return new CSSFilterContext(item); } | ||
}; | ||
|
||
EPUB3_END_NAMESPACE | ||
|
||
#endif /* defined(__ePub3__css_preprocessor__) */ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is
ValidationComplete
the most adequate Content Filter priority? I think so, but just double-checking (must be after decryption).There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I checked: ValidationComplete is the lowest priority, the filter is applied last.