View Javadoc

1   /*
2    * Copyright 2002 - 2008 JEuclid, http://jeuclid.sf.net
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  /* $Id: LayoutContextImpl.java,v 896e3a9ebb36 2008/11/28 13:39:06 maxberger $ */
18  
19  package net.sourceforge.jeuclid.context;
20  
21  import java.awt.Color;
22  import java.io.Serializable;
23  import java.util.ArrayList;
24  import java.util.Collections;
25  import java.util.List;
26  import java.util.Map;
27  import java.util.TreeMap;
28  
29  import net.sourceforge.jeuclid.Constants;
30  import net.sourceforge.jeuclid.LayoutContext;
31  import net.sourceforge.jeuclid.MutableLayoutContext;
32  
33  /**
34   * @version $Revision: 896e3a9ebb36 $
35   */
36  public class LayoutContextImpl implements MutableLayoutContext, Serializable {
37  
38      /**
39       * 
40       */
41      private static final long serialVersionUID = 1L;
42  
43      private static final class SingletonHolder {
44          private static final LayoutContextImpl INSTANCE = new LayoutContextImpl();
45  
46          private SingletonHolder() {
47          }
48      }
49  
50      private final Map<Parameter, Object> context;
51  
52      // CHECKSTYLE:OFF
53      /**
54       * Default constructor.
55       */
56      protected LayoutContextImpl() {
57          this.context = new TreeMap<Parameter, Object>();
58          this.context.put(Parameter.MATHSIZE, Constants.DEFAULT_FONTSIZE);
59          this.context.put(Parameter.SCRIPTMINSIZE, 8f);
60          this.context.put(Parameter.ANTIALIAS_MINSIZE, 10f);
61          this.context.put(Parameter.SCRIPTSIZEMULTIPLIER,
62                  Constants.DEFAULT_SCIPTSIZEMULTIPLIER);
63          this.context.put(Parameter.SCRIPTLEVEL, 0);
64          this.context.put(Parameter.DISPLAY, Display.BLOCK);
65          this.context.put(Parameter.DEBUG, false);
66          this.context.put(Parameter.ANTIALIAS, true);
67          this.context.put(Parameter.MATHCOLOR, Color.BLACK);
68          this.context.put(Parameter.MATHBACKGROUND, null);
69  
70          final List<String> fontsSanserif = new ArrayList<String>(12);
71          fontsSanserif.add("Verdana");
72          fontsSanserif.add("Helvetica");
73          fontsSanserif.add("Arial");
74          fontsSanserif.add("Arial Unicode MS");
75          fontsSanserif.add("Lucida Sans Unicode");
76          fontsSanserif.add("Lucida Sans");
77          fontsSanserif.add("Lucida Grande");
78          fontsSanserif.add("DejaVu Sans");
79          fontsSanserif.add("DejaVuSans");
80          fontsSanserif.add("Bitstream Vera Sans");
81          fontsSanserif.add("Luxi Sans");
82          fontsSanserif.add("FreeSans");
83          fontsSanserif.add("sansserif");
84          this.context.put(Parameter.FONTS_SANSSERIF, Collections
85                  .unmodifiableList(fontsSanserif));
86  
87          final List<String> fontsSerif = new ArrayList<String>(10);
88          fontsSerif.add("Constantina");
89          fontsSerif.add("Cambria");
90          fontsSerif.add("Times");
91          fontsSerif.add("Times New Roman");
92          fontsSerif.add("Lucida Bright");
93          fontsSerif.add("DejaVu Serif");
94          fontsSerif.add("DejaVuSerif");
95          fontsSerif.add("Bitstream Vera Serif");
96          fontsSerif.add("Luxi Serif");
97          fontsSerif.add("FreeSerif");
98          fontsSerif.add("serif");
99          this.context.put(Parameter.FONTS_SERIF, Collections
100                 .unmodifiableList(fontsSerif));
101 
102         final List<String> fontsMonospaced = new ArrayList<String>(10);
103         fontsMonospaced.add("Andale Mono");
104         fontsMonospaced.add("Courier");
105         fontsMonospaced.add("Courier Mono");
106         fontsMonospaced.add("Courier New");
107         fontsMonospaced.add("Lucida Sans Typewriter");
108         fontsMonospaced.add("DejaVu Sans Mono");
109         fontsMonospaced.add("DejaVuSansMono");
110         fontsMonospaced.add("Bitstream Vera Sans Mono");
111         fontsMonospaced.add("Luxi Mono");
112         fontsMonospaced.add("FreeMono");
113         fontsMonospaced.add("monospaced");
114         this.context.put(Parameter.FONTS_MONOSPACED, Collections
115                 .unmodifiableList(fontsMonospaced));
116 
117         final List<String> fontsScript = new ArrayList<String>(12);
118         fontsScript.add("EUSM10");
119         fontsScript.add("cmsy10");
120         fontsScript.add("Math5");
121         fontsScript.add("Mathematica5");
122         fontsScript.add("Savoye LET");
123         fontsScript.add("Brush Script MT");
124         fontsScript.add("Zapfino");
125         fontsScript.add("Apple Chancery");
126         fontsScript.add("Edwardian Script ITC");
127         fontsScript.add("Lucida Handwriting");
128         fontsScript.add("Monotype Corsiva");
129         fontsScript.add("Santa Fe LET");
130         this.context.put(Parameter.FONTS_SCRIPT, Collections
131                 .unmodifiableList(fontsScript));
132 
133         final List<String> fontsFraktur = new ArrayList<String>(7);
134         fontsFraktur.add("EUFM10");
135         fontsFraktur.add("Mathematica6");
136         fontsFraktur.add("FetteFraktur");
137         fontsFraktur.add("Fette Fraktur");
138         fontsFraktur.add("Euclid Fraktur");
139         fontsFraktur.add("Lucida Blackletter");
140         fontsFraktur.add("Blackmoor LET");
141         this.context.put(Parameter.FONTS_FRAKTUR, Collections
142                 .unmodifiableList(fontsFraktur));
143 
144         final List<String> fontsDoublestruck = new ArrayList<String>(8);
145         fontsDoublestruck.add("MSBM10");
146         fontsDoublestruck.add("Mathematica7");
147         fontsDoublestruck.add("Caslon Open Face");
148         fontsDoublestruck.add("Caslon Openface");
149         fontsDoublestruck.add("Cloister Open Face");
150         fontsDoublestruck.add("Academy Engraved LET");
151         fontsDoublestruck.add("Colonna MT");
152         fontsDoublestruck.add("Imprint MT Shadow");
153         this.context.put(Parameter.FONTS_DOUBLESTRUCK, Collections
154                 .unmodifiableList(fontsDoublestruck));
155 
156         this.context.put(Parameter.MFRAC_KEEP_SCRIPTLEVEL, Boolean.FALSE);
157         // CHECKSTYLE:ON
158     }
159 
160     /**
161      * Default Constructor.
162      * 
163      * @param copyFromContext
164      *            LayoutContext to use for initialization of values
165      */
166     public LayoutContextImpl(final LayoutContext copyFromContext) {
167 
168         if (copyFromContext instanceof LayoutContextImpl) {
169             this.context = new TreeMap<Parameter, Object>(
170                     ((LayoutContextImpl) copyFromContext).getParameters());
171         } else {
172             throw new UnsupportedOperationException("LayoutContextImpl("
173                     + copyFromContext.getClass() + ") not supported.");
174         }
175     }
176 
177     /**
178      * Retrieve the default layout context.
179      * 
180      * @return the default layout context.
181      */
182     public static LayoutContext getDefaultLayoutContext() {
183         return LayoutContextImpl.SingletonHolder.INSTANCE;
184     }
185 
186     /** {@inheritDoc} */
187     public LayoutContext setParameter(final Parameter which,
188             final Object newValue) {
189         if (which.valid(newValue)) {
190             this.context.put(which, newValue);
191         } else {
192             this.context.put(which, which.fromString(newValue.toString()));
193         }
194         return this;
195     }
196 
197     /** {@inheritDoc} */
198     public Object getParameter(final Parameter which) {
199         return this.context.get(which);
200     }
201 
202     /**
203      * Retrieve all parameters set for this LayoutContext. Please note: The
204      * returned map should be treated as read-only.
205      * 
206      * @return all Parameters in this context.
207      */
208     private Map<Parameter, Object> getParameters() {
209         return Collections.unmodifiableMap(this.context);
210     }
211 }