diff --git a/.gitignore b/.gitignore index eead1875..59d130e9 100644 --- a/.gitignore +++ b/.gitignore @@ -13,3 +13,4 @@ hs_err_pid* # JMH report file jmh.csv +/target diff --git a/pom.xml b/pom.xml index ee4bc0d9..32de8e0a 100644 --- a/pom.xml +++ b/pom.xml @@ -54,7 +54,7 @@ org.apache.poi poi-ooxml - 3.15-beta1 + 3.15-beta2 test @@ -66,7 +66,7 @@ org.assertj assertj-core - 3.4.1 + 3.5.2 test @@ -78,13 +78,13 @@ org.openjdk.jmh jmh-core - 1.12 + 1.13 test org.openjdk.jmh jmh-generator-annprocess - 1.12 + 1.13 test @@ -131,7 +131,7 @@ org.jacoco jacoco-maven-plugin - 0.7.6.201602180812 + 0.7.7.201606060606 pre-unit-test diff --git a/src/main/java/org/dhatim/fastexcel/Font.java b/src/main/java/org/dhatim/fastexcel/Font.java index 02f7eba2..65a41048 100644 --- a/src/main/java/org/dhatim/fastexcel/Font.java +++ b/src/main/java/org/dhatim/fastexcel/Font.java @@ -29,7 +29,7 @@ class Font { /** * Default font. */ - protected static final Font DEFAULT = build(false, false, "000000"); + protected static final Font DEFAULT = build(false, false, null, null, null); /** * Bold flag. @@ -43,10 +43,6 @@ class Font { * Font name. */ private final String name; - /** - * Font family. - */ - private final int family; /** * Font size. */ @@ -62,34 +58,34 @@ class Font { * @param bold Bold flag. * @param italic Italic flag. * @param name Font name. - * @param family Font family numbering. * @param size Font size, in points. * @param rgbColor RGB font color. */ - Font(boolean bold, boolean italic, String name, int family, BigDecimal size, String rgbColor) { + private Font(boolean bold, boolean italic, String name, BigDecimal size, String rgbColor) { this.bold = bold; this.italic = italic; this.name = name; - this.family = family; - this.size = size; + this.size = size.setScale(2); this.rgbColor = rgbColor; } /** - * Helper to create a new "Calibri" font, family 2. + * Helper to create a new font. * * @param bold Bold flag. * @param italic Italic flag. - * @param rgbColor RGB font color. + * @param name Font name. Defaults to "Calibri". + * @param size Font size, in points. Defaults to 11.0. + * @param rgbColor RGB font color. Defaults to "000000". * @return New font object. */ - static Font build(boolean bold, boolean italic, String rgbColor) { - return new Font(bold, italic, "Calibri", 2, BigDecimal.valueOf(11.0), rgbColor); + static Font build(boolean bold, boolean italic, String name, BigDecimal size, String rgbColor) { + return new Font(bold, italic, name == null ? "Calibri" : name, size == null ? BigDecimal.valueOf(11.0) : size, rgbColor == null ? "000000" : rgbColor); } @Override public int hashCode() { - return Objects.hash(bold, italic, name, family, size, rgbColor); + return Objects.hash(bold, italic, name, size, rgbColor); } @Override @@ -97,7 +93,7 @@ public boolean equals(Object obj) { boolean result; if (obj != null && obj.getClass() == this.getClass()) { Font other = (Font) obj; - result = Objects.equals(bold, other.bold) && Objects.equals(italic, other.italic) && Objects.equals(name, other.name) && Objects.equals(family, other.family) && Objects.equals(size, other.size) && Objects.equals(rgbColor, other.rgbColor); + result = Objects.equals(bold, other.bold) && Objects.equals(italic, other.italic) && Objects.equals(name, other.name) && Objects.equals(size, other.size) && Objects.equals(rgbColor, other.rgbColor); } else { result = false; } @@ -115,6 +111,7 @@ void write(Writer w) throws IOException { if (rgbColor != null) { w.append(""); } - w.append(""); + w.append(""); + w.append(""); } } diff --git a/src/main/java/org/dhatim/fastexcel/StyleSetter.java b/src/main/java/org/dhatim/fastexcel/StyleSetter.java index eec57b71..6d845e54 100644 --- a/src/main/java/org/dhatim/fastexcel/StyleSetter.java +++ b/src/main/java/org/dhatim/fastexcel/StyleSetter.java @@ -15,6 +15,7 @@ */ package org.dhatim.fastexcel; +import java.math.BigDecimal; import java.util.Map; import java.util.Set; import java.util.function.Function; @@ -55,6 +56,14 @@ public class StyleSetter { * Italic flag. */ private boolean italic; + /** + * Font name. + */ + private String fontName; + /** + * Font size. + */ + private BigDecimal fontSize; /** * RGB font color. */ @@ -135,6 +144,39 @@ public StyleSetter fontColor(String rgb) { return this; } + /** + * Set font name. + * + * @param name Font name. + * @return This style setter. + */ + public StyleSetter fontName(String name) { + this.fontName = name; + return this; + } + + /** + * Set font size. + * + * @param size Font size, in points. + * @return This style setter. + */ + public StyleSetter fontSize(BigDecimal size) { + this.fontSize = size; + return this; + } + + /** + * Set font size. + * + * @param size Font size, in points. + * @return This style setter. + */ + public StyleSetter fontSize(int size) { + this.fontSize = BigDecimal.valueOf(size); + return this; + } + /** * Use bold text. * @@ -236,8 +278,8 @@ public void set() { alignment = null; } Font font; - if (bold || italic || fontColor != null) { - font = Font.build(bold, italic, fontColor); + if (bold || italic || fontColor != null || fontName != null || fontSize != null) { + font = Font.build(bold, italic, fontName, fontSize, fontColor); } else { font = Font.DEFAULT; } diff --git a/src/test/java/org/dhatim/fastexcel/Correctness.java b/src/test/java/org/dhatim/fastexcel/Correctness.java index ad0a5dde..60e60363 100644 --- a/src/test/java/org/dhatim/fastexcel/Correctness.java +++ b/src/test/java/org/dhatim/fastexcel/Correctness.java @@ -268,7 +268,7 @@ public void multipleWorksheets() throws Exception { ws.formula(numRows + 1, 4, "=AVERAGE(" + ws.range(1, 4, numRows, 4).toString() + ")"); ws.style(numRows + 1, 4).format("yyyy-MM-dd HH:mm:ss").set(); ws.formula(numRows + 1, 5, "=AVERAGE(" + ws.range(1, 5, numRows, 5).toString() + ")"); - ws.style(numRows + 1, 5).format("yyyy-MM-dd").bold().italic().fontColor(Color.RED).horizontalAlignment("center").verticalAlignment("top").wrapText(true).set(); + ws.style(numRows + 1, 5).format("yyyy-MM-dd").bold().italic().fontColor(Color.RED).fontName("Garamond").fontSize(new BigDecimal("14.5")).horizontalAlignment("center").verticalAlignment("top").wrapText(true).set(); ws.range(1, 0, numRows, numCols - 1).style().borderColor(Color.RED).borderStyle("thick").shadeAlternateRows(Color.RED).set(); }); cfs[i] = cf;