| 1 |
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
| 2 |
/* |
| 3 |
* This file is part of the libqxp project. |
| 4 |
* |
| 5 |
* This Source Code Form is subject to the terms of the Mozilla Public |
| 6 |
* License, v. 2.0. If a copy of the MPL was not distributed with this |
| 7 |
* file, You can obtain one at http://mozilla.org/MPL/2.0/. |
| 8 |
*/ |
| 9 |
|
| 10 |
#include "QXPTextParser.h" |
| 11 |
|
| 12 |
#include "QXPHeader.h" |
| 13 |
#include "QXPTypes.h" |
| 14 |
|
| 15 |
namespace libqxp |
| 16 |
{ |
| 17 |
|
| 18 |
using std::make_shared; |
| 19 |
|
| 20 |
namespace |
| 21 |
{ |
| 22 |
|
| 23 |
template <class Format, class FormatSpec> |
| 24 |
void parseFormatSpec( |
| 25 |
const std::shared_ptr<librevenge::RVNGInputStream> &infoStream, |
| 26 |
const std::vector<std::shared_ptr<Format>> &formats, |
| 27 |
unsigned version, bool be, |
| 28 |
std::vector<FormatSpec> &textFormats) |
| 29 |
{ |
| 30 |
unsigned specLength = readU32(infoStream, be); |
| 31 |
if (specLength > getRemainingLength(infoStream)) |
| 32 |
specLength = unsigned(getRemainingLength(infoStream)); |
| 33 |
const long end = infoStream->tell() + specLength; |
| 34 |
while (infoStream->tell() < end) |
| 35 |
{ |
| 36 |
const unsigned formatIndex = version >= QXP_4 ? readU32(infoStream, be) : readU16(infoStream, be); |
| 37 |
const unsigned length = readU32(infoStream, be); |
| 38 |
const unsigned startIndex = textFormats.empty() ? 0 : textFormats.back().afterEndIndex(); |
| 39 |
|
| 40 |
std::shared_ptr<Format> format; |
| 41 |
if (formatIndex >= formats.size()) |
| 42 |
{ |
| 43 |
QXP_DEBUG_MSG(("Char format %u not found\n", formatIndex)); |
| 44 |
format = formats.empty() ? std::make_shared<Format>() : formats[0]; |
| 45 |
} |
| 46 |
else |
| 47 |
{ |
| 48 |
format = formats[formatIndex]; |
| 49 |
} |
| 50 |
textFormats.push_back(FormatSpec(format, startIndex, length)); |
| 51 |
} |
| 52 |
} |
| 53 |
|
| 54 |
} |
| 55 |
|
| 56 |
QXPTextParser::QXPTextParser(const std::shared_ptr<librevenge::RVNGInputStream> &input, const std::shared_ptr<QXPHeader> &header) |
| 57 |
: m_header(header) |
| 58 |
, be(header->isBigEndian()) |
| 59 |
, m_encoding(header->encoding()) |
| 60 |
, m_blockParser(input, header) |
| 61 |
{ |
| 62 |
} |
| 63 |
|
| 64 |
std::shared_ptr<Text> QXPTextParser::parseText(unsigned index, |
| 65 |
const std::vector<std::shared_ptr<CharFormat>> &charFormats, |
| 66 |
const std::vector<std::shared_ptr<ParagraphFormat>> ¶graphFormats) |
| 67 |
{ |
| 68 |
auto infoStream = m_blockParser.getChain(index); |
| 69 |
|
| 70 |
auto text = make_shared<Text>(); |
| 71 |
text->encoding = m_encoding; |
| 72 |
|
| 73 |
skip(infoStream, 4); |
| 74 |
|
| 75 |
{ |
| 76 |
const unsigned blocksSpecLength = readU32(infoStream, be); |
| 77 |
const long end = infoStream->tell() + blocksSpecLength; |
| 78 |
while (infoStream->tell() < end) |
| 79 |
{ |
| 80 |
const unsigned blockIndex = readU32(infoStream, be); |
| 81 |
unsigned length = m_header->version() >= QXP_4 ? readU32(infoStream, be) : readU16(infoStream, be); |
| 82 |
auto blockStream = m_blockParser.getBlock(blockIndex); |
| 83 |
if (length > getRemainingLength(blockStream)) |
| 84 |
length = getRemainingLength(blockStream); |
| 85 |
text->text += readString(blockStream, length); |
| 86 |
} |
| 87 |
} |
| 88 |
|
| 89 |
parseFormatSpec(infoStream, charFormats, m_header->version(), be, text->charFormats); |
| 90 |
parseFormatSpec(infoStream, paragraphFormats, m_header->version(), be, text->paragraphs); |
| 91 |
|
| 92 |
return text; |
| 93 |
} |
| 94 |
|
| 95 |
} |
| 96 |
|
| 97 |
/* vim:set shiftwidth=2 softtabstop=2 expandtab: */ |