54 #include <sys/types.h>
59 #if defined(TOOLS_GL2PS_HAVE_ZLIB)
63 #if defined(TOOLS_GL2PS_HAVE_LIBPNG)
76 #define TOOLS_GL2PS_EPSILON 5.0e-3F
77 #define TOOLS_GL2PS_ZSCALE 1000.0F
78 #define TOOLS_GL2PS_ZOFFSET 5.0e-2F
79 #define TOOLS_GL2PS_ZOFFSET_LARGE 20.0F
80 #define TOOLS_GL2PS_ZERO(arg) (fabs(arg) < 1.e-20)
84 #define TOOLS_GL2PS_COINCIDENT 1
85 #define TOOLS_GL2PS_IN_FRONT_OF 2
86 #define TOOLS_GL2PS_IN_BACK_OF 3
87 #define TOOLS_GL2PS_SPANNING 4
91 #define TOOLS_GL2PS_POINT_COINCIDENT 0
92 #define TOOLS_GL2PS_POINT_INFRONT 1
93 #define TOOLS_GL2PS_POINT_BACK 2
97 #define TOOLS_GL2PS_BEGIN_OFFSET_TOKEN 1
98 #define TOOLS_GL2PS_END_OFFSET_TOKEN 2
99 #define TOOLS_GL2PS_BEGIN_BOUNDARY_TOKEN 3
100 #define TOOLS_GL2PS_END_BOUNDARY_TOKEN 4
101 #define TOOLS_GL2PS_BEGIN_STIPPLE_TOKEN 5
102 #define TOOLS_GL2PS_END_STIPPLE_TOKEN 6
103 #define TOOLS_GL2PS_POINT_SIZE_TOKEN 7
104 #define TOOLS_GL2PS_LINE_CAP_TOKEN 8
105 #define TOOLS_GL2PS_LINE_JOIN_TOKEN 9
106 #define TOOLS_GL2PS_LINE_WIDTH_TOKEN 10
107 #define TOOLS_GL2PS_BEGIN_BLEND_TOKEN 11
108 #define TOOLS_GL2PS_END_BLEND_TOKEN 12
109 #define TOOLS_GL2PS_SRC_BLEND_TOKEN 13
110 #define TOOLS_GL2PS_DST_BLEND_TOKEN 14
111 #define TOOLS_GL2PS_IMAGEMAP_TOKEN 15
112 #define TOOLS_GL2PS_DRAW_PIXELS_TOKEN 16
113 #define TOOLS_GL2PS_TEXT_TOKEN 17
191 #if defined(TOOLS_GL2PS_HAVE_ZLIB)
192 Bytef *dest, *src, *start;
193 uLongf destLen, srcLen;
249 void (*printHeader)(void);
250 void (*printFooter)(void);
253 void (*printPrimitive)(
void *data);
254 void (*printFinalPrimitive)(void);
266 static tools_GLint tools_gl2psPrintPrimitives(
void);
274 static void tools_gl2psMsg(
tools_GLint level,
const char *fmt, ...)
285 vfprintf(stderr, fmt, args);
287 fprintf(stderr,
"\n");
292 static void *tools_gl2psMalloc(
size_t size)
296 if(!
size)
return NULL;
305 static void *tools_gl2psRealloc(
void *ptr,
size_t size)
308 if(!
size)
return NULL;
318 static void tools_gl2psFree(
void *ptr)
324 static int tools_gl2psWriteBigEndian(
unsigned long data,
int bytes)
327 int size =
sizeof(
unsigned long);
328 for(i = 1; i <= bytes; ++i){
329 fputc(0xff & (data >> (
size - i) * 8), tools_gl2ps_context->
stream);
336 #if defined(TOOLS_GL2PS_HAVE_ZLIB)
338 static void tools_gl2psSetupCompress(
void)
341 tools_gl2ps_context->
compress->src = NULL;
342 tools_gl2ps_context->
compress->start = NULL;
343 tools_gl2ps_context->
compress->dest = NULL;
344 tools_gl2ps_context->
compress->srcLen = 0;
345 tools_gl2ps_context->
compress->destLen = 0;
348 static void tools_gl2psFreeCompress(
void)
352 tools_gl2psFree(tools_gl2ps_context->
compress->start);
353 tools_gl2psFree(tools_gl2ps_context->
compress->dest);
354 tools_gl2ps_context->
compress->src = NULL;
355 tools_gl2ps_context->
compress->start = NULL;
356 tools_gl2ps_context->
compress->dest = NULL;
357 tools_gl2ps_context->
compress->srcLen = 0;
358 tools_gl2ps_context->
compress->destLen = 0;
361 static int tools_gl2psAllocCompress(
unsigned int srcsize)
363 tools_gl2psFreeCompress();
365 if(!tools_gl2ps_context->
compress || !srcsize)
368 tools_gl2ps_context->
compress->srcLen = srcsize;
369 tools_gl2ps_context->
compress->destLen = (int)ceil(1.001 * tools_gl2ps_context->
compress->srcLen + 12);
370 tools_gl2ps_context->
compress->src = (Bytef*)tools_gl2psMalloc(tools_gl2ps_context->
compress->srcLen);
372 tools_gl2ps_context->
compress->dest = (Bytef*)tools_gl2psMalloc(tools_gl2ps_context->
compress->destLen);
377 static void *tools_gl2psReallocCompress(
unsigned int srcsize)
379 if(!tools_gl2ps_context->
compress || !srcsize)
382 if(srcsize < tools_gl2ps_context->compress->srcLen)
383 return tools_gl2ps_context->
compress->start;
385 tools_gl2ps_context->
compress->srcLen = srcsize;
386 tools_gl2ps_context->
compress->destLen = (int)ceil(1.001 * tools_gl2ps_context->
compress->srcLen + 12);
387 tools_gl2ps_context->
compress->src = (Bytef*)tools_gl2psRealloc(tools_gl2ps_context->
compress->src,
388 tools_gl2ps_context->
compress->srcLen);
390 tools_gl2ps_context->
compress->dest = (Bytef*)tools_gl2psRealloc(tools_gl2ps_context->
compress->dest,
391 tools_gl2ps_context->
compress->destLen);
393 return tools_gl2ps_context->
compress->start;
396 static int tools_gl2psWriteBigEndianCompress(
unsigned long data,
int bytes)
399 int size =
sizeof(
unsigned long);
400 for(i = 1; i <= bytes; ++i){
401 *tools_gl2ps_context->
compress->src = (Bytef)(0xff & (data >> (
size-i) * 8));
402 ++tools_gl2ps_context->
compress->src;
407 static int tools_gl2psDeflate(
void)
411 return compress(tools_gl2ps_context->
compress->dest, &tools_gl2ps_context->
compress->destLen,
412 tools_gl2ps_context->
compress->start, tools_gl2ps_context->
compress->srcLen);
417 static int tools_gl2psPrintf(
const char* fmt, ...)
422 #if defined(TOOLS_GL2PS_HAVE_ZLIB)
423 static char buf[1024];
426 unsigned int oldsize = 0;
427 #if !defined(TOOLS_GL2PS_HAVE_NO_VSNPRINTF)
430 int bufsize =
sizeof(buf);
434 #if defined(TOOLS_GL2PS_HAVE_NO_VSNPRINTF)
437 ret = vsnprintf(bufptr, bufsize, fmt, args);
440 #if !defined(TOOLS_GL2PS_HAVE_NO_VSNPRINTF)
441 while(ret >= (bufsize - 1) || ret < 0){
445 bufptr = (
char *)tools_gl2psMalloc(bufsize);
448 ret = vsnprintf(bufptr, bufsize, fmt, args);
452 oldsize = tools_gl2ps_context->
compress->srcLen;
453 tools_gl2ps_context->
compress->start = (Bytef*)tools_gl2psReallocCompress(oldsize + ret);
454 memcpy(tools_gl2ps_context->
compress->start + oldsize, bufptr, ret);
461 ret = vfprintf(tools_gl2ps_context->
stream, fmt, args);
463 #if defined(TOOLS_GL2PS_HAVE_ZLIB)
469 static void tools_gl2psPrintGzipHeader(
void)
471 #if defined(TOOLS_GL2PS_HAVE_ZLIB)
472 char tmp[10] = {
'\x1f',
'\x8b',
480 tools_gl2psSetupCompress();
482 fwrite(tmp, 10, 1, tools_gl2ps_context->
stream);
487 static void tools_gl2psPrintGzipFooter(
void)
489 #if defined(TOOLS_GL2PS_HAVE_ZLIB)
495 if(Z_OK != tools_gl2psDeflate()){
501 if(tools_gl2ps_context->
compress->dest[1] & (1<<5)){
505 fwrite(tools_gl2ps_context->
compress->dest+n, tools_gl2ps_context->
compress->destLen-(n+4),
506 1, tools_gl2ps_context->
stream);
508 crc = crc32(0L, tools_gl2ps_context->
compress->start, tools_gl2ps_context->
compress->srcLen);
509 for(n = 0; n < 4; ++n){
510 tmp[n] = (char)(crc & 0xff);
513 len = tools_gl2ps_context->
compress->srcLen;
514 for(n = 4; n < 8; ++n){
515 tmp[n] = (char)(len & 0xff);
518 fwrite(tmp, 8, 1, tools_gl2ps_context->
stream);
520 tools_gl2psFreeCompress();
521 tools_gl2psFree(tools_gl2ps_context->
compress);
522 tools_gl2ps_context->
compress = NULL;
538 list->
array = (
char*)tools_gl2psMalloc(list->
nmax * list->
size);
543 list->
array = (
char*)tools_gl2psRealloc(list->
array,
554 if(incr <= 0) incr = 1;
561 tools_gl2psListRealloc(list, n);
574 tools_gl2psFree(list->
array);
575 tools_gl2psFree(list);
585 tools_gl2psListRealloc(list, list->
n);
586 memcpy(&list->
array[(list->
n - 1) * list->
size], data, list->
size);
602 if((idx < 0) || (idx >= list->
n)){
610 int (*fcmp)(
const void *a,
const void *b))
614 qsort(list->
array, list->
n, list->
size, fcmp);
617 static void tools_gl2psListAction(
tools_GL2PSlist *list,
void (*action)(
void *data))
621 for(i = 0; i < tools_gl2psListNbr(list); i++){
622 (*action)(tools_gl2psListPointer(list, i));
626 static void tools_tools_gl2psListActionInverse(
tools_GL2PSlist *list,
void (*action)(
void *data))
630 for(i = tools_gl2psListNbr(list); i > 0; i--){
631 (*action)(tools_gl2psListPointer(list, i-1));
635 #if defined(TOOLS_GL2PS_HAVE_LIBPNG)
637 static void tools_gl2psListRead(
tools_GL2PSlist *list,
int index,
void *data)
639 if((index < 0) || (index >= list->
n))
644 static void tools_gl2psEncodeBase64Block(
unsigned char in[3],
unsigned char out[4],
int len)
646 static const char cb64[] =
647 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
649 out[0] = cb64[ in[0] >> 2 ];
650 out[1] = cb64[ ((in[0] & 0x03) << 4) | ((in[1] & 0xf0) >> 4) ];
651 out[2] = (len > 1) ? cb64[ ((in[1] & 0x0f) << 2) | ((in[2] & 0xc0) >> 6) ] :
'=';
652 out[3] = (len > 2) ? cb64[ in[2] & 0x3f ] :
'=';
657 unsigned char *buffer, in[3], out[4];
658 int i, n, index, len;
660 n = list->
n * list->
size;
661 buffer = (
unsigned char*)tools_gl2psMalloc(n *
sizeof(
unsigned char));
662 memcpy(buffer, list->
array, n *
sizeof(
unsigned char));
663 tools_gl2psListReset(list);
668 for(i = 0; i < 3; i++) {
670 in[i] = buffer[index];
679 tools_gl2psEncodeBase64Block(in, out, len);
680 for(i = 0; i < 4; i++)
681 tools_gl2psListAdd(list, &out[i]);
684 tools_gl2psFree(buffer);
704 for(i = 1; i < prim->
numverts; i++){
719 for(i = 1; i < n; i++){
720 if(fabs(rgba[0][0] - rgba[i][0]) > threshold[0] ||
721 fabs(rgba[0][1] - rgba[i][1]) > threshold[1] ||
722 fabs(rgba[0][2] - rgba[i][2]) > threshold[2])
732 for(i = 0; i < 3; ++i){
733 tools_gl2ps_context->
lastrgba[i] = rgba[i];
749 pimag = pixels + 4 * (width * (height - 1 - y) + x);
753 pimag = pixels + 3 * (width * (height - 1 - y) + x);
756 *red = *pimag; pimag++;
757 *green = *pimag; pimag++;
758 *blue = *pimag; pimag++;
797 tools_gl2psFree(im->
pixels);
801 #if defined(TOOLS_GL2PS_HAVE_LIBPNG)
803 #if !defined(png_jmpbuf)
804 # define png_jmpbuf(png_ptr) ((png_ptr)->jmpbuf)
807 static void tools_gl2psUserWritePNG(png_structp png_ptr, png_bytep data, png_size_t length)
811 for(i = 0; i < length; i++)
812 tools_gl2psListAdd(png, &data[i]);
815 static void tools_gl2psUserFlushPNG(png_structp png_ptr)
824 unsigned char *row_data;
828 if(!(png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL)))
831 if(!(info_ptr = png_create_info_struct(png_ptr))){
832 png_destroy_write_struct(&png_ptr, NULL);
836 if(setjmp(png_jmpbuf(png_ptr))) {
837 png_destroy_write_struct(&png_ptr, &info_ptr);
841 png_set_write_fn(png_ptr, (
void *)png, tools_gl2psUserWritePNG, tools_gl2psUserFlushPNG);
843 png_set_IHDR(png_ptr, info_ptr, pixmap->
width, pixmap->
height, 8,
844 PNG_COLOR_TYPE_RGB, PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_BASE,
845 PNG_FILTER_TYPE_BASE);
846 png_write_info(png_ptr, info_ptr);
848 row_data = (
unsigned char*)tools_gl2psMalloc(3 * pixmap->
width *
sizeof(
unsigned char));
849 for(row = 0; row < pixmap->
height; row++){
850 for(col = 0; col < pixmap->
width; col++){
851 tools_gl2psGetRGB(pixmap, col, row, &dr, &dg, &db);
852 row_data[3*col] = (
unsigned char)(255. * dr);
853 row_data[3*col+1] = (
unsigned char)(255. * dg);
854 row_data[3*col+2] = (
unsigned char)(255. * db);
856 png_write_row(png_ptr, (png_bytep)row_data);
858 tools_gl2psFree(row_data);
860 png_write_end(png_ptr, info_ptr);
861 png_destroy_write_struct(&png_ptr, &info_ptr);
911 memcpy(prim->
verts[0].
rgba, color, 4 *
sizeof(
float));
925 prim->
data.
text->
str = (
char*)tools_gl2psMalloc((strlen(str)+1)*
sizeof(
char));
927 prim->
data.
text->
fontname = (
char*)tools_gl2psMalloc((strlen(fontname)+1)*
sizeof(char));
937 tools_gl2psListAdd(tools_gl2ps_context->
primitives, &prim);
940 tools_gl2psListAdd(tools_gl2ps_context->
auxprimitives, &prim);
950 text->
str = (
char*)tools_gl2psMalloc((strlen(t->
str)+1)*
sizeof(
char));
951 strcpy(text->
str, t->
str);
952 text->
fontname = (
char*)tools_gl2psMalloc((strlen(t->
fontname)+1)*
sizeof(char));
965 tools_gl2psFree(text->
str);
967 tools_gl2psFree(text);
990 if(!v || !tools_gl2ps_context)
998 switch(tools_gl2ps_context->
blendfunc[0]){
1047 tools_gl2psAssignTriangleProperties(t);
1053 tools_GL2PSvertex vertex = { {-1.0F, -1.0F, -1.0F}, {-1.0F, -1.0F, -1.0F, -1.0F} };
1054 for(i = 0; i < 3; i++)
1061 static void tools_gl2psResetLineProperties(
void)
1125 return (plane[0] * point[0] +
1126 plane[1] * point[1] +
1127 plane[2] * point[2] +
1133 return (a[0]*b[0] + a[1]*b[1] + a[2]*b[2]);
1138 c[0] = a[1]*b[2] - a[2]*b[1];
1139 c[1] = a[2]*b[0] - a[0]*b[2];
1140 c[2] = a[0]*b[1] - a[1]*b[0];
1145 return (
tools_GLfloat)sqrt(a[0]*a[0] + a[1]*a[1] + a[2]*a[2]);
1152 tools_gl2psPvec(a, b, c);
1182 plane[0] = plane[1] = 0.0F;
1187 tools_gl2psGetNormal(v, w, plane);
1191 - plane[2] * prim->
verts[0].
xyz[2];
1199 plane[0] = plane[1] = 0.0F;
1207 tools_gl2psGetNormal(v, w, plane);
1211 - plane[2] * prim->
verts[0].
xyz[2];
1219 plane[0] = plane[1] = 0.0F;
1225 plane[0] = plane[1] = plane[3] = 0.0F;
1237 v[0] = b->
xyz[0] - a->
xyz[0];
1238 v[1] = b->
xyz[1] - a->
xyz[1];
1239 v[2] = b->
xyz[2] - a->
xyz[2];
1242 sect = -tools_gl2psComparePointPlane(a->
xyz, plane) / psca;
1246 c->
xyz[0] = a->
xyz[0] + v[0] * sect;
1247 c->
xyz[1] = a->
xyz[1] + v[1] * sect;
1248 c->
xyz[2] = a->
xyz[2] + v[2] * sect;
1250 c->
rgba[0] = (1 - sect) * a->
rgba[0] + sect * b->
rgba[0];
1251 c->
rgba[1] = (1 - sect) * a->
rgba[1] + sect * b->
rgba[1];
1252 c->
rgba[2] = (1 - sect) * a->
rgba[2] + sect * b->
rgba[2];
1253 c->
rgba[3] = (1 - sect) * a->
rgba[3] + sect * b->
rgba[3];
1293 for(i = 0; i < numverts; i++){
1298 tools_gl2psCutEdge(&parent->
verts[index0[i]], &parent->
verts[index1[i]],
1299 plane, &child->
verts[i]);
1309 for(k = 0; k < *nb; k++){
1310 if((index0[k] == i && index1[k] == j) ||
1311 (index1[k] == i && index0[k] == j))
return;
1320 return (i < num - 1) ? i + 1 : 0;
1329 for(i = 0; i < prim->
numverts; i++){
1330 d[i] = tools_gl2psComparePointPlane(prim->
verts[i].
xyz, plane);
1337 for(i = 0; i < prim->
numverts; i++){
1338 j = tools_gl2psGetIndex(i, prim->
numverts);
1357 tools_GLshort i, j, in = 0, out = 0, in0[5], in1[5], out0[5], out1[5];
1363 for(i = 0; i < prim->
numverts; i++){
1364 d[i] = tools_gl2psComparePointPlane(prim->
verts[i].
xyz, plane);
1374 for(i = 0; i < prim->
numverts; i++){
1375 j = tools_gl2psGetIndex(i, prim->
numverts);
1380 tools_gl2psAddIndex(in0, in1, &in, i, j);
1381 tools_gl2psAddIndex(out0, out1, &out, i, j);
1384 tools_gl2psAddIndex(out0, out1, &out, j, -1);
1390 tools_gl2psAddIndex(in0, in1, &in, i, j);
1391 tools_gl2psAddIndex(out0, out1, &out, i, j);
1394 tools_gl2psAddIndex(in0, in1, &in, j, -1);
1397 tools_gl2psAddIndex(in0, in1, &in, j, -1);
1398 tools_gl2psAddIndex(out0, out1, &out, j, -1);
1407 tools_gl2psCreateSplitPrimitive(prim, plane, *back, out, out0, out1);
1408 tools_gl2psCreateSplitPrimitive(prim, plane, *front, in, in0, in1);
1420 (*t1)->numverts = (*t2)->numverts = 3;
1421 (*t1)->culled = (*t2)->culled = quad->
culled;
1422 (*t1)->offset = (*t2)->offset = quad->
offset;
1423 (*t1)->ofactor = (*t2)->ofactor = quad->
ofactor;
1424 (*t1)->ounits = (*t2)->ounits = quad->
ounits;
1425 (*t1)->pattern = (*t2)->pattern = quad->
pattern;
1426 (*t1)->factor = (*t2)->factor = quad->
factor;
1427 (*t1)->width = (*t2)->width = quad->
width;
1428 (*t1)->linecap = (*t2)->linecap = quad->
linecap;
1429 (*t1)->linejoin = (*t2)->linejoin = quad->
linejoin;
1432 (*t1)->verts[0] = quad->
verts[0];
1433 (*t1)->verts[1] = quad->
verts[1];
1434 (*t1)->verts[2] = quad->
verts[2];
1435 (*t1)->boundary = ((quad->
boundary & 1) ? 1 : 0) | ((quad->
boundary & 2) ? 2 : 0);
1436 (*t2)->verts[0] = quad->
verts[0];
1437 (*t2)->verts[1] = quad->
verts[2];
1438 (*t2)->verts[2] = quad->
verts[3];
1439 (*t2)->boundary = ((quad->
boundary & 4) ? 2 : 0) | ((quad->
boundary & 8) ? 4 : 0);
1442 static int tools_gl2psCompareDepth(
const void *a,
const void *b)
1456 for(i = 0; i < w->numverts; i++){
1473 static int tools_gl2psTrianglesFirst(
const void *a,
const void *b)
1479 return (q->
type < w->
type ? 1 : -1);
1489 if(!tools_gl2psListNbr(primitives)){
1497 maxp = tools_gl2psListNbr(primitives);
1501 for(i = 0; i < maxp; i++){
1503 tools_gl2psGetPlane(prim1, plane);
1505 for(j = 0; j < tools_gl2psListNbr(primitives); j++){
1508 count += tools_gl2psTestSplitPrimitive(prim2, plane);
1510 if(count > best)
break;
1516 if(!count)
return idx;
1530 while(list != NULL){
1533 tools_gl2psFree(list->
image);
1534 tools_gl2psFree(list);
1539 static void tools_tools_gl2psFreePrimitive(
void *data)
1544 tools_gl2psFree(q->
verts);
1546 tools_tools_gl2psFreeText(q->
data.
text);
1549 tools_tools_gl2psFreePixmap(q->
data.
image);
1559 tools_gl2psListAdd(list, &prim);
1562 tools_gl2psDivideQuad(prim, &t1, &t2);
1563 tools_gl2psListAdd(list, &t1);
1564 tools_gl2psListAdd(list, &t2);
1565 tools_tools_gl2psFreePrimitive(&prim);
1573 if((*tree)->back) tools_tools_gl2psFreeBspTree(&(*tree)->back);
1574 if((*tree)->primitives){
1575 tools_gl2psListAction((*tree)->primitives, tools_tools_gl2psFreePrimitive);
1576 tools_gl2psListDelete((*tree)->primitives);
1578 if((*tree)->front) tools_tools_gl2psFreeBspTree(&(*tree)->front);
1579 tools_gl2psFree(*tree);
1605 idx = tools_gl2psFindRoot(primitives, &prim);
1606 tools_gl2psGetPlane(prim, tree->
plane);
1607 tools_gl2psAddPrimitiveInList(prim, tree->
primitives);
1612 for(i = 0; i < tools_gl2psListNbr(primitives); i++){
1615 switch(tools_gl2psSplitPrimitive(prim, tree->
plane, &frontprim, &backprim)){
1617 tools_gl2psAddPrimitiveInList(prim, tree->
primitives);
1620 tools_gl2psAddPrimitiveInList(prim, backlist);
1623 tools_gl2psAddPrimitiveInList(prim, frontlist);
1626 tools_gl2psAddPrimitiveInList(backprim, backlist);
1627 tools_gl2psAddPrimitiveInList(frontprim, frontlist);
1628 tools_tools_gl2psFreePrimitive(&prim);
1635 tools_gl2psListSort(tree->
primitives, tools_gl2psTrianglesFirst);
1638 if(tools_gl2psListNbr(frontlist)){
1639 tools_gl2psListSort(frontlist, tools_gl2psTrianglesFirst);
1641 tools_gl2psBuildBspTree(tree->
front, frontlist);
1644 tools_gl2psListDelete(frontlist);
1647 if(tools_gl2psListNbr(backlist)){
1648 tools_gl2psListSort(backlist, tools_gl2psTrianglesFirst);
1650 tools_gl2psBuildBspTree(tree->
back, backlist);
1653 tools_gl2psListDelete(backlist);
1656 tools_gl2psListDelete(primitives);
1661 void (*action)(
void *data),
int inverse)
1667 result = tools_gl2psComparePointPlane(eye, tree->
plane);
1670 tools_gl2psTraverseBspTree(tree->
back, eye, epsilon, compare, action, inverse);
1672 tools_tools_gl2psListActionInverse(tree->
primitives, action);
1675 tools_gl2psListAction(tree->
primitives, action);
1677 tools_gl2psTraverseBspTree(tree->
front, eye, epsilon, compare, action, inverse);
1680 tools_gl2psTraverseBspTree(tree->
front, eye, epsilon, compare, action, inverse);
1682 tools_tools_gl2psListActionInverse(tree->
primitives, action);
1685 tools_gl2psListAction(tree->
primitives, action);
1687 tools_gl2psTraverseBspTree(tree->
back, eye, epsilon, compare, action, inverse);
1690 tools_gl2psTraverseBspTree(tree->
front, eye, epsilon, compare, action, inverse);
1691 tools_gl2psTraverseBspTree(tree->
back, eye, epsilon, compare, action, inverse);
1695 static void tools_gl2psRescaleAndOffset(
void)
1702 if(!tools_gl2psListNbr(tools_gl2ps_context->
primitives))
1707 minZ = maxZ = prim->
verts[0].
xyz[2];
1708 for(i = 1; i < prim->
numverts; i++){
1712 for(i = 1; i < tools_gl2psListNbr(tools_gl2ps_context->
primitives); i++){
1714 for(j = 0; j < prim->
numverts; j++){
1719 rangeZ = (maxZ - minZ);
1725 if(scaleZ > 100000.F) scaleZ = 100000.F;
1728 for(i = 0; i < tools_gl2psListNbr(tools_gl2ps_context->
primitives); i++){
1730 for(j = 0; j < prim->
numverts; j++){
1768 dZ = factor * maxdZ + units;
1786 plane[0] = b[1] - a[1];
1787 plane[1] = a[0] - b[0];
1788 n = (
tools_GLfloat)sqrt(plane[0]*plane[0] + plane[1]*plane[1]);
1793 plane[3] = -plane[0]*a[0]-plane[1]*a[1];
1807 if((*tree)->back) tools_tools_gl2psFreeBspImageTree(&(*tree)->back);
1808 if((*tree)->front) tools_tools_gl2psFreeBspImageTree(&(*tree)->front);
1809 tools_gl2psFree(*tree);
1818 pt_dis = tools_gl2psComparePointPlane(point, plane);
1832 if((*tree == NULL) && (prim->
numverts > 2)){
1838 for(i = 0; i < prim->
numverts-1; i++){
1839 if(!tools_tools_gl2psGetPlaneFromPoints(prim->
verts[i].
xyz,
1846 tools_gl2psFree(head);
1856 for(i = 2+offset; i < prim->
numverts; i++){
1857 ret = tools_gl2psCheckPoint(prim->
verts[i].
xyz, head->
plane);
1863 for(i = 1+offset; i < prim->
numverts-1; i++){
1864 if(cur->front == NULL){
1867 if(tools_tools_gl2psGetPlaneFromPoints(prim->
verts[i].
xyz,
1869 cur->front->plane)){
1875 if(cur->front == NULL){
1878 if(tools_tools_gl2psGetPlaneFromPoints(prim->
verts[i].
xyz,
1880 cur->front->plane)){
1881 cur->front->front = NULL;
1882 cur->front->back = NULL;
1885 tools_gl2psFree(cur->front);
1890 for(i = 0; i < 4; i++){
1894 for(i = 1+offset; i < prim->
numverts-1; i++){
1895 if(cur->front == NULL){
1898 if(tools_tools_gl2psGetPlaneFromPoints(prim->
verts[i+1].
xyz,
1900 cur->front->plane)){
1906 if(cur->front == NULL){
1909 if(tools_tools_gl2psGetPlaneFromPoints(prim->
verts[offset].
xyz,
1911 cur->front->plane)){
1912 cur->front->front = NULL;
1913 cur->front->back = NULL;
1916 tools_gl2psFree(cur->front);
1921 tools_gl2psFree(head);
1933 pos = tools_gl2psCheckPoint(prim->
verts[0].
xyz, plane);
1934 for(i = 1; i < prim->
numverts; i++){
1935 pos |= tools_gl2psCheckPoint(prim->
verts[i].
xyz, plane);
1975 for(i = 0; i < numverts; i++){
1976 child->
verts[i] = vertx[i];
1991 tools_GLint cur = -1, prev = -1, i, v1 = 0, v2 = 0, flag = 1, prev0 = -1;
1999 for(i = 0; i <= prim->
numverts; i++){
2008 cur = tools_gl2psCheckPoint(prim->
verts[v1].
xyz, plane);
2013 if(((prev == -1) || (prev == cur) || (prev == 0) || (cur == 0)) &&
2014 (i < prim->numverts)){
2019 front_list[front_count-1] = prim->
verts[v1];
2025 back_list[back_count-1] = prim->
verts[v1];
2031 front_list[front_count-1] = prim->
verts[v1];
2035 back_list[back_count-1] = prim->
verts[v1];
2039 else if((prev != cur) && (cur != 0) && (prev != 0)){
2047 tools_gl2psCutEdge(&prim->
verts[v2], &prim->
verts[v1],
2048 plane, &front_list[front_count-1]);
2052 back_list[back_count-1] = front_list[front_count-1];
2057 *front = tools_tools_gl2psCreateSplitPrimitive2D(prim, front_count, front_list);
2058 *back = tools_tools_gl2psCreateSplitPrimitive2D(prim, back_count, back_list);
2059 tools_gl2psFree(front_list);
2060 tools_gl2psFree(back_list);
2079 tools_gl2psAddPlanesInBspTreeImage(tools_gl2ps_context->
primitivetoadd, tree);
2084 switch(tools_gl2psCheckPrimitive(prim, (*tree)->plane)){
2087 if((*tree)->front != NULL)
return tools_gl2psAddInBspImageTree(prim, &(*tree)->front);
2090 tools_tools_gl2psSplitPrimitive2D(prim, (*tree)->plane, &frontprim, &backprim);
2091 ret = tools_gl2psAddInBspImageTree(backprim, &(*tree)->back);
2092 if((*tree)->front != NULL){
2093 if(tools_gl2psAddInBspImageTree(frontprim, &(*tree)->front)){
2097 tools_gl2psFree(frontprim->
verts);
2098 tools_gl2psFree(frontprim);
2099 tools_gl2psFree(backprim->verts);
2100 tools_gl2psFree(backprim);
2103 if((*tree)->back != NULL){
2105 ret = tools_gl2psAddInBspImageTree(prim, &(*tree)->back);
2109 if((*tree)->front != NULL){
2111 ret = tools_gl2psAddInBspImageTree(prim, &(*tree)->front);
2122 static void tools_tools_gl2psAddInImageTree(
void *data)
2129 else if(!tools_gl2psAddInBspImageTree(prim, &tools_gl2ps_context->
imagetree)){
2145 c[0] = c[1] = c[2] = 0.0F;
2146 for(i = 0; i < prim->
numverts; i++){
2153 for(i = 0; i < prim->
numverts; i++){
2171 v[0] = c[0] - prim->
verts[i].
xyz[0];
2172 v[1] = c[1] - prim->
verts[i].
xyz[1];
2174 norm = tools_gl2psNorm(v);
2182 norm = tools_gl2psNorm(v);
2205 tools_gl2psListAdd(list, &b);
2217 tools_gl2psBuildPolygonBoundary(tree->
back);
2218 for(i = 0; i < tools_gl2psListNbr(tree->
primitives); i++){
2222 tools_gl2psBuildPolygonBoundary(tree->
front);
2246 prim->
offset = (char)offset;
2251 prim->
width = width;
2259 tools_gl2psListAdd(tools_gl2ps_context->
primitives, &prim);
2287 static void tools_gl2psParseFeedbackBuffer(
tools_GLint used)
2292 tools_GLint i, sizeoffloat, count, v, vtot, offset = 0, factor = 0, auxindex = 0;
2294 tools_GLfloat lwidth = 1.0F, psize = 1.0F, ofactor = 0.0F, ounits = 0.0F;
2300 current = tools_gl2ps_context->
feedback;
2311 i = tools_gl2psGetVertex(&vertices[0], current);
2315 pattern, factor, psize, lcap, ljoin, 0);
2321 i = tools_gl2psGetVertex(&vertices[0], current);
2324 i = tools_gl2psGetVertex(&vertices[1], current);
2328 pattern, factor, lwidth, lcap, ljoin, 0);
2335 while(count > 0 && used > 0){
2336 i = tools_gl2psGetVertex(&vertices[v], current);
2337 tools_gl2psAdaptVertexForBlending(&vertices[v]);
2344 if(!count && vtot == 2) flag = 1|2|4;
2345 else if(!count) flag = 2|4;
2346 else if(vtot == 2) flag = 1|2;
2352 ounits, pattern, factor, 1, lcap, ljoin,
2354 vertices[1] = vertices[2];
2365 i = tools_gl2psGetVertex(&vertices[0], current);
2375 ofactor = current[1];
2378 ounits = current[1];
2426 lwidth = current[1];
2457 current += 2; used -= 2;
2458 i = tools_gl2psGetVertex(&prim->
verts[0], ¤t[1]);
2459 current += i; used -= i;
2462 current += 2; used -= 2;
2466 for(i = 1; i < 4; i++){
2467 for(v = 0; v < 3; v++){
2479 v = 2 * sizeoffloat;
2486 for(i = 0; i < vtot; i += sizeoffloat){
2487 current += 2; used -= 2;
2489 memcpy(&(((
char*)(node->
image->
pixels))[i + v]), &(current[2]), sizeoffloat);
2491 memcpy(&(((
char*)(node->
image->
pixels))[i + v]), &(current[2]), vtot - i);
2494 tools_gl2psListAdd(tools_gl2ps_context->
primitives, &prim);
2498 if(auxindex < tools_gl2psListNbr(tools_gl2ps_context->
auxprimitives))
2499 tools_gl2psListAdd(tools_gl2ps_context->
primitives,
2500 tools_gl2psListPointer(tools_gl2ps_context->
auxprimitives, auxindex++));
2502 tools_gl2psMsg(
TOOLS_GL2PS_ERROR,
"Wrong number of auxiliary tokens in buffer");
2525 static void tools_gl2psWriteByte(
unsigned char byte)
2527 unsigned char h =
byte / 16;
2528 unsigned char l =
byte % 16;
2529 tools_gl2psPrintf(
"%x%x", h, l);
2533 int greyscale,
int nbit)
2538 unsigned char red = 0, green = 0, blue = 0, b, grey;
2547 if((width <= 0) || (height <= 0))
return;
2549 tools_gl2psPrintf(
"gsave\n");
2550 tools_gl2psPrintf(
"%.2f %.2f translate\n", x, y);
2551 tools_gl2psPrintf(
"%.2f %.2f scale\n", width * im->
zoom_x, height * im->
zoom_y);
2554 tools_gl2psPrintf(
"/picstr %d string def\n", width);
2555 tools_gl2psPrintf(
"%d %d %d\n", width, height, 8);
2556 tools_gl2psPrintf(
"[ %d 0 0 -%d 0 %d ]\n", width, height, height);
2557 tools_gl2psPrintf(
"{ currentfile picstr readhexstring pop }\n");
2558 tools_gl2psPrintf(
"image\n");
2559 for(row = 0; row < height; row++){
2560 for(col = 0; col < width; col++){
2561 tools_gl2psGetRGB(im, col, row, &dr, &dg, &db);
2562 fgrey = (0.30F * dr + 0.59F * dg + 0.11F * db);
2563 grey = (
unsigned char)(255. * fgrey);
2564 tools_gl2psWriteByte(grey);
2566 tools_gl2psPrintf(
"\n");
2568 nbhex = width * height * 2;
2569 tools_gl2psPrintf(
"%%%% nbhex digit :%d\n", nbhex);
2573 nbits = nrgb * nbit;
2575 if((nbyte * 8) != nbits) nbyte++;
2576 tools_gl2psPrintf(
"/rgbstr %d string def\n", nbyte);
2577 tools_gl2psPrintf(
"%d %d %d\n", width, height, nbit);
2578 tools_gl2psPrintf(
"[ %d 0 0 -%d 0 %d ]\n", width, height, height);
2579 tools_gl2psPrintf(
"{ currentfile rgbstr readhexstring pop }\n");
2580 tools_gl2psPrintf(
"false 3\n");
2581 tools_gl2psPrintf(
"colorimage\n");
2582 for(row = 0; row < height; row++){
2586 for(ibyte = 0; ibyte < nbyte; ibyte++){
2589 tools_gl2psGetRGB(im, col, row, &dr, &dg, &db);
2595 red = (
unsigned char)(3. * dr);
2596 green = (
unsigned char)(3. * dg);
2597 blue = (
unsigned char)(3. * db);
2602 tools_gl2psGetRGB(im, col, row, &dr, &dg, &db);
2608 red = (
unsigned char)(3. * dr);
2609 green = (
unsigned char)(3. * dg);
2610 blue = (
unsigned char)(3. * db);
2612 tools_gl2psWriteByte(b);
2616 else if(icase == 2) {
2620 tools_gl2psGetRGB(im, col, row, &dr, &dg, &db);
2626 red = (
unsigned char)(3. * dr);
2627 green = (
unsigned char)(3. * dg);
2628 blue = (
unsigned char)(3. * db);
2631 tools_gl2psWriteByte(b);
2635 else if(icase == 3) {
2638 tools_gl2psGetRGB(im, col, row, &dr, &dg, &db);
2644 red = (
unsigned char)(3. * dr);
2645 green = (
unsigned char)(3. * dg);
2646 blue = (
unsigned char)(3. * db);
2650 tools_gl2psWriteByte(b);
2655 tools_gl2psPrintf(
"\n");
2660 nbits = nrgb * nbit;
2662 if((nbyte * 8) != nbits) nbyte++;
2663 tools_gl2psPrintf(
"/rgbstr %d string def\n", nbyte);
2664 tools_gl2psPrintf(
"%d %d %d\n", width, height, nbit);
2665 tools_gl2psPrintf(
"[ %d 0 0 -%d 0 %d ]\n", width, height, height);
2666 tools_gl2psPrintf(
"{ currentfile rgbstr readhexstring pop }\n");
2667 tools_gl2psPrintf(
"false 3\n");
2668 tools_gl2psPrintf(
"colorimage\n");
2669 for(row = 0; row < height; row++){
2672 for(ibyte = 0; ibyte < nbyte; ibyte++){
2675 tools_gl2psGetRGB(im, col, row, &dr, &dg, &db);
2681 red = (
unsigned char)(15. * dr);
2682 green = (
unsigned char)(15. * dg);
2683 tools_gl2psPrintf(
"%x%x", red, green);
2686 else if(icase == 2) {
2687 blue = (
unsigned char)(15. * db);
2689 tools_gl2psGetRGB(im, col, row, &dr, &dg, &db);
2695 red = (
unsigned char)(15. * dr);
2696 tools_gl2psPrintf(
"%x%x", blue, red);
2699 else if(icase == 3) {
2700 green = (
unsigned char)(15. * dg);
2701 blue = (
unsigned char)(15. * db);
2702 tools_gl2psPrintf(
"%x%x", green, blue);
2706 tools_gl2psPrintf(
"\n");
2711 tools_gl2psPrintf(
"/rgbstr %d string def\n", nbyte);
2712 tools_gl2psPrintf(
"%d %d %d\n", width, height, 8);
2713 tools_gl2psPrintf(
"[ %d 0 0 -%d 0 %d ]\n", width, height, height);
2714 tools_gl2psPrintf(
"{ currentfile rgbstr readhexstring pop }\n");
2715 tools_gl2psPrintf(
"false 3\n");
2716 tools_gl2psPrintf(
"colorimage\n");
2717 for(row = 0; row < height; row++){
2718 for(col = 0; col < width; col++){
2719 tools_gl2psGetRGB(im, col, row, &dr, &dg, &db);
2720 red = (
unsigned char)(255. * dr);
2721 tools_gl2psWriteByte(red);
2722 green = (
unsigned char)(255. * dg);
2723 tools_gl2psWriteByte(green);
2724 blue = (
unsigned char)(255. * db);
2725 tools_gl2psWriteByte(blue);
2727 tools_gl2psPrintf(
"\n");
2731 tools_gl2psPrintf(
"grestore\n");
2736 const unsigned char *imagemap){
2739 if((width <= 0) || (height <= 0))
return;
2741 size = height + height * (width - 1) / 8;
2743 tools_gl2psPrintf(
"gsave\n");
2744 tools_gl2psPrintf(
"%.2f %.2f translate\n", x, y);
2745 tools_gl2psPrintf(
"%d %d scale\n%d %d\ntrue\n", width, height,width, height);
2746 tools_gl2psPrintf(
"[ %d 0 0 -%d 0 %d ] {<", width, height, height);
2747 for(i = 0; i <
size; i++){
2748 tools_gl2psWriteByte(*imagemap);
2751 tools_gl2psPrintf(
">} imagemask\ngrestore\n");
2754 static void tools_gl2psPrintPostScriptHeader(
void)
2761 tools_gl2psPrintGzipHeader();
2766 tools_gl2psPrintf(
"%%!PS-Adobe-3.0\n");
2769 tools_gl2psPrintf(
"%%!PS-Adobe-3.0 EPSF-3.0\n");
2772 tools_gl2psPrintf(
"%%%%Title: %s\n"
2773 "%%%%Creator: GL2PS %d.%d.%d%s, %s\n"
2775 "%%%%CreationDate: %s"
2776 "%%%%LanguageLevel: 3\n"
2777 "%%%%DocumentData: Clean7Bit\n"
2781 tools_gl2ps_context->
producer, ctime(&now));
2784 tools_gl2psPrintf(
"%%%%Orientation: %s\n"
2785 "%%%%DocumentMedia: Default %d %d 0 () ()\n",
2788 (
int)tools_gl2ps_context->
viewport[2],
2790 (
int)tools_gl2ps_context->
viewport[3]);
2793 tools_gl2psPrintf(
"%%%%BoundingBox: %d %d %d %d\n"
2794 "%%%%EndComments\n",
2796 (
int)tools_gl2ps_context->
viewport[0],
2798 (
int)tools_gl2ps_context->
viewport[1],
2800 (
int)tools_gl2ps_context->
viewport[2],
2802 (
int)tools_gl2ps_context->
viewport[3]);
2817 tools_gl2psPrintf(
"%%%%BeginProlog\n"
2818 "/gl2psdict 64 dict def gl2psdict begin\n"
2819 "/tryPS3shading %s def %% set to false to force subdivision\n"
2820 "/rThreshold %g def %% red component subdivision threshold\n"
2821 "/gThreshold %g def %% green component subdivision threshold\n"
2822 "/bThreshold %g def %% blue component subdivision threshold\n",
2826 tools_gl2psPrintf(
"/BD { bind def } bind def\n"
2827 "/C { setrgbcolor } BD\n"
2828 "/G { 0.082 mul exch 0.6094 mul add exch 0.3086 mul add neg 1.0 add setgray } BD\n"
2829 "/W { setlinewidth } BD\n"
2830 "/LC { setlinecap } BD\n"
2831 "/LJ { setlinejoin } BD\n");
2833 tools_gl2psPrintf(
"/FC { findfont exch /SH exch def SH scalefont setfont } BD\n"
2834 "/SW { dup stringwidth pop } BD\n"
2835 "/S { FC moveto show } BD\n"
2836 "/SBC{ FC moveto SW -2 div 0 rmoveto show } BD\n"
2837 "/SBR{ FC moveto SW neg 0 rmoveto show } BD\n"
2838 "/SCL{ FC moveto 0 SH -2 div rmoveto show } BD\n"
2839 "/SCC{ FC moveto SW -2 div SH -2 div rmoveto show } BD\n"
2840 "/SCR{ FC moveto SW neg SH -2 div rmoveto show } BD\n"
2841 "/STL{ FC moveto 0 SH neg rmoveto show } BD\n"
2842 "/STC{ FC moveto SW -2 div SH neg rmoveto show } BD\n"
2843 "/STR{ FC moveto SW neg SH neg rmoveto show } BD\n");
2847 tools_gl2psPrintf(
"/FCT { FC translate 0 0 } BD\n"
2848 "/SR { gsave FCT moveto rotate show grestore } BD\n"
2849 "/SBCR{ gsave FCT moveto rotate SW -2 div 0 rmoveto show grestore } BD\n"
2850 "/SBRR{ gsave FCT moveto rotate SW neg 0 rmoveto show grestore } BD\n"
2851 "/SCLR{ gsave FCT moveto rotate 0 SH -2 div rmoveto show grestore} BD\n");
2852 tools_gl2psPrintf(
"/SCCR{ gsave FCT moveto rotate SW -2 div SH -2 div rmoveto show grestore} BD\n"
2853 "/SCRR{ gsave FCT moveto rotate SW neg SH -2 div rmoveto show grestore} BD\n"
2854 "/STLR{ gsave FCT moveto rotate 0 SH neg rmoveto show grestore } BD\n"
2855 "/STCR{ gsave FCT moveto rotate SW -2 div SH neg rmoveto show grestore } BD\n"
2856 "/STRR{ gsave FCT moveto rotate SW neg SH neg rmoveto show grestore } BD\n");
2858 tools_gl2psPrintf(
"/P { newpath 0.0 360.0 arc closepath fill } BD\n"
2859 "/LS { newpath moveto } BD\n"
2860 "/L { lineto } BD\n"
2861 "/LE { lineto stroke } BD\n"
2862 "/T { newpath moveto lineto lineto closepath fill } BD\n");
2867 tools_gl2psPrintf(
"/STshfill {\n"
2868 " /b1 exch def /g1 exch def /r1 exch def /y1 exch def /x1 exch def\n"
2869 " /b2 exch def /g2 exch def /r2 exch def /y2 exch def /x2 exch def\n"
2870 " /b3 exch def /g3 exch def /r3 exch def /y3 exch def /x3 exch def\n"
2871 " gsave << /ShadingType 4 /ColorSpace [/DeviceRGB]\n"
2872 " /DataSource [ 0 x1 y1 r1 g1 b1 0 x2 y2 r2 g2 b2 0 x3 y3 r3 g3 b3 ] >>\n"
2873 " shfill grestore } BD\n");
2879 "/Tm { 3 -1 roll 8 -1 roll 13 -1 roll add add 3 div\n"
2881 " 3 -1 roll 7 -1 roll 11 -1 roll add add 3 div\n"
2883 " 3 -1 roll 6 -1 roll 9 -1 roll add add 3 div"
2892 tools_gl2psPrintf(
"/STsplit {\n"
2893 " 4 index 15 index add 0.5 mul\n"
2894 " 4 index 15 index add 0.5 mul\n"
2895 " 4 index 15 index add 0.5 mul\n"
2896 " 4 index 15 index add 0.5 mul\n"
2897 " 4 index 15 index add 0.5 mul\n"
2898 " 5 copy 5 copy 25 15 roll\n");
2902 tools_gl2psPrintf(
" 9 index 30 index add 0.5 mul\n"
2903 " 9 index 30 index add 0.5 mul\n"
2904 " 9 index 30 index add 0.5 mul\n"
2905 " 9 index 30 index add 0.5 mul\n"
2906 " 9 index 30 index add 0.5 mul\n"
2907 " 5 copy 5 copy 35 5 roll 25 5 roll 15 5 roll\n");
2911 tools_gl2psPrintf(
" 4 index 10 index add 0.5 mul\n"
2912 " 4 index 10 index add 0.5 mul\n"
2913 " 4 index 10 index add 0.5 mul\n"
2914 " 4 index 10 index add 0.5 mul\n"
2915 " 4 index 10 index add 0.5 mul\n"
2916 " 5 copy 5 copy 40 5 roll 25 5 roll 15 5 roll 25 5 roll\n");
2920 tools_gl2psPrintf(
" STnoshfill STnoshfill STnoshfill STnoshfill } BD\n");
2926 tools_gl2psPrintf(
"/STnoshfill {\n"
2927 " 2 index 8 index sub abs rThreshold gt\n"
2929 " { 1 index 7 index sub abs gThreshold gt\n"
2931 " { dup 6 index sub abs bThreshold gt\n"
2933 " { 2 index 13 index sub abs rThreshold gt\n"
2935 " { 1 index 12 index sub abs gThreshold gt\n"
2937 " { dup 11 index sub abs bThreshold gt\n"
2939 " { 7 index 13 index sub abs rThreshold gt\n");
2940 tools_gl2psPrintf(
" { STsplit }\n"
2941 " { 6 index 12 index sub abs gThreshold gt\n"
2943 " { 5 index 11 index sub abs bThreshold gt\n"
2956 tools_gl2psPrintf(
"tryPS3shading\n"
2958 " { /ST { STshfill } BD }\n"
2959 " { /ST { STnoshfill } BD }\n"
2961 "{ /ST { STnoshfill } BD }\n"
2964 tools_gl2psPrintf(
"end\n"
2967 "/DeviceRGB setcolorspace\n"
2971 "%%%%BeginPageSetup\n");
2974 tools_gl2psPrintf(
"%d 0 translate 90 rotate\n",
2975 (
int)tools_gl2ps_context->
viewport[3]);
2978 tools_gl2psPrintf(
"%%%%EndPageSetup\n"
2984 tools_gl2psPrintf(
"%g %g %g C\n"
2985 "newpath %d %d moveto %d %d lineto %d %d lineto %d %d lineto\n"
2988 (
int)tools_gl2ps_context->
viewport[0], (
int)tools_gl2ps_context->
viewport[1], (
int)tools_gl2ps_context->
viewport[2],
2989 (
int)tools_gl2ps_context->
viewport[1], (
int)tools_gl2ps_context->
viewport[2], (
int)tools_gl2ps_context->
viewport[3],
2990 (
int)tools_gl2ps_context->
viewport[0], (
int)tools_gl2ps_context->
viewport[3]);
2996 if(!tools_gl2psSameColor(tools_gl2ps_context->
lastrgba, rgba)){
2997 tools_gl2psSetLastColor(rgba);
2998 tools_gl2psPrintf(
"%g %g %g C\n", rgba[0], rgba[1], rgba[2]);
3002 static void tools_gl2psResetPostScriptColor(
void)
3007 static void tools_gl2psEndPostScriptLine(
void)
3012 for(i = 0; i < 3; i++)
3014 for(i = 0; i < 4; i++)
3020 int *nb,
int array[10])
3023 int on[8] = {0, 0, 0, 0, 0, 0, 0, 0};
3024 int off[8] = {0, 0, 0, 0, 0, 0, 0, 0};
3028 for(n = 15; n >= 0; n--){
3029 tmp[n] = (char)(pattern & 0x01);
3034 for(i = 0; i < 8; i++){
3035 while(n < 16 && !tmp[n]){ off[i]++; n++; }
3036 while(n < 16 && tmp[n]){ on[i]++; n++; }
3037 if(n >= 15){ i++;
break; }
3046 for(n = i - 1; n >= 0; n--){
3047 array[(*nb)++] = factor * on[n];
3048 array[(*nb)++] = factor * off[n];
3049 if(*nb == 10)
break;
3055 int len = 0, i, n, array[10];
3063 if(!pattern || !factor){
3065 len += tools_gl2psPrintf(
"[] 0 %s\n", str);
3068 tools_gl2psParseStipplePattern(pattern, factor, &n, array);
3069 len += tools_gl2psPrintf(
"[");
3070 for(i = 0; i < n; i++){
3071 if(i) len += tools_gl2psPrintf(
" ");
3072 len += tools_gl2psPrintf(
"%d", array[i]);
3074 len += tools_gl2psPrintf(
"] 0 %s\n", str);
3080 static void tools_gl2psPrintPostScriptPrimitive(
void *data)
3098 tools_gl2psPrintPostScriptColor(prim->
verts[0].
rgba);
3099 tools_gl2psPrintf(
"%g %g %g P\n",
3116 tools_gl2psEndPostScriptLine();
3124 tools_gl2psPrintf(
"%g W\n", tools_gl2ps_context->
lastlinewidth);
3128 tools_gl2psPrintf(
"%d LC\n", tools_gl2ps_context->
lastlinecap);
3132 tools_gl2psPrintf(
"%d LJ\n", tools_gl2ps_context->
lastlinejoin);
3134 tools_gl2psPrintPostScriptDash(prim->
pattern, prim->
factor,
"setdash");
3135 tools_gl2psPrintPostScriptColor(prim->
verts[0].
rgba);
3136 tools_gl2psPrintf(
"%g %g %s\n", prim->
verts[0].
xyz[0], prim->
verts[0].
xyz[1],
3137 newline ?
"LS" :
"L");
3141 if(!tools_gl2psVertsSameColor(prim)){
3142 tools_gl2psResetPostScriptColor();
3143 tools_gl2psPrintf(
"%g %g %g %g %g %g %g %g %g %g %g %g %g %g %g ST\n",
3154 tools_gl2psPrintPostScriptColor(prim->
verts[0].
rgba);
3155 tools_gl2psPrintf(
"%g %g %g %g %g %g T\n",
3169 tools_gl2psPrintPostScriptColor(prim->
verts[0].
rgba);
3178 tools_gl2psPrintPostScriptColor(prim->
verts[0].
rgba);
3182 tools_gl2psPrintf(
"%g %g %d /%s ",
3187 tools_gl2psPrintf(prim->
data.
text->
angle ?
"SCCR\n" :
"SCC\n");
3190 tools_gl2psPrintf(prim->
data.
text->
angle ?
"SCLR\n" :
"SCL\n");
3193 tools_gl2psPrintf(prim->
data.
text->
angle ?
"SCRR\n" :
"SCR\n");
3196 tools_gl2psPrintf(prim->
data.
text->
angle ?
"SBCR\n" :
"SBC\n");
3199 tools_gl2psPrintf(prim->
data.
text->
angle ?
"SBRR\n" :
"SBR\n");
3202 tools_gl2psPrintf(prim->
data.
text->
angle ?
"STCR\n" :
"STC\n");
3205 tools_gl2psPrintf(prim->
data.
text->
angle ?
"STLR\n" :
"STL\n");
3208 tools_gl2psPrintf(prim->
data.
text->
angle ?
"STRR\n" :
"STR\n");
3228 static void tools_gl2psPrintPostScriptFooter(
void)
3230 tools_gl2psPrintf(
"grestore\n"
3238 tools_gl2psPrintGzipFooter();
3241 static void tools_gl2psPrintPostScriptBeginViewport(
tools_GLint viewport[4])
3245 int x = viewport[0], y = viewport[1], w = viewport[2], h = viewport[3];
3249 if(tools_gl2ps_context->
header){
3250 tools_gl2psPrintPostScriptHeader();
3254 tools_gl2psResetPostScriptColor();
3255 tools_gl2psResetLineProperties();
3257 tools_gl2psPrintf(
"gsave\n"
3266 rgba[0] = tools_gl2ps_context->
colormap[idx][0];
3267 rgba[1] = tools_gl2ps_context->
colormap[idx][1];
3268 rgba[2] = tools_gl2ps_context->
colormap[idx][2];
3271 tools_gl2psPrintf(
"%g %g %g C\n"
3272 "newpath %d %d moveto %d %d lineto %d %d lineto %d %d lineto\n"
3274 rgba[0], rgba[1], rgba[2],
3275 x, y, x+w, y, x+w, y+h, x, y+h);
3278 tools_gl2psPrintf(
"newpath %d %d moveto %d %d lineto %d %d lineto %d %d lineto\n"
3280 x, y, x+w, y, x+w, y+h, x, y+h);
3284 static tools_GLint tools_gl2psPrintPostScriptEndViewport(
void)
3288 res = tools_gl2psPrintPrimitives();
3289 tools_gl2psPrintf(
"grestore\n");
3293 static void tools_gl2psPrintPostScriptFinalPrimitive(
void)
3296 tools_gl2psEndPostScriptLine();
3302 tools_gl2psPrintPostScriptHeader,
3303 tools_gl2psPrintPostScriptFooter,
3304 tools_gl2psPrintPostScriptBeginViewport,
3305 tools_gl2psPrintPostScriptEndViewport,
3306 tools_gl2psPrintPostScriptPrimitive,
3307 tools_gl2psPrintPostScriptFinalPrimitive,
3313 tools_gl2psPrintPostScriptHeader,
3314 tools_gl2psPrintPostScriptFooter,
3315 tools_gl2psPrintPostScriptBeginViewport,
3316 tools_gl2psPrintPostScriptEndViewport,
3317 tools_gl2psPrintPostScriptPrimitive,
3318 tools_gl2psPrintPostScriptFinalPrimitive,
3320 "Encapsulated Postscript"
3329 static void tools_gl2psPrintTeXHeader(
void)
3335 if(tools_gl2ps_context->
filename && strlen(tools_gl2ps_context->
filename) < 256){
3336 for(i = (
int)strlen(tools_gl2ps_context->
filename) - 1; i >= 0; i--){
3337 if(tools_gl2ps_context->
filename[i] ==
'.'){
3338 strncpy(name, tools_gl2ps_context->
filename, i);
3343 if(i <= 0) strcpy(name, tools_gl2ps_context->
filename);
3346 strcpy(name,
"untitled");
3351 fprintf(tools_gl2ps_context->
stream,
3353 "%% Creator: GL2PS %d.%d.%d%s, %s\n"
3355 "%% CreationDate: %s",
3358 tools_gl2ps_context->
producer, ctime(&now));
3360 fprintf(tools_gl2ps_context->
stream,
3361 "\\setlength{\\unitlength}{1pt}\n"
3362 "\\begin{picture}(0,0)\n"
3363 "\\includegraphics{%s}\n"
3364 "\\end{picture}%%\n"
3365 "%s\\begin{picture}(%d,%d)(0,0)\n",
3367 (
int)tools_gl2ps_context->
viewport[2], (
int)tools_gl2ps_context->
viewport[3]);
3370 static void tools_gl2psPrintTeXPrimitive(
void *data)
3378 fprintf(tools_gl2ps_context->
stream,
"\\fontsize{%d}{0}\n\\selectfont",
3380 fprintf(tools_gl2ps_context->
stream,
"\\put(%g,%g)",
3384 fprintf(tools_gl2ps_context->
stream,
"{\\makebox(0,0)");
3387 fprintf(tools_gl2ps_context->
stream,
"{");
3390 fprintf(tools_gl2ps_context->
stream,
"[l]{");
3393 fprintf(tools_gl2ps_context->
stream,
"[r]{");
3396 fprintf(tools_gl2ps_context->
stream,
"[b]{");
3399 fprintf(tools_gl2ps_context->
stream,
"[br]{");
3402 fprintf(tools_gl2ps_context->
stream,
"[t]{");
3405 fprintf(tools_gl2ps_context->
stream,
"[tl]{");
3408 fprintf(tools_gl2ps_context->
stream,
"[tr]{");
3412 fprintf(tools_gl2ps_context->
stream,
"[bl]{");
3415 fprintf(tools_gl2ps_context->
stream,
"\\textcolor[rgb]{%g,%g,%g}{{%s}}",
3419 fprintf(tools_gl2ps_context->
stream,
"}");
3420 fprintf(tools_gl2ps_context->
stream,
"}}\n");
3433 static void tools_gl2psPrintTeXFooter(
void)
3435 fprintf(tools_gl2ps_context->
stream,
"\\end{picture}%s\n",
3439 static void tools_gl2psPrintTeXBeginViewport(
tools_GLint viewport[4])
3444 tools_gl2psResetLineProperties();
3446 if(tools_gl2ps_context->
header){
3447 tools_gl2psPrintTeXHeader();
3452 static tools_GLint tools_gl2psPrintTeXEndViewport(
void)
3454 return tools_gl2psPrintPrimitives();
3457 static void tools_gl2psPrintTeXFinalPrimitive(
void)
3464 tools_gl2psPrintTeXHeader,
3465 tools_gl2psPrintTeXFooter,
3466 tools_gl2psPrintTeXBeginViewport,
3467 tools_gl2psPrintTeXEndViewport,
3468 tools_gl2psPrintTeXPrimitive,
3469 tools_gl2psPrintTeXFinalPrimitive,
3480 static int tools_gl2psPrintPDFCompressorType(
void)
3482 #if defined(TOOLS_GL2PS_HAVE_ZLIB)
3484 return fprintf(tools_gl2ps_context->
stream,
"/Filter [/FlateDecode]\n");
3494 tools_gl2psSetLastColor(rgba);
3495 for(i = 0; i < 3; ++i){
3497 offs += tools_gl2psPrintf(
"%.0f ", 0.);
3498 else if(rgba[i] < 1e-4 || rgba[i] > 1e6)
3499 offs += tools_gl2psPrintf(
"%f ", rgba[i]);
3501 offs += tools_gl2psPrintf(
"%g ", rgba[i]);
3503 offs += tools_gl2psPrintf(
"RG\n");
3511 for(i = 0; i < 3; ++i){
3513 offs += tools_gl2psPrintf(
"%.0f ", 0.);
3514 else if(rgba[i] < 1e-4 || rgba[i] > 1e6)
3515 offs += tools_gl2psPrintf(
"%f ", rgba[i]);
3517 offs += tools_gl2psPrintf(
"%g ", rgba[i]);
3519 offs += tools_gl2psPrintf(
"rg\n");
3526 return tools_gl2psPrintf(
"%.0f w\n", 0.);
3527 else if(lw < 1e-4 || lw > 1e6)
3528 return tools_gl2psPrintf(
"%f w\n", lw);
3530 return tools_gl2psPrintf(
"%g w\n", lw);
3533 static int tools_gl2psPrintPDFLineCap(
tools_GLint lc)
3538 return tools_gl2psPrintf(
"%d J\n", lc);
3541 static int tools_gl2psPrintPDFLineJoin(
tools_GLint lj)
3546 return tools_gl2psPrintf(
"%d j\n", lj);
3553 if(text->
angle == 0.0F){
3569 "%f %f %f %f %f %f Tm\n"
3572 cnt, text->
fontsize, crad, srad, -srad, crad, x, y, text->
str);
3576 static void tools_gl2psPutPDFSpecial(
int prim,
int sec,
tools_GL2PSstring *text)
3578 tools_gl2ps_context->
streamlength += tools_gl2psPrintf(
"/GS%d%d gs\n", prim, sec);
3579 tools_gl2ps_context->
streamlength += tools_gl2psPrintf(
"%s\n", text->
str);
3586 "%d 0 0 %d %f %f cm\n"
3593 static void tools_tools_gl2psPDFstacksInit(
void)
3617 static void tools_tools_gl2psPDFgroupListInit(
void)
3630 int lastTriangleWasNotSimpleWithSameColor = 0;
3636 tools_gl2psInitTriangle(&lastt);
3638 for(i = 0; i < tools_gl2psListNbr(tools_gl2ps_context->
pdfprimlist); ++i){
3642 tools_tools_gl2psPDFgroupObjectInit(&gro);
3645 tools_gl2psListAdd(gro.
ptrlist, &p);
3646 tools_gl2psListAdd(tools_gl2ps_context->
pdfgrouplist, &gro);
3649 tools_tools_gl2psPDFgroupObjectInit(&gro);
3652 tools_gl2psListAdd(gro.
ptrlist, &p);
3653 tools_gl2psListAdd(tools_gl2ps_context->
pdfgrouplist, &gro);
3656 if(lasttype != p->
type || lastwidth != p->
width ||
3659 !tools_gl2psSameColor(p->
verts[0].
rgba, lastrgba)){
3660 tools_tools_gl2psPDFgroupObjectInit(&gro);
3662 tools_gl2psListAdd(gro.
ptrlist, &p);
3663 tools_gl2psListAdd(tools_gl2ps_context->
pdfgrouplist, &gro);
3666 tools_gl2psListAdd(gro.
ptrlist, &p);
3670 lastwidth = p->
width;
3678 if(lasttype != p->
type || lastwidth != p->
width ||
3679 !tools_gl2psSameColor(p->
verts[0].
rgba, lastrgba)){
3680 tools_tools_gl2psPDFgroupObjectInit(&gro);
3682 tools_gl2psListAdd(gro.
ptrlist, &p);
3683 tools_gl2psListAdd(tools_gl2ps_context->
pdfgrouplist, &gro);
3686 tools_gl2psListAdd(gro.
ptrlist, &p);
3688 lastwidth = p->
width;
3694 tools_gl2psFillTriangleFromPrimitive(&tmpt, p,
TOOLS_GL_TRUE);
3695 lastTriangleWasNotSimpleWithSameColor =
3699 lastTriangleWasNotSimpleWithSameColor){
3701 tools_gl2psListAdd(gro.
ptrlist, &p);
3704 tools_tools_gl2psPDFgroupObjectInit(&gro);
3706 tools_gl2psListAdd(gro.
ptrlist, &p);
3707 tools_gl2psListAdd(tools_gl2ps_context->
pdfgrouplist, &gro);
3712 tools_tools_gl2psPDFgroupObjectInit(&gro);
3714 tools_gl2psListAdd(gro.
ptrlist, &p);
3715 tools_gl2psListAdd(tools_gl2ps_context->
pdfgrouplist, &gro);
3732 if(!tools_gl2psListNbr(gro->
ptrlist))
3740 tools_gl2psFillTriangleFromPrimitive(&t, prim,
TOOLS_GL_TRUE);
3778 static void tools_tools_gl2psPDFgroupListWriteMainStream(
void)
3780 int i, j, lastel, count;
3788 count = tools_gl2psListNbr(tools_gl2ps_context->
pdfgrouplist);
3790 for(i = 0; i < count; ++i){
3793 lastel = tools_gl2psListNbr(gro->
ptrlist) - 1;
3801 tools_gl2ps_context->
streamlength += tools_gl2psPrintf(
"1 J\n");
3804 for(j = 0; j <= lastel; ++j){
3807 tools_gl2psPrintf(
"%f %f m %f %f l\n",
3811 tools_gl2ps_context->
streamlength += tools_gl2psPrintf(
"S\n");
3812 tools_gl2ps_context->
streamlength += tools_gl2psPrintf(
"0 J\n");
3825 tools_gl2psPrintf(
"%f %f m\n",
3828 for(j = 1; j <= lastel; ++j){
3831 if(!tools_gl2psSamePosition(prim->
verts[0].
xyz, prev->verts[1].xyz)){
3836 tools_gl2psPrintf(
"%f %f l\n",
3837 prev->verts[1].xyz[0], prev->verts[1].xyz[1]);
3839 tools_gl2psPrintf(
"%f %f m\n",
3846 tools_gl2psPrintf(
"%f %f l\n",
3852 tools_gl2psPrintf(
"%f %f l\n",
3854 tools_gl2ps_context->
streamlength += tools_gl2psPrintf(
"S\n");
3857 tools_gl2psFillTriangleFromPrimitive(&t, prim,
TOOLS_GL_TRUE);
3858 tools_gl2psSortOutTrianglePDFgroup(gro);
3863 for(j = 0; j <= lastel; ++j){
3867 += tools_gl2psPrintf(
"%f %f m\n"
3879 tools_gl2ps_context->
streamlength += tools_gl2psPrintf(
"q\n"
3883 for(j = 0; j <= lastel; ++j){
3887 += tools_gl2psPrintf(
"%f %f m\n"
3895 tools_gl2ps_context->
streamlength += tools_gl2psPrintf(
"Q\n");
3901 tools_gl2ps_context->
streamlength += tools_gl2psPrintf(
"q\n"
3906 for(j = 0; j <= lastel; ++j){
3910 += tools_gl2psPrintf(
"%f %f m\n"
3918 tools_gl2ps_context->
streamlength += tools_gl2psPrintf(
"Q\n");
3923 tools_gl2ps_context->
streamlength += tools_gl2psPrintf(
"/Sh%d sh\n", gro->
shno);
3929 tools_gl2ps_context->
streamlength += tools_gl2psPrintf(
"q\n"
3939 tools_gl2ps_context->
streamlength += tools_gl2psPrintf(
"q\n"
3948 for(j = 0; j <= lastel; ++j){
3955 for(j = 0; j <= lastel; ++j){
3963 lastel = tools_gl2psListNbr(gro->
ptrlist) - 1;
3967 for(j = 0; j <= lastel; ++j){
3969 tools_gl2psPutPDFSpecial(i, j, prim->
data.
text);
3979 static int tools_tools_gl2psPDFgroupListWriteGStateResources(
void)
3985 offs += fprintf(tools_gl2ps_context->
stream,
3989 for(i = 0; i < tools_gl2psListNbr(tools_gl2ps_context->
pdfgrouplist); ++i){
3992 offs += fprintf(tools_gl2ps_context->
stream,
"/GS%d %d 0 R\n", gro->
gsno, gro->
gsobjno);
3994 offs += fprintf(tools_gl2ps_context->
stream,
">>\n");
4000 static int tools_tools_gl2psPDFgroupListWriteShaderResources(
void)
4006 offs += fprintf(tools_gl2ps_context->
stream,
4009 for(i = 0; i < tools_gl2psListNbr(tools_gl2ps_context->
pdfgrouplist); ++i){
4012 offs += fprintf(tools_gl2ps_context->
stream,
"/Sh%d %d 0 R\n", gro->
shno, gro->
shobjno);
4016 offs += fprintf(tools_gl2ps_context->
stream,
">>\n");
4021 static int tools_tools_gl2psPDFgroupListWriteXObjectResources(
void)
4028 offs += fprintf(tools_gl2ps_context->
stream,
4032 for(i = 0; i < tools_gl2psListNbr(tools_gl2ps_context->
pdfgrouplist); ++i){
4034 if(!tools_gl2psListNbr(gro->
ptrlist))
4042 offs += fprintf(tools_gl2ps_context->
stream,
"/Im%d %d 0 R\n", gro->
imno, gro->
imobjno);
4052 offs += fprintf(tools_gl2ps_context->
stream,
">>\n");
4058 static int tools_tools_gl2psPDFgroupListWriteFontResources(
void)
4064 offs += fprintf(tools_gl2ps_context->
stream,
"/Font\n<<\n");
4066 for(i = 0; i < tools_gl2psListNbr(tools_gl2ps_context->
pdfgrouplist); ++i){
4073 offs += fprintf(tools_gl2ps_context->
stream,
">>\n");
4078 static void tools_tools_gl2psPDFgroupListDelete(
void)
4086 for(i = 0; i < tools_gl2psListNbr(tools_gl2ps_context->
pdfgrouplist); ++i){
4088 tools_gl2psListDelete(gro->
ptrlist);
4091 tools_gl2psListDelete(tools_gl2ps_context->
pdfgrouplist);
4097 static int tools_gl2psPrintPDFInfo(
void)
4104 newtime = gmtime(&now);
4106 offs = fprintf(tools_gl2ps_context->
stream,
4110 "/Creator (GL2PS %d.%d.%d%s, %s)\n"
4117 offs += fprintf(tools_gl2ps_context->
stream,
4123 offs += fprintf(tools_gl2ps_context->
stream,
4124 "/CreationDate (D:%d%02d%02d%02d%02d%02d)\n"
4127 newtime->tm_year+1900,
4138 static int tools_gl2psPrintPDFCatalog(
void)
4140 return fprintf(tools_gl2ps_context->
stream,
4149 static int tools_gl2psPrintPDFPages(
void)
4151 return fprintf(tools_gl2ps_context->
stream,
4163 static int tools_gl2psOpenPDFDataStream(
void)
4167 offs += fprintf(tools_gl2ps_context->
stream,
4170 "/Length 5 0 R\n" );
4171 offs += tools_gl2psPrintPDFCompressorType();
4172 offs += fprintf(tools_gl2ps_context->
stream,
4180 static int tools_tools_gl2psOpenPDFDataStreamWritePreface(
void)
4184 offs = tools_gl2psPrintf(
"/GSa gs\n");
4187 offs += tools_gl2psPrintPDFFillColor(tools_gl2ps_context->
bgcolor);
4188 offs += tools_gl2psPrintf(
"%d %d %d %d re\n",
4189 (
int)tools_gl2ps_context->
viewport[0], (
int)tools_gl2ps_context->
viewport[1],
4190 (
int)tools_gl2ps_context->
viewport[2], (
int)tools_gl2ps_context->
viewport[3]);
4191 offs += tools_gl2psPrintf(
"f\n");
4198 static void tools_gl2psPrintPDFHeader(
void)
4202 tools_tools_gl2psPDFstacksInit();
4204 tools_gl2ps_context->
xreflist = (
int*)tools_gl2psMalloc(
sizeof(
int) * tools_gl2ps_context->
objects_stack);
4206 #if defined(TOOLS_GL2PS_HAVE_ZLIB)
4208 tools_gl2psSetupCompress();
4211 tools_gl2ps_context->
xreflist[0] = 0;
4212 offs += fprintf(tools_gl2ps_context->
stream,
"%%PDF-1.4\n");
4213 tools_gl2ps_context->
xreflist[1] = offs;
4215 offs += tools_gl2psPrintPDFInfo();
4216 tools_gl2ps_context->
xreflist[2] = offs;
4218 offs += tools_gl2psPrintPDFCatalog();
4219 tools_gl2ps_context->
xreflist[3] = offs;
4221 offs += tools_gl2psPrintPDFPages();
4222 tools_gl2ps_context->
xreflist[4] = offs;
4224 offs += tools_gl2psOpenPDFDataStream();
4225 tools_gl2ps_context->
xreflist[5] = offs;
4226 tools_gl2ps_context->
streamlength = tools_tools_gl2psOpenPDFDataStreamWritePreface();
4231 static void tools_gl2psPrintPDFPrimitive(
void *data)
4238 prim = tools_gl2psCopyPrimitive(prim);
4239 tools_gl2psListAdd(tools_gl2ps_context->
pdfprimlist, &prim);
4244 static int tools_gl2psClosePDFDataStream(
void)
4248 #if defined(TOOLS_GL2PS_HAVE_ZLIB)
4250 if(Z_OK != tools_gl2psDeflate())
4253 fwrite(tools_gl2ps_context->
compress->dest, tools_gl2ps_context->
compress->destLen, 1, tools_gl2ps_context->
stream);
4257 tools_gl2psFreeCompress();
4261 offs += fprintf(tools_gl2ps_context->
stream,
4269 static int tools_gl2psPrintPDFDataStreamLength(
int val)
4271 return fprintf(tools_gl2ps_context->
stream,
4279 static int tools_gl2psPrintPDFOpenPage(
void)
4285 offs = fprintf(tools_gl2ps_context->
stream,
4290 "/MediaBox [%d %d %d %d]\n",
4291 (
int)tools_gl2ps_context->
viewport[0], (
int)tools_gl2ps_context->
viewport[1],
4292 (
int)tools_gl2ps_context->
viewport[2], (
int)tools_gl2ps_context->
viewport[3]);
4295 offs += fprintf(tools_gl2ps_context->
stream,
"/Rotate -90\n");
4297 offs += fprintf(tools_gl2ps_context->
stream,
4301 "/ProcSet [/PDF /Text /ImageB /ImageC] %%/ImageI\n");
4308 static int tools_tools_gl2psPDFgroupListWriteVariableResources(
void)
4313 offs += tools_tools_gl2psPDFgroupListWriteGStateResources();
4316 offs += tools_tools_gl2psPDFgroupListWriteShaderResources();
4319 offs += tools_tools_gl2psPDFgroupListWriteXObjectResources();
4322 offs += tools_tools_gl2psPDFgroupListWriteFontResources();
4325 offs += fprintf(tools_gl2ps_context->
stream,
4334 static int tools_gl2psPrintPDFGSObject(
void)
4336 return fprintf(tools_gl2ps_context->
stream,
4339 "/Type /ExtGState\n"
4354 static int tools_tools_tools_gl2psPrintPDFShaderStreamDataCoord(
tools_GL2PSvertex *vertex,
4355 int (*action)(
unsigned long data,
int size),
4363 double dmax = (double)~1UL;
4367 if(
sizeof(
unsigned long) == 8) dmax = dmax - 2048.;
4369 offs += (*action)(edgeflag, 1);
4375 offs += (*action)(0, 4);
4376 offs += (*action)(0, 4);
4379 diff = (vertex->
xyz[0] - xmin) / dx;
4384 imap = (
unsigned long)(
diff * dmax);
4385 offs += (*action)(imap, 4);
4387 diff = (vertex->
xyz[1] - ymin) / dy;
4392 imap = (
unsigned long)(
diff * dmax);
4393 offs += (*action)(imap, 4);
4401 static int tools_tools_tools_gl2psPrintPDFShaderStreamDataRGB(
tools_GL2PSvertex *vertex,
4402 int (*action)(
unsigned long data,
int size))
4407 double dmax = (double)~1UL;
4410 if(
sizeof(
unsigned long) == 8) dmax = dmax - 2048.;
4412 imap = (
unsigned long)((vertex->
rgba[0]) * dmax);
4413 offs += (*action)(imap, 1);
4415 imap = (
unsigned long)((vertex->
rgba[1]) * dmax);
4416 offs += (*action)(imap, 1);
4418 imap = (
unsigned long)((vertex->
rgba[2]) * dmax);
4419 offs += (*action)(imap, 1);
4426 static int tools_tools_tools_gl2psPrintPDFShaderStreamDataAlpha(
tools_GL2PSvertex *vertex,
4427 int (*action)(
unsigned long data,
int size),
4433 double dmax = (double)~1UL;
4436 if(
sizeof(
unsigned long) == 8) dmax = dmax - 2048.;
4438 if(sigbyte != 8 && sigbyte != 16)
4443 imap = (
unsigned long)((vertex->
rgba[3]) * dmax);
4445 offs += (*action)(imap, sigbyte);
4455 int (*action)(
unsigned long data,
int size),
4461 if(gray && gray != 8 && gray != 16)
4464 for(i = 0; i < 3; ++i){
4465 offs += tools_tools_tools_gl2psPrintPDFShaderStreamDataCoord(&triangle->
vertex[i], action,
4466 dx, dy, xmin, ymin);
4469 offs += tools_tools_tools_gl2psPrintPDFShaderStreamDataAlpha(&v, action, gray);
4472 offs += tools_tools_tools_gl2psPrintPDFShaderStreamDataRGB(&triangle->
vertex[i], action);
4490 for(i = 0; i < cnt; ++i){
4491 for(j = 0; j < 3; ++j){
4492 if(*xmin >
triangles[i].vertex[j].xyz[0])
4494 if(*xmax <
triangles[i].vertex[j].xyz[0])
4496 if(*ymin >
triangles[i].vertex[j].xyz[1])
4498 if(*ymax <
triangles[i].vertex[j].xyz[1])
4512 int i, offs = 0, vertexbytes, done = 0;
4517 vertexbytes = 1+4+4+1+1+1;
4520 vertexbytes = 1+4+4+1;
4523 vertexbytes = 1+4+4+2;
4527 vertexbytes = 1+4+4+1;
4531 tools_tools_gl2psPDFRectHull(&xmin, &xmax, &ymin, &ymax,
triangles,
size);
4533 offs += fprintf(tools_gl2ps_context->
stream,
4538 "/BitsPerCoordinate 32 "
4539 "/BitsPerComponent %d "
4541 "/Decode [%f %f %f %f 0 1 %s] ",
4543 (gray) ?
"/DeviceGray" :
"/DeviceRGB",
4545 xmin, xmax, ymin, ymax,
4546 (gray) ?
"" :
"0 1 0 1");
4548 #if defined(TOOLS_GL2PS_HAVE_ZLIB)
4550 tools_gl2psAllocCompress(vertexbytes *
size * 3);
4552 for(i = 0; i <
size; ++i)
4553 tools_tools_gl2psPrintPDFShaderStreamData(&
triangles[i],
4554 xmax-xmin, ymax-ymin, xmin, ymin,
4555 tools_gl2psWriteBigEndianCompress, gray);
4557 if(Z_OK == tools_gl2psDeflate() && 23 + tools_gl2ps_context->
compress->destLen < tools_gl2ps_context->
compress->srcLen){
4558 offs += tools_gl2psPrintPDFCompressorType();
4559 offs += fprintf(tools_gl2ps_context->
stream,
4563 (
int)tools_gl2ps_context->
compress->destLen);
4564 offs += tools_gl2ps_context->
compress->destLen * fwrite(tools_gl2ps_context->
compress->dest,
4565 tools_gl2ps_context->
compress->destLen,
4566 1, tools_gl2ps_context->
stream);
4569 tools_gl2psFreeCompress();
4576 offs += fprintf(tools_gl2ps_context->
stream,
4580 vertexbytes * 3 *
size);
4581 for(i = 0; i <
size; ++i)
4582 offs += tools_tools_gl2psPrintPDFShaderStreamData(&
triangles[i],
4583 xmax-xmin, ymax-ymin, xmin, ymin,
4584 tools_gl2psWriteBigEndian, gray);
4587 offs += fprintf(tools_gl2ps_context->
stream,
4596 static int tools_tools_gl2psPrintPDFShaderMask(
int obj,
int childobj)
4600 offs += fprintf(tools_gl2ps_context->
stream,
4605 "/BBox [ %d %d %d %d ]\n"
4606 "/Group \n<<\n/S /Transparency /CS /DeviceRGB\n"
4609 (
int)tools_gl2ps_context->
viewport[0], (
int)tools_gl2ps_context->
viewport[1],
4610 (
int)tools_gl2ps_context->
viewport[2], (
int)tools_gl2ps_context->
viewport[3]);
4613 ? (
int)strlen(
"/TrSh sh\n") + (int)log10((
double)childobj)+1
4614 : (
int)strlen(
"/TrSh0 sh\n");
4616 offs += fprintf(tools_gl2ps_context->
stream,
4621 offs += fprintf(tools_gl2ps_context->
stream,
4624 offs += fprintf(tools_gl2ps_context->
stream,
4635 static int tools_tools_gl2psPrintPDFShaderExtGS(
int obj,
int childobj)
4639 offs += fprintf(tools_gl2ps_context->
stream,
4644 offs += fprintf(tools_gl2ps_context->
stream,
4645 "/SMask << /S /Alpha /G %d 0 R >> ",
4648 offs += fprintf(tools_gl2ps_context->
stream,
4656 static int tools_tools_gl2psPrintPDFShaderSimpleExtGS(
int obj,
tools_GLfloat alpha)
4660 offs += fprintf(tools_gl2ps_context->
stream,
4673 int (*action)(
unsigned long data,
int size),
4682 if(gray && gray != 8 && gray != 16)
4687 shift = (
sizeof(
unsigned long) - 1) * 8;
4689 for(y = 0; y < im->
height; ++y){
4690 for(x = 0; x < im->
width; ++x){
4691 a = tools_gl2psGetRGB(im, x, y, &r, &g, &b);
4693 (*action)((
unsigned long)(a * 255) << shift, gray);
4696 (*action)((
unsigned long)(r * 255) << shift, 1);
4697 (*action)((
unsigned long)(g * 255) << shift, 1);
4698 (*action)((
unsigned long)(b * 255) << shift, 1);
4711 static int tools_gl2psPrintPDFPixmap(
int obj,
int childobj,
tools_GL2PSimage *im,
int gray)
4713 int offs = 0, done = 0, sigbytes = 3;
4715 if(gray && gray !=8 && gray != 16)
4719 sigbytes = gray / 8;
4721 offs += fprintf(tools_gl2ps_context->
stream,
4729 "/BitsPerComponent 8\n",
4732 (gray) ?
"/DeviceGray" :
"/DeviceRGB" );
4734 offs += fprintf(tools_gl2ps_context->
stream,
4739 #if defined(TOOLS_GL2PS_HAVE_ZLIB)
4741 tools_gl2psAllocCompress((
int)(im->
width * im->
height * sigbytes));
4743 tools_tools_gl2psPrintPDFPixmapStreamData(im, tools_gl2psWriteBigEndianCompress, gray);
4745 if(Z_OK == tools_gl2psDeflate() && 23 + tools_gl2ps_context->
compress->destLen < tools_gl2ps_context->
compress->srcLen){
4746 offs += tools_gl2psPrintPDFCompressorType();
4747 offs += fprintf(tools_gl2ps_context->
stream,
4751 (
int)tools_gl2ps_context->
compress->destLen);
4752 offs += tools_gl2ps_context->
compress->destLen * fwrite(tools_gl2ps_context->
compress->dest, tools_gl2ps_context->
compress->destLen,
4753 1, tools_gl2ps_context->
stream);
4756 tools_gl2psFreeCompress();
4763 offs += fprintf(tools_gl2ps_context->
stream,
4768 offs += tools_tools_gl2psPrintPDFPixmapStreamData(im, tools_gl2psWriteBigEndian, gray);
4771 offs += fprintf(tools_gl2ps_context->
stream,
4778 static int tools_gl2psPrintPDFText(
int obj,
tools_GL2PSstring *s,
int fontnumber)
4782 offs += fprintf(tools_gl2ps_context->
stream,
4789 "/Encoding /MacRomanEncoding\n"
4798 static int tools_tools_gl2psPDFgroupListWriteObjects(
int entryoffs)
4803 int offs = entryoffs;
4810 for(i = 0; i < tools_gl2psListNbr(tools_gl2ps_context->
pdfgrouplist); ++i){
4812 if(!tools_gl2psListNbr(gro->
ptrlist))
4823 for(j = 0; j <
size; ++j){
4833 offs += tools_tools_gl2psPrintPDFShaderSimpleExtGS(gro->
gsobjno,
triangles[0].vertex[0].rgba[3]);
4874 static void tools_gl2psPrintPDFFooter(
void)
4878 tools_tools_gl2psPDFgroupListInit();
4879 tools_tools_gl2psPDFgroupListWriteMainStream();
4882 offs += tools_gl2psClosePDFDataStream();
4883 tools_gl2ps_context->
xreflist[5] = offs;
4885 offs += tools_gl2psPrintPDFDataStreamLength(tools_gl2ps_context->
streamlength);
4886 tools_gl2ps_context->
xreflist[6] = offs;
4889 offs += tools_gl2psPrintPDFOpenPage();
4890 offs += tools_tools_gl2psPDFgroupListWriteVariableResources();
4891 tools_gl2ps_context->
xreflist = (
int*)tools_gl2psRealloc(tools_gl2ps_context->
xreflist,
4893 tools_gl2ps_context->
xreflist[7] = offs;
4895 offs += tools_gl2psPrintPDFGSObject();
4896 tools_gl2ps_context->
xreflist[8] = offs;
4899 tools_tools_gl2psPDFgroupListWriteObjects(tools_gl2ps_context->
xreflist[8]);
4903 fprintf(tools_gl2ps_context->
stream,
4909 fprintf(tools_gl2ps_context->
stream,
"%010d 00000 n \n", tools_gl2ps_context->
xreflist[i]);
4911 fprintf(tools_gl2ps_context->
stream,
4923 tools_gl2psFree(tools_gl2ps_context->
xreflist);
4924 tools_gl2psListAction(tools_gl2ps_context->
pdfprimlist, tools_tools_gl2psFreePrimitive);
4925 tools_gl2psListDelete(tools_gl2ps_context->
pdfprimlist);
4926 tools_tools_gl2psPDFgroupListDelete();
4928 #if defined(TOOLS_GL2PS_HAVE_ZLIB)
4930 tools_gl2psFreeCompress();
4931 tools_gl2psFree(tools_gl2ps_context->
compress);
4932 tools_gl2ps_context->
compress = NULL;
4939 static void tools_gl2psPrintPDFBeginViewport(
tools_GLint viewport[4])
4944 int x = viewport[0], y = viewport[1], w = viewport[2], h = viewport[3];
4948 tools_gl2psResetLineProperties();
4950 if(tools_gl2ps_context->
header){
4951 tools_gl2psPrintPDFHeader();
4955 offs += tools_gl2psPrintf(
"q\n");
4963 rgba[0] = tools_gl2ps_context->
colormap[idx][0];
4964 rgba[1] = tools_gl2ps_context->
colormap[idx][1];
4965 rgba[2] = tools_gl2ps_context->
colormap[idx][2];
4968 offs += tools_gl2psPrintPDFFillColor(rgba);
4969 offs += tools_gl2psPrintf(
"%d %d %d %d re\n"
4975 offs += tools_gl2psPrintf(
"%d %d %d %d re\n"
4984 static tools_GLint tools_gl2psPrintPDFEndViewport(
void)
4988 res = tools_gl2psPrintPrimitives();
4989 tools_gl2ps_context->
streamlength += tools_gl2psPrintf(
"Q\n");
4993 static void tools_gl2psPrintPDFFinalPrimitive(
void)
5000 tools_gl2psPrintPDFHeader,
5001 tools_gl2psPrintPDFFooter,
5002 tools_gl2psPrintPDFBeginViewport,
5003 tools_gl2psPrintPDFEndViewport,
5004 tools_gl2psPrintPDFPrimitive,
5005 tools_gl2psPrintPDFFinalPrimitive,
5007 "Portable Document Format"
5016 static void tools_tools_gl2psSVGGetCoordsAndColors(
int n,
tools_GL2PSvertex *verts,
5021 for(i = 0; i < n; i++){
5022 xyz[i][0] = verts[i].
xyz[0];
5023 xyz[i][1] = tools_gl2ps_context->
viewport[3] - verts[i].
xyz[1];
5025 for(j = 0; j < 4; j++)
5026 rgba[i][j] = verts[i].rgba[j];
5030 static void tools_tools_gl2psSVGGetColorString(
tools_GL2PSrgba rgba,
char str[32])
5032 int r = (int)(255. * rgba[0]);
5033 int g = (int)(255. * rgba[1]);
5034 int b = (int)(255. * rgba[2]);
5035 int rc = (r < 0) ? 0 : (r > 255) ? 255 : r;
5036 int gc = (g < 0) ? 0 : (g > 255) ? 255 : g;
5037 int bc = (b < 0) ? 0 : (b > 255) ? 255 : b;
5038 sprintf(str,
"#%2.2x%2.2x%2.2x", rc, gc, bc);
5041 static void tools_gl2psPrintSVGHeader(
void)
5043 int x, y, width, height;
5050 x = (int)tools_gl2ps_context->
viewport[1];
5051 y = (
int)tools_gl2ps_context->
viewport[0];
5052 width = (int)tools_gl2ps_context->
viewport[3];
5053 height = (
int)tools_gl2ps_context->
viewport[2];
5056 x = (int)tools_gl2ps_context->
viewport[0];
5057 y = (
int)tools_gl2ps_context->
viewport[1];
5058 width = (int)tools_gl2ps_context->
viewport[2];
5059 height = (
int)tools_gl2ps_context->
viewport[3];
5063 tools_gl2psPrintGzipHeader();
5065 tools_gl2psPrintf(
"<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>\n");
5066 tools_gl2psPrintf(
"<svg xmlns=\"http://www.w3.org/2000/svg\"\n");
5067 tools_gl2psPrintf(
" xmlns:xlink=\"http://www.w3.org/1999/xlink\"\n"
5068 " width=\"%dpx\" height=\"%dpx\" viewBox=\"%d %d %d %d\">\n",
5069 width, height, x, y, width, height);
5070 tools_gl2psPrintf(
"<title>%s</title>\n", tools_gl2ps_context->
title);
5071 tools_gl2psPrintf(
"<desc>\n");
5072 tools_gl2psPrintf(
"Creator: GL2PS %d.%d.%d%s, %s\n"
5077 tools_gl2psPrintf(
"</desc>\n");
5078 tools_gl2psPrintf(
"<defs>\n");
5079 tools_gl2psPrintf(
"</defs>\n");
5082 tools_tools_gl2psSVGGetColorString(tools_gl2ps_context->
bgcolor, col);
5083 tools_gl2psPrintf(
"<polygon fill=\"%s\" points=\"%d,%d %d,%d %d,%d %d,%d\"/>\n", col,
5084 (
int)tools_gl2ps_context->
viewport[0], (
int)tools_gl2ps_context->
viewport[1],
5085 (
int)tools_gl2ps_context->
viewport[2], (
int)tools_gl2ps_context->
viewport[1],
5086 (
int)tools_gl2ps_context->
viewport[2], (
int)tools_gl2ps_context->
viewport[3],
5087 (
int)tools_gl2ps_context->
viewport[0], (
int)tools_gl2ps_context->
viewport[3]);
5091 tools_gl2psPrintf(
"<g>\n");
5105 if(tools_tools_gl2psSameColorThreshold(3, rgba, tools_gl2ps_context->
threshold)){
5106 tools_tools_gl2psSVGGetColorString(rgba[0], col);
5107 tools_gl2psPrintf(
"<polygon fill=\"%s\" ", col);
5108 if(rgba[0][3] < 1.0F) tools_gl2psPrintf(
"fill-opacity=\"%g\" ", rgba[0][3]);
5109 tools_gl2psPrintf(
"shape-rendering=\"crispEdges\" ");
5110 tools_gl2psPrintf(
"points=\"%g,%g %g,%g %g,%g\"/>\n", xyz[0][0], xyz[0][1],
5111 xyz[1][0], xyz[1][1], xyz[2][0], xyz[2][1]);
5115 for(i = 0; i < 3; i++){
5116 xyz2[0][i] = xyz[0][i];
5117 xyz2[1][i] = 0.5F * (xyz[0][i] + xyz[1][i]);
5118 xyz2[2][i] = 0.5F * (xyz[0][i] + xyz[2][i]);
5120 for(i = 0; i < 4; i++){
5121 rgba2[0][i] = rgba[0][i];
5122 rgba2[1][i] = 0.5F * (rgba[0][i] + rgba[1][i]);
5123 rgba2[2][i] = 0.5F * (rgba[0][i] + rgba[2][i]);
5125 tools_gl2psPrintSVGSmoothTriangle(xyz2, rgba2);
5126 for(i = 0; i < 3; i++){
5127 xyz2[0][i] = 0.5F * (xyz[0][i] + xyz[1][i]);
5128 xyz2[1][i] = xyz[1][i];
5129 xyz2[2][i] = 0.5F * (xyz[1][i] + xyz[2][i]);
5131 for(i = 0; i < 4; i++){
5132 rgba2[0][i] = 0.5F * (rgba[0][i] + rgba[1][i]);
5133 rgba2[1][i] = rgba[1][i];
5134 rgba2[2][i] = 0.5F * (rgba[1][i] + rgba[2][i]);
5136 tools_gl2psPrintSVGSmoothTriangle(xyz2, rgba2);
5137 for(i = 0; i < 3; i++){
5138 xyz2[0][i] = 0.5F * (xyz[0][i] + xyz[2][i]);
5139 xyz2[1][i] = xyz[2][i];
5140 xyz2[2][i] = 0.5F * (xyz[1][i] + xyz[2][i]);
5142 for(i = 0; i < 4; i++){
5143 rgba2[0][i] = 0.5F * (rgba[0][i] + rgba[2][i]);
5144 rgba2[1][i] = rgba[2][i];
5145 rgba2[2][i] = 0.5F * (rgba[1][i] + rgba[2][i]);
5147 tools_gl2psPrintSVGSmoothTriangle(xyz2, rgba2);
5148 for(i = 0; i < 3; i++){
5149 xyz2[0][i] = 0.5F * (xyz[0][i] + xyz[1][i]);
5150 xyz2[1][i] = 0.5F * (xyz[1][i] + xyz[2][i]);
5151 xyz2[2][i] = 0.5F * (xyz[0][i] + xyz[2][i]);
5153 for(i = 0; i < 4; i++){
5154 rgba2[0][i] = 0.5F * (rgba[0][i] + rgba[1][i]);
5155 rgba2[1][i] = 0.5F * (rgba[1][i] + rgba[2][i]);
5156 rgba2[2][i] = 0.5F * (rgba[0][i] + rgba[2][i]);
5158 tools_gl2psPrintSVGSmoothTriangle(xyz2, rgba2);
5164 int i, n, array[10];
5166 if(!pattern || !factor)
return;
5168 tools_gl2psParseStipplePattern(pattern, factor, &n, array);
5169 tools_gl2psPrintf(
"stroke-dasharray=\"");
5170 for(i = 0; i < n; i++){
5171 if(i) tools_gl2psPrintf(
",");
5172 tools_gl2psPrintf(
"%d", array[i]);
5174 tools_gl2psPrintf(
"\" ");
5177 static void tools_gl2psEndSVGLine(
void)
5181 tools_gl2psPrintf(
"%g,%g\"/>\n", tools_gl2ps_context->
lastvertex.
xyz[0],
5183 for(i = 0; i < 3; i++)
5185 for(i = 0; i < 4; i++)
5192 #if defined(TOOLS_GL2PS_HAVE_LIBPNG)
5203 png = tools_gl2psListCreate(pixmap->
width * pixmap->
height * 3, 1000,
5204 sizeof(
unsigned char));
5205 tools_gl2psConvertPixmapToPNG(pixmap, png);
5206 tools_gl2psListEncodeBase64(png);
5211 tools_gl2psPrintf(
"<image x=\"%g\" y=\"%g\" width=\"%d\" height=\"%d\"\n",
5213 tools_gl2psPrintf(
"transform=\"matrix(%g,0,0,%g,%g,%g)\"\n",
5215 tools_gl2psPrintf(
"xlink:href=\"data:image/png;base64,");
5216 for(i = 0; i < tools_gl2psListNbr(png); i++){
5217 tools_gl2psListRead(png, i, &c);
5218 tools_gl2psPrintf(
"%c", c);
5220 tools_gl2psPrintf(
"\"/>\n");
5221 tools_gl2psListDelete(png);
5223 (void) x; (void) y; (void) pixmap;
5225 "order to embed images in SVG streams");
5229 static void tools_gl2psPrintSVGPrimitive(
void *data)
5235 char lcap[7], ljoin[7];
5247 tools_tools_gl2psSVGGetCoordsAndColors(prim->
numverts, prim->
verts, xyz, rgba);
5251 tools_tools_gl2psSVGGetColorString(rgba[0], col);
5252 tools_gl2psPrintf(
"<circle fill=\"%s\" ", col);
5253 if(rgba[0][3] < 1.0F) tools_gl2psPrintf(
"fill-opacity=\"%g\" ", rgba[0][3]);
5254 tools_gl2psPrintf(
"cx=\"%g\" cy=\"%g\" r=\"%g\"/>\n",
5255 xyz[0][0], xyz[0][1], 0.5 * prim->
width);
5269 tools_gl2psEndSVGLine();
5276 tools_gl2psSetLastColor(prim->
verts[0].
rgba);
5283 tools_tools_gl2psSVGGetColorString(rgba[0], col);
5284 tools_gl2psPrintf(
"<polyline fill=\"none\" stroke=\"%s\" stroke-width=\"%g\" ",
5291 sprintf (lcap,
"%s",
"round");
5294 sprintf (lcap,
"%s",
"square");
5302 sprintf (ljoin,
"%s",
"miter");
5305 sprintf (ljoin,
"%s",
"round");
5308 sprintf (ljoin,
"%s",
"bevel");
5311 sprintf (ljoin,
"%s",
"miter");
5314 tools_gl2psPrintf(
"stroke-linecap=\"%s\" stroke-linejoin=\"%s\" ",
5316 if(rgba[0][3] < 1.0F) tools_gl2psPrintf(
"stroke-opacity=\"%g\" ", rgba[0][3]);
5318 tools_gl2psPrintf(
"points=\"%g,%g ", xyz[0][0], xyz[0][1]);
5321 tools_gl2psPrintf(
"%g,%g ", xyz[0][0], xyz[0][1]);
5325 tools_gl2psPrintSVGSmoothTriangle(xyz, rgba);
5331 tools_gl2psPrintSVGPixmap(xyz[0][0], xyz[0][1], prim->
data.
image);
5334 tools_tools_gl2psSVGGetColorString(prim->
verts[0].
rgba, col);
5335 tools_gl2psPrintf(
"<text fill=\"%s\" x=\"%g\" y=\"%g\" font-size=\"%d\" ",
5338 tools_gl2psPrintf(
"transform=\"rotate(%g, %g, %g)\" ",
5342 tools_gl2psPrintf(
"text-anchor=\"middle\" dy=\"%d\" ",
5346 tools_gl2psPrintf(
"text-anchor=\"start\" dy=\"%d\" ",
5350 tools_gl2psPrintf(
"text-anchor=\"end\" dy=\"%d\" ",
5354 tools_gl2psPrintf(
"text-anchor=\"middle\" dy=\"0\" ");
5357 tools_gl2psPrintf(
"text-anchor=\"end\" dy=\"0\" ");
5360 tools_gl2psPrintf(
"text-anchor=\"middle\" dy=\"%d\" ",
5364 tools_gl2psPrintf(
"text-anchor=\"start\" dy=\"%d\" ",
5368 tools_gl2psPrintf(
"text-anchor=\"end\" dy=\"%d\" ",
5373 tools_gl2psPrintf(
"text-anchor=\"start\" dy=\"0\" ");
5377 tools_gl2psPrintf(
"font-family=\"Times\">");
5379 tools_gl2psPrintf(
"font-family=\"Times\" font-weight=\"bold\">");
5381 tools_gl2psPrintf(
"font-family=\"Times\" font-style=\"italic\">");
5383 tools_gl2psPrintf(
"font-family=\"Times\" font-style=\"italic\" font-weight=\"bold\">");
5385 tools_gl2psPrintf(
"font-family=\"Helvetica\" font-weight=\"bold\">");
5387 tools_gl2psPrintf(
"font-family=\"Helvetica\" font-style=\"oblique\">");
5389 tools_gl2psPrintf(
"font-family=\"Helvetica\" font-style=\"oblique\" font-weight=\"bold\">");
5391 tools_gl2psPrintf(
"font-family=\"Courier\" font-weight=\"bold\">");
5393 tools_gl2psPrintf(
"font-family=\"Courier\" font-style=\"oblique\">");
5395 tools_gl2psPrintf(
"font-family=\"Courier\" font-style=\"oblique\" font-weight=\"bold\">");
5398 tools_gl2psPrintf(
"%s</text>\n", prim->
data.
text->
str);
5411 static void tools_gl2psPrintSVGFooter(
void)
5413 tools_gl2psPrintf(
"</g>\n");
5414 tools_gl2psPrintf(
"</svg>\n");
5416 tools_gl2psPrintGzipFooter();
5419 static void tools_gl2psPrintSVGBeginViewport(
tools_GLint viewport[4])
5424 int x = viewport[0], y = viewport[1], w = viewport[2], h = viewport[3];
5428 tools_gl2psResetLineProperties();
5430 if(tools_gl2ps_context->
header){
5431 tools_gl2psPrintSVGHeader();
5441 rgba[0] = tools_gl2ps_context->
colormap[idx][0];
5442 rgba[1] = tools_gl2ps_context->
colormap[idx][1];
5443 rgba[2] = tools_gl2ps_context->
colormap[idx][2];
5446 tools_tools_gl2psSVGGetColorString(rgba, col);
5447 tools_gl2psPrintf(
"<polygon fill=\"%s\" points=\"%d,%d %d,%d %d,%d %d,%d\" ", col,
5448 x, tools_gl2ps_context->
viewport[3] - y,
5449 x + w, tools_gl2ps_context->
viewport[3] - y,
5450 x + w, tools_gl2ps_context->
viewport[3] - (y + h),
5451 x, tools_gl2ps_context->
viewport[3] - (y + h));
5452 tools_gl2psPrintf(
"shape-rendering=\"crispEdges\"/>\n");
5455 tools_gl2psPrintf(
"<clipPath id=\"cp%d%d%d%d\">\n", x, y, w, h);
5456 tools_gl2psPrintf(
" <polygon points=\"%d,%d %d,%d %d,%d %d,%d\"/>\n",
5457 x, tools_gl2ps_context->
viewport[3] - y,
5458 x + w, tools_gl2ps_context->
viewport[3] - y,
5459 x + w, tools_gl2ps_context->
viewport[3] - (y + h),
5460 x, tools_gl2ps_context->
viewport[3] - (y + h));
5461 tools_gl2psPrintf(
"</clipPath>\n");
5462 tools_gl2psPrintf(
"<g clip-path=\"url(#cp%d%d%d%d)\">\n", x, y, w, h);
5465 static tools_GLint tools_gl2psPrintSVGEndViewport(
void)
5469 res = tools_gl2psPrintPrimitives();
5470 tools_gl2psPrintf(
"</g>\n");
5474 static void tools_gl2psPrintSVGFinalPrimitive(
void)
5477 tools_gl2psEndSVGLine();
5483 tools_gl2psPrintSVGHeader,
5484 tools_gl2psPrintSVGFooter,
5485 tools_gl2psPrintSVGBeginViewport,
5486 tools_gl2psPrintSVGEndViewport,
5487 tools_gl2psPrintSVGPrimitive,
5488 tools_gl2psPrintSVGFinalPrimitive,
5490 "Scalable Vector Graphics"
5501 if(!tools_gl2psSameColor(tools_gl2ps_context->
lastrgba, rgba)){
5502 tools_gl2psSetLastColor(rgba);
5503 fprintf(tools_gl2ps_context->
stream,
"\\color[rgb]{%f,%f,%f}\n", rgba[0], rgba[1], rgba[2]);
5507 static void tools_gl2psPrintPGFHeader(
void)
5513 fprintf(tools_gl2ps_context->
stream,
5515 "%% Creator: GL2PS %d.%d.%d%s, %s\n"
5517 "%% CreationDate: %s",
5520 tools_gl2ps_context->
producer, ctime(&now));
5522 fprintf(tools_gl2ps_context->
stream,
"\\begin{pgfpicture}\n");
5524 tools_gl2psPrintPGFColor(tools_gl2ps_context->
bgcolor);
5525 fprintf(tools_gl2ps_context->
stream,
5526 "\\pgfpathrectanglecorners{"
5527 "\\pgfpoint{%dpt}{%dpt}}{\\pgfpoint{%dpt}{%dpt}}\n"
5528 "\\pgfusepath{fill}\n",
5529 (
int)tools_gl2ps_context->
viewport[0], (
int)tools_gl2ps_context->
viewport[1],
5530 (
int)tools_gl2ps_context->
viewport[2], (
int)tools_gl2ps_context->
viewport[3]);
5536 int i, n, array[10];
5544 if(!pattern || !factor){
5546 fprintf(tools_gl2ps_context->
stream,
"\\pgfsetdash{}{0pt}\n");
5549 tools_gl2psParseStipplePattern(pattern, factor, &n, array);
5550 fprintf(tools_gl2ps_context->
stream,
"\\pgfsetdash{");
5551 for(i = 0; i < n; i++) fprintf(tools_gl2ps_context->
stream,
"{%dpt}", array[i]);
5552 fprintf(tools_gl2ps_context->
stream,
"}{0pt}\n");
5556 static const char *tools_tools_gl2psPGFTextAlignment(
int align)
5568 default :
return "south west";
5572 static void tools_gl2psPrintPGFPrimitive(
void *data)
5581 tools_gl2psPrintPGFColor(prim->
verts[0].
rgba);
5582 fprintf(tools_gl2ps_context->
stream,
5583 "\\pgfpathrectangle{\\pgfpoint{%fpt}{%fpt}}"
5584 "{\\pgfpoint{%fpt}{%fpt}}\n\\pgfusepath{fill}\n",
5590 tools_gl2psPrintPGFColor(prim->
verts[0].
rgba);
5593 fprintf(tools_gl2ps_context->
stream,
"\\pgfsetlinewidth{%fpt}\n", tools_gl2ps_context->
lastlinewidth);
5599 fprintf(tools_gl2ps_context->
stream,
"\\pgfset%s\n",
"buttcap");
5602 fprintf(tools_gl2ps_context->
stream,
"\\pgfset%s\n",
"roundcap");
5605 fprintf(tools_gl2ps_context->
stream,
"\\pgfset%s\n",
"rectcap");
5613 fprintf(tools_gl2ps_context->
stream,
"\\pgfset%s\n",
"miterjoin");
5616 fprintf(tools_gl2ps_context->
stream,
"\\pgfset%s\n",
"roundjoin");
5619 fprintf(tools_gl2ps_context->
stream,
"\\pgfset%s\n",
"beveljoin");
5624 fprintf(tools_gl2ps_context->
stream,
5625 "\\pgfpathmoveto{\\pgfpoint{%fpt}{%fpt}}\n"
5626 "\\pgflineto{\\pgfpoint{%fpt}{%fpt}}\n"
5627 "\\pgfusepath{stroke}\n",
5634 fprintf(tools_gl2ps_context->
stream,
"\\pgfsetlinewidth{0.01pt}\n");
5640 fprintf(tools_gl2ps_context->
stream,
"\\pgfset%s\n",
"buttcap");
5643 fprintf(tools_gl2ps_context->
stream,
"\\pgfset%s\n",
"roundcap");
5646 fprintf(tools_gl2ps_context->
stream,
"\\pgfset%s\n",
"rectcap");
5654 fprintf(tools_gl2ps_context->
stream,
"\\pgfset%s\n",
"miterjoin");
5657 fprintf(tools_gl2ps_context->
stream,
"\\pgfset%s\n",
"roundjoin");
5660 fprintf(tools_gl2ps_context->
stream,
"\\pgfset%s\n",
"beveljoin");
5664 tools_gl2psPrintPGFColor(prim->
verts[0].
rgba);
5665 fprintf(tools_gl2ps_context->
stream,
5666 "\\pgfpathmoveto{\\pgfpoint{%fpt}{%fpt}}\n"
5667 "\\pgflineto{\\pgfpoint{%fpt}{%fpt}}\n"
5668 "\\pgflineto{\\pgfpoint{%fpt}{%fpt}}\n"
5670 "\\pgfusepath{fill,stroke}\n",
5676 fprintf(tools_gl2ps_context->
stream,
"{\n\\pgftransformshift{\\pgfpoint{%fpt}{%fpt}}\n",
5680 fprintf(tools_gl2ps_context->
stream,
"\\pgftransformrotate{%f}{", prim->
data.
text->
angle);
5682 fprintf(tools_gl2ps_context->
stream,
"\\pgfnode{rectangle}{%s}{\\fontsize{%d}{0}\\selectfont",
5686 fprintf(tools_gl2ps_context->
stream,
"\\textcolor[rgb]{%g,%g,%g}{{%s}}",
5690 fprintf(tools_gl2ps_context->
stream,
"}{}{\\pgfusepath{discard}}}");
5693 fprintf(tools_gl2ps_context->
stream,
"}");
5695 fprintf(tools_gl2ps_context->
stream,
"\n");
5708 static void tools_gl2psPrintPGFFooter(
void)
5710 fprintf(tools_gl2ps_context->
stream,
"\\end{pgfpicture}\n");
5713 static void tools_gl2psPrintPGFBeginViewport(
tools_GLint viewport[4])
5717 int x = viewport[0], y = viewport[1], w = viewport[2], h = viewport[3];
5721 tools_gl2psResetLineProperties();
5723 if(tools_gl2ps_context->
header){
5724 tools_gl2psPrintPGFHeader();
5728 fprintf(tools_gl2ps_context->
stream,
"\\begin{pgfscope}\n");
5735 rgba[0] = tools_gl2ps_context->
colormap[idx][0];
5736 rgba[1] = tools_gl2ps_context->
colormap[idx][1];
5737 rgba[2] = tools_gl2ps_context->
colormap[idx][2];
5740 tools_gl2psPrintPGFColor(rgba);
5741 fprintf(tools_gl2ps_context->
stream,
5742 "\\pgfpathrectangle{\\pgfpoint{%dpt}{%dpt}}"
5743 "{\\pgfpoint{%dpt}{%dpt}}\n"
5744 "\\pgfusepath{fill}\n",
5748 fprintf(tools_gl2ps_context->
stream,
5749 "\\pgfpathrectangle{\\pgfpoint{%dpt}{%dpt}}"
5750 "{\\pgfpoint{%dpt}{%dpt}}\n"
5751 "\\pgfusepath{clip}\n",
5755 static tools_GLint tools_gl2psPrintPGFEndViewport(
void)
5758 res = tools_gl2psPrintPrimitives();
5759 fprintf(tools_gl2ps_context->
stream,
"\\end{pgfscope}\n");
5763 static void tools_gl2psPrintPGFFinalPrimitive(
void)
5770 tools_gl2psPrintPGFHeader,
5771 tools_gl2psPrintPGFFooter,
5772 tools_gl2psPrintPGFBeginViewport,
5773 tools_gl2psPrintPGFEndViewport,
5774 tools_gl2psPrintPGFPrimitive,
5775 tools_gl2psPrintPGFFinalPrimitive,
5777 "PGF Latex Graphics"
5798 static void tools_gl2psComputeTightBoundingBox(
void *data)
5805 for(i = 0; i < prim->
numverts; i++){
5817 static tools_GLint tools_gl2psPrintPrimitives(
void)
5833 tools_gl2psParseFeedbackBuffer(used);
5835 tools_gl2psRescaleAndOffset();
5837 if(tools_gl2ps_context->
header){
5838 if(tools_gl2psListNbr(tools_gl2ps_context->
primitives) &&
5840 tools_gl2ps_context->
viewport[0] = tools_gl2ps_context->
viewport[1] = 100000;
5841 tools_gl2ps_context->
viewport[2] = tools_gl2ps_context->
viewport[3] = -100000;
5842 tools_gl2psListAction(tools_gl2ps_context->
primitives, tools_gl2psComputeTightBoundingBox);
5848 if(!tools_gl2psListNbr(tools_gl2ps_context->
primitives)){
5853 switch(tools_gl2ps_context->
sort){
5855 tools_gl2psListAction(tools_gl2ps_context->
primitives, tools_gl2psbackends[tools_gl2ps_context->
format]->printPrimitive);
5856 tools_gl2psListAction(tools_gl2ps_context->
primitives, tools_tools_gl2psFreePrimitive);
5858 tools_gl2psListReset(tools_gl2ps_context->
primitives);
5861 tools_gl2psListSort(tools_gl2ps_context->
primitives, tools_gl2psCompareDepth);
5863 tools_tools_gl2psListActionInverse(tools_gl2ps_context->
primitives, tools_tools_gl2psAddInImageTree);
5864 tools_tools_gl2psFreeBspImageTree(&tools_gl2ps_context->
imagetree);
5866 tools_gl2psListAction(tools_gl2ps_context->
primitives, tools_gl2psbackends[tools_gl2ps_context->
format]->printPrimitive);
5867 tools_gl2psListAction(tools_gl2ps_context->
primitives, tools_tools_gl2psFreePrimitive);
5869 tools_gl2psListReset(tools_gl2ps_context->
primitives);
5873 tools_gl2psBuildBspTree(root, tools_gl2ps_context->
primitives);
5877 tools_tools_gl2psAddInImageTree, 1);
5878 tools_tools_gl2psFreeBspImageTree(&tools_gl2ps_context->
imagetree);
5881 tools_gl2psbackends[tools_gl2ps_context->
format]->printPrimitive, 0);
5882 tools_tools_gl2psFreeBspTree(&root);
5898 "TOOLS_GL2PS_DRAW_BACKGROUND are incompatible.");
5903 "TOOLS_GL2PS_USE_CURRENT_VIEWPORT are incompatible.");
5907 tools_gl2psMsg(
TOOLS_GL2PS_ERROR,
"Option TOOLS_GL2PS_NO_OPENGL_CONTEXT requires "
5908 "option TOOLS_GL2PS_NO_BLENDING.");
5912 tools_gl2psMsg(
TOOLS_GL2PS_ERROR,
"Option TOOLS_GL2PS_NO_OPENGL_CONTEXT requires colormode "
5913 "to be TOOLS_GL_RGBA.");
5932 FILE *stream,
const char *filename)
5937 if(tools_gl2ps_context){
5938 tools_gl2psMsg(
TOOLS_GL2PS_ERROR,
"tools_gl2psBeginPage called in wrong program state");
5945 if (tools_gl2psCheckOptions(options, colormode) ==
TOOLS_GL_FALSE) {
5946 tools_gl2psFree(tools_gl2ps_context);
5947 tools_gl2ps_context = NULL;
5951 if(format >= 0 && format < (
tools_GLint)(
sizeof(tools_gl2psbackends) /
sizeof(tools_gl2psbackends[0]))){
5952 tools_gl2ps_context->
format = format;
5956 tools_gl2psFree(tools_gl2ps_context);
5957 tools_gl2ps_context = NULL;
5965 tools_gl2ps_context->
sort = sort;
5969 tools_gl2psFree(tools_gl2ps_context);
5970 tools_gl2ps_context = NULL;
5975 tools_gl2ps_context->
stream = stream;
5979 tools_gl2psFree(tools_gl2ps_context);
5980 tools_gl2ps_context = NULL;
5987 tools_gl2ps_context->
options = options;
5988 tools_gl2ps_context->
compress = NULL;
5996 for(i = 0; i < 4; i++){
5997 tools_gl2ps_context->
viewport[i] = viewport[i];
6001 if(!tools_gl2ps_context->
viewport[2] || !tools_gl2ps_context->
viewport[3]){
6002 tools_gl2psMsg(
TOOLS_GL2PS_ERROR,
"Incorrect viewport (x=%d, y=%d, width=%d, height=%d)",
6005 tools_gl2psFree(tools_gl2ps_context);
6006 tools_gl2ps_context = NULL;
6013 tools_gl2ps_context->
colormode = colormode;
6014 tools_gl2ps_context->
buffersize = buffersize > 0 ? buffersize : 2048 * 2048;
6015 for(i = 0; i < 3; i++){
6018 for(i = 0; i < 4; i++){
6020 tools_gl2ps_context->
lastrgba[i] = -1.0F;
6032 tools_gl2ps_context->
xreflist = NULL;
6048 tools_gl2ps_context->
colormap = NULL;
6054 if(!colorsize || !colormap){
6055 tools_gl2psMsg(
TOOLS_GL2PS_ERROR,
"Missing colormap for TOOLS_GL_COLOR_INDEX rendering");
6056 tools_gl2psFree(tools_gl2ps_context);
6057 tools_gl2ps_context = NULL;
6060 tools_gl2ps_context->
colorsize = colorsize;
6064 tools_gl2ps_context->
bgcolor[0] = tools_gl2ps_context->
colormap[idx][0];
6065 tools_gl2ps_context->
bgcolor[1] = tools_gl2ps_context->
colormap[idx][1];
6066 tools_gl2ps_context->
bgcolor[2] = tools_gl2ps_context->
colormap[idx][2];
6067 tools_gl2ps_context->
bgcolor[3] = 1.0F;
6070 tools_gl2psMsg(
TOOLS_GL2PS_ERROR,
"Unknown color mode in tools_gl2psBeginPage");
6071 tools_gl2psFree(tools_gl2ps_context);
6072 tools_gl2ps_context = NULL;
6077 tools_gl2ps_context->
title = (
char*)tools_gl2psMalloc(
sizeof(
char));
6078 tools_gl2ps_context->
title[0] =
'\0';
6081 tools_gl2ps_context->
title = (
char*)tools_gl2psMalloc((strlen(title)+1)*
sizeof(
char));
6082 strcpy(tools_gl2ps_context->
title, title);
6086 tools_gl2ps_context->
producer = (
char*)tools_gl2psMalloc(
sizeof(
char));
6087 tools_gl2ps_context->
producer[0] =
'\0';
6090 tools_gl2ps_context->
producer = (
char*)tools_gl2psMalloc((strlen(producer)+1)*
sizeof(
char));
6091 strcpy(tools_gl2ps_context->
producer, producer);
6095 tools_gl2ps_context->
filename = (
char*)tools_gl2psMalloc(
sizeof(
char));
6096 tools_gl2ps_context->
filename[0] =
'\0';
6099 tools_gl2ps_context->
filename = (
char*)tools_gl2psMalloc((strlen(filename)+1)*
sizeof(
char));
6100 strcpy(tools_gl2ps_context->
filename, filename);
6112 tools_gl2ps_context->
feedback = NULL;
6125 res = tools_gl2psPrintPrimitives();
6130 fflush(tools_gl2ps_context->
stream);
6132 tools_gl2psListDelete(tools_gl2ps_context->
primitives);
6134 tools_tools_gl2psFreeImagemap(tools_gl2ps_context->
imagemap_head);
6135 tools_gl2psFree(tools_gl2ps_context->
colormap);
6136 tools_gl2psFree(tools_gl2ps_context->
title);
6137 tools_gl2psFree(tools_gl2ps_context->
producer);
6138 tools_gl2psFree(tools_gl2ps_context->
filename);
6139 tools_gl2psFree(tools_gl2ps_context->
feedback);
6140 tools_gl2psFree(tools_gl2ps_context);
6141 tools_gl2ps_context = NULL;
6164 tools_gl2psResetLineProperties();
6173 return tools_gl2psAddText(
TOOLS_GL2PS_TEXT, str, fontname, fontsize, alignment, angle,
6180 return tools_gl2psAddText(
TOOLS_GL2PS_TEXT, str, fontname, fontsize, alignment, angle, NULL);
6218 "TOOLS_GL_RGB/TOOLS_GL_RGBA, TOOLS_GL_FLOAT pixels");
6244 prim->
verts[0].
xyz[0] = pos[0] + xorig;
6245 prim->
verts[0].
xyz[1] = pos[1] + yorig;
6278 size = height * width * 3;
6281 for(i = 0; i <
size; ++i, ++piv){
6288 size = height * width * 4;
6295 size = height * width * 3;
6303 tools_gl2psListAdd(tools_gl2ps_context->
auxprimitives, &prim);
6307 tools_gl2psListAdd(tools_gl2ps_context->
primitives, &prim);
6315 const unsigned char *imagemap){
6323 size = height + height * ((width - 1) / 8);
6326 tools_glVertex3f(position[0], position[1],position[2]);
6330 for(i = 0; i <
size; i += sizeoffloat){
6331 const float *value = (
const float*)imagemap;
6332 tools_glPassThrough(*value);
6333 imagemap += sizeoffloat;
6349 tools_glPassThrough(tmp2);
6351 tools_glPassThrough(tmp2);
6404 tools_glPassThrough(value);
6414 tools_glPassThrough(value);
6443 if(
TOOLS_GL_FALSE == tools_gl2psSupportedBlendMode(sfactor, dfactor))
6462 tools_gl2ps_context->
options = options;
6469 if(!tools_gl2ps_context) {
6474 *options = tools_gl2ps_context->
options;
6481 if(format >= 0 && format < (
tools_GLint)(
sizeof(tools_gl2psbackends) /
sizeof(tools_gl2psbackends[0])))
6484 return "Unknown format";
6489 if(format >= 0 && format < (
tools_GLint)(
sizeof(tools_gl2psbackends) /
sizeof(tools_gl2psbackends[0])))
6492 return "Unknown format";
6497 return tools_gl2ps_context->
format;
6503 if(!tools_gl2ps_context) {