public override IList<IElement> Start(IWorkerContext ctx, Tag tag) { float x = 0, y = 0; IDictionary<String, String> attributes = tag.Attributes; if(attributes != null){ try { if (attributes.ContainsKey(X)) { x = int.Parse(attributes[X]); } } catch { // TODO: handle exception } try { if (attributes.ContainsKey(Y)) { y = int.Parse(attributes[Y]); } } catch { // TODO: handle exception } } IList<IElement> l = new List<IElement>(0); Chunk c = new ChunkCssApplier().Apply(new Chunk(""), tag); l.Add(new Text(c, x, y, tag.CSS, Split(attributes[DX]), Split(attributes[DY]))); return l; }
/** * */ public CssAppliers() { chunk = new ChunkCssApplier(); paragraph = new ParagraphCssApplier(); nonewlineparagraph = new NoNewLineParagraphCssApplier(); htmlcell = new HtmlCellCssApplier(); list = new ListStyleTypeCssApplier(); lineseparator = new LineSeparatorCssApplier(); image = new ImageCssApplier(); }
public void ResolveFontSize() { Tag t = new Tag("t"); t.CSS["font-size"] = "12pt"; Chunk c = new Chunk("default text with no styles attached."); c = new ChunkCssApplier().Apply(c, t); Assert.AreEqual(12, c.Font.Size, 0); t.CSS["font-size"] = "18pt"; c = new ChunkCssApplier().Apply(c, t); Assert.AreEqual(18, c.Font.Size, 0); }
public void ResolveDefaultToSmaller() { p.CSS[CSS.Property.FONT_SIZE] = fst.TranslateFontSize(p).ToString(CultureInfo.InvariantCulture) + "pt"; Chunk c1 = new ChunkCssApplier().Apply(new Chunk("Text before span "), p); Assert.AreEqual(12, c1.Font.Size, 0); span.CSS[CSS.Property.FONT_SIZE] = CSS.Value.SMALLER; span.CSS[CSS.Property.FONT_SIZE] = fst.TranslateFontSize(span).ToString(CultureInfo.InvariantCulture) + "pt"; Chunk c2 = new ChunkCssApplier().Apply(new Chunk("text in span "), span); Assert.AreEqual(9.75f, c2.Font.Size, 0); }
/* * (non-Javadoc) * * @see * com.itextpdf.tool.xml.ITagProcessor#content(com.itextpdf.tool.xml.Tag, * java.util.List, com.itextpdf.text.Document, java.lang.String) */ public override IList<IElement> Content(IWorkerContext ctx, Tag tag, String content) { String sanitized = HTMLUtils.SanitizeInline(content); IList<IElement> l = new List<IElement>(1); if (sanitized.Length > 0) { Chunk c = new ChunkCssApplier().Apply(new Chunk(sanitized), tag); // NoNewLineParagraph noNewLineParagraph = new NoNewLineParagraphCssApplier(configuration).Apply(new NoNewLineParagraph(c), tag); l.Add(c); } return l; }
/* (non-Javadoc) * @see com.itextpdf.tool.xml.ITagProcessor#content(com.itextpdf.tool.xml.Tag, java.lang.String) */ public override IList<IElement> Content(IWorkerContext ctx, Tag tag, String content) { String sanitized = HTMLUtils.SanitizeInline(content); IList<IElement> l = new List<IElement>(1); if (sanitized.Length > 0) { Chunk c = new ChunkCssApplier().Apply(new Chunk(sanitized), tag); try { l.Add(GetCssAppliers().Apply(new NoNewLineParagraph(c), tag, GetHtmlPipelineContext(ctx))); } catch (NoCustomContextException e) { throw new RuntimeWorkerException(e); } } return l; }
public void ResolveLargeXLargeXXLarge() { p.CSS[CSS.Property.FONT_SIZE] = CSS.Value.LARGE; p.CSS[CSS.Property.FONT_SIZE] = fst.TranslateFontSize(p).ToString(CultureInfo.InvariantCulture) + "pt"; Chunk c1 = new ChunkCssApplier().Apply(new Chunk("Text before span "), p); Assert.AreEqual(13.5f, c1.Font.Size, 0); span.CSS[CSS.Property.FONT_SIZE] = CSS.Value.X_LARGE; span.CSS[CSS.Property.FONT_SIZE] = fst.TranslateFontSize(span).ToString(CultureInfo.InvariantCulture) + "pt"; Chunk c2 = new ChunkCssApplier().Apply(new Chunk("text in span "), span); Assert.AreEqual(18f, c2.Font.Size, 0); p.CSS[CSS.Property.FONT_SIZE] = CSS.Value.XX_LARGE; p.CSS[CSS.Property.FONT_SIZE] = fst.TranslateFontSize(p) + "pt"; Chunk c3 = new ChunkCssApplier().Apply(new Chunk("Text after span."), p); Assert.AreEqual(24f, c3.Font.Size, 0); }
public void Resolve34ToLarger() { p.CSS[CSS.Property.FONT_SIZE] = "34pt"; p.CSS[CSS.Property.FONT_SIZE] = fst.TranslateFontSize(p) + "pt"; Chunk c1 = new ChunkCssApplier().Apply(new Chunk("Text before span "), p); Assert.AreEqual(34, c1.Font.Size, 0); span.CSS[CSS.Property.FONT_SIZE] = CSS.Value.LARGER; span.CSS[CSS.Property.FONT_SIZE] = fst.TranslateFontSize(span) + "pt"; Chunk c2 = new ChunkCssApplier().Apply(new Chunk("text in span "), span); Assert.AreEqual(34*1.5f, c2.Font.Size, 0); }
public void Resolve15ToLarger() { p.CSS[CSS.Property.FONT_SIZE] = "15pt"; p.CSS[CSS.Property.FONT_SIZE] = fst.TranslateFontSize(p).ToString(CultureInfo.InvariantCulture) + "pt"; Chunk c1 = new ChunkCssApplier().Apply(new Chunk("Text before span "), p); Assert.AreEqual(15, c1.Font.Size, 0); span.CSS[CSS.Property.FONT_SIZE] = CSS.Value.LARGER; span.CSS[CSS.Property.FONT_SIZE] = fst.TranslateFontSize(span).ToString(CultureInfo.InvariantCulture) + "pt"; Chunk c2 = new ChunkCssApplier().Apply(new Chunk("text in span "), span); Assert.AreEqual(15*1.5f, c2.Font.Size, 0); }
public override IList<IElement> Content(IWorkerContext ctx, Tag tag, String content) { float x = -1, y = -1; IDictionary<String, String> attributes = tag.Attributes; if(attributes != null){ try{ if (attributes.ContainsKey(X)) { x = int.Parse(attributes[X]); } }catch { // TODO: handle exception } try{ if (attributes.ContainsKey(Y)) { y = int.Parse(attributes[Y]); } }catch { // TODO: handle exception } } IList<IElement> l = new List<IElement>(1); String sanitized = content.Trim(); //TODO check this if (sanitized.Length > 0 && attributes.ContainsKey(DX) && attributes.ContainsKey(DY)) { Chunk c = new ChunkCssApplier().Apply(new Chunk(sanitized), tag); if (x == -1 && y == -1){ l.Add(new Text(c, tag.CSS, Split(attributes[DX]), Split(attributes[DY]))); }else{ l.Add(new Text(c, x, y, tag.CSS, Split(attributes[DX]), Split(attributes[DY]))); } } return l; }
/** * Sets the default cell width and widest word of a cell. * <ul> * <li>cell width = {@link Table#getCellStartWidth(HtmlCell)} + the width of the widest line of text.</li> * <li>widest word = {@link Table#getCellStartWidth(HtmlCell)} + the widest word of the cell.</li> * </ul> * These 2 widths are used as the starting point when determining the width of the table in * @param cell HtmlCell of which the widths are needed. * @return float array containing the default cell width and the widest word. * <ul> * <li>float[0] = cell width.</li> * <li>float[1] = widest word.</li> * </ul> */ private float[] SetCellWidthAndWidestWord(HtmlCell cell) { IList<float> rulesWidth = new List<float>(); float widestWordOfCell = 0f; float startWidth = GetCellStartWidth(cell); float cellWidth; float widthDeviation = 0.001f; IList<IElement> compositeElements = cell.CompositeElements; if (compositeElements != null) { foreach (IElement baseLevel in compositeElements) { cellWidth = float.NaN; if (baseLevel is Phrase) { for(int i = 0; i < ((Phrase)baseLevel).Count; i++) { IElement inner = ((Phrase)baseLevel)[i]; if (inner is Chunk) { if(float.IsNaN(cellWidth)) cellWidth = startWidth + widthDeviation; cellWidth += ((Chunk)inner).GetWidthPoint(); float widestWord = startWidth + widthDeviation + GetCssAppliers().ChunkCssAplier.GetWidestWord((Chunk)inner); if(widestWord > widestWordOfCell) { widestWordOfCell = widestWord; } } } if(!float.IsNaN(cellWidth)) rulesWidth.Add(cellWidth); } else if (baseLevel is List) { foreach (IElement li in ((List)baseLevel).Items) { cellWidth = startWidth + widthDeviation + ((ListItem)li).IndentationLeft; foreach (Chunk c in li.Chunks) { cellWidth += c.GetWidthPoint(); float widestWord = new ChunkCssApplier().GetWidestWord(c); if(startWidth + widthDeviation + widestWord > widestWordOfCell) { widestWordOfCell = startWidth + widthDeviation + widestWord; } } rulesWidth.Add(cellWidth); } } else if (baseLevel is PdfPTable) { cellWidth = startWidth + widthDeviation + ((PdfPTable)baseLevel).TotalWidth; foreach (PdfPRow innerRow in ((PdfPTable)baseLevel).Rows) { int size = innerRow.GetCells().Length; TableBorderEvent evente = (TableBorderEvent) ((PdfPTable)baseLevel).TableEvent; TableStyleValues values = evente.TableStyleValues; float minRowWidth = values.BorderWidthLeft+(size+1)*values.HorBorderSpacing+values.BorderWidthRight; int celnr = 0; foreach (PdfPCell innerCell in innerRow.GetCells()) { celnr++; if(innerCell != null) { float innerWidestWordOfCell = SetCellWidthAndWidestWord(new HtmlCell(innerCell, celnr == size))[1]; minRowWidth += innerWidestWordOfCell; } } if(minRowWidth > widestWordOfCell){ widestWordOfCell = minRowWidth; } } rulesWidth.Add(cellWidth); } else if (baseLevel is PdfDiv) { PdfDiv div = (PdfDiv) baseLevel; float? divActualWidth = div.Width; if (divActualWidth == null) { divActualWidth = CalculateDivWidestElementWidth(div.Content); } cellWidth = startWidth + widthDeviation + (float)divActualWidth; rulesWidth.Add(cellWidth); } } } cellWidth = startWidth; foreach (float width in rulesWidth) { if(width > cellWidth) { cellWidth = width; } } return new float[]{cellWidth, widestWordOfCell}; }
/* (non-Javadoc) * @see com.itextpdf.tool.xml.css.CssApplier#apply(com.itextpdf.text.Element, com.itextpdf.tool.xml.Tag) */ public Paragraph Apply(Paragraph p, Tag t, IMarginMemory configuration) { /*if (this.configuration.GetRootTags().Contains(t.Name)) { * m.SetLeading(t); * } else { * m.SetVariablesBasedOnChildren(t); * }*/ float fontSize = FontSizeTranslator.GetInstance().GetFontSize(t); float lmb = 0; bool hasLMB = false; IDictionary <String, String> css = t.CSS; foreach (KeyValuePair <String, String> entry in css) { String key = entry.Key; String value = entry.Value; if (Util.EqualsIgnoreCase(CSS.Property.MARGIN_TOP, key)) { p.SpacingBefore = p.SpacingBefore + utils.CalculateMarginTop(value, fontSize, configuration); } else if (Util.EqualsIgnoreCase(CSS.Property.PADDING_TOP, key)) { p.SpacingBefore = p.SpacingBefore + utils.ParseValueToPt(value, fontSize); } else if (Util.EqualsIgnoreCase(CSS.Property.MARGIN_BOTTOM, key)) { float after = utils.ParseValueToPt(value, fontSize); p.SpacingAfter = p.SpacingAfter + after; lmb = after; hasLMB = true; } else if (Util.EqualsIgnoreCase(CSS.Property.PADDING_BOTTOM, key)) { p.SpacingAfter = p.SpacingAfter + utils.ParseValueToPt(value, fontSize); } else if (Util.EqualsIgnoreCase(CSS.Property.MARGIN_LEFT, key)) { p.IndentationLeft = p.IndentationLeft + utils.ParseValueToPt(value, fontSize); } else if (Util.EqualsIgnoreCase(CSS.Property.MARGIN_RIGHT, key)) { p.IndentationRight = p.IndentationRight + utils.ParseValueToPt(value, fontSize); } else if (Util.EqualsIgnoreCase(CSS.Property.PADDING_LEFT, key)) { p.IndentationLeft = p.IndentationLeft + utils.ParseValueToPt(value, fontSize); } else if (Util.EqualsIgnoreCase(CSS.Property.PADDING_RIGHT, key)) { p.IndentationRight = p.IndentationRight + utils.ParseValueToPt(value, fontSize); } else if (Util.EqualsIgnoreCase(CSS.Property.TEXT_ALIGN, key)) { if (Util.EqualsIgnoreCase(CSS.Value.RIGHT, value)) { p.Alignment = Element.ALIGN_RIGHT; } else if (Util.EqualsIgnoreCase(CSS.Value.CENTER, value)) { p.Alignment = Element.ALIGN_CENTER; } else if (Util.EqualsIgnoreCase(CSS.Value.LEFT, value)) { p.Alignment = Element.ALIGN_LEFT; } else if (Util.EqualsIgnoreCase(CSS.Value.JUSTIFY, value)) { p.Alignment = Element.ALIGN_JUSTIFIED; } } else if (Util.EqualsIgnoreCase(CSS.Property.TEXT_INDENT, key)) { p.FirstLineIndent = utils.ParseValueToPt(value, fontSize); } else if (Util.EqualsIgnoreCase(CSS.Property.LINE_HEIGHT, key)) { if (utils.IsNumericValue(value)) { p.Leading = float.Parse(value, CultureInfo.InvariantCulture) * fontSize; } else if (utils.IsRelativeValue(value)) { p.Leading = utils.ParseRelativeValue(value, fontSize); } else if (utils.IsMetricValue(value)) { p.Leading = utils.ParsePxInCmMmPcToPt(value); } } } // setDefaultMargin to largestFont if no margin-bottom is set and p-tag is child of the root tag. /*if (null != t.Parent) { * String parent = t.Parent.Name; * if (!css.ContainsKey(CSS.Property.MARGIN_TOP) && configuration.GetRootTags().Contains(parent)) { * p.SpacingBefore = p.SpacingBefore+utils.CalculateMarginTop(fontSize.ToString(CultureInfo.InvariantCulture)+"pt", 0, configuration); * } * if (!css.ContainsKey(CSS.Property.MARGIN_BOTTOM) && configuration.GetRootTags().Contains(parent)) { * p.SpacingAfter = p.SpacingAfter+fontSize; * css[CSS.Property.MARGIN_BOTTOM] = fontSize.ToString(CultureInfo.InvariantCulture)+"pt"; * lmb = fontSize; * hasLMB = true; * } * p.Leading = m.GetLargestLeading(); * if (p.Alignment == -1) { * p.Alignment = Element.ALIGN_LEFT; * } * }*/ if (hasLMB) { configuration.LastMarginBottom = lmb; } //TODO this only work around for applaying of font properties to paragraph Chunk dummy = new ChunkCssApplier().Apply(new Chunk("dummy"), t); p.Font = dummy.Font; return(p); }
/* (non-Javadoc) * @see com.itextpdf.tool.xml.css.CssApplier#apply(com.itextpdf.text.Element, com.itextpdf.tool.xml.Tag) */ public Paragraph Apply(Paragraph p, Tag t, IMarginMemory configuration) { /*if (this.configuration.GetRootTags().Contains(t.Name)) { m.SetLeading(t); } else { m.SetVariablesBasedOnChildren(t); }*/ float fontSize = FontSizeTranslator.GetInstance().GetFontSize(t); float lmb = 0; bool hasLMB = false; IDictionary<String, String> css = t.CSS; foreach (KeyValuePair<String, String> entry in css) { String key = entry.Key; String value = entry.Value; if (Util.EqualsIgnoreCase(CSS.Property.MARGIN_TOP, key)) { p.SpacingBefore = p.SpacingBefore + utils.CalculateMarginTop(value, fontSize, configuration); } else if (Util.EqualsIgnoreCase(CSS.Property.PADDING_TOP, key)) { p.SpacingBefore = p.SpacingBefore + utils.ParseValueToPt(value, fontSize); } else if (Util.EqualsIgnoreCase(CSS.Property.MARGIN_BOTTOM, key)) { float after = utils.ParseValueToPt(value, fontSize); p.SpacingAfter = p.SpacingAfter + after; lmb = after; hasLMB = true; } else if (Util.EqualsIgnoreCase(CSS.Property.PADDING_BOTTOM, key)) { p.SpacingAfter = p.SpacingAfter + utils.ParseValueToPt(value, fontSize); } else if (Util.EqualsIgnoreCase(CSS.Property.MARGIN_LEFT, key)) { p.IndentationLeft = p.IndentationLeft + utils.ParseValueToPt(value, fontSize); } else if (Util.EqualsIgnoreCase(CSS.Property.MARGIN_RIGHT, key)) { p.IndentationRight = p.IndentationRight + utils.ParseValueToPt(value, fontSize); } else if (Util.EqualsIgnoreCase(CSS.Property.PADDING_LEFT, key)) { p.IndentationLeft = p.IndentationLeft + utils.ParseValueToPt(value, fontSize); } else if (Util.EqualsIgnoreCase(CSS.Property.PADDING_RIGHT, key)) { p.IndentationRight = p.IndentationRight + utils.ParseValueToPt(value, fontSize); } else if (Util.EqualsIgnoreCase(CSS.Property.TEXT_ALIGN, key)) { if (Util.EqualsIgnoreCase(CSS.Value.RIGHT, value)){ p.Alignment = Element.ALIGN_RIGHT; } else if (Util.EqualsIgnoreCase(CSS.Value.CENTER, value)){ p.Alignment = Element.ALIGN_CENTER; } else if (Util.EqualsIgnoreCase(CSS.Value.LEFT, value)){ p.Alignment = Element.ALIGN_LEFT; } else if (Util.EqualsIgnoreCase(CSS.Value.JUSTIFY, value)) { p.Alignment = Element.ALIGN_JUSTIFIED; } } else if (Util.EqualsIgnoreCase(CSS.Property.TEXT_INDENT, key)) { p.FirstLineIndent = utils.ParseValueToPt(value, fontSize); } else if (Util.EqualsIgnoreCase(CSS.Property.LINE_HEIGHT, key)) { if(utils.IsNumericValue(value)) { p.Leading = float.Parse(value, CultureInfo.InvariantCulture) * fontSize; } else if (utils.IsRelativeValue(value)) { p.Leading = utils.ParseRelativeValue(value, fontSize); } else if (utils.IsMetricValue(value)){ p.Leading = utils.ParsePxInCmMmPcToPt(value); } } } // setDefaultMargin to largestFont if no margin-bottom is set and p-tag is child of the root tag. /*if (null != t.Parent) { String parent = t.Parent.Name; if (!css.ContainsKey(CSS.Property.MARGIN_TOP) && configuration.GetRootTags().Contains(parent)) { p.SpacingBefore = p.SpacingBefore+utils.CalculateMarginTop(fontSize.ToString(CultureInfo.InvariantCulture)+"pt", 0, configuration); } if (!css.ContainsKey(CSS.Property.MARGIN_BOTTOM) && configuration.GetRootTags().Contains(parent)) { p.SpacingAfter = p.SpacingAfter+fontSize; css[CSS.Property.MARGIN_BOTTOM] = fontSize.ToString(CultureInfo.InvariantCulture)+"pt"; lmb = fontSize; hasLMB = true; } p.Leading = m.GetLargestLeading(); if (p.Alignment == -1) { p.Alignment = Element.ALIGN_LEFT; } }*/ if (hasLMB) { configuration.LastMarginBottom = lmb; } //TODO this only work around for applaying of font properties to paragraph Chunk dummy = new ChunkCssApplier().Apply(new Chunk("dummy"), t); p.Font = dummy.Font; return p; }
public override IList<IElement> Content(IWorkerContext ctx, Tag tag, String content) { IList<IElement> l = new List<IElement>(1); String sanitized = content.Trim(); //TODO check this if (sanitized.Length > 0) { Chunk c = new ChunkCssApplier().Apply(new Chunk(sanitized), tag); l.Add(new Text(c, tag.CSS, null, null)); } return l; }