1 /**************************************************************
2 *
3 * Licensed to the Apache Software Foundation (ASF) under one
4 * or more contributor license agreements.  See the NOTICE file
5 * distributed with this work for additional information
6 * regarding copyright ownership.  The ASF licenses this file
7 * to you under the Apache License, Version 2.0 (the
8 * "License"); you may not use this file except in compliance
9 * with the License.  You may obtain a copy of the License at
10 *
11 *   http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing,
14 * software distributed under the License is distributed on an
15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16 * KIND, either express or implied.  See the License for the
17 * specific language governing permissions and limitations
18 * under the License.
19 *
20 *************************************************************/
21 
22 package org.apache.openoffice.ooxml.viewer.tokenview;
23 
24 import java.awt.Dimension;
25 import java.awt.Graphics2D;
26 import java.util.Vector;
27 
28 public class Formatter<TokenType>
29 {
Formatter()30     public Formatter ()
31     {
32         mnLastFormattedLine = -1;
33         mnWidth = 0;
34         mnHeight = 0;
35     }
36 
37 
38 
39 
FormatText( final Graphics2D aG2, final LineContainer<TokenType> aLines)40     public FormatState<TokenType> FormatText (
41         final Graphics2D aG2,
42         final LineContainer<TokenType> aLines)
43     {
44         FormatLines(aG2, aLines);
45 
46         final double nTop = aG2.getClipBounds().getMinY();
47         final double nBottom = aG2.getClipBounds().getMaxY();
48         final Vector<Line<TokenType>> aVisibleLines = new Vector<>();
49         for (final Line<TokenType> aLine : aLines.GetLines())
50         {
51             if (aLine.Overlaps(nTop, nBottom))
52             {
53                 // Line is (partially) visible.
54                 aVisibleLines.add(aLine);
55             }
56         }
57 
58         return new FormatState<TokenType>(
59             new Dimension(mnWidth,mnHeight),
60             aVisibleLines);
61     }
62 
63 
64 
65 
FormatLines( final Graphics2D aG2, final LineContainer<TokenType> aLines)66     private void FormatLines (
67         final Graphics2D aG2,
68         final LineContainer<TokenType> aLines)
69     {
70         for (int nIndex=mnLastFormattedLine+1,nCount=aLines.GetLineCount(); nIndex<nCount; ++nIndex)
71         {
72             final Line<TokenType> aLine = aLines.GetLine(nIndex);
73             final int nY;
74             if (nIndex > 0)
75                 nY = aLines.GetLine(nIndex-1).GetBottom();
76             else
77                 nY = 0;
78             aLine.Format(aG2, nY);
79             if (aLine.GetWidth() > mnWidth)
80                 mnWidth = aLine.GetWidth();
81             if (aLine.GetBottom() > mnHeight)
82                 mnHeight = aLine.GetBottom();
83         }
84         mnLastFormattedLine = aLines.GetLineCount()-1;
85     }
86 
87 
88 
89 
90     private int mnLastFormattedLine;
91     private int mnWidth;
92     private int mnHeight;
93 }
94