Пример #1
0
// Added by John to enable caller to get text from current visible page.
void ZLTextSelectionModel::text(std::string & text, int max)
{
    text.clear();
    ZLTextWordCursor start = myView.startCursor();
    ZLTextWordCursor end = myView.endCursor();
    ZLTextWordCursor cursor = start;
    while (cursor < end)
    {
        if (cursor.isEndOfParagraph())
        {
            cursor.nextParagraph();
            continue;
        }
        const ZLTextElement &element = cursor.element();
        switch (element.kind())
        {
        case ZLTextElement::WORD_ELEMENT:
            {
                const ZLTextWord &word = (const ZLTextWord&)element;
                if (cursor.sameElementAs(end))
                {
                    if (start.sameElementAs(end))
                    {
                        int skip = ZLUnicodeUtil::length(word.Data, start.charIndex());
                        int length = ZLUnicodeUtil::length(word.Data, end.charIndex()) - skip;
                        text.append(word.Data + skip, length);
                    }
                    else
                    {
                        text.append(word.Data, ZLUnicodeUtil::length(word.Data, end.charIndex()));
                    }
                }
                else if (cursor.charIndex() == 0)
                {
                    text.append(word.Data, word.Size);
                }
                else /* cursor == start */
                {
                    int skip = ZLUnicodeUtil::length(word.Data, cursor.charIndex());
                    text.append(word.Data + skip, word.Size - skip);
                }
            }
            break;
        case ZLTextElement::HSPACE_ELEMENT:
        case ZLTextElement::NB_HSPACE_ELEMENT:
            {
                text += ' ';
            }
            break;
        }
        cursor.nextWord();
        if (text.size() >= max)
        {
            return;
        }
    }
}
Пример #2
0
void ZLTextSelectionModel::createData() const {
	if (!myTextIsUpToDate && !isEmpty()) {
		Range r = internalRange();

		ZLTextWordCursor start = myArea.startCursor();
		start.moveToParagraph(r.first.ParagraphIndex);
		start.moveTo(r.first.ElementIndex, r.first.CharIndex);

		ZLTextWordCursor end = myArea.startCursor();
		end.moveToParagraph(r.second.ParagraphIndex);
		end.moveTo(r.second.ElementIndex, r.second.CharIndex);

		std::set<ZLTextParagraphCursorPtr> pcursors;
		pcursors.insert(start.paragraphCursorPtr());

		ZLTextWordCursor cursor = start;
		while (cursor < end) {
			if (cursor.isEndOfParagraph()) {
				cursor.nextParagraph();
				pcursors.insert(cursor.paragraphCursorPtr());
				myText.append(ZLibrary::EndOfLine);
				continue;
			}
			const ZLTextElement &element = cursor.element();
			switch (element.kind()) {
				case ZLTextElement::WORD_ELEMENT:
				{
					const ZLTextWord &word = (const ZLTextWord&)element;
					if (cursor.sameElementAs(end)) {
						if (start.sameElementAs(end)) {
							int skip = ZLUnicodeUtil::length(word.Data, start.charIndex());
							int length = ZLUnicodeUtil::length(word.Data, end.charIndex()) - skip;
							myText.append(word.Data + skip, length);
						} else {
							myText.append(word.Data, ZLUnicodeUtil::length(word.Data, end.charIndex()));
						}
					} else if (cursor.charIndex() == 0) {
						myText.append(word.Data, word.Size);
					} else /* cursor == start */ {
						int skip = ZLUnicodeUtil::length(word.Data, cursor.charIndex());
						myText.append(word.Data + skip, word.Size - skip);
					}
					break;
				}
				case ZLTextElement::IMAGE_ELEMENT:
					if (myImage.isNull()) {
						myImage = ((const ZLTextImageElement&)element).image();
					}
					break;
				case ZLTextElement::HSPACE_ELEMENT:
				case ZLTextElement::NB_HSPACE_ELEMENT:
					myText += ' ';
					break;
				default:
					break;
			}
			cursor.nextWord();
		}
		if ((cursor == end) && !cursor.isEndOfParagraph() && myImage.isNull()) {
			const ZLTextElement &element = cursor.element();
			if (element.kind() == ZLTextElement::IMAGE_ELEMENT) {
				myImage = ((const ZLTextImageElement&)element).image();
			}
		}

		myCursors.swap(pcursors);
		myTextIsUpToDate = true;
	}
}