From cae53cf14d59bc2fa6317c9493197804928d6e5e Mon Sep 17 00:00:00 2001 From: Tomaž Vajngerl Date: Sat, 27 Apr 2019 11:18:42 +0900 Subject: Decouple reading/writing of Color into GenericTypeSerializer MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This adds GenericTypeSerializer, which is now responsible of serializing the Color into a stream (other types will follow), but only for the older version of the binary format. The new version we just write the sal_UInt32 mValue directly. This is a start of decoupling the serialization of generic types in tools and vcl module from the actual type, so we can in the future replace those with basegfx variant. Change-Id: I92738e7c178cac5cbca882dcbe45c80cc8269466 Reviewed-on: https://gerrit.libreoffice.org/71404 Tested-by: Jenkins Reviewed-by: Tomaž Vajngerl --- tools/Library_tl.mk | 1 + tools/qa/cppunit/test_color.cxx | 18 ----- tools/source/generic/color.cxx | 96 ----------------------- tools/source/stream/GenericTypeSerializer.cxx | 105 ++++++++++++++++++++++++++ 4 files changed, 106 insertions(+), 114 deletions(-) create mode 100644 tools/source/stream/GenericTypeSerializer.cxx (limited to 'tools') diff --git a/tools/Library_tl.mk b/tools/Library_tl.mk index d6705d0b5925..9f3df2dc825f 100644 --- a/tools/Library_tl.mk +++ b/tools/Library_tl.mk @@ -73,6 +73,7 @@ $(eval $(call gb_Library_add_exception_objects,tl,\ tools/source/ref/ref \ tools/source/stream/stream \ tools/source/stream/vcompat \ + tools/source/stream/GenericTypeSerializer \ tools/source/string/tenccvt \ tools/source/zcodec/zcodec \ tools/source/xml/XmlWriter \ diff --git a/tools/qa/cppunit/test_color.cxx b/tools/qa/cppunit/test_color.cxx index 8ecf03572295..011fbcbdfa65 100644 --- a/tools/qa/cppunit/test_color.cxx +++ b/tools/qa/cppunit/test_color.cxx @@ -23,7 +23,6 @@ class Test: public CppUnit::TestFixture public: void testVariables(); void test_asRGBColor(); - void test_readAndWriteStream(); void test_ApplyTintOrShade(); void testGetColorError(); void testInvert(); @@ -32,7 +31,6 @@ public: CPPUNIT_TEST_SUITE(Test); CPPUNIT_TEST(testVariables); CPPUNIT_TEST(test_asRGBColor); - CPPUNIT_TEST(test_readAndWriteStream); CPPUNIT_TEST(test_ApplyTintOrShade); CPPUNIT_TEST(testGetColorError); CPPUNIT_TEST(testInvert); @@ -107,22 +105,6 @@ void Test::test_asRGBColor() CPPUNIT_ASSERT_EQUAL(OUString("ffffff"), aColor.AsRGBHexString()); } -void Test::test_readAndWriteStream() -{ - SvMemoryStream aStream; - Color aReadColor; - - WriteColor(aStream, Color(0x12, 0x34, 0x56)); - - aStream.Seek(STREAM_SEEK_TO_BEGIN); - - ReadColor(aStream, aReadColor); - - CPPUNIT_ASSERT_EQUAL(sal_uInt8(0x12), aReadColor.GetRed()); - CPPUNIT_ASSERT_EQUAL(sal_uInt8(0x34), aReadColor.GetGreen()); - CPPUNIT_ASSERT_EQUAL(sal_uInt8(0x56), aReadColor.GetBlue()); -} - OUString createTintShade(sal_uInt8 nR, sal_uInt8 nG, sal_uInt8 nB, OUString const & sReference, sal_Int16 nTintShade) { Color aColor(nR, nG, nB); diff --git a/tools/source/generic/color.cxx b/tools/source/generic/color.cxx index f79342d37862..a06c6e84a964 100644 --- a/tools/source/generic/color.cxx +++ b/tools/source/generic/color.cxx @@ -25,7 +25,6 @@ #include #include -#include #include #include @@ -184,18 +183,6 @@ Color Color::HSBtoRGB( sal_uInt16 nHue, sal_uInt16 nSat, sal_uInt16 nBri ) return Color( cR, cG, cB ); } -SvStream& Color::Read( SvStream& rIStm ) -{ - rIStm.ReadUInt32(mValue); - return rIStm; -} - -SvStream& Color::Write( SvStream& rOStm ) const -{ - rOStm.WriteUInt32(mValue); - return rOStm; -} - OUString Color::AsRGBHexString() const { std::stringstream ss; @@ -203,72 +190,6 @@ OUString Color::AsRGBHexString() const return OUString::createFromAscii(ss.str().c_str()); } -#define COL_NAME_USER (sal_uInt16(0x8000)) - -SvStream& ReadColor( SvStream& rIStream, Color& rColor ) -{ - sal_uInt16 nColorName(0); - - rIStream.ReadUInt16( nColorName ); - - if ( nColorName & COL_NAME_USER ) - { - sal_uInt16 nRed; - sal_uInt16 nGreen; - sal_uInt16 nBlue; - - rIStream.ReadUInt16( nRed ); - rIStream.ReadUInt16( nGreen ); - rIStream.ReadUInt16( nBlue ); - - rColor = Color( nRed>>8, nGreen>>8, nBlue>>8 ); - } - else - { - static const Color aColAry[] = - { - COL_BLACK, // COL_BLACK - COL_BLUE, // COL_BLUE - COL_GREEN, // COL_GREEN - COL_CYAN, // COL_CYAN - COL_RED, // COL_RED - COL_MAGENTA, // COL_MAGENTA - COL_BROWN, // COL_BROWN - COL_GRAY, // COL_GRAY - COL_LIGHTGRAY, // COL_LIGHTGRAY - COL_LIGHTBLUE, // COL_LIGHTBLUE - COL_LIGHTGREEN, // COL_LIGHTGREEN - COL_LIGHTCYAN, // COL_LIGHTCYAN - COL_LIGHTRED, // COL_LIGHTRED - COL_LIGHTMAGENTA, // COL_LIGHTMAGENTA - COL_YELLOW, // COL_YELLOW - COL_WHITE, // COL_WHITE - COL_WHITE, // COL_MENUBAR - COL_BLACK, // COL_MENUBARTEXT - COL_WHITE, // COL_POPUPMENU - COL_BLACK, // COL_POPUPMENUTEXT - COL_BLACK, // COL_WINDOWTEXT - COL_WHITE, // COL_WINDOWWORKSPACE - COL_BLACK, // COL_HIGHLIGHT - COL_WHITE, // COL_HIGHLIGHTTEXT - COL_BLACK, // COL_3DTEXT - COL_LIGHTGRAY, // COL_3DFACE - COL_WHITE, // COL_3DLIGHT - COL_GRAY, // COL_3DSHADOW - COL_LIGHTGRAY, // COL_SCROLLBAR - COL_WHITE, // COL_FIELD - COL_BLACK // COL_FIELDTEXT - }; - - if ( nColorName < SAL_N_ELEMENTS( aColAry ) ) - rColor = aColAry[nColorName]; - else - rColor = COL_BLACK; - } - - return rIStream; -} - void Color::ApplyTintOrShade(sal_Int16 n100thPercent) { if (n100thPercent == 0) @@ -295,21 +216,4 @@ void Color::ApplyTintOrShade(sal_Int16 n100thPercent) B = sal_uInt8(std::lround(aBColor.getBlue() * 255.0)); } -SvStream& WriteColor( SvStream& rOStream, const Color& rColor ) -{ - sal_uInt16 nRed = rColor.GetRed(); - sal_uInt16 nGreen = rColor.GetGreen(); - sal_uInt16 nBlue = rColor.GetBlue(); - nRed = (nRed<<8) + nRed; - nGreen = (nGreen<<8) + nGreen; - nBlue = (nBlue<<8) + nBlue; - - rOStream.WriteUInt16( COL_NAME_USER ); - rOStream.WriteUInt16( nRed ); - rOStream.WriteUInt16( nGreen ); - rOStream.WriteUInt16( nBlue ); - - return rOStream; -} - /* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/tools/source/stream/GenericTypeSerializer.cxx b/tools/source/stream/GenericTypeSerializer.cxx new file mode 100644 index 000000000000..7e261350ad50 --- /dev/null +++ b/tools/source/stream/GenericTypeSerializer.cxx @@ -0,0 +1,105 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + * + * This file incorporates work covered by the following license notice: + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed + * with this work for additional information regarding copyright + * ownership. The ASF licenses this file to you under the Apache + * License, Version 2.0 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.apache.org/licenses/LICENSE-2.0 . + */ + +#include + +#include +#include + +namespace tools +{ +constexpr sal_uInt16 COL_NAME_USER = 0x8000; + +void GenericTypeSerializer::readColor(Color& rColor) +{ + sal_uInt16 nColorNameID(0); + + mrStream.ReadUInt16(nColorNameID); + + if (nColorNameID & COL_NAME_USER) + { + sal_uInt16 nRed; + sal_uInt16 nGreen; + sal_uInt16 nBlue; + + mrStream.ReadUInt16(nRed); + mrStream.ReadUInt16(nGreen); + mrStream.ReadUInt16(nBlue); + + rColor = Color(nRed >> 8, nGreen >> 8, nBlue >> 8); + } + else + { + static const std::vector staticColorArray = { + COL_BLACK, // COL_BLACK + COL_BLUE, // COL_BLUE + COL_GREEN, // COL_GREEN + COL_CYAN, // COL_CYAN + COL_RED, // COL_RED + COL_MAGENTA, // COL_MAGENTA + COL_BROWN, // COL_BROWN + COL_GRAY, // COL_GRAY + COL_LIGHTGRAY, // COL_LIGHTGRAY + COL_LIGHTBLUE, // COL_LIGHTBLUE + COL_LIGHTGREEN, // COL_LIGHTGREEN + COL_LIGHTCYAN, // COL_LIGHTCYAN + COL_LIGHTRED, // COL_LIGHTRED + COL_LIGHTMAGENTA, // COL_LIGHTMAGENTA + COL_YELLOW, // COL_YELLOW + COL_WHITE, // COL_WHITE + COL_WHITE, // COL_MENUBAR + COL_BLACK, // COL_MENUBARTEXT + COL_WHITE, // COL_POPUPMENU + COL_BLACK, // COL_POPUPMENUTEXT + COL_BLACK, // COL_WINDOWTEXT + COL_WHITE, // COL_WINDOWWORKSPACE + COL_BLACK, // COL_HIGHLIGHT + COL_WHITE, // COL_HIGHLIGHTTEXT + COL_BLACK, // COL_3DTEXT + COL_LIGHTGRAY, // COL_3DFACE + COL_WHITE, // COL_3DLIGHT + COL_GRAY, // COL_3DSHADOW + COL_LIGHTGRAY, // COL_SCROLLBAR + COL_WHITE, // COL_FIELD + COL_BLACK // COL_FIELDTEXT + }; + + if (nColorNameID < staticColorArray.size()) + rColor = staticColorArray[nColorNameID]; + else + rColor = COL_BLACK; + } +} + +void GenericTypeSerializer::writeColor(const Color& rColor) +{ + mrStream.WriteUInt16(COL_NAME_USER); + + sal_uInt16 nR = rColor.GetRed(); + sal_uInt16 nG = rColor.GetGreen(); + sal_uInt16 nB = rColor.GetBlue(); + + mrStream.WriteUInt16((nR << 8) + nR); + mrStream.WriteUInt16((nG << 8) + nG); + mrStream.WriteUInt16((nB << 8) + nB); +} + +} // end namespace tools + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ -- cgit