1 /*
   2  * Copyright (c) 2012, 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
  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     @Override
  46     public boolean setContent(String text, Object font) {
  47         this.text = text;
  48         final StubFontLoader.StubFont stub = ((StubFontLoader.StubFont)font);
  49         this.font = stub == null ? null : stub.font;
  50         return true;
  51     }
  52 
  53     @Override
  54     public boolean setAlignment(int alignment) {
  55         return true;
  56     }
  57 
  58     @Override
  59     public boolean setWrapWidth(float wrapWidth) {
  60         return true;
  61     }
  62 
  63     @Override
  64     public boolean setLineSpacing(float spacing) {
  65         return true;
  66     }
  67 
  68     @Override
  69     public boolean setDirection(int direction) {
  70         return true;
  71     }
  72 
  73     @Override
  74     public boolean setBoundsType(int type) {
  75         return true;
  76     }
  77 
  78     @Override
  79     public BaseBounds getBounds() {
  80         return getBounds(null, new RectBounds());
  81     }
  82 
  83     @Override
  84     public BaseBounds getBounds(TextSpan filter, BaseBounds bounds) {
  85         final double fontSize = (font == null ? 0 : ((Font)font).getSize());
  86         final String[] lines = text.split("\n");
  87         double width = 0.0;
  88         double height = fontSize * lines.length;
  89         for (String line : lines) {
  90             width = Math.max(width, fontSize * line.length());
  91         }
  92         return bounds.deriveWithNewBounds(0, (float)-fontSize, 0,
  93                 (float)width, (float)(height-fontSize), 0);
  94     }
  95 
  96     class StubTextLine implements TextLine {
  97         @Override public GlyphList[] getRuns() {
  98             return new GlyphList[0];
  99         }
 100         @Override public RectBounds getBounds() {
 101             return new RectBounds();
 102         }
 103         @Override public float getLeftSideBearing() {
 104             return 0;
 105         }
 106         @Override public float getRightSideBearing() {
 107             return 0;
 108         }
 109         @Override public int getStart() {
 110             return 0;
 111         }
 112         @Override public int getLength() {
 113             return 0;
 114         }
 115     }
 116 
 117     @Override
 118     public TextLine[] getLines() {
 119         return new TextLine[] {new StubTextLine()};
 120     }
 121 
 122     @Override
 123     public GlyphList[] getRuns() {
 124         return new GlyphList[0];
 125     }
 126 
 127     @Override
 128     public Shape getShape(int type, TextSpan filter) {
 129         return new Path2D();
 130     }
 131 
 132     @Override
 133     public HitInfo getHitInfo(float x, float y) {
 134         // TODO this probably needs to be entirely rewritten...
 135         if (text == null) {
 136             final HitInfo hit = new HitInfo();
 137             hit.setCharIndex(0);
 138             hit.setLeading(true);
 139             return hit;
 140         }
 141 
 142         final double fontSize = (font == null ? 0 : ((Font)font).getSize());
 143         final String[] lines = text.split("\n");
 144         int lineIndex = Math.min(lines.length - 1, (int) (y / fontSize));
 145         if (lineIndex >= lines.length) {
 146             throw new IllegalStateException("Asked for hit info out of y range: x=" + x + "y=" +
 147                     + y + "text='" + text + "', lineIndex=" + lineIndex + ", numLines=" + lines.length +
 148                     ", fontSize=" + fontSize);
 149         }
 150         int offset = 0;
 151         for (int i=0; i<lineIndex; i++) {
 152             offset += lines[i].length() + 1; // add in the \n
 153         }
 154 
 155         int charPos = (int) (x / lines[lineIndex].length());
 156         if (charPos + offset > text.length()) {
 157             throw new IllegalStateException("Asked for hit info out of x range");
 158         }
 159 
 160         final HitInfo hit = new HitInfo();
 161         hit.setCharIndex(offset + charPos);
 162         return hit;
 163     }
 164 
 165     @Override
 166     public PathElement[] getCaretShape(int offset, boolean isLeading, float x,
 167             float y) {
 168         return new PathElement[0];
 169     }
 170 
 171     @Override
 172     public PathElement[] getRange(int start, int end, int type, float x, float y) {
 173         return new PathElement[0];
 174     }
 175 
 176     @Override
 177     public BaseBounds getVisualBounds(int type) {
 178         return new RectBounds();
 179     }
 180 
 181 }