]> git.saurik.com Git - wxWidgets.git/blame - utils/tex2rtf/src/readshg.cpp
don't lose fonts underlined flag in ReInit() (bug fix for the change in rev. 1.76)
[wxWidgets.git] / utils / tex2rtf / src / readshg.cpp
CommitLineData
9a29912f
JS
1/////////////////////////////////////////////////////////////////////////////
2// Name: readshg.cpp
3// Purpose: Petr Smilauer's .SHG (Segmented Hypergraphics file) reading
4// code.
5// Note: .SHG is undocumented (anywhere!) so this is
6// reverse-engineering
7// and guesswork at its best.
8// Author: Petr Smilauer
9// Modified by:
10// Created: 01/01/99
11// RCS-ID: $Id$
12// Copyright: (c) Petr Smilauer
13// Licence: wxWindows licence
14/////////////////////////////////////////////////////////////////////////////
15
16#ifdef __GNUG__
17#pragma implementation
18#endif
19
20// For compilers that support precompilation, includes "wx.h".
21#include "wx/wxprec.h"
22
23#ifdef __BORLANDC__
24#pragma hdrstop
25#endif
26
27#ifndef WX_PRECOMP
9a29912f
JS
28#endif
29
30#include <stdio.h>
31#include <stdlib.h>
32
33#include "readshg.h"
34#include "tex2any.h"
35
36// Returns the number of hotspots, and the array of hotspots.
37// E.g.
38// HotSpots *array;
39// int n = ParseSHG("thing.shg", &array);
40
6c155d33
JS
41int ParseSHG( const wxChar* fileName, HotSpot **hotspots)
42{ FILE* fSHG = wxFopen( fileName, _T("rb"));
9a29912f
JS
43 long offset;
44 int nHotspots = 0;
45
46 if(fSHG == 0)
47 return 0;
48 nHotspots = 0;
49 //first, look at offset OFF_OFFSET to get another offset :-)
50 fseek( fSHG, OFF_OFFSET, SEEK_SET);
51 offset = 0L; // init whole 4-byte variable
52 fread( &offset, 2, 1, fSHG); // get the offset in first two bytes..
53 if(offset == 0) // if zero, used next DWORD field
54 fread( &offset, 4, 1, fSHG);// this is our offset for very long DIB
55 offset += 9; // don't know hot this delta comes-about
56 if(fseek( fSHG, offset, SEEK_SET) != 0)
57 {
58 fclose( fSHG);
59 return -1; // this is probably because incorrect offset calculation.
60 }
61 fread( &nHotspots, 2, 1, fSHG);
62
63 *hotspots = new HotSpot[nHotspots];
64
65 int nMacroStrings = 0;
66
67 fread( &nMacroStrings, 2, 1, fSHG); // we can ignore the macros, as this is
68 // repeated later, but we need to know how much to skip
69 fseek( fSHG, 2, SEEK_CUR); // skip another 2 bytes I do not understand ;-)
70
71 ShgInfoBlock sib;
72 int i;
73
74 int sizeOf = sizeof( ShgInfoBlock);
75
76 for( i = 0 ; i < nHotspots ; ++i)
77 {
78 fread( &sib, sizeOf, 1, fSHG); // read one hotspot' info
79 // analyse it:
80 (*hotspots)[i].type = (HotspotType)(sib.hotspotType & 0xFB);
81 (*hotspots)[i].left = sib.left;
82 (*hotspots)[i].top = sib.top;
83 (*hotspots)[i].right = sib.left + sib.width;
84 (*hotspots)[i].bottom = sib.top + sib.height;
85 (*hotspots)[i].IsVisible = ((sib.hotspotType & 4) == 0);
86 (*hotspots)[i].szHlpTopic_Macro[0] = '\0';
87 }
88 // we have it...now read-off the macro-string block
89 if(nMacroStrings > 0)
90 fseek( fSHG, nMacroStrings, SEEK_CUR); //nMacroStrings is byte offset...
91 // and, at the last, read through the strings: hotspot-id[ignored], then topic/macro
92 int c;
93 for( i = 0 ; i < nHotspots ; ++i)
94 {
95 while( (c = fgetc( fSHG)) != 0)
96 ;
97 // now read it:
98 int j = 0;
99 while( (c = fgetc( fSHG)) != 0)
100 {
101 (*hotspots)[i].szHlpTopic_Macro[j] = c;
102 ++j;
103 }
104 (*hotspots)[i].szHlpTopic_Macro[j] = 0;
105 }
106 fclose( fSHG);
107 return nHotspots;
108}
109
110
111// Convert Windows .SHG file to HTML map file
112
6c155d33 113bool SHGToMap(wxChar *filename, wxChar *defaultFile)
9a29912f
JS
114{
115 // Test the SHG parser
116 HotSpot *hotspots = NULL;
117 int n = ParseSHG(filename, &hotspots);
118 if (n == 0)
119 return FALSE;
120
6c155d33
JS
121 wxChar buf[100];
122 wxSprintf(buf, _T("Converting .SHG file to HTML map file: there are %d hotspots in %s."), n, filename);
9a29912f
JS
123 OnInform(buf);
124
6c155d33
JS
125 wxChar outBuf[256];
126 wxStrcpy(outBuf, filename);
9a29912f 127 StripExtension(outBuf);
6c155d33 128 wxStrcat(outBuf, _T(".map"));
9a29912f 129
6c155d33 130 FILE *fd = wxFopen(outBuf, _T("w"));
9a29912f
JS
131 if (!fd)
132 {
6c155d33 133 OnError(_T("Could not open .map file for writing."));
9a29912f
JS
134 delete[] hotspots;
135 return FALSE;
136 }
137
6c155d33 138 wxFprintf(fd, _T("default %s\n"), defaultFile);
9a29912f
JS
139 for (int i = 0; i < n; i++)
140 {
6c155d33 141 wxChar *refFilename = _T("??");
9a29912f
JS
142
143 TexRef *texRef = FindReference(hotspots[i].szHlpTopic_Macro);
144 if (texRef)
145 refFilename = texRef->refFile;
146 else
147 {
6c155d33
JS
148 wxChar buf[300];
149 wxSprintf(buf, _T("Warning: could not find hotspot reference %s"), hotspots[i].szHlpTopic_Macro);
9a29912f
JS
150 OnInform(buf);
151 }
6c155d33 152 wxFprintf(fd, _T("rect %s %d %d %d %d\n"), refFilename, (int)hotspots[i].left, (int)hotspots[i].top,
9a29912f
JS
153 (int)hotspots[i].right, (int)hotspots[i].bottom);
154 }
6c155d33 155 wxFprintf(fd, _T("\n"));
9a29912f
JS
156
157 fclose(fd);
158
159 delete[] hotspots;
160 return TRUE;
161}
162