fdo#64956: editeng: fix RTF color table export

The editengine RTF export produces this:
{\colortbl\red255\green255\blue255;;}

... and then it proceeds to map COL_AUTO to \cf0 i.e. the "white" entry
that is the result of erroneously writing out the 0th entry regardless
of whether it is COL_AUTO or not.

Fix the color table export to always put COL_AUTO first (as the Writer
RTF export already does), and simplify the code a bit.

Change-Id: Ia8ce19f387e3627a1b4a26bcc723edcf5b1ffdf8
diff --git a/editeng/source/editeng/editdoc.cxx b/editeng/source/editeng/editdoc.cxx
index 61b4a36..64fe731 100644
--- a/editeng/source/editeng/editdoc.cxx
+++ b/editeng/source/editeng/editdoc.cxx
@@ -2958,45 +2958,6 @@ bool CharAttribList::DbgCheckAttribs() const
#endif


SvxColorList::SvxColorList()
{
}

SvxColorList::~SvxColorList()
{
    for ( sal_Int32 i = 0, n = aColorList.size(); i < n; ++i )
        delete aColorList[ i ];
    aColorList.clear();
}

sal_Int32 SvxColorList::GetId( const SvxColorItem& rColorItem )
{
    for ( sal_Int32 i = 0, n = aColorList.size(); i < n; ++i )
        if ( *aColorList[ i ] == rColorItem )
            return i;
    DBG_WARNING( "Color not found: GetId()" );
    return 0;
}

void SvxColorList::Insert( SvxColorItem* pItem, sal_Int32 nIndex )
{
    if ( nIndex >= (sal_Int32)aColorList.size() )
    {
        aColorList.push_back( pItem );
    }
    else
    {
        DummyColorList::iterator it = aColorList.begin();
        ::std::advance( it, nIndex );
        aColorList.insert( it, pItem );
    }
}

SvxColorItem* SvxColorList::GetObject( sal_Int32 nIndex )
{
    return ( nIndex >= (sal_Int32)aColorList.size() ) ? NULL : aColorList[ nIndex ];
}

EditEngineItemPool::EditEngineItemPool( bool bPersistenRefCounts )
    : SfxItemPool( "EditEngineItemPool", EE_ITEMS_START, EE_ITEMS_END,
                    aItemInfos, 0, bPersistenRefCounts )
diff --git a/editeng/source/editeng/editdoc.hxx b/editeng/source/editeng/editdoc.hxx
index a939241..1d8c5f9 100644
--- a/editeng/source/editeng/editdoc.hxx
+++ b/editeng/source/editeng/editdoc.hxx
@@ -131,22 +131,7 @@ public:

//  class SvxColorList


class SvxColorList
{
private:
    typedef std::vector<SvxColorItem*> DummyColorList;
    DummyColorList aColorList;

public:
            SvxColorList();
            ~SvxColorList();

    sal_Int32  GetId( const SvxColorItem& rColor );
    sal_Int32  Count() { return aColorList.size(); };
    void    Insert( SvxColorItem* pItem, sal_Int32 nIndex );
    SvxColorItem* GetObject( sal_Int32 nIndex );
};
typedef std::vector<Color> SvxColorList;


//  class ItemList
diff --git a/editeng/source/editeng/impedit.hxx b/editeng/source/editeng/impedit.hxx
index b225ac66..b1e64b1a 100644
--- a/editeng/source/editeng/impedit.hxx
+++ b/editeng/source/editeng/impedit.hxx
@@ -74,7 +74,6 @@

class EditView;
class EditEngine;
class SvxColorList;

class SvxSearchItem;
class SvxLRSpaceItem;
diff --git a/editeng/source/editeng/impedit4.cxx b/editeng/source/editeng/impedit4.cxx
index 77827e4..22ee40d 100644
--- a/editeng/source/editeng/impedit4.cxx
+++ b/editeng/source/editeng/impedit4.cxx
@@ -415,30 +415,40 @@ sal_uInt32 ImpEditEngine::WriteRTF( SvStream& rOutput, EditSelection aSel )

    // Write out ColorList  ...
    SvxColorList aColorList;
    // COL_AUTO should be the default color, always put it first
    aColorList.push_back(COL_AUTO);
    SvxColorItem const& rDefault(static_cast<SvxColorItem const&>(
                aEditDoc.GetItemPool().GetDefaultItem(EE_CHAR_COLOR)));
    if (rDefault.GetValue() != COL_AUTO) // is the default always AUTO?
    {
        aColorList.push_back(rDefault.GetValue());
    }
    sal_uInt32 i = 0;
    SvxColorItem* pColorItem = (SvxColorItem*)aEditDoc.GetItemPool().GetItem2( EE_CHAR_COLOR, i );
    SvxColorItem const* pColorItem = static_cast<SvxColorItem const*>(
            aEditDoc.GetItemPool().GetItem2( EE_CHAR_COLOR, i));
    while ( pColorItem )
    {
        sal_uInt32 nPos = i;
        if ( pColorItem->GetValue() == COL_AUTO )
            nPos = 0;
        aColorList.Insert( new SvxColorItem( *pColorItem ), nPos );
        pColorItem = (SvxColorItem*)aEditDoc.GetItemPool().GetItem2( EE_CHAR_COLOR, ++i );
        ++i;
        if ( pColorItem->GetValue() != COL_AUTO )
        {
            aColorList.push_back(pColorItem->GetValue());
        }
        pColorItem = static_cast<SvxColorItem const*>(
                aEditDoc.GetItemPool().GetItem2(EE_CHAR_COLOR, i));
    }
    aColorList.Insert( new SvxColorItem( (const SvxColorItem&)aEditDoc.GetItemPool().GetDefaultItem( EE_CHAR_COLOR) ), i );

    rOutput.WriteChar( '{' ).WriteCharPtr( OOO_STRING_SVTOOLS_RTF_COLORTBL );
    for ( j = 0; j < aColorList.Count(); j++ )
    for ( j = 0; j < aColorList.size(); j++ )
    {
        pColorItem = aColorList.GetObject( j );
        if ( !j || ( pColorItem->GetValue() != COL_AUTO ) )
        Color const color = aColorList[j];
        if (color != COL_AUTO) // auto is represented by "empty" element
        {
            rOutput.WriteCharPtr( OOO_STRING_SVTOOLS_RTF_RED );
            rOutput.WriteNumber( static_cast<sal_uInt32>(pColorItem->GetValue().GetRed()) );
            rOutput.WriteNumber( static_cast<sal_uInt32>(color.GetRed()) );
            rOutput.WriteCharPtr( OOO_STRING_SVTOOLS_RTF_GREEN );
            rOutput.WriteNumber( static_cast<sal_uInt32>(pColorItem->GetValue().GetGreen()) );
            rOutput.WriteNumber( static_cast<sal_uInt32>(color.GetGreen()) );
            rOutput.WriteCharPtr( OOO_STRING_SVTOOLS_RTF_BLUE );
            rOutput.WriteNumber( static_cast<sal_uInt32>(pColorItem->GetValue().GetBlue()) );
            rOutput.WriteNumber( static_cast<sal_uInt32>(color.GetBlue()) );
        }
        rOutput.WriteChar( ';' );
    }
@@ -784,7 +794,11 @@ void ImpEditEngine::WriteItemAsRTF( const SfxPoolItem& rItem, SvStream& rOutput,
        break;
        case EE_CHAR_COLOR:
        {
            sal_uInt32 n = rColorList.GetId( (const SvxColorItem&)rItem );
            SvxColorList::const_iterator const iter = ::std::find(
                    rColorList.begin(), rColorList.end(),
                    static_cast<SvxColorItem const&>(rItem).GetValue());
            assert(iter != rColorList.end());
            sal_uInt32 const n = iter - rColorList.begin();
            rOutput.WriteCharPtr( OOO_STRING_SVTOOLS_RTF_CF );
            rOutput.WriteNumber( n );
        }