001 /*
002 * Copyright 2002 - 2008 JEuclid, http://jeuclid.sf.net
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017 /* $Id: LayoutContextImpl.java,v 896e3a9ebb36 2008/11/28 13:39:06 maxberger $ */
018
019 package net.sourceforge.jeuclid.context;
020
021 import java.awt.Color;
022 import java.io.Serializable;
023 import java.util.ArrayList;
024 import java.util.Collections;
025 import java.util.List;
026 import java.util.Map;
027 import java.util.TreeMap;
028
029 import net.sourceforge.jeuclid.Constants;
030 import net.sourceforge.jeuclid.LayoutContext;
031 import net.sourceforge.jeuclid.MutableLayoutContext;
032
033 /**
034 * @version $Revision: 896e3a9ebb36 $
035 */
036 public class LayoutContextImpl implements MutableLayoutContext, Serializable {
037
038 /**
039 *
040 */
041 private static final long serialVersionUID = 1L;
042
043 private static final class SingletonHolder {
044 private static final LayoutContextImpl INSTANCE = new LayoutContextImpl();
045
046 private SingletonHolder() {
047 }
048 }
049
050 private final Map<Parameter, Object> context;
051
052 // CHECKSTYLE:OFF
053 /**
054 * Default constructor.
055 */
056 protected LayoutContextImpl() {
057 this.context = new TreeMap<Parameter, Object>();
058 this.context.put(Parameter.MATHSIZE, Constants.DEFAULT_FONTSIZE);
059 this.context.put(Parameter.SCRIPTMINSIZE, 8f);
060 this.context.put(Parameter.ANTIALIAS_MINSIZE, 10f);
061 this.context.put(Parameter.SCRIPTSIZEMULTIPLIER,
062 Constants.DEFAULT_SCIPTSIZEMULTIPLIER);
063 this.context.put(Parameter.SCRIPTLEVEL, 0);
064 this.context.put(Parameter.DISPLAY, Display.BLOCK);
065 this.context.put(Parameter.DEBUG, false);
066 this.context.put(Parameter.ANTIALIAS, true);
067 this.context.put(Parameter.MATHCOLOR, Color.BLACK);
068 this.context.put(Parameter.MATHBACKGROUND, null);
069
070 final List<String> fontsSanserif = new ArrayList<String>(12);
071 fontsSanserif.add("Verdana");
072 fontsSanserif.add("Helvetica");
073 fontsSanserif.add("Arial");
074 fontsSanserif.add("Arial Unicode MS");
075 fontsSanserif.add("Lucida Sans Unicode");
076 fontsSanserif.add("Lucida Sans");
077 fontsSanserif.add("Lucida Grande");
078 fontsSanserif.add("DejaVu Sans");
079 fontsSanserif.add("DejaVuSans");
080 fontsSanserif.add("Bitstream Vera Sans");
081 fontsSanserif.add("Luxi Sans");
082 fontsSanserif.add("FreeSans");
083 fontsSanserif.add("sansserif");
084 this.context.put(Parameter.FONTS_SANSSERIF, Collections
085 .unmodifiableList(fontsSanserif));
086
087 final List<String> fontsSerif = new ArrayList<String>(10);
088 fontsSerif.add("Constantina");
089 fontsSerif.add("Cambria");
090 fontsSerif.add("Times");
091 fontsSerif.add("Times New Roman");
092 fontsSerif.add("Lucida Bright");
093 fontsSerif.add("DejaVu Serif");
094 fontsSerif.add("DejaVuSerif");
095 fontsSerif.add("Bitstream Vera Serif");
096 fontsSerif.add("Luxi Serif");
097 fontsSerif.add("FreeSerif");
098 fontsSerif.add("serif");
099 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 }