source: git/src/useful.h @ 92debc5

stereo-2025
Last change on this file since 92debc5 was bfe0bc01, checked in by Olly Betts <olly@…>, 11 months ago

Support scaling for HPGL export

Previous the export was always at 1:40000.

  • Property mode set to 100644
File size: 4.1 KB
RevLine 
[421b7d2]1/* useful.h
[d1b1380]2 * Lots of oddments that come in handy generally
[a4454620]3 * Copyright (C) 1993-2003,2004,2010,2011,2014 Olly Betts
[846746e]4 *
[89231c4]5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
[846746e]9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
[89231c4]12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 * GNU General Public License for more details.
[846746e]14 *
[89231c4]15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
[ecbc6c18]17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
[d1b1380]18 */
19
20/* only include once */
21#ifndef USEFUL_H
22#define USEFUL_H
23
[04078a7]24#ifndef PACKAGE
25# error config.h must be included first in each C/C++ source file
26#endif
[fa42426]27
[5b894c3]28#include <stdint.h>
[d1b1380]29#include <stdlib.h> /* for Borland C which #defines max() & min() there */
30#include <stdio.h>
31#include <math.h>
32
33/* Macro to allow easy building of macros contain multiple statements, such
[0804fbe]34 * that the likes of “if (x == y) macro1(x); else x = 2;” works properly  */
[a420b49]35#define BLK(X) do {X} while(0)
[d1b1380]36
37/* Macro to do nothing, but avoid compiler warnings about empty if bodies &c */
38#define NOP (void)0
39
[0580c6a]40/* In C++ code, #include<algorithm> and use std::max and std::min instead. */
41#ifndef __cplusplus
42/* Return max/min of two numbers. */
43/* May be defined already (e.g. by Borland C in stdlib.h) */
[d1b1380]44/* NB Bad news if X or Y has side-effects... */
[0580c6a]45# ifndef max
46#  define max(X, Y) ((X) > (Y) ? (X) : (Y))
47# endif
48# ifndef min
49#  define min(X, Y) ((X) < (Y) ? (X) : (Y))
50# endif
[a420b49]51#endif
[d1b1380]52
[1ec8db0]53/* M_PI, etc may be defined in math.h */
[bca0071]54#ifndef M_PI
55# ifdef PI /* MSVC defines PI IIRC */
56#  define M_PI PI
[d1b1380]57# else
[bca0071]58#  define M_PI 3.14159265358979323846264338327950288419716939937510582097494459
[d1b1380]59# endif
[bca0071]60#endif
[bceebf4]61#ifndef M_PI_2
62# define M_PI_2 (M_PI / 2.0)
63#endif
64#ifndef M_PI_4
65# define M_PI_4 (M_PI / 4.0)
66#endif
[d1b1380]67
[a420b49]68#define MM_PER_INCH 25.4 /* exact value */
[d1b1380]69#define METRES_PER_FOOT 0.3048 /* exact value */
[bfe0bc01]70#define POINTS_PER_INCH 72.0
71#define POINTS_PER_MM (POINTS_PER_INCH / MM_PER_INCH)
[d1b1380]72
73#define putnl() putchar('\n')    /* print a newline char */
[e02a6a6]74#define fputnl(FH) PUTC('\n', (FH)) /* print a newline char to a file */
[d1b1380]75/* print a line followed by a newline char to a file */
[e02a6a6]76#define fputsnl(SZ, FH) BLK(fputs((SZ), (FH)); PUTC('\n', (FH));)
[a420b49]77#define sqrd(X) ((X) * (X))        /* macro to square things */
[27b8b59]78
79/* 2D Euclidean distance */
80#ifndef HAVE_HYPOT
81# define hypot(X, Y) sqrt(sqrd((double)(X)) + sqrd((double)(Y)))
82#endif
[bca0071]83#define rad(X) ((M_PI / 180.0) * (X))  /* convert from degrees to radians */
84#define deg(X) ((180.0 / M_PI) * (X))  /* convert from radians to degrees */
[d1b1380]85
[bd1913f]86/* macro to convert argument to a string literal */
[fa42426]87#define STRING(X) STRING_(X)
88#define STRING_(X) #X
[d1b1380]89
[c0563da]90#ifndef WORDS_BIGENDIAN
[8c45eea]91# define put16(W, FH) BLK(int16_t w = (W); fwrite(&w, 2, 1, (FH));)
92# define put32(W, FH) BLK(int32_t w = (W); fwrite(&w, 4, 1, (FH));)
[5dd874a]93
[d9434a0]94# ifdef __GNUC__
95__attribute__((unused))
96# endif
[5dd874a]97static inline int16_t get16(FILE *fh) {
98    int16_t w;
[a4454620]99    if (fread(&w, 2, 1, fh) == 0) {
100        /* We check feof() and ferror() afterwards, so checking the return
101         * value achieves nothing, but we get a warning from glibc's
102         * _FORTIFY_SOURCE if we don't pretend to. */
103    }
[5dd874a]104    return w;
105}
106
[d9434a0]107# ifdef __GNUC__
108__attribute__((unused))
109# endif
[5dd874a]110static inline int32_t get32(FILE *fh) {
111    int32_t w;
[a4454620]112    if (fread(&w, 4, 1, fh) == 0) {
113        /* We check feof() and ferror() afterwards, so checking the return
114         * value achieves nothing, but we get a warning from glibc's
115         * _FORTIFY_SOURCE if we don't pretend to. */
116    }
[5dd874a]117    return w;
118}
[c0563da]119#else
[9965b2b]120void useful_put16(int16_t, FILE *);
121void useful_put32(int32_t, FILE *);
122int16_t useful_get16(FILE *);
123int32_t useful_get32(FILE *);
[d1b1380]124
[c0563da]125# define put16(W, FH) useful_put16(W, FH)
126# define put32(W, FH) useful_put32(W, FH)
127# define get16(FH) useful_get16(FH)
128# define get32(FH) useful_get32(FH)
[d1b1380]129#endif
130
131#endif /* !USEFUL_H */
Note: See TracBrowser for help on using the repository browser.