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.Graphics2D; 25 import java.util.Iterator; 26 import java.util.Vector; 27 28 public class Line<TokenType> 29 30 implements Iterable<Run<TokenType>> 31 { Line()32 Line () 33 { 34 maRuns = new Vector<Run<TokenType>>(); 35 mnWidth = -1; 36 mnHeight = -1; 37 mnY = -1; 38 mnStartOffset = -1; 39 mnEndOffset = -1; 40 } 41 42 43 44 AddRun(final Run<TokenType> aRun)45 public void AddRun (final Run<TokenType> aRun) 46 { 47 maRuns.add(aRun); 48 mnWidth += aRun.GetWidth(); 49 if (aRun.GetHeight() > mnHeight) 50 mnHeight = aRun.GetHeight(); 51 aRun.SetLine(this); 52 53 if (aRun.GetStreamOffset() >= 0) 54 { 55 if (mnStartOffset < 0) 56 mnStartOffset = aRun.GetStreamOffset(); 57 58 if (mnEndOffset < aRun.GetStreamEndOffset()) 59 mnEndOffset = aRun.GetStreamEndOffset(); 60 } 61 } 62 63 64 65 Format( final Graphics2D aG2, final int nY)66 public void Format ( 67 final Graphics2D aG2, 68 final int nY) 69 { 70 mnY = nY; 71 72 mnWidth = 0; 73 mnHeight = 0; 74 for (final Run<TokenType> aRun : maRuns) 75 { 76 aRun.Format(aG2); 77 mnWidth += aRun.GetWidth(); 78 if (mnHeight < aRun.GetHeight()) 79 mnHeight = aRun.GetHeight(); 80 } 81 } 82 83 84 85 GetWidth()86 public int GetWidth () 87 { 88 return mnWidth; 89 } 90 91 92 93 GetHeight()94 public int GetHeight () 95 { 96 return mnHeight; 97 } 98 99 100 101 GetTop()102 public int GetTop () 103 { 104 return mnY; 105 } 106 107 108 109 GetBottom()110 public int GetBottom () 111 { 112 return mnY + mnHeight; 113 } 114 115 116 117 Overlaps( final double nTop, final double nBottom)118 public boolean Overlaps ( 119 final double nTop, 120 final double nBottom) 121 { 122 return mnY<=nBottom && mnY+mnHeight>nTop; 123 } 124 125 126 127 Contains(final int nY)128 public boolean Contains (final int nY) 129 { 130 return nY>=mnY && nY<mnY+mnHeight; 131 } 132 133 134 135 136 @Override iterator()137 public Iterator<Run<TokenType>> iterator() 138 { 139 return maRuns.iterator(); 140 } 141 142 143 144 GetRunForX(final int nX)145 public Run<TokenType> GetRunForX (final int nX) 146 { 147 int nRunX = 0; 148 for (final Run<TokenType> aRun : maRuns) 149 { 150 final int nRunWidth = aRun.GetWidth(); 151 final int nRight = nRunX + nRunWidth; 152 if (nX>=nRunX && nX<nRight) 153 return aRun; 154 nRunX = nRight; 155 } 156 return null; 157 } 158 159 160 161 GetRunForOffset(int nOffset)162 public Run<TokenType> GetRunForOffset (int nOffset) 163 { 164 for (int nIndex=0; nIndex<maRuns.size(); ++nIndex) 165 { 166 final Run<TokenType> aRun = maRuns.get(nIndex); 167 final int nRunOffset = aRun.GetStreamOffset(); 168 if (nRunOffset >= 0) 169 if (nRunOffset<=nOffset && nOffset<=aRun.GetStreamEndOffset()) 170 return aRun; 171 } 172 return null; 173 } 174 175 176 177 GetRunsForOffsets( final int nStartOffset, final int nEndOffset)178 public Iterable<Run<TokenType>> GetRunsForOffsets ( 179 final int nStartOffset, 180 final int nEndOffset) 181 { 182 final Vector<Run<TokenType>> aRuns = new Vector<>(); 183 184 for (final Run<TokenType> aRun : maRuns) 185 { 186 if (aRun.GetStreamOffset() >= nEndOffset) 187 break; 188 else if (aRun.GetStreamEndOffset()<nStartOffset) 189 continue; 190 else 191 aRuns.add(aRun); 192 } 193 194 return aRuns; 195 } 196 197 198 199 GetStartOffset()200 public int GetStartOffset() 201 { 202 return mnStartOffset; 203 } 204 205 206 207 GetEndOffset()208 public int GetEndOffset () 209 { 210 return mnEndOffset; 211 } 212 213 214 215 ContainsOffset(final int nOffset)216 public boolean ContainsOffset (final int nOffset) 217 { 218 return mnStartOffset<=nOffset && nOffset<mnEndOffset; 219 } 220 221 222 223 224 @Override toString()225 public String toString () 226 { 227 return String.format("line of %d runs: %s", maRuns.size(), maRuns.toString()); 228 } 229 230 231 232 233 private final Vector<Run<TokenType>> maRuns; 234 private int mnY; 235 private int mnWidth; 236 private int mnHeight; 237 private int mnStartOffset; 238 private int mnEndOffset; 239 } 240