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.

Java2DUtil.java 5.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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.render.java2d;
  19. import java.awt.Graphics2D;
  20. import java.awt.font.GlyphVector;
  21. import java.util.Arrays;
  22. import org.apache.fop.apps.FOUserAgent;
  23. import org.apache.fop.fonts.Font;
  24. import org.apache.fop.fonts.FontCollection;
  25. import org.apache.fop.fonts.FontEventAdapter;
  26. import org.apache.fop.fonts.FontInfo;
  27. import org.apache.fop.fonts.FontManager;
  28. import org.apache.fop.fonts.LazyFont;
  29. import org.apache.fop.fonts.MultiByteFont;
  30. import org.apache.fop.fonts.Typeface;
  31. import org.apache.fop.util.CharUtilities;
  32. /**
  33. * Rendering-related utilities for Java2D.
  34. */
  35. public final class Java2DUtil {
  36. private Java2DUtil() {
  37. }
  38. /**
  39. * Builds a default {@link FontInfo} object for use with output formats using the Java2D
  40. * font setup.
  41. * @param fontInfo the font info object to populate
  42. * @param userAgent the user agent
  43. * @return the populated font information object
  44. */
  45. public static FontInfo buildDefaultJava2DBasedFontInfo(
  46. FontInfo fontInfo, FOUserAgent userAgent) {
  47. Java2DFontMetrics java2DFontMetrics = new Java2DFontMetrics();
  48. FontManager fontManager = userAgent.getFontManager();
  49. FontCollection[] fontCollections = new FontCollection[] {
  50. new org.apache.fop.render.java2d.Base14FontCollection(java2DFontMetrics),
  51. new InstalledFontCollection(java2DFontMetrics)
  52. };
  53. FontInfo fi = (fontInfo != null ? fontInfo : new FontInfo());
  54. fi.setEventListener(new FontEventAdapter(userAgent.getEventBroadcaster()));
  55. fontManager.setup(fi, fontCollections);
  56. return fi;
  57. }
  58. /**
  59. * Creates an instance of {@link GlyphVector} that correctly handle surrogate pairs and advanced font features such
  60. * as GSUB/GPOS/GDEF.
  61. *
  62. * @param text Text to render
  63. * @param g2d the target Graphics2D instance
  64. * @param font the font instance
  65. * @param fontInfo the font information
  66. * @return an instance of {@link GlyphVector}
  67. */
  68. public static GlyphVector createGlyphVector(String text, Graphics2D g2d, Font font, FontInfo fontInfo) {
  69. MultiByteFont multiByteFont = getMultiByteFont(font.getFontName(), fontInfo);
  70. if (multiByteFont == null) {
  71. return createGlyphVector(text, g2d);
  72. }
  73. return createGlyphVectorMultiByteFont(text, g2d, multiByteFont);
  74. }
  75. /**
  76. * Creates a {@link GlyphVector} using characters. Filters out non-bmp characters.
  77. */
  78. private static GlyphVector createGlyphVector(String text, Graphics2D g2d) {
  79. StringBuilder sb = new StringBuilder(text.length());
  80. for (int cp : CharUtilities.codepointsIter(text)) {
  81. // If we are here we probably do not support non-BMP codepoints
  82. sb.appendCodePoint(cp <= 0xFFFF ? cp : Typeface.NOT_FOUND);
  83. }
  84. return g2d.getFont().createGlyphVector(g2d.getFontRenderContext(), sb.toString());
  85. }
  86. /**
  87. * Creates a {@link GlyphVector} using glyph indexes instead of characters. To correctly support the advanced font
  88. * features we have to build the GlyphVector passing the glyph indexes instead of the characters. This because some
  89. * of the chars in text might have been replaced by an internal font representation during
  90. * GlyphMapping.processWordMapping. Eg 'fi' replaced with the corresponding character in the font ligatures table
  91. * (GSUB).
  92. */
  93. private static GlyphVector createGlyphVectorMultiByteFont(String text, Graphics2D g2d,
  94. MultiByteFont multiByteFont) {
  95. int[] glyphCodes = new int[text.length()];
  96. int currentIdx = 0;
  97. for (int cp : CharUtilities.codepointsIter(text)) {
  98. // mapChar is not working here because MultiByteFont.mapChar replaces the glyph index with
  99. // CIDSet.mapChar when isEmbeddable == true.
  100. glyphCodes[currentIdx++] = multiByteFont.findGlyphIndex(cp);
  101. }
  102. // Trims glyphCodes
  103. if (currentIdx != text.length()) {
  104. glyphCodes = Arrays.copyOf(glyphCodes, currentIdx);
  105. }
  106. return g2d.getFont().createGlyphVector(g2d.getFontRenderContext(), glyphCodes);
  107. }
  108. /**
  109. * Returns an instance of {@link MultiByteFont} for the given font name. This method will try to unwrap containers
  110. * such as {@link CustomFontMetricsMapper} and {@link LazyFont}
  111. *
  112. * @param fontName font key
  113. * @param fontInfo font information
  114. * @return An instance of {@link MultiByteFont} or null if it
  115. */
  116. private static MultiByteFont getMultiByteFont(String fontName, FontInfo fontInfo) {
  117. Typeface tf = fontInfo.getFonts().get(fontName);
  118. if (tf instanceof CustomFontMetricsMapper) {
  119. tf = ((CustomFontMetricsMapper) tf).getRealFont();
  120. }
  121. if (tf instanceof LazyFont) {
  122. tf = ((LazyFont) tf).getRealFont();
  123. }
  124. return (tf instanceof MultiByteFont) ? (MultiByteFont) tf : null;
  125. }
  126. }