File: fkp.c

package info (click to toggle)
wv 1.2.9-3
  • links: PTS
  • area: main
  • in suites: wheezy
  • size: 5,040 kB
  • sloc: ansic: 31,036; sh: 11,951; xml: 1,677; makefile: 21
file content (383 lines) | stat: -rw-r--r-- 10,475 bytes parent folder | download | duplicates (8)
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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
/* wvWare
 * Copyright (C) Caolan McNamara, Dom Lachowicz, and others
 *
 * 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.
 */

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "wv.h"

#include "wvinternal.h"

static PAPX_FKP wvPAPX_FKP_previous;
static U32 wvPAPX_pn_previous = 0;
static CHPX_FKP wvCHPX_FKP_previous;
static U32 wvCHPX_pn_previous = 0;


void
external_wvReleasePAPX_FKP (void)
{
        if (wvPAPX_pn_previous != 0)
        {
                wvPAPX_pn_previous = 0;
        }
}

void
external_wvReleaseCHPX_FKP (void)
{
        if (wvCHPX_pn_previous != 0)
        {
                wvCHPX_pn_previous = 0;
        }
}

void
internal_wvReleasePAPX_FKP (PAPX_FKP * fkp)
{
    int i;
    wvFree (fkp->rgfc);
    fkp->rgfc = NULL;
    wvFree (fkp->rgbx);
    fkp->rgbx = NULL;
    for (i = 0; i < fkp->crun; i++)
	wvReleasePAPX (&(fkp->grppapx[i]));
    fkp->crun = 0;
    wvFree (fkp->grppapx);
    fkp->grppapx = NULL;
}

/*
At offset
511 is a 1-byte count named crun, which is a count of paragraphs in PAPX 
FKPs. Beginning at offset 0 of the FKP is an array of crun+1 FCs, named 
rgfc, which records the beginning and limit FCs of crun paragraphs.

immediately following the fkp.rgfc is an array of 13 byte
entries called BXs. This array called the rgbx is in 1-to-1 correspondence
with the rgfc. The first byte of the ith BX entry contains a single byte
field which gives the word offset of the PAPX that belongs to the paragraph
whose beginning in FC space is rgfc[i] and whose limit is rgfc[i+1] in FC
space. The last 12 bytes of the ith BX entry contain a PHE structure that
stores the current paragraph height of the paragraph whose beginning in FC
space is rgfc[i] and whose limit is rgfc[i+1] in FC space.
*/

/*
The first byte of each BX is the word offset of the PAPX recorded for
the paragraph corresponding to this BX. .. If the byte stored is 0,
this represents a 1 line paragraph 15 pixels high with Normal style
(stc == 0) whose column width is 7980 dxas. The last 12 bytes of
the BX is a PHE structure which stores the current paragraph height
for the paragraph corresponding to the BX. If a plcfphe has an entry 
that maps to the FC for this paragraph, that entry's PHE overrides the PHE
stored in the FKP.11*fkp.crun+4 unused space. As new runs/paragraphs
are recorded in the FKP, unused space is reduced by 17 if CHPX/PAPX
is already recorded and is reduced by 17+sizeof(PAPX) if property is not
already recorded.
*/

void
wvGetPAPX_FKP (wvVersion ver, PAPX_FKP * fkp, U32 pn, wvStream * fd)
{
    int i;
    U8 page[WV_PAGESIZE];
    U16 pos = 0;
    /*size_t bytes_read; */

    /* brian.ewins@bt.com */
    /* there seem to be a lot of repeat calls... */
    /* pn=0 is safe because thats the index block, not a PAPX_FKP */
    if (pn != 0 && pn == wvPAPX_pn_previous)
      {
	  memcpy (fkp, &wvPAPX_FKP_previous, sizeof (PAPX_FKP));
	  return;
      }

    wvTrace (
	     ("seeking to %x to get crun\n",
	      pn * WV_PAGESIZE + (WV_PAGESIZE - 1)));
    wvStream_goto (fd, pn * WV_PAGESIZE);
    /*bytes_read= */ wvStream_read (page, WV_PAGESIZE, 1, fd);
    fkp->crun = (U8) page[WV_PAGESIZE - 1];
    fkp->rgfc = (U32 *) wvMalloc (sizeof (U32) * (fkp->crun + 1));
    fkp->rgbx = (BX *) wvMalloc (sizeof (BX) * (fkp->crun));
    fkp->grppapx = (PAPX *) wvMalloc (sizeof (PAPX) * (fkp->crun));
    for (i = 0; i < fkp->crun + 1; i++)
      {
	  fkp->rgfc[i] = bread_32ubit (&(page[pos]), &pos);
	  wvTrace (("rgfc is %x\n", fkp->rgfc[i]));
      }

    for (i = 0; i < fkp->crun; i++)
      {
	  if (ver == WORD8)
	      wvGetBX (&fkp->rgbx[i], page, &pos);
	  else
	      wvGetBX6 (&fkp->rgbx[i], page, &pos);
      }

    for (i = 0; i < fkp->crun; i++)
      {
	  if (fkp->rgbx[i].offset == 0)
	    {
		wvTrace (("i is %d, using clear papx\n", i));
		wvInitPAPX (&(fkp->grppapx[i]));
	    }
	  else
	    {
		wvTrace (
			 ("papx index i is %d, offset is %x\n", i,
			  pn * WV_PAGESIZE + fkp->rgbx[i].offset * 2));
		pos = fkp->rgbx[i].offset * 2;
		wvGetPAPX (ver, &(fkp->grppapx[i]), page, &pos);
	    }
      }
    if (wvPAPX_pn_previous != 0)
	internal_wvReleasePAPX_FKP (&wvPAPX_FKP_previous);
    memcpy (&wvPAPX_FKP_previous, fkp, sizeof (PAPX_FKP));
    wvPAPX_pn_previous = pn;
}

/*
Using the FC, search the FCs FKP for the largest FC less than the character's FC,
    call it fcTest.
*/
U32
wvSearchNextLargestFCPAPX_FKP (PAPX_FKP * fkp, U32 currentfc)
{
    U32 i = 0;
    U8 until = fkp->crun + 1;
    U32 fcTest = 0;


    while (i < until)
      {
	  wvTrace (("searching fkp %x %x\n", currentfc, fkp->rgfc[i]));
	  if ((wvNormFC (fkp->rgfc[i], NULL) < currentfc)
	      && (wvNormFC (fkp->rgfc[i], NULL) > fcTest))
	      fcTest = wvNormFC (fkp->rgfc[i], NULL);
	  else if (wvNormFC (fkp->rgfc[i], NULL) == currentfc)
	      fcTest = currentfc + 1;
	  i++;
      }

    /*for the first paragraph return the current pos as the beginning */
    /*
       if (fcTest == 0)
       fcTest = currentfc+1;
     */

    return (fcTest);
}

U32
wvSearchNextLargestFCCHPX_FKP (CHPX_FKP * fkp, U32 currentfc)
{
    U32 i = 0;
    U8 until = fkp->crun + 1;
    U32 fcTest = 0;


    while (i < until)
      {
	  wvTrace (("searching fkp %x %x\n", currentfc, fkp->rgfc[i]));
	  if ((wvNormFC (fkp->rgfc[i], NULL) <= currentfc)
	      && (wvNormFC (fkp->rgfc[i], NULL) > fcTest))
	      fcTest = wvNormFC (fkp->rgfc[i], NULL);
	  i++;
      }

    /*for the first paragraph return the current pos as the beginning */
    /*
       if (fcTest == 0)
       fcTest = currentfc+1;
     */

    return (fcTest);
}

/*
Using the FC of the character, first search the FKP that describes the
character to find the smallest FC in the rgfc that is larger than the character
FC.
*/
U32
wvSearchNextSmallestFCPAPX_FKP (PAPX_FKP * fkp, U32 currentfc)
{
    U32 i = 0;
    U32 fcTest = 0xffffffffL;
    U8 until = fkp->crun + 1;

    while (i < until)
      {
	  wvTrace (
		   ("Smallest %x, %x %x\n", currentfc,
		    wvNormFC (fkp->rgfc[i], NULL), wvNormFC (fkp->rgfc[i],
							     NULL)));
	  if ((wvNormFC (fkp->rgfc[i], NULL) > currentfc)
	      && (wvNormFC (fkp->rgfc[i], NULL) < fcTest))
	      fcTest = wvNormFC (fkp->rgfc[i], NULL);
	  i++;
      }
    return (fcTest);
}

void
wvReleasePAPX_FKP (PAPX_FKP * fkp)
{
    return;
}


void
wvInitPAPX_FKP (PAPX_FKP * fkp)
{
    fkp->rgfc = NULL;
    fkp->rgbx = NULL;
    fkp->crun = 0;
    fkp->grppapx = NULL;
}

int
wvGetIndexFCInFKP_PAPX (PAPX_FKP * fkp, U32 currentfc)
{
    U32 i = 1;			/*was 0, there is something slightly out of sync in the system */
    U8 until = fkp->crun + 1;

    while (i < until)
      {
	  wvTrace (
		   ("current fc is %x, %x, %x\n", currentfc,
		    wvNormFC (fkp->rgfc[i], NULL), fkp->rgfc[i]));
	  if (wvNormFC (fkp->rgfc[i], NULL) == currentfc)
	      return (i);
	  i++;
      }
    /*
       basically read 
       Algorithm to determine paragraph properties for a paragraph &
       Formatted Disk Page for PAPXs, somehow the currentfc sent in was wrong
       or my understanding is !
     */
    wvTrace (("Shite, fix me %x %x\n", currentfc, fkp->rgfc[0]));
    /*return 1 to make things continue on their merry way */
    return (1);
}

void
internal_wvReleaseCHPX_FKP (CHPX_FKP * fkp)
{
    int i;
    wvTrace (("chpx fkp b freeed\n"));
    wvFree (fkp->rgfc);
    fkp->rgfc = NULL;
    wvFree (fkp->rgb);
    fkp->rgb = NULL;
    for (i = 0; i < fkp->crun; i++)
	wvReleaseCHPX (&(fkp->grpchpx[i]));
    fkp->crun = 0;
    wvFree (fkp->grpchpx);
    fkp->grpchpx = NULL;
    wvTrace (("chpx fkp e freeed\n"));
}


/* Character properties 
 * -basically just like PAPX FKPs above
 * however, rather than an array of BX structs in rgbx,
 * there is an array of bytes (giving the word offset to the CHPX) in rgb
 * -JB
 */
void
wvGetCHPX_FKP (wvVersion ver, CHPX_FKP * fkp, U32 pn, wvStream * fd)
{
    int i;
    U8 page[WV_PAGESIZE];
    U16 pos = 0;
    /*size_t bytes_read; */

    /* brian.ewins@bt.com */
    /* there seem to be a lot of repeat calls... */
    /* pn=0 is safe because thats the index block, not a CHPX_FKP */
    if (pn != 0 && pn == wvCHPX_pn_previous)
      {
	  memcpy (fkp, &wvCHPX_FKP_previous, sizeof (CHPX_FKP));
	  return;
      }
    wvStream_goto (fd, pn * WV_PAGESIZE);
    /*bytes_read= */ wvStream_read (page, WV_PAGESIZE, 1, fd);
    fkp->crun = (U8) page[WV_PAGESIZE - 1];
    wvTrace (("chpx fkp gone to %x\n", pn * WV_PAGESIZE + (WV_PAGESIZE - 1)));
    wvTrace (("crun is %d\n", fkp->crun));
    fkp->rgfc = (U32 *) wvMalloc (sizeof (U32) * (fkp->crun + 1));
    fkp->rgb = (U8 *) wvMalloc (sizeof (U8) * (fkp->crun));
    fkp->grpchpx = (CHPX *) wvMalloc (sizeof (CHPX) * (fkp->crun));
    wvStream_goto (fd, pn * WV_PAGESIZE);
    wvTrace (("offset is %x\n", pn * WV_PAGESIZE));
    for (i = 0; i < fkp->crun + 1; i++)
      {
	  fkp->rgfc[i] = bread_32ubit (&(page[pos]), &pos);
	  wvTrace (("rgfc is %x\n", fkp->rgfc[i]));
      }

    for (i = 0; i < fkp->crun; i++)
	fkp->rgb[i] = bread_8ubit (&(page[pos]), &pos);

    for (i = 0; i < fkp->crun; i++)
      {
	  if (fkp->rgb[i] == 0)
	    {
		wvTrace (("i is %d, using clear chpx\n", i));
		wvInitCHPX (&(fkp->grpchpx[i]));
	    }
	  else
	    {
		wvTrace (
			 ("chpx index i is %d, offset is %x\n", i,
			  (pn * WV_PAGESIZE) + (fkp->rgb[i] * 2)));
		pos = fkp->rgb[i] * 2;
		wvGetCHPX (ver, &(fkp->grpchpx[i]), page, &pos);
	    }
      }
    if (wvCHPX_pn_previous != 0)
	internal_wvReleaseCHPX_FKP (&wvCHPX_FKP_previous);
    memcpy (&wvCHPX_FKP_previous, fkp, sizeof (CHPX_FKP));
    wvCHPX_pn_previous = pn;
}

void
wvReleaseCHPX_FKP (CHPX_FKP * fkp)
{
    return;
}


void
wvInitCHPX_FKP (CHPX_FKP * fkp)
{
    fkp->rgfc = NULL;
    fkp->rgb = NULL;
    fkp->crun = 0;
    fkp->grpchpx = NULL;
}