1 /* 2 * Copyright (c) 2011, 2015, 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.css; 27 28 import javafx.css.FontFace; 29 import javafx.css.StyleConverter.StringStore; 30 31 import java.io.DataInputStream; 32 import java.io.DataOutputStream; 33 import java.io.IOException; 34 import java.util.ArrayList; 35 import java.util.HashMap; 36 import java.util.List; 37 import java.util.Map; 38 import java.util.Set; 39 40 /** 41 * A FontFace is a @font-face definition from CSS file 42 */ 43 final public class FontFaceImpl extends FontFace { 44 public static enum FontFaceSrcType {URL,LOCAL, REFERENCE} 45 46 private final Map<String,String> descriptors; 47 private final List<FontFaceSrc> sources; 48 49 public FontFaceImpl(Map<String, String> descriptors, List<FontFaceSrc> sources) { 50 this.descriptors = descriptors; 51 this.sources = sources; 52 } 53 54 public Map<String, String> getDescriptors() { 55 return descriptors; 56 } 57 58 public List<FontFaceSrc> getSources() { 59 return sources; 60 } 61 62 @Override public String toString() { 63 StringBuilder sb = new StringBuilder("@font-face { "); 64 for(Map.Entry<String,String> desc: descriptors.entrySet()) { 65 sb.append(desc.getKey()); 66 sb.append(" : "); 67 sb.append(desc.getValue()); 68 sb.append("; "); 69 } 70 sb.append("src : "); 71 for(FontFaceSrc src: sources) { 72 sb.append(src.getType()); 73 sb.append(" \""); 74 sb.append(src.getSrc()); 75 sb.append("\", "); 76 } 77 sb.append("; "); 78 sb.append(" }"); 79 return sb.toString(); 80 } 81 82 public final void writeBinary(final DataOutputStream os, final StringStore stringStore) throws IOException 83 { 84 Set<Map.Entry<String,String>> entrySet = getDescriptors() != null ? getDescriptors().entrySet() : null; 85 int nEntries = entrySet != null ? entrySet.size() : 0; 86 os.writeShort(nEntries); 87 if (entrySet != null) { 88 for(Map.Entry<String,String> entry : entrySet) { 89 int index = stringStore.addString(entry.getKey()); 90 os.writeInt(index); 91 index = stringStore.addString(entry.getValue()); 92 os.writeInt(index); 93 } 94 } 95 96 List<FontFaceSrc> fontFaceSrcs = getSources(); 97 nEntries = fontFaceSrcs != null ? fontFaceSrcs.size() : 0; 98 os.writeShort(nEntries); 99 for (int n=0; n<nEntries; n++) { 100 FontFaceSrc fontFaceSrc = fontFaceSrcs.get(n); 101 fontFaceSrc.writeBinary(os, stringStore); 102 } 103 104 } 105 106 public final static FontFaceImpl readBinary(int bssVersion, DataInputStream is, String[] strings) throws IOException 107 { 108 int nEntries = is.readShort(); 109 Map<String,String> descriptors = new HashMap(nEntries); 110 for (int n=0; n<nEntries; n++) { 111 int index = is.readInt(); 112 String key = strings[index]; 113 index = is.readInt(); 114 String value = strings[index]; 115 descriptors.put(key, value); 116 } 117 118 nEntries = is.readShort(); 119 List<FontFaceSrc> fontFaceSrcs = new ArrayList<>(nEntries); 120 for (int n=0; n<nEntries; n++) { 121 FontFaceSrc fontFaceSrc = FontFaceSrc.readBinary(bssVersion, is, strings); 122 fontFaceSrcs.add(fontFaceSrc); 123 } 124 125 return new FontFaceImpl(descriptors, fontFaceSrcs); 126 } 127 128 public static class FontFaceSrc { 129 private final FontFaceSrcType type; 130 private final String src; 131 private final String format; 132 133 public FontFaceSrc(FontFaceSrcType type, String src, String format) { 134 this.type = type; 135 this.src = src; 136 this.format = format; 137 } 138 139 public FontFaceSrc(FontFaceSrcType type, String src) { 140 this.type = type; 141 this.src = src; 142 this.format = null; 143 } 144 145 public FontFaceSrcType getType() { 146 return type; 147 } 148 149 public String getSrc() { 150 return src; 151 } 152 153 public String getFormat() { 154 return format; 155 } 156 157 final void writeBinary(final DataOutputStream os, final StringStore stringStore) throws IOException 158 { 159 // ok if type, src or format are null since StringStore allows null 160 os.writeInt(stringStore.addString(type.name())); 161 os.writeInt(stringStore.addString(src)); 162 os.writeInt(stringStore.addString(format)); 163 } 164 165 final static FontFaceSrc readBinary(int bssVersion, DataInputStream is, String[] strings) throws IOException 166 { 167 int index = is.readInt(); 168 FontFaceSrcType type = (strings[index] != null) ? FontFaceSrcType.valueOf(strings[index]) : null; 169 170 index = is.readInt(); 171 String src = strings[index]; 172 173 index = is.readInt(); 174 String format = strings[index]; 175 176 return new FontFaceSrc(type, src, format); 177 178 } 179 } 180 }