1 /*
2 * Copyright (c) 1999, 2006, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
201 * broken view would occupy >= 0; this may be useful for
202 * things like tab calculations
203 * @param len specifies the distance into the view
204 * where a potential break is desired >= 0
205 * @return the model location desired for a break
206 * @see View#breakView
207 */
208 public int getBoundedPosition(GlyphView v, int p0, float x, float len) {
209 sync(v);
210 TabExpander expander = v.getTabExpander();
211 Segment s = v.getText(p0, v.getEndOffset());
212 int[] justificationData = getJustificationData(v);
213 int index = Utilities.getTabbedTextOffset(v, s, metrics, (int)x, (int)(x+len),
214 expander, p0, false,
215 justificationData);
216 SegmentCache.releaseSharedSegment(s);
217 int p1 = p0 + index;
218 return p1;
219 }
220
221 void sync(GlyphView v) {
222 Font f = v.getFont();
223 if ((metrics == null) || (! f.equals(metrics.getFont()))) {
224 // fetch a new FontMetrics
225 Container c = v.getContainer();
226 metrics = (c != null) ? c.getFontMetrics(f) :
227 Toolkit.getDefaultToolkit().getFontMetrics(f);
228 }
229 }
230
231
232
233 /**
234 * @return justificationData from the ParagraphRow this GlyphView
235 * is in or {@code null} if no justification is needed
236 */
237 private int[] getJustificationData(GlyphView v) {
238 View parent = v.getParent();
239 int [] ret = null;
240 if (parent instanceof ParagraphView.Row) {
|
1 /*
2 * Copyright (c) 1999, 2014, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
201 * broken view would occupy >= 0; this may be useful for
202 * things like tab calculations
203 * @param len specifies the distance into the view
204 * where a potential break is desired >= 0
205 * @return the model location desired for a break
206 * @see View#breakView
207 */
208 public int getBoundedPosition(GlyphView v, int p0, float x, float len) {
209 sync(v);
210 TabExpander expander = v.getTabExpander();
211 Segment s = v.getText(p0, v.getEndOffset());
212 int[] justificationData = getJustificationData(v);
213 int index = Utilities.getTabbedTextOffset(v, s, metrics, (int)x, (int)(x+len),
214 expander, p0, false,
215 justificationData);
216 SegmentCache.releaseSharedSegment(s);
217 int p1 = p0 + index;
218 return p1;
219 }
220
221 @SuppressWarnings("deprecation")
222 void sync(GlyphView v) {
223 Font f = v.getFont();
224 if ((metrics == null) || (! f.equals(metrics.getFont()))) {
225 // fetch a new FontMetrics
226 Container c = v.getContainer();
227 metrics = (c != null) ? c.getFontMetrics(f) :
228 Toolkit.getDefaultToolkit().getFontMetrics(f);
229 }
230 }
231
232
233
234 /**
235 * @return justificationData from the ParagraphRow this GlyphView
236 * is in or {@code null} if no justification is needed
237 */
238 private int[] getJustificationData(GlyphView v) {
239 View parent = v.getParent();
240 int [] ret = null;
241 if (parent instanceof ParagraphView.Row) {
|