diff --git a/src/lib/MSPUBCollector.cpp b/src/lib/MSPUBCollector.cpp index 5ba6c0e..7100a0b 100644 --- a/src/lib/MSPUBCollector.cpp +++ b/src/lib/MSPUBCollector.cpp @@ -499,7 +499,7 @@ void MSPUBCollector::setNextPage(unsigned pageSeqNum) MSPUBCollector::MSPUBCollector(librevenge::RVNGDrawingInterface *painter) : m_painter(painter), m_contentChunkReferences(), m_width(0), m_height(0), m_widthSet(false), m_heightSet(false), - m_numPages(0), m_textStringsById(), m_pagesBySeqNum(), + m_numPages(0), m_textStringsById(), m_textHyphenationById(), m_pagesBySeqNum(), m_images(), m_borderImages(), m_textColors(), m_fonts(), m_defaultCharStyles(), m_defaultParaStyles(), m_shapeTypesBySeqNum(), @@ -525,6 +525,164 @@ void MSPUBCollector::setTextStringOffset( m_stringOffsetsByTextId[textId] = offset; } +namespace +{ + +void appendCodePoint(librevenge::RVNGString &text, unsigned c) +{ + if (c < 0x80) + text.append(char(c)); + else if (c < 0x800) + { + text.append(char(0xc0 | (c >> 6))); + text.append(char(0x80 | (c & 0x3f))); + } + else if (c < 0x10000) + { + text.append(char(0xe0 | (c >> 12))); + text.append(char(0x80 | ((c >> 6) & 0x3f))); + text.append(char(0x80 | (c & 0x3f))); + } + else + { + text.append(char(0xf0 | (c >> 18))); + text.append(char(0x80 | ((c >> 12) & 0x3f))); + text.append(char(0x80 | ((c >> 6) & 0x3f))); + text.append(char(0x80 | (c & 0x3f))); + } +} + +bool isSameList(const ListInfo &a, const ListInfo &b) +{ + return a.m_listType == b.m_listType && a.m_bulletChar == b.m_bulletChar + && a.m_numberingType == b.m_numberingType && a.m_numberingDelimiter == b.m_numberingDelimiter + && !b.m_numberIfRestarted; +} + +librevenge::RVNGPropertyList getListLevelProps(const ListInfo &info) +{ + librevenge::RVNGPropertyList props; + props.insert("librevenge:level", 1); + if (info.m_listType == UNORDERED) + { + librevenge::RVNGString bullet; + unsigned c = info.m_bulletChar.get_value_or(0x2022); + if (c >= 0xf020 && c <= 0xf0ff) // a symbol font character in the private use area + c -= 0xf000; + if (c == 0xb7) // the bullet of the Symbol font, which Publisher uses by default + c = 0x2022; + appendCodePoint(bullet, (c >= 0x20 && c < 0x110000 && !(c >= 0xd800 && c < 0xe000)) ? c : 0x2022); + props.insert("text:bullet-char", bullet); + return props; + } + const char *format = "1"; + switch (info.m_numberingType.get_value_or(STANDARD_WESTERN)) + { + case UPPERCASE_ROMAN: + format = "I"; + break; + case LOWERCASE_ROMAN: + format = "i"; + break; + case UPPERCASE_LETTERS: + format = "A"; + break; + case LOWERCASE_LETTERS: + format = "a"; + break; + default: + break; + } + props.insert("style:num-format", format); + const char *prefix = ""; + const char *suffix = "."; + switch (info.m_numberingDelimiter.get_value_or(NO_DELIMITER)) + { + case PARENTHESIS: + suffix = ")"; + break; + case PARENTHESES_SURROUND: + prefix = "("; + suffix = ")"; + break; + case SQUARE_BRACKET: + suffix = "]"; + break; + case COLON: + suffix = ":"; + break; + case SQUARE_BRACKET_SURROUND: + prefix = "["; + suffix = "]"; + break; + case HYPHEN_SURROUND: + prefix = "-"; + suffix = "-"; + break; + case IDEOGRAPHIC_HALF_COMMA: + suffix = "\xe3\x80\x81"; + break; + default: + break; + } + if (*prefix) + props.insert("style:num-prefix", prefix); + props.insert("style:num-suffix", suffix); + props.insert("text:start-value", int(info.m_numberIfRestarted.get_value_or(1))); + return props; +} + +void closeListLevel(librevenge::RVNGDrawingInterface *painter, boost::optional &list) +{ + if (!list) + return; + if (list->m_listType == UNORDERED) + painter->closeUnorderedListLevel(); + else + painter->closeOrderedListLevel(); + list = boost::none; +} + +// Opens a list level for a list paragraph, or closes the open one before a plain paragraph +void updateListLevel(librevenge::RVNGDrawingInterface *painter, boost::optional &list, const boost::optional ¶) +{ + if (list && para && isSameList(*list, *para)) + return; + closeListLevel(painter, list); + if (!para) + return; + if (para->m_listType == UNORDERED) + painter->openUnorderedListLevel(getListLevelProps(*para)); + else + painter->openOrderedListLevel(getListLevelProps(*para)); + list = para; +} + +} + +void MSPUBCollector::setTextHyphenation(unsigned textId, bool hyphenate, unsigned zoneInEmu) +{ + m_textHyphenationById[textId] = std::make_pair(hyphenate, zoneInEmu); +} + +// A paragraph is in a list when its own style or the style it is based on says so +boost::optional MSPUBCollector::getParaListInfo(const ParagraphStyle &style) const +{ + if (style.m_listInfo) + return style.m_listInfo; + if (style.m_defaultCharStyleIndex && style.m_defaultCharStyleIndex.get() < m_defaultParaStyles.size()) + return m_defaultParaStyles[style.m_defaultCharStyleIndex.get()].m_listInfo; + return boost::none; +} + +// Automatic hyphenation of the shape's story (on or off) and its hyphenation zone, when known +const std::pair *MSPUBCollector::getTextHyphenation(const ShapeInfo &info) const +{ + if (!info.m_textId) + return nullptr; + return getIfExists_const(m_textHyphenationById, info.m_textId.get()); +} + void MSPUBCollector::setTableCellTextEnds( const unsigned textId, const std::vector &ends) { @@ -1151,6 +1309,7 @@ std::function MSPUBCollector::paintShape(const ShapeInfo &info, cons ParagraphTexts_t paraTexts; mapTableTextToCells(text, tableCellTextEnds, getCalculatedEncoding(), paraToCellMap, paraTexts); + const std::pair *const hyphenation = getTextHyphenation(info); for (unsigned row = 0; row != tableLayout.shape()[0]; ++row) { librevenge::RVNGPropertyList rowProps; @@ -1183,11 +1342,15 @@ std::function MSPUBCollector::paintShape(const ShapeInfo &info, cons for (unsigned para = cellParas.first; para <= cellParas.second; ++para) { librevenge::RVNGPropertyList paraProps = getParaStyleProps(text[para].style, text[para].style.m_defaultCharStyleIndex); + if (hyphenation && hyphenation->first) + paraProps.insert("librevenge:hyphenation-zone", double(hyphenation->second) / EMUS_IN_INCH); m_painter->openParagraph(paraProps); for (size_t i_spans = 0; i_spans < paraTexts[para].size(); ++i_spans) { librevenge::RVNGPropertyList charProps = getCharStyleProps(text[para].spans[i_spans].style, text[para].style.m_defaultCharStyleIndex); + if (hyphenation) + charProps.insert("fo:hyphenate", hyphenation->first); m_painter->openSpan(charProps); separateSpacesAndInsertText(m_painter, paraTexts[para][i_spans]); m_painter->closeSpan(); @@ -1242,22 +1405,36 @@ std::function MSPUBCollector::paintShape(const ShapeInfo &info, cons props.insert("fo:column-gap", (double)ngap / EMUS_IN_INCH); } m_painter->startTextObject(props); + const std::pair *const hyphenation = getTextHyphenation(info); + boost::optional openList; for (const auto &line : text) { librevenge::RVNGPropertyList paraProps = getParaStyleProps(line.style, line.style.m_defaultCharStyleIndex); - m_painter->openParagraph(paraProps); + if (hyphenation && hyphenation->first) + paraProps.insert("librevenge:hyphenation-zone", double(hyphenation->second) / EMUS_IN_INCH); + updateListLevel(m_painter, openList, getParaListInfo(line.style)); + if (openList) + m_painter->openListElement(paraProps); + else + m_painter->openParagraph(paraProps); for (size_t i_spans = 0; i_spans < line.spans.size(); ++i_spans) { librevenge::RVNGString textString; appendCharacters(textString, line.spans[i_spans].chars, getCalculatedEncoding()); librevenge::RVNGPropertyList charProps = getCharStyleProps(line.spans[i_spans].style, line.style.m_defaultCharStyleIndex); + if (hyphenation) + charProps.insert("fo:hyphenate", hyphenation->first); m_painter->openSpan(charProps); separateSpacesAndInsertText(m_painter, textString); m_painter->closeSpan(); } - m_painter->closeParagraph(); + if (openList) + m_painter->closeListElement(); + else + m_painter->closeParagraph(); } + closeListLevel(m_painter, openList); m_painter->endTextObject(); } } diff --git a/src/lib/MSPUBCollector.h b/src/lib/MSPUBCollector.h index eb7f05a..90498eb 100644 --- a/src/lib/MSPUBCollector.h +++ b/src/lib/MSPUBCollector.h @@ -123,6 +123,7 @@ public: void setTableCellTextEnds(unsigned textId, const std::vector &ends); void setTextStringOffset(unsigned textId, unsigned offset); + void setTextHyphenation(unsigned textId, bool hyphenate, unsigned zoneInEmu); bool go(); @@ -144,6 +145,7 @@ private: bool m_widthSet, m_heightSet; unsigned short m_numPages; std::map > m_textStringsById; + std::map > m_textHyphenationById; std::map m_pagesBySeqNum; std::vector > m_images; std::vector m_borderImages; @@ -195,6 +197,8 @@ private: librevenge::RVNGPropertyList getCharStyleProps(const CharacterStyle &, boost::optional defaultCharStyleIndex) const; librevenge::RVNGPropertyList getParaStyleProps(const ParagraphStyle &, boost::optional defaultParaStyleIndex) const; + const std::pair *getTextHyphenation(const ShapeInfo &info) const; + boost::optional getParaListInfo(const ParagraphStyle &style) const; double getSpecialValue(const ShapeInfo &info, const CustomShape &shape, int arg, const std::vector &adjustValues) const; void ponderStringEncoding(const std::vector &str); const char *getCalculatedEncoding() const; diff --git a/src/lib/MSPUBParser.cpp b/src/lib/MSPUBParser.cpp index 3ee1934..c614e15 100644 --- a/src/lib/MSPUBParser.cpp +++ b/src/lib/MSPUBParser.cpp @@ -107,6 +107,25 @@ NumberingType readNumberingType(const unsigned value) return NumberingType(value); } +NumberingDelimiter readNumberingDelimiter(const unsigned value) +{ + switch (value) + { + case PARENTHESIS: + case PARENTHESES_SURROUND: + case PERIOD: + case SQUARE_BRACKET: + case COLON: + case SQUARE_BRACKET_SURROUND: + case HYPHEN_SURROUND: + case IDEOGRAPHIC_HALF_COMMA: + return NumberingDelimiter(value); + default: + MSPUB_DEBUG_MSG(("unknown numbering delimiter %u\n", value)); + return NO_DELIMITER; + } +} + Alignment readAlignment(const unsigned value) { switch (value & 0xff) @@ -1086,6 +1105,7 @@ bool MSPUBParser::parseQuill(librevenge::RVNGInputStream *input) std::vector spans; std::vector paras; unsigned whichStsh = 0; + std::vector > storyHyphenation; for (std::list::const_iterator i = chunkReferences.begin(); i != chunkReferences.end(); ++i) { if (i->name == "TEXT") @@ -1156,6 +1176,11 @@ bool MSPUBParser::parseQuill(librevenge::RVNGInputStream *input) input->seek(i->offset, librevenge::RVNG_SEEK_SET); tableCellTextEnds[i->id] = parseTableCellDefinitions(input, *i); } + else if (i->name == "MCLD") + { + input->seek(i->offset, librevenge::RVNG_SEEK_SET); + storyHyphenation = parseStoryHyphenation(input, *i); + } } if (parsedStrs && parsedSyid && parsedFdpc && parsedFdpp && parsedStsh && parsedFont && textChunkReference != chunkReferences.end()) { @@ -1214,6 +1239,8 @@ bool MSPUBParser::parseQuill(librevenge::RVNGInputStream *input) } m_collector->addTextString(readParas, textIDs[j]); m_collector->setTextStringOffset(textIDs[j], textOffsets[j]); + if (j < storyHyphenation.size()) + m_collector->setTextHyphenation(textIDs[j], storyHyphenation[j].first, storyHyphenation[j].second); const std::map >::const_iterator it = tableCellTextEnds.find(j); if (it != tableCellTextEnds.end()) m_collector->setTableCellTextEnds(textIDs[j], it->second); @@ -1223,6 +1250,64 @@ bool MSPUBParser::parseQuill(librevenge::RVNGInputStream *input) return true; } +// The MCLD chunk has one record per story, in the same order as the STRS chunk. A record is a list of +// story-level blocks (block 0: automatic hyphenation, on when its type is 0x0a; block 1: the hyphenation +// zone in EMU), then the number of text frames of the story and a block list for each frame. +std::vector > MSPUBParser::parseStoryHyphenation(librevenge::RVNGInputStream *input, const QuillChunkReference &chunk) +{ + std::vector > ret; + const unsigned long end = chunk.offset + chunk.length; + if (chunk.length < 8) + return ret; + readU32(input); + const unsigned numStories = readU32(input); + if (numStories > (chunk.length - 8) / 4) + return ret; + input->seek(input->tell() + 4 * (unsigned long)numStories, librevenge::RVNG_SEEK_SET); + for (unsigned i = 0; i < numStories; ++i) + { + const unsigned long start = input->tell(); + if (start + 8 > end) + break; + const unsigned headerLength = readU32(input); + if (headerLength < 4 || start + headerLength + 4 > end) + break; + bool hyphenate = false; + unsigned zone = 228600; // 0.25 inch, Publisher's default + while (stillReading(input, start + headerLength)) + { + const MSPUBBlockInfo info = parseBlock(input, true); + if (info.id == 0) + hyphenate = info.type == 0x0a; + else if (info.id == 1 && info.dataLength == 4) + zone = info.data; + } + input->seek(start + headerLength, librevenge::RVNG_SEEK_SET); + const unsigned numFrames = readU32(input); + bool broken = false; + for (unsigned j = 0; j < numFrames; ++j) + { + const unsigned long frameStart = input->tell(); + if (frameStart + 4 > end) + { + broken = true; + break; + } + const unsigned frameLength = readU32(input); + if (frameLength < 4 || frameStart + frameLength > end) + { + broken = true; + break; + } + input->seek(frameStart + frameLength, librevenge::RVNG_SEEK_SET); + } + ret.push_back(std::make_pair(hyphenate, zone)); + if (broken) + break; + } + return ret; +} + void MSPUBParser::parseFonts(librevenge::RVNGInputStream *input, const QuillChunkReference &) { readU32(input); @@ -1433,10 +1518,10 @@ ParagraphStyle MSPUBParser::getParagraphStyle(librevenge::RVNGInputStream *input switch (listSubInfo.id) { case PARAGRAPH_LIST_NUMBERING_TYPE: - numberingType = readNumberingType(info.data); + numberingType = readNumberingType(listSubInfo.data); break; case PARAGRAPH_LIST_BULLET_CHAR: - bulletChar = info.data; + bulletChar = listSubInfo.data; break; default: break; @@ -1447,6 +1532,9 @@ ParagraphStyle MSPUBParser::getParagraphStyle(librevenge::RVNGInputStream *input case PARAGRAPH_LIST_NUMBER_RESTART: numberIfRestarted = info.data; break; + case PARAGRAPH_LIST_NUMBERING_DELIMITER: + numberingDelimiter = readNumberingDelimiter(info.data); + break; case PARAGRAPH_DROP_CAP_LINES: ret.m_dropCapLines = info.data; break; diff --git a/src/lib/MSPUBParser.h b/src/lib/MSPUBParser.h index 96a0dac..e97bafe 100644 --- a/src/lib/MSPUBParser.h +++ b/src/lib/MSPUBParser.h @@ -115,6 +115,7 @@ protected: void parsePaletteEntry(librevenge::RVNGInputStream *input, const MSPUBBlockInfo &block); void parseColors(librevenge::RVNGInputStream *input, const QuillChunkReference &chunk); void parseFonts(librevenge::RVNGInputStream *input, const QuillChunkReference &chunk); + std::vector > parseStoryHyphenation(librevenge::RVNGInputStream *input, const QuillChunkReference &chunk); void parseDefaultStyle(librevenge::RVNGInputStream *input, const QuillChunkReference &chunk); void parseShapeGroup(librevenge::RVNGInputStream *input, const EscherContainerInfo &spgr, Coordinate parentCoordinateSystem, Coordinate parentGroupAbsoluteCoord, unsigned depth = 0); void skipBlock(librevenge::RVNGInputStream *input, const MSPUBBlockInfo &block);