Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
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/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
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 .
18 : */
19 :
20 :
21 : #include <string.h>
22 : #include <dxfreprd.hxx>
23 : #include <boost/scoped_ptr.hpp>
24 :
25 : //------------------DXFBoundingBox--------------------------------------------
26 :
27 :
28 0 : void DXFBoundingBox::Union(const DXFVector & rVector)
29 : {
30 0 : if (bEmpty==sal_True) {
31 0 : fMinX=rVector.fx;
32 0 : fMinY=rVector.fy;
33 0 : fMinZ=rVector.fz;
34 0 : fMaxX=rVector.fx;
35 0 : fMaxY=rVector.fy;
36 0 : fMaxZ=rVector.fz;
37 0 : bEmpty=sal_False;
38 : }
39 : else {
40 0 : if (fMinX>rVector.fx) fMinX=rVector.fx;
41 0 : if (fMinY>rVector.fy) fMinY=rVector.fy;
42 0 : if (fMinZ>rVector.fz) fMinZ=rVector.fz;
43 0 : if (fMaxX<rVector.fx) fMaxX=rVector.fx;
44 0 : if (fMaxY<rVector.fy) fMaxY=rVector.fy;
45 0 : if (fMaxZ<rVector.fz) fMaxZ=rVector.fz;
46 : }
47 0 : }
48 :
49 :
50 : //------------------DXFPalette------------------------------------------------
51 :
52 :
53 0 : DXFPalette::DXFPalette()
54 : {
55 : short i,j,nHue,nNSat,nVal,nC[3],nmax,nmed,nmin;
56 : sal_uInt8 nV;
57 :
58 0 : pRed =new sal_uInt8[256];
59 0 : pGreen=new sal_uInt8[256];
60 0 : pBlue =new sal_uInt8[256];
61 :
62 : // colors 0 - 9 (normal colors)
63 0 : SetColor(0, 0x00, 0x00, 0x00); // actually never being used
64 0 : SetColor(1, 0xff, 0x00, 0x00);
65 0 : SetColor(2, 0xff, 0xff, 0x00);
66 0 : SetColor(3, 0x00, 0xff, 0x00);
67 0 : SetColor(4, 0x00, 0xff, 0xff);
68 0 : SetColor(5, 0x00, 0x00, 0xff);
69 0 : SetColor(6, 0xff, 0x00, 0xff);
70 0 : SetColor(7, 0x0f, 0x0f, 0x0f); // actually white???
71 0 : SetColor(8, 0x80, 0x80, 0x80);
72 0 : SetColor(9, 0xc0, 0xc0, 0xc0);
73 :
74 : // colors 10 - 249
75 : // (Universal-Palette: 24 hues * 5 lightnesses * 2 saturations )
76 0 : i=10;
77 0 : for (nHue=0; nHue<24; nHue++) {
78 0 : for (nVal=5; nVal>=1; nVal--) {
79 0 : for (nNSat=0; nNSat<2; nNSat++) {
80 0 : nmax=((nHue+3)>>3)%3;
81 0 : j=nHue-(nmax<<3); if (j>4) j=j-24;
82 0 : if (j>=0) {
83 0 : nmed=(nmax+1)%3;
84 0 : nmin=(nmax+2)%3;
85 : }
86 : else {
87 0 : nmed=(nmax+2)%3;
88 0 : nmin=(nmax+1)%3;
89 0 : j=-j;
90 : }
91 0 : nC[nmin]=0;
92 0 : nC[nmed]=255*j/4;
93 0 : nC[nmax]=255;
94 0 : if (nNSat!=0) {
95 0 : for (j=0; j<3; j++) nC[j]=(nC[j]>>1)+128;
96 : }
97 0 : for (j=0; j<3; j++) nC[j]=nC[j]*nVal/5;
98 0 : SetColor((sal_uInt8)(i++),(sal_uInt8)nC[0],(sal_uInt8)nC[1],(sal_uInt8)nC[2]);
99 : }
100 : }
101 : }
102 :
103 : // Farben 250 - 255 (shades of gray)
104 0 : for (i=0; i<6; i++) {
105 0 : nV=(sal_uInt8)(i*38+65);
106 0 : SetColor((sal_uInt8)(250+i),nV,nV,nV);
107 : }
108 0 : }
109 :
110 :
111 0 : DXFPalette::~DXFPalette()
112 : {
113 0 : delete[] pBlue;
114 0 : delete[] pGreen;
115 0 : delete[] pRed;
116 0 : }
117 :
118 :
119 0 : void DXFPalette::SetColor(sal_uInt8 nIndex, sal_uInt8 nRed, sal_uInt8 nGreen, sal_uInt8 nBlue)
120 : {
121 0 : pRed[nIndex]=nRed;
122 0 : pGreen[nIndex]=nGreen;
123 0 : pBlue[nIndex]=nBlue;
124 0 : }
125 :
126 :
127 : //------------------DXFRepresentation-----------------------------------------
128 :
129 :
130 0 : DXFRepresentation::DXFRepresentation()
131 : {
132 0 : setTextEncoding(RTL_TEXTENCODING_IBM_437);
133 0 : setGlobalLineTypeScale(1.0);
134 0 : }
135 :
136 :
137 0 : DXFRepresentation::~DXFRepresentation()
138 : {
139 0 : }
140 :
141 :
142 0 : sal_Bool DXFRepresentation::Read( SvStream & rIStream, sal_uInt16 nMinPercent, sal_uInt16 nMaxPercent)
143 : {
144 : sal_Bool bRes;
145 :
146 0 : aTables.Clear();
147 0 : aBlocks.Clear();
148 0 : aEntities.Clear();
149 :
150 0 : boost::scoped_ptr<DXFGroupReader> pDGR(new DXFGroupReader( rIStream, nMinPercent, nMaxPercent ));
151 :
152 0 : pDGR->Read();
153 0 : while (pDGR->GetG()!=0 || strcmp(pDGR->GetS(),"EOF")!=0) {
154 0 : if (pDGR->GetG()==0 && strcmp(pDGR->GetS(),"SECTION")==0) {
155 0 : if (pDGR->Read()!=2) {
156 0 : pDGR->SetError();
157 0 : break;
158 : }
159 0 : if (strcmp(pDGR->GetS(),"HEADER" )==0) ReadHeader(*pDGR);
160 0 : else if (strcmp(pDGR->GetS(),"TABLES" )==0) aTables.Read(*pDGR);
161 0 : else if (strcmp(pDGR->GetS(),"BLOCKS" )==0) aBlocks.Read(*pDGR);
162 0 : else if (strcmp(pDGR->GetS(),"ENTITIES")==0) aEntities.Read(*pDGR);
163 0 : else pDGR->Read();
164 : }
165 0 : else pDGR->Read();
166 : }
167 :
168 0 : bRes=pDGR->GetStatus();
169 :
170 0 : pDGR.reset();
171 :
172 0 : if (bRes==sal_True && aBoundingBox.bEmpty==sal_True)
173 0 : CalcBoundingBox(aEntities,aBoundingBox);
174 :
175 0 : return bRes;
176 : }
177 :
178 :
179 0 : void DXFRepresentation::ReadHeader(DXFGroupReader & rDGR)
180 : {
181 :
182 0 : while (rDGR.GetG()!=0 || (strcmp(rDGR.GetS(),"EOF")!=0 && strcmp(rDGR.GetS(),"ENDSEC")!=0) )
183 : {
184 0 : if (rDGR.GetG()==9) {
185 0 : if (strcmp(rDGR.GetS(),"$EXTMIN")==0 ||
186 0 : strcmp(rDGR.GetS(),"$EXTMAX")==0)
187 : {
188 0 : DXFVector aVector;
189 0 : rDGR.SetF(10,0.0);
190 0 : rDGR.SetF(20,0.0);
191 0 : rDGR.SetF(30,0.0);
192 0 : do {
193 0 : rDGR.Read();
194 0 : } while (rDGR.GetG()!=9 && rDGR.GetG()!=0);
195 0 : aVector.fx=rDGR.GetF(10);
196 0 : aVector.fy=rDGR.GetF(20);
197 0 : aVector.fz=rDGR.GetF(30);
198 0 : aBoundingBox.Union(aVector);
199 : } else {
200 0 : if (strcmp(rDGR.GetS(),"$DWGCODEPAGE")==0)
201 : {
202 0 : rDGR.Read();
203 :
204 : // FIXME: we really need a whole table of
205 : // $DWGCODEPAGE to encodings mappings
206 0 : if ( (strcmp(rDGR.GetS(),"ANSI_932")==0) ||
207 0 : (strcmp(rDGR.GetS(),"ansi_932")==0) ||
208 0 : (strcmp(rDGR.GetS(),"DOS932")==0) ||
209 0 : (strcmp(rDGR.GetS(),"dos932")==0) )
210 : {
211 0 : setTextEncoding(RTL_TEXTENCODING_MS_932);
212 : }
213 : }
214 0 : else if (strcmp(rDGR.GetS(),"$LTSCALE")==0)
215 : {
216 0 : rDGR.Read();
217 0 : setGlobalLineTypeScale(getGlobalLineTypeScale() * rDGR.GetF());
218 : }
219 0 : else rDGR.Read();
220 : }
221 : }
222 0 : else rDGR.Read();
223 : }
224 0 : }
225 :
226 :
227 0 : void DXFRepresentation::CalcBoundingBox(const DXFEntities & rEntities,
228 : DXFBoundingBox & rBox)
229 : {
230 0 : DXFBasicEntity * pBE=rEntities.pFirst;
231 0 : while (pBE!=NULL) {
232 0 : switch (pBE->eType) {
233 : case DXF_LINE: {
234 0 : const DXFLineEntity * pE = (DXFLineEntity*)pBE;
235 0 : rBox.Union(pE->aP0);
236 0 : rBox.Union(pE->aP1);
237 0 : break;
238 : }
239 : case DXF_POINT: {
240 0 : const DXFPointEntity * pE = (DXFPointEntity*)pBE;
241 0 : rBox.Union(pE->aP0);
242 0 : break;
243 : }
244 : case DXF_CIRCLE: {
245 0 : const DXFCircleEntity * pE = (DXFCircleEntity*)pBE;
246 0 : DXFVector aP;
247 0 : aP=pE->aP0;
248 0 : aP.fx-=pE->fRadius;
249 0 : aP.fy-=pE->fRadius;
250 0 : rBox.Union(aP);
251 0 : aP=pE->aP0;
252 0 : aP.fx+=pE->fRadius;
253 0 : aP.fy+=pE->fRadius;
254 0 : rBox.Union(aP);
255 0 : break;
256 : }
257 : case DXF_ARC: {
258 0 : const DXFArcEntity * pE = (DXFArcEntity*)pBE;
259 0 : DXFVector aP;
260 0 : aP=pE->aP0;
261 0 : aP.fx-=pE->fRadius;
262 0 : aP.fy-=pE->fRadius;
263 0 : rBox.Union(aP);
264 0 : aP=pE->aP0;
265 0 : aP.fx+=pE->fRadius;
266 0 : aP.fy+=pE->fRadius;
267 0 : rBox.Union(aP);
268 0 : break;
269 : }
270 : case DXF_TRACE: {
271 0 : const DXFTraceEntity * pE = (DXFTraceEntity*)pBE;
272 0 : rBox.Union(pE->aP0);
273 0 : rBox.Union(pE->aP1);
274 0 : rBox.Union(pE->aP2);
275 0 : rBox.Union(pE->aP3);
276 0 : break;
277 : }
278 : case DXF_SOLID: {
279 0 : const DXFSolidEntity * pE = (DXFSolidEntity*)pBE;
280 0 : rBox.Union(pE->aP0);
281 0 : rBox.Union(pE->aP1);
282 0 : rBox.Union(pE->aP2);
283 0 : rBox.Union(pE->aP3);
284 0 : break;
285 : }
286 : case DXF_TEXT: {
287 : //const DXFTextEntity * pE = (DXFTextEntity*)pBE;
288 : //???
289 0 : break;
290 : }
291 : case DXF_SHAPE: {
292 : //const DXFShapeEntity * pE = (DXFShapeEntity*)pBE;
293 : //???
294 0 : break;
295 : }
296 : case DXF_INSERT: {
297 0 : const DXFInsertEntity * pE = (DXFInsertEntity*)pBE;
298 : DXFBlock * pB;
299 0 : DXFBoundingBox aBox;
300 0 : DXFVector aP;
301 0 : pB=aBlocks.Search(pE->m_sName);
302 0 : if (pB==NULL) break;
303 0 : CalcBoundingBox(*pB,aBox);
304 0 : if (aBox.bEmpty==sal_True) break;
305 0 : aP.fx=(aBox.fMinX-pB->aBasePoint.fx)*pE->fXScale+pE->aP0.fx;
306 0 : aP.fy=(aBox.fMinY-pB->aBasePoint.fy)*pE->fYScale+pE->aP0.fy;
307 0 : aP.fz=(aBox.fMinZ-pB->aBasePoint.fz)*pE->fZScale+pE->aP0.fz;
308 0 : rBox.Union(aP);
309 0 : aP.fx=(aBox.fMaxX-pB->aBasePoint.fx)*pE->fXScale+pE->aP0.fx;
310 0 : aP.fy=(aBox.fMaxY-pB->aBasePoint.fy)*pE->fYScale+pE->aP0.fy;
311 0 : aP.fz=(aBox.fMaxZ-pB->aBasePoint.fz)*pE->fZScale+pE->aP0.fz;
312 0 : rBox.Union(aP);
313 0 : break;
314 : }
315 : case DXF_ATTDEF: {
316 : //const DXFAttDefEntity * pE = (DXFAttDefEntity*)pBE;
317 : //???
318 0 : break;
319 : }
320 : case DXF_ATTRIB: {
321 : //const DXFAttribEntity * pE = (DXFAttribEntity*)pBE;
322 : //???
323 0 : break;
324 : }
325 : case DXF_VERTEX: {
326 0 : const DXFVertexEntity * pE = (DXFVertexEntity*)pBE;
327 0 : rBox.Union(pE->aP0);
328 0 : break;
329 : }
330 : case DXF_3DFACE: {
331 0 : const DXF3DFaceEntity * pE = (DXF3DFaceEntity*)pBE;
332 0 : rBox.Union(pE->aP0);
333 0 : rBox.Union(pE->aP1);
334 0 : rBox.Union(pE->aP2);
335 0 : rBox.Union(pE->aP3);
336 0 : break;
337 : }
338 : case DXF_DIMENSION: {
339 0 : const DXFDimensionEntity * pE = (DXFDimensionEntity*)pBE;
340 : DXFBlock * pB;
341 0 : DXFBoundingBox aBox;
342 0 : DXFVector aP;
343 0 : pB = aBlocks.Search(pE->m_sPseudoBlock);
344 0 : if (pB==NULL) break;
345 0 : CalcBoundingBox(*pB,aBox);
346 0 : if (aBox.bEmpty==sal_True) break;
347 0 : aP.fx=aBox.fMinX-pB->aBasePoint.fx;
348 0 : aP.fy=aBox.fMinY-pB->aBasePoint.fy;
349 0 : aP.fz=aBox.fMinZ-pB->aBasePoint.fz;
350 0 : rBox.Union(aP);
351 0 : aP.fx=aBox.fMaxX-pB->aBasePoint.fx;
352 0 : aP.fy=aBox.fMaxY-pB->aBasePoint.fy;
353 0 : aP.fz=aBox.fMaxZ-pB->aBasePoint.fz;
354 0 : rBox.Union(aP);
355 0 : break;
356 : }
357 : case DXF_POLYLINE: {
358 : //const DXFAttribEntity * pE = (DXFAttribEntity*)pBE;
359 : //???
360 0 : break;
361 : }
362 : case DXF_SEQEND: {
363 : //const DXFAttribEntity * pE = (DXFAttribEntity*)pBE;
364 : //???
365 0 : break;
366 : }
367 : case DXF_HATCH :
368 0 : break;
369 : case DXF_LWPOLYLINE :
370 0 : break;
371 : }
372 0 : pBE=pBE->pSucc;
373 : }
374 0 : }
375 :
376 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|