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.

GlyphNameFieldPart.java 2.7KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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.util.text;
  19. import java.util.Map;
  20. import org.apache.xmlgraphics.fonts.Glyphs;
  21. import org.apache.fop.util.text.AdvancedMessageFormat.Part;
  22. import org.apache.fop.util.text.AdvancedMessageFormat.PartFactory;
  23. /**
  24. * Function formatting a character to a glyph name.
  25. */
  26. public class GlyphNameFieldPart implements Part {
  27. private String fieldName;
  28. /**
  29. * Creates a new glyph name field part
  30. * @param fieldName the field name
  31. */
  32. public GlyphNameFieldPart(String fieldName) {
  33. this.fieldName = fieldName;
  34. }
  35. /** {@inheritDoc} */
  36. public boolean isGenerated(Map params) {
  37. Object obj = params.get(fieldName);
  38. return obj != null && getGlyphName(obj).length() > 0;
  39. }
  40. private String getGlyphName(Object obj) {
  41. if (obj instanceof Character) {
  42. return Glyphs.charToGlyphName((Character) obj);
  43. } else {
  44. throw new IllegalArgumentException(
  45. "Value for glyph name part must be a Character but was: "
  46. + obj.getClass().getName());
  47. }
  48. }
  49. /** {@inheritDoc} */
  50. public void write(StringBuffer sb, Map params) {
  51. if (!params.containsKey(fieldName)) {
  52. throw new IllegalArgumentException(
  53. "Message pattern contains unsupported field name: " + fieldName);
  54. }
  55. Object obj = params.get(fieldName);
  56. sb.append(getGlyphName(obj));
  57. }
  58. /** {@inheritDoc} */
  59. public String toString() {
  60. return "{" + this.fieldName + ",glyph-name}";
  61. }
  62. /** Factory for {@link GlyphNameFieldPart}. */
  63. public static class Factory implements PartFactory {
  64. /** {@inheritDoc} */
  65. public Part newPart(String fieldName, String values) {
  66. return new GlyphNameFieldPart(fieldName);
  67. }
  68. /** {@inheritDoc} */
  69. public String getFormat() {
  70. return "glyph-name";
  71. }
  72. }
  73. }