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.

NativeTextPainterTest.java 3.1KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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;
  19. import java.awt.Graphics2D;
  20. import java.awt.geom.AffineTransform;
  21. import java.io.File;
  22. import java.io.IOException;
  23. import org.w3c.dom.Document;
  24. import org.apache.batik.anim.dom.SAXSVGDocumentFactory;
  25. import org.apache.batik.bridge.BridgeContext;
  26. import org.apache.batik.bridge.GVTBuilder;
  27. import org.apache.batik.bridge.TextPainter;
  28. import org.apache.batik.gvt.GraphicsNode;
  29. import org.apache.fop.apps.FOUserAgent;
  30. import org.apache.fop.apps.FopFactory;
  31. import org.apache.fop.fonts.FontInfo;
  32. import org.apache.fop.fonts.base14.Base14FontCollection;
  33. import org.apache.fop.svg.font.FOPFontFamilyResolverImpl;
  34. abstract class NativeTextPainterTest {
  35. protected final Graphics2D runTest(String testcase, OperatorValidator validator) throws Exception {
  36. FontInfo fontInfo = createFontInfo();
  37. BridgeContext bridgeContext = createBridgeContext(fontInfo);
  38. GraphicsNode svg = loadSVG(bridgeContext, testcase);
  39. Graphics2D g2d = createGraphics2D(fontInfo, validator);
  40. svg.paint(g2d);
  41. validator.end();
  42. return g2d;
  43. }
  44. private FontInfo createFontInfo() {
  45. FontInfo fontInfo = new FontInfo();
  46. new Base14FontCollection(true).setup(0, fontInfo);
  47. return fontInfo;
  48. }
  49. private BridgeContext createBridgeContext(FontInfo fontInfo) {
  50. FOUserAgent userAgent = FopFactory.newInstance(new File(".").toURI()).newFOUserAgent();
  51. SVGUserAgent svgUserAgent = new SVGUserAgent(userAgent, new FOPFontFamilyResolverImpl(fontInfo),
  52. new AffineTransform());
  53. BridgeContext bridgeContext = new BridgeContext(svgUserAgent);
  54. bridgeContext.setTextPainter(createTextPainter(fontInfo));
  55. return bridgeContext;
  56. }
  57. protected abstract TextPainter createTextPainter(FontInfo fontInfo);
  58. private GraphicsNode loadSVG(BridgeContext bridgeContext, String resourceName) throws IOException {
  59. SAXSVGDocumentFactory factory = new SAXSVGDocumentFactory(null);
  60. Document svg = factory.createDocument(null, getClass().getResourceAsStream(resourceName));
  61. GVTBuilder builder = new GVTBuilder();
  62. return builder.build(bridgeContext, svg);
  63. }
  64. protected abstract Graphics2D createGraphics2D(FontInfo fontInfo, OperatorValidator validator);
  65. }