void TextArea::_calculateSize() { if (_text.length() == 0 || _font == 0) return; _calculatedWidth = 0; _calculatedHeight = 0; // searching for maximum width for (std::vector<Surface *>::iterator it = textSurfaces()->begin(); it != textSurfaces()->end(); ++it) { if ((*it)->width() > _calculatedWidth) _calculatedWidth = (*it)->width(); } _calculatedHeight = textLines()->size() * _font->height() + (textLines()->size() - 1) * _font->verticalGap(); }
std::vector<Surface *> * TextArea::textSurfaces() { if (_textSurfaces != 0) return _textSurfaces; _textSurfaces = new std::vector<Surface *>; std::vector<std::string>::iterator it; for (it = textLines()->begin(); it != textLines()->end(); ++it) { unsigned int width = 0; for (unsigned int i = 0; i < (*it).length(); i++) { width += ((*it).c_str()[i] == 0x20) ? (_font->spaceWidth()) : (_font->glyph((*it).c_str()[i])->width() + _font->horizontalGap()); } _textSurfaces->push_back(new Surface(width, _font->height())); } return _textSurfaces; }
void LayoutTest::layoutToXml() const { QImage imgQt(mConfig.imagePath()); cv::Mat img = Image::qImage2Mat(imgQt); Timer dt; // find super pixels rdf::SuperPixel superPixel(img); if (!superPixel.compute()) qWarning() << "could not compute super pixel!"; QVector<QSharedPointer<Pixel> > sp = superPixel.getSuperPixels(); // find local orientation per pixel rdf::LocalOrientation lo(sp); if (!lo.compute()) qWarning() << "could not compute local orientation"; // smooth estimation rdf::GraphCutOrientation pse(sp); if (!pse.compute()) qWarning() << "could not compute set orientation"; //// find tab stops //rdf::TabStopAnalysis tabStops(sp); //if (!tabStops.compute()) // qWarning() << "could not compute text block segmentation!"; // find text lines rdf::TextLineSegmentation textLines(sp); if (!textLines.compute()) qWarning() << "could not compute text block segmentation!"; qInfo() << "algorithm computation time" << dt; // drawing cv::Mat rImg = img.clone(); // save super pixel image //rImg = superPixel.drawSuperPixels(rImg); //rImg = tabStops.draw(rImg); rImg = textLines.draw(rImg); QString dstPath = rdf::Utils::instance().createFilePath(mConfig.outputPath(), "-textlines"); rdf::Image::save(rImg, dstPath); qDebug() << "debug image saved: " << dstPath; // write XML ----------------------------------- QString loadXmlPath = rdf::PageXmlParser::imagePathToXmlPath(mConfig.imagePath()); rdf::PageXmlParser parser; parser.read(loadXmlPath); auto pe = parser.page(); pe->setCreator(QString("CVL")); pe->setImageSize(QSize(img.rows, img.cols)); pe->setImageFileName(QFileInfo(mConfig.imagePath()).fileName()); // start writing content auto ps = PixelSet::fromEdges(PixelSet::connect(sp)); if (!ps.empty()) { QSharedPointer<Region> textRegion = QSharedPointer<Region>(new Region()); textRegion->setType(Region::type_text_region); textRegion->setPolygon(ps[0]->convexHull()); for (auto tl : textLines.textLines()) { textRegion->addUniqueChild(tl); } pe->rootRegion()->addUniqueChild(textRegion); } parser.write(mConfig.xmlPath(), pe); qDebug() << "results written to" << mConfig.xmlPath(); }
void TextArea::draw() { if (!needRedraw()) return; InteractiveSurface::draw(); if (!_font) throw Exception("TextArea::draw() - font is not setted"); // if text is empty if (_text.length() == 0) { loadFromSurface(new Surface(0, 0, this->x(), this->y())); return; } unsigned int line = 0; for (std::vector<std::string>::iterator it = textLines()->begin(); it != textLines()->end(); ++it) { //draw characters on string surface unsigned int x = 0; for(unsigned int i = 0; i != (*it).size(); ++i) { unsigned char chr = (*it).at(i); if (chr == ' ') { x += _font->spaceWidth(); } else { Surface * glyph = _font->glyph(chr); glyph->setX(x); glyph->setY(0); glyph->copyTo(textSurfaces()->at(line)); x += glyph->width() + _font->horizontalGap(); if (i == (*it).size() - 1) x -= _font->horizontalGap(); } } line++; } // Creating resulting surface Surface * surface = new Surface(this->width(), this->height()); surface->setBackgroundColor(backgroundColor()); surface->clear(); // foreach lines surfaces unsigned int x = 0; unsigned int y = 0; for (std::vector<Surface *>::iterator it = textSurfaces()->begin(); it != textSurfaces()->end(); ++it) { switch(_horizontalAlign) { case HORIZONTAL_ALIGN_LEFT: break; case HORIZONTAL_ALIGN_CENTER: x = (this->width() -(*it)->width())/2; break; case HORIZONTAL_ALIGN_RIGHT: x = this->width() - (*it)->width(); break; case HORIZONTAL_ALIGN_JUSTIFY: //@todo justify break; } (*it)->setX(x); (*it)->setY(y); (*it)->copyTo(surface); y += _font->height() + _font->verticalGap(); } surface->setX(this->x()); surface->setY(this->y()); loadFromSurface(surface); // clear used memory while(!_textSurfaces->empty()) { delete _textSurfaces->back(); _textSurfaces->pop_back(); } delete _textSurfaces; _textSurfaces = 0; delete surface; }