/* text.c 1.1 */ /* Copyright 1992, 1993 O'Reilly and Associates, Inc. Permission to use, copy, and modify this program is hereby granted, as long as this copyright notice appears in each copy of the program source code. */ #include "book_utils.h" /* Structure ID */ static PEXStructure struct_id; static void create_structure( dpy ) Display *dpy; { PEXCoord text_pt, floor[4]; PEXCoord2D text_pt2D; PEXVector dir[2]; PEXColor color; PEXVector2D up_vector; char *text; struct_id = PEXCreateStructure( dpy ); /* Set the view index to view 1. */ PEXSetViewIndex( dpy, struct_id, PEXOCStore, 1 ); /* First, define a blue floor as a reference frame. */ SET_COLOR( 0.2, 0.2, 1, color ); PEXSetSurfaceColor( dpy, struct_id, PEXOCStore, PEXColorTypeRGB, &color ); PEXSetInteriorStyle( dpy, struct_id, PEXOCStore, PEXInteriorStyleSolid ); floor[0].x = 0.0; floor[0].y = 0.0; floor[0].z = 0.0; floor[1].x = 0.0; floor[1].y = 0.0; floor[1].z = 1.0; floor[2].x = 1.0; floor[2].y = 0.0; floor[2].z = 1.0; floor[3].x = 1.0; floor[3].y = 0.0; floor[3].z = 0.0; PEXFillArea( dpy, struct_id, PEXOCStore, PEXShapeConvex, True, 4, floor ); /* Set the text precision to STROKE so that all the text * attributes are used. */ PEXSetTextPrecision( dpy, struct_id, PEXOCStore, PEXStrokePrecision ); /* ----- First Text Primitive ----- * Put a 2D text primitive at the MC origin. Set the * height to 5% of the width of the floor. */ /* Set the character height. */ PEXSetCharHeight( dpy, struct_id, PEXOCStore, 0.05 ); /* Set the text point and create the text element. */ text = "PEX Default Text Plane"; text_pt2D.x = text_pt2D.y = 0.0; PEXText2D( dpy, struct_id, PEXOCStore, &text_pt2D, strlen(text), text ); /* Set the text color to yellow for the rest of the examples. */ SET_COLOR( 1, 1, 0, color ); PEXSetTextColor( dpy, struct_id, PEXOCStore, PEXColorTypeRGB, &color ); /* ----- Second Text Primitive ----- * Put a text primitive in the X-Z plane, and raised slightly. * The first direction vector is parallel to the X MC axis. The * second is parallel to the Z MC axis, but in the opposite * direction. Use the same character height as the previous * primitive. Center the text in the unit plane and add some * spacing between characters. */ /* Center the string using text alignment. */ PEXSetTextAlignment( dpy, struct_id, PEXOCStore, PEXHAlignCenter, PEXVAlignHalf ); /* Add some spacing -- 5% of the character height. */ PEXSetCharSpacing( dpy, struct_id, PEXOCStore, 0.05 ); /* The first direction vector. */ dir[0].x = 1.0; dir[0].y = 0.0; dir[0].z = 0.0; /* The second direction vector. */ dir[1].x = 0.0; dir[1].y = 0.0; dir[1].z = -1.0; /* The text point. */ text_pt.x = 0.5; text_pt.y = 0.01; text_pt.z = 0.5; /* Create the 3D Text element. */ text = "PEX - X-Z plane"; PEXText( dpy, struct_id, PEXOCStore, &text_pt, &dir[0], &dir[1], strlen(text), text ); /* ----- Third Text Primitive ----- * Put text parallel to the X-Y plane, but towards the viewer in * Z. Use a different font. Don't reset the text alignment, so * that the string will be centered on the text point. */ PEXSetTextFontIndex( dpy, struct_id, PEXOCStore, 2 ); /* Reset the character spacing to no added spacing. */ PEXSetCharSpacing( dpy, struct_id, PEXOCStore, 0.0 ); /* Create the primitive. */ dir[0].x = 1.0; dir[0].y = 0.0; dir[0].z = 0.0; dir[1].x = 0.0; dir[1].y = 1.0; dir[1].z = 0.0; text_pt.x = 0.5; text_pt.y = 0.0; text_pt.z = 0.9; text= "PEX - X-Y plane"; PEXText( dpy, struct_id, PEXOCStore, &text_pt, &dir[0], &dir[1], strlen(text), text ); /* ----- Fourth Text Primitive ----- * Put text in the Y-Z plane, centered. Distort the characters * with the character expansion factor and print the text * vertically. Rotate the text with the character up vector. */ /* Reset the text font. */ PEXSetTextFontIndex( dpy, struct_id, PEXOCStore, 1 ); /* Set the character expansion. */ PEXSetCharExpansion( dpy, struct_id, PEXOCStore, 1.2 ); /* Make text vertical. */ PEXSetTextPath( dpy, struct_id, PEXOCStore, PEXPathDown ); /* Rotate the text 45 degrees clockwise. */ up_vector.x = 1.0; up_vector.y = 1.0; PEXSetCharUpVector( dpy, struct_id, PEXOCStore, &up_vector ); dir[0].x = 0.0; dir[0].y = 0.0; dir[0].z = -1.0; dir[1].x = 0.0; dir[1].y = 1.0; dir[1].z = 0.0; text_pt.x = 0.0; text_pt.y = 0.5; text_pt.z = 0.5; text = "PEX - Y-Z plane"; PEXText( dpy, struct_id, PEXOCStore, &text_pt, &dir[0], &dir[1], strlen(text), text ); /* ----- Fifth Text Primitive ----- * Put text in a non-primary text plane. */ /* Reset the text attributes. */ PEXSetCharHeight( dpy, struct_id, PEXOCStore, 0.1 ); PEXSetCharExpansion( dpy, struct_id, PEXOCStore, 1.0 ); PEXSetTextPath( dpy, struct_id, PEXOCStore, PEXPathRight ); up_vector.x = 0.0; up_vector.y = 1.0; PEXSetCharUpVector( dpy, struct_id, PEXOCStore, &up_vector ); dir[0].x = 1.0; dir[0].y = 0.0; dir[0].z = 0.0; dir[1].x = 0.0; dir[1].y = 1.0; dir[1].z = -1.0; text_pt.x = 0.5; text_pt.y = 0.7; text_pt.z = 0.3; text = "PEX - Tilted"; PEXText( dpy, struct_id, PEXOCStore, &text_pt, &dir[0], &dir[1], strlen(text), text ); } static void redraw( dpy, window, renderer ) Display *dpy; Window window; PEXRenderer renderer; { XClearWindow( dpy, window ); PEXRenderNetwork( dpy, window, renderer, struct_id ); XFlush( dpy ); } #define WIN_GEOM "500x500+50+50" extern void set_view(); /* A utility defined below. */ main( argc, argv ) int argc; char *argv[]; { Display *dpy; Window window; XEvent event; XVisualInfo vis_info; XColor bkgd_color; XStandardColormap cmap_info; PEXColorApproxEntry capx_info; PEXRenderer renderer; PEXRendererAttributes rattrs; PEXExtensionInfo *pexinfo; int done = 0; /* Open a display and initialize PEX. */ dpy = ora_init_pex( argv, &pexinfo ); if ( !dpy ) exit(1); /* Determine the best visual to use. */ ora_find_best_visual( dpy, &vis_info ); /* Get a standard colormap for the visual. */ if ( !ora_get_standard_colormap( dpy, &vis_info, &cmap_info ) ) { fprintf ( stderr, "Cannot find a standard colormap\n" ); exit(1); } ora_set_stdcmap_approx( &vis_info, &cmap_info, &capx_info ); /* Create the window, giving it a gray background. */ bkgd_color.red = bkgd_color.green = bkgd_color.blue = 0x5555; bkgd_color.flags = DoRed | DoGreen | DoBlue; window = ora_create_window( dpy, &vis_info, &cmap_info, WIN_GEOM, &bkgd_color ); /* Create the structure and Set up the PEX renderer. */ if ( STRUCTURE_SPT( pexinfo ) ) { create_structure( dpy ); renderer = ora_setup_renderer( dpy, window, &capx_info, &rattrs ); } else { fprintf( stderr, "Structures not supported.\n" ); exit(1); } set_view( dpy, rattrs.view_table ); /* Read and respond to X events. */ XSelectInput( dpy, window, ExposureMask | ButtonPressMask ); while ( !done ) { XNextEvent( dpy, &event ); switch ( event.type ) { case Expose: /* Flush remaining Expose events, then redraw. */ while ( XCheckTypedWindowEvent( dpy, window, Expose, &event ) ) ; /* empty statement */ redraw( dpy, window, renderer ); break; case ButtonPress: done = 1; break; } } XCloseDisplay( dpy ); return 0; } static void set_view( dpy, view_table ) Display *dpy; PEXLookupTable view_table; { PEXViewEntry view; PEXCoord2D window[2]; double view_plane, front_plane, back_plane; PEXCoord prp; PEXNPCSubVolume viewport; /* The view orientation parameters. */ static PEXCoord view_ref_pt = {0.5, 0.5, 0.5}; static PEXVector view_plane_normal = {0.8,1,3}; static PEXVector view_up_vec = {0,1,0}; /* Compute the view orientation transform. */ (void)PEXViewOrientationMatrix( &view_ref_pt, &view_plane_normal, &view_up_vec, view.orientation ); /* The view mapping parameters. */ prp.x = 0.5; prp.y = 0.5; prp.z = 6; window[0].x = -1.0; window[1].x = 1.1; window[0].y = -1.2; window[1].y = 1.0; front_plane = 1.2; view_plane = 0; back_plane = -1.2; viewport.min.x = 0; viewport.max.x = 1; viewport.min.y = 0; viewport.max.y = 1; viewport.min.z = 0; viewport.max.z = 1; /* Compute the view mapping transform. */ (void)PEXViewMappingMatrix( window, &viewport, True, &prp, view_plane, back_plane, front_plane, view.mapping ); /* The view clipping parameters. */ view.clip_flags = PEXClippingAll; view.clip_limits = viewport; /* Set view 1. */ PEXSetTableEntries( dpy, view_table, 1, 1, PEXLUTView, (PEXPointer) &view ); }