175584Sru// -*- C++ -*-
275584Sru/* Copyright (C) 1989, 1990, 1991, 1992 Free Software Foundation, Inc.
375584Sru     Written by James Clark (jjc@jclark.com)
475584Sru
575584SruThis file is part of groff.
675584Sru
775584Srugroff is free software; you can redistribute it and/or modify it under
875584Sruthe terms of the GNU General Public License as published by the Free
975584SruSoftware Foundation; either version 2, or (at your option) any later
1075584Sruversion.
1175584Sru
1275584Srugroff is distributed in the hope that it will be useful, but WITHOUT ANY
1375584SruWARRANTY; without even the implied warranty of MERCHANTABILITY or
1475584SruFITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
1575584Srufor more details.
1675584Sru
1775584SruYou should have received a copy of the GNU General Public License along
1875584Sruwith groff; see the file COPYING.  If not, write to the Free Software
19151497SruFoundation, 51 Franklin St - Fifth Floor, Boston, MA 02110-1301, USA. */
2075584Sru
2175584Sru
2275584Sruclass vunits {
2375584Sru  int n;
2475584Srupublic:
2575584Sru  vunits();
2675584Sru  vunits(units);
2775584Sru  units to_units();
2875584Sru  int is_zero();
2975584Sru  vunits& operator+=(const vunits&);
3075584Sru  vunits& operator-=(const vunits&);
3175584Sru  friend inline vunits scale(vunits n, units x, units y); // scale n by x/y
3275584Sru  friend inline vunits scale(vunits n, vunits x, vunits y);
3375584Sru  friend inline vunits operator +(const vunits&, const vunits&);
3475584Sru  friend inline vunits operator -(const vunits&, const vunits&);
3575584Sru  friend inline vunits operator -(const vunits&);
3675584Sru  friend inline int operator /(const vunits&, const vunits&);
3775584Sru  friend inline vunits operator /(const vunits&, int);
3875584Sru  friend inline vunits operator *(const vunits&, int);
3975584Sru  friend inline vunits operator *(int, const vunits&);
4075584Sru  friend inline int operator <(const vunits&, const vunits&);
4175584Sru  friend inline int operator >(const vunits&, const vunits&);
4275584Sru  friend inline int operator <=(const vunits&, const vunits&);
4375584Sru  friend inline int operator >=(const vunits&, const vunits&);
4475584Sru  friend inline int operator ==(const vunits&, const vunits&);
4575584Sru  friend inline int operator !=(const vunits&, const vunits&);
4675584Sru};
4775584Sru
4875584Sruextern vunits V0;
4975584Sru
5075584Sru
5175584Sruclass hunits {
5275584Sru  int n;
5375584Srupublic:
5475584Sru  hunits();
5575584Sru  hunits(units);
5675584Sru  units to_units();
5775584Sru  int is_zero();
5875584Sru  hunits& operator+=(const hunits&);
5975584Sru  hunits& operator-=(const hunits&);
6075584Sru  friend inline hunits scale(hunits n, units x, units y); // scale n by x/y
6175584Sru  friend inline hunits scale(hunits n, double x);
6275584Sru  friend inline hunits operator +(const hunits&, const hunits&);
6375584Sru  friend inline hunits operator -(const hunits&, const hunits&);
6475584Sru  friend inline hunits operator -(const hunits&);
6575584Sru  friend inline int operator /(const hunits&, const hunits&);
6675584Sru  friend inline hunits operator /(const hunits&, int);
6775584Sru  friend inline hunits operator *(const hunits&, int);
6875584Sru  friend inline hunits operator *(int, const hunits&);
6975584Sru  friend inline int operator <(const hunits&, const hunits&);
7075584Sru  friend inline int operator >(const hunits&, const hunits&);
7175584Sru  friend inline int operator <=(const hunits&, const hunits&);
7275584Sru  friend inline int operator >=(const hunits&, const hunits&);
7375584Sru  friend inline int operator ==(const hunits&, const hunits&);
7475584Sru  friend inline int operator !=(const hunits&, const hunits&);
7575584Sru};
7675584Sru
7775584Sruextern hunits H0;
7875584Sru
7975584Sruextern int get_vunits(vunits *, unsigned char si);
8075584Sruextern int get_hunits(hunits *, unsigned char si);
8175584Sruextern int get_vunits(vunits *, unsigned char si, vunits prev_value);
8275584Sruextern int get_hunits(hunits *, unsigned char si, hunits prev_value);
8375584Sru
8475584Sruinline vunits:: vunits() : n(0)
8575584Sru{
8675584Sru}
8775584Sru
8875584Sruinline units vunits::to_units()
8975584Sru{
9075584Sru  return n*vresolution;
9175584Sru}
9275584Sru
9375584Sruinline int vunits::is_zero()
9475584Sru{
9575584Sru  return n == 0;
9675584Sru}
9775584Sru
9875584Sruinline vunits operator +(const vunits & x, const vunits & y)
9975584Sru{
10075584Sru  vunits r;
10175584Sru  r = x;
10275584Sru  r.n += y.n;
10375584Sru  return r;
10475584Sru}
10575584Sru
10675584Sruinline vunits operator -(const vunits & x, const vunits & y)
10775584Sru{
10875584Sru  vunits r;
10975584Sru  r = x;
11075584Sru  r.n -= y.n;
11175584Sru  return r;
11275584Sru}
11375584Sru
11475584Sruinline vunits operator -(const vunits & x)
11575584Sru{
11675584Sru  vunits r;
11775584Sru  r.n = -x.n;
11875584Sru  return r;
11975584Sru}
12075584Sru
12175584Sruinline int operator /(const vunits & x, const vunits & y)
12275584Sru{
12375584Sru  return x.n/y.n;
12475584Sru}
12575584Sru
12675584Sruinline vunits operator /(const vunits & x, int n)
12775584Sru{
12875584Sru  vunits r;
12975584Sru  r = x;
13075584Sru  r.n /= n;
13175584Sru  return r;
13275584Sru}
13375584Sru
13475584Sruinline vunits operator *(const vunits & x, int n)
13575584Sru{
13675584Sru  vunits r;
13775584Sru  r = x;
13875584Sru  r.n *= n;
13975584Sru  return r;
14075584Sru}
14175584Sru
14275584Sruinline vunits operator *(int n, const vunits & x)
14375584Sru{
14475584Sru  vunits r;
14575584Sru  r = x;
14675584Sru  r.n *= n;
14775584Sru  return r;
14875584Sru}
14975584Sru
15075584Sruinline int operator <(const vunits & x, const vunits & y)
15175584Sru{
15275584Sru  return x.n < y.n;
15375584Sru}
15475584Sru
15575584Sruinline int operator >(const vunits & x, const vunits & y)
15675584Sru{
15775584Sru  return x.n > y.n;
15875584Sru}
15975584Sru
16075584Sruinline int operator <=(const vunits & x, const vunits & y)
16175584Sru{
16275584Sru  return x.n <= y.n;
16375584Sru}
16475584Sru
16575584Sruinline int operator >=(const vunits & x, const vunits & y)
16675584Sru{
16775584Sru  return x.n >= y.n;
16875584Sru}
16975584Sru
17075584Sruinline int operator ==(const vunits & x, const vunits & y)
17175584Sru{
17275584Sru  return x.n == y.n;
17375584Sru}
17475584Sru
17575584Sruinline int operator !=(const vunits & x, const vunits & y)
17675584Sru{
17775584Sru  return x.n != y.n;
17875584Sru}
17975584Sru
18075584Sru
18175584Sruinline vunits& vunits::operator+=(const vunits & x)
18275584Sru{
18375584Sru  n += x.n;
18475584Sru  return *this;
18575584Sru}
18675584Sru
18775584Sruinline vunits& vunits::operator-=(const vunits & x)
18875584Sru{
18975584Sru  n -= x.n;
19075584Sru  return *this;
19175584Sru}
19275584Sru
19375584Sruinline hunits:: hunits() : n(0)
19475584Sru{
19575584Sru}
19675584Sru
19775584Sruinline units hunits::to_units()
19875584Sru{
19975584Sru  return n*hresolution;
20075584Sru}
20175584Sru
20275584Sruinline int hunits::is_zero()
20375584Sru{
20475584Sru  return n == 0;
20575584Sru}
20675584Sru
20775584Sruinline hunits operator +(const hunits & x, const hunits & y)
20875584Sru{
20975584Sru  hunits r;
21075584Sru  r = x;
21175584Sru  r.n += y.n;
21275584Sru  return r;
21375584Sru}
21475584Sru
21575584Sruinline hunits operator -(const hunits & x, const hunits & y)
21675584Sru{
21775584Sru  hunits r;
21875584Sru  r = x;
21975584Sru  r.n -= y.n;
22075584Sru  return r;
22175584Sru}
22275584Sru
22375584Sruinline hunits operator -(const hunits & x)
22475584Sru{
22575584Sru  hunits r;
22675584Sru  r = x;
22775584Sru  r.n = -x.n;
22875584Sru  return r;
22975584Sru}
23075584Sru
23175584Sruinline int operator /(const hunits & x, const hunits & y)
23275584Sru{
23375584Sru  return x.n/y.n;
23475584Sru}
23575584Sru
23675584Sruinline hunits operator /(const hunits & x, int n)
23775584Sru{
23875584Sru  hunits r;
23975584Sru  r = x;
24075584Sru  r.n /= n;
24175584Sru  return r;
24275584Sru}
24375584Sru
24475584Sruinline hunits operator *(const hunits & x, int n)
24575584Sru{
24675584Sru  hunits r;
24775584Sru  r = x;
24875584Sru  r.n *= n;
24975584Sru  return r;
25075584Sru}
25175584Sru
25275584Sruinline hunits operator *(int n, const hunits & x)
25375584Sru{
25475584Sru  hunits r;
25575584Sru  r = x;
25675584Sru  r.n *= n;
25775584Sru  return r;
25875584Sru}
25975584Sru
26075584Sruinline int operator <(const hunits & x, const hunits & y)
26175584Sru{
26275584Sru  return x.n < y.n;
26375584Sru}
26475584Sru
26575584Sruinline int operator >(const hunits & x, const hunits & y)
26675584Sru{
26775584Sru  return x.n > y.n;
26875584Sru}
26975584Sru
27075584Sruinline int operator <=(const hunits & x, const hunits & y)
27175584Sru{
27275584Sru  return x.n <= y.n;
27375584Sru}
27475584Sru
27575584Sruinline int operator >=(const hunits & x, const hunits & y)
27675584Sru{
27775584Sru  return x.n >= y.n;
27875584Sru}
27975584Sru
28075584Sruinline int operator ==(const hunits & x, const hunits & y)
28175584Sru{
28275584Sru  return x.n == y.n;
28375584Sru}
28475584Sru
28575584Sruinline int operator !=(const hunits & x, const hunits & y)
28675584Sru{
28775584Sru  return x.n != y.n;
28875584Sru}
28975584Sru
29075584Sru
29175584Sruinline hunits& hunits::operator+=(const hunits & x)
29275584Sru{
29375584Sru  n += x.n;
29475584Sru  return *this;
29575584Sru}
29675584Sru
29775584Sruinline hunits& hunits::operator-=(const hunits & x)
29875584Sru{
29975584Sru  n -= x.n;
30075584Sru  return *this;
30175584Sru}
30275584Sru
30375584Sruinline hunits scale(hunits n, units x, units y)
30475584Sru{
30575584Sru  hunits r;
30675584Sru  r.n = scale(n.n, x, y);
30775584Sru  return r;
30875584Sru}
30975584Sru
31075584Sruinline vunits scale(vunits n, units x, units y)
31175584Sru{
31275584Sru  vunits r;
31375584Sru  r.n = scale(n.n, x, y);
31475584Sru  return r;
31575584Sru}
31675584Sru
31775584Sruinline vunits scale(vunits n, vunits x, vunits y)
31875584Sru{
31975584Sru  vunits r;
32075584Sru  r.n = scale(n.n, x.n, y.n);
32175584Sru  return r;
32275584Sru}
32375584Sru
32475584Sruinline hunits scale(hunits n, double x)
32575584Sru{
32675584Sru  hunits r;
32775584Sru  r.n = int(n.n*x);
32875584Sru  return r;
32975584Sru}
33075584Sru
33175584Sruinline units scale(units n, double x)
33275584Sru{
33375584Sru  return int(n*x);
33475584Sru}
33575584Sru
33675584Sruinline units points_to_units(units n)
33775584Sru{
33875584Sru  return scale(n, units_per_inch, 72);
33975584Sru}
34075584Sru
341