summaryrefslogtreecommitdiff
path: root/noncore/apps/opie-reader/plucker.cpp
blob: 4ea807affcbb89a4159cde7882da87544825310b (plain)
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
#include <stdio.h>
#include <string.h>
#include <qmessagebox.h>
#include <qpixmap.h>
#ifdef USEQPE
#include <qpe/qcopenvelope_qws.h>
#endif
#ifdef LOCALPICTURES
#include <qscrollview.h>
#endif
#ifdef USEQPE
#include <qpe/global.h>
#include <qpe/qpeapplication.h>
#else
#include <qapplication.h>
#endif
#include <qclipboard.h>

#include "plucker.h"
#include "Aportis.h"
#include "Palm2QImage.h"


struct CPlucker_dataRecord
{
    UInt16 uid;
    UInt16 nParagraphs;
    UInt16 size;
    UInt8 type;
    UInt8 reserved;
};

int CPlucker::HeaderSize()
{
    return sizeof(CPlucker_dataRecord);
}

void CPlucker::GetHeader(UInt16& uid, UInt16& nParagraphs, UInt32& size, UInt8& type, UInt8& reserved)
{
    CPlucker_dataRecord thishdr;
    fread(&thishdr, 1, HeaderSize(), fin);
    uid = ntohs(thishdr.uid);
    nParagraphs = ntohs(thishdr.nParagraphs);
    size = ntohs(thishdr.size);
    type = thishdr.type;
    reserved = thishdr.reserved;
}

CPlucker::CPlucker()
 { /*printf("constructing:%x\n",fin);*/
     EOPPhase = 0;
 }

bool CPlucker::CorrectDecoder()
{
    return (memcmp(&head.type, "DataPlkr", 8) == 0);
}

int CPlucker::bgetch()
{
    int ch = EOF;
    if (bufferpos >= buffercontent)
    {
	if (isEndOfSection(bufferrec))
	{
	  if (!m_continuous) return EOF;
	}
	if (bufferrec >= ntohs(head.recordList.numRecords) - 1)
	{
		return EOF;
	}
	if (!expand(bufferrec+1))
	{
		return EOF;
	}
	mystyle.unset();
	if (m_ParaOffsets[m_nextParaIndex] == 0)
	{
	    while (m_ParaOffsets[m_nextParaIndex+1] == 0)
	    {
//		qDebug("Skipping extraspace:%d", m_ParaAttrs[m_nextParaIndex]&7);
		m_nextParaIndex++;
	    }
	}
	mystyle.setExtraSpace((m_ParaAttrs[m_nextParaIndex]&7)*2);
//	qDebug("Using extraspace:%d", m_ParaAttrs[m_nextParaIndex]&7);
	ch = 10;
	if (m_continuous) EOPPhase = 4;
    }
    else if (bufferpos == m_nextPara)
    {
	while (bufferpos == m_nextPara)
	{
	    UInt16 attr = m_ParaAttrs[m_nextParaIndex];
	    m_nextParaIndex++;
//	    qDebug("Skipping extraspace:%d", m_ParaAttrs[m_nextParaIndex]&7);
	    if (m_nextParaIndex == m_nParas)
	    {
		m_nextPara = -1;
	    }
	    else
	    {
		m_nextPara += m_ParaOffsets[m_nextParaIndex];
	    }
	}
	mystyle.unset();
	mystyle.setExtraSpace((m_ParaAttrs[m_nextParaIndex]&7)*2);
//	qDebug("Using extraspace:%d", m_ParaAttrs[m_nextParaIndex]&7);
	if (m_lastBreak == locate())
	{
	    currentpos++;
	    ch = expandedtextbuffer[bufferpos++];
	}
	else
	{
	    ch = 10;
	}
    }
    else
    {
	currentpos++;
	ch = expandedtextbuffer[bufferpos++];
    }
    return ch;
}

tchar CPlucker::getch(bool fast)
{
    mystyle.clearPicture();

    if (EOPPhase > 0)
    {
	int ch = 10;
	switch (EOPPhase)
	{
	    case 4:
		if (!fast) mystyle.setPicture(false, hRule(100,5));
		mystyle.setCentreJustify();
		ch = '#';
		break;
	    case 3:
		mystyle.setFontSize(3);
		ch = 10;
		break;
	    case 2:
		ch = 10;
		break;
	    case 1:
		mystyle.unset();
	    default:
		ch = 10;
	}
	EOPPhase--;
	return ch;
    }

    return getch_base(fast);
}

QImage* CPlucker::imagefromdata(UInt8* imgbuffer, UInt32 imgsize)
{
    QImage* qimage = Palm2QImage(imgbuffer, imgsize);
    delete [] imgbuffer;
    return qimage;
}

void CPlucker::start2endSection() // to plucker base
{
    m_currentstart = NEFstartSection();
    m_currentend = NEFendSection();
}

unsigned long CPlucker::NEFstartSection() // virtual in plucker base - differs only in obsoleted nef so move implementation
{
//inefficient - Should calc start/end of section on entry to section?
    UInt16 thishdr_uid, thishdr_nParagraphs;
    UInt32 thishdr_size;
    UInt8 thishdr_type, thishdr_reserved;
    unsigned long textlength = currentpos-bufferpos;
    for (UInt16 recptr = bufferrec-1; recptr >= 1; recptr--)
    {
	gotorecordnumber(recptr);
	//qDebug("recptr:%u", recptr);
	GetHeader(thishdr_uid, thishdr_nParagraphs, thishdr_size, thishdr_type, thishdr_reserved);
	if (thishdr_type < 2)
	{
	    if ((thishdr_reserved && continuation_bit) == 0) break;
	    textlength -= thishdr_size;
	    //qDebug("Textlength:%u, reserved:%u, recptr:%u", textlength, thishdr_reserved, recptr);
	}
    }
    return textlength;
}

unsigned long CPlucker::NEFendSection() // virtual in plucker base - differs only in obsoleted nef so move implementation
{
//inefficient - Should calc start/end of section on entry to section?
    unsigned long textlength = currentpos-bufferpos+buffercontent;
    gotorecordnumber(bufferrec);
    UInt16 thishdr_uid, thishdr_nParagraphs;
    UInt32 thishdr_size;
    UInt8 thishdr_type, thishdr_reserved;
    GetHeader(thishdr_uid, thishdr_nParagraphs, thishdr_size, thishdr_type, thishdr_reserved);
    if ((thishdr_reserved && continuation_bit) != 0)
      {
	for (UInt16 recptr = bufferrec+1; recptr < ntohs(head.recordList.numRecords); recptr++)
	  {
	    gotorecordnumber(recptr);
	    UInt16 thishdr_uid, thishdr_nParagraphs;
	    UInt32 thishdr_size;
	    UInt8 thishdr_type, thishdr_reserved;
	    GetHeader(thishdr_uid, thishdr_nParagraphs, thishdr_size, thishdr_type, thishdr_reserved);
	    //	qDebug("recptr %u bufferrec %u type %u m_reserved %u", recptr, bufferrec, typ
	    if (thishdr_type < 2)
	      {
		textlength += thishdr_size;
		if ((thishdr_reserved && continuation_bit) == 0) break;
	      }
	  }
      }
    return textlength;
}

#ifndef __STATIC
extern "C"
{
  CExpander* newcodec() { return new CPlucker; }
}
#endif