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.

AFPBase12FontCollection.java 7.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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.afp.fonts;
  19. import org.apache.fop.afp.AFPEventProducer;
  20. import org.apache.fop.fonts.Base14Font;
  21. import org.apache.fop.fonts.Font;
  22. import org.apache.fop.fonts.FontCollection;
  23. import org.apache.fop.fonts.FontInfo;
  24. import org.apache.fop.fonts.base14.Courier;
  25. import org.apache.fop.fonts.base14.CourierBold;
  26. import org.apache.fop.fonts.base14.CourierBoldOblique;
  27. import org.apache.fop.fonts.base14.CourierOblique;
  28. import org.apache.fop.fonts.base14.Helvetica;
  29. import org.apache.fop.fonts.base14.HelveticaBold;
  30. import org.apache.fop.fonts.base14.HelveticaOblique;
  31. import org.apache.fop.fonts.base14.TimesBold;
  32. import org.apache.fop.fonts.base14.TimesBoldItalic;
  33. import org.apache.fop.fonts.base14.TimesItalic;
  34. import org.apache.fop.fonts.base14.TimesRoman;
  35. /**
  36. * Sets up a typical Base 12 font configuration for AFP
  37. */
  38. public class AFPBase12FontCollection implements FontCollection {
  39. private final AFPEventProducer eventProducer;
  40. /**
  41. * @param eventProducer the AFP-specific event producer
  42. */
  43. public AFPBase12FontCollection(AFPEventProducer eventProducer) {
  44. this.eventProducer = eventProducer;
  45. }
  46. /** standard raster font sizes */
  47. private static final int[] RASTER_SIZES = {6, 7, 8, 9, 10, 11, 12, 14, 16, 18, 20, 24, 30, 36};
  48. /** standard raster font charset references */
  49. private static final String[] CHARSET_REF = {
  50. "60", "70", "80", "90", "00", "A0", "B0", "D0", "F0", "H0", "J0", "N0", "T0", "Z0"};
  51. private void addCharacterSet(RasterFont font, String charsetName, Base14Font base14) {
  52. for (int i = 0; i < RASTER_SIZES.length; i++) {
  53. int size = RASTER_SIZES[i] * 1000;
  54. FopCharacterSet characterSet = new FopCharacterSet(
  55. CharacterSet.DEFAULT_CODEPAGE, CharacterSet.DEFAULT_ENCODING,
  56. charsetName + CHARSET_REF[i], base14, eventProducer);
  57. font.addCharacterSet(size, characterSet);
  58. }
  59. }
  60. private int addFontProperties(FontInfo fontInfo, AFPFont font,
  61. String[] names, String style, int weight, int num) {
  62. String internalFontKey = "F" + num;
  63. fontInfo.addMetrics(internalFontKey, font);
  64. fontInfo.addFontProperties(internalFontKey, names, style, weight);
  65. num++;
  66. return num;
  67. }
  68. /** {@inheritDoc} */
  69. public int setup(int start, FontInfo fontInfo) {
  70. /**
  71. * Add the base 12 fonts (Helvetica, Times and Courier)
  72. *
  73. * Note: this default font configuration may not be available
  74. * on your AFP environment.
  75. */
  76. int num = start;
  77. RasterFont font = null;
  78. /** standard font family reference names for Helvetica font */
  79. final String[] helveticaNames = {"Helvetica", "Arial", "sans-serif"};
  80. font = createReferencedRasterFont("Helvetica");
  81. addCharacterSet(font, "C0H200", new Helvetica());
  82. num = addFontProperties(fontInfo, font, helveticaNames,
  83. Font.STYLE_NORMAL, Font.WEIGHT_NORMAL, num);
  84. font = createReferencedRasterFont("Helvetica Italic");
  85. addCharacterSet(font, "C0H300", new HelveticaOblique());
  86. num = addFontProperties(fontInfo, font, helveticaNames,
  87. Font.STYLE_ITALIC, Font.WEIGHT_NORMAL, num);
  88. font = createReferencedRasterFont("Helvetica (Semi) Bold");
  89. addCharacterSet(font, "C0H400", new HelveticaBold());
  90. num = addFontProperties(fontInfo, font, helveticaNames,
  91. Font.STYLE_NORMAL, Font.WEIGHT_BOLD, num);
  92. font = createReferencedRasterFont("Helvetica Italic (Semi) Bold");
  93. addCharacterSet(font, "C0H500", new HelveticaOblique());
  94. num = addFontProperties(fontInfo, font, helveticaNames,
  95. Font.STYLE_ITALIC, Font.WEIGHT_BOLD, num);
  96. /** standard font family reference names for Times font */
  97. /** any is treated as serif */
  98. final String[] timesNames = {"Times", "TimesRoman", "Times Roman", "Times-Roman",
  99. "Times New Roman", "TimesNewRoman", "serif", "any"};
  100. font = createReferencedRasterFont("Times Roman");
  101. addCharacterSet(font, "C0N200", new TimesRoman());
  102. num = addFontProperties(fontInfo, font, timesNames,
  103. Font.STYLE_NORMAL, Font.WEIGHT_NORMAL, num);
  104. font = createReferencedRasterFont("Times Roman Italic");
  105. addCharacterSet(font, "C0N300", new TimesItalic());
  106. num = addFontProperties(fontInfo, font, timesNames,
  107. Font.STYLE_ITALIC, Font.WEIGHT_NORMAL, num);
  108. font = createReferencedRasterFont("Times Roman Bold");
  109. addCharacterSet(font, "C0N400", new TimesBold());
  110. num = addFontProperties(fontInfo, font, timesNames,
  111. Font.STYLE_NORMAL, Font.WEIGHT_BOLD, num);
  112. font = createReferencedRasterFont("Times Roman Italic Bold");
  113. addCharacterSet(font, "C0N500", new TimesBoldItalic());
  114. num = addFontProperties(fontInfo, font, timesNames,
  115. Font.STYLE_ITALIC, Font.WEIGHT_BOLD, num);
  116. /** standard font family reference names for Courier font */
  117. final String[] courierNames = {"Courier", "monospace"};
  118. font = createReferencedRasterFont("Courier");
  119. addCharacterSet(font, "C04200", new Courier());
  120. num = addFontProperties(fontInfo, font, courierNames,
  121. Font.STYLE_NORMAL, Font.WEIGHT_NORMAL, num);
  122. font = createReferencedRasterFont("Courier Italic");
  123. addCharacterSet(font, "C04300", new CourierOblique());
  124. num = addFontProperties(fontInfo, font, courierNames,
  125. Font.STYLE_ITALIC, Font.WEIGHT_NORMAL, num);
  126. font = createReferencedRasterFont("Courier Bold");
  127. addCharacterSet(font, "C04400", new CourierBold());
  128. num = addFontProperties(fontInfo, font, courierNames,
  129. Font.STYLE_NORMAL, Font.WEIGHT_BOLD, num);
  130. font = createReferencedRasterFont("Courier Italic Bold");
  131. addCharacterSet(font, "C04500", new CourierBoldOblique());
  132. num = addFontProperties(fontInfo, font, courierNames,
  133. Font.STYLE_ITALIC, Font.WEIGHT_BOLD, num);
  134. return num;
  135. }
  136. private RasterFont createReferencedRasterFont(String fontFamily) {
  137. boolean embeddable = false; //Font is assumed to be available on the target platform
  138. return new RasterFont(fontFamily, embeddable);
  139. }
  140. }