FontSizeTranslator { public float translateFontSize(final Tag tag) { float size = Font.UNDEFINED; if (tag.getCSS().get(CSS.Property.FONT_SIZE) != null) { String value = tag.getCSS().get(CSS.Property.FONT_SIZE); if (value.equalsIgnoreCase(CSS.Value.XX_SMALL)) { size = 6.75f; } else if (value.equalsIgnoreCase(CSS.Value.X_SMALL)) { size = 7.5f; } else if (value.equalsIgnoreCase(CSS.Value.SMALL)) { size = 9.75f; } else if (value.equalsIgnoreCase(CSS.Value.MEDIUM)) { size = 12f; } else if (value.equalsIgnoreCase(CSS.Value.LARGE)) { size = 13.5f; } else if (value.equalsIgnoreCase(CSS.Value.X_LARGE)) { size = 18f; } else if (value.equalsIgnoreCase(CSS.Value.XX_LARGE)) { size = 24f; } else if (value.equalsIgnoreCase(CSS.Value.SMALLER)) { if (tag.getParent() != null) { float parentSize = getFontSize(tag.getParent()); if (parentSize == Font.UNDEFINED) { size = 9.75f; } else if (parentSize <= 6.75f) { size = parentSize - 1; } else if (parentSize == 7.5f) { size = 6.75f; } else if (parentSize == 9.75f) { size = 7.5f; } else if (parentSize == 12f) { size = 9.75f; } else if (parentSize == 13.5f) { size = 12f; } else if (parentSize == 18f) { size = 13.5f; } else if (parentSize == 24f) { size = 18f; } else if (parentSize < 24f) { size = parentSize * 0.85f; } else if (parentSize >= 24) { size = parentSize * 2 / 3; } } else { size = 9.75f; } } else if (value.equalsIgnoreCase(CSS.Value.LARGER)) { if (tag.getParent() != null) { float parentSize = getFontSize(tag.getParent()); if (parentSize == Font.UNDEFINED) { size = 13.5f; } else if (parentSize == 6.75f) { size = 7.5f; } else if (parentSize == 7.5f) { size = 9.75f; } else if (parentSize == 9.75f) { size = 12f; } else if (parentSize == 12f) { size = 13.5f; } else if (parentSize == 13.5f) { size = 18f; } else if (parentSize == 18f) { size = 24f; } else { size = parentSize * 1.5f; } } else { size = 13.5f; } } else if (utils.isMetricValue(value) || utils.isNumericValue(value)) { size = utils.parsePxInCmMmPcToPt(value); } else if (utils.isRelativeValue(value)) { float baseValue = Font.UNDEFINED; if (tag.getParent() != null) { baseValue = getFontSize(tag.getParent()); } if (baseValue == Font.UNDEFINED) { baseValue = 12; } size = utils.parseRelativeValue(value, baseValue); } } return size; }  static synchronized FontSizeTranslator getInstance(); float translateFontSize(final Tag tag); float getFontSize(final Tag tag); static final float DEFAULT_FONT_SIZE; }
@Test public void resolveXXAndXSmall() throws IOException { p.getCSS().put(CSS.Property.FONT_SIZE, CSS.Value.XX_SMALL); p.getCSS().put(CSS.Property.FONT_SIZE, fst.translateFontSize(p) + "pt"); Chunk c1 = new ChunkCssApplier().apply(new Chunk("Text before span "), p); assertEquals(6.75f, c1.getFont().getSize(), 0); span.getCSS().put(CSS.Property.FONT_SIZE, CSS.Value.X_SMALL); span.getCSS().put(CSS.Property.FONT_SIZE, fst.translateFontSize(span) + "pt"); Chunk c2 = new ChunkCssApplier().apply(new Chunk("text in span "), span); assertEquals(7.5f, c2.getFont().getSize(), 0); }
@Test public void resolveSmallAndMedium() throws IOException { p.getCSS().put(CSS.Property.FONT_SIZE, CSS.Value.SMALL); p.getCSS().put(CSS.Property.FONT_SIZE, fst.translateFontSize(p) + "pt"); Chunk c1 = new ChunkCssApplier().apply(new Chunk("Text before span "), p); assertEquals(9.75f, c1.getFont().getSize(), 0); span.getCSS().put(CSS.Property.FONT_SIZE, CSS.Value.MEDIUM); span.getCSS().put(CSS.Property.FONT_SIZE, fst.translateFontSize(span) + "pt"); Chunk c2 = new ChunkCssApplier().apply(new Chunk("text in span "), span); assertEquals(12f, c2.getFont().getSize(), 0); }
@Test public void resolveLargeXLargeXXLarge() throws IOException { p.getCSS().put(CSS.Property.FONT_SIZE, CSS.Value.LARGE); p.getCSS().put(CSS.Property.FONT_SIZE, fst.translateFontSize(p) + "pt"); Chunk c1 = new ChunkCssApplier().apply(new Chunk("Text before span "), p); assertEquals(13.5f, c1.getFont().getSize(), 0); span.getCSS().put(CSS.Property.FONT_SIZE, CSS.Value.X_LARGE); span.getCSS().put(CSS.Property.FONT_SIZE, fst.translateFontSize(span) + "pt"); Chunk c2 = new ChunkCssApplier().apply(new Chunk("text in span "), span); assertEquals(18f, c2.getFont().getSize(), 0); p.getCSS().put(CSS.Property.FONT_SIZE, CSS.Value.XX_LARGE); p.getCSS().put(CSS.Property.FONT_SIZE, fst.translateFontSize(p) + "pt"); Chunk c3 = new ChunkCssApplier().apply(new Chunk("Text after span."), p); assertEquals(24f, c3.getFont().getSize(), 0); }
@Test public void resolveDefaultToSmaller() throws IOException { p.getCSS().put(CSS.Property.FONT_SIZE, fst.translateFontSize(p) + "pt"); Chunk c1 = new ChunkCssApplier().apply(new Chunk("Text before span "), p); assertEquals(12, c1.getFont().getSize(), 0); span.getCSS().put(CSS.Property.FONT_SIZE, CSS.Value.SMALLER); span.getCSS().put(CSS.Property.FONT_SIZE, fst.translateFontSize(span) + "pt"); Chunk c2 = new ChunkCssApplier().apply(new Chunk("text in span "), span); assertEquals(9.75f, c2.getFont().getSize(), 0); }
@Test public void resolveDefaultToLarger() throws IOException { p.getCSS().put(CSS.Property.FONT_SIZE, fst.translateFontSize(p) + "pt"); Chunk c1 = new ChunkCssApplier().apply(new Chunk("Text before span "), p); assertEquals(12, c1.getFont().getSize(), 0); span.getCSS().put(CSS.Property.FONT_SIZE, CSS.Value.LARGER); span.getCSS().put(CSS.Property.FONT_SIZE, fst.translateFontSize(span) + "pt"); Chunk c2 = new ChunkCssApplier().apply(new Chunk("text in span "), span); assertEquals(13.5f, c2.getFont().getSize(), 0); }
@Test public void resolve15ToSmaller() throws IOException { p.getCSS().put(CSS.Property.FONT_SIZE, "15pt"); p.getCSS().put(CSS.Property.FONT_SIZE, fst.translateFontSize(p) + "pt"); Chunk c1 = new ChunkCssApplier().apply(new Chunk("Text before span "), p); assertEquals(15, c1.getFont().getSize(), 0); span.getCSS().put(CSS.Property.FONT_SIZE, CSS.Value.SMALLER); span.getCSS().put(CSS.Property.FONT_SIZE, fst.translateFontSize(span) + "pt"); Chunk c2 = new ChunkCssApplier().apply(new Chunk("text in span "), span); assertEquals(15 * 0.85f, c2.getFont().getSize(), 0); }
@Test public void resolve15ToLarger() throws IOException { p.getCSS().put(CSS.Property.FONT_SIZE, "15pt"); p.getCSS().put(CSS.Property.FONT_SIZE, fst.translateFontSize(p) + "pt"); Chunk c1 = new ChunkCssApplier().apply(new Chunk("Text before span "), p); assertEquals(15, c1.getFont().getSize(), 0); span.getCSS().put(CSS.Property.FONT_SIZE, CSS.Value.LARGER); span.getCSS().put(CSS.Property.FONT_SIZE, fst.translateFontSize(span) + "pt"); Chunk c2 = new ChunkCssApplier().apply(new Chunk("text in span "), span); assertEquals(15 * 1.5f, c2.getFont().getSize(), 0); }
@Test public void resolve34ToSmaller() throws IOException { p.getCSS().put(CSS.Property.FONT_SIZE, "34pt"); p.getCSS().put(CSS.Property.FONT_SIZE, fst.translateFontSize(p) + "pt"); Chunk c1 = new ChunkCssApplier().apply(new Chunk("Text before span "), p); assertEquals(34, c1.getFont().getSize(), 0); span.getCSS().put(CSS.Property.FONT_SIZE, CSS.Value.SMALLER); span.getCSS().put(CSS.Property.FONT_SIZE, fst.translateFontSize(span) + "pt"); Chunk c2 = new ChunkCssApplier().apply(new Chunk("text in span "), span); assertEquals(34 * 2 / 3f, c2.getFont().getSize(), 0); }
@Test public void resolve34ToLarger() throws IOException { p.getCSS().put(CSS.Property.FONT_SIZE, "34pt"); p.getCSS().put(CSS.Property.FONT_SIZE, fst.translateFontSize(p) + "pt"); Chunk c1 = new ChunkCssApplier().apply(new Chunk("Text before span "), p); assertEquals(34, c1.getFont().getSize(), 0); span.getCSS().put(CSS.Property.FONT_SIZE, CSS.Value.LARGER); span.getCSS().put(CSS.Property.FONT_SIZE, fst.translateFontSize(span) + "pt"); Chunk c2 = new ChunkCssApplier().apply(new Chunk("text in span "), span); assertEquals(34 * 1.5f, c2.getFont().getSize(), 0); }