File: paper.c

package info (click to toggle)
dia 0.97.3%2Bgit20160930-9
  • links: PTS
  • area: main
  • in suites: bullseye
  • size: 54,372 kB
  • sloc: ansic: 155,065; xml: 16,326; python: 6,641; cpp: 4,935; makefile: 3,833; sh: 540; perl: 137; sed: 19
file content (206 lines) | stat: -rw-r--r-- 5,827 bytes parent folder | download | duplicates (3)
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
/* Dia -- an diagram creation/manipulation program
 * Copyright (C) 1998 Alexander Larsson
 *
 * 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 <config.h>

#include <stdio.h>
#include <string.h>

#include "paper.h"
#include "diagramdata.h" /* for NewDiagramData */

/* Paper definitions stolen from gnome-libs.
 * All measurements are in centimetres. */

static const struct _dia_paper_metrics {
  gchar *paper;
  gdouble pswidth, psheight;
  gdouble lmargin, tmargin, rmargin, bmargin;
} paper_metrics[] = {
  { "A0", 84.1, 118.9, 2.8222, 2.8222, 2.8222, 2.8222 },
  { "A1", 59.4, 84.1, 2.8222, 2.8222, 2.8222, 2.8222 },
  { "A2", 42.0, 59.4, 2.8222, 2.8222, 2.8222, 2.8222 },
  { "A3", 29.7, 42.0, 2.8222, 2.8222, 2.8222, 2.8222 },
  { "A4", 21.0, 29.7, 2.8222, 2.8222, 2.8222, 2.8222 },
  { "A5", 14.8, 21.0, 2.8222, 2.8222, 2.8222, 2.8222 },
  { "A6", 10.5, 14.8, 2.8222, 2.8222, 2.8222, 2.8222 },
  { "B0-Japan", 103.0, 145.6, 2.1167, 2.1167, 2.1167, 2.1167 },
  { "B1-Japan", 72.8, 103.0, 2.1167, 2.1167, 2.1167, 2.1167 },
  { "B2-Japan", 51.5, 72.8, 2.1167, 2.1167, 2.1167, 2.1167 },
  { "B3-Japan", 36.4, 51.5, 2.1167, 2.1167, 2.1167, 2.1167 },
  { "B4-Japan", 25.7, 36.4, 2.1167, 2.1167, 2.1167, 2.1167 },
  { "B5-Japan", 18.2, 25.7, 2.8222, 2.8222, 2.8222, 2.8222 },
  { "B6-Japan", 12.8, 18.2, 2.8222, 2.8222, 2.8222, 2.8222 },
  { "B0", 100.0, 141.4, 2.1167, 2.1167, 2.1167, 2.1167 },
  { "B1", 70.7, 100.0, 2.1167, 2.1167, 2.1167, 2.1167 },
  { "B2", 50.0, 70.7, 2.1167, 2.1167, 2.1167, 2.1167 },
  { "B3", 35.3, 50.0, 2.1167, 2.1167, 2.1167, 2.1167 },
  { "B4", 25.0, 35.3, 2.1167, 2.1167, 2.1167, 2.1167 },
  { "B5", 17.6, 25.0, 2.8222, 2.8222, 2.8222, 2.8222 },
  { "B6", 12.5, 17.6, 2.8222, 2.8222, 2.8222, 2.8222 },
  { "Letter", 21.59, 27.94, 2.54, 2.54, 2.54, 2.54 },
  { "Legal", 21.59, 35.56, 2.54, 2.54, 2.54, 2.54 },
  { "Ledger", 27.9, 43.2, 2.54, 2.54, 2.54, 2.54 },
  { "Half-Letter", 21.59, 14.0, 2.54, 2.54, 2.54, 2.54 },
  { "Executive", 18.45, 26.74, 2.54, 2.54, 2.54, 2.54 },
  { "Tabloid", 28.01, 43.2858, 2.54, 2.54, 2.54, 2.54 },
  { "Monarch", 9.8778, 19.12, 0.3528, 0.3528, 0.3528, 0.3528 },
  { "SuperB", 29.74, 43.2858, 2.8222, 2.8222, 2.8222, 2.8222 },
  { "Envelope-Commercial", 10.5128, 24.2, 0.1764, 0.1764, 0.1764, 0.1764 },
  { "Envelope-Monarch", 9.8778, 19.12, 0.1764, 0.1764, 0.1764, 0.1764 },
  { "Envelope-DL", 11.0, 22.0, 0.1764, 0.1764, 0.1764, 0.1764 },
  { "Envelope-C5", 16.2278, 22.9306, 0.1764, 0.1764, 0.1764, 0.1764 },
  { "EuroPostcard", 10.5128, 14.8167, 0.1764, 0.1764, 0.1764, 0.1764 },
  { NULL, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 }
};

int
find_paper(const gchar *name)
{
  int i;

  if (name == NULL) return -1;
  for (i = 0; paper_metrics[i].paper != NULL; i++) {
    if (!g_ascii_strncasecmp(paper_metrics[i].paper, name, 
		             strlen(paper_metrics[i].paper)))
      break;
  }
  if (paper_metrics[i].paper == NULL)
    i = -1;

  return i;
}

int
get_default_paper(void)
{
  FILE *papersize;
  gchar paper[100];
  const gchar *env;
  gint i;

  if((env = g_getenv("PAPERCONF")) != NULL) {
    strncpy(paper, env, sizeof(paper));
  }
  else if((papersize = fopen("/etc/papersize", "r")) != NULL) {
    while(fgets(paper, sizeof(paper), papersize))
      if(g_ascii_isalnum(paper[0])) {
	break;
      }
    fclose(papersize);
  }
  else
    strcpy(paper, "a4");

  i = find_paper(paper);
  if (i == -1)
    i = find_paper("a4");

  return i;
}

void
get_paper_info(PaperInfo *paper, int i, NewDiagramData *prefs)
{
  if (i == -1 && prefs != NULL)
    i = find_paper(prefs->papertype);
  if (i == -1)
    i = get_default_paper();

  paper->name = g_strdup(paper_metrics[i].paper);
  paper->tmargin = paper_metrics[i].tmargin;
  paper->bmargin = paper_metrics[i].bmargin;
  paper->lmargin = paper_metrics[i].lmargin;
  paper->rmargin = paper_metrics[i].rmargin;
  if (prefs != NULL)
    paper->is_portrait = prefs->is_portrait;
  else
    paper->is_portrait = TRUE;
  paper->scaling = 1.0;
  paper->fitto = FALSE;
  paper->fitwidth = 1;
  paper->fitheight = 1;
  paper->width = paper_metrics[i].pswidth - 
    paper_metrics[i].lmargin - 
    paper_metrics[i].rmargin;
  paper->height = paper_metrics[i].psheight - 
    paper_metrics[i].tmargin - 
    paper_metrics[i].bmargin;
  if (!paper->is_portrait) {
    double tmp = paper->width;
    paper->width = paper->height;
    paper->height = tmp;
  }
}

GList *
get_paper_name_list(void)
{
  int i;
  static GList *name_list = NULL;

  if (name_list == NULL) {
    for (i = 0; paper_metrics[i].paper != NULL; i++) {
      name_list = g_list_append(name_list, paper_metrics[i].paper);
    }
  }

  return name_list;
}

const gchar *
get_paper_name(int i)
{
  return paper_metrics[i].paper;
}

gdouble
get_paper_psheight(int i)
{
  return paper_metrics[i].psheight;
}

gdouble
get_paper_pswidth(int i)
{
  return paper_metrics[i].pswidth;
}

gdouble
get_paper_lmargin(int i)
{
  return paper_metrics[i].lmargin;
}

gdouble
get_paper_rmargin(int i)
{
  return paper_metrics[i].rmargin;
}

gdouble
get_paper_tmargin(int i)
{
  return paper_metrics[i].tmargin;
}

gdouble
get_paper_bmargin(int i)
{
  return paper_metrics[i].bmargin;
}