crashtesting: assert on reimport of docx export of ooo102874-2.doc
[LibreOffice.git] / tools / source / stream / GenericTypeSerializer.cxx
blob3eefb008ea67927fe05892a34d6e07f6ffe1c136
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #include <tools/GenericTypeSerializer.hxx>
21 #include <sal/config.h>
22 #include <sal/log.hxx>
23 #include <vector>
25 namespace tools
27 constexpr sal_uInt16 COL_NAME_USER = 0x8000;
29 constexpr sal_Int32 RECT_EMPTY_VALUE_RIGHT_BOTTOM = -32767;
31 void GenericTypeSerializer::readColor(Color& rColor)
33 sal_uInt16 nColorNameID(0);
35 mrStream.ReadUInt16(nColorNameID);
37 if (nColorNameID & COL_NAME_USER)
39 sal_uInt16 nRed(0);
40 sal_uInt16 nGreen(0);
41 sal_uInt16 nBlue(0);
43 mrStream.ReadUInt16(nRed);
44 mrStream.ReadUInt16(nGreen);
45 mrStream.ReadUInt16(nBlue);
47 rColor = Color(nRed >> 8, nGreen >> 8, nBlue >> 8);
49 else
51 static const std::vector<Color> staticColorArray = {
52 COL_BLACK, // COL_BLACK
53 COL_BLUE, // COL_BLUE
54 COL_GREEN, // COL_GREEN
55 COL_CYAN, // COL_CYAN
56 COL_RED, // COL_RED
57 COL_MAGENTA, // COL_MAGENTA
58 COL_BROWN, // COL_BROWN
59 COL_GRAY, // COL_GRAY
60 COL_LIGHTGRAY, // COL_LIGHTGRAY
61 COL_LIGHTBLUE, // COL_LIGHTBLUE
62 COL_LIGHTGREEN, // COL_LIGHTGREEN
63 COL_LIGHTCYAN, // COL_LIGHTCYAN
64 COL_LIGHTRED, // COL_LIGHTRED
65 COL_LIGHTMAGENTA, // COL_LIGHTMAGENTA
66 COL_YELLOW, // COL_YELLOW
67 COL_WHITE, // COL_WHITE
68 COL_WHITE, // COL_MENUBAR
69 COL_BLACK, // COL_MENUBARTEXT
70 COL_WHITE, // COL_POPUPMENU
71 COL_BLACK, // COL_POPUPMENUTEXT
72 COL_BLACK, // COL_WINDOWTEXT
73 COL_WHITE, // COL_WINDOWWORKSPACE
74 COL_BLACK, // COL_HIGHLIGHT
75 COL_WHITE, // COL_HIGHLIGHTTEXT
76 COL_BLACK, // COL_3DTEXT
77 COL_LIGHTGRAY, // COL_3DFACE
78 COL_WHITE, // COL_3DLIGHT
79 COL_GRAY, // COL_3DSHADOW
80 COL_LIGHTGRAY, // COL_SCROLLBAR
81 COL_WHITE, // COL_FIELD
82 COL_BLACK // COL_FIELDTEXT
85 if (nColorNameID < staticColorArray.size())
86 rColor = staticColorArray[nColorNameID];
87 else
88 rColor = COL_BLACK;
92 void GenericTypeSerializer::writeColor(const Color& rColor)
94 mrStream.WriteUInt16(COL_NAME_USER);
96 sal_uInt16 nR = rColor.GetRed();
97 sal_uInt16 nG = rColor.GetGreen();
98 sal_uInt16 nB = rColor.GetBlue();
100 mrStream.WriteUInt16((nR << 8) + nR);
101 mrStream.WriteUInt16((nG << 8) + nG);
102 mrStream.WriteUInt16((nB << 8) + nB);
105 void GenericTypeSerializer::readPoint(Point& rPoint)
107 sal_Int32 nX(0);
108 sal_Int32 nY(0);
110 mrStream.ReadInt32(nX);
111 mrStream.ReadInt32(nY);
113 rPoint.setX(nX);
114 rPoint.setY(nY);
117 void GenericTypeSerializer::writePoint(const Point& rPoint)
119 mrStream.WriteInt32(rPoint.getX());
120 mrStream.WriteInt32(rPoint.getY());
123 void GenericTypeSerializer::readSize(Size& rSize)
125 sal_Int32 nWidth(0);
126 sal_Int32 nHeight(0);
128 mrStream.ReadInt32(nWidth);
129 mrStream.ReadInt32(nHeight);
131 rSize.setWidth(nWidth);
132 rSize.setHeight(nHeight);
134 // sanitize negative size dimensions
135 if (rSize.Width() < 0)
137 SAL_WARN("tools", "negative width");
138 rSize.setWidth(0);
140 if (rSize.Height() < 0)
142 SAL_WARN("tools", "negative height");
143 rSize.setHeight(0);
147 void GenericTypeSerializer::writeSize(const Size& rSize)
149 mrStream.WriteInt32(rSize.getWidth());
150 mrStream.WriteInt32(rSize.getHeight());
153 void GenericTypeSerializer::readRectangle(Rectangle& rRectangle)
155 sal_Int32 nLeft(0);
156 sal_Int32 nTop(0);
157 sal_Int32 nRight(0);
158 sal_Int32 nBottom(0);
160 mrStream.ReadInt32(nLeft);
161 mrStream.ReadInt32(nTop);
162 mrStream.ReadInt32(nRight);
163 mrStream.ReadInt32(nBottom);
165 if (nRight == RECT_EMPTY_VALUE_RIGHT_BOTTOM || nBottom == RECT_EMPTY_VALUE_RIGHT_BOTTOM)
167 rRectangle.SetEmpty();
169 else
171 rRectangle.SetLeft(nLeft);
172 rRectangle.SetTop(nTop);
173 rRectangle.SetRight(nRight);
174 rRectangle.SetBottom(nBottom);
178 void GenericTypeSerializer::writeRectangle(const Rectangle& rRectangle)
180 if (rRectangle.IsEmpty())
182 mrStream.WriteInt32(0);
183 mrStream.WriteInt32(0);
184 mrStream.WriteInt32(RECT_EMPTY_VALUE_RIGHT_BOTTOM);
185 mrStream.WriteInt32(RECT_EMPTY_VALUE_RIGHT_BOTTOM);
187 else
189 mrStream.WriteInt32(rRectangle.Left());
190 mrStream.WriteInt32(rRectangle.Top());
191 mrStream.WriteInt32(rRectangle.Right());
192 mrStream.WriteInt32(rRectangle.Bottom());
196 void GenericTypeSerializer::readFraction(Fraction& rFraction)
198 sal_Int32 nNumerator(0);
199 sal_Int32 nDenominator(0);
201 mrStream.ReadInt32(nNumerator);
202 mrStream.ReadInt32(nDenominator);
204 rFraction = Fraction(nNumerator, nDenominator);
207 void GenericTypeSerializer::writeFraction(Fraction const& rFraction)
209 if (!rFraction.IsValid())
211 SAL_WARN("tools.fraction", "'writeFraction()' write an invalid fraction");
212 mrStream.WriteInt32(0);
213 mrStream.WriteInt32(0);
215 else
217 mrStream.WriteInt32(rFraction.GetNumerator());
218 mrStream.WriteInt32(rFraction.GetDenominator());
222 } // end namespace tools
224 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */