modules/graphics/src/main/java/com/sun/javafx/css/FontFaceImpl.java

Print this page
rev 9240 : 8076423: JEP 253: Prepare JavaFX UI Controls & CSS APIs for Modularization
   1 /*
   2  * Copyright (c) 2011, 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 com.sun.javafx.css;
  27 



  28 import java.io.DataInputStream;
  29 import java.io.DataOutputStream;
  30 import java.io.IOException;
  31 import java.util.ArrayList;
  32 import java.util.HashMap;
  33 import java.util.List;
  34 import java.util.Map;
  35 import java.util.Set;
  36 
  37 /**
  38  * A FontFace is a @font-face definition from CSS file
  39  */
  40 final public class FontFace {
  41     public static enum FontFaceSrcType {URL,LOCAL, REFERENCE}
  42 
  43     private final Map<String,String> descriptors;
  44     private final List<FontFaceSrc> sources;
  45 
  46     public FontFace(Map<String, String> descriptors, List<FontFaceSrc> sources) {
  47         this.descriptors = descriptors;
  48         this.sources = sources;
  49     }
  50 
  51     public Map<String, String> getDescriptors() {
  52         return descriptors;
  53     }
  54 
  55     public List<FontFaceSrc> getSources() {
  56         return sources;
  57     }
  58 
  59     @Override public String toString() {
  60         StringBuilder sb = new StringBuilder("@font-face { ");
  61         for(Map.Entry<String,String> desc: descriptors.entrySet()) {
  62             sb.append(desc.getKey());
  63             sb.append(" : ");
  64             sb.append(desc.getValue());
  65             sb.append("; ");
  66         }
  67         sb.append("src : ");
  68         for(FontFaceSrc src: sources) {
  69             sb.append(src.getType());
  70             sb.append(" \"");
  71             sb.append(src.getSrc());
  72             sb.append("\", ");
  73         }
  74         sb.append("; ");
  75         sb.append(" }");
  76         return sb.toString();
  77     }
  78 
  79     final void writeBinary(final DataOutputStream os, final StringStore stringStore) throws IOException
  80     {
  81         Set<Map.Entry<String,String>> entrySet = getDescriptors() != null ? getDescriptors().entrySet() : null;
  82         int nEntries = entrySet != null ? entrySet.size() : 0;
  83         os.writeShort(nEntries);
  84         if (entrySet != null) {
  85             for(Map.Entry<String,String> entry : entrySet) {
  86                 int index = stringStore.addString(entry.getKey());
  87                 os.writeInt(index);
  88                 index = stringStore.addString(entry.getValue());
  89                 os.writeInt(index);
  90             }
  91         }
  92 
  93         List<FontFaceSrc> fontFaceSrcs = getSources();
  94         nEntries = fontFaceSrcs != null ? fontFaceSrcs.size() : 0;
  95         os.writeShort(nEntries);
  96         for (int n=0; n<nEntries; n++) {
  97             FontFaceSrc fontFaceSrc = fontFaceSrcs.get(n);
  98             fontFaceSrc.writeBinary(os, stringStore);
  99         }
 100 
 101     }
 102 
 103     final static FontFace readBinary(int bssVersion, DataInputStream is, String[] strings) throws IOException
 104     {
 105         int nEntries = is.readShort();
 106         Map<String,String> descriptors = new HashMap(nEntries);
 107         for (int n=0; n<nEntries; n++) {
 108             int index = is.readInt();
 109             String key = strings[index];
 110             index = is.readInt();
 111             String value = strings[index];
 112             descriptors.put(key, value);
 113         }
 114 
 115         nEntries = is.readShort();
 116         List<FontFaceSrc> fontFaceSrcs = new ArrayList<>(nEntries);
 117         for (int n=0; n<nEntries; n++) {
 118             FontFaceSrc fontFaceSrc = FontFaceSrc.readBinary(bssVersion, is, strings);
 119             fontFaceSrcs.add(fontFaceSrc);
 120         }
 121 
 122         return new FontFace(descriptors, fontFaceSrcs);
 123 
 124     }
 125 
 126     public static class FontFaceSrc {
 127         private final FontFaceSrcType type;
 128         private final String src;
 129         private final String format;
 130 
 131         public FontFaceSrc(FontFaceSrcType type, String src, String format) {
 132             this.type = type;
 133             this.src = src;
 134             this.format = format;
 135         }
 136 
 137         public FontFaceSrc(FontFaceSrcType type, String src) {
 138             this.type = type;
 139             this.src = src;
 140             this.format = null;
 141         }
 142 
 143         public FontFaceSrcType getType() {


   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() {