Commit ea2e184d authored by Elen.Subbotina's avatar Elen.Subbotina Committed by Alexander Trofimov

конвертация таблиц в документах

git-svn-id: svn://fileserver/activex/AVS/Sources/TeamlabOffice/trunk/ServerComponents@57186 954022d7-b5bf-4e40-9824-e11837661b57
parent 557b1562
......@@ -113,7 +113,7 @@ namespace odf
{
CP_XML_NODE(L"meta:generator")
{
CP_XML_STREAM() << L"Teamlab Office";
CP_XML_STREAM() << L"ONLYOFFICE Online Editor";
}
CP_XML_NODE(L"meta:initial-creator");
CP_XML_NODE(L"meta:creation-date");
......
#include "precompiled_cpodf.h"
#include "logging.h"
#include <boost/foreach.hpp>
#include <iostream>
#include "odf_conversion_context.h"
#include "odf_table_context.h" //easy(lite) case ods_table_state/context - make based???
#include "styles.h"
#include "table.h"
#include "style_table_properties.h"
//#include "style_text_properties.h"
//#include "style_paragraph_properties.h"
//#include "style_graphic_properties.h"
namespace cpdoccore
{
namespace odf
{
struct odf_table_state
{
odf_table_state()
{
current_row = 0;
current_column = 0;
count_header_row = 0;
current_spanned_row = 0;
}
std::vector<odf_element_state> rows;
std::vector<odf_element_state> columns;
std::vector<odf_element_state> cells;
odf_element_state table;
__int32 current_row;
__int32 current_column;
__int32 count_header_row;
__int32 current_spanned_row;
};
class odf_table_context::Impl
{
public:
Impl(odf_conversion_context *odf_context) :odf_context_(odf_context)
{
}
odf_table_state & current_table() {return tables_.back();}
bool empty () {return tables_.size() >0 ? false : true; }
void start_table(odf_table_state & state) {tables_.push_back(state);}
void end_table() {if (tables_.size() > 0) tables_.pop_back();}
odf_style_context * styles_context() {return odf_context_->styles_context();}
private:
std::vector<odf_table_state> tables_;// current level ...
odf_conversion_context *odf_context_;
};
////////////////////////////////////////////////////////////////////////////
odf_table_context::odf_table_context(odf_conversion_context *odf_context)
: impl_(new odf_table_context::Impl(odf_context))
//for embedded need styles_context ??? todooo
{
}
odf_table_context::~odf_table_context()
{
}
void odf_table_context::start_table(office_element_ptr &elm, bool styled)
{
table_table * table = dynamic_cast<table_table *>(elm.get());
if (!table)return;
odf_table_state state;
state.table.elm = elm;
if (styled)
{
odf_style_state * style_state = impl_->styles_context()->last_state(style_family::Table);
if (style_state)
{
state.table.style_elm = style_state->get_office_element();
state.table.style_name = style_state->get_name();
table->table_table_attlist_.table_style_name_ = state.table.style_name;
}
}
impl_->start_table(state);
}
void odf_table_context::end_table()
{
impl_->end_table();
}
void odf_table_context::start_row(office_element_ptr &elm, bool styled)
{
if (impl_->empty()) return;
table_table_row * row = dynamic_cast<table_table_row *>(elm.get());;
if (!row)return;
odf_element_state state;
state.elm = elm;
if (styled)
{
odf_style_state * style_state = impl_->styles_context()->last_state(style_family::TableRow);
if (style_state)
{
state.style_elm = style_state->get_office_element();
state.style_name = style_state->get_name();
row->table_table_row_attlist_.table_style_name_ = state.style_name;
}
}
impl_->current_table().rows.push_back(state);
impl_->current_table().current_column =0;
impl_->current_table().current_row ++;
}
void odf_table_context::end_row()
{
if (impl_->empty()) return;
}
void odf_table_context::add_column(office_element_ptr &elm, bool styled)
{
if (impl_->empty()) return;
table_table_column * column = dynamic_cast<table_table_column *>(elm.get());;
if (!column)return;
odf_element_state state;
state.elm = elm;
if (styled)
{
odf_style_state * style_state = impl_->styles_context()->last_state(style_family::TableColumn);
if (style_state)
{
state.style_elm = style_state->get_office_element();
state.style_name = style_state->get_name();
column->table_table_column_attlist_.table_style_name_ = state.style_name;
}
}
impl_->current_table().columns.push_back(state);
}
void odf_table_context::set_column_width(double width)
{
if (impl_->empty()) return;
if (impl_->current_table().columns.size() < 1)return;
style *style_ = dynamic_cast<style*>(impl_->current_table().columns.back().style_elm.get());
if (style_ == NULL) return;
style_table_column_properties *properties = style_->style_content_.get_style_table_column_properties();
if (properties == NULL) return;
if (width >= 0)
{
properties->style_table_column_properties_attlist_.style_column_width_ = length(length(width,length::pt).get_value_unit(length::cm),length::cm);
properties->style_table_column_properties_attlist_.style_use_optimal_column_width_ = false;
}
else
properties->style_table_column_properties_attlist_.style_use_optimal_column_width_ = true;
}
void odf_table_context::start_cell(office_element_ptr &elm, bool styled)
{
if (impl_->empty()) return;
table_table_cell * cell = dynamic_cast<table_table_cell *>(elm.get());;
if (!cell)return;
odf_element_state state;
state.elm = elm;
if (styled)
{
odf_style_state * style_state = impl_->styles_context()->last_state(style_family::TableCell);
if (style_state)
{
state.style_elm = style_state->get_office_element();
state.style_name = style_state->get_name();
cell->table_table_cell_attlist_.table_style_name_ = state.style_name;
}
}
impl_->current_table().cells.push_back(state);
impl_->current_table().current_column++;
}
void odf_table_context::set_cell_spanned(int spanned)
{
if (impl_->empty()) return;
if (impl_->current_table().cells.size() < 1)return;
if (spanned < 1)return;
table_table_cell * cell = dynamic_cast<table_table_cell *>(impl_->current_table().cells.back().elm.get());;
if (!cell)return;
cell->table_table_cell_attlist_extra_.table_number_columns_spanned_ = spanned;
impl_->current_table().current_column += spanned-1;
//cell_elm->table_table_cell_attlist_extra_.table_number_rows_spanned_ = spanned_rows;
}
void odf_table_context::end_cell()
{
if (impl_->empty()) return;
}
}
}
\ No newline at end of file
#pragma once
#include <string>
#include <vector>
#include <cpdoccore/CPOptional.h>
#include "office_elements.h"
#include "office_elements_create.h"
namespace cpdoccore {
namespace odf
{
class odf_conversion_context;
class odf_style_context;
class odf_table_context
{
public:
odf_table_context(odf_conversion_context *odf_context);
~odf_table_context();
void set_styles_context(odf_style_context* styles_context);
void start_table(office_element_ptr &elm, bool styled = false);
void end_table();
void start_row(office_element_ptr &elm, bool styled = false);
void end_row();
void start_cell(office_element_ptr &elm, bool styled = false);
void set_cell_spanned(int spanned);
void end_cell();
void add_column(office_element_ptr &elm, bool styled = false);
void set_column_width(double width);
private:
class Impl;
_CP_PTR(Impl) impl_;
};
}
}
\ No newline at end of file
......@@ -73,13 +73,13 @@ void odt_conversion_context::end_document()
root_document_->add_child_element(sections_[i].elm);
}
sections_.clear();
//add last paragraphs to root
for (long i=0; i< current_paragraphs_.size(); i++)
//add last elements to root
for (long i=0; i< current_root_elements_.size(); i++)
{
root_document_->add_child_element(current_paragraphs_[i]);
root_document_->add_child_element(current_root_elements_[i]);
}
current_paragraphs_.clear();
//current_level_.pop_back();
current_root_elements_.clear();
odf_conversion_context::end_document();
}
......@@ -97,6 +97,10 @@ odf_comment_context* odt_conversion_context::comment_context()
{
return &comment_context_;
}
odf_table_context* odt_conversion_context::table_context()
{
return &table_context_;
}
odf_text_context* odt_conversion_context::text_context()
{
......@@ -144,6 +148,17 @@ void odt_conversion_context::add_text_content(std::wstring & text)
text_context()->add_text_content(text);
}
void odt_conversion_context::add_to_root()
{
if (text_context_.size() >0) return; // íå root element (embedded) ????
if (comment_context_.is_started()) return;
if (text_context()->current_level_.size() > 1) return;
office_element_ptr & elm = text_context()->current_level_.back();
current_root_elements_.push_back(elm);
}
void odt_conversion_context::start_drawings()
{
odf_drawing_context_ptr new_drawing_context_ = boost::shared_ptr<odf_drawing_context>(new odf_drawing_context(this));
......@@ -160,8 +175,8 @@ void odt_conversion_context::end_drawings()
office_element_ptr & elm = drawing_context()->get_root_element();
if (elm )
{
//if (current_paragraphs_.size() >0)
// current_paragraphs_.back()->add_child_element(elm);
//if (current_root_elements_.size() >0)
// current_root_elements_.back()->add_child_element(elm);
text_context()->current_level_.back()->add_child_element(elm);
}
......@@ -174,14 +189,11 @@ void odt_conversion_context::start_paragraph(bool styled)
{
text_context()->start_paragraph(styled);
if (text_context_.size() < 1 && !comment_context_.is_started())
{
office_element_ptr & paragr_elm = text_context()->current_level_.back();
//current_level_.back()->add_child_element(paragr_elm);
//current_level_.push_back(paragr_elm);
current_paragraphs_.push_back(paragr_elm);
}
add_to_root();
}
void odt_conversion_context::start_hyperlink(std::wstring ref)
{
office_element_ptr hyperlink_elm;
......@@ -320,11 +332,11 @@ void odt_conversion_context::end_paragraph()
if (sections_.size() > 0 && sections_.back().empty)
{
for (long i=0; i< current_paragraphs_.size(); i++)
for (long i=0; i< current_root_elements_.size(); i++)
{
sections_.back().elm->add_child_element(current_paragraphs_[i]);
sections_.back().elm->add_child_element(current_root_elements_[i]);
}
current_paragraphs_.clear();
current_root_elements_.clear();
sections_.back().empty = false;
}
......@@ -459,33 +471,89 @@ void odt_conversion_context::end_drop_cap()
drop_cap_state_.clear();
}
///////////////////////////////////////
void odt_conversion_context::start_table()
void odt_conversion_context::start_table(bool styled)
{
office_element_ptr elm;
create_element(L"table", L"table",elm,this);
table_context()->start_table(elm, styled);
text_context()->start_element(elm);
add_to_root();
}
void odt_conversion_context::start_table_columns()
{
office_element_ptr elm;
create_element(L"table", L"table-columns",elm,this);
text_context()->start_element(elm);
}
void odt_conversion_context::add_table_column()
void odt_conversion_context::add_table_column(double width)
{
office_element_ptr elm;
create_element(L"table", L"table-column",elm,this);
styles_context()->create_style(L"",odf::style_family::TableColumn, true, false, -1);
table_context()->add_column(elm, true);
table_context()->set_column_width(width);
text_context()->start_element(elm); // äëÿ ñâÿçè ýëìåíòîâ
text_context()->end_element();
}
void odt_conversion_context::end_table_columns()
{
text_context()->end_element();
}
void odt_conversion_context::start_table_header_rows()
{
office_element_ptr elm;
create_element(L"table", L"table-header-rows",elm,this);
text_context()->start_element(elm);
}
void odt_conversion_context::end_table_header_rows()
{
text_context()->end_element();
}
void odt_conversion_context::start_table_row()
void odt_conversion_context::start_table_row(bool styled)
{
office_element_ptr elm;
create_element(L"table", L"table-row",elm,this);
if (styled)
{
styles_context()->create_style(L"",odf::style_family::TableRow, true, false, -1);
}
text_context()->start_element(elm);
table_context()->start_row(elm, styled);
}
void odt_conversion_context::start_table_cell()
void odt_conversion_context::start_table_cell(int col, int spanned, bool styled)
{
office_element_ptr elm;
create_element(L"table", L"table-cell",elm,this);
table_context()->start_cell(elm, styled);
text_context()->start_element(elm);
table_context()->set_cell_spanned(spanned);
}
void odt_conversion_context::end_table_cell()
{
table_context()->end_cell();
text_context()->end_element();
}
void odt_conversion_context::end_table_row()
{
table_context()->end_row();
text_context()->end_element();
}
void odt_conversion_context::end_table()
{
table_context()->end_table();
text_context()->end_element();
}
}
}
......@@ -39,7 +39,8 @@ public:
virtual odf_drawing_context * drawing_context();
virtual odf_text_context * text_context();
odf_comment_context * comment_context();
odf_comment_context * comment_context();
odf_table_context * table_context();
void start_drawings();
void end_drawings();
......@@ -75,12 +76,14 @@ public:
void start_comment_content();
void end_comment_content();
void start_table();
void start_table(bool styled = false);
void start_table_columns();
void add_table_column();
void add_table_column(double width = -1);
void end_table_columns();
void start_table_row();
void start_table_cell();
void start_table_header_rows();
void end_table_header_rows();
void start_table_row(bool styled = false);
void start_table_cell(int col, int spanned = 1, bool styled = false);
void end_table_cell();
void end_table_row();
void end_table();
......@@ -89,17 +92,19 @@ private:
office_text* root_text_;
office_element_ptr root_document_;
odf_text_context* main_text_context_;
odf_page_layout_context page_layout_context_;
odf_table_context table_context_;
odf_comment_context comment_context_;
odf_text_context* main_text_context_;
std::vector<odf_drawing_context_ptr> drawing_context_;
std::vector<odf_text_context_ptr> text_context_;//for embedded
std::vector<office_element_ptr> current_paragraphs_; // for section, if needed
std::vector<office_element_ptr> current_root_elements_; // for section, if needed
std::vector<odt_section_state> sections_;
odf_table_context table_context_;
void add_to_root();
struct _field_state
{
......
......@@ -739,7 +739,8 @@ void table_rows_no_group::add_child_element(office_element_ptr & child_element)
else if (type == typeTableTableHeaderRows)
{
was_header_ = true;
table_table_header_rows_ = child_element;
if (table_table_header_rows_ == NULL)
table_table_header_rows_ = child_element;
}
}
void table_rows_no_group::serialize(std::wostream & _Wostream)
......
......@@ -209,6 +209,21 @@ void DocxConverter::convert(OOX::WritingElement *oox_unknown)
OOX::Logic::CTbl* pTable= static_cast<OOX::Logic::CTbl*>(oox_unknown);
convert(pTable);
}break;
case OOX::et_w_tr:
{
OOX::Logic::CTr* pRow= static_cast<OOX::Logic::CTr*>(oox_unknown);
convert(pRow);
}break;
case OOX::et_w_tc:
{
OOX::Logic::CTc* pCell= static_cast<OOX::Logic::CTc*>(oox_unknown);
convert(pCell);
}break;
case OOX::et_w_proofErr:
case OOX::et_w_proofState:
{
//
}break;
default:
{
OoxConverter::convert(oox_unknown);
......@@ -1465,13 +1480,21 @@ void DocxConverter::convert(OOX::Logic::CTbl *oox_table)
{
if (oox_table == NULL) return;
odt_context->start_table();
odt_context->start_table(convert(oox_table->m_oTableProperties));
convert(oox_table->m_oTblGrid.GetPointer());
for (int i =0 ; i < oox_table->m_arrItems.GetSize(); i++)
{
convert(oox_table->m_arrItems[i]);
switch(oox_table->m_arrItems[i]->getType())
{
case OOX::et_w_tblPr:
{
//skip
}break;
default:
convert(oox_table->m_arrItems[i]);
}
}
odt_context->end_table();
......@@ -1484,8 +1507,14 @@ void DocxConverter::convert(OOX::Logic::CTblGrid *oox_table_grid)
//nullable<OOX::Logic::CTblGridChange > m_oTblGridChange;
for (int i =0 ; i < oox_table_grid->m_arrGridCol.GetSize(); i++)
{
//m_oW
odt_context->add_table_column(/*10*/);
double width = -1;
if (oox_table_grid->m_arrGridCol[i].m_oW.IsInit())
{
width = oox_table_grid->m_arrGridCol[i].m_oW->ToPoints();
}
odt_context->add_table_column(width);
}
odt_context->end_table_columns();
}
......@@ -1494,28 +1523,124 @@ void DocxConverter::convert(OOX::Logic::CTr *oox_table_row)
{
if (oox_table_row == NULL) return;
bool styled = oox_table_row->m_oTableRowProperties ? true : false;
bool is_header = false;
if (styled && oox_table_row->m_oTableRowProperties->m_oTblHeader.IsInit()
&& oox_table_row->m_oTableRowProperties->m_oTblHeader->m_oVal.ToBool() )is_header = true;
if (is_header)odt_context->start_table_header_rows();
odt_context->start_table_row(styled);
convert(oox_table_row->m_oTableRowProperties);
for (int i =0 ; i < oox_table_row->m_arrItems.GetSize(); i++)
{
odt_context->start_table_row();
convert(oox_table_row->m_arrItems[i]);
odt_context->end_table_row();
switch(oox_table_row->m_arrItems[i]->getType())
{
case OOX::et_w_trPr:
{
//skip
}break;
default:
convert(oox_table_row->m_arrItems[i]);
}
}
odt_context->end_table_row();
if (is_header)odt_context->end_table_header_rows();
}
void DocxConverter::convert(OOX::Logic::CTc *oox_table_cell)
{
if (oox_table_cell == NULL) return;
odt_context->start_table_cell();
int spanned = 1;
if (oox_table_cell->m_oTableCellProperties && oox_table_cell->m_oTableCellProperties->m_oGridSpan.IsInit() && oox_table_cell->m_oTableCellProperties->m_oGridSpan->m_oVal.IsInit())
spanned = oox_table_cell->m_oTableCellProperties->m_oGridSpan->m_oVal->GetValue();
odt_context->start_table_cell( oox_table_cell->m_nNumCol, spanned, convert(oox_table_cell->m_oTableCellProperties));
for (int i =0 ; i < oox_table_cell->m_arrItems.GetSize(); i++)
{
convert(oox_table_cell->m_arrItems[i]);
switch(oox_table_cell->m_arrItems[i]->getType())
{
case OOX::et_w_tcPr:
{
//skip
}break;
default:
convert(oox_table_cell->m_arrItems[i]);
}
}
odt_context->end_table_cell();
}
void DocxConverter::convert(OOX::Logic::CTableCellProperties *oox_table_cell_pr)
bool DocxConverter::convert(OOX::Logic::CTableProperty *oox_table_pr)
{
if (oox_table_pr == NULL) return false;
odt_context->styles_context()->create_style(L"",odf::style_family::Table, true, false, -1);
odf::style_table_properties * table_properties = odt_context->styles_context()->last_state().get_table_properties();
//odf::style_table_cell_properties * table_cell_properties = odt_context->styles_context()->last_state().get_table_cell_properties();
return true;
}
void DocxConverter::convert(OOX::Logic::CTableRowProperties *oox_table_row_pr)
{
if (oox_table_cell_pr == NULL) return;
if (oox_table_row_pr == NULL) return;
odf::style_table_row_properties * table_row_properties = odt_context->styles_context()->last_state().get_table_row_properties();
if (table_row_properties == NULL) return;
if (oox_table_row_pr->m_oTblHeight.IsInit())
{
_CP_OPT(odf::length) length;
convert(static_cast<SimpleTypes::CUniversalMeasure *>(oox_table_row_pr->m_oTblHeight->m_oVal.GetPointer()), length);
if (oox_table_row_pr->m_oTblHeight->m_oHRule.IsInit())
{
switch(oox_table_row_pr->m_oTblHeight->m_oHRule->GetValue())
{
case SimpleTypes::heightruleAtLeast:
table_row_properties->style_table_row_properties_attlist_.style_min_row_height_ = length; break;
case SimpleTypes::heightruleExact:
table_row_properties->style_table_row_properties_attlist_.style_row_height_ = length; break;
case SimpleTypes::heightruleAuto:
table_row_properties->style_table_row_properties_attlist_.style_use_optimal_row_height_ = true; break;
}
}
}
if (oox_table_row_pr->m_oCnfStyle.IsInit())
{
}
//nullable<ComplexTypes::Word::COnOff2<SimpleTypes::onoffTrue> > m_oCantSplit;
//nullable<ComplexTypes::Word::CCnf > m_oCnfStyle;
//nullable<ComplexTypes::Word::CTrackChange > m_oDel;
//nullable<ComplexTypes::Word::CDecimalNumber > m_oDivId;
//nullable<ComplexTypes::Word::CDecimalNumber > m_oGridAfter;
//nullable<ComplexTypes::Word::CDecimalNumber > m_oGridBefore;
//nullable<ComplexTypes::Word::COnOff2<SimpleTypes::onoffTrue> > m_oHidden;
//nullable<ComplexTypes::Word::CTrackChange > m_oIns;
//nullable<ComplexTypes::Word::CJcTable > m_oJc;
//nullable<ComplexTypes::Word::CTblWidth > m_oTblCellSpacing;
//nullable<OOX::Logic::CTrPrChange > m_oTrPrChange;
//nullable<ComplexTypes::Word::CTblWidth > m_oWAfter;
//nullable<ComplexTypes::Word::CTblWidth > m_oWBefore;
}
bool DocxConverter::convert(OOX::Logic::CTableCellProperties *oox_table_cell_pr)
{
if (oox_table_cell_pr == NULL) return false;
odt_context->styles_context()->create_style(L"",odf::style_family::TableCell, true, false, -1);
odf::style_table_cell_properties * table_cell_properties = odt_context->styles_context()->last_state().get_table_cell_properties();
//nullable<ComplexTypes::Word::CTrackChange > m_oCellDel;
//nullable<ComplexTypes::Word::CTrackChange > m_oCellIns;
//nullable<ComplexTypes::Word::CCellMergeTrackChange > m_oCellMerge;
......@@ -1534,6 +1659,8 @@ void DocxConverter::convert(OOX::Logic::CTableCellProperties *oox_table_cell_pr)
//nullable<ComplexTypes::Word::CTextDirection > m_oTextDirection;
//nullable<ComplexTypes::Word::CVerticalJc > m_oVAlign;
//nullable<ComplexTypes::Word::CVMerge > m_oVMerge;
return true;
}
}
......
......@@ -38,8 +38,10 @@ namespace OOX
class CTblGrid;
class CTr;
class CTc;
class CTableCellProperties;
class CTblGridCol;
class CTableCellProperties;
class CTableProperty;
class CTableRowProperties;
}
......@@ -140,7 +142,10 @@ namespace Oox2Odf
void convert(OOX::Logic::CTblGrid *oox_table_grid);
void convert(OOX::Logic::CTr *oox_table_row);
void convert(OOX::Logic::CTc *oox_table_cell);
void convert(OOX::Logic::CTableCellProperties *oox_table_cell_pr);
bool convert(OOX::Logic::CTableCellProperties *oox_table_cell_pr);
bool convert(OOX::Logic::CTableProperty *oox_table_pr);
void convert(OOX::Logic::CTableRowProperties *oox_table_row_pr);
//--------------------------------------------------------------------------------
bool m_bKeepNextParagraph;
};
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment