< prev index next >

modules/javafx.graphics/src/test/java/test/com/sun/javafx/pgstub/StubTextLayout.java

Print this page
@@ -1,7 +1,7 @@
  /*
-  * Copyright (c) 2012, 2016, Oracle and/or its affiliates. All rights reserved.
+  * Copyright (c) 2012, 2019, Oracle and/or its affiliates. All rights reserved.
   * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   *
   * This code is free software; you can redistribute it and/or modify it
   * under the terms of the GNU General Public License version 2 only, as
   * published by the Free Software Foundation.  Oracle designates this

@@ -40,10 +40,12 @@
          return true;
      }
  
      private String text;
      private Font font;
+     private int tabSize = DEFAULT_TAB_SIZE;
+ 
      @Override
      public boolean setContent(String text, Object font) {
          this.text = text;
          final StubFontLoader.StubFont stub = ((StubFontLoader.StubFont)font);
          this.font = stub == null ? null : stub.font;

@@ -85,11 +87,30 @@
          final double fontSize = (font == null ? 0 : ((Font)font).getSize());
          final String[] lines = text.split("\n");
          double width = 0.0;
          double height = fontSize * lines.length;
          for (String line : lines) {
-             width = Math.max(width, fontSize * line.length());
+             int tabs = 0;
+             final int length;
+             if (line.contains("\t")) {
+                 // count chars but when encountering a tab round up to a tabSize boundary
+                 char [] chrs = line.toCharArray();
+                 int spaces = 0;
+                 for (int i = 0; i < chrs.length; i++) {
+                     if (chrs[i] == '\t') {
+                         if (tabSize != 0) {
+                             while ((++spaces % tabSize) != 0) {}
+                         }
+                     } else {
+                         spaces++;
+                     }
+                 }
+                 length = spaces;
+             } else {
+                 length = line.length();
+             }
+             width = Math.max(width, fontSize * length);
          }
          return bounds.deriveWithNewBounds(0, (float)-fontSize, 0,
                  (float)width, (float)(height-fontSize), 0);
      }
  

@@ -171,6 +192,17 @@
      @Override
      public BaseBounds getVisualBounds(int type) {
          return new RectBounds();
      }
  
+     @Override
+     public boolean setTabSize(int spaces) {
+         if (spaces < 1)
+             spaces = 1;
+         if (tabSize != spaces) {
+             tabSize = spaces;
+             return true;
+         }
+         return false;
+     }
+ 
  }
< prev index next >