xref: /haiku/src/kits/print/PicturePrinter.cpp (revision 4bd0c1066b227cec4b79883bdef697c7a27f2e90)
1 /*
2 
3 PicturePrinter
4 
5 Copyright (c) 2002 OpenBeOS.
6 
7 Author:
8 	Michael Pfeiffer
9 
10 Permission is hereby granted, free of charge, to any person obtaining a copy of
11 this software and associated documentation files (the "Software"), to deal in
12 the Software without restriction, including without limitation the rights to
13 use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
14 of the Software, and to permit persons to whom the Software is furnished to do
15 so, subject to the following conditions:
16 
17 The above copyright notice and this permission notice shall be included in all
18 copies or substantial portions of the Software.
19 
20 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
23 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
26 THE SOFTWARE.
27 
28 */
29 
30 #include <stdio.h>
31 
32 #include "PicturePrinter.h"
33 
34 PicturePrinter::PicturePrinter(int indent)
35 	: fIndent(indent)
36  {
37  }
38 
39 void PicturePrinter::Print(const char* text) {
40 	printf("%s ", text);
41 }
42 
43 void PicturePrinter::Print(BPoint* p) {
44 	printf("point (%f, %f) ", p->x, p->y);
45 }
46 
47 void PicturePrinter::Print(BRect* r) {
48 	printf("rect [l: %f, t: %f, r: %f, b: %f] ", r->left, r->top, r->right, r->bottom);
49 }
50 
51 void PicturePrinter::Print(int numPoints, BPoint* points) {
52 	for (int i = 0; i < numPoints; i ++) {
53 		Indent(1); printf("%d ", i); Print(&points[i]); Cr();
54 	}
55 }
56 
57 void PicturePrinter::Print(int numRects, BRect* rects) {
58 	for (int i = 0; i < numRects; i ++) {
59 		Indent(1); printf("%d ", i); Print(&rects[i]); Cr();
60 	}
61 }
62 
63 void PicturePrinter::Print(BShape* shape) {
64 	printf("Shape %p\n", shape);
65 	ShapePrinter printer(this);
66 	printer.Iterate(shape);
67 }
68 
69 void PicturePrinter::Print(const char* text, float f) {
70 	printf("%s %f ", text, f);
71 }
72 
73 void PicturePrinter::Print(const char* text, BPoint* point) {
74 	Print(text); Print(point);
75 }
76 
77 void PicturePrinter::Print(rgb_color color) {
78 	printf("color r: %d g: %d b: %d", color.red, color.green, color.blue);
79 }
80 
81 void PicturePrinter::Print(float f) {
82 	printf("%f ", f);
83 }
84 
85 void PicturePrinter::Cr() {
86 	printf("\n");
87 }
88 
89 void PicturePrinter::Indent(int inc) {
90 	for (int i = fIndent + inc; i > 0; i --) printf("  ");
91 }
92 
93 void PicturePrinter::IncIndent() {
94 	fIndent ++;
95 }
96 
97 void PicturePrinter::DecIndent() {
98 	fIndent --;
99 }
100 
101 void PicturePrinter::Op(int number) {
102 	Indent(); printf("Unknown operator %d\n", number); Cr();
103 }
104 
105 
106 void PicturePrinter::MovePenBy(BPoint delta) {
107 	Indent(); Print("MovePenBy"); Print(&delta); Cr();
108 }
109 
110 
111 void PicturePrinter::StrokeLine(BPoint start, BPoint end) {
112 	Indent(); Print("StrokeLine"); Print(&start); Print(&end); Cr();
113 }
114 
115 
116 void PicturePrinter::StrokeRect(BRect rect) {
117 	Indent(); Print("StrokeRect"); Print(&rect); Cr();
118 }
119 
120 
121 void PicturePrinter::FillRect(BRect rect) {
122 	Indent(); Print("FillRect"); Print(&rect); Cr();
123 }
124 
125 
126 void PicturePrinter::StrokeRoundRect(BRect rect, BPoint radii) {
127 	Indent(); Print("StrokeRoundRect"); Print(&rect); Print("radii", &radii); Cr();
128 }
129 
130 
131 void PicturePrinter::FillRoundRect(BRect rect, BPoint radii) {
132 	Indent(); Print("FillRoundRect"); Print(&rect); Print("radii", &radii); Cr();
133 }
134 
135 
136 void PicturePrinter::StrokeBezier(BPoint *control) {
137 	Indent(); Print("StrokeBezier"); Print(4, control); Cr();
138 }
139 
140 
141 void PicturePrinter::FillBezier(BPoint *control) {
142 	Indent(); Print("FillBezier"); Print(4, control); Cr();
143 }
144 
145 
146 void PicturePrinter::StrokeArc(BPoint center, BPoint radii, float startTheta, float arcTheta) {
147 	Indent(); Print("StrokeArc center="); Print(&center); Print("radii="); Print(&radii); Print("arcTheta=", arcTheta); Cr();
148 }
149 
150 
151 void PicturePrinter::FillArc(BPoint center, BPoint radii, float startTheta, float arcTheta) {
152 	Indent(); Print("FillArc center="); Print(&center); Print("radii="); Print(&radii); Print("arcTheta=", arcTheta); Cr();
153 }
154 
155 
156 void PicturePrinter::StrokeEllipse(BPoint center, BPoint radii) {
157 	Indent(); Print("StrokeEllipse center="); Print(&center); Print("radii="); Print(&radii); Cr();
158 }
159 
160 
161 void PicturePrinter::FillEllipse(BPoint center, BPoint radii) {
162 	Indent(); Print("FillEllipse center="); Print(&center); Print("radii="); Print(&radii); Cr();
163 }
164 
165 
166 void PicturePrinter::StrokePolygon(int32 numPoints, BPoint *points, bool isClosed) {
167 	Indent(); Print("StrokePolygon");
168 	printf("%s ", isClosed ? "closed" : "open"); Cr();
169 	Print(numPoints, points);
170 }
171 
172 
173 void PicturePrinter::FillPolygon(int32 numPoints, BPoint *points, bool isClosed) {
174 	Indent(); Print("FillPolygon");
175 	printf("%s ", isClosed ? "closed" : "open"); Cr();
176 	Print(numPoints, points);
177 }
178 
179 
180 void PicturePrinter::StrokeShape(BShape *shape) {
181 	Indent(); Print("StrokeShape"); Print(shape); Cr();
182 }
183 
184 
185 void PicturePrinter::FillShape(BShape *shape) {
186 	Indent(); Print("FillShape"); Print(shape); Cr();
187 }
188 
189 
190 void PicturePrinter::DrawString(char *string, float escapement_nospace, float escapement_space) {
191 	Indent(); Print("DrawString");
192 	Print("escapement_nospace", escapement_nospace);
193 	Print("escapement_space", escapement_space);
194 	Print("text:"); Print(string); Cr();
195 }
196 
197 
198 void PicturePrinter::DrawPixels(BRect src, BRect dest, int32 width, int32 height, int32 bytesPerRow, int32 pixelFormat, int32 flags, void *data) {
199 	Indent(); Print("DrawPixels"); Cr();
200 }
201 
202 
203 void PicturePrinter::SetClippingRects(BRect *rects, uint32 numRects) {
204 	Indent(); Print("SetClippingRects");
205 	if (numRects == 0) Print("none");
206 	Cr();
207 	Print(numRects, rects);
208 }
209 
210 
211 void PicturePrinter::ClipToPicture(BPicture *picture, BPoint point, bool clip_to_inverse_picture) {
212 	Indent();
213 	Print(clip_to_inverse_picture ? "ClipToInversePicture" : "ClipToPicture");
214 	Print("point=", &point); Cr();
215 	PicturePrinter printer(fIndent+1);
216 	printer.Iterate(picture);
217 }
218 
219 
220 void PicturePrinter::PushState() {
221 	Indent(); Print("PushState"); Cr();
222 	IncIndent();
223 }
224 
225 
226 void PicturePrinter::PopState() {
227 	DecIndent();
228 	Indent(); Print("PopState"); Cr();
229 }
230 
231 
232 void PicturePrinter::EnterStateChange() {
233 	Indent(); Print("EnterStateChange"); Cr();
234 }
235 
236 
237 void PicturePrinter::ExitStateChange() {
238 	Indent(); Print("ExitStateChange"); Cr();
239 }
240 
241 
242 void PicturePrinter::EnterFontState() {
243 	Indent(); Print("EnterFontState"); Cr();
244 }
245 
246 
247 void PicturePrinter::ExitFontState() {
248 	Indent(); Print("ExitFontState"); Cr();
249 }
250 
251 
252 void PicturePrinter::SetOrigin(BPoint pt) {
253 	Indent(); Print("SetOrigin"); Print(&pt); Cr();
254 }
255 
256 
257 void PicturePrinter::SetPenLocation(BPoint pt) {
258 	Indent(); Print("SetPenLocation"); Print(&pt); Cr();
259 }
260 
261 
262 void PicturePrinter::SetDrawingMode(drawing_mode mode) {
263 	Indent(); Print("SetDrawingMode");
264 	switch (mode) {
265 		case B_OP_COPY: Print("B_OP_COPY"); break;
266 		case B_OP_OVER: Print("B_OP_OVER"); break;
267 		case B_OP_ERASE: Print("B_OP_ERASE"); break;
268 		case B_OP_INVERT: Print("B_OP_INVERT"); break;
269 		case B_OP_SELECT: Print("B_OP_SELECT"); break;
270 		case B_OP_ALPHA: Print("B_OP_ALPHA"); break;
271 		case B_OP_MIN: Print("B_OP_MIN"); break;
272 		case B_OP_MAX: Print("B_OP_MAX"); break;
273 		case B_OP_ADD: Print("B_OP_ADD"); break;
274 		case B_OP_SUBTRACT: Print("B_OP_SUBTRACT"); break;
275 		case B_OP_BLEND: Print("B_OP_BLEND"); break;
276 		default: Print("Unknown mode: ", (float)mode);
277 	}
278 	Cr();
279 }
280 
281 
282 void PicturePrinter::SetLineMode(cap_mode capMode, join_mode joinMode, float miterLimit) {
283 	Indent(); Print("SetLineMode");
284 	switch (capMode) {
285 		case B_BUTT_CAP:   Print("B_BUTT_CAP"); break;
286 		case B_ROUND_CAP:  Print("B_ROUND_CAP"); break;
287 		case B_SQUARE_CAP: Print("B_SQUARE_CAP"); break;
288 	}
289 	switch (joinMode) {
290 		case B_MITER_JOIN: Print("B_MITER_JOIN"); break;
291 		case B_ROUND_JOIN: Print("B_ROUND_JOIN"); break;
292 		case B_BUTT_JOIN: Print("B_BUTT_JOIN"); break;
293 		case B_SQUARE_JOIN: Print("B_SQUARE_JOIN"); break;
294 		case B_BEVEL_JOIN: Print("B_BEVEL_JOIN"); break;
295 	}
296 	Print("miterLimit", miterLimit);
297 	Cr();
298 }
299 
300 
301 void PicturePrinter::SetPenSize(float size) {
302 	Indent(); Print("SetPenSize", size); Cr();
303 }
304 
305 
306 void PicturePrinter::SetForeColor(rgb_color color) {
307 	Indent(); Print("SetForeColor"); Print(color); Cr();
308 }
309 
310 
311 void PicturePrinter::SetBackColor(rgb_color color) {
312 	Indent(); Print("SetBackColor"); Print(color); Cr();
313 }
314 
315 static bool compare(pattern a, pattern b) {
316 	for (int i = 0; i < 8; i ++) {
317 		if (a.data[i] != b.data[i]) return false;
318 	}
319 	return true;
320 }
321 
322 void PicturePrinter::SetStipplePattern(pattern p) {
323 	Indent(); Print("SetStipplePattern");
324 	if (compare(p, B_SOLID_HIGH)) Print("B_SOLID_HIGH");
325 	else if (compare(p, B_SOLID_LOW)) Print("B_SOLID_LOW");
326 	else if (compare(p, B_MIXED_COLORS)) Print("B_MIXED_COLORS");
327 	else {
328 		for (int i = 0; i < 8; i++) {
329 			printf("%2.2x ", (unsigned int)p.data[i]);
330 		}
331 	}
332 	Cr();
333 }
334 
335 
336 void PicturePrinter::SetScale(float scale) {
337 	Indent(); Print("SetScale", scale); Cr();
338 }
339 
340 
341 void PicturePrinter::SetFontFamily(char *family) {
342 	Indent(); Print("SetFontFamily"); Print(family); Cr();
343 }
344 
345 
346 void PicturePrinter::SetFontStyle(char *style) {
347 	Indent(); Print("SetFontStyle"); Print(style); Cr();
348 }
349 
350 
351 void PicturePrinter::SetFontSpacing(int32 spacing) {
352 	Indent(); Print("SetFontSpacing");
353 	switch(spacing) {
354 		case B_CHAR_SPACING: Print("B_CHAR_SPACING"); break;
355 		case B_STRING_SPACING: Print("B_STRING_SPACING"); break;
356 		case B_BITMAP_SPACING: Print("B_BITMAP_SPACING"); break;
357 		case B_FIXED_SPACING: Print("B_FIXED_SPACING"); break;
358 		default: Print("Unknown: ", (float)spacing);
359 	}
360 	Cr();
361 }
362 
363 
364 void PicturePrinter::SetFontSize(float size) {
365 	Indent(); Print("SetFontSize", size); Cr();
366 }
367 
368 
369 void PicturePrinter::SetFontRotate(float rotation) {
370 	Indent(); Print("SetFontRotation", rotation); Cr();
371 }
372 
373 
374 void PicturePrinter::SetFontEncoding(int32 encoding) {
375 	Indent(); Print("SetFontEncoding");
376 	switch (encoding) {
377 		case B_UNICODE_UTF8: Print("B_UNICODE_UTF8"); break;
378 		case B_ISO_8859_1: Print("B_ISO_8859_1"); break;
379 		case B_ISO_8859_2: Print("B_ISO_8859_2"); break;
380 		case B_ISO_8859_3: Print("B_ISO_8859_3"); break;
381 		case B_ISO_8859_4: Print("B_ISO_8859_4"); break;
382 		case B_ISO_8859_5: Print("B_ISO_8859_5"); break;
383 		case B_ISO_8859_6: Print("B_ISO_8859_6"); break;
384 		case B_ISO_8859_7: Print("B_ISO_8859_7"); break;
385 		case B_ISO_8859_8: Print("B_ISO_8859_8"); break;
386 		case B_ISO_8859_9: Print("B_ISO_8859_9"); break;
387 		case B_ISO_8859_10: Print("B_ISO_8859_10"); break;
388 		case B_MACINTOSH_ROMAN: Print("B_MACINTOSH_ROMAN"); break;
389 		default: Print("Unknown:", (float)encoding);
390 	}
391 	Cr();
392 }
393 
394 #define PRINT_FLAG(flag) \
395   if (flags & flag) { f |= flag; Print(#flag); }
396 
397 void PicturePrinter::SetFontFlags(int32 flags) {
398 	Indent(); Print("SetFontFlags");
399 	int f = 0;
400 	if (flags == 0) Print("none set");
401 	PRINT_FLAG(B_DISABLE_ANTIALIASING);
402 	PRINT_FLAG(B_FORCE_ANTIALIASING);
403 	if (flags != f) printf("Unknown Additional Flags %" B_PRId32 "", flags & ~f);
404 	Cr();
405 }
406 
407 
408 void PicturePrinter::SetFontShear(float shear) {
409 	Indent(); Print("SetFontShear", shear); Cr();
410 }
411 
412 
413 void PicturePrinter::SetFontFace(int32 flags) {
414 	Indent(); Print("SetFontFace");
415 	int32 f = 0;
416 	if (flags == 0) Print("none set");
417 	PRINT_FLAG(B_REGULAR_FACE);
418 	PRINT_FLAG(B_BOLD_FACE);
419 	PRINT_FLAG(B_ITALIC_FACE);
420 	PRINT_FLAG(B_NEGATIVE_FACE);
421 	PRINT_FLAG(B_OUTLINED_FACE);
422 	PRINT_FLAG(B_UNDERSCORE_FACE);
423 	PRINT_FLAG(B_STRIKEOUT_FACE);
424 	if (flags != f) printf("Unknown Additional Flags %" B_PRId32 "", flags & ~f);
425 	Cr();
426 }
427 
428 
429 // Implementation of ShapePrinter
430 ShapePrinter::ShapePrinter(PicturePrinter* printer)
431 	: fPrinter(printer)
432 {
433 	fPrinter->IncIndent();
434 }
435 
436 ShapePrinter::~ShapePrinter() {
437 	fPrinter->DecIndent();
438 }
439 
440 status_t
441 ShapePrinter::IterateBezierTo(int32 bezierCount, BPoint *control)
442 {
443 	fPrinter->Indent(); fPrinter->Print("BezierTo"); fPrinter->Cr();
444 	for (int32 i = 0; i < bezierCount; i++, control += 3) {
445 		fPrinter->Indent(1);
446 		fPrinter->Print(i / 3.0);
447 		fPrinter->Print(&control[0]);
448 		fPrinter->Print(&control[1]);
449 		fPrinter->Print(&control[2]);
450 		fPrinter->Cr();
451 	}
452 	return B_OK;
453 }
454 
455 status_t
456 ShapePrinter::IterateClose(void)
457 {
458 	fPrinter->Indent(); fPrinter->Print("Close"); fPrinter->Cr();
459 	return B_OK;
460 }
461 
462 status_t
463 ShapePrinter::IterateLineTo(int32 lineCount, BPoint *linePoints)
464 {
465 	fPrinter->Indent(); fPrinter->Print("LineTo"); fPrinter->Cr();
466 	BPoint *p = linePoints;
467 	for (int32 i = 0; i < lineCount; i++) {
468 		fPrinter->Indent(1); fPrinter->Print(p); fPrinter->Cr();
469 		p++;
470 	}
471 	return B_OK;
472 }
473 
474 status_t
475 ShapePrinter::IterateMoveTo(BPoint *point)
476 {
477 	fPrinter->Indent(); fPrinter->Print("MoveTo", point); fPrinter->Cr();
478 	return B_OK;
479 }
480 
481