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.

FontWeightRange.java 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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.substitute;
  19. import java.util.StringTokenizer;
  20. import org.apache.commons.logging.Log;
  21. import org.apache.commons.logging.LogFactory;
  22. /**
  23. * Encapsulates a range of font weight values
  24. */
  25. public class FontWeightRange {
  26. /** logging instance */
  27. protected static final Log log = LogFactory.getLog("org.apache.fop.render.fonts");
  28. /**
  29. * Returns an <code>FontWeightRange</code> object holding the
  30. * range values of the specified <code>String</code>.
  31. *
  32. * @param weightRangeString the value range string
  33. * @return an <code>FontWeightRange</code> object holding the value ranges
  34. */
  35. public static FontWeightRange valueOf(String weightRangeString) {
  36. StringTokenizer rangeToken = new StringTokenizer(weightRangeString, "..");
  37. FontWeightRange weightRange = null;
  38. if (rangeToken.countTokens() == 2) {
  39. String weightString = rangeToken.nextToken().trim();
  40. try {
  41. int start = Integer.parseInt(weightString);
  42. if (start % 100 != 0) {
  43. log.error("font-weight start range is not a multiple of 100");
  44. }
  45. int end = Integer.parseInt(rangeToken.nextToken());
  46. if (end % 100 != 0) {
  47. log.error("font-weight end range is not a multiple of 100");
  48. }
  49. if (start <= end) {
  50. weightRange = new FontWeightRange(start, end);
  51. } else {
  52. log.error("font-weight start range is greater than end range");
  53. }
  54. } catch (NumberFormatException e) {
  55. log.error("invalid font-weight value " + weightString);
  56. }
  57. }
  58. return weightRange;
  59. }
  60. /** the start range */
  61. private int start;
  62. /** the end range */
  63. private int end;
  64. /**
  65. * Main constructor
  66. * @param start the start value range
  67. * @param end the end value range
  68. */
  69. public FontWeightRange(int start, int end) {
  70. this.start = start;
  71. this.end = end;
  72. }
  73. /**
  74. * Returns true if the given integer value is within this integer range
  75. * @param value the integer value
  76. * @return true if the given integer value is within this integer range
  77. */
  78. public boolean isWithinRange(int value) {
  79. return (value >= start && value <= end);
  80. }
  81. /**
  82. * {@inheritDoc}
  83. */
  84. public String toString() {
  85. return start + ".." + end;
  86. }
  87. /**
  88. * @return an integer array containing the weight ranges
  89. */
  90. public int[] toArray() {
  91. int cnt = 0;
  92. for (int i = start; i <= end; i += 100) {
  93. cnt++;
  94. }
  95. int[] range = new int[cnt];
  96. for (int i = 0; i < cnt; i++) {
  97. range[i] = start + (i * 100);
  98. }
  99. return range;
  100. }
  101. }