/
MConvert.cpp
315 lines (254 loc) · 11 KB
/
MConvert.cpp
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
//---------------------------------------------------------------------------
#include <vcl.h>
#include <stdio.h>
#include <Printers.hpp>
#include <string.h>
#include "MConvert.h"
#include "qvarnt.h"
#include "ExprDraw.hpp"
#include "ExprMake.hpp"
#include "tkurs.h"
#include "qvarnt.h"
#include "MLog.h"
#pragma hdrstop
#include <process.h>
//---------------------------------------------------------------------------
extern tkurs* kurs;
#pragma package(smart_init)
//---------------------------------------------------------------------------
//#define OLDTXTTOBMP
#ifndef OLDTXTTOBMP
/** This version of TxtToBmp uses txt2bmp utility located in /utility dir */
void TxtToBmp (TList *List, Graphics::TBitmap *Bitmap, TFont *Font, int left, int top)
{
AnsiString /** Define locations of some file needed for conversion */
workdir = ExtractFilePath(Application->ExeName)+"utility\\",
utility = workdir + "txt2bmp.exe", /** utility itself, */
textfile = workdir + "__temp.txt", /** input file, */
picfile = workdir + "__temp.bmp", /** resulting file, */
conffile = workdir + "custom.cfg"; /** config to write font *
* and indent settings. */
Log->Add("Converting parameters:");
/** Save text to temporary text file */
FILE *f = fopen(textfile.c_str(), "wt");
for(int i=0,j; i<List->Count; i++)
{
char str[10000];
strcpy(str, (const char *)List->Items[i]);
if ((j=strcspn(str,"#"))<strlen(str)) str[j]=' ';
fprintf(f, "%s\n", str);
}
fclose(f);
/** * * * * * * * * * * * * * * * **** * * * * * * * * * * * * * * **
* *
* Set custom config. File must have the following text format: *
* *
* - left\n, top\n : integer values, indents *
* - fontface\n : string, name of the font to draw image with *
* - fontsize\n : integer, size of the font *
* - stylestring : string, determines font style. it must not *
* contain any delimiters. *
* For stylestring, if it contains 'b' character, font will *
* be set to bold, 'i' and 'u' is for italic and underline rspct. *
* *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
/* FILE *fconfig = fopen(conffile.c_str(), "wt");
fprintf(fconfig, "%d\n%d\n%s\n%d\n%c%c%c",
left, top,
Font->Name.c_str(),
Font->Size,
Font->Style.Contains(fsBold)?'b':'x',
Font->Style.Contains(fsItalic)?'i':'x',
Font->Style.Contains(fsUnderline)?'u':'x'
);
fclose(f); */
// Call the utility
SHELLEXECUTEINFO sei;
sei.cbSize = sizeof(sei);
sei.fMask = SEE_MASK_NOCLOSEPROCESS;
sei.lpVerb = NULL;
sei.lpFile = utility.c_str();
Log->Add(strdup(("\""+textfile + "\" \"" + picfile+"\"").c_str()));
sei.lpParameters = strdup(("\""+textfile + "\" \"" + picfile+"\"").c_str());
sei.lpDirectory = ("\""+workdir+"\"").c_str();
sei.nShow = SW_HIDE;
ShellExecuteEx(&sei);
/// ... wait while it works
WaitForSingleObject(sei.hProcess, INFINITE);
//spawnlp(P_WAIT,utility.c_str(),strdup(("\""+textfile + "\" \"" + picfile+"\"").c_str()),NULL);
//Load the result*/
Bitmap->LoadFromFile(picfile);
}
void TxtToBmp (TStrings *List, Graphics::TBitmap *Bitmap, TFont *Font, int left, int top)
{
TList *newlist = new TList;
for(int i=0; i<List->Count; ++i)
{
char str[10000];
strcpy(str, List->Strings[i].c_str());
newlist->Add(strdup(str));
}
TxtToBmp(newlist, Bitmap, Font, left, top);
}
#else
void TxtToBmp ( TList * List, Graphics::TBitmap * Bitmap, TFont * Font, int left, int top )
{
FILE *f = fopen("f:\\1.txt", "wt");
for(int i=0,j; i<List->Count; i++)
{
char str[10000];
strcpy(str, (const char *)List->Items[i]);
if ((j=strcspn(str,"#"))<strlen(str)) str[j]=' ';
fprintf(f, "%s\n", str);
}
fclose(f);
return;
int i, j, x, y;
TExprClass *Expr;// = new TExprClass();
char Str[10000];
x = left;
y = top;
Bitmap->Width = 1500;
Bitmap->Height = 9000;
for(i=0; i<List->Count; i++)
{
strcpy(Str, (char*)List->Items[i]);
if ((j=strcspn(Str,"#")) < strlen(Str))
{
Str[j] = ' ';
strcpy((char *)List->Items[i], Str);
}
if (strcmp(Str, (char*)List->Items[i]))
ShowMessage("memory corruption! before");
#ifdef LOG_TXTTOBMP
FILE *fil = fopen("c:\\mentor.log", "at");
fprintf(fil, "%s\n", Str);
fclose(fil);
#endif
Expr = BuildExpr(Str);
if (strcmp(Str, (char*)List->Items[i]))
ShowMessage("memory corruption! after");
Expr->Canvas = Bitmap->Canvas;
Expr->Font = Font;
//Expr->Font->Size = Font->Size;
//Expr->Color = Font->Color;
Expr->Draw( left, y, 1500, 9000 );
y = y + Expr->Height;
if (x<Expr->Width)
x = Expr->Width;
delete Expr;
}
Bitmap->Width = x + left;
Bitmap->Height = y;
Bitmap->SaveToFile("c:\\1.bmp");
}
//------------------------------------------------------------------------------
void TxtToBmp ( TStrings * List, Graphics::TBitmap * Bitmap, TFont * Font, int left, int top )
{
int i, j, x, y;
TExprClass * Expr = new TExprClass();
char Str[10000];
x = left;
y = top;
Bitmap->Width = 1500;
Bitmap->Height = 9000;
for ( i = 0; i < List->Count; i ++ )
{
strcpy(Str, List->Strings[i].c_str());
j = strcspn(Str, "#" );
if( j < strlen( Str ) )
{
Str[j] = ' ';
List->Strings[i] = (AnsiString)Str;
}
Expr = BuildExpr(Str);
Expr->Canvas = Bitmap->Canvas;
Expr->Font = Font;
Expr->Font->Size = Font->Size;
Expr->Color = Font->Color;
Expr->Draw( left, y, 1500, 9000 );
y = y + Expr->Height;
if ( x < Expr->Width )
x = Expr->Width;
delete Expr;
}
Bitmap->Width = x + left;
Bitmap->Height = y;
}
#endif
//------------------------------------------------------------------------------
void FilePrint ( AnsiString FileName )
{
int TempHeight, w, h, str_n = 1;
Graphics::TBitmap * Bitmap = new Graphics::TBitmap();
qvar->ImageEnProc1->AttachedBitmap = Bitmap;
qvar->ImageEnIO1->AttachedBitmap = Bitmap;
//Bitmap->LoadFromFile( FileName );
qvar->ImageEnIO1->LoadFromFile( FileName );
qvar->ImageEnIO1->Params->JPEG_Quality = 70;
//w = Bitmap->Width * 6;
w = -1;
//h = Bitmap->Height * Printer()->PageHeight / 1100;
h = Bitmap->Height * 6;
/*ShowMessage( Printer()->PageHeight );
ShowMessage( h );*/
//qvar->ImageEnProc1->Resample( w, h, rfBilinear );
//qvar->ImageEnProc1->Resample( w, h, rfBSpline );
qvar->ImageEnProc1->Resample( w, h, rfFastLinear );
//qvar->ImageEnIO1->DoPrintPreviewDialog( iedtDialog, "Ïðîñìîòð ïå÷àòè", true, "Ïå÷àòü" );
/*Printer()->BeginDoc();
qvar->ImageEnIO1->PrintImage( Printer()->Canvas,0,0,0,0,ievpTOP,iehpLEFT,iesNORMAL,0,0,1);
Printer()->EndDoc();*/
if ( h <= Printer()->PageHeight )
{
Printer()->BeginDoc();
Printer()->Canvas->Draw( 0, 0, qvar->ImageEnIO1->AttachedBitmap );
//qvar->ImageEnIO1->PrintImage( Printer()->Canvas,0,0,0,0,ievpTOP,iehpLEFT,iesNORMAL,0,0,1);
//qvar->ImageEnIO1->PrintImagePos( Printer()->Canvas, 0, 0, w, h, 1 );
Printer()->EndDoc();
}
else
{
TempHeight = 0;
Printer()->BeginDoc();
while ( h - TempHeight > 0 )
{
Printer()->Canvas->CopyRect( Rect( 0, 0, Printer()->PageWidth, Printer()->PageHeight ), qvar->ImageEnIO1->AttachedBitmap->Canvas, Rect( 0, TempHeight, Printer()->PageWidth, TempHeight + Printer()->PageHeight ) );
//qvar->ImageEnIO1->PrintImagePos( Printer()->Canvas, 0, TempHeight, Printer()->PageWidth, TempHeight + Printer()->PageHeight, 1 );
//qvar->ImageEnIO1->PrintImagePos( Printer()->Canvas, TempHeight, 0, qvar->ImageEnIO1->AttachedBitmap->Width, TempHeight + Printer()->PageHeight, 1 );
Printer()->Canvas->TextOut( Printer()->PageWidth / 2, 10, "Ñòðàíèöà ¹" + IntToStr( Printer()->PageNumber ));
//Printer()->Canvas->TextOut( Printer()->PageWidth / 2, 10, "Ñòðàíèöà ¹" + IntToStr( str_n ++ ));
TempHeight += Printer()->PageHeight;
if ( h - TempHeight > 0 )
Printer()->NewPage();
}
Printer()->EndDoc();
}
delete Bitmap;
unlink( FileName.c_str() );
/*int TempHeight;
Graphics::TBitmap * Bitmap = new Graphics::TBitmap();
Bitmap->LoadFromFile( FileName );
if ( Bitmap->Height <= Printer()->PageHeight )
{
Printer()->BeginDoc();
Printer()->Canvas->Draw( 0, 0, Bitmap );
Printer()->EndDoc();
}
else
{
TempHeight = 0;
Printer()->BeginDoc();
while ( Bitmap->Height - TempHeight > 0 )
{
Printer()->Canvas->CopyRect( Rect( 0, 0, Printer()->PageWidth, Printer()->PageHeight ), Bitmap->Canvas, Rect( 0, TempHeight, Printer()->PageWidth, TempHeight + Printer()->PageHeight ) );
Printer()->Canvas->TextOut( Printer()->PageWidth / 2, 10, "Ñòðàíèöà ¹" + IntToStr( Printer()->PageNumber ));
TempHeight += Printer()->PageHeight;
if ( Bitmap->Height - TempHeight > 0 )
Printer()->NewPage();
}
Printer()->EndDoc();
}
delete Bitmap;*/
}
//------------------------------------------------------------------------------