xref: /haiku/src/libs/print/libprint/PrintProcess.cpp (revision b8a45b3a2df2379b4301bf3bd5949b9a105be4ba)
1 /*
2  * PrintProcess.cpp
3  * Copyright 1999-2000 Y.Takagi. All Rights Reserved.
4  */
5 
6 #include <File.h>
7 #include <Picture.h>
8 #include <unistd.h>
9 
10 #include "PrintProcess.h"
11 #include "DbgMsg.h"
12 
13 
14 PictureData::PictureData(BFile *file)
15 {
16 	DBGMSG(("construct PictureData\n"));
17 	DBGMSG(("1: current seek position = 0x%x\n", (int)file->Position()));
18 
19 	file->Read(&point,  sizeof(BPoint));
20 	file->Read(&rect,   sizeof(BRect));
21 
22 	picture = new BPicture();
23 
24 	DBGMSG(("picture_data::point = %f, %f\n", point.x, point.y));
25 	DBGMSG(("picture_data::rect = %f, %f, %f, %f\n",
26 		rect.left, rect.top, rect.right, rect.bottom));
27 	DBGMSG(("2: current seek position = 0x%x\n", (int)file->Position()));
28 
29 	picture->Unflatten(file);
30 
31 	DBGMSG(("3: current seek position = 0x%x\n", (int)file->Position()));
32 }
33 
34 
35 PictureData::~PictureData()
36 {
37 	delete picture;
38 }
39 
40 
41 PageData::PageData()
42 {
43 	fHollow = true;
44 }
45 
46 
47 PageData::PageData(BFile *file, bool reverse)
48 {
49 	fFile    = file;
50 	fReverse = reverse;
51 	fPictureCount = 0;
52 	fRest    = 0;
53 	fOffset  = 0;
54 	fHollow  = false;
55 
56 	if (reverse) {
57 		file->Read(&fPictureCount, sizeof(int32));
58 		DBGMSG(("picture_count = %" B_PRId32 "\n", fPictureCount));
59 		fOffset = fFile->Position();
60 		off_t o = fOffset;
61 		// seek to start of next page
62 		fFile->Read(&o, sizeof(o));
63 		fFile->Seek(o, SEEK_SET);
64 	}
65 }
66 
67 
68 bool
69 PageData::startEnum()
70 {
71 	off_t offset;
72 	uchar dummy[40];
73 
74 	if (fHollow)
75 		return false;
76 
77 	if (fOffset == 0) {
78 		fFile->Read(&fPictureCount, sizeof(int32));
79 		DBGMSG(("picture_count = %" B_PRId32 "\n", fPictureCount));
80 		fOffset = fFile->Position();
81 	} else {
82 		fFile->Seek(fOffset, SEEK_SET);
83 	}
84 	// skip page header
85 	fFile->Seek(sizeof(offset) + sizeof(dummy), SEEK_CUR);
86 
87 	fRest = fPictureCount;
88 	return fPictureCount > 0;
89 }
90 
91 
92 bool
93 PageData::enumObject(PictureData **picture_data)
94 {
95 	if (fHollow || fPictureCount <= 0) {
96 		*picture_data = NULL;
97 	} else {
98 		*picture_data = new PictureData(fFile);
99 		if (--fRest > 0) {
100 			return true;
101 		}
102 	}
103 	return false;
104 }
105 
106 
107 SpoolData::SpoolData(BFile *file, int32 page_count, int32 nup, bool reverse)
108 {
109 	DBGMSG(("nup        = %" B_PRId32 "\n", nup));
110 	DBGMSG(("page_count = %" B_PRId32 "\n", page_count));
111 	DBGMSG(("reverse    = %s\n", reverse ? "true" : "false"));
112 
113 	if (reverse) {
114 		if (nup > 1) {
115 			for (int32 page_index = 0; page_index < page_count; page_index++) {
116 				if (page_index % nup == 0) {
117 					fPages.push_front(new PageData(file, reverse));
118 					fIt = fPages.begin();
119 					fIt++;
120 				} else {
121 					fPages.insert(fIt, new PageData(file, reverse));
122 				}
123 			}
124 			page_count = nup - page_count % nup;
125 			if (page_count < nup) {
126 				while (page_count--) {
127 					fPages.insert(fIt, new PageData);
128 				}
129 			}
130 		} else {
131 			while (page_count--) {
132 				fPages.push_front(new PageData(file, reverse));
133 			}
134 		}
135 	} else {
136 		while (page_count--) {
137 			fPages.push_back(new PageData(file, reverse));
138 		}
139 	}
140 }
141 
142 
143 SpoolData::~SpoolData()
144 {
145 	for (fIt = fPages.begin(); fIt != fPages.end(); fIt++) {
146 		delete (*fIt);
147 	}
148 }
149 
150 
151 bool
152 SpoolData::startEnum()
153 {
154 	fIt = fPages.begin();
155 	return true;
156 }
157 
158 
159 bool
160 SpoolData::enumObject(PageData **page_data)
161 {
162 	*page_data = *fIt++;
163 	if (fIt == fPages.end()) {
164 		return false;
165 	}
166 	return true;
167 }
168