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.

HardcodedFonts.java 5.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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.pcl;
  19. import java.io.IOException;
  20. import org.apache.commons.logging.Log;
  21. import org.apache.commons.logging.LogFactory;
  22. /**
  23. * This class hold code for selecting a set of hard-coded fonts available in practically all
  24. * PCL implementations. We hope this can be improved in the future.
  25. */
  26. final class HardcodedFonts {
  27. private HardcodedFonts() {
  28. }
  29. /** logging instance */
  30. private static final Log LOG = LogFactory.getLog(HardcodedFonts.class);
  31. /**
  32. * Sets the current font (NOTE: Hard-coded font mappings ATM!)
  33. * @param name the font name (internal F* names for now)
  34. * @param size the font size (in millipoints)
  35. * @param text the text to be rendered (used to determine if there are non-printable chars)
  36. * @return true if the font can be mapped to PCL
  37. * @throws IOException if an I/O problem occurs
  38. */
  39. public static boolean setFont(PCLGenerator gen, String name, int size, String text)
  40. throws IOException {
  41. byte[] encoded = text.getBytes("ISO-8859-1");
  42. for (int i = 0, c = encoded.length; i < c; i++) {
  43. if (encoded[i] == 0x3F && text.charAt(i) != '?') {
  44. return false;
  45. }
  46. }
  47. return selectFont(gen, name, size);
  48. }
  49. protected static boolean selectFont(PCLGenerator gen, String name, int size) throws IOException {
  50. int fontcode = 0;
  51. if (name.length() > 1 && name.charAt(0) == 'F') {
  52. try {
  53. fontcode = Integer.parseInt(name.substring(1));
  54. } catch (Exception e) {
  55. LOG.error(e);
  56. }
  57. }
  58. //Note "(ON" selects ISO 8859-1 symbol set as used by PCLGenerator
  59. String formattedSize = gen.formatDouble2(size / 1000.0);
  60. switch (fontcode) {
  61. case 1: // F1 = Helvetica
  62. // gen.writeCommand("(8U");
  63. // gen.writeCommand("(s1p" + formattedSize + "v0s0b24580T");
  64. // Arial is more common among PCL5 printers than Helvetica - so use Arial
  65. gen.writeCommand("(0N");
  66. gen.writeCommand("(s1p" + formattedSize + "v0s0b16602T");
  67. break;
  68. case 2: // F2 = Helvetica Oblique
  69. gen.writeCommand("(0N");
  70. gen.writeCommand("(s1p" + formattedSize + "v1s0b16602T");
  71. break;
  72. case 3: // F3 = Helvetica Bold
  73. gen.writeCommand("(0N");
  74. gen.writeCommand("(s1p" + formattedSize + "v0s3b16602T");
  75. break;
  76. case 4: // F4 = Helvetica Bold Oblique
  77. gen.writeCommand("(0N");
  78. gen.writeCommand("(s1p" + formattedSize + "v1s3b16602T");
  79. break;
  80. case 5: // F5 = Times Roman
  81. // gen.writeCommand("(8U");
  82. // gen.writeCommand("(s1p" + formattedSize + "v0s0b25093T");
  83. // Times New is more common among PCL5 printers than Times - so use Times New
  84. gen.writeCommand("(0N");
  85. gen.writeCommand("(s1p" + formattedSize + "v0s0b16901T");
  86. break;
  87. case 6: // F6 = Times Italic
  88. gen.writeCommand("(0N");
  89. gen.writeCommand("(s1p" + formattedSize + "v1s0b16901T");
  90. break;
  91. case 7: // F7 = Times Bold
  92. gen.writeCommand("(0N");
  93. gen.writeCommand("(s1p" + formattedSize + "v0s3b16901T");
  94. break;
  95. case 8: // F8 = Times Bold Italic
  96. gen.writeCommand("(0N");
  97. gen.writeCommand("(s1p" + formattedSize + "v1s3b16901T");
  98. break;
  99. case 9: // F9 = Courier
  100. gen.writeCommand("(0N");
  101. gen.writeCommand("(s0p" + gen.formatDouble2(120.01f / (size / 1000.00f))
  102. + "h0s0b4099T");
  103. break;
  104. case 10: // F10 = Courier Oblique
  105. gen.writeCommand("(0N");
  106. gen.writeCommand("(s0p" + gen.formatDouble2(120.01f / (size / 1000.00f))
  107. + "h1s0b4099T");
  108. break;
  109. case 11: // F11 = Courier Bold
  110. gen.writeCommand("(0N");
  111. gen.writeCommand("(s0p" + gen.formatDouble2(120.01f / (size / 1000.00f))
  112. + "h0s3b4099T");
  113. break;
  114. case 12: // F12 = Courier Bold Oblique
  115. gen.writeCommand("(0N");
  116. gen.writeCommand("(s0p" + gen.formatDouble2(120.01f / (size / 1000.00f))
  117. + "h1s3b4099T");
  118. break;
  119. case 13: // F13 = Symbol
  120. return false;
  121. //gen.writeCommand("(19M");
  122. //gen.writeCommand("(s1p" + formattedSize + "v0s0b16686T");
  123. // ECMA Latin 1 Symbol Set in Times Roman???
  124. // gen.writeCommand("(9U");
  125. // gen.writeCommand("(s1p" + formattedSize + "v0s0b25093T");
  126. //break;
  127. case 14: // F14 = Zapf Dingbats
  128. return false;
  129. //gen.writeCommand("(14L");
  130. //gen.writeCommand("(s1p" + formattedSize + "v0s0b45101T");
  131. //break;
  132. default:
  133. //gen.writeCommand("(0N");
  134. //gen.writeCommand("(s" + formattedSize + "V");
  135. return false;
  136. }
  137. return true;
  138. }
  139. }