Clover icon

Magnolia Imaging Module 3.2.3

  1. Project Clover database Fri Nov 6 2015 14:54:03 CET
  2. Package info.magnolia.imaging.operations.text

File BasicTextRenderer.java

 

Coverage histogram

../../../../../img/srcFileCovDistChart9.png
50% of files have more coverage

Code metrics

2
17
1
1
97
40
2
0.12
17
1
2
16.7% of code in this file is excluded from these metrics.

Classes

Class Line # Actions
BasicTextRenderer 49 17 16.7% 2 3
0.8585%
 

Contributing tests

This file is covered by 2 tests. .

Source view

1    /**
2    * This file Copyright (c) 2009-2015 Magnolia International
3    * Ltd. (http://www.magnolia-cms.com). All rights reserved.
4    *
5    *
6    * This file is dual-licensed under both the Magnolia
7    * Network Agreement and the GNU General Public License.
8    * You may elect to use one or the other of these licenses.
9    *
10    * This file is distributed in the hope that it will be
11    * useful, but AS-IS and WITHOUT ANY WARRANTY; without even the
12    * implied warranty of MERCHANTABILITY or FITNESS FOR A
13    * PARTICULAR PURPOSE, TITLE, or NONINFRINGEMENT.
14    * Redistribution, except as permitted by whichever of the GPL
15    * or MNA you select, is prohibited.
16    *
17    * 1. For the GPL license (GPL), you can redistribute and/or
18    * modify this file under the terms of the GNU General
19    * Public License, Version 3, as published by the Free Software
20    * Foundation. You should have received a copy of the GNU
21    * General Public License, Version 3 along with this program;
22    * if not, write to the Free Software Foundation, Inc., 51
23    * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
24    *
25    * 2. For the Magnolia Network Agreement (MNA), this file
26    * and the accompanying materials are made available under the
27    * terms of the MNA which accompanies this distribution, and
28    * is available at http://www.magnolia-cms.com/mna.html
29    *
30    * Any modifications to this file must keep this entire header
31    * intact.
32    *
33    */
34    package info.magnolia.imaging.operations.text;
35   
36    import java.awt.Color;
37    import java.awt.Font;
38    import java.awt.Graphics2D;
39    import java.awt.Rectangle;
40    import java.awt.RenderingHints;
41    import java.awt.font.FontRenderContext;
42    import java.awt.font.GlyphVector;
43    import java.awt.geom.Rectangle2D;
44    import java.awt.image.BufferedImage;
45   
46    /**
47    * Basic implementation of a TextRenderer.
48    */
 
49    public class BasicTextRenderer implements TextRenderer {
50    /**
51    * for debugging purposes.
52    */
53    private boolean drawBoundingBox;
54   
55    // TODO this currently does not allow specifying text/character spacing
56   
 
57  4 toggle @Override
58    public void renderText(BufferedImage img, String txt, TextStyle style, HorizontalAlignment horizontalAlignment, VerticalAlignment verticalAlignment, int txtPositionX, int txtPositionY) {
59  4 final Graphics2D g = img.createGraphics();
60   
61  4 g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
62  4 g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
63   
64  4 g.setColor(style.getColor());
65  4 final Font font = style.getFont();
66  4 final FontRenderContext fontRenderContext = g.getFontRenderContext();
67  4 final GlyphVector glyph = font.createGlyphVector(fontRenderContext, txt);
68  4 final Rectangle2D bounds = glyph.getVisualBounds();
69  4 final float textWidth = (float) bounds.getWidth();
70  4 final float textHeight = (float) bounds.getHeight();
71   
72    // Could use ascent and descent for more precise positioning.
73    // As well as TextLayout, and add support for multi-line, etc.
74    // This is left as an exercise to the reader.
75  4 final float x = ((Alignment) horizontalAlignment).getPositionFor(textWidth, img.getWidth(), txtPositionX);
76  4 final float y = ((Alignment) verticalAlignment).getPositionFor(textHeight, img.getHeight(), txtPositionY);
77  4 g.drawGlyphVector(glyph, x, y);
78   
79  4 if (drawBoundingBox) {
80  0 g.setColor(Color.YELLOW);
81  0 g.draw(new Rectangle((int) x, (int) (y - textHeight), (int) textWidth, (int) textHeight));
82    }
83   
84  4 g.dispose();
85    }
86   
 
87    toggle public boolean isDrawBoundingBox() {
88    return drawBoundingBox;
89    }
90   
91    /**
92    * Enable this to draw a yellow box around the generated text, can be helpful to help positioning.
93    */
 
94    toggle public void setDrawBoundingBox(boolean drawBoundingBox) {
95    this.drawBoundingBox = drawBoundingBox;
96    }
97    }