Skip to content
This repository has been archived by the owner on Nov 26, 2020. It is now read-only.

Rtl support closes #104 #105

Merged
merged 6 commits into from
Aug 3, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 7 additions & 1 deletion Source/EPUBCore/FREpubParser.swift
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,13 @@ class FREpubParser: NSObject, SSZipArchiveDelegate {
}

// Read Spine
book.spine = readSpine(xmlDoc.root["spine"].children)
let spine = xmlDoc.root["spine"]
book.spine = readSpine(spine.children)

// Page progress direction `ltr` or `rtl`
if let pageProgressionDirection = spine.attributes["page-progression-direction"] {
book.spine.pageProgressionDirection = pageProgressionDirection
}
} catch {
print("Cannot read .opf file.")
}
Expand Down
7 changes: 7 additions & 0 deletions Source/EPUBCore/FRSpine.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,14 @@ struct Spine {
}

class FRSpine: NSObject {
var pageProgressionDirection: String?
var spineReferences = [Spine]()
var isRtl: Bool {
if let pageProgressionDirection = pageProgressionDirection where pageProgressionDirection == "rtl" {
return true
}
return false
}

func nextChapter(href: String) -> FRResource? {
var found = false;
Expand Down
11 changes: 6 additions & 5 deletions Source/FolioReaderAudioPlayer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ class FolioReaderAudioPlayer: NSObject {

func play() {
if book.hasAudio() {
let currentPage = FolioReader.sharedInstance.readerCenter.currentPage
guard let currentPage = FolioReader.sharedInstance.readerCenter.currentPage else { return }
currentPage.webView.js("playAudio()")
} else {
readCurrentSentence()
Expand Down Expand Up @@ -266,7 +266,7 @@ class FolioReaderAudioPlayer: NSObject {

guard let player = player else { return false }

setRate(FolioReader.sharedInstance.currentAudioRate)
setRate(FolioReader.currentAudioRate)
player.enableRate = true
player.prepareToPlay()
player.delegate = self
Expand Down Expand Up @@ -335,7 +335,7 @@ class FolioReaderAudioPlayer: NSObject {
if synthesizer == nil {
synthesizer = AVSpeechSynthesizer()
synthesizer.delegate = self
setRate(FolioReader.sharedInstance.currentAudioRate)
setRate(FolioReader.currentAudioRate)
}

let utterance = AVSpeechUtterance(string: text)
Expand Down Expand Up @@ -378,8 +378,9 @@ class FolioReaderAudioPlayer: NSObject {
} else {
if synthesizer.speaking {
stopSynthesizer(immediate: false, completion: {
let currentPage = FolioReader.sharedInstance.readerCenter.currentPage
currentPage.webView.js("resetCurrentSentenceIndex()")
if let currentPage = FolioReader.sharedInstance.readerCenter.currentPage {
currentPage.webView.js("resetCurrentSentenceIndex()")
}
self.speakSentence()
})
} else {
Expand Down
86 changes: 62 additions & 24 deletions Source/FolioReaderCenter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class FolioReaderCenter: UIViewController, UICollectionViewDelegate, UICollectio
var pages: [String]!
var totalPages: Int!
var tempFragment: String?
var currentPage: FolioReaderPage!
var currentPage: FolioReaderPage?
var animator: ZFModalTransitionAnimator!
var pageIndicatorView: FolioReaderPageIndicator!
var bookShareLink: String?
Expand Down Expand Up @@ -156,6 +156,7 @@ class FolioReaderCenter: UIViewController, UICollectionViewDelegate, UICollectio

collectionView.reloadData()
configureNavBarButtons()
setCollectionViewProgressiveDirection()

if let position = FolioReader.defaults.valueForKey(kBookId) as? NSDictionary,
let pageNumber = position["pageNumber"] as? Int where pageNumber > 0 {
Expand All @@ -167,9 +168,31 @@ class FolioReaderCenter: UIViewController, UICollectionViewDelegate, UICollectio
currentPageNumber = 1
}

// MARK: Change page progressive direction

func setCollectionViewProgressiveDirection() {
if FolioReader.needsRTLChange {
collectionView.transform = CGAffineTransformMakeScale(-1, 1)
} else {
collectionView.transform = CGAffineTransformIdentity
}
}

func setPageProgressiveDirection(page: FolioReaderPage) {
if FolioReader.needsRTLChange {
// if page.transform.a == -1 { return }
page.transform = CGAffineTransformMakeScale(-1, 1)
} else {
page.transform = CGAffineTransformIdentity
}
}


// MARK: Change layout orientation

func setScrollDirection(direction: FolioReaderScrollDirection) {
guard let currentPage = currentPage else { return }

// Get internal page offset before layout change
let pageScrollView = currentPage.webView.scrollView
pageOffsetRate = pageScrollView.contentOffset.forDirection() / pageScrollView.contentSize.forDirection()
Expand All @@ -181,6 +204,11 @@ class FolioReaderCenter: UIViewController, UICollectionViewDelegate, UICollectio
collectionView.collectionViewLayout.invalidateLayout()
collectionView.setContentOffset(frameForPage(currentPageNumber).origin, animated: false)

// Page progressive direction
setCollectionViewProgressiveDirection()
delay(0.2) { self.setPageProgressiveDirection(currentPage) }


/**
* This delay is needed because the page will not be ready yet
* so the delay wait until layout finished the changes.
Expand Down Expand Up @@ -265,6 +293,8 @@ class FolioReaderCenter: UIViewController, UICollectionViewDelegate, UICollectio
cell.delegate = self
cell.backgroundColor = UIColor.clearColor()

setPageProgressiveDirection(cell)

// Configure the cell
let resource = book.spine.spineReferences[indexPath.row].resource
var html = try? String(contentsOfFile: resource.fullHref, encoding: NSUTF8StringEncoding)
Expand All @@ -282,7 +312,7 @@ class FolioReaderCenter: UIViewController, UICollectionViewDelegate, UICollectio

// Font class name
var classes = ""
let currentFontName = FolioReader.sharedInstance.currentFontName
let currentFontName = FolioReader.currentFontName
switch currentFontName {
case 0:
classes = "andada"
Expand All @@ -300,15 +330,15 @@ class FolioReaderCenter: UIViewController, UICollectionViewDelegate, UICollectio
break
}

classes += " "+FolioReader.sharedInstance.currentMediaOverlayStyle.className()
classes += " "+FolioReader.currentMediaOverlayStyle.className()

// Night mode
if FolioReader.sharedInstance.nightMode {
if FolioReader.nightMode {
classes += " nightMode"
}

// Font Size
let currentFontSize = FolioReader.sharedInstance.currentFontSize
let currentFontSize = FolioReader.currentFontSize
switch currentFontSize {
case 0:
classes += " textSizeOne"
Expand Down Expand Up @@ -343,7 +373,7 @@ class FolioReaderCenter: UIViewController, UICollectionViewDelegate, UICollectio
// MARK: - Device rotation

override func willRotateToInterfaceOrientation(toInterfaceOrientation: UIInterfaceOrientation, duration: NSTimeInterval) {
if !FolioReader.sharedInstance.isReaderReady { return }
guard FolioReader.isReaderReady else { return }

setPageSize(toInterfaceOrientation)
updateCurrentPage()
Expand Down Expand Up @@ -375,13 +405,15 @@ class FolioReaderCenter: UIViewController, UICollectionViewDelegate, UICollectio
self.collectionView.collectionViewLayout.invalidateLayout()

// Adjust internal page offset
let pageScrollView = self.currentPage.webView.scrollView
guard let currentPage = self.currentPage else { return }
let pageScrollView = currentPage.webView.scrollView
self.pageOffsetRate = pageScrollView.contentOffset.forDirection() / pageScrollView.contentSize.forDirection()
})
}

override func didRotateFromInterfaceOrientation(fromInterfaceOrientation: UIInterfaceOrientation) {
if !FolioReader.sharedInstance.isReaderReady { return }
guard FolioReader.isReaderReady else { return }
guard let currentPage = currentPage else { return }

// Update pages
pagesForCurrentPage(currentPage)
Expand All @@ -390,7 +422,7 @@ class FolioReaderCenter: UIViewController, UICollectionViewDelegate, UICollectio
scrollScrubber.setSliderVal()

// After rotation fix internal page offset
var pageOffset = self.currentPage.webView.scrollView.contentSize.forDirection() * pageOffsetRate
var pageOffset = currentPage.webView.scrollView.contentSize.forDirection() * pageOffsetRate

// Fix the offset for paged scroll
if readerConfig.scrollDirection == .horizontal {
Expand All @@ -399,11 +431,11 @@ class FolioReaderCenter: UIViewController, UICollectionViewDelegate, UICollectio
}

let pageOffsetPoint = isVerticalDirection(CGPoint(x: 0, y: pageOffset), CGPoint(x: pageOffset, y: 0))
self.currentPage.webView.scrollView.setContentOffset(pageOffsetPoint, animated: true)
currentPage.webView.scrollView.setContentOffset(pageOffsetPoint, animated: true)
}

override func willAnimateRotationToInterfaceOrientation(toInterfaceOrientation: UIInterfaceOrientation, duration: NSTimeInterval) {
if !FolioReader.sharedInstance.isReaderReady { return }
guard FolioReader.isReaderReady else { return }

if currentPageNumber+1 >= totalPages {
UIView.animateWithDuration(duration, animations: {
Expand Down Expand Up @@ -481,12 +513,11 @@ class FolioReaderCenter: UIViewController, UICollectionViewDelegate, UICollectio
}

func pagesForCurrentPage(page: FolioReaderPage?) {
if let page = page {
let pageSize = isVerticalDirection(pageHeight, pageWidth)
pageIndicatorView.totalPages = Int(ceil(page.webView.scrollView.contentSize.forDirection()/pageSize))
let webViewPage = pageForOffset(currentPage.webView.scrollView.contentOffset.x, pageHeight: pageSize)
pageIndicatorView.currentPage = webViewPage
}
guard let page = page else { return }
let pageSize = isVerticalDirection(pageHeight, pageWidth)
pageIndicatorView.totalPages = Int(ceil(page.webView.scrollView.contentSize.forDirection()/pageSize))
let webViewPage = pageForOffset(page.webView.scrollView.contentOffset.x, pageHeight: pageSize)
pageIndicatorView.currentPage = webViewPage
}

func pageForOffset(offset: CGFloat, pageHeight height: CGFloat) -> Int {
Expand Down Expand Up @@ -543,10 +574,10 @@ class FolioReaderCenter: UIViewController, UICollectionViewDelegate, UICollectio

func changePageWith(page page: Int, andFragment fragment: String, animated: Bool = false, completion: (() -> Void)? = nil) {
if currentPageNumber == page {
if fragment != "" && currentPage != nil {
if let currentPage = currentPage where fragment != "" {
currentPage.handleAnchor(fragment, avoidBeginningAnchors: true, animated: animated)
completion?()
}
completion?()
} else {
tempFragment = fragment
changePageWith(page: page, animated: animated, completion: { () -> Void in
Expand All @@ -569,17 +600,18 @@ class FolioReaderCenter: UIViewController, UICollectionViewDelegate, UICollectio

func changePageWith(href href: String, andAudioMarkID markID: String) {
if recentlyScrolled { return } // if user recently scrolled, do not change pages or scroll the webview
guard let currentPage = currentPage else { return }

let item = findPageByHref(href)
let pageUpdateNeeded = item+1 != currentPage.pageNumber
let indexPath = NSIndexPath(forRow: item, inSection: 0)
changePageWith(indexPath: indexPath, animated: true) { () -> Void in
if pageUpdateNeeded {
self.updateCurrentPage({ () -> Void in
self.currentPage.audioMarkID(markID)
currentPage.audioMarkID(markID)
})
} else {
self.currentPage.audioMarkID(markID)
currentPage.audioMarkID(markID)
}
}
}
Expand Down Expand Up @@ -701,6 +733,7 @@ class FolioReaderCenter: UIViewController, UICollectionViewDelegate, UICollectio
Sharing chapter method.
*/
func shareChapter(sender: UIBarButtonItem) {
guard let currentPage = currentPage else { return }

if let chapterText = currentPage.webView.js("getBodyText()") {

Expand Down Expand Up @@ -970,15 +1003,20 @@ extension FolioReaderCenter: FolioReaderPageDelegate {
if currentPageNumber == pageNumber && pageOffset > 0 {
page.scrollPageToOffset(pageOffset, animated: false)
}
} else {
updateCurrentPage(page)

if !isScrolling && FolioReader.needsRTLChange {
page.scrollPageToBottom()
}
}

} else if isFirstLoad {
updateCurrentPage(page)
isFirstLoad = false
}

// Go to fragment if needed
if let fragmentID = tempFragment where fragmentID != "" && currentPage != nil {
if let fragmentID = tempFragment, let currentPage = currentPage where fragmentID != "" {
currentPage.handleAnchor(fragmentID, avoidBeginningAnchors: true, animated: true)
tempFragment = nil
}
Expand Down Expand Up @@ -1008,7 +1046,7 @@ extension FolioReaderCenter: FolioReaderChapterListDelegate {

// Move to #fragment
if let reference = tempReference {
if let fragmentID = reference.fragmentID where fragmentID != "" && currentPage != nil {
if let fragmentID = reference.fragmentID, let currentPage = currentPage where fragmentID != "" {
currentPage.handleAnchor(reference.fragmentID!, avoidBeginningAnchors: true, animated: true)
}
tempReference = nil
Expand Down
6 changes: 3 additions & 3 deletions Source/FolioReaderContainer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ class FolioReaderContainer: UIViewController {

// If user can change scroll direction use the last saved
if readerConfig.canChangeScrollDirection {
let direction = FolioReaderScrollDirection(rawValue: FolioReader.sharedInstance.currentScrollDirection) ?? .vertical
let direction = FolioReaderScrollDirection(rawValue: FolioReader.currentScrollDirection) ?? .vertical
readerConfig.scrollDirection = direction
}

Expand Down Expand Up @@ -92,7 +92,7 @@ class FolioReaderContainer: UIViewController {
self.errorOnLoad = true
}

FolioReader.sharedInstance.isReaderOpen = true
FolioReader.isReaderOpen = true

if !self.errorOnLoad {
// Reload data
Expand All @@ -105,7 +105,7 @@ class FolioReaderContainer: UIViewController {

self.centerViewController.reloadData()

FolioReader.sharedInstance.isReaderReady = true
FolioReader.isReaderReady = true
})
}
})
Expand Down