/** * Renders the text specified by the specified <code>String</code>, * using the current text attribute state in the <code>Graphics2D</code> context. * The baseline of the first character is at position * (<i>x</i>, <i>y</i>) in the User Space. * The rendering attributes applied include the <code>Clip</code>, * <code>Transform</code>, <code>Paint</code>, <code>Font</code> and * <code>Composite</code> attributes. For characters in script systems * such as Hebrew and Arabic, the glyphs can be rendered from right to * left, in which case the coordinate supplied is the location of the * leftmost character on the baseline. * @param s the <code>String</code> to be rendered * @param x the x coordinate of the location where the * <code>String</code> should be rendered * @param y the y coordinate of the location where the * <code>String</code> should be rendered * @throws NullPointerException if <code>str</code> is * <code>null</code> * @see #setPaint * @see java.awt.Graphics#setColor * @see java.awt.Graphics#setFont * @see #setTransform * @see #setComposite * @see #setClip */ public void DrawString(String s, float x, float y) { TextBox txt = new TextBox(_group); txt.GetTextRun().supplySlideShow(_group.Sheet.GetSlideShow()); txt.GetTextRun().SetSheet(_group.Sheet); txt.SetText(s); RichTextRun rt = txt.GetTextRun().GetRichTextRuns()[0]; rt.SetFontSize(_font.GetSize()); rt.SetFontName(_font.GetFamily()); if (getColor() != null) rt.SetFontColor(getColor()); if (_font.IsBold()) rt.SetBold(true); if (_font.IsItalic()) rt.SetItalic(true); txt.SetMarginBottom(0); txt.SetMarginTop(0); txt.SetMarginLeft(0); txt.SetMarginRight(0); txt.SetWordWrap(TextBox.WrapNone); txt.SetHorizontalAlignment(TextBox.AlignLeft); txt.SetVerticalAlignment(TextBox.AnchorMiddle); TextLayout layout = new TextLayout(s, _font, GetFontRenderContext()); float ascent = layout.GetAscent(); float width = (float) Math.floor(layout.GetAdvance()); /** * Even if top and bottom margins are Set to 0 PowerPoint * always Sets extra space between the text and its bounding box. * * The approximation height = ascent*2 works good enough in most cases */ float height = ascent * 2; /* In powerpoint anchor of a shape is its top left corner. Java graphics Sets string coordinates by the baseline of the first character so we need to shift up by the height of the textbox */ y -= height / 2 + ascent / 2; /* In powerpoint anchor of a shape is its top left corner. Java graphics Sets string coordinates by the baseline of the first character so we need to shift down by the height of the textbox */ txt.SetAnchor(new Rectangle2D.Float(x, y, width, height)); _group.AddShape(txt); }