Removed conflict indicators.
[dia.git] / lib / paper.c
blob3f6e2492fd3052e3c0a1f1c0b05b6d46aff58d5d
1 /* Dia -- an diagram creation/manipulation program
2 * Copyright (C) 1998 Alexander Larsson
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
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.
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.
19 #include <config.h>
21 #include <stdio.h>
22 #include <string.h>
24 #include "paper.h"
26 /* Paper definitions stolen from gnome-libs.
27 * All measurements are in centimetres. */
29 static const struct _dia_paper_metrics {
30 gchar *paper;
31 gdouble pswidth, psheight;
32 gdouble lmargin, tmargin, rmargin, bmargin;
33 } paper_metrics[] = {
34 { "A3", 29.7, 42.0, 2.8222, 2.8222, 2.8222, 2.8222 },
35 { "A4", 21.0, 29.7, 2.8222, 2.8222, 2.8222, 2.8222 },
36 { "A5", 14.85, 21.0, 2.8222, 2.8222, 2.8222, 2.8222 },
37 { "B4", 25.7528, 36.4772, 2.1167, 2.1167, 2.1167, 2.1167 },
38 { "B5", 17.6389, 25.0472, 2.8222, 2.8222, 2.8222, 2.8222 },
39 { "B5-Japan", 18.2386, 25.7528, 2.8222, 2.8222, 2.8222, 2.8222 },
40 { "Letter", 21.59, 27.94, 2.54, 2.54, 2.54, 2.54 },
41 { "Legal", 21.59, 35.56, 2.54, 2.54, 2.54, 2.54 },
42 { "Ledger", 27.9, 43.2, 2.54, 2.54, 2.54, 2.54 },
43 { "Half-Letter", 21.59, 14.0, 2.54, 2.54, 2.54, 2.54 },
44 { "Executive", 18.45, 26.74, 2.54, 2.54, 2.54, 2.54 },
45 { "Tabloid", 28.01, 43.2858, 2.54, 2.54, 2.54, 2.54 },
46 { "Monarch", 9.8778, 19.12, 0.3528, 0.3528, 0.3528, 0.3528 },
47 { "SuperB", 29.74, 43.2858, 2.8222, 2.8222, 2.8222, 2.8222 },
48 { "Envelope-Commercial", 10.5128, 24.2, 0.1764, 0.1764, 0.1764, 0.1764 },
49 { "Envelope-Monarch", 9.8778, 19.12, 0.1764, 0.1764, 0.1764, 0.1764 },
50 { "Envelope-DL", 11.0, 22.0, 0.1764, 0.1764, 0.1764, 0.1764 },
51 { "Envelope-C5", 16.2278, 22.9306, 0.1764, 0.1764, 0.1764, 0.1764 },
52 { "EuroPostcard", 10.5128, 14.8167, 0.1764, 0.1764, 0.1764, 0.1764 },
53 { "A0", 84.1, 118.9, 2.8222, 2.8222, 2.8222, 2.8222 },
54 { "A1", 59.4, 84.1, 2.8222, 2.8222, 2.8222, 2.8222 },
55 { "A2", 42.0, 59.4, 2.8222, 2.8222, 2.8222, 2.8222 },
56 { NULL, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 }
59 int
60 find_paper(const gchar *name)
62 int i;
64 if (name == NULL) return -1;
65 for (i = 0; paper_metrics[i].paper != NULL; i++) {
66 if (!g_strncasecmp(paper_metrics[i].paper, name,
67 strlen(paper_metrics[i].paper)))
68 break;
70 if (paper_metrics[i].paper == NULL)
71 i = -1;
73 return i;
76 int
77 get_default_paper(void)
79 FILE *papersize;
80 gchar paper[100];
81 const gchar *env;
82 gint i;
84 if((env = g_getenv("PAPERCONF")) != NULL) {
85 strncpy(paper, env, sizeof(paper));
87 else if((papersize = fopen("/etc/papersize", "r")) != NULL) {
88 while(fgets(paper, sizeof(paper), papersize))
89 if(g_ascii_isalnum(paper[0])) {
90 break;
92 fclose(papersize);
94 else
95 strcpy(paper, "a4");
97 i = find_paper(paper);
98 if (i == -1)
99 i = find_paper("a4");
101 return i;
104 void
105 get_paper_info(PaperInfo *paper, int i, NewDiagramData *prefs)
107 if (i == -1 && prefs != NULL)
108 i = find_paper(prefs->papertype);
109 if (i == -1)
110 i = get_default_paper();
112 paper->name = g_strdup(paper_metrics[i].paper);
113 paper->tmargin = paper_metrics[i].tmargin;
114 paper->bmargin = paper_metrics[i].bmargin;
115 paper->lmargin = paper_metrics[i].lmargin;
116 paper->rmargin = paper_metrics[i].rmargin;
117 if (prefs != NULL)
118 paper->is_portrait = prefs->is_portrait;
119 else
120 paper->is_portrait = TRUE;
121 paper->scaling = 1.0;
122 paper->fitto = FALSE;
123 paper->fitwidth = 1;
124 paper->fitheight = 1;
125 paper->width = paper_metrics[i].pswidth -
126 paper_metrics[i].lmargin -
127 paper_metrics[i].rmargin;
128 paper->height = paper_metrics[i].psheight -
129 paper_metrics[i].tmargin -
130 paper_metrics[i].bmargin;
131 if (!paper->is_portrait) {
132 double tmp = paper->width;
133 paper->width = paper->height;
134 paper->height = tmp;
138 GList *
139 get_paper_name_list(void)
141 int i;
142 static GList *name_list = NULL;
144 if (name_list == NULL) {
145 for (i = 0; paper_metrics[i].paper != NULL; i++) {
146 name_list = g_list_append(name_list, paper_metrics[i].paper);
150 return name_list;
153 #if defined(_MSC_VER) || defined(__SUNPRO_C)
154 /* can't export inlined functions. Shouldn't they be declared as such
155 * in paper.h ? The following is a hack. Isn't there a proper sollution
156 * provided by glib ?
158 #undef inline
159 #define inline
160 #endif
162 inline const gchar *
163 get_paper_name(int i)
165 return paper_metrics[i].paper;
168 inline gdouble
169 get_paper_psheight(int i)
171 return paper_metrics[i].psheight;
174 inline gdouble
175 get_paper_pswidth(int i)
177 return paper_metrics[i].pswidth;
180 inline gdouble
181 get_paper_lmargin(int i)
183 return paper_metrics[i].lmargin;
186 inline gdouble
187 get_paper_rmargin(int i)
189 return paper_metrics[i].rmargin;
192 inline gdouble
193 get_paper_tmargin(int i)
195 return paper_metrics[i].tmargin;
198 inline gdouble
199 get_paper_bmargin(int i)
201 return paper_metrics[i].bmargin;