You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

FontTriplet.java 4.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one or more
  3. * contributor license agreements. See the NOTICE file distributed with
  4. * this work for additional information regarding copyright ownership.
  5. * The ASF licenses this file to You under the Apache License, Version 2.0
  6. * (the "License"); you may not use this file except in compliance with
  7. * the License. You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. /* $Id$ */
  18. package org.apache.fop.fonts;
  19. import java.io.Serializable;
  20. /**
  21. * FontTriplet contains information on name, style and weight of one font
  22. */
  23. public class FontTriplet implements Comparable<FontTriplet>, Serializable {
  24. public static final FontTriplet DEFAULT_FONT_TRIPLET
  25. = new FontTriplet("any", Font.STYLE_NORMAL, Font.WEIGHT_NORMAL);
  26. /** serial version UID */
  27. private static final long serialVersionUID = 1168991106658033508L;
  28. private String name;
  29. private String style;
  30. private int weight;
  31. private int priority; // priority of this triplet/font mapping
  32. //This is only a cache
  33. private transient String key;
  34. /**
  35. * Creates a new font triplet.
  36. * @param name font name
  37. * @param style font style (normal, italic etc.)
  38. * @param weight font weight (100, 200, 300...800, 900)
  39. */
  40. public FontTriplet(String name, String style, int weight) {
  41. this(name, style, weight, Font.PRIORITY_DEFAULT);
  42. }
  43. /**
  44. * Creates a new font triplet.
  45. * @param name font name
  46. * @param style font style (normal, italic etc.)
  47. * @param weight font weight (100, 200, 300...800, 900)
  48. * @param priority priority of this triplet/font mapping
  49. */
  50. public FontTriplet(String name, String style, int weight, int priority) {
  51. this.name = name;
  52. this.style = style;
  53. this.weight = weight;
  54. this.priority = priority;
  55. }
  56. /** @return the font name */
  57. public String getName() {
  58. return name;
  59. }
  60. /** @return the font style */
  61. public String getStyle() {
  62. return style;
  63. }
  64. /** @return the font weight */
  65. public int getWeight() {
  66. return weight;
  67. }
  68. /** @return the priority of this triplet/font mapping */
  69. public int getPriority() {
  70. return priority;
  71. }
  72. private String getKey() {
  73. if (this.key == null) {
  74. //This caches the combined key
  75. this.key = getName() + "," + getStyle() + "," + getWeight();
  76. }
  77. return this.key;
  78. }
  79. /** {@inheritDoc} */
  80. public int compareTo(FontTriplet o) {
  81. return getKey().compareTo(o.getKey());
  82. }
  83. /** {@inheritDoc} */
  84. public int hashCode() {
  85. return toString().hashCode();
  86. }
  87. /** {@inheritDoc} */
  88. public boolean equals(Object obj) {
  89. if (obj == null) {
  90. return false;
  91. } else if (obj == this) {
  92. return true;
  93. } else {
  94. if (obj instanceof FontTriplet) {
  95. FontTriplet other = (FontTriplet)obj;
  96. return (getName().equals(other.getName())
  97. && getStyle().equals(other.getStyle())
  98. && (getWeight() == other.getWeight()));
  99. }
  100. }
  101. return false;
  102. }
  103. /** {@inheritDoc} */
  104. public String toString() {
  105. return getKey();
  106. }
  107. /**
  108. * Matcher interface for {@link FontTriplet}.
  109. */
  110. public interface Matcher {
  111. /**
  112. * Indicates whether the given {@link FontTriplet} matches a particular criterium.
  113. * @param triplet the font triplet
  114. * @return true if the font triplet is a match
  115. */
  116. boolean matches(FontTriplet triplet);
  117. }
  118. }