]>
git.saurik.com Git - wxWidgets.git/blob - utils/tex2rtf/src/readshg.cpp
1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: Petr Smilauer's .SHG (Segmented Hypergraphics file) reading
5 // Note: .SHG is undocumented (anywhere!) so this is
7 // and guesswork at its best.
8 // Author: Petr Smilauer
9 // Modified by: Wlodzimiez ABX Skiba 2003/2004 Unicode support
13 // Copyright: (c) Petr Smilauer
14 // Licence: wxWindows licence
15 /////////////////////////////////////////////////////////////////////////////
18 #pragma implementation
21 // For compilers that support precompilation, includes "wx.h".
22 #include "wx/wxprec.h"
37 // Returns the number of hotspots, and the array of hotspots.
40 // int n = ParseSHG("thing.shg", &array);
42 int ParseSHG( const wxChar
* fileName
, HotSpot
**hotspots
)
43 { FILE* fSHG
= wxFopen( fileName
, _T("rb"));
50 //first, look at offset OFF_OFFSET to get another offset :-)
51 fseek( fSHG
, OFF_OFFSET
, SEEK_SET
);
52 offset
= 0L; // init whole 4-byte variable
53 fread( &offset
, 2, 1, fSHG
); // get the offset in first two bytes..
54 if(offset
== 0) // if zero, used next DWORD field
55 fread( &offset
, 4, 1, fSHG
);// this is our offset for very long DIB
56 offset
+= 9; // don't know hot this delta comes-about
57 if(fseek( fSHG
, offset
, SEEK_SET
) != 0)
60 return -1; // this is probably because incorrect offset calculation.
62 fread( &nHotspots
, 2, 1, fSHG
);
64 *hotspots
= new HotSpot
[nHotspots
];
66 int nMacroStrings
= 0;
68 fread( &nMacroStrings
, 2, 1, fSHG
); // we can ignore the macros, as this is
69 // repeated later, but we need to know how much to skip
70 fseek( fSHG
, 2, SEEK_CUR
); // skip another 2 bytes I do not understand ;-)
75 int sizeOf
= sizeof( ShgInfoBlock
);
77 for( i
= 0 ; i
< nHotspots
; ++i
)
79 fread( &sib
, sizeOf
, 1, fSHG
); // read one hotspot' info
81 (*hotspots
)[i
].type
= (HotspotType
)(sib
.hotspotType
& 0xFB);
82 (*hotspots
)[i
].left
= sib
.left
;
83 (*hotspots
)[i
].top
= sib
.top
;
84 (*hotspots
)[i
].right
= sib
.left
+ sib
.width
;
85 (*hotspots
)[i
].bottom
= sib
.top
+ sib
.height
;
86 (*hotspots
)[i
].IsVisible
= ((sib
.hotspotType
& 4) == 0);
87 (*hotspots
)[i
].szHlpTopic_Macro
[0] = '\0';
89 // we have it...now read-off the macro-string block
91 fseek( fSHG
, nMacroStrings
, SEEK_CUR
); //nMacroStrings is byte offset...
92 // and, at the last, read through the strings: hotspot-id[ignored], then topic/macro
94 for( i
= 0 ; i
< nHotspots
; ++i
)
96 while( (c
= fgetc( fSHG
)) != 0)
100 while( (c
= fgetc( fSHG
)) != 0)
102 (*hotspots
)[i
].szHlpTopic_Macro
[j
] = c
;
105 (*hotspots
)[i
].szHlpTopic_Macro
[j
] = 0;
112 // Convert Windows .SHG file to HTML map file
114 bool SHGToMap(wxChar
*filename
, wxChar
*defaultFile
)
116 // Test the SHG parser
117 HotSpot
*hotspots
= NULL
;
118 int n
= ParseSHG(filename
, &hotspots
);
123 wxSnprintf(buf
, sizeof(buf
), _T("Converting .SHG file to HTML map file: there are %d hotspots in %s."), n
, filename
);
127 wxStrcpy(outBuf
, filename
);
128 StripExtension(outBuf
);
129 wxStrcat(outBuf
, _T(".map"));
131 FILE *fd
= wxFopen(outBuf
, _T("w"));
134 OnError(_T("Could not open .map file for writing."));
139 wxFprintf(fd
, _T("default %s\n"), defaultFile
);
140 for (int i
= 0; i
< n
; i
++)
142 wxChar
*refFilename
= _T("??");
144 TexRef
*texRef
= FindReference(hotspots
[i
].szHlpTopic_Macro
);
146 refFilename
= texRef
->refFile
;
150 wxSnprintf(buf
, sizeof(buf
), _T("Warning: could not find hotspot reference %s"), hotspots
[i
].szHlpTopic_Macro
);
153 wxFprintf(fd
, _T("rect %s %d %d %d %d\n"), refFilename
, (int)hotspots
[i
].left
, (int)hotspots
[i
].top
,
154 (int)hotspots
[i
].right
, (int)hotspots
[i
].bottom
);
156 wxFprintf(fd
, _T("\n"));