1 /* 2 * Copyright (c) 2011, 2016, 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 com.sun.javafx.webkit.prism; 27 28 import com.sun.javafx.font.CharToGlyphMapper; 29 import com.sun.javafx.font.FontFactory; 30 import com.sun.javafx.font.FontResource; 31 import com.sun.javafx.font.FontStrike; 32 import com.sun.javafx.font.PGFont; 33 import com.sun.javafx.geom.BaseBounds; 34 import com.sun.javafx.geom.transform.BaseTransform; 35 import com.sun.javafx.logging.PlatformLogger; 36 import com.sun.javafx.logging.PlatformLogger.Level; 37 import com.sun.javafx.scene.text.GlyphList; 38 import com.sun.javafx.scene.text.TextLayout; 39 import com.sun.javafx.text.TextRun; 40 import static com.sun.javafx.webkit.prism.TextUtilities.getLayoutWidth; 41 import static com.sun.javafx.webkit.prism.TextUtilities.getLayoutBounds; 42 import com.sun.prism.GraphicsPipeline; 43 import com.sun.webkit.graphics.WCFont; 44 import com.sun.webkit.graphics.WCGlyphBuffer; 45 import java.util.HashMap; 46 47 final class WCFontImpl extends WCFont { 48 private final static PlatformLogger log = 49 PlatformLogger.getLogger(WCFontImpl.class.getName()); 50 51 private static final HashMap<String, String> FONT_MAP = new HashMap<String, String>(); 52 53 static WCFont getFont(String name, boolean bold, boolean italic, float size) { 54 FontFactory factory = GraphicsPipeline.getPipeline().getFontFactory(); 55 synchronized (FONT_MAP) { 56 if (FONT_MAP.isEmpty()) { 57 FONT_MAP.put("serif", "Serif"); 58 FONT_MAP.put("dialog", "SansSerif"); 59 FONT_MAP.put("helvetica", "SansSerif"); 60 FONT_MAP.put("sansserif", "SansSerif"); 61 FONT_MAP.put("sans-serif", "SansSerif"); 62 FONT_MAP.put("monospace", "Monospaced"); 63 FONT_MAP.put("monospaced", "Monospaced"); 64 for (String family : factory.getFontFamilyNames()) { 65 FONT_MAP.put(family.toLowerCase(), family); 66 } 67 } 68 } 69 String family = FONT_MAP.get(name.toLowerCase()); 70 if (log.isLoggable(Level.FINE)) { 71 StringBuilder sb = new StringBuilder("WCFontImpl.get("); 72 sb.append(name).append(", ").append(size); 73 if (bold) { 74 sb.append(", bold"); 75 } 76 if (italic) { 77 sb.append(", italic"); 78 } 79 log.fine(sb.append(") = ").append(family).toString()); 80 } 81 return (family != null) 82 ? new WCFontImpl(factory.createFont(family, bold, italic, size)) 83 : null; 84 } 85 86 private final PGFont font; 87 88 WCFontImpl(PGFont font) { 89 this.font = font; 90 } 91 92 @Override public WCFont deriveFont(float size) { 93 FontFactory factory = GraphicsPipeline.getPipeline().getFontFactory(); 94 return new WCFontImpl( 95 factory.deriveFont(font, 96 font.getFontResource().isBold(), 97 font.getFontResource().isItalic(), 98 size)); 99 } 100 101 @Override public int getOffsetForPosition(String str, float x) { 102 TextLayout layout = TextUtilities.createLayout(str, font); 103 GlyphList[] runs = layout.getRuns(); 104 TextRun run = (TextRun) runs[0]; 105 int offset = run.getOffsetAtX(x, null); 106 if (log.isLoggable(Level.FINE)) { 107 log.fine(String.format("str='%s' (length=%d), x=%.2f => %d", 108 str, str.length(), x, offset)); 109 } 110 return offset; 111 } 112 113 @Override public WCGlyphBuffer getGlyphsAndAdvances(String str, int from, int to, boolean rtl) { 114 if (log.isLoggable(Level.FINE)) { 115 log.fine(String.format( 116 "str='%s' (length=%d), from=%d, to=%d, rtl=%b", 117 str, str.length(), from, to, rtl)); 118 } 119 TextLayout layout = TextUtilities.createLayout( 120 str.substring(from, to), getPlatformFont()); 121 int count = 0; 122 GlyphList[] runs = layout.getRuns(); 123 for (GlyphList run: runs) { 124 count += run.getGlyphCount(); 125 } 126 127 int[] glyphs = new int[count]; 128 float[] adv = new float[count]; 129 count = 0; 130 for (GlyphList run: layout.getRuns()) { 131 int gc = run.getGlyphCount(); 132 for (int i = 0; i < gc; i++) { 133 glyphs[count] = run.getGlyphCode(i); 134 adv[count] = run.getPosX(i + 1) - run.getPosX(i); 135 count++; 136 } 137 } 138 139 // inital advance (see RT-29908) 140 float x = 0; 141 if (rtl) { 142 x += (TextUtilities.getLayoutWidth(str.substring(from), getPlatformFont()) - 143 layout.getBounds().getWidth()); 144 } else { 145 x += TextUtilities.getLayoutWidth(str.substring(0, from), getPlatformFont()); 146 } 147 return new WCGlyphBuffer(glyphs, adv, x); 148 } 149 150 private FontStrike strike; 151 private FontStrike getFontStrike() 152 { 153 if (strike == null) { 154 strike = font.getStrike(BaseTransform.IDENTITY_TRANSFORM, FontResource.AA_LCD); 155 } 156 return strike; 157 } 158 159 @Override public double getGlyphWidth(int glyph) { 160 return getFontStrike().getFontResource().getAdvance(glyph, font.getSize()); 161 } 162 163 @Override public float getXHeight() { 164 return getFontStrike().getMetrics().getXHeight(); 165 } 166 167 @Override public int[] getGlyphCodes(char[] chars) { 168 int[] glyphs = new int[chars.length]; 169 CharToGlyphMapper mapper = getFontStrike().getFontResource().getGlyphMapper(); 170 mapper.charsToGlyphs(chars.length, chars, glyphs); 171 return glyphs; 172 } 173 174 @Override public double getStringWidth(String str) { 175 double result = getLayoutWidth(str, font); 176 if (log.isLoggable(Level.FINE)) { 177 log.fine(String.format("str='%s' (length=%d) => %.1f", 178 str, str.length(), result)); 179 } 180 return result; 181 } 182 183 @Override public double[] getStringBounds(String str, int from, int to, boolean rtl) { 184 float beforeWidth = getLayoutWidth(str.substring(0, from), font); 185 BaseBounds bounds = getLayoutBounds(str.substring(0, to), font); 186 double[] result = new double[] { 187 beforeWidth, // see RTL case below 188 0, // not really used 189 bounds.getWidth() - beforeWidth, 190 bounds.getHeight(), // not really used 191 }; 192 if (rtl) { 193 float totalWidth = getLayoutWidth(str, font); 194 result[0] = totalWidth - bounds.getWidth(); 195 } 196 if (log.isLoggable(Level.FINE)) { 197 log.fine(String.format( 198 "str='%s' (length=%d) [%d, %d], rtl=%b => [%.1f, %.1f + %.1f x %.1f]", 199 str, str.length(), from, to, rtl, 200 result[0], result[1], result[2], result[3])); 201 } 202 return result; 203 } 204 205 public float getAscent() { 206 // REMIND: This method needs to require a render context. 207 float res = - getFontStrike().getMetrics().getAscent(); 208 if (log.isLoggable(Level.FINER)) { 209 log.finer("getAscent({0}, {1}) = {2}", 210 new Object[] {font.getName(), font.getSize(), 211 res}); 212 } 213 return res; 214 } 215 216 public float getDescent() { 217 // REMIND: This method needs to require a render context. 218 float res = getFontStrike().getMetrics().getDescent(); 219 if (log.isLoggable(Level.FINER)) { 220 log.finer("getDescent({0}, {1}) = {2}", 221 new Object[] {font.getName(), font.getSize(), 222 res}); 223 } 224 return res; 225 } 226 227 public float getLineSpacing() { 228 // REMIND: This method needs to require a render context. 229 float res = getFontStrike().getMetrics().getLineHeight(); 230 if (log.isLoggable(Level.FINER)) { 231 log.finer("getLineSpacing({0}, {1}) = {2}", 232 new Object[] {font.getName(), font.getSize(), 233 res}); 234 } 235 return res; 236 } 237 238 public float getLineGap() { 239 // REMIND: This method needs to require a render context. 240 float res = getFontStrike().getMetrics().getLineGap(); 241 if (log.isLoggable(Level.FINER)) { 242 log.finer("getLineGap({0}, {1}) = {2}", 243 new Object[] {font.getName(), font.getSize(), 244 res }); 245 } 246 return res; 247 } 248 249 public boolean hasUniformLineMetrics() { 250 return false; 251 } 252 253 public Object getPlatformFont() { 254 return font; 255 } 256 257 @Override public float getCapHeight() { 258 return getFontStrike().getMetrics().getCapHeight(); 259 } 260 }