]>
Commit | Line | Data |
---|---|---|
d4a28ab0 | 1 | /* |
bd695173 CW |
2 | * banner - make posters |
3 | * Copyright (C) 1999-2001 Joerg Schaible | |
4 | * | |
5 | * This program is free software; you can redistribute it and/or modify | |
6 | * it under the terms of the GNU General Public License as published by | |
7 | * the Free Software Foundation in version 2 of the License. | |
8 | * | |
9 | * This program is distributed in the hope that it will be useful, | |
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
12 | * GNU General Public License for more details. | |
13 | * | |
14 | * You should have received a copy of the GNU General Public License | |
15 | * along with this program; if not, write to the Free Software | |
16 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
17 | */ | |
d4a28ab0 | 18 | |
bd695173 CW |
19 | #if HAVE_CONFIG_H |
20 | # include "config.h" | |
21 | #endif | |
d4a28ab0 | 22 | |
d2b03e6a | 23 | #include "common.h" |
d4a28ab0 CW |
24 | |
25 | static const char versionID[] = | |
26 | "1.0"; | |
27 | /* for future CVS */ | |
28 | static const char revID[] = | |
29 | "$Id$"; | |
30 | static const char copyrightID[] = | |
31 | "Copyright (c) 1999-2001\nJoerg Schaible. All rights reserved.\nLicensed under GPL v2.0\n"; | |
32 | ||
33 | static void printTopDescription(FILE * f, char * name); | |
34 | static void printBottomDescription(FILE * f, char * name); | |
35 | static const char * getVersion(void); | |
36 | static void usage(poptContext optCon, FILE * f, char * name); | |
37 | static void help(poptContext optCon, FILE * f, char * name); | |
38 | static void version(poptContext optCon, FILE * f, char * name); | |
39 | static void license(poptContext optCon, FILE * f, char * name); | |
40 | ||
41 | static char * program_name; | |
42 | ||
43 | static char *c = "X"; | |
44 | static int w = 80; | |
45 | static TEXTMETRIC tm; | |
46 | static HFONT hFont; | |
47 | static HBITMAP hBmp; | |
48 | static HBITMAP hBmpOrg; | |
49 | static HDC hMem; | |
50 | static RECT rt; | |
51 | static BITMAP bm; | |
52 | static BITMAPINFO bmi; | |
53 | static unsigned char *buffer; | |
54 | ||
55 | void initialize() | |
56 | { | |
57 | HDC hScreen; | |
58 | hFont = GetStockObject( ANSI_FIXED_FONT ); | |
59 | hScreen = GetDC( 0 ); | |
60 | hMem = CreateCompatibleDC( hScreen ); | |
61 | ReleaseDC( 0, hScreen ); | |
62 | ||
63 | SelectObject( hMem, hFont ); | |
64 | GetTextMetrics( hMem, &tm ); | |
65 | ||
66 | hBmp = CreateBitmap( w, tm.tmHeight, 1, 1, 0 ); | |
67 | GetObject( hBmp, sizeof( bm ), &bm ); | |
68 | memset( &bmi.bmiHeader, 0, sizeof( BITMAPINFOHEADER )); | |
69 | bmi.bmiHeader.biSize = sizeof( BITMAPINFOHEADER ); | |
70 | bmi.bmiHeader.biWidth = bm.bmWidth; | |
71 | bmi.bmiHeader.biHeight = bm.bmHeight; | |
72 | bmi.bmiHeader.biPlanes = 1; | |
73 | bmi.bmiHeader.biCompression = BI_RGB; | |
74 | bmi.bmiHeader.biBitCount = 1; | |
75 | bmi.bmiHeader.biSizeImage = | |
76 | (((( unsigned int )bm.bmWidth + 31 ) & ~31 ) >> 3 ) | |
77 | * bm.bmHeight; | |
78 | bmi.bmiHeader.biClrUsed = 2; | |
79 | buffer = ( unsigned char * )malloc( bmi.bmiHeader.biSizeImage ); | |
80 | ||
81 | hBmpOrg = SelectObject( hMem, hBmp ); | |
82 | SelectObject( hMem, GetStockObject( WHITE_BRUSH )); | |
83 | SelectObject( hMem, GetStockObject( WHITE_PEN )); | |
84 | ||
85 | SetBkColor( hMem, RGB( 0, 0, 0 )); | |
86 | SetTextColor( hMem, RGB( 255, 255, 255 )); | |
87 | SetBkMode( hMem, OPAQUE ); | |
88 | ||
89 | rt.top = 0; | |
90 | rt.left = 0; | |
91 | rt.right = w; | |
92 | rt.bottom = tm.tmHeight; | |
93 | } | |
94 | ||
95 | void deinitialize() | |
96 | { | |
97 | SelectObject( hMem, hBmpOrg ); | |
98 | DeleteObject( hBmp ); | |
99 | DeleteDC( hMem ); | |
100 | free( buffer ); | |
101 | } | |
102 | ||
103 | int print_line( const char *line ) | |
104 | { | |
105 | int x, y; | |
106 | int len = strlen( line ); | |
107 | if( len > ( w / tm.tmMaxCharWidth )) | |
108 | len = ( w / tm.tmMaxCharWidth ); | |
109 | ||
110 | Rectangle( hMem, 0, 0, rt.right, rt.bottom ); | |
111 | DrawText( hMem, line, len, &rt, | |
112 | DT_NOCLIP | DT_NOPREFIX | DT_SINGLELINE ); | |
113 | ||
114 | SelectObject( hMem, hBmpOrg ); | |
115 | GetDIBits( hMem, hBmp, 0, bmi.bmiHeader.biHeight, buffer, | |
116 | ( LPBITMAPINFO )&bmi.bmiHeader, DIB_RGB_COLORS ); | |
117 | SelectObject( hMem, hBmp ); | |
118 | ||
119 | for( y = tm.tmHeight; y--; ) | |
120 | { | |
121 | unsigned char *line = buffer + y * ( bmi.bmiHeader.biSizeImage / bmi.bmiHeader.biHeight ); | |
122 | ||
123 | for( x = 0; x < len * tm.tmMaxCharWidth; ++x ) | |
124 | { | |
125 | unsigned char byte = *( line + ( x / 8 )); | |
126 | putchar(( byte & ( 1 << (7 - (( int )x % 8 )))) ? *c : ' ' ); | |
127 | } | |
128 | ||
129 | putchar( '\n' ); | |
130 | } | |
131 | return 0; | |
132 | } | |
133 | ||
134 | int main(int argc, const char ** argv) | |
135 | { | |
136 | poptContext optCon; | |
137 | const char ** rest; | |
138 | int rc; | |
139 | int ec = 0; | |
140 | int i; | |
141 | ||
142 | struct poptOption generalOptionsTable[] = { | |
143 | { "char", 'c', POPT_ARG_STRING, &c, 'c', \ | |
144 | "use character <X>", "X"}, | |
145 | { "width", 'w', POPT_ARG_INT, &w, 'w', \ | |
146 | "set display width to <80> ", "80"}, | |
147 | { NULL, '\0', 0, NULL, 0, NULL, NULL } | |
148 | }; | |
149 | ||
150 | struct poptOption helpOptionsTable[] = { | |
151 | { "help", '?', POPT_ARG_NONE, NULL, '?', \ | |
152 | "Show this help message", NULL}, | |
153 | { "usage", '\0', POPT_ARG_NONE, NULL, 'u', \ | |
154 | "Display brief usage message", NULL}, | |
155 | { "version", '\0', POPT_ARG_NONE, NULL, 'v', \ | |
156 | "Display version information", NULL}, | |
157 | { "license", '\0', POPT_ARG_NONE, NULL, 'l', \ | |
158 | "Display licensing information", NULL}, | |
159 | { NULL, '\0', 0, NULL, 0, NULL, NULL } | |
160 | }; | |
161 | ||
162 | struct poptOption opt[] = { | |
163 | { NULL, '\0', POPT_ARG_INCLUDE_TABLE, generalOptionsTable, 0, \ | |
164 | "General options", NULL }, | |
165 | { NULL, '\0', POPT_ARG_INCLUDE_TABLE, helpOptionsTable, 0, \ | |
166 | "Help options", NULL }, | |
167 | { NULL, '\0', 0, NULL, 0, NULL, NULL } | |
168 | }; | |
169 | ||
170 | if( (program_name = strdup(argv[0])) == NULL ) { | |
171 | fprintf(stderr, "%s: memory allocation error\n", argv[0]); | |
172 | exit(1); | |
173 | } | |
174 | optCon = poptGetContext(NULL, argc, argv, opt, 0); | |
175 | poptSetOtherOptionHelp(optCon, "A string to print..."); | |
176 | ||
177 | while ((rc = poptGetNextOpt(optCon)) > 0) { | |
178 | switch (rc) { | |
179 | case '?': help(optCon, stderr, program_name); | |
180 | goto exit; | |
181 | case 'u': usage(optCon, stderr, program_name); | |
182 | goto exit; | |
183 | case 'v': version(optCon, stderr, program_name); | |
184 | goto exit; | |
185 | case 'l': license(optCon, stderr, program_name); | |
186 | goto exit; | |
187 | case 'c': /* no additional action needed */ | |
188 | break; | |
189 | case 'w': /* no additional action needed */ | |
190 | break; | |
191 | } | |
192 | } | |
193 | if (rc < -1 ) { | |
194 | fprintf(stderr, "%s: bad argument %s: %s\n", | |
195 | program_name, poptBadOption(optCon, POPT_BADOPTION_NOALIAS), | |
196 | poptStrerror(rc)); | |
197 | ec = 2; | |
198 | goto exit; | |
199 | } | |
200 | rest = poptGetArgs(optCon); | |
201 | ||
202 | if (rest == NULL) { | |
203 | fprintf(stderr, "%s: not enough arguments\n",program_name); | |
204 | usage(optCon, stderr, program_name); | |
205 | } | |
206 | else { | |
207 | initialize(); | |
208 | while (*rest && !ec) { | |
209 | ec |= print_line( *rest ); | |
210 | rest++; | |
211 | } | |
212 | deinitialize(); | |
213 | } | |
214 | ||
215 | exit: | |
216 | poptFreeContext(optCon); | |
217 | free(program_name); | |
218 | return(ec); | |
219 | } | |
220 | ||
221 | static const char * getVersion() | |
222 | { | |
223 | return versionID; | |
224 | } | |
225 | ||
226 | static void printTopDescription(FILE * f, char * name) | |
227 | { | |
228 | fprintf(f, "%s version %s\n", name, getVersion()); | |
229 | fprintf(f, " Prints a string enlarged as a banner on the screen.\n\n"); | |
230 | } | |
231 | ||
232 | static void printBottomDescription(FILE * f, char * name) | |
233 | { | |
234 | fprintf(f, "\nThis version works the same way as System V'S banner does:\n"); | |
235 | fprintf(f, "The banner text is displayed horizontally.\n"); | |
236 | } | |
237 | ||
238 | static printLicense(FILE * f, char * name) | |
239 | { | |
240 | fprintf(f, "This program is free software; you can redistribute it and/or\n"); | |
241 | fprintf(f, "modify it under the terms of the GNU General Public License\n"); | |
242 | fprintf(f, "as published by the Free Software Foundation; either version 2\n"); | |
243 | fprintf(f, "of the License, or (at your option) any later version.\n"); | |
244 | fprintf(f, "\n"); | |
245 | fprintf(f, "This program is distributed in the hope that it will be useful,\n"); | |
246 | fprintf(f, "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"); | |
247 | fprintf(f, "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n"); | |
248 | fprintf(f, "GNU General Public License for more details.\n"); | |
249 | fprintf(f, "\n"); | |
250 | fprintf(f, "You should have received a copy of the GNU General Public License\n"); | |
251 | fprintf(f, "along with this program; if not, write to the Free Software\n"); | |
252 | fprintf(f, "Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.\n"); | |
253 | fprintf(f, "\n"); | |
254 | fprintf(f, "See the COPYING file for license information.\n"); | |
255 | } | |
256 | ||
257 | static void usage(poptContext optCon, FILE * f, char * name) | |
258 | { | |
259 | poptPrintUsage(optCon, f, 0); | |
260 | } | |
261 | ||
262 | static void help(poptContext optCon, FILE * f, char * name) | |
263 | { | |
264 | printTopDescription(f, name); | |
265 | poptPrintHelp(optCon, f, 0); | |
266 | printBottomDescription(f, name); | |
267 | } | |
268 | ||
269 | static void version(poptContext optCon, FILE * f, char * name) | |
270 | { | |
271 | printTopDescription(f, name); | |
272 | } | |
273 | ||
274 | static void license(poptContext optCon, FILE * f, char * name) | |
275 | { | |
276 | printTopDescription(f, name); | |
277 | printLicense(f, name); | |
278 | } | |
279 |