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.

RGBICCColorFunction.java 4.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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.fo.expr;
  19. import org.apache.fop.datatypes.PercentBase;
  20. import org.apache.fop.datatypes.PercentBaseContext;
  21. import org.apache.fop.fo.pagination.ColorProfile;
  22. import org.apache.fop.fo.pagination.Declarations;
  23. import org.apache.fop.fo.properties.ColorProperty;
  24. import org.apache.fop.fo.properties.Property;
  25. import org.apache.fop.util.ColorUtil;
  26. /**
  27. * Implements the rgb-icc() function.
  28. */
  29. class RGBICCColorFunction extends FunctionBase {
  30. /** {@inheritDoc} */
  31. public int getRequiredArgsCount() {
  32. return 4;
  33. }
  34. @Override
  35. /** {@inheritDoc} */
  36. public boolean hasVariableArgs() {
  37. return true;
  38. }
  39. @Override
  40. /** {@inheritDoc} */
  41. public PercentBase getPercentBase() {
  42. return new ICCPercentBase();
  43. }
  44. /** {@inheritDoc} */
  45. public Property eval(Property[] args, PropertyInfo pInfo) throws PropertyException {
  46. // Map color profile NCNAME to src from declarations/color-profile element
  47. String colorProfileName = args[3].getString();
  48. Declarations decls = (pInfo.getFO() != null
  49. ? pInfo.getFO().getRoot().getDeclarations()
  50. : null);
  51. ColorProfile cp = null;
  52. if (decls == null) {
  53. //function used in a color-specification
  54. //on a FO occurring:
  55. //a) before the fo:declarations,
  56. //b) or in a document without fo:declarations?
  57. //=> return the sRGB fallback
  58. if (!ColorUtil.isPseudoProfile(colorProfileName)) {
  59. Property[] rgbArgs = new Property[3];
  60. System.arraycopy(args, 0, rgbArgs, 0, 3);
  61. return new RGBColorFunction().eval(rgbArgs, pInfo);
  62. }
  63. } else {
  64. cp = decls.getColorProfile(colorProfileName);
  65. if (cp == null) {
  66. if (!ColorUtil.isPseudoProfile(colorProfileName)) {
  67. PropertyException pe = new PropertyException("The " + colorProfileName
  68. + " color profile was not declared");
  69. pe.setPropertyInfo(pInfo);
  70. throw pe;
  71. }
  72. }
  73. }
  74. String src = (cp != null ? cp.getSrc() : "");
  75. float red = 0;
  76. float green = 0;
  77. float blue = 0;
  78. red = args[0].getNumber().floatValue();
  79. green = args[1].getNumber().floatValue();
  80. blue = args[2].getNumber().floatValue();
  81. /* Verify rgb replacement arguments */
  82. if ((red < 0 || red > 255) || (green < 0 || green > 255) || (blue < 0 || blue > 255)) {
  83. throw new PropertyException("Color values out of range. "
  84. + "Arguments to rgb-icc() must be [0..255] or [0%..100%]");
  85. }
  86. // rgb-icc is replaced with fop-rgb-icc which has an extra fifth argument containing the
  87. // color profile src attribute as it is defined in the color-profile declarations element.
  88. StringBuffer sb = new StringBuffer();
  89. sb.append("fop-rgb-icc(");
  90. sb.append(red / 255f);
  91. sb.append(',').append(green / 255f);
  92. sb.append(',').append(blue / 255f);
  93. for (int ix = 3; ix < args.length; ix++) {
  94. if (ix == 3) {
  95. sb.append(',').append(colorProfileName);
  96. sb.append(',').append(src);
  97. } else {
  98. sb.append(',').append(args[ix]);
  99. }
  100. }
  101. sb.append(")");
  102. return ColorProperty.getInstance(pInfo.getUserAgent(), sb.toString());
  103. }
  104. private static final class ICCPercentBase implements PercentBase {
  105. /** {@inheritDoc} */
  106. public int getBaseLength(PercentBaseContext context) throws PropertyException {
  107. return 0;
  108. }
  109. /** {@inheritDoc} */
  110. public double getBaseValue() {
  111. return 255f;
  112. }
  113. /** {@inheritDoc} */
  114. public int getDimension() {
  115. return 0;
  116. }
  117. }
  118. }