add some const, will make the code easier to read later on

remotes/origin/textfind-and-transparency
Albert Astals Cid 15 years ago
parent 7dbc4f48e1
commit 2bec8e3aaa
  1. 213
      core/textpage.cpp

@ -372,8 +372,8 @@ RegularAreaRect * TextPage::textArea ( TextSelection * sel) const
} }
} }
#else #else
double scaleX = d->m_page->m_page->width(); const double scaleX = d->m_page->m_page->width();
double scaleY = d->m_page->m_page->height(); const double scaleY = d->m_page->m_page->height();
NormalizedPoint startC = sel->start(); NormalizedPoint startC = sel->start();
NormalizedPoint endC = sel->end(); NormalizedPoint endC = sel->end();
@ -388,11 +388,12 @@ RegularAreaRect * TextPage::textArea ( TextSelection * sel) const
} }
// minX,maxX,minY,maxY gives the bounding rectangle coordinates of the document // minX,maxX,minY,maxY gives the bounding rectangle coordinates of the document
NormalizedRect boundingRect = d->m_page->m_page->boundingBox(); const NormalizedRect boundingRect = d->m_page->m_page->boundingBox();
QRect content = boundingRect.geometry(scaleX,scaleY); const QRect content = boundingRect.geometry(scaleX,scaleY);
double minX, maxX, minY, maxY; const double minX = content.left();
minX = content.left(), maxX = content.right(); const double maxX = content.right();
minY = content.top(), maxY = content.bottom(); const double minY = content.top();
const double maxY = content.bottom();
/** /**
* We will now find out the TinyTextEntity for the startRectangle and TinyTextEntity for * We will now find out the TinyTextEntity for the startRectangle and TinyTextEntity for
@ -426,10 +427,8 @@ RegularAreaRect * TextPage::textArea ( TextSelection * sel) const
*/ */
// we know that startC.x > endC.x, we need to decide which is top and which is bottom // we know that startC.x > endC.x, we need to decide which is top and which is bottom
NormalizedRect start_end; const NormalizedRect start_end = (startC.y < endC.y) ? NormalizedRect(startC.x, startC.y, endC.x, endC.y)
if(startC.y < endC.y) : NormalizedRect(startC.x, endC.y, endC.x, startC.y);
start_end = NormalizedRect(startC.x, startC.y, endC.x, endC.y);
else start_end = NormalizedRect(startC.x, endC.y, endC.x, startC.y);
// Case 1(a) // Case 1(a)
if(!boundingRect.intersects(start_end)) return ret; if(!boundingRect.intersects(start_end)) return ret;
@ -1043,19 +1042,13 @@ static bool doesConsumeX(const QRect& first, const QRect& second, int threshold)
// or if there is overlap of space by more than threshold% // or if there is overlap of space by more than threshold%
// there is overlap // there is overlap
int overlap;
if(second.right() >= first.left() && first.right() >= second.left()) if(second.right() >= first.left() && first.right() >= second.left())
{ {
int percentage; const int overlap = (second.right() >= first.right()) ? first.right() - second.left()
if(second.right() >= first.right()) overlap = first.right() - second.left(); : second.right() - first.left();
else overlap = second.right() - first.left();
// we will divide by the smaller rectangle to calculate the overlap // we will divide by the smaller rectangle to calculate the overlap
if( first.width() < second.width()) const int percentage = (first.width() < second.width()) ? overlap * 100 / (first.right() - first.left())
percentage = overlap * 100 / (first.right() - first.left()); : overlap * 100 / (second.right() - second.left());
else
percentage = overlap * 100 / (second.right() - second.left());
if(percentage >= threshold) return true; if(percentage >= threshold) return true;
} }
@ -1076,18 +1069,13 @@ static bool doesConsumeY(const QRect& first, const QRect& second, int threshold)
// or if there is overlap of space by more than 80% // or if there is overlap of space by more than 80%
// there is overlap // there is overlap
int overlap;
if(second.bottom() >= first.top() && first.bottom() >= second.top()) if(second.bottom() >= first.top() && first.bottom() >= second.top())
{ {
int percentage; const int overlap = (second.bottom() >= first.bottom()) ? first.bottom() - second.top()
if(second.bottom() >= first.bottom()) overlap = first.bottom() - second.top(); : second.bottom() - first.top();
else overlap = second.bottom() - first.top();
//we will divide by the smaller rectangle to calculate the overlap //we will divide by the smaller rectangle to calculate the overlap
if( first.width() < second.width()) const int percentage = (first.width() < second.width()) ? overlap * 100 / (first.bottom() - first.top())
percentage = overlap * 100 / (first.bottom() - first.top()); : overlap * 100 / (second.bottom() - second.top());
else
percentage = overlap * 100 / (second.bottom() - second.top());
if(percentage >= threshold) return true; if(percentage >= threshold) return true;
} }
@ -1102,7 +1090,7 @@ static bool doesConsumeY(const QRect& first, const QRect& second, int threshold)
void TextPagePrivate::removeSpace() void TextPagePrivate::removeSpace()
{ {
TextList::Iterator it = m_words.begin(), itEnd = m_words.end(); TextList::Iterator it = m_words.begin(), itEnd = m_words.end();
QString str(' '); const QString str(' ');
it = m_words.begin(), itEnd = m_words.end(); it = m_words.begin(), itEnd = m_words.end();
for( ; it != itEnd ; it++) for( ; it != itEnd ; it++)
@ -1145,7 +1133,8 @@ void TextPagePrivate::makeWordFromCharacters()
TextList::Iterator it = tmpList.begin(), itEnd = tmpList.end(), tmpIt; TextList::Iterator it = tmpList.begin(), itEnd = tmpList.end(), tmpIt;
int newLeft,newRight,newTop,newBottom; int newLeft,newRight,newTop,newBottom;
int pageWidth = m_page->m_page->width(), pageHeight = m_page->m_page->height(); const int pageWidth = m_page->m_page->width();
const int pageHeight = m_page->m_page->height();
int index = 0; int index = 0;
for( ; it != itEnd ; it++) for( ; it != itEnd ; it++)
@ -1192,13 +1181,13 @@ void TextPagePrivate::makeWordFromCharacters()
break; break;
} }
int text_y1 = elementArea.top() , const int text_y1 = elementArea.top() ,
text_x1 = elementArea.left(), text_x1 = elementArea.left(),
text_y2 = elementArea.y() + elementArea.height(), text_y2 = elementArea.y() + elementArea.height(),
text_x2 = elementArea.x() + elementArea.width(); text_x2 = elementArea.x() + elementArea.width();
int line_y1 = lineArea.top() ,line_x1 = lineArea.left(), const int line_y1 = lineArea.top() ,line_x1 = lineArea.left(),
line_y2 = lineArea.y() + lineArea.height(), line_y2 = lineArea.y() + lineArea.height(),
line_x2 = lineArea.x() + lineArea.width(); line_x2 = lineArea.x() + lineArea.width();
space = elementArea.left() - lineArea.right(); space = elementArea.left() - lineArea.right();
@ -1224,13 +1213,13 @@ void TextPagePrivate::makeWordFromCharacters()
// if newString is not empty, save it // if newString is not empty, save it
if(newString.length()) if(newString.length())
{ {
NormalizedRect newRect(lineArea,pageWidth,pageHeight); const NormalizedRect newRect(lineArea,pageWidth,pageHeight);
newList.append(new TinyTextEntity(newString.normalized newList.append(new TinyTextEntity(newString.normalized
(QString::NormalizationForm_KC), newRect )); (QString::NormalizationForm_KC), newRect ));
QRect rect = newRect.geometry(pageWidth,pageHeight); const QRect rect = newRect.geometry(pageWidth,pageHeight);
RegionText regionWord(word,rect); const RegionText regionWord(word,rect);
int keyRect = rect.left() * rect.top() const int keyRect = rect.left() * rect.top()
+ rect.right() * rect.bottom(); + rect.right() * rect.bottom();
// there may be more than one element in the same key // there may be more than one element in the same key
m_word_chars_map.insertMulti(keyRect,regionWord); m_word_chars_map.insertMulti(keyRect,regionWord);
@ -1281,12 +1270,13 @@ void TextPagePrivate::makeAndSortLines(const TextList &wordsTmp,
TextList::Iterator it = words.begin(), itEnd = words.end(); TextList::Iterator it = words.begin(), itEnd = words.end();
int i = 0; int i = 0;
int newLeft,newRight,newTop,newBottom; int newLeft,newRight,newTop,newBottom;
int pageWidth = m_page->m_page->width(), pageHeight = m_page->m_page->height(); const int pageWidth = m_page->m_page->width();
const int pageHeight = m_page->m_page->height();
//for every non-space texts(characters/words) in the textList //for every non-space texts(characters/words) in the textList
for( ; it != itEnd ; it++) for( ; it != itEnd ; it++)
{ {
QRect elementArea = (*it)->area.roundedGeometry(pageWidth,pageHeight); const QRect elementArea = (*it)->area.roundedGeometry(pageWidth,pageHeight);
bool found = false; bool found = false;
for( i = 0 ; i < lines.length() ; i++) for( i = 0 ; i < lines.length() ; i++)
@ -1295,15 +1285,15 @@ void TextPagePrivate::makeAndSortLines(const TextList &wordsTmp,
line_rects is only necessary to preserve the topmin and bottommax of all line_rects is only necessary to preserve the topmin and bottommax of all
the texts in the line, left and right is not necessary at all the texts in the line, left and right is not necessary at all
*/ */
QRect lineArea = line_rects.at(i); const QRect lineArea = line_rects.at(i);
int text_y1 = elementArea.top() , const int text_y1 = elementArea.top() ,
text_y2 = elementArea.top() + elementArea.height() , text_y2 = elementArea.top() + elementArea.height() ,
text_x1 = elementArea.left(), text_x1 = elementArea.left(),
text_x2 = elementArea.left() + elementArea.width(); text_x2 = elementArea.left() + elementArea.width();
int line_y1 = lineArea.top() , const int line_y1 = lineArea.top() ,
line_y2 = lineArea.top() + lineArea.height(), line_y2 = lineArea.top() + lineArea.height(),
line_x1 = lineArea.left(), line_x1 = lineArea.left(),
line_x2 = lineArea.left() + lineArea.width(); line_x2 = lineArea.left() + lineArea.width();
/* /*
if the new text and the line has y overlapping parts of more than 70%, if the new text and the line has y overlapping parts of more than 70%,
@ -1368,10 +1358,10 @@ void TextPagePrivate::calculateStatisticalInformation(const SortedTextList &line
QMap<int,int> line_space_stat; QMap<int,int> line_space_stat;
for(int i = 0 ; i < line_rects.length(); i++) for(int i = 0 ; i < line_rects.length(); i++)
{ {
QRect rectUpper = line_rects.at(i); const QRect rectUpper = line_rects.at(i);
if(i+1 == line_rects.length()) break; if(i+1 == line_rects.length()) break;
QRect rectLower = line_rects.at(i+1); const QRect rectLower = line_rects.at(i+1);
int linespace = rectLower.top() - (rectUpper.top() + rectUpper.height()); int linespace = rectLower.top() - (rectUpper.top() + rectUpper.height());
if(linespace < 0) linespace =-linespace; if(linespace < 0) linespace =-linespace;
@ -1422,10 +1412,10 @@ void TextPagePrivate::calculateStatisticalInformation(const SortedTextList &line
// for every line // for every line
for( ; it != itEnd ; it++ ) for( ; it != itEnd ; it++ )
{ {
QRect area1 = (*it)->area.roundedGeometry(pageWidth,pageHeight); const QRect area1 = (*it)->area.roundedGeometry(pageWidth,pageHeight);
if( it+1 == itEnd ) break; if( it+1 == itEnd ) break;
QRect area2 = (*(it+1))->area.roundedGeometry(pageWidth,pageHeight); const QRect area2 = (*(it+1))->area.roundedGeometry(pageWidth,pageHeight);
int space = area2.left() - area1.right(); int space = area2.left() - area1.right();
if(space > maxSpace) if(space > maxSpace)
@ -1475,14 +1465,14 @@ void TextPagePrivate::calculateStatisticalInformation(const SortedTextList &line
else col_space_stat[maxSpace] = 1; else col_space_stat[maxSpace] = 1;
//store the max rect of each line //store the max rect of each line
int left,right,top,bottom; const int left = max_area1.right();
left = max_area1.right(); const int right = max_area2.left();
right = max_area2.left(); const int top = (max_area1.top() > max_area2.top()) ? max_area2.top() :
max_area1.top();
max_area1.top() > max_area2.top() ? top = max_area2.top() : top = max_area1.top(); const int bottom = (max_area1.bottom() < max_area2.bottom()) ? max_area2.bottom() :
max_area1.bottom() < max_area2.bottom() ? bottom = max_area2.bottom() : bottom = max_area1.bottom(); max_area1.bottom();
QRect rect(left,top,right-left,bottom-top); const QRect rect(left,top,right-left,bottom-top);
max_hor_space_rects.append(rect); max_hor_space_rects.append(rect);
} }
else max_hor_space_rects.append(QRect(0,0,0,0)); else max_hor_space_rects.append(QRect(0,0,0,0));
@ -1522,7 +1512,8 @@ void TextPagePrivate::calculateStatisticalInformation(const SortedTextList &line
*/ */
void TextPagePrivate::XYCutForBoundingBoxes(int tcx, int tcy) void TextPagePrivate::XYCutForBoundingBoxes(int tcx, int tcy)
{ {
int pageWidth = m_page->m_page->width(), pageHeight = m_page->m_page->height(); const int pageWidth = m_page->m_page->width();
const int pageHeight = m_page->m_page->height();
// proj_on_yaxis will start from 0(rect.left()) to N(rect.right) // proj_on_yaxis will start from 0(rect.left()) to N(rect.right)
int proj_on_yaxis[5000], proj_on_xaxis[5000]; int proj_on_yaxis[5000], proj_on_xaxis[5000];
@ -1531,7 +1522,7 @@ void TextPagePrivate::XYCutForBoundingBoxes(int tcx, int tcy)
QRect contentRect(m_page->m_page->boundingBox().geometry(pageWidth,pageHeight)); QRect contentRect(m_page->m_page->boundingBox().geometry(pageWidth,pageHeight));
TextList words; TextList words;
copyToList(words); copyToList(words);
RegionText root(words,contentRect); const RegionText root(words,contentRect);
// start the tree with the root, it is our only region at the start // start the tree with the root, it is our only region at the start
tree.push_back(root); tree.push_back(root);
@ -1542,7 +1533,7 @@ void TextPagePrivate::XYCutForBoundingBoxes(int tcx, int tcy)
// while traversing the tree has not been ended // while traversing the tree has not been ended
while(i < tree.length()) while(i < tree.length())
{ {
RegionText node = tree.at(i); const RegionText node = tree.at(i);
QRect regionRect = node.area(); QRect regionRect = node.area();
/** /**
@ -1699,27 +1690,26 @@ void TextPagePrivate::XYCutForBoundingBoxes(int tcx, int tcy)
bool cut_hor = false, cut_ver = false; bool cut_hor = false, cut_ver = false;
// For horizontal cut // For horizontal cut
int topHeight = cut_pos_y - (regionRect.top() - old_top); const int topHeight = cut_pos_y - (regionRect.top() - old_top);
QRect topRect(regionRect.left(), const QRect topRect(regionRect.left(),
regionRect.top(), regionRect.top(),
regionRect.width(), regionRect.width(),
topHeight); topHeight);
QRect bottomRect(regionRect.left(), const QRect bottomRect(regionRect.left(),
regionRect.top() + topHeight, regionRect.top() + topHeight,
regionRect.width(), regionRect.width(),
regionRect.height() - topHeight ); regionRect.height() - topHeight );
// For vertical Cut // For vertical Cut
int leftWidth = cut_pos_x - (regionRect.left() - old_left); const int leftWidth = cut_pos_x - (regionRect.left() - old_left);
const QRect leftRect(regionRect.left(),
QRect leftRect(regionRect.left(), regionRect.top(),
regionRect.top(), leftWidth,
leftWidth, regionRect.height());
regionRect.height()); const QRect rightRect(regionRect.left() + leftWidth,
QRect rightRect(regionRect.left() + leftWidth, regionRect.top(),
regionRect.top(), regionRect.width() - leftWidth,
regionRect.width() - leftWidth, regionRect.height());
regionRect.height());
if(gap_y >= gap_x && gap_y >= tcy) if(gap_y >= gap_x && gap_y >= tcy)
cut_hor = true; cut_hor = true;
@ -1823,7 +1813,8 @@ void TextPagePrivate::addNecessarySpace()
RegionTextList tree = m_XY_cut_tree; RegionTextList tree = m_XY_cut_tree;
int i,j,k; int i,j,k;
int pageWidth = m_page->m_page->width(), pageHeight = m_page->m_page->height(); const int pageWidth = m_page->m_page->width();
const int pageHeight = m_page->m_page->height();
// Only change the texts under RegionTexts, not the area // Only change the texts under RegionTexts, not the area
for(j = 0 ; j < tree.length() ; j++) for(j = 0 ; j < tree.length() ; j++)
@ -1842,25 +1833,23 @@ void TextPagePrivate::addNecessarySpace()
TextList list = lines.at(i); TextList list = lines.at(i);
for( k = 0 ; k < list.length() ; k++ ) for( k = 0 ; k < list.length() ; k++ )
{ {
QRect area1 = list.at(k)->area.roundedGeometry(pageWidth,pageHeight); const QRect area1 = list.at(k)->area.roundedGeometry(pageWidth,pageHeight);
if( k+1 >= list.length() ) break; if( k+1 >= list.length() ) break;
QRect area2 = list.at(k+1)->area.roundedGeometry(pageWidth,pageHeight); const QRect area2 = list.at(k+1)->area.roundedGeometry(pageWidth,pageHeight);
int space = area2.left() - area1.right(); const int space = area2.left() - area1.right();
if(space != 0) if(space != 0)
{ {
// Make a TinyTextEntity of string space and push it between it and it+1 // Make a TinyTextEntity of string space and push it between it and it+1
int left,right,top,bottom; const int left = area1.right();
const int right = area2.left();
left = area1.right(); const int top = area2.top() < area1.top() ? area2.top() : area1.top();
right = area2.left(); const int bottom = area2.bottom() > area1.bottom() ? area2.bottom() : area1.bottom();
top = area2.top() < area1.top() ? area2.top() : area1.top();
bottom = area2.bottom() > area1.bottom() ? area2.bottom() : area1.bottom(); const QString spaceStr(" ");
const QRect rect(QPoint(left,top),QPoint(right,bottom));
QString spaceStr(" "); const NormalizedRect entRect(rect,pageWidth,pageHeight);
QRect rect(QPoint(left,top),QPoint(right,bottom));
NormalizedRect entRect(rect,pageWidth,pageHeight);
TinyTextEntity *ent = new TinyTextEntity(spaceStr,entRect); TinyTextEntity *ent = new TinyTextEntity(spaceStr,entRect);
list.insert(k+1,ent); list.insert(k+1,ent);
@ -1909,28 +1898,28 @@ void TextPagePrivate::addNecessarySpace()
*/ */
void TextPagePrivate::breakWordIntoCharacters() void TextPagePrivate::breakWordIntoCharacters()
{ {
QString spaceStr(" "); const QString spaceStr(" ");
TextList tmp; TextList tmp;
int count = 0, i; const int pageWidth = m_page->m_page->width();
int pageWidth = m_page->m_page->width(), pageHeight = m_page->m_page->height(); const int pageHeight = m_page->m_page->height();
for(i = 0 ; i < m_words.length() ; i++) for(int i = 0 ; i < m_words.length() ; i++)
{ {
TinyTextEntity *ent = m_words.at(i); TinyTextEntity *ent = m_words.at(i);
QRect rect = ent->area.geometry(pageWidth,pageHeight); const QRect rect = ent->area.geometry(pageWidth,pageHeight);
// the spaces contains only one character, so we can skip them // the spaces contains only one character, so we can skip them
if(ent->text() == spaceStr) if(ent->text() == spaceStr)
tmp.append(ent); tmp.append(ent);
else else
{ {
int key = rect.left() * rect.top() const int key = rect.left() * rect.top()
+ rect.right() * rect.bottom(); + rect.right() * rect.bottom();
RegionText word_text = m_word_chars_map.value(key); RegionText word_text = m_word_chars_map.value(key);
TextList list = word_text.text(); TextList list = word_text.text();
count = m_word_chars_map.count(key); const int count = m_word_chars_map.count(key);
if(count > 1) if(count > 1)
{ {
QMap<int, RegionText>::iterator it = m_word_chars_map.find(key); QMap<int, RegionText>::iterator it = m_word_chars_map.find(key);
@ -1940,7 +1929,7 @@ void TextPagePrivate::breakWordIntoCharacters()
it++; it++;
list = word_text.text(); list = word_text.text();
QRect regionRect = word_text.area(); const QRect regionRect = word_text.area();
if(regionRect.left() == rect.left() && regionRect.top() == rect.top()) if(regionRect.left() == rect.left() && regionRect.top() == rect.top())
break; break;

Loading…
Cancel
Save