Skip to content

Commit

Permalink
Merge 3963aca into 24830db
Browse files Browse the repository at this point in the history
  • Loading branch information
Olivier Chédru committed Aug 30, 2016
2 parents 24830db + 3963aca commit 4c8b32e
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 24 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,4 @@ hs_err_pid*

# JMH report file
jmh.csv
/target
10 changes: 5 additions & 5 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.15-beta1</version>
<version>3.15-beta2</version>
<scope>test</scope>
</dependency>
<dependency>
Expand All @@ -66,7 +66,7 @@
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<version>3.4.1</version>
<version>3.5.2</version>
<scope>test</scope>
</dependency>
<dependency>
Expand All @@ -78,13 +78,13 @@
<dependency>
<groupId>org.openjdk.jmh</groupId>
<artifactId>jmh-core</artifactId>
<version>1.12</version>
<version>1.13</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.openjdk.jmh</groupId>
<artifactId>jmh-generator-annprocess</artifactId>
<version>1.12</version>
<version>1.13</version>
<scope>test</scope>
</dependency>
</dependencies>
Expand Down Expand Up @@ -131,7 +131,7 @@
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.7.6.201602180812</version>
<version>0.7.7.201606060606</version>
<executions>
<execution>
<id>pre-unit-test</id>
Expand Down
29 changes: 13 additions & 16 deletions src/main/java/org/dhatim/fastexcel/Font.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -43,10 +43,6 @@ class Font {
* Font name.
*/
private final String name;
/**
* Font family.
*/
private final int family;
/**
* Font size.
*/
Expand All @@ -62,42 +58,42 @@ 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
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;
}
Expand All @@ -115,6 +111,7 @@ void write(Writer w) throws IOException {
if (rgbColor != null) {
w.append("<color rgb=\"").append(rgbColor).append("\"/>");
}
w.append("<name val=\"").appendEscaped(name).append("\"/></font>");
w.append("<name val=\"").appendEscaped(name).append("\"/>");
w.append("</font>");
}
}
46 changes: 44 additions & 2 deletions src/main/java/org/dhatim/fastexcel/StyleSetter.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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.
*/
Expand Down Expand Up @@ -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.
*
Expand Down Expand Up @@ -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;
}
Expand Down
2 changes: 1 addition & 1 deletion src/test/java/org/dhatim/fastexcel/Correctness.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 4c8b32e

Please sign in to comment.