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.7KB

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