-
Notifications
You must be signed in to change notification settings - Fork 10
/
layout_single.cpp
550 lines (467 loc) · 12 KB
/
layout_single.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
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
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
/*
* layout_single.cpp - Has responsibility for tracking the list of images and their layout.
* Also has responsibility for building the ImageSource stack at print time.
*
* Copyright (c) 2004 - 2008 by Alastair M. Robinson
* Distributed under the terms of the GNU General Public License -
* see the file named "COPYING" for more details.
*
* 2004-12-24: Now opens the imagesource when placing an image, since we need both the
* natural size of the image (which the thumbnail code could provide)
* and the resolution of the image (which the thumbnail code can't provide).
* Getting the actual imagesource (with CMS transform) from the ImageInfo
* is now delegated to the superclasses.
*
* 2005-08-21: Added allowcropping and allowrotation parameters to allow for the menu
* generalisation.
*
* 2008-09-25: Opening the imagesource and fetching the resolution is now handled
* by the Layout_ImageInfo superclass, since the UI needs to present
* physical size information for all layout types.
*/
#include <iostream>
#include <string.h>
#include "config.h"
#include "miscwidgets/generaldialogs.h"
#include "errordialogqueue.h"
#include "dialogs.h"
#include "pixbufthumbnail/egg-pixbuf-thumbnail.h"
#include "imageutils/rotatepixbuf.h"
#include "imagesource/imagesource_util.h"
#include "imagesource/imagesource_crop.h"
#include "imagesource/imagesource_rotate.h"
#include "photoprint_state.h"
#include "pp_layout_single.h"
#include "debug.h"
#include "layout_single.h"
using namespace std;
ConfigTemplate Layout_SingleDB::Template[]=
{
ConfigTemplate("LeftMargin",10),
ConfigTemplate("RightMargin",10),
ConfigTemplate("TopMargin",10),
ConfigTemplate("BottomMargin",10),
ConfigTemplate()
};
void Layout_Single_ImageInfo::Init()
{
Layout_Single *l=(Layout_Single *)&layout;
l->GetImageableArea();
float sw=(width*72.0)/xres;
float sh=(height*72.0)/yres;
int mm=(l->imageablewidth>l->imageableheight);
mm^=width>height;
if(mm)
{
rotation=PP_ROTATION_90;
sw=(height*72.0)/yres;
sh=(width*72.0)/xres;
}
else
rotation=PP_ROTATION_NONE;
float s=100.0;
float s2=100.0;
if(sw>l->imageablewidth)
s=(100.0*l->imageablewidth)/sw;
if(sh>l->imageableheight)
s2=(100.0*l->imageableheight)/sh;
if(s2<s)
s=s2;
hscale=s;
vscale=s;
}
Layout_Single_ImageInfo::Layout_Single_ImageInfo(Layout_Single &layout, const char *filename,int page,bool allowcropping,PP_ROTATION rotation,bool fliphorizontal,bool flipvertical)
: Layout_ImageInfo((Layout &)layout,filename,page,allowcropping,rotation,fliphorizontal,flipvertical), hscale(100), vscale(100)
{
Init();
}
Layout_Single_ImageInfo::Layout_Single_ImageInfo(Layout_Single &layout, Layout_ImageInfo *ii,int page)
: Layout_ImageInfo((Layout &)layout,ii,page), hscale(100), vscale(100)
{
Init();
}
Layout_Single_ImageInfo::~Layout_Single_ImageInfo()
{
}
void Layout_Single_ImageInfo::DrawThumbnail(GtkWidget *widget,int xpos,int ypos,int dwidth,int dheight)
{
Debug[TRACE] << "Drawing thumbnail" << endl;
GdkPixbuf *thumbnail=GetThumbnail();
GdkPixbuf *transformed=NULL;
Layout_Single *l=(Layout_Single *)&layout;
double xr=(xres*100.0)/hscale;
double yr=(yres*100.0)/vscale;
int w=0,h=0;
Debug[TRACE] << "rotation " << rotation << endl;
switch(rotation)
{
case PP_ROTATION_AUTO:
case PP_ROTATION_NONE:
{
double pw=width*72; pw/=xr;
double ph=height*72; ph/=yr;
w=int(pw*dwidth/l->pagewidth);
h=int(ph*dheight/l->pageheight);
transformed=gdk_pixbuf_scale_simple(thumbnail,w,h,GDK_INTERP_NEAREST);
}
break;
case PP_ROTATION_90:
{
double pw=width*72; pw/=yr;
double ph=height*72; ph/=xr;
GdkPixbuf *tmp=gdk_pixbuf_rotate_simple(thumbnail,GDK_PIXBUF_ROTATE_COUNTERCLOCKWISE);
w=int(ph*dwidth/l->pagewidth);
h=int(pw*dheight/l->pageheight);
transformed=gdk_pixbuf_scale_simple(tmp,w,h,GDK_INTERP_NEAREST);
g_object_unref(G_OBJECT(tmp));
}
break;
case PP_ROTATION_180:
{
double pw=width*72; pw/=yr;
double ph=height*72; ph/=xr;
GdkPixbuf *tmp=gdk_pixbuf_rotate_simple(thumbnail,GDK_PIXBUF_ROTATE_UPSIDEDOWN);
w=int(pw*dwidth/l->pagewidth);
h=int(ph*dheight/l->pageheight);
transformed=gdk_pixbuf_scale_simple(tmp,w,h,GDK_INTERP_NEAREST);
g_object_unref(G_OBJECT(tmp));
}
break;
case PP_ROTATION_270:
{
double pw=width*72; pw/=yr;
double ph=height*72; ph/=xr;
GdkPixbuf *tmp=gdk_pixbuf_rotate_simple(thumbnail,GDK_PIXBUF_ROTATE_CLOCKWISE);
w=int(ph*dwidth/l->pagewidth);
h=int(pw*dheight/l->pageheight);
transformed=gdk_pixbuf_scale_simple(tmp,w,h,GDK_INTERP_NEAREST);
g_object_unref(G_OBJECT(tmp));
}
break;
}
l->GetImageableArea();
int iw=(l->imageablewidth*dwidth)/l->pagewidth;
if(w>iw)
w=iw;
int ih=(l->imageableheight*dheight)/l->pageheight;
if(h>ih)
h=ih;
int lm=(l->leftmargin*dwidth)/l->pagewidth;
int tm=(l->topmargin*dheight)/l->pageheight;
gdk_draw_pixbuf(widget->window,NULL,transformed,
0,0,
xpos+lm,ypos+tm,
w,h,
GDK_RGB_DITHER_NONE,0,0);
g_object_unref(transformed);
Debug[TRACE] << "Finished drawing" << endl;
}
ImageSource *Layout_Single_ImageInfo::GetImageSource(CMColourDevice target,CMTransformFactory *factory)
{
ImageSource *is=Layout_ImageInfo::GetImageSource(target,factory);
// Need to swap H and V scale if the image is rotated.
switch(rotation)
{
case PP_ROTATION_AUTO:
case PP_ROTATION_NONE:
case PP_ROTATION_180:
is->xres=(xres*100)/hscale;
is->yres=(yres*100)/vscale;
break;
case PP_ROTATION_90:
case PP_ROTATION_270:
is->xres=(xres*100)/vscale;
is->yres=(yres*100)/hscale;
break;
}
return(is);
}
LayoutRectangle *Layout_Single_ImageInfo::GetBounds()
{
Debug[TRACE] << "Pixel width: " << width << endl;
Debug[TRACE] << "HScale: " << hscale << endl;
Debug[TRACE] << "XRes: " << xres << endl;
Debug[TRACE] << "Pixel height: " << height << endl;
Debug[TRACE] << "VScale: " << vscale << endl;
Debug[TRACE] << "YRes: " << yres << endl;
float w,h;
switch(rotation)
{
case PP_ROTATION_90:
case PP_ROTATION_270:
Debug[TRACE] << "Rotated" << endl;
w=(width*72*vscale)/(xres*100);
h=(height*72*hscale)/(yres*100);
break;
default:
Debug[TRACE] << "No rotation" << endl;
w=(width*72*hscale)/(xres*100);
h=(height*72*vscale)/(yres*100);
break;
}
return(new LayoutRectangle(0,0,w,h));
}
RectFit *Layout_Single_ImageInfo::GetFit(double scale)
{
LayoutRectangle *r=GetBounds();
RectFit *result=new RectFit;
result->xpos=result->ypos=result->xoffset=result->yoffset=0;
result->width=r->w;
result->height=r->h;
result->scale=1.0;
result->rotation=PP_ROTATION_NONE;
delete r;
return(result);
}
Layout_Single::Layout_Single(PhotoPrint_State &state,Layout *oldlayout)
: Layout(state,oldlayout)
{
}
Layout_Single::~Layout_Single()
{
}
void Layout_Single::Reflow()
{
int page=0;
LayoutIterator it(*this);
Layout_Single_ImageInfo *ii=(Layout_Single_ImageInfo *)it.FirstImage();
while(ii)
{
ii->page=page;
ii=(Layout_Single_ImageInfo *)it.NextImage();
++page;
}
if(page<1)
page=1;
pages=page;
if(currentpage>=page)
currentpage=page-1;
}
int Layout_Single::AddImage(const char *filename,bool allowcropping,PP_ROTATION rotation,bool fliphorizontal,bool flipvertical)
{
int page=pages+1;
for(int i=0;i<=pages;++i)
{
if(!ImageAt(i))
{
Debug[WARN] << "No image found at page " << i << endl;
page=i;
i=pages;
}
}
Debug[TRACE] << "Adding image to page " << page << endl;
Layout_Single_ImageInfo *ii=NULL;
try
{
ii=new Layout_Single_ImageInfo(*this,filename,page,false,rotation,fliphorizontal,flipvertical);
}
catch(const char *msg)
{
// ErrorMessage_Dialog(msg);
ErrorDialogs.AddMessage(msg);
}
if(ii)
{
imagelist.push_back(ii);
if(page>=pages)
++pages;
}
else
page=currentpage;
return(page);
}
void Layout_Single::CopyImage(Layout_ImageInfo *ii)
{
int page=pages+1;
for(int i=0;i<=pages;++i)
{
if(!ImageAt(i))
{
Debug[WARN] << "No image found at page " << i << endl;
page=i;
i=pages;
}
}
ii=new Layout_Single_ImageInfo(*this,ii,page);
imagelist.push_back(ii);
if(page>=pages)
++pages;
}
ImageSource *Layout_Single::GetImageSource(int page,CMColourDevice target,CMTransformFactory *factory,int res,bool completepage)
{
ImageSource *result=NULL;
try
{
Layout_Single_ImageInfo *ii=(Layout_Single_ImageInfo *)ImageAt(page);
if(ii)
{
ImageSource *is=ii->GetImageSource(target,factory);
switch(ii->rotation)
{
case PP_ROTATION_90:
is=new ImageSource_Rotate(is,90);
break;
case PP_ROTATION_180:
is=new ImageSource_Rotate(is,180);
break;
case PP_ROTATION_270:
is=new ImageSource_Rotate(is,270);
break;
default:
break;
}
xoffset=leftmargin;
yoffset=topmargin;
GetImageableArea();
int iw=int((imageablewidth*is->xres)/72.0);
int ih=int((imageableheight*is->yres)/72.0);
if((iw<is->width) || (ih<is->height))
{
is=new ImageSource_Crop(is,0,0,iw,ih);
}
return(is);
}
}
catch (const char *msg)
{
// ErrorMessage_Dialog(msg);
ErrorDialogs.AddMessage(msg);
}
return(result);
}
void Layout_Single::SetPageExtent(PageExtent &pe)
{
pe.GetImageableArea();
pagewidth=pe.pagewidth;
pageheight=pe.pageheight;
leftmargin=pe.leftmargin;
rightmargin=pe.rightmargin;
topmargin=pe.topmargin;
bottommargin=pe.bottommargin;
GetImageableArea();
}
void Layout_Single::DBToLayout(LayoutDB &db)
{
Layout::DBToLayout(db);
SetPageExtent(state.printer);
SetMargins(db.singledb.FindInt("LeftMargin"),db.singledb.FindInt("RightMargin"),
db.singledb.FindInt("TopMargin"),db.singledb.FindInt("BottomMargin"));
}
void Layout_Single::LayoutToDB(LayoutDB &db)
{
Layout::LayoutToDB(db);
db.singledb.SetInt("LeftMargin",leftmargin);
db.singledb.SetInt("RightMargin",rightmargin);
db.singledb.SetInt("TopMargin",topmargin);
db.singledb.SetInt("BottomMargin",bottommargin);
}
Layout_Single_ImageInfo *Layout_Single::ImageAt(int page)
{
Layout_Single_ImageInfo *result=NULL;
LayoutIterator it(*this);
Layout_ImageInfo *ii=it.FirstImage();
while(ii)
{
Layout_Single_ImageInfo *nii=(Layout_Single_ImageInfo *)ii;
if(nii->page==page)
{
result=nii;
}
ii=it.NextImage();
}
return(result);
}
GtkWidget *Layout_Single::CreateWidget()
{
return(pp_layout_single_new(&state));
}
void Layout_Single::RefreshWidget(GtkWidget *widget)
{
pp_layout_single_refresh(PP_LAYOUT_SINGLE(widget));
}
const char *Layout_Single::GetType()
{
return("Single");
}
void (*Layout_Single::SetUnitFunc())(GtkWidget *wid,enum Units unit)
{
return(pp_layout_single_set_unit);
}
int Layout_Single::GetCapabilities()
{
return(PPLAYOUT_ROTATE|PPLAYOUT_MASK|PPLAYOUT_EFFECTS|PPLAYOUT_PROFILE);
}
// We override this to set the top/left margin
void Layout_Single::Print(Progress *p)
{
xoffset=leftmargin;
yoffset=topmargin;
Layout::Print(p);
}
bool Layout_Single_ImageInfo::GetSelected()
{
Layout_Single *l=(Layout_Single *)&layout;
return(page==l->currentpage);
}
#if 0
class LayoutIterator_Single : public LayoutIterator
{
LayoutIterator_Single(Layout_Single &header) : LayoutIterator(header)
{
}
virtual ~LayoutIterator_Single()
{
}
virtual Layout_ImageInfo *FirstSelected()
{
Layout_ImageInfo *ii=FirstImage();
while(ii)
{
if(ii->page==header.currentpage)
return(ii);
ii=NextSelected();
}
return(NULL);
}
virtual Layout_ImageInfo *NextSelected()
{
Layout_ImageInfo *ii=NextImage();
while(ii)
{
if(ii->page==header.currentpage)
return(ii);
ii=NextSelected();
}
return(NULL);
}
};
LayoutIterator Layout_Single::GetIterator()
{
return(LayoutIterator_Single(*this));
}
#endif
#if 0
// Re-implement this with a subclass of LayoutIterator
Layout_ImageInfo *Layout_Single::FirstSelected()
{
Layout_ImageInfo *ii=FirstImage();
while(ii)
{
if(ii->page==currentpage)
return(ii);
ii=NextSelected();
}
return(NULL);
}
Layout_ImageInfo *Layout_Single::NextSelected()
{
Layout_ImageInfo *ii=NextImage();
while(ii)
{
if(ii->page==currentpage)
return(ii);
ii=NextSelected();
}
return(NULL);
}
#endif