diff --git a/src/lib/EscherFieldIds.h b/src/lib/EscherFieldIds.h index 6245ea4..f23c810 100644 --- a/src/lib/EscherFieldIds.h +++ b/src/lib/EscherFieldIds.h @@ -17,6 +17,16 @@ #define FIELDID_PXID 0x4104 #define FIELDID_BG_PXID 0x4186 #define FIELDID_SHAPE_ID 0x6801 +#define FIELDID_TABLE_ID 0x6802 // in the anchor of a table border +#define FIELDID_BORDER_DIRECTION 0x2001 // 1: along a row line, 2: along a column line +#define FIELDID_BORDER_START_ROW 0x2004 +#define FIELDID_BORDER_START_COLUMN 0x2005 +#define FIELDID_BORDER_END_ROW 0x2006 +#define FIELDID_BORDER_END_COLUMN 0x2007 +#define FIELDID_DX_WRAP_DIST_LEFT 0x0384 +#define FIELDID_DY_WRAP_DIST_TOP 0x0385 +#define FIELDID_DX_WRAP_DIST_RIGHT 0x0386 +#define FIELDID_DY_WRAP_DIST_BOTTOM 0x0387 #define FIELDID_FILL_COLOR 0x0181 #define FIELDID_FILL_OPACITY 0x0182 #define FIELDID_FILL_BACK_COLOR 0x0183 diff --git a/src/lib/MSPUBBlockID.h b/src/lib/MSPUBBlockID.h index 38f6139..d9db2c9 100644 --- a/src/lib/MSPUBBlockID.h +++ b/src/lib/MSPUBBlockID.h @@ -24,6 +24,7 @@ enum MSPUBBlockID // Don't be alarmed by multiple elements with the same value; CHUNK_OFFSET = 0x4, CHUNK_PARENT_SEQNUM = 0x5, SHAPE_CROP = 0xb7, + SHAPE_WRAP = 0x04, SHAPE_WIDTH = 0xaa, SHAPE_HEIGHT = 0xab, SHAPE_DONT_STRETCH_BA = 0x07, @@ -88,7 +89,8 @@ enum MSPUBBlockID // Don't be alarmed by multiple elements with the same value; PARAGRAPH_TABS = 0x32, TAB_ARRAY = 0x28, TAB_ENTRY = 0x0, - TAB_AMOUNT = 0x0 + TAB_AMOUNT = 0x0, + TAB_ALIGNMENT = 0x1 }; } // namespace libmspub diff --git a/src/lib/MSPUBCollector.cpp b/src/lib/MSPUBCollector.cpp index 7100a0b..452c70e 100644 --- a/src/lib/MSPUBCollector.cpp +++ b/src/lib/MSPUBCollector.cpp @@ -211,6 +211,7 @@ bool hasDrawableBorderArt(const Coordinate &coord, unsigned borderImageWidthInEm } const unsigned MAX_TABLE_CELLS = 1 << 20; +const unsigned MAX_TABLE_BORDERS = 1 << 16; bool hasDrawableTable(const TableInfo &tableInfo) { @@ -759,6 +760,23 @@ bool MSPUBCollector::hasPage(unsigned seqNum) const return m_pagesBySeqNum.find(seqNum) != m_pagesBySeqNum.end(); } +void MSPUBCollector::addTableBorder(unsigned seqNum, const TableBorder &border) +{ + std::vector &borders = m_shapeInfosBySeqNum[seqNum].m_tableBorders; + if (borders.size() < MAX_TABLE_BORDERS) + borders.push_back(border); +} + +void MSPUBCollector::setShapeWrap(unsigned seqNum, unsigned wrap) +{ + m_shapeInfosBySeqNum[seqNum].m_wrap = wrap; +} + +void MSPUBCollector::setShapeWrapDistances(unsigned seqNum, unsigned left, unsigned top, unsigned right, unsigned bottom) +{ + m_shapeInfosBySeqNum[seqNum].m_wrapDistances = Margins(left, top, right, bottom); +} + void MSPUBCollector::setShapeMargins(unsigned seqNum, unsigned left, unsigned top, unsigned right, unsigned bottom) { m_shapeInfosBySeqNum[seqNum].m_margins = Margins(left, top, right, bottom); @@ -882,6 +900,19 @@ std::function MSPUBCollector::paintShape(const ShapeInfo &info, cons { info.m_fill->getProperties(&graphicsProps); } + // how text of other boxes flows around this shape + if (bool(info.m_wrap) && get(info.m_wrap) == 0) + { + graphicsProps.insert("style:wrap", "none"); + } + if (bool(info.m_wrapDistances)) + { + const Margins &distances = get(info.m_wrapDistances); + graphicsProps.insert("fo:margin-left", double(distances.m_left) / EMUS_IN_INCH); + graphicsProps.insert("fo:margin-top", double(distances.m_top) / EMUS_IN_INCH); + graphicsProps.insert("fo:margin-right", double(distances.m_right) / EMUS_IN_INCH); + graphicsProps.insert("fo:margin-bottom", double(distances.m_bottom) / EMUS_IN_INCH); + } bool hasStroke = false; bool hasBorderArt = false; boost::optional maybeBorderImg = info.m_borderImgIndex; @@ -1297,6 +1328,25 @@ std::function MSPUBCollector::paintShape(const ShapeInfo &info, cons } props.insert("librevenge:table-columns", columnWidths); + if (!info.m_tableBorders.empty()) + { + // the borders run along grid lines, which keeps edges of merged cells exact + librevenge::RVNGPropertyListVector borders; + for (const TableBorder &tableBorder : info.m_tableBorders) + { + librevenge::RVNGPropertyList border; + border.insert("librevenge:vertical", tableBorder.m_vertical); + border.insert("librevenge:start-row", int(tableBorder.m_startRow)); + border.insert("librevenge:start-column", int(tableBorder.m_startColumn)); + border.insert("librevenge:end-row", int(tableBorder.m_endRow)); + border.insert("librevenge:end-column", int(tableBorder.m_endColumn)); + border.insert("svg:stroke-color", getColorString(tableBorder.m_color.getFinalColor(m_paletteColors))); + border.insert("svg:stroke-width", double(tableBorder.m_widthInEmu) / EMUS_IN_INCH); + borders.append(border); + } + props.insert("librevenge:table-borders", borders); + } + m_painter->startTableObject(props); const std::map >::const_iterator it = m_tableCellTextEndsByTextId.find(get(info.m_textId)); @@ -1309,6 +1359,13 @@ std::function MSPUBCollector::paintShape(const ShapeInfo &info, cons ParagraphTexts_t paraTexts; mapTableTextToCells(text, tableCellTextEnds, getCalculatedEncoding(), paraToCellMap, paraTexts); + // A cell that stores no margins has none. Only when no cell of the table stores any + // (an older layout) are they left out, so that the reader's default applies. + const std::vector &tableCells = get(info.m_tableInfo).m_cells; + bool cellsHaveMargins = false; + for (const CellInfo &tableCell : tableCells) + cellsHaveMargins = cellsHaveMargins || tableCell.m_hasMargins; + const std::pair *const hyphenation = getTextHyphenation(info); for (unsigned row = 0; row != tableLayout.shape()[0]; ++row) { @@ -1334,6 +1391,23 @@ std::function MSPUBCollector::paintShape(const ShapeInfo &info, cons if (tableLayout[row][col].m_rowSpan > 1) cellProps.insert("table:number-rows-spanned", int(tableLayout[row][col].m_rowSpan)); + if (tableLayout[row][col].m_cell < tableCells.size()) + { + const CellInfo &tableCell = tableCells[tableLayout[row][col].m_cell]; + if (cellsHaveMargins) + { + const unsigned maxMargin = 2 * EMUS_IN_INCH; + cellProps.insert("fo:padding-left", double(std::min(tableCell.m_marginLeftInEmu, maxMargin)) / EMUS_IN_INCH); + cellProps.insert("fo:padding-top", double(std::min(tableCell.m_marginTopInEmu, maxMargin)) / EMUS_IN_INCH); + cellProps.insert("fo:padding-right", double(std::min(tableCell.m_marginRightInEmu, maxMargin)) / EMUS_IN_INCH); + cellProps.insert("fo:padding-bottom", double(std::min(tableCell.m_marginBottomInEmu, maxMargin)) / EMUS_IN_INCH); + } + if (tableCell.m_verticalAlign == 1) + cellProps.insert("style:vertical-align", "middle"); + else if (tableCell.m_verticalAlign == 2) + cellProps.insert("style:vertical-align", "bottom"); + } + m_painter->openTableCell(cellProps); if (tableLayout[row][col].m_cell < paraToCellMap.size()) @@ -1821,6 +1895,35 @@ librevenge::RVNGPropertyList MSPUBCollector::getParaStyleProps(const ParagraphSt { ret.insert("style:length", (int)dropCapLetters); } + // tab stops, from the paragraph or else from the style it is based on + const ParagraphStyle &tabStyle = style.m_tabStopsInEmu.empty() ? defaultStyle : style; + if (!tabStyle.m_tabStopsInEmu.empty()) + { + librevenge::RVNGPropertyListVector tabStops; + for (size_t i = 0; i < tabStyle.m_tabStopsInEmu.size(); ++i) + { + librevenge::RVNGPropertyList tab; + tab.insert("style:position", double(tabStyle.m_tabStopsInEmu[i]) / EMUS_IN_INCH); + switch (i < tabStyle.m_tabStopAlignments.size() ? tabStyle.m_tabStopAlignments[i] : 0) + { + case 1: + tab.insert("style:type", "right"); + break; + case 2: + tab.insert("style:type", "center"); + break; + case 3: + tab.insert("style:type", "char"); + tab.insert("style:char", "."); + break; + default: + tab.insert("style:type", "left"); + break; + } + tabStops.append(tab); + } + ret.insert("style:tab-stops", tabStops); + } return ret; } diff --git a/src/lib/MSPUBCollector.h b/src/lib/MSPUBCollector.h index 90498eb..7789d73 100644 --- a/src/lib/MSPUBCollector.h +++ b/src/lib/MSPUBCollector.h @@ -73,6 +73,9 @@ public: void setShapePictureBrightness(unsigned seqNum, int brightness); void setShapePictureContrast(unsigned seqNum, int contrast); void setShapeTableInfo(unsigned seqNum, const TableInfo &ti); + void addTableBorder(unsigned seqNum, const TableBorder &border); + void setShapeWrap(unsigned seqNum, unsigned wrap); + void setShapeWrapDistances(unsigned seqNum, unsigned left, unsigned top, unsigned right, unsigned bottom); void setShapeBorderImageId(unsigned seqNum, unsigned borderImageId); void setShapeCoordinatesInEmu(unsigned seqNum, int xs, int ys, int xe, int ye); void setShapeImgIndex(unsigned seqNum, unsigned index); diff --git a/src/lib/MSPUBConstants.h b/src/lib/MSPUBConstants.h index c4712b2..e709c9b 100644 --- a/src/lib/MSPUBConstants.h +++ b/src/lib/MSPUBConstants.h @@ -13,6 +13,10 @@ #define EMUS_IN_INCH 914400 #define POINTS_IN_INCH 72 #define DEFAULT_MARGIN 36576 +#define DEFAULT_WRAP_DIST_X 114300 +#define DEFAULT_WRAP_DIST_Y 0 +#define DEFAULT_LINE_WIDTH_EMU 9525 +#define MAX_BORDER_WIDTH_EMU 1270000 #endif /* INCLUDED_MSPUBCONSTANTS_H */ /* vim:set shiftwidth=2 softtabstop=2 expandtab: */ diff --git a/src/lib/MSPUBParser.cpp b/src/lib/MSPUBParser.cpp index c614e15..7df523a 100644 --- a/src/lib/MSPUBParser.cpp +++ b/src/lib/MSPUBParser.cpp @@ -813,6 +813,10 @@ bool MSPUBParser::parseShape(librevenge::RVNGInputStream *input, while (stillReading(input, pos + length)) { MSPUBBlockInfo info = parseBlock(input, true); + if (info.id == SHAPE_WRAP) + { + m_collector->setShapeWrap(chunk.seqNum, info.data & 0xff); + } if (info.id == TABLE_CELLS_SEQNUM) { cellsSeqNum = info.data; @@ -929,7 +933,26 @@ bool MSPUBParser::parseShape(librevenge::RVNGInputStream *input, case 0x04: currentCell.m_endColumn = subInfo.data; break; - // TODO: 0x09 - 0x0e: width/height of content + margins? + case 0x07: + currentCell.m_verticalAlign = subInfo.data & 0xff; + break; + case 0x0a: + currentCell.m_marginLeftInEmu = subInfo.data; + currentCell.m_hasMargins = true; + break; + case 0x0b: + currentCell.m_marginRightInEmu = subInfo.data; + currentCell.m_hasMargins = true; + break; + case 0x0c: + currentCell.m_marginTopInEmu = subInfo.data; + currentCell.m_hasMargins = true; + break; + case 0x0d: + currentCell.m_marginBottomInEmu = subInfo.data; + currentCell.m_hasMargins = true; + break; + // TODO: 0x09: height of the content? 0x0e: unknown default: break; } @@ -968,7 +991,11 @@ bool MSPUBParser::parseShape(librevenge::RVNGInputStream *input, while (stillReading(input, pos + length)) { MSPUBBlockInfo info = parseBlock(input, true); - if (info.id == SHAPE_WIDTH) + if (info.id == SHAPE_WRAP) + { + m_collector->setShapeWrap(chunk.seqNum, info.data & 0xff); + } + else if (info.id == SHAPE_WIDTH) { width = info.data; } @@ -1497,11 +1524,22 @@ ParagraphStyle MSPUBParser::getParagraphStyle(librevenge::RVNGInputStream *input MSPUBBlockInfo tabEntryInfo = parseBlock(input, true); if (tabEntryInfo.type == GENERAL_CONTAINER) { + // a tab stop: its position (block 0) and, when not a left tab, its alignment (block 1) input->seek(tabEntryInfo.dataOffset + 4, librevenge::RVNG_SEEK_SET); - MSPUBBlockInfo tabInfo = parseBlock(input, true); - if (tabInfo.id == TAB_AMOUNT) + boost::optional position; + unsigned alignment = 0; + while (stillReading(input, tabEntryInfo.dataOffset + tabEntryInfo.dataLength)) + { + MSPUBBlockInfo tabInfo = parseBlock(input, true); + if (tabInfo.id == TAB_AMOUNT) + position = tabInfo.data; + else if (tabInfo.id == TAB_ALIGNMENT) + alignment = tabInfo.data & 0xff; + } + if (position) { - ret.m_tabStopsInEmu.push_back(tabInfo.data); + ret.m_tabStopsInEmu.push_back(get(position)); + ret.m_tabStopAlignments.push_back(alignment); } } } @@ -1987,6 +2025,21 @@ void MSPUBParser::parseEscherShape(librevenge::RVNGInputStream *input, const Esc rotated90 = (rotation >= 45 && rotation < 135) || (rotation >= 225 && rotation < 315); } + { + // how far text that wraps around this shape stays away from it + const unsigned *const wrapLeft = getIfExists(foptValues.m_scalarValues, FIELDID_DX_WRAP_DIST_LEFT); + const unsigned *const wrapTop = getIfExists(foptValues.m_scalarValues, FIELDID_DY_WRAP_DIST_TOP); + const unsigned *const wrapRight = getIfExists(foptValues.m_scalarValues, FIELDID_DX_WRAP_DIST_RIGHT); + const unsigned *const wrapBottom = getIfExists(foptValues.m_scalarValues, FIELDID_DY_WRAP_DIST_BOTTOM); + if (wrapLeft || wrapTop || wrapRight || wrapBottom) + { + m_collector->setShapeWrapDistances(*shapeSeqNum, + wrapLeft ? *wrapLeft : DEFAULT_WRAP_DIST_X, + wrapTop ? *wrapTop : DEFAULT_WRAP_DIST_Y, + wrapRight ? *wrapRight : DEFAULT_WRAP_DIST_X, + wrapBottom ? *wrapBottom : DEFAULT_WRAP_DIST_Y); + } + } unsigned *ptr_left = getIfExists(foptValues.m_scalarValues, FIELDID_DY_TEXT_LEFT); unsigned *ptr_top = getIfExists(foptValues.m_scalarValues, FIELDID_DY_TEXT_TOP); unsigned *ptr_right = getIfExists(foptValues.m_scalarValues, FIELDID_DY_TEXT_RIGHT); @@ -2162,6 +2215,47 @@ void MSPUBParser::parseEscherShape(librevenge::RVNGInputStream *input, const Esc } } } + else + { + parseTableBorder(input, sp); + } +} + +// A table's borders are stored as shapes of their own: one per run of cell edges, anchored +// to the table's grid lines in place of a position, and filled with the border's color. +void MSPUBParser::parseTableBorder(librevenge::RVNGInputStream *input, const EscherContainerInfo &sp) +{ + EscherContainerInfo cAnchor; + input->seek(sp.contentsOffset, librevenge::RVNG_SEEK_SET); + if (!findEscherContainer(input, sp, cAnchor, OFFICE_ART_CLIENT_ANCHOR)) + return; + const std::map anchor = extractEscherValues(input, cAnchor); + const unsigned *const table = getIfExists_const(anchor, FIELDID_TABLE_ID); + const unsigned *const direction = getIfExists_const(anchor, FIELDID_BORDER_DIRECTION); + if (!table || !direction || (*direction != 1 && *direction != 2)) + return; + const unsigned *const startRow = getIfExists_const(anchor, FIELDID_BORDER_START_ROW); + const unsigned *const startColumn = getIfExists_const(anchor, FIELDID_BORDER_START_COLUMN); + const unsigned *const endRow = getIfExists_const(anchor, FIELDID_BORDER_END_ROW); + const unsigned *const endColumn = getIfExists_const(anchor, FIELDID_BORDER_END_COLUMN); + EscherContainerInfo cFopt; + input->seek(sp.contentsOffset, librevenge::RVNG_SEEK_SET); + if (!findEscherContainer(input, sp, cFopt, OFFICE_ART_FOPT)) + return; + const FOPTValues fopt = extractFOPTValues(input, cFopt); + const unsigned *const fillFlags = getIfExists_const(fopt.m_scalarValues, FIELDID_FIELD_STYLE_BOOL_PROPS); + if (fillFlags && (*fillFlags & 0x100000) && !(*fillFlags & 0x10)) + return; // explicitly not filled: no border here + const unsigned *const color = getIfExists_const(fopt.m_scalarValues, FIELDID_FILL_COLOR); + const unsigned *const width = getIfExists_const(fopt.m_scalarValues, FIELDID_LINE_WIDTH); + TableBorder border(*direction == 2, + startRow ? *startRow : 0, startColumn ? *startColumn : 0, + endRow ? *endRow : 0, endColumn ? *endColumn : 0, + ColorReference(color ? *color : 0x08000000), + width ? *width : DEFAULT_LINE_WIDTH_EMU); + if (border.m_widthInEmu == 0 || border.m_widthInEmu > MAX_BORDER_WIDTH_EMU) + return; + m_collector->addTableBorder(*table, border); } std::shared_ptr MSPUBParser::getNewFill(const std::map &foptProperties, diff --git a/src/lib/MSPUBParser.h b/src/lib/MSPUBParser.h index e97bafe..f9d7de1 100644 --- a/src/lib/MSPUBParser.h +++ b/src/lib/MSPUBParser.h @@ -120,6 +120,7 @@ protected: void parseShapeGroup(librevenge::RVNGInputStream *input, const EscherContainerInfo &spgr, Coordinate parentCoordinateSystem, Coordinate parentGroupAbsoluteCoord, unsigned depth = 0); void skipBlock(librevenge::RVNGInputStream *input, const MSPUBBlockInfo &block); void parseEscherShape(librevenge::RVNGInputStream *input, const EscherContainerInfo &sp, Coordinate &parentCoordinateSystem, Coordinate &parentGroupAbsoluteCoord); + void parseTableBorder(librevenge::RVNGInputStream *input, const EscherContainerInfo &sp); bool findEscherContainer(librevenge::RVNGInputStream *input, const EscherContainerInfo &parent, EscherContainerInfo &out, unsigned short type); bool findEscherContainerWithTypeInSet(librevenge::RVNGInputStream *input, const EscherContainerInfo &parent, EscherContainerInfo &out, const std::set &types); std::map extractEscherValues(librevenge::RVNGInputStream *input, const EscherContainerInfo &record); diff --git a/src/lib/MSPUBTypes.h b/src/lib/MSPUBTypes.h index d44d020..522276f 100644 --- a/src/lib/MSPUBTypes.h +++ b/src/lib/MSPUBTypes.h @@ -172,12 +172,13 @@ struct ParagraphStyle boost::optional m_rightIndentEmu; boost::optional m_listInfo; std::vector m_tabStopsInEmu; + std::vector m_tabStopAlignments; boost::optional m_dropCapLines; boost::optional m_dropCapLetters; ParagraphStyle() : m_align(), m_defaultCharStyleIndex(), m_lineSpacing(), m_spaceBeforeEmu(), m_spaceAfterEmu(), m_firstLineIndentEmu(), m_leftIndentEmu(), - m_rightIndentEmu(), m_listInfo(), m_tabStopsInEmu(), m_dropCapLines(), m_dropCapLetters() + m_rightIndentEmu(), m_listInfo(), m_tabStopsInEmu(), m_tabStopAlignments(), m_dropCapLines(), m_dropCapLetters() { } }; diff --git a/src/lib/ShapeInfo.h b/src/lib/ShapeInfo.h index d69628d..4bad19d 100644 --- a/src/lib/ShapeInfo.h +++ b/src/lib/ShapeInfo.h @@ -67,6 +67,9 @@ struct ShapeInfo std::vector m_clipPath; boost::optional m_pictureBrightness; boost::optional m_pictureContrast; + std::vector m_tableBorders; + boost::optional m_wrap; // 0: text does not wrap around this shape + boost::optional m_wrapDistances; ShapeInfo() : m_type(), m_cropType(), m_imgIndex(), m_borderImgIndex(), m_coordinates(), m_lines(), m_pageSeqNum(), m_textId(), m_adjustValuesByIndex(), m_adjustValues(), @@ -75,7 +78,8 @@ struct ShapeInfo m_lineBackColor(), m_dash(), m_tableInfo(), m_numColumns(), m_columnSpacing(0), m_beginArrow(), m_endArrow(), - m_verticalAlign(), m_pictureRecolor(), m_shadow(), m_innerRotation(), m_clipPath(), m_pictureBrightness(), m_pictureContrast() + m_verticalAlign(), m_pictureRecolor(), m_shadow(), m_innerRotation(), m_clipPath(), m_pictureBrightness(), m_pictureContrast(), + m_tableBorders(), m_wrap(), m_wrapDistances() { } std::shared_ptr getCustomShape() const diff --git a/src/lib/TableInfo.h b/src/lib/TableInfo.h index 7ff4240..7ceb3f4 100644 --- a/src/lib/TableInfo.h +++ b/src/lib/TableInfo.h @@ -12,6 +12,8 @@ #include +#include "ColorReference.h" + namespace libmspub { @@ -22,6 +24,12 @@ struct CellInfo , m_endRow() , m_startColumn() , m_endColumn() + , m_hasMargins(false) + , m_marginLeftInEmu(0) + , m_marginTopInEmu(0) + , m_marginRightInEmu(0) + , m_marginBottomInEmu(0) + , m_verticalAlign(0) { } @@ -29,6 +37,38 @@ struct CellInfo unsigned m_endRow; unsigned m_startColumn; unsigned m_endColumn; + // the space between the cell's edges and its text + bool m_hasMargins; + unsigned m_marginLeftInEmu; + unsigned m_marginTopInEmu; + unsigned m_marginRightInEmu; + unsigned m_marginBottomInEmu; + unsigned m_verticalAlign; // 0: top, 1: middle, 2: bottom +}; + +// A run of cell edges that carries a border. Rows and columns count the table's grid lines: +// line 0 is the top or left edge of the table. +struct TableBorder +{ + TableBorder(bool vertical, unsigned startRow, unsigned startColumn, unsigned endRow, unsigned endColumn, + ColorReference color, unsigned widthInEmu) + : m_vertical(vertical) + , m_startRow(startRow) + , m_startColumn(startColumn) + , m_endRow(endRow) + , m_endColumn(endColumn) + , m_color(color) + , m_widthInEmu(widthInEmu) + { + } + + bool m_vertical; + unsigned m_startRow; + unsigned m_startColumn; + unsigned m_endRow; + unsigned m_endColumn; + ColorReference m_color; + unsigned m_widthInEmu; }; struct TableInfo