Пример #1
0
//sumatrapdf code
int extractText(miniexp_t item, Arraylist list, fz_bbox * target) {
    miniexp_t type = miniexp_car(item);

    if (!miniexp_symbolp(type))
        return 0;

    item = miniexp_cdr(item);

    if (!miniexp_numberp(miniexp_car(item))) return 0;
    int x0 = miniexp_to_int(miniexp_car(item)); item = miniexp_cdr(item);
    if (!miniexp_numberp(miniexp_car(item))) return 0;
    int y0 = miniexp_to_int(miniexp_car(item)); item = miniexp_cdr(item);
    if (!miniexp_numberp(miniexp_car(item))) return 0;
    int x1 = miniexp_to_int(miniexp_car(item)); item = miniexp_cdr(item);
    if (!miniexp_numberp(miniexp_car(item))) return 0;
    int y1 = miniexp_to_int(miniexp_car(item)); item = miniexp_cdr(item);
    //RectI rect = RectI::FromXY(x0, y0, x1, y1);
    fz_bbox rect = {x0 , y0 , x1 , y1};

    miniexp_t str = miniexp_car(item);

    if (miniexp_stringp(str) && !miniexp_cdr(item)) {
        fz_bbox inters = fz_intersect_bbox(rect, *target);
            //LOGI("Start text extraction: rectangle=[%d,%d,%d,%d] %s", rect.x0, rect.y0, rect.x1, rect.y1, content);
        if (!fz_is_empty_bbox(inters)) {
            const char *content = miniexp_to_str(str);

            while (*content) {
                arraylist_add(list, *content++);
            }

    //        if (value) {
    //            size_t len = str::Len(value);
    //            // TODO: split the rectangle into individual parts per glyph
    //            for (size_t i = 0; i < len; i++)
    //                coords.Append(RectI(rect.x, rect.y, rect.dx, rect.dy));
    //            extracted.AppendAndFree(value);
    //        }
            if (miniexp_symbol("word") == type) {
                arraylist_add(list, ' ');
                //coords.Append(RectI(rect.x + rect.dx, rect.y, 2, rect.dy));
            }
            else if (miniexp_symbol("char") != type) {
                arraylist_add(list, '\n');
    //            extracted.Append(lineSep);
    //            for (size_t i = 0; i < str::Len(lineSep); i++)
    //                coords.Append(RectI());
            }
        }
        item = miniexp_cdr(item);
    }

    while (miniexp_consp(str)) {
        extractText(str, list, target);
        item = miniexp_cdr(item);
        str = miniexp_car(item);
    }

    return !item;
}
Пример #2
0
Pvoid_t html_tokenize_into_features(const char * html, Pvoid_t features) {
	xmlSubstituteEntitiesDefault(0);
	htmlDocPtr doc = htmlParseDoc(BAD_CAST html, "UTF-8");

	if (doc) {
		Buffer *buf = extractText(doc);
		features = tokenize_text(buf->buf, buf->length, features);
		features = tokenize_uris(doc, features);
		free_buffer(buf);
		xmlFreeDoc(doc);
	}

	return features;
}
Пример #3
0
JNIEXPORT jstring JNICALL
Java_universe_constellation_orion_viewer_djvu_DjvuDocument_getText(JNIEnv *env, jobject thiz, int pageNumber,
		int startX, int startY, int width, int height)
{

    LOGI("==================Start Text Extraction==============");

    miniexp_t pagetext;
    while ((pagetext=ddjvu_document_get_pagetext(doc,pageNumber,0))==miniexp_dummy) {
      //handle_ddjvu_messages(ctx, TRUE);
    }

    if (miniexp_nil == pagetext) {
        return NULL;
    }
//
    ddjvu_status_t status;
    ddjvu_pageinfo_t info;
    while ((status = ddjvu_document_get_pageinfo(doc, pageNumber, &info)) < DDJVU_JOB_OK) {
        //nothing
    }

	LOGI("Extraction rectangle=[%d,%d,%d,%d]", startX, startY, width, height);

	Arraylist values = arraylist_create();

    int w = info.width;
    int h = info.height;
    fz_bbox target = {startX, h - startY - height, startX + width, h - startY};
    LOGI("Extraction irectangle=[%d,%d,%d,%d]", target.x0, target.y0, target.x1, target.y1);

	extractText(pagetext, values, &target);

	arraylist_add(values, 0);

    LOGI("Data: %s", arraylist_getData(values));
    jstring result = (*env)->NewStringUTF(env, arraylist_getData(values));
    arraylist_free(values);

    return result;
}
bool CodeCompletionsExtractor::next()
{
    const uint cxCodeCompleteResultCount = cxCodeCompleteResults->NumResults;

    ++cxCodeCompleteResultIndex;

    if (cxCodeCompleteResultIndex < cxCodeCompleteResultCount) {
        currentCxCodeCompleteResult = cxCodeCompleteResults->Results[cxCodeCompleteResultIndex];

        currentCodeCompletion_ = CodeCompletion();

        extractCompletionKind();
        extractText();
        extractPriority();
        extractAvailability();
        extractHasParameters();
        extractCompletionChunks();
        adaptPriority();

        return true;
    }

    return false;
}
Пример #5
0
bool KDReports::XmlParser::processNode( const QDomNode& node, KDReports::ReportBuilder* builder, bool inHeader, bool inFooter )
{
    // Loop over elements
    for ( QDomElement element = node.firstChildElement() ; !element.isNull();
          element = element.nextSiblingElement() ) {

        if ( testForErrorAndFillErrorDetails() )
            return false;

        const QString name = element.tagName();
        if( name == QLatin1String( "text" ) ) {
            // Handle <text> element
            KDReports::TextElement textElement;
            QString id;
            const QString text = extractText( element, &id, m_report->d->m_currentModel, m_report->d->m_currentRow );
            textElement.setText( text );
            textElement.setId( id );
            const QColor bgColor = KDReports::XmlHelper::readBackground( element );
            if( bgColor.isValid() )
                textElement.setBackground( bgColor );
            if( element.hasAttribute( QLatin1String( "pointsize" ) ) ) {
                const int pointSize = element.attribute( QLatin1String( "pointsize" ) ).toInt();
                if( pointSize )
                    textElement.setPointSize( pointSize );
            }
            if( element.hasAttribute( QLatin1String( "color" ) ) ) {
                const QString name = element.attribute( QLatin1String( "color" ) );
                textElement.setTextColor( QColor( name ) );
            }
            if( element.hasAttribute( QLatin1String( "font" ) ) ) {
                textElement.setFontFamily( element.attribute( QLatin1String( "font" ) ) );
            }
            if( element.hasAttribute( QLatin1String( "bold" ) ) ) {
                bool bold = false;
                if( element.attribute( QLatin1String( "bold" ) ) == QLatin1String( "true" ) )
                    bold = true;
                textElement.setBold( bold );
            }
            if( element.hasAttribute( QLatin1String( "italic" ) ) ) {
                bool italic = false;
                if( element.attribute( QLatin1String( "italic" ) ) == QLatin1String( "true" ) )
                    italic = true;
                textElement.setItalic( italic );
            }
            if( element.hasAttribute( QLatin1String( "strikeout" ) ) ) {
                bool strikeOut = false;
                if( element.attribute( QLatin1String( "strikeout" ) ) == QLatin1String( "true" ) )
                    strikeOut = true;
                textElement.setStrikeOut( strikeOut );
            }
            if( element.hasAttribute( QLatin1String( "underline" ) ) ) {
                bool underline = false;
                if( element.attribute( QLatin1String( "underline" ) ) == QLatin1String( "true" ) )
                    underline = true;
                textElement.setUnderline( underline );
            }

            const QString oldId = textElement.id();

            if ( m_xmlElementHandler && !m_xmlElementHandler->textElement( textElement, element ) )
                continue;

            if ( textElement.id() != oldId ) {
                // The handler modified the text element's id, look up the value again.
                const QHash<QString, QString>::const_iterator it = m_textValues.constFind( textElement.id() );
                if ( it != m_textValues.constEnd() ) {
                    textElement.setText( *it );
                }
            }

            if ( !builder ) {
                error( QObject::tr( "<text> is only supported in WordProcessing mode" ) );
            } else {
                if( element.hasAttribute( QLatin1String( "inline" ) ) ) {
                    builder->addInlineElementPublic( textElement );
                } else {
                    Qt::AlignmentFlag alignment = Qt::AlignLeft;
                    if( element.hasAttribute( QLatin1String( "alignment" ) ) )
                        alignment = KDReports::XmlHelper::stringToAlignment( element.attribute( QLatin1String( "alignment" ) ) );
                    const QColor bgColor = KDReports::XmlHelper::readColor( element, "paragraph-background" );
                    builder->addBlockElementPublic( textElement, alignment, bgColor );
                }
            }

        } else if( name == QLatin1String( "html" ) ) {
            // Handle <html> element
            KDReports::HtmlElement htmlElement;
            QString id;
            const QString text = extractText( element, &id );
            htmlElement.setHtml( text );
            htmlElement.setId( id );
            QColor bgColor = KDReports::XmlHelper::readBackground( element );
            if( bgColor.isValid() )
                htmlElement.setBackground( bgColor );

            const QString oldId = htmlElement.id();

            if ( m_xmlElementHandler && !m_xmlElementHandler->htmlElement( htmlElement, element ) )
                continue;

            if ( htmlElement.id() != oldId ) {
                // The handler modified the text element's id, look up the value again.
                const QHash<QString, QString>::const_iterator it = m_textValues.constFind( htmlElement.id() );
                if ( it != m_textValues.constEnd() ) {
                    htmlElement.setHtml( *it );
                }
            }

            addElement( htmlElement, builder, element );

        } else if ( name == QLatin1String( "tabs" ) ) {

            if ( !builder ) {
                error( QObject::tr( "<tabs> is only supported in WordProcessing mode" ) );
            } else {
                parseTabs( builder, element );
            }

        } else if ( name == QLatin1String( "paragraph-margins" ) ) {

            if ( !builder ) {
                error( QObject::tr( "<paragraph-margins> is only supported in WordProcessing mode" ) );
            } else {
                parseParagraphMargins( builder, element );
            }

        } else if( name == QLatin1String( "hr" ) ) {
            // Handle <hr> element
            KDReports::HtmlElement htmlElement;
            htmlElement.setHtml( QString::fromLatin1("<hr>") );
            if ( m_xmlElementHandler && !m_xmlElementHandler->htmlElement( htmlElement, element ) )
                continue;

            addElement( htmlElement, builder, element );

        } else if( name == QLatin1String( "vspace" ) ) {
            // Handle <vspace> element
            if( !element.hasAttribute( QLatin1String( "size" ) ) )
                continue; // size attribute is mandatory for VSpace
            int size = element.attribute( QLatin1String( "size" ) ).toInt();
            if ( !builder ) {
                error( QObject::tr( "<vspace> is only supported in WordProcessing mode" ) );
            } else {
                if( builder != m_report->d->builder() ) {
                    error( QObject::tr("<vspace> not allowed in headers, footers, or table cells") );
                    return false;
                }
            }
            XmlElementHandlerV2* v2 = dynamic_cast<XmlElementHandlerV2 *>(m_xmlElementHandler);
            if (v2 && !v2->vspace(size, element))
                continue;
            m_report->addVerticalSpacing( size );
        } else if( name == QLatin1String( "table" ) ) {
            // Handle <table> element
            const QString model = element.attribute( QLatin1String( "model" ) );
            if ( model.isEmpty() ) {
                if ( !builder ) {
                    error( QObject::tr( "<table> without a model is only supported in WordProcessing mode" ) );
                    continue;
                }
                KDReports::TableElement tableElement;
                const int headerRowCount = element.attribute( QLatin1String( "headerRowCount" ) ).toInt(); // default 0
                tableElement.setHeaderRowCount( headerRowCount );
                if ( element.hasAttribute( QLatin1String( "cellpadding" ) ) )
                    tableElement.setPadding( element.attribute( QLatin1String( "cellpadding" ) ).toInt() );
                parseCommonTableAttributes( tableElement, element );

                if ( m_xmlElementHandler && !m_xmlElementHandler->startTableElement( tableElement, element ) )
                    continue;

                if ( !parseTableContents( tableElement, element, *builder, inHeader, inFooter ) )
                    return false;

                if ( m_xmlElementHandler && !m_xmlElementHandler->endTableElement( tableElement, element ) )
                    continue;

                addElement( tableElement, builder, element );

            } else {
                KDReports::AutoTableElement tableElement( model );
                if( element.attribute( QLatin1String( "verticalHeaderVisible" ) ) == QLatin1String( "false" ) )
                    tableElement.setVerticalHeaderVisible(false);
                if( element.attribute( QLatin1String( "horizontalHeaderVisible" ) ) == QLatin1String( "false" ) )
                    tableElement.setHorizontalHeaderVisible(false);
                QColor headerBgColor = KDReports::XmlHelper::readColor( element, "header-background" );
                if ( headerBgColor.isValid() )
                    tableElement.setHeaderBackground( headerBgColor );
                parseCommonTableAttributes( tableElement, element );
                if ( m_xmlElementHandler && !m_xmlElementHandler->autoTableElement( tableElement, element ) )
                    continue;

                if ( m_report->reportMode() == Report::SpreadSheet ) {
                    m_report->mainTable()->setAutoTableElement( tableElement );
                } else {
                    addElement( tableElement, builder, element );
                }
            }
        } else if( name == QLatin1String( "chart" ) ) {
            // Handle <chart> element

            KDReports::ChartElement chartElement( element.attribute( QLatin1String( "model" ) ) );
            QColor bgColor = KDReports::XmlHelper::readBackground( element );
            if( bgColor.isValid() )
                chartElement.setBackground( bgColor );
            int width = 100, height = 100;
            Unit unit = Millimeters;
            if( element.hasAttribute( QLatin1String( "width" ) ) ) {
                QString str = element.attribute( QLatin1String( "width" ) );
                if ( str.endsWith( QLatin1Char('%') ) ) {
                    str.chop( 1 );
                    unit = Percent;
                }
                width = str.toInt();
            }
            if( element.hasAttribute( QLatin1String( "height" ) ) ) {
                QString str = element.attribute( QLatin1String( "height" ) );
                if ( str.endsWith( QLatin1Char('%') ) ) {
                    str.chop( 1 );
                    unit = Percent;
                }
                height = str.toInt();
            }
            chartElement.setSize( width, height, unit );

            if ( m_xmlElementHandler && !m_xmlElementHandler->chartElement( chartElement, element ) )
                continue;

            addElement( chartElement, builder, element );

        } else if( name == QLatin1String( "image" ) ) {
            // Handle <image> element

            QString id;
            QImage image = extractImage( element, &id );
            KDReports::ImageElement imageElement( image );
            imageElement.setId(id);
            if( element.hasAttribute( QLatin1String( "width" ) ) ) {
                QString widthStr = element.attribute( QLatin1String( "width" ) );
                if ( widthStr.endsWith( QLatin1Char('%') ) ) {
                    widthStr.truncate( widthStr.length() - 1 );
                    imageElement.setWidth( widthStr.toInt(), KDReports::Percent );
                } else {
                    imageElement.setWidth( widthStr.toInt() );
                }
            } else if( element.hasAttribute( QLatin1String( "height" ) ) ) { // mutually exclusive with "width"!
                QString heightStr = element.attribute( QLatin1String( "height" ) );
                if ( heightStr.endsWith( QLatin1Char('%') ) ) {
                    heightStr.truncate( heightStr.length() - 1 );
                    imageElement.setHeight( heightStr.toInt(), KDReports::Percent );
                } else {
                    imageElement.setHeight( heightStr.toInt() );
                }
            } else if ( element.hasAttribute( QLatin1String( "fitToPage" ) ) ) {
                imageElement.setFitToPage();
            }

            if ( m_xmlElementHandler && !m_xmlElementHandler->imageElement( imageElement, element ) )
                continue;

            addElement( imageElement, builder, element );

        } else if( name == QLatin1String( "header" ) ) {
            const KDReports::HeaderLocations loc = KDReports::XmlHelper::parseHeaderLocation( element.attribute( QLatin1String( "location" ) ) );
            KDReports::Header& header = m_report->header( loc );
            parseHeaderFooterAttribute( header, element );
            if ( m_xmlElementHandler && !m_xmlElementHandler->startHeader( header, element ) )
                continue;
            if( !processNode( element, &header.d->m_builder, true, false ) )
                return false;
            if ( m_xmlElementHandler )
                m_xmlElementHandler->endHeader( header, element );
        } else if( name == QLatin1String( "footer" ) ) {
            const KDReports::HeaderLocations loc = KDReports::XmlHelper::parseHeaderLocation( element.attribute( QLatin1String( "location" ) ) );
            KDReports::Footer& footer = m_report->footer( loc );
            parseHeaderFooterAttribute( footer, element );
            if ( m_xmlElementHandler && !m_xmlElementHandler->startFooter( footer, element ) )
                continue;
            if( !processNode( element, &footer.d->m_builder, false, true ) )
                return false;
            if ( m_xmlElementHandler )
                m_xmlElementHandler->endFooter( footer, element );
        } else if( name == QLatin1String( "variable" ) ) {
            if( !inHeader && !inFooter ) {
                error( QObject::tr("<variable> tags only allowed in headers and footers") );
                return false;
            }
            if( !element.hasAttribute( QLatin1String( "type" ) ) ) {
                error( QObject::tr("<variable> tags must have a 'type' attribute") );
                return false;
            }
            Q_ASSERT( builder );
            if ( builder ) {
                const QString type = element.attribute( QLatin1String( "type" ) );
                KDReports::VariableType vt = KDReports::XmlHelper::stringToVariableType( type );
                XmlElementHandlerV2* v2 = dynamic_cast<XmlElementHandlerV2 *>(m_xmlElementHandler);
                if (v2  && !v2->variable(vt, element))
                    continue;
                builder->addVariablePublic( vt );
            }
        } else if ( name == QLatin1String( "page-break" ) ) {
            if ( m_xmlElementHandler && !m_xmlElementHandler->pageBreak( element ) )
                continue;
            m_report->addPageBreak();
        } else if ( name == QLatin1String( "ifdef" ) ) {
            if ( element.hasAttribute( QLatin1String( "id" ) ) ) {
                const QString id = element.attribute( QLatin1String( "id" ) );
                const bool skip = m_textValues.value( id ).isEmpty();
                if( !skip ) {
                    if ( !processNode( element, builder, inHeader, inFooter ) )
                        return false;
                }
            }
        } else if ( name == QLatin1String( "custom" ) ) {
            if ( m_xmlElementHandler )
                m_xmlElementHandler->customElement( element );
        } else if ( name == QLatin1String( "hline" ) ) {
            KDReports::HLineElement hLineElement;

            if ( element.hasAttribute( QLatin1String( "thickness" ) ) ) {
                const double thickness = element.attribute( QLatin1String( "thickness" ) ).toDouble();
                hLineElement.setThickness( thickness );
            }

            if ( element.hasAttribute( QLatin1String( "color" ) ) ) {
                const QColor color = KDReports::XmlHelper::readColor( element, "color" );
                hLineElement.setColor( color );
            }

            if ( element.hasAttribute( QLatin1String( "margin" ) ) ) {
                const int margin = element.attribute( QLatin1String( "margin" ) ).toInt();
                hLineElement.setMargin( margin );
            }

#ifdef KDREPORTS_ALLOW_BINARY_INCOMPATIBILITY
            if ( m_xmlElementHandler && !m_xmlElementHandler->hLineElement( hLineElement, element ) )
               continue;
#else
            XmlElementHandlerV2* v2 = dynamic_cast<XmlElementHandlerV2 *>(m_xmlElementHandler);
            if (v2  && !v2->hLineElement(hLineElement, element))
                continue;
#endif

            addElement( hLineElement, builder, element );
        } else {
            error( QObject::tr( "KDReports::Report::loadFromXML(): Unknown element %1" ).arg( name ) );
        }
    }

    if ( testForErrorAndFillErrorDetails() )
        return false;

    return true;
}