Skip to content
Permalink
master
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Go to file
 
 
Cannot retrieve contributors at this time
#include "DocumentPage.h"
#include <iostream>
using namespace std;
DocumentPage :: DocumentPage(int maxWidth, int maxHeight)
: maxWidth(maxWidth),
maxHeight(maxHeight),
pageNum(0),
prevPage(__null),
resumeNextAppend(false)
{
}
DocumentPage :: DocumentPage(int maxWidth, int maxHeight, int pageNum)
: maxWidth(maxWidth),
maxHeight(maxHeight),
pageNum(pageNum),
prevPage(__null),
resumeNextAppend(false)
{
}
DocumentPage :: DocumentPage(DocumentPage &page)
: maxWidth(page.maxWidth),
maxHeight(page.maxHeight),
pageNum(page.pageNum+1),
prevPage(&page),
resumeNextAppend(false)
{
}
DocumentPage :: ~DocumentPage()
{
ClearPage();
}
void DocumentPage :: ClearPage()
{
dParas.clear();
}
string DocumentPage :: GetLine(int lineNum)
{
if (lineNum >= GetNumLines()) {
string str = "Attempted to GetLine at " + to_string(lineNum) + " Invalid Index!";
throw str;
}
auto itr = dParas.begin();
for (; lineNum >= (*itr)->GetLineCount(); ++itr) {
lineNum -= (*itr)->GetLineCount();
}
return (*itr)->GetLine(lineNum);
}
int DocumentPage :: GetLineLength(int lineNum)
{
if (lineNum >= GetNumLines()) {
string str = "Attempted to GetLineLength at " + to_string(lineNum) + "Invalid Index";
throw str;
}
auto itr = dParas.begin();
for (; lineNum >= (*itr)->GetLineCount(); ++itr) {
lineNum -= (*itr)->GetLineCount();
}
return (*itr)->GetLineLength(lineNum);
}
int DocumentPage :: GetNumLines()
{
int numLines = 0;
for (shared_ptr<DocumentParagraph> para : dParas) {
numLines += para->GetLineCount();
}
return numLines;
}
int DocumentPage :: GetNumParagraphs()
{
return dParas.size();
}
shared_ptr<DocumentParagraph> DocumentPage :: GetParagraph(int i)
{
return (i < GetNumParagraphs()) ? dParas[i] : __null;
}
shared_ptr<DocumentParagraph> DocumentPage :: GetParagraphContainingLine(int &lineNum)
{
shared_ptr<DocumentParagraph> para;
if (GetNumLines() > lineNum) {
auto paraItr = dParas.begin();
while (lineNum >= (*paraItr)->GetLineCount()) {
lineNum -= (*paraItr)->GetLineCount();
++paraItr;
}
para = *paraItr;
}
return para;
}
shared_ptr<DocumentParagraph> DocumentPage :: GetLastParagraph()
{
return GetParagraph(GetNumParagraphs()-1);
}
int DocumentPage :: GetPageNum()
{
return pageNum;
}
int DocumentPage :: AppendParagraph(vector<shared_ptr<string>> &splitString, int pNum)
{
if (GetNumLines() == maxHeight) { return 0; }
shared_ptr<DocumentParagraph> appendingParagraph;
if (resumeNextAppend) {
resumeNextAppend = false;
appendingParagraph = *(dParas.rbegin());
} else {
appendingParagraph = shared_ptr<DocumentParagraph>(new DocumentParagraph(maxWidth, maxHeight-GetNumLines()));
appendingParagraph->SetParagraphNumber(pNum);
dParas.push_back(appendingParagraph);
}
int numAppended = 0;
while (numAppended < splitString.size() && appendingParagraph->AppendWord(splitString[numAppended]))
{
++numAppended;
}
return numAppended;
}
void DocumentPage :: ContinueParagraph()
{
if (GetNumLines()) { throw string("Attempted to continue paragraph on a non-empty page!"); }
if (prevPage == __null) { throw string("DocumentPage must be initialized using a previous page in order to continue a paragraph"); }
shared_ptr<DocumentParagraph> continuedParagraph = prevPage->GetLastParagraph();
dParas.push_back(shared_ptr<DocumentParagraph>(new DocumentParagraph(maxWidth,maxHeight,*continuedParagraph)));
resumeNextAppend = true;
}