1 #ifndef __font_h
2 #define __font_h
3 
4 // ===========================================================================
5 //
6 // The module <font> contains some classes which re-implement some
7 // textoutput-function on the X-client side. The images of the characters
8 // are transferred (on demand) from the server to the client, where the
9 // client then can rotate them or do any other manipulations
10 //
11 // To draw them on the screen, they are transferred back into a pixmap of the
12 // server.
13 //
14 // This module actually gets somehow obsolete which functionality of the
15 // Xserver and fontserver to rotate fonts in X11R6, but nevertheless is
16 // a  nice demo.
17 //
18 // ===========================================================================
19 // classes:
20 //
21 // Character:    a Character-object holds the image and the size information
22 //               for a single character (as ximage on the client side and as
23 //               a pixmap on the server side) and has some functions like
24 //               Rotating and Mirroring...
25 // ScalableFont: contains a list of characters, which is constructed on the fly
26 //               when characters of the desired font need to be drawn
27 // FlipFont:     extension to ScalableFont, where letters can be used both
28 //               upside down and normal
29 //
30 // ===========================================================================
31 
32 #include <X11/Xlib.h>
33 #include <X11/Xutil.h>
34 
35 // ===========================================================================
36 
37 class ScalableFont {
38 	public:
39 		ScalableFont( Display *dpy, const char *fontname, int mode );
40 		virtual ~ScalableFont();
41 
42 		void Draw( Drawable w, GC gc, int x, int y, const char *str );
43 		void DrawCentered( Drawable d, GC gc, int x, int y, const char *str );
44 
45 		void Width( const char *str, int *ox, int *oy );
46 		void BBox( const char *str, int *tlx, int *tly, int *brx, int *bry );
47 
48 		void TurnRight();
49 		void TurnLeft();
50 		void Flip();
51 
52 		virtual class Character *GetChar(char c);
53 		virtual const char *GetString(const char *str);
54 
55 	protected:
56 		Display				*dpy;
57 		XFontStruct			*fs;
58 		class Character	**chr;
59 		char					*name;
60 		int					mode;
61 		int					nchr;		// number of characters
62 
63 	friend class Character;
64 };
65 
66 // ===========================================================================
67 
68 class Character {
69 	public:
70 		Character(class ScalableFont *sf,char c);
71 		~Character();
72 
73 		void Draw( Drawable d, GC gc, int *x, int *y );
Width(int * ox,int * oy)74 		void Width( int *ox, int *oy )		{ *ox  = addx;	*oy  = addy; }
AddWidth(int * ox,int * oy)75 		void AddWidth( int *ox, int *oy )	{ *ox += addx;	*oy += addy; }
76 		void BBox( int *ulx, int *uly, int *brx, int *bry );
77 
78 		void TurnRight();
79 		void TurnLeft();
80 		void ImgFlipX();
81 		void FlipX();
82 		void ImgFlipY();
83 		void FlipY();
Mirror()84 		void Mirror()	{ FlipX(); FlipY(); }
85 
86 	protected:
87 		void Init(class ScalableFont *sf=0l);
88 
89 		void Pix2Img();
90 		void Img2Pix();
91 		void PrintImg();
92 
DropPix()93 		void DropPix()
94 			{ if (pix!=0)	{ XFreePixmap(dpy,pix); pix=0; } }
DropImg()95 		void DropImg()
96 			{ if (img!=0)	{ XDestroyImage(img); img=0l; } }
DropData()97 		void DropData()
98 			{ DropPix(); DropImg(); }
99 
PreparePix()100 		void PreparePix()
101 			{ if (pix==0)	Init(); }
PrepareImg()102 		void PrepareImg()
103 			{	if (img==0) {
104 					PreparePix();
105 					Pix2Img();
106 				}
107 			}
108 
109 	private:
110 		Display			*dpy;
111 	//	ScalableFont	*sf;
112 	//	XCharStruct		*cs;
113 		Pixmap			pix;
114 		XImage			*img;
115 		int				w,h;				// width, height
116 		int				offx, offy;		// offset of starting position
117 		int				addx, addy;		// offset to next point;
118 		char				c;
119 };
120 
121 // ===========================================================================
122 
123 class FlipFont : public ScalableFont {
124 	public:
125 		FlipFont( Display *dpy, const char *fontname, int mode );
126 		virtual ~FlipFont();
127 
128 		virtual class Character *GetChar(char c);
129 		virtual const char *GetString(const char *str);
130 };
131 
132 // ===========================================================================
133 
134 #endif
135