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.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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 void 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. }
  43. private FontInfo createFontInfo() {
  44. FontInfo fontInfo = new FontInfo();
  45. new Base14FontCollection(true).setup(0, fontInfo);
  46. return fontInfo;
  47. }
  48. private BridgeContext createBridgeContext(FontInfo fontInfo) {
  49. FOUserAgent userAgent = FopFactory.newInstance(new File(".").toURI()).newFOUserAgent();
  50. SVGUserAgent svgUserAgent = new SVGUserAgent(userAgent, new FOPFontFamilyResolverImpl(fontInfo),
  51. new AffineTransform());
  52. BridgeContext bridgeContext = new BridgeContext(svgUserAgent);
  53. bridgeContext.setTextPainter(createTextPainter(fontInfo));
  54. return bridgeContext;
  55. }
  56. protected abstract TextPainter createTextPainter(FontInfo fontInfo);
  57. private GraphicsNode loadSVG(BridgeContext bridgeContext, String resourceName) throws IOException {
  58. SAXSVGDocumentFactory factory = new SAXSVGDocumentFactory(null);
  59. Document svg = factory.createDocument(null, getClass().getResourceAsStream(resourceName));
  60. GVTBuilder builder = new GVTBuilder();
  61. return builder.build(bridgeContext, svg);
  62. }
  63. protected abstract Graphics2D createGraphics2D(FontInfo fontInfo, OperatorValidator validator);
  64. }