/// <summary>Process the text contained in the text-node</summary> /// <param name="textNode">node containing text to process</param> private void ProcessText(ITextNode textNode) { ISvgNodeRenderer parentRenderer = this.processorState.Top(); if (parentRenderer is TextSvgBranchRenderer) { String wholeText = textNode.WholeText(); if (!"".Equals(wholeText) && !SvgTextUtil.IsOnlyWhiteSpace(wholeText)) { TextLeafSvgNodeRenderer textLeaf = new TextLeafSvgNodeRenderer(); textLeaf.SetParent(parentRenderer); textLeaf.SetAttribute(SvgConstants.Attributes.TEXT_CONTENT, wholeText); ((TextSvgBranchRenderer)parentRenderer).AddChild(textLeaf); } } }
public virtual float GetTextContentLength(float parentFontSize, PdfFont font) { float contentLength = 0.0f; if (font != null && this.attributesAndStyles != null && this.attributesAndStyles.ContainsKey(SvgConstants.Attributes .TEXT_CONTENT)) { //Use own font-size declaration if it is present, parent's otherwise float fontSize = (float)SvgTextUtil.ResolveFontSize(this, parentFontSize); String content = this.attributesAndStyles.Get(SvgConstants.Attributes.TEXT_CONTENT); contentLength = font.GetWidth(content, fontSize); } return(contentLength); }
/// <summary>Process the text contained in the text-node</summary> /// <param name="textNode">node containing text to process</param> private void ProcessText(ITextNode textNode) { ISvgNodeRenderer parentRenderer = this.processorState.Top(); if (parentRenderer is TextSvgNodeRenderer) { // when svg is parsed by jsoup it leaves all whitespace in text element as is. Meaning that // tab/space indented xml files will retain their tabs and spaces. // The following regex replaces all whitespace with a single space. //TODO(RND-906) evaluate regex and trim methods String trimmedText = iText.IO.Util.StringUtil.ReplaceAll(textNode.WholeText(), "\\s+", " "); //Trim leading whitespace trimmedText = SvgTextUtil.TrimLeadingWhitespace(trimmedText); //Trim trailing whitespace trimmedText = SvgTextUtil.TrimTrailingWhitespace(trimmedText); parentRenderer.SetAttribute(SvgConstants.Attributes.TEXT_CONTENT, trimmedText); } }
internal static void DrawMarker(SvgDrawContext context, String moveX, String moveY, MarkerVertexType markerToUse , AbstractSvgNodeRenderer parent) { String elementToReUse = parent.attributesAndStyles.Get(markerToUse.ToString()); String normalizedName = SvgTextUtil.FilterReferenceValue(elementToReUse); ISvgNodeRenderer template = context.GetNamedObject(normalizedName); //Clone template ISvgNodeRenderer namedObject = template == null ? null : template.CreateDeepCopy(); if (namedObject is MarkerSvgNodeRenderer && // Having markerWidth or markerHeight with negative or zero value disables rendering of the element . MarkerWidthHeightAreCorrect((MarkerSvgNodeRenderer)namedObject)) { // setting the parent of the referenced element to this instance namedObject.SetParent(parent); namedObject.SetAttribute(SvgConstants.Tags.MARKER, markerToUse.ToString()); namedObject.SetAttribute(SvgConstants.Attributes.X, moveX); namedObject.SetAttribute(SvgConstants.Attributes.Y, moveY); namedObject.Draw(context); // unsetting the parent of the referenced element namedObject.SetParent(null); } }
internal virtual void ResolveFontSize() { //TODO: DEVSIX-2607 (re)move static variable fontSize = (float)SvgTextUtil.ResolveFontSize(this, DEFAULT_FONT_SIZE); }
/// <summary> /// Method that will set properties to be inherited by this branch renderer's /// children and will iterate over all children in order to draw them. /// </summary> /// <param name="context"> /// the object that knows the place to draw this element and /// maintains its state /// </param> protected internal override void DoDraw(SvgDrawContext context) { if (GetChildren().Count > 0) { // if branch has no children, don't do anything PdfCanvas currentCanvas = context.GetCurrentCanvas(); if (performRootTransformations) { currentCanvas.BeginText(); //Current transformation matrix results in the character glyphs being mirrored, correct with inverse tf AffineTransform rootTf; if (this.ContainsAbsolutePositionChange()) { rootTf = GetTextTransform(this.GetAbsolutePositionChanges(), context); } else { rootTf = new AffineTransform(TEXTFLIP); } currentCanvas.SetTextMatrix(rootTf); //Reset context of text move context.ResetTextMove(); //Apply relative move if (this.ContainsRelativeMove()) { float[] rootMove = this.GetRelativeTranslation(); context.AddTextMove(rootMove[0], -rootMove[1]); } //-y to account for the text-matrix transform we do in the text root to account for the coordinates //handle white-spaces if (!whiteSpaceProcessed) { SvgTextUtil.ProcessWhiteSpace(this, true); } } ApplyTextRenderingMode(currentCanvas); if (this.attributesAndStyles != null) { ResolveFontSize(); ResolveFont(context); currentCanvas.SetFontAndSize(font, fontSize); foreach (ISvgTextNodeRenderer c in children) { float childLength = c.GetTextContentLength(fontSize, font); if (c.ContainsAbsolutePositionChange()) { //TODO: DEVSIX-2507 support rotate and other attributes float[][] absolutePositions = c.GetAbsolutePositionChanges(); AffineTransform newTransform = GetTextTransform(absolutePositions, context); //overwrite the last transformation stored in the context context.SetLastTextTransform(newTransform); //Apply transformation currentCanvas.SetTextMatrix(newTransform); //Absolute position changes requires resetting the current text move in the context context.ResetTextMove(); } //Handle Text-Anchor declarations float textAnchorCorrection = GetTextAnchorAlignmentCorrection(childLength); if (!CssUtils.CompareFloats(0f, textAnchorCorrection)) { context.AddTextMove(textAnchorCorrection, 0); } //Move needs to happen before the saving of the state in order for it to cascade beyond if (c.ContainsRelativeMove()) { float[] childMove = c.GetRelativeTranslation(); context.AddTextMove(childMove[0], -childMove[1]); } //-y to account for the text-matrix transform we do in the text root to account for the coordinates currentCanvas.SaveState(); c.Draw(context); context.AddTextMove(childLength, 0); currentCanvas.RestoreState(); //Restore transformation matrix if (!context.GetLastTextTransform().IsIdentity()) { currentCanvas.SetTextMatrix(context.GetLastTextTransform()); } } if (performRootTransformations) { currentCanvas.EndText(); } } } }
protected internal override void DoDraw(SvgDrawContext context) { if (this.attributesAndStyles != null) { String elementToReUse = this.attributesAndStyles.Get(SvgConstants.Attributes.XLINK_HREF); if (elementToReUse == null) { elementToReUse = this.attributesAndStyles.Get(SvgConstants.Attributes.HREF); } if (elementToReUse != null && !String.IsNullOrEmpty(elementToReUse) && IsValidHref(elementToReUse)) { String normalizedName = SvgTextUtil.FilterReferenceValue(elementToReUse); if (!context.IsIdUsedByUseTagBefore(normalizedName)) { ISvgNodeRenderer template = context.GetNamedObject(normalizedName); //Clone template ISvgNodeRenderer namedObject = template == null ? null : template.CreateDeepCopy(); //Resolve parent inheritance SvgNodeRendererInheritanceResolver iresolver = new SvgNodeRendererInheritanceResolver(); iresolver.ApplyInheritanceToSubTree(this, namedObject); if (namedObject != null) { if (namedObject is AbstractSvgNodeRenderer) { ((AbstractSvgNodeRenderer)namedObject).SetPartOfClipPath(partOfClipPath); } PdfCanvas currentCanvas = context.GetCurrentCanvas(); float x = 0f; float y = 0f; if (this.attributesAndStyles.ContainsKey(SvgConstants.Attributes.X)) { x = CssUtils.ParseAbsoluteLength(this.attributesAndStyles.Get(SvgConstants.Attributes.X)); } if (this.attributesAndStyles.ContainsKey(SvgConstants.Attributes.Y)) { y = CssUtils.ParseAbsoluteLength(this.attributesAndStyles.Get(SvgConstants.Attributes.Y)); } AffineTransform inverseMatrix = null; if (!CssUtils.CompareFloats(x, 0) || !CssUtils.CompareFloats(y, 0)) { AffineTransform translation = AffineTransform.GetTranslateInstance(x, y); currentCanvas.ConcatMatrix(translation); if (partOfClipPath) { try { inverseMatrix = translation.CreateInverse(); } catch (NoninvertibleTransformException ex) { LogManager.GetLogger(typeof(UseSvgNodeRenderer)).Warn(SvgLogMessageConstant.NONINVERTIBLE_TRANSFORMATION_MATRIX_USED_IN_CLIP_PATH , ex); } } } // setting the parent of the referenced element to this instance namedObject.SetParent(this); namedObject.Draw(context); // unsetting the parent of the referenced element namedObject.SetParent(null); if (inverseMatrix != null) { currentCanvas.ConcatMatrix(inverseMatrix); } } } } } }
private void ResolveFontSize() { //TODO (DEVSIX-2607) (re)move static variable fontSize = (float)SvgTextUtil.ResolveFontSize(this, DEFAULT_FONT_SIZE); }