Debian Codegraph @

src/main/java/org/jfree/pdf/internal/FontKey.java

raw
1 /* =====================================================================
2 * JFreePDF : a fast, light-weight PDF library for the Java(tm) platform
3 * =====================================================================
4 *
5 * (C)opyright 2013-2022, by David Gilbert. All rights reserved.
6 *
7 * https://github.com/jfree/orsonpdf
8 *
9 * This program is free software: you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation, either version 3 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program. If not, see <http://www.gnu.org/licenses/>.
21 *
22 * [Oracle and Java are registered trademarks of Oracle and/or its affiliates.
23 * Other names may be trademarks of their respective owners.]
24 *
25 * If you do not wish to be bound by the terms of the GPL, an alternative
26 * runtime license is available to JFree sponsors:
27 *
28 * https://github.com/sponsors/jfree
29 *
30 */
31
32 package org.jfree.pdf.internal;
33
34 import java.awt.Font;
35 import org.jfree.pdf.util.Args;
36 import org.jfree.pdf.util.ObjectUtils;
37
38 /**
39 * A key to represent a Java2D font. This is used to maintain a mapping
40 * between Java2D fonts and PDF fonts.
41 */
42 public class FontKey {
43
44 /** The key name. */
45 private String name;
46
47 /** Flag for bold. */
48 private boolean isBold;
49
50 /** Flag for italic. */
51 private boolean isItalic;
52
53 /**
54 * Creates a new key for a given font.
55 *
56 * @param f the font ({@code null} not permitted).
57 *
58 * @return The font key.
59 */
60 public static FontKey createFontKey(Font f) {
61 Args.nullNotPermitted(f, "f");
62 String family = f.getFamily().replace(' ', '_');
63 boolean bold = f.isBold();
64 boolean italic = f.isItalic();
65 return new FontKey(family, bold, italic);
66 }
67
68 /**
69 * Creates a new font key.
70 *
71 * @param name the name.
72 * @param bold the bold flag.
73 * @param italic the italic flag.
74 *
75 * @see FontKey#createFontKey(java.awt.Font)
76 */
77 public FontKey(String name, boolean bold, boolean italic) {
78 this.name = name;
79 this.isBold = bold;
80 this.isItalic = italic;
81 }
82
83 /**
84 * Tests this key for equality with an arbitrary object.
85 *
86 * @param obj the object to test against ({@code null} permitted).
87 *
88 * @return A boolean.
89 */
90 @Override
91 public boolean equals(Object obj) {
92 if (obj == null) {
93 return false;
94 }
95 if (getClass() != obj.getClass()) {
96 return false;
97 }
98 final FontKey other = (FontKey) obj;
99 if (!ObjectUtils.equals(this.name, other.name)) {
100 return false;
101 }
102 if (this.isBold != other.isBold) {
103 return false;
104 }
105 if (this.isItalic != other.isItalic) {
106 return false;
107 }
108 return true;
109 }
110
111 /**
112 * Returns a hash code for this instance.
113 *
114 * @return A hash code.
115 */
116 @Override
117 public int hashCode() {
118 int hash = 3;
119 hash = 97 * hash + ObjectUtils.hashCode(this.name);
120 hash = 97 * hash + (this.isBold ? 1 : 0);
121 hash = 97 * hash + (this.isItalic ? 1 : 0);
122 return hash;
123 }
124
125 /**
126 * Returns a string representation of this instance, primarily for
127 * debugging purposes.
128 *
129 * @return A string.
130 */
131 @Override
132 public String toString() {
133 StringBuilder sb = new StringBuilder();
134 sb.append("FontKey[name=").append(this.name);
135 sb.append(",isBold=").append(this.isBold);
136 sb.append(",isItalic=").append(this.isItalic);
137 sb.append("]");
138 return sb.toString();
139 }
140
141 }