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.

FontInfoBuilder.java 3.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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.svg.font;
  19. import java.io.File;
  20. import java.io.IOException;
  21. import java.net.URI;
  22. import java.net.URISyntaxException;
  23. import org.apache.fop.apps.io.InternalResourceResolver;
  24. import org.apache.fop.apps.io.ResourceResolverFactory;
  25. import org.apache.fop.fonts.EmbeddingMode;
  26. import org.apache.fop.fonts.EncodingMode;
  27. import org.apache.fop.fonts.Font;
  28. import org.apache.fop.fonts.FontInfo;
  29. import org.apache.fop.fonts.FontMetrics;
  30. import org.apache.fop.fonts.truetype.OFFontLoader;
  31. class FontInfoBuilder {
  32. public static final String DEJAVU_LGC_SERIF = "DejaVu LGC Serif";
  33. public static final String DROID_SANS_MONO = "Droid Sans Mono";
  34. private static final boolean USE_ADVANCED_BY_DEFAULT = true;
  35. private FontInfo fontInfo;
  36. private int fontKey;
  37. public FontInfoBuilder() {
  38. reset();
  39. }
  40. private void reset() {
  41. fontInfo = new FontInfo();
  42. fontKey = 1;
  43. }
  44. public FontInfoBuilder useDejaVuLGCSerif() {
  45. return useDejaVuLGCSerif(USE_ADVANCED_BY_DEFAULT);
  46. }
  47. public FontInfoBuilder useDejaVuLGCSerif(boolean useAdvanced) {
  48. try {
  49. return useFont(DEJAVU_LGC_SERIF, "DejaVuLGCSerif.ttf", useAdvanced);
  50. } catch (Exception e) {
  51. throw new RuntimeException(e);
  52. }
  53. }
  54. public FontInfoBuilder useDroidSansMono() {
  55. return useDroidSansMono(USE_ADVANCED_BY_DEFAULT);
  56. }
  57. public FontInfoBuilder useDroidSansMono(boolean useAdvanced) {
  58. try {
  59. return useFont(DROID_SANS_MONO, "DroidSansMono.ttf", useAdvanced);
  60. } catch (Exception e) {
  61. throw new RuntimeException(e);
  62. }
  63. }
  64. private FontInfoBuilder useFont(String fontName, String filename, boolean useAdvanced)
  65. throws IOException, URISyntaxException {
  66. URI baseURI = new File("test/resources/fonts/ttf").toURI();
  67. InternalResourceResolver resolver = ResourceResolverFactory.createDefaultInternalResourceResolver(baseURI);
  68. OFFontLoader fontLoader = new OFFontLoader(new URI(filename), null, true,
  69. EmbeddingMode.AUTO, EncodingMode.AUTO, true, useAdvanced, resolver, false, false, true);
  70. FontMetrics font = fontLoader.getFont();
  71. registerFont(font, "F" + fontKey++, fontName);
  72. return this;
  73. }
  74. private void registerFont(FontMetrics font, String key, String familyName) {
  75. fontInfo.addMetrics(key, font);
  76. fontInfo.addFontProperties(key, familyName, Font.STYLE_NORMAL, Font.WEIGHT_NORMAL);
  77. }
  78. public FontInfo build() {
  79. FontInfo fontInfo = this.fontInfo;
  80. reset();
  81. return fontInfo;
  82. }
  83. }