Permalink
Cannot retrieve contributors at this time
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?
CSE3150_Project/DocumentParagraph.cpp
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
107 lines (91 sloc)
2.51 KB
This file contains 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
/** | |
* DocumentParagraph.cpp | |
* | |
* Author: Jamey Calabrese | |
*/ | |
#include "DocumentParagraph.h" | |
using namespace std; | |
// Public Methods | |
DocumentParagraph :: DocumentParagraph(int maxWidth, int maxHeight) | |
: maxWidth(maxWidth), maxHeight(maxHeight), leadingRow(0) | |
{ | |
dRows = shared_ptr<vector<shared_ptr<DocumentRow>>>(new vector<shared_ptr<DocumentRow>>()); | |
dRows->push_back(shared_ptr<DocumentRow>(new DocumentRow(maxWidth))); | |
} | |
DocumentParagraph :: DocumentParagraph(int maxWidth, int maxHeight, DocumentParagraph &dp) | |
: maxWidth(maxWidth), maxHeight(maxHeight), dRows(dp.dRows), leadingRow(dp.GetActualLineCount()), pNum(dp.pNum) | |
{ | |
} | |
DocumentParagraph :: ~DocumentParagraph() | |
{ | |
} | |
void DocumentParagraph :: SetParagraphNumber(int pNum) | |
{ | |
this->pNum = pNum; | |
} | |
int DocumentParagraph :: GetParagraphNumber() | |
{ | |
return pNum; | |
} | |
int DocumentParagraph :: GetActualLineCount() | |
{ | |
return dRows->size(); | |
} | |
int DocumentParagraph :: GetLineCount() | |
{ | |
int lessLeading = dRows->size() - leadingRow; | |
return lessLeading < maxHeight ? lessLeading : maxHeight; | |
} | |
int DocumentParagraph :: GetLineLength(int lineNum) | |
{ | |
return (*dRows)[lineNum+leadingRow]->GetLength(); | |
} | |
int DocumentParagraph :: GetRowFromGridCol(int &col) | |
{ | |
int curRow = 0; | |
int rowLength = GetLineLength(curRow); | |
while (col > rowLength) { | |
col -= rowLength+1; | |
rowLength = GetLineLength(++curRow); | |
} | |
return curRow+leadingRow; | |
} | |
int DocumentParagraph :: TranslateCol(int row, int col) | |
{ | |
int aRow = leadingRow + row; | |
int aCol = aRow + col; | |
for (int i = 0; i<aRow; ++i) { | |
aCol += GetLineLength(i-leadingRow); | |
} | |
return aCol; | |
} | |
string DocumentParagraph :: GetLine(int lineNumber) | |
{ | |
return (*dRows)[lineNumber+leadingRow]->GetString(); | |
} | |
string DocumentParagraph :: GetString() | |
{ | |
string str; | |
for (shared_ptr<DocumentRow> dr : *dRows) { | |
for (char c : dr->GetString()) { | |
str.push_back(c); | |
} | |
str.push_back(' '); | |
} | |
if (str.size()) { str.pop_back(); } | |
return str; | |
} | |
bool DocumentParagraph :: AppendWord(shared_ptr<string> word) | |
{ | |
if (!GetLineCount()) { dRows->push_back(shared_ptr<DocumentRow>(new DocumentRow(maxWidth))); } | |
bool wordFits = (*(dRows->rbegin()))->AppendWord(word); | |
bool newRowFits = false; | |
if (!wordFits) { | |
newRowFits = (GetLineCount()+1) <= maxHeight; | |
} | |
if (newRowFits) { | |
dRows->push_back(shared_ptr<DocumentRow>(new DocumentRow(maxWidth))); | |
wordFits = (*(dRows->rbegin()))->AppendWord(word); // Case where a single word overflows accross the length of an entire line will be an issue | |
} | |
return wordFits; | |
} |