1 /*
  2  * Copyright (c) 2012, 2019, 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
 23  * questions.
 24  */
 25 
 26 package test.com.sun.javafx.pgstub;
 27 
 28 import com.sun.javafx.geom.BaseBounds;
 29 import com.sun.javafx.geom.Path2D;
 30 import com.sun.javafx.geom.RectBounds;
 31 import com.sun.javafx.geom.Shape;
 32 import com.sun.javafx.scene.text.*;
 33 import javafx.scene.shape.PathElement;
 34 import javafx.scene.text.Font;
 35 
 36 public class StubTextLayout implements TextLayout {
 37 
 38     @Override
 39     public boolean setContent(TextSpan[] spans) {
 40         return true;
 41     }
 42 
 43     private String text;
 44     private Font font;
 45     private int tabSize = DEFAULT_TAB_SIZE;
 46 
 47     @Override
 48     public boolean setContent(String text, Object font) {
 49         this.text = text;
 50         final StubFontLoader.StubFont stub = ((StubFontLoader.StubFont)font);
 51         this.font = stub == null ? null : stub.font;
 52         return true;
 53     }
 54 
 55     @Override
 56     public boolean setAlignment(int alignment) {
 57         return true;
 58     }
 59 
 60     @Override
 61     public boolean setWrapWidth(float wrapWidth) {
 62         return true;
 63     }
 64 
 65     @Override
 66     public boolean setLineSpacing(float spacing) {
 67         return true;
 68     }
 69 
 70     @Override
 71     public boolean setDirection(int direction) {
 72         return true;
 73     }
 74 
 75     @Override
 76     public boolean setBoundsType(int type) {
 77         return true;
 78     }
 79 
 80     @Override
 81     public BaseBounds getBounds() {
 82         return getBounds(null, new RectBounds());
 83     }
 84 
 85     @Override
 86     public BaseBounds getBounds(TextSpan filter, BaseBounds bounds) {
 87         final double fontSize = (font == null ? 0 : ((Font)font).getSize());
 88         final String[] lines = text.split("\n");
 89         double width = 0.0;
 90         double height = fontSize * lines.length;
 91         for (String line : lines) {
 92             int tabs = 0;
 93             final int length;
 94             if (line.contains("\t")) {
 95                 // count chars but when encountering a tab round up to a tabSize boundary
 96                 char [] chrs = line.toCharArray();
 97                 int spaces = 0;
 98                 for (int i = 0; i < chrs.length; i++) {
 99                     if (chrs[i] == '\t') {
100                         if (tabSize != 0) {
101                             while ((++spaces % tabSize) != 0) {}
102                         }
103                     } else {
104                         spaces++;
105                     }
106                 }
107                 length = spaces;
108             } else {
109                 length = line.length();
110             }
111             width = Math.max(width, fontSize * length);
112         }
113         return bounds.deriveWithNewBounds(0, (float)-fontSize, 0,
114                 (float)width, (float)(height-fontSize), 0);
115     }
116 
117     class StubTextLine implements TextLine {
118         @Override public GlyphList[] getRuns() {
119             return new GlyphList[0];
120         }
121         @Override public RectBounds getBounds() {
122             return new RectBounds();
123         }
124         @Override public float getLeftSideBearing() {
125             return 0;
126         }
127         @Override public float getRightSideBearing() {
128             return 0;
129         }
130         @Override public int getStart() {
131             return 0;
132         }
133         @Override public int getLength() {
134             return 0;
135         }
136     }
137 
138     @Override
139     public TextLine[] getLines() {
140         return new TextLine[] {new StubTextLine()};
141     }
142 
143     @Override
144     public GlyphList[] getRuns() {
145         return new GlyphList[0];
146     }
147 
148     @Override
149     public Shape getShape(int type, TextSpan filter) {
150         return new Path2D();
151     }
152 
153     @Override
154     public Hit getHitInfo(float x, float y) {
155         // TODO this probably needs to be entirely rewritten...
156         if (text == null) {
157             return new Hit(0, -1, true);
158         }
159 
160         final double fontSize = (font == null ? 0 : ((Font)font).getSize());
161         final String[] lines = text.split("\n");
162         int lineIndex = Math.min(lines.length - 1, (int) (y / fontSize));
163         if (lineIndex >= lines.length) {
164             throw new IllegalStateException("Asked for hit info out of y range: x=" + x + "y=" +
165                     + y + "text='" + text + "', lineIndex=" + lineIndex + ", numLines=" + lines.length +
166                     ", fontSize=" + fontSize);
167         }
168         int offset = 0;
169         for (int i=0; i<lineIndex; i++) {
170             offset += lines[i].length() + 1; // add in the \n
171         }
172 
173         int charPos = (int) (x / lines[lineIndex].length());
174         if (charPos + offset > text.length()) {
175             throw new IllegalStateException("Asked for hit info out of x range");
176         }
177 
178         return new Hit(offset + charPos, -1, true);
179     }
180 
181     @Override
182     public PathElement[] getCaretShape(int offset, boolean isLeading, float x,
183             float y) {
184         return new PathElement[0];
185     }
186 
187     @Override
188     public PathElement[] getRange(int start, int end, int type, float x, float y) {
189         return new PathElement[0];
190     }
191 
192     @Override
193     public BaseBounds getVisualBounds(int type) {
194         return new RectBounds();
195     }
196 
197     @Override
198     public boolean setTabSize(int spaces) {
199         if (spaces < 1)
200             spaces = 1;
201         if (tabSize != spaces) {
202             tabSize = spaces;
203             return true;
204         }
205         return false;
206     }
207 
208 }