summaryrefslogtreecommitdiff
path: root/app/bin/dbitmap.c
blob: 62c708ebfd98f1efb016b22f3a9a52f5f84ad05c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
/** \file dbitmap.c
 *  Print to Bitmap
 */

/*  XTrkCad - Model Railroad CAD
 *  Copyright (C) 2005 Dave Bullis
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */

#include "custom.h"
#include "dynstring.h"
#include "fileio.h"
#include "layout.h"
#include "param.h"
#include "paths.h"
#include "track.h"
#include "common-ui.h"

#ifdef WIN32
#ifdef _WIN64
#define BITMAPDIM 50000
#define BITMAPSIZE 500e6
#else
#define BITMAPDIM 32000
#define BITMAPSIZE 150e6
#endif
#else // Not WIN
#define BITMAPDIM 50000
#define BITMAPSIZE 500e6
#endif // WIN32

/** Option flags for bitmap export */
#define BITMAPDRAWTITLE 1
#define BITMAPDRAWFRAMEONLY (1<<1)
#define BITMAPDRAWCENTERLINE (1<<2)
#define BITMAPDRAWBACKGROUND (1<<3)

#define POINTSTOINCH 72.0
#define BITMAPEXPORTFONTSIZE 18
// line height is 20 percent larger than fontsize and converted to inches
#define LINEHEIGHT (BITMAPEXPORTFONTSIZE * 1.2 / POINTSTOINCH)

#define DEFAULTMARGIN 0.2
#define LEFTMARGIN (DEFAULTMARGIN + 0.3)
#define BOTTOMMARGIN (DEFAULTMARGIN + LINEHEIGHT)

static long outputBitMapTogglesV = 3;
static double outputBitMapDensity = 10;

static struct wFilSel_t * bitmap_fs;
static wWinPix_t bitmap_w, bitmap_h;
static drawCmd_t bitmap_d = {
	NULL,
	&screenDrawFuncs,
	0,
	16.0,
	0.0,
	{0.0, 0.0}, {1.0, 1.0},
	Pix2CoOrd, CoOrd2Pix
};

/**
 * Show string at given y position centered in x direction
 *
 * \param [in]	string   If non-null, the string.
 * \param 		font	 The font.
 * \param 		fontSize Size of the font.
 * \param 		yPos	 The position.
 */

static void DrawTextCenterXPosY( char *string, wFont_p font, wFontSize_t fontSize,
                     POS_T yPos )
{
	coOrd textSize;
	coOrd p;

	DrawTextSize( &mainD, string, font, fontSize * bitmap_d.scale, FALSE,
	              &textSize );
	p.x = ( bitmap_d.size.x - textSize.x ) / 2.0 + bitmap_d.orig.x;
	p.y = mapD.size.y + yPos*bitmap_d.scale;
	DrawString( &bitmap_d, p, 0.0, string, font, fontSize*bitmap_d.scale,
	            wDrawColorBlack );
}

/**
* Draw the product info to the bitmap
*
* \param [in]	preFix   preFix to add to product name
* \param 		fontSize Size of the font.
* \param 		yPos	 The position.
*/

static void
DrawProductInfo( char *preFix, wFontSize_t fontSize, POS_T yPos )
{
	wFont_p fp, fp_bi;
	coOrd textsize, textsize1;
	coOrd textPos;

	fp = wStandardFont( F_TIMES, FALSE, FALSE );
	fp_bi = wStandardFont( F_TIMES, TRUE, TRUE );
	DrawTextSize( &mainD, preFix, fp, fontSize * bitmap_d.scale, FALSE, &textsize );
	DrawTextSize( &mainD, sProdName, fp_bi, fontSize * bitmap_d.scale, FALSE,
	              &textsize1 );
	textPos.x = ( bitmap_d.size.x - ( textsize.x + textsize1.x ) ) /
	            2.0 + bitmap_d.orig.x;
	textPos.y = -LINEHEIGHT*bitmap_d.scale;
	DrawString( &bitmap_d, textPos, 0.0, preFix, fp,
	            fontSize * bitmap_d.scale, wDrawColorBlack );
	textPos.x += textsize.x;
	DrawString( &bitmap_d, textPos, 0.0, sProdName, fp_bi,
	            fontSize * bitmap_d.scale, wDrawColorBlack );
}

/**
 * Saves a bitmap file
 *
 * \param 		   files    number of files, must be 1
 * \param [in]     fileName name of the file
 * \param [in,out] data	    unused
 *
 * \returns true on success, false otherwise
 */

static int SaveBitmapFile(
        int files,
        char **fileName,
        void * data )
{
	bool result;

	assert( fileName != NULL );
	assert( files == 1 );

	wSetCursor( mainD.d, wCursorWait );
	InfoMessage( _( "Drawing tracks to bitmap" ) );

	SetCurrentPath( BITMAPPATHKEY, fileName[ 0 ] );

	bitmap_d.d = wBitMapCreate( bitmap_w, bitmap_h, 8 );

	if( !bitmap_d.d ) {
		NoticeMessage( MSG_WBITMAP_FAILED, _( "Ok" ), NULL );
		return false;
	}

	if( outputBitMapTogglesV & ( BITMAPDRAWFRAMEONLY | BITMAPDRAWTITLE ) ) {
		coOrd p[4];

		p[0].x = p[3].x = 0.0;
		p[1].x = p[2].x = mapD.size.x;
		p[0].y = p[1].y = 0.0;
		p[2].y = p[3].y = mapD.size.y;
		DrawPoly( &bitmap_d, 4, p, NULL, wDrawColorBlack, 2, DRAW_CLOSED );

		if( ( outputBitMapTogglesV & BITMAPDRAWFRAMEONLY ) ) {
			DrawRuler( &bitmap_d, p[0], p[1], 0.0, TRUE, FALSE, wDrawColorBlack );
			DrawRuler( &bitmap_d, p[0], p[3], 0.0, TRUE, TRUE, wDrawColorBlack );
			DrawRuler( &bitmap_d, p[1], p[2], 0.0, FALSE, FALSE, wDrawColorBlack );
			DrawRuler( &bitmap_d, p[3], p[2], 0.0, FALSE, TRUE, wDrawColorBlack );
			//y0 = 0.37;
			//y1 = 0.2;
		}

		if( outputBitMapTogglesV & BITMAPDRAWTITLE ) {
			wFont_p fp;

			fp = wStandardFont( F_TIMES, FALSE, FALSE );
			DrawTextCenterXPosY( GetLayoutTitle(), fp, BITMAPEXPORTFONTSIZE,
			                     1.4 * LINEHEIGHT );
			DrawTextCenterXPosY( GetLayoutSubtitle(), fp, BITMAPEXPORTFONTSIZE,
			                     0.4 * LINEHEIGHT );
			DrawProductInfo( N_( "Drawn with " ), BITMAPEXPORTFONTSIZE, -LINEHEIGHT );
		}
	}

	wDrawClip( bitmap_d.d,
	           ( wWinPix_t )( -bitmap_d.orig.x/bitmap_d.scale*bitmap_d.dpi ),
	           ( wWinPix_t )( -bitmap_d.orig.y/bitmap_d.scale*bitmap_d.dpi ),
	           ( wWinPix_t )( mapD.size.x/bitmap_d.scale*bitmap_d.dpi ),
	           ( wWinPix_t )( mapD.size.y/bitmap_d.scale*bitmap_d.dpi ) );

	DrawSnapGrid( &bitmap_d, mapD.size, TRUE );

	if( outputBitMapTogglesV & BITMAPDRAWBACKGROUND &&
	    GetLayoutBackGroundScreen() < 100.0 ) {
		wWinPix_t bitmapPosX;
		wWinPix_t bitmapPosY;
		wWinPix_t bitmapWidth;

		TranslateBackground( &bitmap_d,
		                     bitmap_d.orig.x,
		                     bitmap_d.orig.y,
		                     &bitmapPosX,
		                     &bitmapPosY,
		                     &bitmapWidth );
		wDrawCloneBackground( mainD.d, bitmap_d.d );

		wDrawShowBackground( bitmap_d.d,
		                     bitmapPosX,
		                     bitmapPosY,
		                     bitmapWidth,
		                     GetLayoutBackGroundAngle(),
		                     GetLayoutBackGroundScreen() );
	}

	if( outputBitMapTogglesV & BITMAPDRAWCENTERLINE ) {
		bitmap_d.options |= DC_CENTERLINE;
	} else {
		bitmap_d.options &= ~DC_CENTERLINE;
	}

	DrawTracks( &bitmap_d, bitmap_d.scale, bitmap_d.orig, bitmap_d.size );

	InfoMessage( _( "Writing bitmap to file" ) );

	if( !wBitMapWriteFile( bitmap_d.d, fileName[0] ) ) {
		NoticeMessage( MSG_WBITMAP_FAILED, _( "Ok" ), NULL );
		result = false;
	} else {
		InfoMessage( "" );
		result = true;
	}

	wSetCursor( mainD.d, defaultCursor );
	wBitMapDelete( bitmap_d.d );
	return result;
}

/*******************************************************************************
 *
 * Output BitMap Dialog
 *
 */

static wWin_p outputBitMapW;

static char *bitmapTogglesLabels[] = { N_( "Layout Titles" ),
                                       N_( "Borders" ),
                                       N_( "Centerline of Track" ),
                                       N_( "Background Image" ),
                                       NULL
                                     };
static paramFloatRange_t dpiRange = { 0.1, 100.0, 60 };

static paramData_t outputBitMapPLs[] = {
#define I_TOGGLES		(0)
	{ PD_TOGGLE, &outputBitMapTogglesV, "toggles", PDO_NOPSHUPD, bitmapTogglesLabels, N_( "Include " ) },
#define I_DENSITY		(1)
	{ PD_FLOAT, &outputBitMapDensity, "density", PDO_NOPSHUPD, &dpiRange, N_( "Resolution " ) },
	{ PD_MESSAGE, N_( "dpi" ), NULL, PDO_DLGHORZ },
	{ PD_MESSAGE, N_( "Bitmap Size " ), NULL, PDO_NOPSHUPD | PDO_DLGRESETMARGIN, 0 },
#define I_MSG1			(4)
	{ PD_MESSAGE, N_( "99999 by 99999 pixels" ), NULL, PDO_DLGHORZ | PDO_DLGUNDERCMDBUTT /* | PDO_DLGWIDE */, I2VP( 180 )},
	{ PD_MESSAGE, N_( "Approximate File Size " ), NULL, PDO_NOPSHUPD, 0 },
#define I_MSG2			(6)
	{ PD_MESSAGE, N_( "999.9Mb" ), NULL, PDO_DLGHORZ | PDO_DLGUNDERCMDBUTT | PDO_DLGBOXEND, I2VP( 180 ) },
};

static paramGroup_t outputBitMapPG = { "outputbitmap", 0, outputBitMapPLs, COUNT( outputBitMapPLs ) };

/**
 * The upper limit for the dpi setting is calculated. The limit is set
 * to make sute that the number of pixels in any direction is below the
 * maximum value.
 *
 * \param [in]		size	The size of the layout
 * \param [in]		marginX	The total size of margins in X direction
 * \param [in]		marginY	The total size of margins in Y direction
 *
 * \returns the maximum allowed dpi value
 */

static double CalculateMaxDPI( coOrd size, POS_T marginX, POS_T marginY )
{
	POS_T maxSize;
	POS_T maxMargin;
	double maxDpi;

	if( size.x > size.y ) {
		maxMargin = marginX;
		maxSize = size.x;
	} else {
		maxMargin = marginY;
		maxSize = size.y;
	}

	maxDpi = ( BITMAPDIM - maxMargin * mainD.dpi ) / maxSize;
	return( floor( maxDpi ) );
}

/**
 * Display the pixel size of the bitmap
 */

static void
OutputBitmapPixelSize( void )
{
	DynString message;
	DynStringMalloc( &message, 16 );
	ParamLoadData( &outputBitMapPG );

	DynStringPrintf( &message, _( "%ld by %ld pixels" ), bitmap_w, bitmap_h );
	ParamLoadMessage( &outputBitMapPG, I_MSG1, DynStringToCStr( &message ) );
	DynStringFree( &message );
}

/**
* Display and return the file size of the bitmap
*
* \returns the estimated file size
*/

static FLOAT_T
OutputBitmapFileSize( void )
{
	DynString message;
	DynStringMalloc( &message, 16 );
	ParamLoadData( &outputBitMapPG );
	FLOAT_T size;

	size = ( FLOAT_T )bitmap_w * bitmap_h;

	if( size < 1e4 ) {
		DynStringPrintf( &message, _( "%0.0f" ), size );
	} else if( size < 1e6 ) {
		DynStringPrintf( &message, _( "%0.1fKb" ), ( size + 50.0 ) / 1e3 );
	} else if( size < 1e9 ) {
		DynStringPrintf( &message, _( "%0.1fMb" ), ( size + 5e4 ) / 1e6 );
	} else {
		DynStringPrintf( &message, _( "%0.1fGb" ), ( size + 5e7 ) / 1e9 );
	}

	ParamLoadMessage( &outputBitMapPG, I_MSG2, DynStringToCStr( &message ) );

	DynStringFree( &message );

	return( size );
}

/**
 * Compute pixel size of bitmap
 */

static void ComputeBitmapSize( void )
{
	FLOAT_T Lborder=0.0, Rborder=0.0, Tborder=0.0, Bborder=0.0;

	bitmap_d.dpi = mainD.dpi;
	bitmap_d.scale = mainD.dpi / outputBitMapDensity;

	if( outputBitMapTogglesV & ( BITMAPDRAWFRAMEONLY | BITMAPDRAWTITLE ) ) {
		Lborder = LEFTMARGIN;
		Rborder = DEFAULTMARGIN;
		Tborder = DEFAULTMARGIN;
		Bborder = BOTTOMMARGIN;
	}

	if( outputBitMapTogglesV & BITMAPDRAWTITLE ) {
		Tborder += 2 * LINEHEIGHT;
		Bborder += LINEHEIGHT;
	}

	dpiRange.high = CalculateMaxDPI( mapD.size, Lborder + Rborder,
	                                 Bborder + Tborder );

	bitmap_d.orig.x = -Lborder*bitmap_d.scale;
	bitmap_d.size.x = mapD.size.x + ( Lborder + Rborder )*bitmap_d.scale;
	bitmap_d.orig.y = -Bborder*bitmap_d.scale;
	bitmap_d.size.y = mapD.size.y + ( Bborder + Tborder )*bitmap_d.scale;

	bitmap_w = ( wWinPix_t )( bitmap_d.size.x / bitmap_d.scale*bitmap_d.dpi );
	bitmap_h = ( wWinPix_t )( bitmap_d.size.y / bitmap_d.scale*bitmap_d.dpi );
}

/**
 * Update the dialog for changed bitmap settings. Based on the new dimensions
 * selected options the bitmap size and and maximum density is calculated and
 * displayed.
 */

void
UpdateBitmapDialog( void )
{
	ParamLoadData( &outputBitMapPG );

	ComputeBitmapSize();
	ParamLoadControl( &outputBitMapPG, I_DENSITY ); // trigger range check

	if( outputBitMapDensity > dpiRange.high ) {
		ParamDialogOkActive( &outputBitMapPG, false );
	} else {
		ParamDialogOkActive( &outputBitMapPG, true );
	}

	OutputBitmapPixelSize();
	OutputBitmapFileSize();
}

/**
 * Check input from bitmap options dialog and trigger file name selection
 *
 * \param [in,out] junk If non-null, the junk.
 */

static void OutputBitMapOk( void * unused )
{
	FLOAT_T size;

	size = OutputBitmapFileSize();

	if( size > BITMAPSIZE ) {
		if( NoticeMessage( MSG_BITMAP_SIZE_WARNING, _( "Continue" ),
		                   _( "Cancel" ) )==0 ) {
			return;
		}
	}

	wHide( outputBitMapW );

	if( !bitmap_fs ) {
		bitmap_fs = wFilSelCreate( mainW, FS_SAVE, 0, _( "Save Bitmap" ),
		                           _( "Portable Network Graphics format (*.png)|*.png|" \
		                              "JPEG format (*.jpg)|*.jpg" ),
		                           SaveBitmapFile, NULL );
	}

	wFilSelect( bitmap_fs, GetCurrentPath( BITMAPPATHKEY ) );
}

/**
 * Handle changes for bitmap export. Only changes relevant here are
 * changes to the map.
 *
 * \param  changes The changes.
 */

static void OutputBitMapChange( long changes )
{
	if( ( changes & CHANGE_MAP ) && outputBitMapW ) {
		ParamLoadControls( &outputBitMapPG );
		ComputeBitmapSize();
	}

	return;
}

/**
 * Executes the output bit map operation
 *
 * \param [in,out] unused.
 */

static void DoOutputBitMap( void* unused )
{
	if( outputBitMapW == NULL ) {
		outputBitMapW = ParamCreateDialog( &outputBitMapPG,
		                                   MakeWindowTitle( _( "Export to bitmap" ) ),
		                                   _( "Ok" ),
		                                   OutputBitMapOk,
		                                   wHide,
		                                   TRUE,
		                                   NULL,
		                                   0,
		                                   ( paramChangeProc )UpdateBitmapDialog );
	}

	ParamLoadControls( &outputBitMapPG );
	ParamGroupRecord( &outputBitMapPG );

	UpdateBitmapDialog();

	if( dpiRange.high < outputBitMapDensity ) {
		outputBitMapDensity = dpiRange.high;
		ParamLoadControl( &outputBitMapPG, I_DENSITY );
	}

	wShow( outputBitMapW );
}

/**
 * Initialize bitmap output
 *
 * \returns entry point for bitmap export
 */

EXPORT addButtonCallBack_t OutputBitMapInit( void )
{
	ParamRegister( &outputBitMapPG );
	RegisterChangeNotification( OutputBitMapChange );
	return &DoOutputBitMap;
}