Spec-Zone .ru
спецификации, руководства, описания, API
001/*
002 * Copyright (c) 2008, 2013, Oracle and/or its affiliates. All rights reserved.
003 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004 *
005 * This code is free software; you can redistribute it and/or modify it
006 * under the terms of the GNU General Public License version 2 only, as
007 * published by the Free Software Foundation.  Oracle designates this
008 * particular file as subject to the "Classpath" exception as provided
009 * by Oracle in the LICENSE file that accompanied this code.
010 *
011 * This code is distributed in the hope that it will be useful, but WITHOUT
012 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
014 * version 2 for more details (a copy is included in the LICENSE file that
015 * accompanied this code).
016 *
017 * You should have received a copy of the GNU General Public License version
018 * 2 along with this work; if not, write to the Free Software Foundation,
019 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020 *
021 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
022 * or visit www.oracle.com if you need additional information or have any
023 * questions.
024 */
025
026package javafx.scene.text;
027
028/**
029 * Specifies different font weights which can be used when searching for a
030 * font on the system.
031 * The names correspond to pre-defined weights in the OS/2 table of the
032 * <A HREF=http://www.microsoft.com/typography/otspec/os2.htm#wtc>
033 * OpenType font specification</A>.
034 * The CSS 3 specification references the same as a sequence of values.
035 */
036public enum FontWeight {
037
038    /** 
039     * represents Thin font weight (100).
040     */
041    THIN(100, "Thin"),
042
043    /** 
044     * represents 'Extra Light' font weight (200).
045     */
046    EXTRA_LIGHT(200, "Extra Light", "Ultra Light"),
047
048    /** 
049     * represents Light font weight (300).
050     */
051    LIGHT(300, "Light"),
052
053    /** 
054     * represents Normal font weight (400).
055     */
056    NORMAL(400, "Normal", "Regular"),
057
058    /** 
059     * represents Medium font weight (500).
060     */
061    MEDIUM(500, "Medium"),
062
063    /** 
064     * represents 'Demi Bold' font weight (600).
065     */
066    SEMI_BOLD(600, "Semi Bold", "Demi Bold"),
067
068    /** 
069     * represents Bold font weight (700).
070     */
071    BOLD(700, "Bold"),
072
073    /** 
074     * represents 'Extra Bold' font weight (800).
075     */
076    EXTRA_BOLD(800, "Extra Bold", "Ultra Bold"),
077
078    /** 
079     * represents Black font weight (900).
080     */
081    BLACK(900, "Black", "Heavy");
082
083    private int weight;
084    private final String[] names;
085    
086    private FontWeight(int weight, String... names) {
087        this.names = names;
088    }
089
090    /**
091     * Return the visual weight (degree of blackness or thickness)
092     * specified by this {@code FontWeight}.
093     * @return weight
094     */
095    public int getWeight() {
096        return weight;
097    }
098
099    /** 
100     * Returns {@code FontWeight} by its name.
101     *
102     * @param name name of the {@code FontWeight}
103     */
104    public static FontWeight findByName(String name) {
105        if (name == null) return null;
106        
107        for (FontWeight w : FontWeight.values()) {
108            for (String n : w.names) {
109                if (n.equalsIgnoreCase(name)) return w;
110            }
111        }
112        
113        return null;
114    }
115
116    /**
117     * Returns the closest @code FontWeight} for a weight
118     * value as defined by the CSS and OpenType specifications.
119     * Where the specified value  is equidistant between two
120     * {@code FontWeight} values, then the implementation may
121     * select either at its discretion.
122     * This lookup is without reference to a font, so this is
123     * purely a mapping to the set of {@code FontWeight} instances
124     * and does not mean that a font of that weight will be available.
125     * @return closest {@code FontWeight}
126     */
127    public static FontWeight findByWeight(int weight) {
128        if (weight <= 150) {
129            return THIN;
130        } else if (weight <= 250) {
131            return EXTRA_LIGHT;
132        } else if (weight < 350) {
133            return LIGHT;
134        } else if (weight <= 450) {
135            return NORMAL;
136        } else if (weight <= 550) {
137            return MEDIUM;
138        } else if (weight < 650) {
139            return SEMI_BOLD;
140        } else if (weight <= 750) {
141            return BOLD;
142        } else if (weight <= 850) {
143            return EXTRA_BOLD;
144        } else {
145            return BLACK;
146        }
147    }
148}