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
/**
* 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;
}