/************************************************************** * * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file * distributed with this work for additional information * regarding copyright ownership. The ASF licenses this file * to you under the Apache License, Version 2.0 (the * "License"); you may not use this file except in compliance * with the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, * software distributed under the License is distributed on an * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * KIND, either express or implied. See the License for the * specific language governing permissions and limitations * under the License. * *************************************************************/ package org.apache.openoffice.ooxml.viewer.tokenview; import java.util.Vector; public class LineContainer { LineContainer () { maLines = new Vector<>(); } boolean IsLineInGroup ( final Line aLine, final Run aGroup) { if (aGroup == null) return false; final Line aStartLine = aGroup.GetLine(); if (aLine.GetTop() < aStartLine.GetTop()) return false; final Line aEndLine = aGroup.GetGroupEnd().GetLine(); if (aLine.GetTop() > aEndLine.GetTop()) return false; return true; } Line GetLineForY (final int nY) { int nMin = 0; int nMax = maLines.size()-1; if (nMin <= nMax) { while (nMin < nMax-1) { final int nMed = (nMax+nMin)/2; if (nY < maLines.get(nMed).GetTop()) nMax = nMed; else nMin = nMed; } for (int nIndex=nMin; nIndex<=nMax; ++nIndex) if (maLines.get(nIndex).Contains(nY)) return maLines.get(nIndex); } return null; } public Line GetLineForOffset (final int nOffset) { final int nLineIndex = GetLineIndexForOffset(nOffset, 0); if (nLineIndex < 0) return null; else return maLines.get(nLineIndex); } public Iterable> GetLinesForOffsets ( final int nStartOffset, final int nEndOffset) { final Vector> aLines = new Vector<>(); final int nStartLineIndex = GetLineIndexForOffset(nStartOffset, -1); final int nEndLineIndex = GetLineIndexForOffset(nEndOffset, +1); if (nStartLineIndex >= 0) { if (nEndLineIndex < 0) aLines.add(maLines.get(nStartLineIndex)); else for (int nIndex=nStartLineIndex; nIndex<=nEndLineIndex; ++nIndex) aLines.add(maLines.get(nIndex)); } return aLines; } public int GetLineCount() { return maLines.size(); } public Line GetLine (final int nIndex) { return maLines.get(nIndex); } public Iterable> GetLines () { return maLines; } public void AddLine (final Line aLine) { maLines.add(aLine); } /** Return the index of the line that contains the given offset. * When there is no line that contains the line that look at the bias * to return the previous or next line. */ private int GetLineIndexForOffset ( final int nOffset, final int nBias) { int nMinIndex = 0; int nMaxIndex = maLines.size()-1; while (nMinIndex < nMaxIndex-1) { final int nMedIndex = (nMinIndex + nMaxIndex) / 2; if (maLines.get(nMedIndex).GetEndOffset() <= nOffset) nMinIndex = nMedIndex; else nMaxIndex = nMedIndex; } for (int nIndex=nMinIndex; nIndex<=nMaxIndex; ++nIndex) { if (maLines.get(nIndex).ContainsOffset(nOffset)) return nIndex; } if (nBias < 0) { for (int nIndex=nMinIndex; nIndex<=nMaxIndex; ++nIndex) { if (maLines.get(nIndex).GetStartOffset() > nOffset) return nIndex; } } else if (nBias > 0) { for (int nIndex=nMaxIndex; nIndex>=nMinIndex; ++nIndex) { if (maLines.get(nIndex).GetEndOffset() < nOffset) return nIndex; } } return -1; } private final Vector> maLines; }