blob: ce185095891b07baea1bb660af510dbc14e924b6 [file] [log] [blame]
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to you under the Apache License,
* Version 2.0 (the "License"); you may not use this file except in
* compliance with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.pivot.wtk;
import java.awt.Font;
import org.apache.pivot.json.JSONSerializer;
import org.apache.pivot.serialization.SerializationException;
import org.apache.pivot.util.Utils;
/**
* Utility class for dealing with fonts.
*/
public final class FontUtilities {
/**
* Private constructor for utility class.
*/
private FontUtilities() {
}
/**
* Interpret a string as a font specification.
*
* @param value Either a JSON dictionary {@link Theme#deriveFont describing
* a font relative to the current theme}, or one of the
* {@link Font#decode(String) standard Java font specifications}.
* @return The font corresponding to the specification.
* @throws IllegalArgumentException if the given string is <tt>null</tt>
* or empty or the font specification cannot be decoded.
*/
public static Font decodeFont(final String value) {
Utils.checkNullOrEmpty(value, "font");
Font font;
if (value.startsWith("{")) {
try {
font = Theme.deriveFont(JSONSerializer.parseMap(value));
} catch (SerializationException exception) {
throw new IllegalArgumentException(exception);
}
} else {
font = Font.decode(value);
}
return font;
}
}