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 :
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 : DXFGroupReader * pDGR;
145 : sal_Bool bRes;
146 :
147 0 : aTables.Clear();
148 0 : aBlocks.Clear();
149 0 : aEntities.Clear();
150 :
151 0 : pDGR = new DXFGroupReader( rIStream, nMinPercent, nMaxPercent );
152 :
153 0 : pDGR->Read();
154 0 : while (pDGR->GetG()!=0 || strcmp(pDGR->GetS(),"EOF")!=0) {
155 0 : if (pDGR->GetG()==0 && strcmp(pDGR->GetS(),"SECTION")==0) {
156 0 : if (pDGR->Read()!=2) {
157 0 : pDGR->SetError();
158 0 : break;
159 : }
160 0 : if (strcmp(pDGR->GetS(),"HEADER" )==0) ReadHeader(*pDGR);
161 0 : else if (strcmp(pDGR->GetS(),"TABLES" )==0) aTables.Read(*pDGR);
162 0 : else if (strcmp(pDGR->GetS(),"BLOCKS" )==0) aBlocks.Read(*pDGR);
163 0 : else if (strcmp(pDGR->GetS(),"ENTITIES")==0) aEntities.Read(*pDGR);
164 0 : else pDGR->Read();
165 : }
166 0 : else pDGR->Read();
167 : }
168 :
169 0 : bRes=pDGR->GetStatus();
170 :
171 0 : delete pDGR;
172 :
173 0 : if (bRes==sal_True && aBoundingBox.bEmpty==sal_True)
174 0 : CalcBoundingBox(aEntities,aBoundingBox);
175 :
176 0 : return bRes;
177 : }
178 :
179 :
180 0 : void DXFRepresentation::ReadHeader(DXFGroupReader & rDGR)
181 : {
182 :
183 0 : while (rDGR.GetG()!=0 || (strcmp(rDGR.GetS(),"EOF")!=0 && strcmp(rDGR.GetS(),"ENDSEC")!=0) )
184 : {
185 0 : if (rDGR.GetG()==9) {
186 0 : if (strcmp(rDGR.GetS(),"$EXTMIN")==0 ||
187 0 : strcmp(rDGR.GetS(),"$EXTMAX")==0)
188 : {
189 0 : DXFVector aVector;
190 0 : rDGR.SetF(10,0.0);
191 0 : rDGR.SetF(20,0.0);
192 0 : rDGR.SetF(30,0.0);
193 0 : do {
194 0 : rDGR.Read();
195 0 : } while (rDGR.GetG()!=9 && rDGR.GetG()!=0);
196 0 : aVector.fx=rDGR.GetF(10);
197 0 : aVector.fy=rDGR.GetF(20);
198 0 : aVector.fz=rDGR.GetF(30);
199 0 : aBoundingBox.Union(aVector);
200 : } else {
201 0 : if (strcmp(rDGR.GetS(),"$DWGCODEPAGE")==0)
202 : {
203 0 : rDGR.Read();
204 :
205 : // FIXME: we really need a whole table of
206 : // $DWGCODEPAGE to encodings mappings
207 0 : if ( (strcmp(rDGR.GetS(),"ANSI_932")==0) ||
208 0 : (strcmp(rDGR.GetS(),"ansi_932")==0) ||
209 0 : (strcmp(rDGR.GetS(),"DOS932")==0) ||
210 0 : (strcmp(rDGR.GetS(),"dos932")==0) )
211 : {
212 0 : setTextEncoding(RTL_TEXTENCODING_MS_932);
213 : }
214 : }
215 0 : else if (strcmp(rDGR.GetS(),"$LTSCALE")==0)
216 : {
217 0 : rDGR.Read();
218 0 : setGlobalLineTypeScale(getGlobalLineTypeScale() * rDGR.GetF());
219 : }
220 0 : else rDGR.Read();
221 : }
222 : }
223 0 : else rDGR.Read();
224 : }
225 0 : }
226 :
227 :
228 0 : void DXFRepresentation::CalcBoundingBox(const DXFEntities & rEntities,
229 : DXFBoundingBox & rBox)
230 : {
231 0 : DXFBasicEntity * pBE=rEntities.pFirst;
232 0 : while (pBE!=NULL) {
233 0 : switch (pBE->eType) {
234 : case DXF_LINE: {
235 0 : const DXFLineEntity * pE = (DXFLineEntity*)pBE;
236 0 : rBox.Union(pE->aP0);
237 0 : rBox.Union(pE->aP1);
238 0 : break;
239 : }
240 : case DXF_POINT: {
241 0 : const DXFPointEntity * pE = (DXFPointEntity*)pBE;
242 0 : rBox.Union(pE->aP0);
243 0 : break;
244 : }
245 : case DXF_CIRCLE: {
246 0 : const DXFCircleEntity * pE = (DXFCircleEntity*)pBE;
247 0 : DXFVector aP;
248 0 : aP=pE->aP0;
249 0 : aP.fx-=pE->fRadius;
250 0 : aP.fy-=pE->fRadius;
251 0 : rBox.Union(aP);
252 0 : aP=pE->aP0;
253 0 : aP.fx+=pE->fRadius;
254 0 : aP.fy+=pE->fRadius;
255 0 : rBox.Union(aP);
256 : break;
257 : }
258 : case DXF_ARC: {
259 0 : const DXFArcEntity * pE = (DXFArcEntity*)pBE;
260 0 : DXFVector aP;
261 0 : aP=pE->aP0;
262 0 : aP.fx-=pE->fRadius;
263 0 : aP.fy-=pE->fRadius;
264 0 : rBox.Union(aP);
265 0 : aP=pE->aP0;
266 0 : aP.fx+=pE->fRadius;
267 0 : aP.fy+=pE->fRadius;
268 0 : rBox.Union(aP);
269 : break;
270 : }
271 : case DXF_TRACE: {
272 0 : const DXFTraceEntity * pE = (DXFTraceEntity*)pBE;
273 0 : rBox.Union(pE->aP0);
274 0 : rBox.Union(pE->aP1);
275 0 : rBox.Union(pE->aP2);
276 0 : rBox.Union(pE->aP3);
277 0 : break;
278 : }
279 : case DXF_SOLID: {
280 0 : const DXFSolidEntity * pE = (DXFSolidEntity*)pBE;
281 0 : rBox.Union(pE->aP0);
282 0 : rBox.Union(pE->aP1);
283 0 : rBox.Union(pE->aP2);
284 0 : rBox.Union(pE->aP3);
285 0 : break;
286 : }
287 : case DXF_TEXT: {
288 : //const DXFTextEntity * pE = (DXFTextEntity*)pBE;
289 : //???
290 0 : break;
291 : }
292 : case DXF_SHAPE: {
293 : //const DXFShapeEntity * pE = (DXFShapeEntity*)pBE;
294 : //???
295 0 : break;
296 : }
297 : case DXF_INSERT: {
298 0 : const DXFInsertEntity * pE = (DXFInsertEntity*)pBE;
299 : DXFBlock * pB;
300 0 : DXFBoundingBox aBox;
301 0 : DXFVector aP;
302 0 : pB=aBlocks.Search(pE->sName);
303 0 : if (pB==NULL) break;
304 0 : CalcBoundingBox(*pB,aBox);
305 0 : if (aBox.bEmpty==sal_True) break;
306 0 : aP.fx=(aBox.fMinX-pB->aBasePoint.fx)*pE->fXScale+pE->aP0.fx;
307 0 : aP.fy=(aBox.fMinY-pB->aBasePoint.fy)*pE->fYScale+pE->aP0.fy;
308 0 : aP.fz=(aBox.fMinZ-pB->aBasePoint.fz)*pE->fZScale+pE->aP0.fz;
309 0 : rBox.Union(aP);
310 0 : aP.fx=(aBox.fMaxX-pB->aBasePoint.fx)*pE->fXScale+pE->aP0.fx;
311 0 : aP.fy=(aBox.fMaxY-pB->aBasePoint.fy)*pE->fYScale+pE->aP0.fy;
312 0 : aP.fz=(aBox.fMaxZ-pB->aBasePoint.fz)*pE->fZScale+pE->aP0.fz;
313 0 : rBox.Union(aP);
314 : break;
315 : }
316 : case DXF_ATTDEF: {
317 : //const DXFAttDefEntity * pE = (DXFAttDefEntity*)pBE;
318 : //???
319 0 : break;
320 : }
321 : case DXF_ATTRIB: {
322 : //const DXFAttribEntity * pE = (DXFAttribEntity*)pBE;
323 : //???
324 0 : break;
325 : }
326 : case DXF_VERTEX: {
327 0 : const DXFVertexEntity * pE = (DXFVertexEntity*)pBE;
328 0 : rBox.Union(pE->aP0);
329 0 : break;
330 : }
331 : case DXF_3DFACE: {
332 0 : const DXF3DFaceEntity * pE = (DXF3DFaceEntity*)pBE;
333 0 : rBox.Union(pE->aP0);
334 0 : rBox.Union(pE->aP1);
335 0 : rBox.Union(pE->aP2);
336 0 : rBox.Union(pE->aP3);
337 0 : break;
338 : }
339 : case DXF_DIMENSION: {
340 0 : const DXFDimensionEntity * pE = (DXFDimensionEntity*)pBE;
341 : DXFBlock * pB;
342 0 : DXFBoundingBox aBox;
343 0 : DXFVector aP;
344 0 : pB=aBlocks.Search(pE->sPseudoBlock);
345 0 : if (pB==NULL) break;
346 0 : CalcBoundingBox(*pB,aBox);
347 0 : if (aBox.bEmpty==sal_True) break;
348 0 : aP.fx=aBox.fMinX-pB->aBasePoint.fx;
349 0 : aP.fy=aBox.fMinY-pB->aBasePoint.fy;
350 0 : aP.fz=aBox.fMinZ-pB->aBasePoint.fz;
351 0 : rBox.Union(aP);
352 0 : aP.fx=aBox.fMaxX-pB->aBasePoint.fx;
353 0 : aP.fy=aBox.fMaxY-pB->aBasePoint.fy;
354 0 : aP.fz=aBox.fMaxZ-pB->aBasePoint.fz;
355 0 : rBox.Union(aP);
356 : break;
357 : }
358 : case DXF_POLYLINE: {
359 : //const DXFAttribEntity * pE = (DXFAttribEntity*)pBE;
360 : //???
361 0 : break;
362 : }
363 : case DXF_SEQEND: {
364 : //const DXFAttribEntity * pE = (DXFAttribEntity*)pBE;
365 : //???
366 0 : break;
367 : }
368 : case DXF_HATCH :
369 0 : break;
370 : case DXF_LWPOLYLINE :
371 0 : break;
372 : }
373 0 : pBE=pBE->pSucc;
374 : }
375 0 : }
376 :
377 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|