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
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
|
/*
* $Id$
* ============================================================================
* The Apache Software License, Version 1.1
* ============================================================================
*
* Copyright (C) 1999-2003 The Apache Software Foundation. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modifica-
* tion, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* 3. The end-user documentation included with the redistribution, if any, must
* include the following acknowledgment: "This product includes software
* developed by the Apache Software Foundation (http://www.apache.org/)."
* Alternately, this acknowledgment may appear in the software itself, if
* and wherever such third-party acknowledgments normally appear.
*
* 4. The names "FOP" and "Apache Software Foundation" must not be used to
* endorse or promote products derived from this software without prior
* written permission. For written permission, please contact
* apache@apache.org.
*
* 5. Products derived from this software may not be called "Apache", nor may
* "Apache" appear in their name, without prior written permission of the
* Apache Software Foundation.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
* FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* APACHE SOFTWARE FOUNDATION OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLU-
* DING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
* ============================================================================
*
* This software consists of voluntary contributions made by many individuals
* on behalf of the Apache Software Foundation and was originally created by
* James Tauber <jtauber@jtauber.com>. For more information on the Apache
* Software Foundation, please see <http://www.apache.org/>.
*/
package org.apache.fop.layoutmgr;
import org.apache.fop.area.LinkResolver;
import org.apache.fop.area.PageViewport;
import org.apache.fop.area.Resolveable;
import org.apache.fop.area.Trait;
import org.apache.fop.area.inline.InlineArea;
import org.apache.fop.area.inline.InlineParent;
import org.apache.fop.area.inline.FilledArea;
import org.apache.fop.area.inline.ForeignObject;
import org.apache.fop.area.inline.Space;
import org.apache.fop.area.inline.UnresolvedPageNumber;
import org.apache.fop.area.inline.Word;
import org.apache.fop.area.inline.Image;
import org.apache.fop.area.inline.Viewport;
import org.apache.fop.datatypes.Length;
import org.apache.fop.fo.FOTreeVisitor;
import org.apache.fop.fo.FObj;
import org.apache.fop.fo.FObjMixed;
import org.apache.fop.fo.FONode;
import org.apache.fop.fo.FOText;
import org.apache.fop.fo.TextInfo;
import org.apache.fop.fo.XMLObj;
import org.apache.fop.fo.flow.BidiOverride;
import org.apache.fop.fo.flow.Inline;
import org.apache.fop.fo.flow.BasicLink;
import org.apache.fop.fo.flow.Block;
import org.apache.fop.fo.flow.Leader;
import org.apache.fop.fo.flow.RetrieveMarker;
import org.apache.fop.fo.flow.Character;
import org.apache.fop.fo.flow.ExternalGraphic;
import org.apache.fop.fo.flow.BlockContainer;
import org.apache.fop.fo.flow.Footnote;
import org.apache.fop.fo.flow.ListBlock;
import org.apache.fop.fo.flow.ListItemBody;
import org.apache.fop.fo.flow.ListItemLabel;
import org.apache.fop.fo.flow.InstreamForeignObject;
import org.apache.fop.fo.flow.InlineContainer;
import org.apache.fop.fo.flow.ListItem;
import org.apache.fop.fo.flow.PageNumber;
import org.apache.fop.fo.flow.PageNumberCitation;
import org.apache.fop.fo.flow.Table;
import org.apache.fop.fo.flow.TableBody;
import org.apache.fop.fo.flow.TableCell;
import org.apache.fop.fo.flow.TableColumn;
import org.apache.fop.fo.flow.TableRow;
import org.apache.fop.fo.pagination.Flow;
import org.apache.fop.fo.properties.LeaderPattern;
import org.apache.fop.fo.properties.CommonBorderAndPadding;
import org.apache.fop.fo.properties.CommonBackground;
import org.apache.fop.fo.properties.Overflow;
import org.apache.fop.fo.properties.Scaling;
import org.apache.fop.layoutmgr.BidiLayoutManager;
import org.apache.fop.layoutmgr.LayoutProcessor;
import org.apache.fop.layoutmgr.LMiter;
import org.apache.fop.layoutmgr.table.Cell;
import org.apache.fop.layoutmgr.table.Column;
import org.apache.fop.layoutmgr.table.Body;
import org.apache.fop.layoutmgr.table.Row;
import org.apache.fop.layoutmgr.table.TableLayoutManager;
import org.apache.fop.layoutmgr.list.Item;
import org.apache.fop.layoutmgr.list.ListBlockLayoutManager;
import org.apache.fop.layoutmgr.list.ListItemLayoutManager;
import org.apache.fop.util.CharUtilities;
import java.util.List;
import java.util.ListIterator;
import java.util.ArrayList;
import java.awt.geom.Point2D;
import java.awt.geom.Rectangle2D;
import org.apache.fop.apps.*;
/**
* Concrete implementation of FOTreeVisitor for the purpose of adding
* Layout Managers for nodes in the FOTree.
* Each method is responsible to return a LayoutManager responsible for laying
* out this FObj's content.
* @see org.apache.fop.fo.FOTreeVisitor
*/
public class AddLMVisitor extends FOTreeVisitor {
/** The List object to which methods in this class should add Layout
* Managers */
private List currentLMList;
/** A List object which can be used to save and restore the currentLMList if
* another List should temporarily be used */
private List saveLMList;
/**
*
* @param fobj the FObj object for which a layout manager should be created
* @param lmList the list to which the newly created layout manager(s)
* should be added
*/
public void addLayoutManager(FObj fobj, List lmList) {
/* Store the List in a global variable so that it can be accessed by the
Visitor methods */
currentLMList = lmList;
fobj.acceptVisitor(this);
}
public void serveVisitor(FOText node) {
if (node.length == 0) {
return;
}
if (node.length < node.ca.length) {
char[] tmp = node.ca;
node.ca = new char[node.length];
System.arraycopy(tmp, 0, node.ca, 0, node.length);
}
LayoutManager lm = new TextLayoutManager(node.ca, node.textInfo);
lm.setFObj(node);
currentLMList.add(lm);
}
public void serveVisitor(FObjMixed node) {
if (node.getChildren() != null) {
InlineStackingLayoutManager lm;
lm = new InlineStackingLayoutManager();
lm.setUserAgent(node.getUserAgent());
lm.setFObj(node);
lm.setLMiter(new LMiter(node.getChildren()));
currentLMList.add(lm);
}
}
public void serveVisitor(BidiOverride node) {
if (false) {
serveVisitor((FObjMixed)node);
} else {
ArrayList childList = new ArrayList();
saveLMList = currentLMList;
currentLMList = childList;
serveVisitor((FObjMixed)node);
currentLMList = saveLMList;
for (int count = childList.size() - 1; count >= 0; count--) {
LayoutProcessor lm = (LayoutProcessor) childList.get(count);
if (lm.generatesInlineAreas()) {
LayoutProcessor blm = new BidiLayoutManager((LeafNodeLayoutManager) lm);
blm.setFObj(node);
currentLMList.add(blm);
} else {
currentLMList.add(lm);
}
}
}
}
/**
* @param node Inline object to process
*/
public void serveVisitor(Inline node) {
serveVisitor((FObjMixed)node);
}
public void serveVisitor(Footnote node) {
if (node.getInlineFO() == null) {
node.getLogger().error("inline required in footnote");
return;
}
serveVisitor(node.getInlineFO());
}
public void serveVisitor(InlineContainer node) {
ArrayList childList = new ArrayList();
saveLMList = currentLMList;
currentLMList = childList;
serveVisitor((FObj)node);
currentLMList = saveLMList;
LayoutManager lm = new ICLayoutManager(childList);
lm.setUserAgent(node.getUserAgent());
lm.setFObj(node);
currentLMList.add(lm);
}
/**
* Add start and end properties for the link
*/
public void serveVisitor(BasicLink node) {
node.setup();
InlineStackingLayoutManager lm;
lm = new InlineStackingLayoutManager() {
protected InlineParent createArea(BasicLink node) {
InlineParent area = super.createArea();
setupBasicLinkArea(node, parentLM, area);
return area;
}
};
lm.setUserAgent(node.getUserAgent());
lm.setFObj(node);
lm.setLMiter(new LMiter(node.getChildren()));
currentLMList.add(lm);
}
protected void setupBasicLinkArea(BasicLink node, LayoutProcessor parentLM,
InlineParent area) {
if (node.getLink() == null) {
return;
}
if (node.getExternal()) {
area.addTrait(Trait.EXTERNAL_LINK, node.getLink());
} else {
PageViewport page = parentLM.resolveRefID(node.getLink());
if (page != null) {
area.addTrait(Trait.INTERNAL_LINK, page.getKey());
} else {
LinkResolver res = new LinkResolver(node.getLink(), area);
parentLM.addUnresolvedArea(node.getLink(), res);
}
}
}
public void serveVisitor(Block node) {
BlockLayoutManager blm = new BlockLayoutManager();
blm.setUserAgent(node.getUserAgent());
blm.setFObj(node);
TextInfo ti = node.getPropertyManager().getTextLayoutProps(node.getFOTreeControl());
blm.setBlockTextInfo(ti);
currentLMList.add(blm);
}
public void serveVisitor(final Leader node) {
LeafNodeLayoutManager lm = new LeafNodeLayoutManager() {
public InlineArea get(LayoutContext context) {
return getLeaderInlineArea(node);
}
protected MinOptMax getAllocationIPD(int refIPD) {
return getLeaderAllocIPD(node, refIPD);
}
/*protected void offsetArea(LayoutContext context) {
if(leaderPattern == LeaderPattern.DOTS) {
curArea.setOffset(context.getBaseline());
}
}*/
};
lm.setUserAgent(node.getUserAgent());
lm.setFObj(node);
lm.setAlignment(node.properties.get("leader-alignment").getEnum());
currentLMList.add(lm);
}
public MinOptMax getLeaderAllocIPD(Leader node, int ipd) {
// length of the leader
int opt = node.getLength("leader-length.optimum", ipd);
int min = node.getLength("leader-length.minimum", ipd);
int max = node.getLength("leader-length.maximum", ipd);
return new MinOptMax(min, opt, max);
}
private InlineArea getLeaderInlineArea(Leader node) {
node.setup();
InlineArea leaderArea = null;
if (node.getLeaderPattern() == LeaderPattern.RULE) {
org.apache.fop.area.inline.Leader leader = new org.apache.fop.area.inline.Leader();
leader.setRuleStyle(node.getRuleStyle());
leader.setRuleThickness(node.getRuleThickness());
leaderArea = leader;
} else if (node.getLeaderPattern() == LeaderPattern.SPACE) {
leaderArea = new Space();
} else if (node.getLeaderPattern() == LeaderPattern.DOTS) {
Word w = new Word();
char dot = '.'; // userAgent.getLeaderDotCharacter();
w.setWord("" + dot);
w.addTrait(Trait.FONT_NAME, node.getFontState().getFontName());
w.addTrait(Trait.FONT_SIZE,
new Integer(node.getFontState().getFontSize()));
// set offset of dot within inline parent
w.setOffset(node.getFontState().getAscender());
int width = CharUtilities.getCharWidth(dot, node.getFontState());
Space spacer = null;
if (node.getPatternWidth() > width) {
spacer = new Space();
spacer.setWidth(node.getPatternWidth() - width);
width = node.getPatternWidth();
}
FilledArea fa = new FilledArea();
fa.setUnitWidth(width);
fa.addChild(w);
if (spacer != null) {
fa.addChild(spacer);
}
fa.setHeight(node.getFontState().getAscender());
leaderArea = fa;
} else if (node.getLeaderPattern() == LeaderPattern.USECONTENT) {
if (node.getChildren() == null) {
node.getLogger().error("Leader use-content with no content");
return null;
}
InlineStackingLayoutManager lm;
lm = new InlineStackingLayoutManager();
lm.setUserAgent(node.getUserAgent());
lm.setFObj(node);
lm.setLMiter(new LMiter(node.getChildren()));
lm.init();
// get breaks then add areas to FilledArea
FilledArea fa = new FilledArea();
ContentLayoutManager clm = new ContentLayoutManager(fa);
clm.setUserAgent(node.getUserAgent());
lm.setParent(clm);
clm.fillArea(lm);
int width = clm.getStackingSize();
Space spacer = null;
if (node.getPatternWidth() > width) {
spacer = new Space();
spacer.setWidth(node.getPatternWidth() - width);
width = node.getPatternWidth();
}
fa.setUnitWidth(width);
if (spacer != null) {
fa.addChild(spacer);
}
leaderArea = fa;
}
return leaderArea;
}
public void serveVisitor(RetrieveMarker node) {
RetrieveMarkerLayoutManager rmlm;
rmlm = new RetrieveMarkerLayoutManager(node.getRetrieveClassName(),
node.getRetrievePosition(),
node.getRetrieveBoundary());
rmlm.setUserAgent(node.getUserAgent());
rmlm.setFObj(node);
currentLMList.add(rmlm);
}
public void serveVisitor(Character node) {
InlineArea inline = getCharacterInlineArea(node);
if (inline != null) {
LeafNodeLayoutManager lm = new LeafNodeLayoutManager();
lm.setUserAgent(node.getUserAgent());
lm.setFObj(node);
lm.setCurrentArea(inline);
currentLMList.add(lm);
}
}
public InlineArea getCharacterInlineArea(Character node) {
String str = node.properties.get("character").getString();
if (str.length() == 1) {
org.apache.fop.area.inline.Character ch =
new org.apache.fop.area.inline.Character(
str.charAt(0));
return ch;
}
return null;
}
/**
* This adds a leafnode layout manager that deals with the
* created viewport/image area.
*/
public void serveVisitor(ExternalGraphic node) {
InlineArea area = getExternalGraphicInlineArea(node);
if (area != null) {
node.setupID();
LeafNodeLayoutManager lm = new LeafNodeLayoutManager();
lm.setUserAgent(node.getUserAgent());
lm.setFObj(node);
lm.setCurrentArea(area);
lm.setAlignment(node.properties.get("vertical-align").getEnum());
lm.setLead(node.getViewHeight());
currentLMList.add(lm);
}
}
/**
* Get the inline area for this external grpahic.
* This creates the image area and puts it inside a viewport.
*
* @return the viewport containing the image area
*/
public InlineArea getExternalGraphicInlineArea(ExternalGraphic node) {
node.setup();
if (node.getURL() == null) {
return null;
}
Image imArea = new Image(node.getURL());
Viewport vp = new Viewport(imArea);
vp.setWidth(node.getViewWidth());
vp.setHeight(node.getViewHeight());
vp.setClip(node.getClip());
vp.setContentPosition(node.getPlacement());
vp.setOffset(0);
// Common Border, Padding, and Background Properties
CommonBorderAndPadding bap = node.getPropertyManager().getBorderAndPadding();
CommonBackground bProps = node.getPropertyManager().getBackgroundProps();
TraitSetter.addBorders(vp, bap);
TraitSetter.addBackground(vp, bProps);
return vp;
}
public void serveVisitor(BlockContainer node) {
BlockContainerLayoutManager blm = new BlockContainerLayoutManager();
blm.setUserAgent(node.getUserAgent());
blm.setFObj(node);
blm.setOverflow(node.properties.get("overflow").getEnum());
currentLMList.add(blm);
}
public void serveVisitor(ListBlock node) {
ListBlockLayoutManager blm = new ListBlockLayoutManager();
blm.setUserAgent(node.getUserAgent());
blm.setFObj(node);
currentLMList.add(blm);
}
public void serveVisitor(InstreamForeignObject node) {
Viewport areaCurrent = getInstreamForeignObjectInlineArea(node);
if (areaCurrent != null) {
LeafNodeLayoutManager lm = new LeafNodeLayoutManager();
lm.setUserAgent(node.getUserAgent());
lm.setFObj(node);
lm.setCurrentArea(areaCurrent);
lm.setAlignment(node.properties.get("vertical-align").getEnum());
lm.setLead(areaCurrent.getHeight());
currentLMList.add(lm);
}
}
/**
* Get the inline area created by this element.
*
* @return the viewport inline area
*/
public Viewport getInstreamForeignObjectInlineArea(InstreamForeignObject node) {
if (node.getChildren() == null) {
return null;
}
if (node.children.size() != 1) {
// error
return null;
}
FONode fo = (FONode)node.children.get(0);
if (!(fo instanceof XMLObj)) {
// error
return null;
}
XMLObj child = (XMLObj)fo;
// viewport size is determined by block-progression-dimension
// and inline-progression-dimension
// if replaced then use height then ignore block-progression-dimension
//int h = this.properties.get("height").getLength().mvalue();
// use specified line-height then ignore dimension in height direction
boolean hasLH = false;//properties.get("line-height").getSpecifiedValue() != null;
Length len;
int bpd = -1;
int ipd = -1;
boolean bpdauto = false;
if (hasLH) {
bpd = node.properties.get("line-height").getLength().getValue();
} else {
// this property does not apply when the line-height applies
// isn't the block-progression-dimension always in the same
// direction as the line height?
len = node.properties.get("block-progression-dimension.optimum").getLength();
if (!len.isAuto()) {
bpd = len.getValue();
} else {
len = node.properties.get("height").getLength();
if (!len.isAuto()) {
bpd = len.getValue();
}
}
}
len = node.properties.get("inline-progression-dimension.optimum").getLength();
if (!len.isAuto()) {
ipd = len.getValue();
} else {
len = node.properties.get("width").getLength();
if (!len.isAuto()) {
ipd = len.getValue();
}
}
// if auto then use the intrinsic size of the content scaled
// to the content-height and content-width
int cwidth = -1;
int cheight = -1;
len = node.properties.get("content-width").getLength();
if (!len.isAuto()) {
/*if(len.scaleToFit()) {
if(ipd != -1) {
cwidth = ipd;
}
} else {*/
cwidth = len.getValue();
}
len = node.properties.get("content-height").getLength();
if (!len.isAuto()) {
/*if(len.scaleToFit()) {
if(bpd != -1) {
cwidth = bpd;
}
} else {*/
cheight = len.getValue();
}
Point2D csize = new Point2D.Float(cwidth == -1 ? -1 : cwidth / 1000f,
cheight == -1 ? -1 : cheight / 1000f);
Point2D size = child.getDimension(csize);
if (size == null) {
// error
return null;
}
if (cwidth == -1) {
cwidth = (int)size.getX() * 1000;
}
if (cheight == -1) {
cheight = (int)size.getY() * 1000;
}
int scaling = node.properties.get("scaling").getEnum();
if (scaling == Scaling.UNIFORM) {
// adjust the larger
double rat1 = cwidth / (size.getX() * 1000f);
double rat2 = cheight / (size.getY() * 1000f);
if (rat1 < rat2) {
// reduce cheight
cheight = (int)(rat1 * size.getY() * 1000);
} else {
cwidth = (int)(rat2 * size.getX() * 1000);
}
}
if (ipd == -1) {
ipd = cwidth;
}
if (bpd == -1) {
bpd = cheight;
}
boolean clip = false;
if (cwidth > ipd || cheight > bpd) {
int overflow = node.properties.get("overflow").getEnum();
if (overflow == Overflow.HIDDEN) {
clip = true;
} else if (overflow == Overflow.ERROR_IF_OVERFLOW) {
node.getLogger().error("Instream foreign object overflows the viewport: clipping");
clip = true;
}
}
int xoffset = node.computeXOffset(ipd, cwidth);
int yoffset = node.computeYOffset(bpd, cheight);
Rectangle2D placement = new Rectangle2D.Float(xoffset, yoffset, cwidth, cheight);
org.w3c.dom.Document doc = child.getDOMDocument();
String ns = child.getDocumentNamespace();
node.children = null;
ForeignObject foreign = new ForeignObject(doc, ns);
Viewport areaCurrent = new Viewport(foreign);
areaCurrent.setWidth(ipd);
areaCurrent.setHeight(bpd);
areaCurrent.setContentPosition(placement);
areaCurrent.setClip(clip);
areaCurrent.setOffset(0);
return areaCurrent;
}
public void serveVisitor(ListItem node) {
if (node.getLabel() != null && node.getBody() != null) {
ListItemLayoutManager blm = new ListItemLayoutManager();
blm.setUserAgent(node.getUserAgent());
blm.setFObj(node);
blm.setLabel(getListItemLabelLayoutManager(node.getLabel()));
blm.setBody(getListItemBodyLayoutManager(node.getBody()));
currentLMList.add(blm);
} else {
node.getLogger().error("list-item requires list-item-label and list-item-body");
}
}
/**
* @return this object's Item layout manager
*/
public Item getListItemLabelLayoutManager(ListItemLabel node) {
Item itemLabel = new Item();
itemLabel.setUserAgent(node.getUserAgent());
itemLabel.setFObj(node);
return itemLabel;
}
/**
* @return Item layout manager
*/
public Item getListItemBodyLayoutManager(ListItemBody node) {
Item item = new Item();
item.setUserAgent(node.getUserAgent());
item.setFObj(node);
return item;
}
/**
* Overridden from FObj
* @param lms the list to which the layout manager(s) should be added
*/
public void serveVisitor(final PageNumber node) {
node.setup();
LayoutManager lm;
lm = new LeafNodeLayoutManager() {
public InlineArea get(LayoutContext context) {
// get page string from parent, build area
Word inline = new Word();
String str = parentLM.getCurrentPageNumber();
int width = 0;
for (int count = 0; count < str.length(); count++) {
width += CharUtilities.getCharWidth(
str.charAt(count), node.getFontState());
}
inline.setWord(str);
inline.setIPD(width);
inline.setHeight(node.getFontState().getAscender()
- node.getFontState().getDescender());
inline.setOffset(node.getFontState().getAscender());
inline.addTrait(Trait.FONT_NAME,
node.getFontState().getFontName());
inline.addTrait(Trait.FONT_SIZE,
new Integer(node.getFontState().getFontSize()));
return inline;
}
protected void offsetArea(LayoutContext context) {
curArea.setOffset(context.getBaseline());
}
};
lm.setUserAgent(node.getUserAgent());
lm.setFObj(node);
currentLMList.add(lm);
}
public void serveVisitor(final PageNumberCitation node) {
node.setup();
LayoutManager lm;
lm = new LeafNodeLayoutManager() {
public InlineArea get(LayoutContext context) {
curArea = getPageNumberCitationInlineArea(node, parentLM);
return curArea;
}
public void addAreas(PositionIterator posIter,
LayoutContext context) {
super.addAreas(posIter, context);
if (node.getUnresolved()) {
parentLM.addUnresolvedArea(node.getRefId(),
(Resolveable) curArea);
}
}
protected void offsetArea(LayoutContext context) {
curArea.setOffset(context.getBaseline());
}
};
lm.setUserAgent(node.getUserAgent());
lm.setFObj(node);
currentLMList.add(lm);
}
// if id can be resolved then simply return a word, otherwise
// return a resolveable area
public InlineArea getPageNumberCitationInlineArea(PageNumberCitation node,
LayoutProcessor parentLM) {
if (node.getRefId().equals("")) {
node.getLogger().error("page-number-citation must contain \"ref-id\"");
return null;
}
PageViewport page = parentLM.resolveRefID(node.getRefId());
InlineArea inline = null;
if (page != null) {
String str = page.getPageNumber();
// get page string from parent, build area
Word word = new Word();
inline = word;
int width = node.getStringWidth(str);
word.setWord(str);
inline.setIPD(width);
inline.setHeight(node.getFontState().getAscender()
- node.getFontState().getDescender());
inline.setOffset(node.getFontState().getAscender());
inline.addTrait(Trait.FONT_NAME, node.getFontState().getFontName());
inline.addTrait(Trait.FONT_SIZE,
new Integer(node.getFontState().getFontSize()));
node.setUnresolved(false);
} else {
node.setUnresolved(true);
inline = new UnresolvedPageNumber(node.getRefId());
String str = "MMM"; // reserve three spaces for page number
int width = node.getStringWidth(str);
inline.setIPD(width);
inline.setHeight(node.getFontState().getAscender()
- node.getFontState().getDescender());
inline.setOffset(node.getFontState().getAscender());
inline.addTrait(Trait.FONT_NAME, node.getFontState().getFontName());
inline.addTrait(Trait.FONT_SIZE,
new Integer(node.getFontState().getFontSize()));
}
return inline;
}
public void serveVisitor(Table node) {
TableLayoutManager tlm = new TableLayoutManager();
tlm.setUserAgent(node.getUserAgent());
tlm.setFObj(node);
ArrayList columnLMs = new ArrayList();
ListIterator iter = node.getColumns().listIterator();
while (iter.hasNext()) {
columnLMs.add(getTableColumnLayoutManager((TableColumn)iter.next()));
}
tlm.setColumns(columnLMs);
if (node.getTableHeader() != null) {
tlm.setTableHeader(getTableBodyLayoutManager(node.getTableHeader()));
}
if (node.getTableFooter() != null) {
tlm.setTableFooter(getTableBodyLayoutManager(node.getTableFooter()));
}
currentLMList.add(tlm);
}
public LayoutManager getTableColumnLayoutManager(TableColumn node) {
node.doSetup();
Column clm = new Column();
clm.setUserAgent(node.getUserAgent());
clm.setFObj(node);
return clm;
}
public void serveVisitor(TableBody node) {
currentLMList.add(getTableBodyLayoutManager(node));
}
public Body getTableBodyLayoutManager(TableBody node) {
Body blm = new Body();
blm.setUserAgent(node.getUserAgent());
blm.setFObj(node);
return blm;
}
public void serveVisitor(TableCell node) {
Cell clm = new Cell();
clm.setUserAgent(node.getUserAgent());
clm.setFObj(node);
currentLMList.add(clm);
}
public void serveVisitor(TableRow node) {
Row rlm = new Row();
rlm.setUserAgent(node.getUserAgent());
rlm.setFObj(node);
currentLMList.add(rlm);
}
public void serveVisitor(Flow node) {
FlowLayoutManager lm = new FlowLayoutManager();
lm.setUserAgent(node.getUserAgent());
lm.setFObj(node);
currentLMList.add(lm);
}
}
|