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(¢er); 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(¢er); 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(¢er); Print("radii="); Print(&radii); Cr(); 158 } 159 160 161 void PicturePrinter::FillEllipse(BPoint center, BPoint radii) { 162 Indent(); Print("FillEllipse center="); Print(¢er); 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"); Cr(); 206 Print(numRects, rects); 207 } 208 209 210 void PicturePrinter::ClipToPicture(BPicture *picture, BPoint point, bool clip_to_inverse_picture) { 211 Indent(); 212 Print(clip_to_inverse_picture ? "ClipToInversePicture" : "ClipToPicture"); 213 Print("point=", &point); Cr(); 214 PicturePrinter printer(fIndent+1); 215 printer.Iterate(picture); 216 } 217 218 219 void PicturePrinter::PushState() { 220 Indent(); Print("PushState"); Cr(); 221 IncIndent(); 222 } 223 224 225 void PicturePrinter::PopState() { 226 DecIndent(); 227 Indent(); Print("PopState"); Cr(); 228 } 229 230 231 void PicturePrinter::EnterStateChange() { 232 Indent(); Print("EnterStateChange"); Cr(); 233 } 234 235 236 void PicturePrinter::ExitStateChange() { 237 Indent(); Print("ExitStateChange"); Cr(); 238 } 239 240 241 void PicturePrinter::EnterFontState() { 242 Indent(); Print("EnterFontState"); Cr(); 243 } 244 245 246 void PicturePrinter::ExitFontState() { 247 Indent(); Print("ExitFontState"); Cr(); 248 } 249 250 251 void PicturePrinter::SetOrigin(BPoint pt) { 252 Indent(); Print("SetOrigin"); Print(&pt); Cr(); 253 } 254 255 256 void PicturePrinter::SetPenLocation(BPoint pt) { 257 Indent(); Print("SetPenLocation"); Print(&pt); Cr(); 258 } 259 260 261 void PicturePrinter::SetDrawingMode(drawing_mode mode) { 262 Indent(); Print("SetDrawingMode"); 263 switch (mode) { 264 case B_OP_COPY: Print("B_OP_COPY"); break; 265 case B_OP_OVER: Print("B_OP_OVER"); break; 266 case B_OP_ERASE: Print("B_OP_ERASE"); break; 267 case B_OP_INVERT: Print("B_OP_INVERT"); break; 268 case B_OP_SELECT: Print("B_OP_SELECT"); break; 269 case B_OP_ALPHA: Print("B_OP_ALPHA"); break; 270 case B_OP_MIN: Print("B_OP_MIN"); break; 271 case B_OP_MAX: Print("B_OP_MAX"); break; 272 case B_OP_ADD: Print("B_OP_ADD"); break; 273 case B_OP_SUBTRACT: Print("B_OP_SUBTRACT"); break; 274 case B_OP_BLEND: Print("B_OP_BLEND"); break; 275 default: Print("Unknown mode: ", (float)mode); 276 } 277 Cr(); 278 } 279 280 281 void PicturePrinter::SetLineMode(cap_mode capMode, join_mode joinMode, float miterLimit) { 282 Indent(); Print("SetLineMode"); 283 switch (capMode) { 284 case B_BUTT_CAP: Print("B_BUTT_CAP"); break; 285 case B_ROUND_CAP: Print("B_ROUND_CAP"); break; 286 case B_SQUARE_CAP: Print("B_SQUARE_CAP"); break; 287 } 288 switch (joinMode) { 289 case B_MITER_JOIN: Print("B_MITER_JOIN"); break; 290 case B_ROUND_JOIN: Print("B_ROUND_JOIN"); break; 291 case B_BUTT_JOIN: Print("B_BUTT_JOIN"); break; 292 case B_SQUARE_JOIN: Print("B_SQUARE_JOIN"); break; 293 case B_BEVEL_JOIN: Print("B_BEVEL_JOIN"); break; 294 } 295 Print("miterLimit", miterLimit); 296 Cr(); 297 } 298 299 300 void PicturePrinter::SetPenSize(float size) { 301 Indent(); Print("SetPenSize", size); Cr(); 302 } 303 304 305 void PicturePrinter::SetForeColor(rgb_color color) { 306 Indent(); Print("SetForeColor"); Print(color); Cr(); 307 } 308 309 310 void PicturePrinter::SetBackColor(rgb_color color) { 311 Indent(); Print("SetBackColor"); Print(color); Cr(); 312 } 313 314 static bool compare(pattern a, pattern b) { 315 for (int i = 0; i < 8; i ++) { 316 if (a.data[i] != b.data[i]) return false; 317 } 318 return true; 319 } 320 321 void PicturePrinter::SetStipplePattern(pattern p) { 322 Indent(); Print("SetStipplePattern"); 323 if (compare(p, B_SOLID_HIGH)) Print("B_SOLID_HIGH"); 324 else if (compare(p, B_SOLID_LOW)) Print("B_SOLID_LOW"); 325 else if (compare(p, B_MIXED_COLORS)) Print("B_MIXED_COLORS"); 326 else { 327 for (int i = 0; i < 8; i++) { 328 printf("%2.2x ", (unsigned int)p.data[i]); 329 } 330 } 331 Cr(); 332 } 333 334 335 void PicturePrinter::SetScale(float scale) { 336 Indent(); Print("SetScale", scale); Cr(); 337 } 338 339 340 void PicturePrinter::SetFontFamily(char *family) { 341 Indent(); Print("SetFontFamily"); Print(family); Cr(); 342 } 343 344 345 void PicturePrinter::SetFontStyle(char *style) { 346 Indent(); Print("SetFontStyle"); Print(style); Cr(); 347 } 348 349 350 void PicturePrinter::SetFontSpacing(int32 spacing) { 351 Indent(); Print("SetFontSpacing"); 352 switch(spacing) { 353 case B_CHAR_SPACING: Print("B_CHAR_SPACING"); break; 354 case B_STRING_SPACING: Print("B_STRING_SPACING"); break; 355 case B_BITMAP_SPACING: Print("B_BITMAP_SPACING"); break; 356 case B_FIXED_SPACING: Print("B_FIXED_SPACING"); break; 357 default: Print("Unknown: ", (float)spacing); 358 } 359 Cr(); 360 } 361 362 363 void PicturePrinter::SetFontSize(float size) { 364 Indent(); Print("SetFontSize", size); Cr(); 365 } 366 367 368 void PicturePrinter::SetFontRotate(float rotation) { 369 Indent(); Print("SetFontRotation", rotation); Cr(); 370 } 371 372 373 void PicturePrinter::SetFontEncoding(int32 encoding) { 374 Indent(); Print("SetFontEncoding"); 375 switch (encoding) { 376 case B_UNICODE_UTF8: Print("B_UNICODE_UTF8"); break; 377 case B_ISO_8859_1: Print("B_ISO_8859_1"); break; 378 case B_ISO_8859_2: Print("B_ISO_8859_2"); break; 379 case B_ISO_8859_3: Print("B_ISO_8859_3"); break; 380 case B_ISO_8859_4: Print("B_ISO_8859_4"); break; 381 case B_ISO_8859_5: Print("B_ISO_8859_5"); break; 382 case B_ISO_8859_6: Print("B_ISO_8859_6"); break; 383 case B_ISO_8859_7: Print("B_ISO_8859_7"); break; 384 case B_ISO_8859_8: Print("B_ISO_8859_8"); break; 385 case B_ISO_8859_9: Print("B_ISO_8859_9"); break; 386 case B_ISO_8859_10: Print("B_ISO_8859_10"); break; 387 case B_MACINTOSH_ROMAN: Print("B_MACINTOSH_ROMAN"); break; 388 default: Print("Unknown:", (float)encoding); 389 } 390 Cr(); 391 } 392 393 #define PRINT_FLAG(flag) \ 394 if (flags & flag) { f |= flag; Print(#flag); } 395 396 void PicturePrinter::SetFontFlags(int32 flags) { 397 Indent(); Print("SetFontFlags"); 398 int f = 0; 399 if (flags == 0) Print("none set"); 400 PRINT_FLAG(B_DISABLE_ANTIALIASING); 401 PRINT_FLAG(B_FORCE_ANTIALIASING); 402 if (flags != f) printf("Unknown Additional Flags %ld", flags & ~f); 403 Cr(); 404 } 405 406 407 void PicturePrinter::SetFontShear(float shear) { 408 Indent(); Print("SetFontShear", shear); Cr(); 409 } 410 411 412 void PicturePrinter::SetFontFace(int32 flags) { 413 Indent(); Print("SetFontFace"); 414 int32 f = 0; 415 if (flags == 0) Print("none set"); 416 PRINT_FLAG(B_REGULAR_FACE); 417 PRINT_FLAG(B_BOLD_FACE); 418 PRINT_FLAG(B_ITALIC_FACE); 419 PRINT_FLAG(B_NEGATIVE_FACE); 420 PRINT_FLAG(B_OUTLINED_FACE); 421 PRINT_FLAG(B_UNDERSCORE_FACE); 422 PRINT_FLAG(B_STRIKEOUT_FACE); 423 if (flags != f) printf("Unknown Additional Flags %ld", flags & ~f); 424 Cr(); 425 } 426 427 428 // Implementation of ShapePrinter 429 ShapePrinter::ShapePrinter(PicturePrinter* printer) 430 : fPrinter(printer) 431 { 432 fPrinter->IncIndent(); 433 } 434 435 ShapePrinter::~ShapePrinter() { 436 fPrinter->DecIndent(); 437 } 438 439 status_t 440 ShapePrinter::IterateBezierTo(int32 bezierCount, BPoint *control) 441 { 442 fPrinter->Indent(); fPrinter->Print("BezierTo"); fPrinter->Cr(); 443 for (int32 i = 0; i < bezierCount; i++, control += 3) { 444 fPrinter->Indent(1); 445 fPrinter->Print(i / 3.0); 446 fPrinter->Print(&control[0]); 447 fPrinter->Print(&control[1]); 448 fPrinter->Print(&control[2]); 449 fPrinter->Cr(); 450 } 451 return B_OK; 452 } 453 454 status_t 455 ShapePrinter::IterateClose(void) 456 { 457 fPrinter->Indent(); fPrinter->Print("Close"); fPrinter->Cr(); 458 return B_OK; 459 } 460 461 status_t 462 ShapePrinter::IterateLineTo(int32 lineCount, BPoint *linePoints) 463 { 464 fPrinter->Indent(); fPrinter->Print("LineTo"); fPrinter->Cr(); 465 BPoint *p = linePoints; 466 for (int32 i = 0; i < lineCount; i++) { 467 fPrinter->Indent(1); fPrinter->Print(p); fPrinter->Cr(); 468 p++; 469 } 470 return B_OK; 471 } 472 473 status_t 474 ShapePrinter::IterateMoveTo(BPoint *point) 475 { 476 fPrinter->Indent(); fPrinter->Print("MoveTo", point); fPrinter->Cr(); 477 return B_OK; 478 } 479 480