File: appPaper.c

package info (click to toggle)
ted 2.11-1
  • links: PTS
  • area: main
  • in suites: woody
  • size: 11,064 kB
  • ctags: 13,935
  • sloc: ansic: 120,446; makefile: 7,469; sh: 253
file content (149 lines) | stat: -rw-r--r-- 3,735 bytes parent folder | download | duplicates (2)
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
/************************************************************************/
/*									*/
/*  Paper sizes.							*/
/*									*/
/************************************************************************/

#   include	"appUtilConfig.h"

#   include	<string.h>

#   include	"appPaper.h"
#   include	"appGeoString.h"
#   include	<appDebugon.h>

typedef struct PaperSize
    {
    char *	psMnemonic;
    char *	psLabel;
    int		psWideTwips;
    int		psHighTwips;
    } PaperSize;

static PaperSize UtilPaperSizes[]=
{
    { "a4",		"A4: 210 x 297 mm",		11909,	16834,	},
    { "a5",		"A5: 149 x 210 mm",		8417,	11909,	},
    { "a6",		"A6: 105 x 149 mm",		5954,	8417,	},
    { "letter",		"Letter: 8 1/2 x 11 \"",	12240,	15840,	},
    { "legal",		"Legal: 8 1/2 x 14 \"",		12240,	20720,	},
    { "executive",	"Executive: 7 1/4 x 10 1/2 \"",	10440,	15120,	},
};

/************************************************************************/
/*									*/
/*  Get information on a certain paper size.				*/
/*									*/
/************************************************************************/

int appPaperGetInformation(	int		n,
				int *		pWidth,
				int *		pHeight,
				const char **	pLabel )
    {
    PaperSize *	ps;

    if  ( n < 0								||
	  n >= (int)( sizeof(UtilPaperSizes)/sizeof(PaperSize) )	)
	{ return -1;	}

    ps= UtilPaperSizes+ n;

    if  ( pWidth )
	{ *pWidth= ps->psWideTwips;	}
    if  ( pHeight )
	{ *pHeight= ps->psHighTwips;	}
    if  ( pLabel )
	{ *pLabel= ps->psLabel;		}

    return 0;
    }

/************************************************************************/
/*									*/
/*  Try to find a paper size by its size.				*/
/*									*/
/************************************************************************/

int appPaperGetBySize(		int		width,
				int		height )
    {
    unsigned	i;

    int		gw= width/200;
    int		gh= height/200;

    for ( i= 0; i < sizeof(UtilPaperSizes)/sizeof(PaperSize); i++ )
	{
	int		dw= UtilPaperSizes[i].psWideTwips- width;
	int		dh= UtilPaperSizes[i].psHighTwips- height;

	if  ( dw < gw && dw > -gw && dh < gh && dh > -gh )
	    { return i; }
	}

    return -1;
    }

/************************************************************************/
/*									*/
/*  Try to find a paper size by its mnemonic.				*/
/*									*/
/************************************************************************/

int appPaperGetByMnemonic(	const char *	mnemonic )
    {
    unsigned		i;

    for ( i= 0; i < sizeof(UtilPaperSizes)/sizeof(PaperSize); i++ )
	{
	if  ( ! strcmp( UtilPaperSizes[i].psMnemonic, mnemonic ) )
	    { return i; }
	}

    for ( i= 0; i < sizeof(UtilPaperSizes)/sizeof(PaperSize); i++ )
	{
	if  ( tolower( mnemonic[0] ) ==
			tolower( UtilPaperSizes[i].psMnemonic[0] )	&&
	      ! strcmp( UtilPaperSizes[i].psMnemonic+ 1, mnemonic+ 1 )	)
	    { return i; }
	}

    return -1;
    }

/************************************************************************/
/*									*/
/*  Try to translate a string to a paper size.				*/
/*									*/
/************************************************************************/

int appPaperFormatFromString(	int *		pFormat,
				int *		pWidth,
				int *		pHeight,
				int		unitType,
				const char *	paperString )
    {
    int		paperFormat;
    int		width;
    int		height;

    paperFormat= appPaperGetByMnemonic( paperString );
    if  ( paperFormat >= 0 )
	{
	if  ( appPaperGetInformation( paperFormat,
					&width, &height, (const char **)0 ) )
	    { SDEB(paperString); return -1;	}
	}
    else{
	if  ( appGeoRectangleFromString( paperString, unitType,
							    &width, &height ) )
	    { SDEB(paperString); return -1;	}
	}

    *pWidth= width;
    *pHeight= height;
    *pFormat= paperFormat;

    return 0;
    }