Debian Codegraph @

texk/xdvipsk/otflib/numbers.c

raw
1 /* This is xdvipsk, an eXtended version of dvips(k) by Tomas Rokicki.
2
3 Copyright (C) 2016 by VTeX Ltd (www.vtex.lt),
4 the xdvipsk project team - Sigitas Tolusis and Arunas Povilaitis.
5
6 Program original code copyright (C) 2007-2014 by Jin-Hwan Cho and
7 Shunsaku Hirata, the dvipdfmx project team.
8
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
13
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
22 */
23
24 #include <config.h>
25
26 #include "system.h"
27 #include "error.h"
28 #include "mfileio.h"
29 #include "numbers.h"
30
31 unsigned char get_unsigned_byte (FILE *file)
32 {
33 int ch;
34 if ((ch = fgetc (file)) < 0)
35 ERROR ("File ended prematurely\n");
36 return (unsigned char) ch;
37 }
38
39 void skip_bytes (unsigned int n, FILE *file)
40 {
41 while (n-- > 0)
42 get_unsigned_byte(file);
43 }
44
45 signed char get_signed_byte (FILE *file)
46 {
47 int byte;
48 byte = get_unsigned_byte(file);
49 if (byte >= 0x80)
50 byte -= 0x100;
51 return (signed char) byte;
52 }
53
54 unsigned short get_unsigned_pair (FILE *file)
55 {
56 unsigned short pair = get_unsigned_byte(file);
57 pair = (pair << 8) | get_unsigned_byte(file);
58 return pair;
59 }
60
61 unsigned short sget_unsigned_pair (unsigned char *s)
62 {
63 unsigned short pair = *(s++);
64 pair = (pair << 8) | *(s++);
65 return pair;
66 }
67
68 signed short get_signed_pair (FILE *file)
69 {
70 signed short pair = get_signed_byte(file);
71 pair = (pair << 8) | get_unsigned_byte(file);
72 return pair;
73 }
74
75
76 unsigned int get_unsigned_triple(FILE *file)
77 {
78 int i;
79 unsigned int triple = 0;
80 for (i=0; i<3; i++) {
81 triple = (triple << 8) | get_unsigned_byte(file);
82 }
83 return triple;
84 }
85
86 signed int get_signed_triple(FILE *file)
87 {
88 int i;
89 signed int triple = get_signed_byte(file);
90 for (i=0; i<2; i++) {
91 triple = (triple << 8) | get_unsigned_byte(file);
92 }
93 return triple;
94 }
95
96 int32_t get_signed_quad(FILE *file)
97 {
98 int i;
99 int32_t quad = get_signed_byte(file);
100 for (i=0; i<3; i++) {
101 quad = (quad << 8) | get_unsigned_byte(file);
102 }
103 return quad;
104 }
105
106 uint32_t get_unsigned_quad(FILE *file)
107 {
108 int i;
109 uint32_t quad = 0;
110 for (i=0; i<4; i++) {
111 quad = (quad << 8) | get_unsigned_byte(file);
112 }
113 return quad;
114 }
115
116 int32_t get_unsigned_num (FILE *file, unsigned char num)
117 {
118 int32_t val = get_unsigned_byte (file);
119 switch (num) {
120 case 3: if (val > 0x7f)
121 val -= 0x100;
122 val = (val << 8) | get_unsigned_byte (file);
123 case 2: val = (val << 8) | get_unsigned_byte (file);
124 case 1: val = (val << 8) | get_unsigned_byte (file);
125 default: break;
126 }
127 return val;
128 }
129
130 /* Compute a signed quad that must be positive */
131 uint32_t get_positive_quad (FILE *file, const char *type, const char *name)
132 {
133 int32_t val = get_signed_quad (file);
134 if (val < 0)
135 ERROR ("Bad %s: negative %s: %d", type, name, val);
136 return (uint32_t)val;
137 }
138
139 int32_t sqxfw (int32_t sq, fixword fw)
140 {
141 int sign = 1;
142 uint32_t a, b, c, d, ad, bd, bc, ac;
143 uint32_t e, f, g, h, i, j, k;
144 int32_t result;
145 /* Make positive. */
146 if (sq < 0) {
147 sign = -sign;
148 sq = -sq;
149 }
150 if (fw < 0) {
151 sign = -sign;
152 fw = -fw;
153 }
154 a = ((uint32_t) sq) >> 16;
155 b = ((uint32_t) sq) & 0xffffu;
156 c = ((uint32_t) fw) >> 16;
157 d = ((uint32_t) fw) & 0xffffu;
158 ad = a*d; bd = b*d; bc = b*c; ac = a*c;
159 e = bd >> 16;
160 f = ad >> 16;
161 g = ad & 0xffffu;
162 h = bc >> 16;
163 i = bc & 0xffffu;
164 j = ac >> 16;
165 k = ac & 0xffffu;
166 result = (e+g+i + (1<<3)) >> 4; /* 1<<3 is for rounding */
167 result += (f+h+k) << 12;
168 result += j << 28;
169 return (sign > 0) ? result : -result;
170 }
171