Browse Source

Improved style sheet generation.

pull/170/head
weisj 5 years ago
parent
commit
1287a9c436
  1. 159
      core/src/main/java/com/github/weisj/darklaf/task/CSSBuilder.java
  2. 395
      core/src/main/java/com/github/weisj/darklaf/task/StyleSheetInitTask.java

159
core/src/main/java/com/github/weisj/darklaf/task/CSSBuilder.java

@ -0,0 +1,159 @@
/*
* MIT License
*
* Copyright (c) 2020 Jannis Weis
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
*/
package com.github.weisj.darklaf.task;
class CSSBuilder {
private final StringBuilder b = new StringBuilder();
private boolean groupOpen = false;
public CSSGroup group(final String name) {
return nextGroup(name);
}
private CSSBuilder closeGroup() {
if (groupOpen) {
b.append("}");
groupOpen = false;
}
return this;
}
private CSSGroup nextGroup(final String name) {
closeGroup();
b.append(name).append(" {");
groupOpen = true;
return new CSSGroup(this);
}
private void addProperty(final String key, final String value) {
b.append(key).append(":").append(value).append(";");
}
@Override
public String toString() {
return b.toString();
}
protected static class CSSGroup {
protected final CSSBuilder builder;
public CSSGroup(final CSSBuilder builder) {
this.builder = builder;
}
public CSSBuilder end() {
return builder.closeGroup();
}
public CSSGroup group(final String name) {
return builder.nextGroup(name);
}
public CSSGroup property(final String key, final Object value) {
builder.addProperty(key, value.toString());
return this;
}
public CSSGroup color(final String value) {
return property("color", value);
}
public CSSGroup border(final Object value) {
return property("border", value);
}
public CSSGroup borderColor(final String value) {
return property("border-color", value);
}
public CSSGroup borderStyle(final String value) {
return property("border-style", value);
}
public CSSGroup borderWidth(final String value) {
return property("border-width", value);
}
public CSSGroup padding(final Object value) {
return property("padding", value);
}
public CSSGroup borderCollapse(final String value) {
return property("border-collapse", value);
}
public CSSGroup fontSize(final Object value) {
return property("font-size", value);
}
public CSSGroup fontWeight(final String value) {
return property("font-weight", value);
}
public CSSGroup fontStyle(final String value) {
return property("font-style", value);
}
public CSSGroup textDecoration(final String value) {
return property("text-decoration", value);
}
public CSSGroup fontFamily(final String family, final String name) {
return property("font-family", family + ", " + name);
}
public CSSGroup marginTop(final Object value) {
return property("margin-top", value);
}
public CSSGroup marginBottom(final Object value) {
return property("margin-bottom", value);
}
public CSSGroup marginLeft(final Object value) {
return property("margin-left", value);
}
public CSSGroup marginRight(final Object value) {
return property("margin-right", value);
}
public CSSGroup marginLeftLTR(final Object value) {
return property("margin-left-ltr", value);
}
public CSSGroup marginRightRTL(final Object value) {
return property("margin-right-rtl", value);
}
public CSSGroup listStyleType(final String value) {
return property("list-style-type", value);
}
public CSSGroup margin(final Object value) {
return property("margin", value);
}
}
}

395
core/src/main/java/com/github/weisj/darklaf/task/StyleSheetInitTask.java

@ -42,139 +42,211 @@ public class StyleSheetInitTask implements DefaultsInitTask {
this.defaults = defaults; this.defaults = defaults;
StyleSheet styleSheet = new StyleSheet(); StyleSheet styleSheet = new StyleSheet();
Font font = defaults.getFont("html.font"); Font font = defaults.getFont("html.font");
StringBuilder b = new StringBuilder(); CSSBuilder builder = new CSSBuilder();
b.append("body {")
.append("font-size: 14pt;") // @formatter:off
.append("font-family: ").append(font.getFamily()).append(", ").append(font.getFontName()).append(";") builder.group("body")
.append("font-weight: normal;") .fontSize(pt(14))
.append("margin-left: 0;") .fontFamily(font.getFamily(), font.getName())
.append("margin-right: 0;") .fontWeight("normal")
.append("color: ").append(hex("textForeground")).append(";") .marginRight(0)
.append("}"); .marginLeft(0)
makeSingle(b, "p", "margin-top", "15px"); .color(hex("textForeground"))
makeHeading(b, 1, "x-large"); .group("p")
makeHeading(b, 2, "large"); .marginTop(px(15))
makeHeading(b, 3, "medium"); .group("h1")
makeHeading(b, 4, "small"); .fontSize("x-large")
makeHeading(b, 5, "x-small"); .fontWeight("bold")
makeHeading(b, 6, "xx-small"); .marginTop(px(10))
makeTBMargins(b, "li p"); .marginBottom(px(10))
makeSingle(b, "td p", "margin-top", "0"); .group("h2")
makeTBMargins(b, "menu li p"); .fontSize("large")
makeZeroMargins(b, "menu li"); .fontWeight("bold")
makeMargins(b, "menu"); .marginTop(px(10))
makeTBMargins(b, "dir li p"); .marginBottom(px(10))
makeZeroMargins(b, "dir li"); .group("h3")
makeMargins(b, "dir"); .fontSize("medium")
makeMargins(b, "dd"); .fontWeight("bold")
makeZeroMargins(b, "dd p"); .marginTop(px(10))
makeTBMargins(b, "dt"); .marginBottom(px(10))
b.append("dl {") .group("h4")
.append("margin-left: 0;") .fontSize("small")
.append("margin-top: 10px;") .fontWeight("bold")
.append("margin-bottom: 10px;") .marginTop(px(10))
.append("}"); .marginBottom(px(10))
makeZeroMargins(b, "ol li"); .group("h5")
b.append("ol {") .fontSize("x-small")
.append("margin-top: 10px;") .fontWeight("bold")
.append("margin-bottom: 10px;") .marginTop(px(10))
.append("margin-left-ltr: 50px;") .marginBottom(px(10))
.append("margin-right-rtl: 50px;") .group("h6")
.append("list-style-type: decimal;") .fontSize("xx-small")
.append("}"); .fontWeight("bold")
makeTBMargins(b, "ol li p"); .marginTop(px(10))
makeZeroMargins(b, "ul li"); .marginBottom(px(10))
b.append("ul {") .group("li p")
.append("margin-top: 10px;") .marginTop(0)
.append("margin-bottom: 10px;") .marginBottom(0)
.append("margin-left-ltr: 50px;") .group("td p")
.append("margin-right-rtl: 50px;") .marginTop(0)
.append("list-style-type: disc;") .group("menu li p")
.append("-bullet-gap: 10px;") .marginTop(0)
.append("}"); .marginBottom(0)
makeZeroMargins(b, "ul li ul li"); .group("menu li")
b.append("ul li ul {") .margin(0)
.append("margin-left-ltr: 25px;") .group("menu")
.append("margin-right-rtl: 25px;") .marginLeftLTR(px(40))
.append("list-style-type: circle;") .marginRightRTL(px(40))
.append("}"); .marginTop(px(10))
makeZeroMargins(b, "ul li ul li ul li"); .marginBottom(px(10))
b.append("ul li menu {") .group("dir li p")
.append("margin-left-ltr: 25px;") .marginTop(0)
.append("margin-right-rtl: 25px;") .marginBottom(0)
.append("list-style-type: circle;") .group("dir li")
.append("}"); .margin(0)
makeTBMargins(b, "ul li p"); .group("dir")
b.append("a {") .marginLeftLTR(px(40))
.append("color: ").append(hex("hyperlink")).append(";") .marginRightRTL(px(40))
.append("text-decoration: underline;") .marginTop(px(10))
.append("}"); .marginBottom(px(10))
b.append("address {") .group("dd")
.append("color: ").append(hex("hyperlink")) .marginLeftLTR(px(40))
.append("font-style: italic;") .marginRightRTL(px(40))
.append("}"); .marginTop(px(10))
makeSingle(b, "big", "font-size", "x-large"); .marginBottom(px(10))
makeSingle(b, "small", "font-size", "x-small"); .group("dd p")
makeMono(b, "samp"); .margin(0)
makeItalic(b, "cite"); .group("dt")
makeMono(b, "code"); .marginTop(0)
makeItalic(b, "dfn"); .marginBottom(0)
makeItalic(b, "em"); .group("dl")
makeItalic(b, "i"); .marginLeft(0)
makeBold(b, "b"); .marginTop(px(10))
makeMono(b, "kbd"); .marginBottom(px(10))
makeSingle(b, "strike", "text-decoration", "line-through"); .group("ol li")
makeSingle(b, "s", "text-decoration", "line-through"); .margin(0)
makeBold(b, "strong"); .group("ol")
makeSingle(b, "sub", "vertical-align", "sub"); .marginTop(px(10))
makeSingle(b, "sup", "vertical-align", "sub"); .marginBottom(px(10))
makeSingle(b, "tt", "font-family", "Monospaced, monospace"); .marginLeftLTR(px(50))
makeSingle(b, "u", "text-decoration", "underline"); .marginRightRTL(px(50))
b.append("var {") .listStyleType("decimal")
.append("font-weight: bold;") .group("ol li p")
.append("font-style: italic;") .marginTop(0)
.append("}"); .marginBottom(0)
b.append("table {") .group("ul li")
.append("border-color: ").append(hex("border")).append(";") .margin(0)
.append("border-style: solid;") .group("ul")
.append("border-width: 1px;") .marginTop(px(10))
.append("border-collapse: collapse;") .marginBottom(px(10))
.append("}"); .marginLeftLTR(px(50))
makeSingle(b, "tr", "text-align", "left"); .marginRightRTL(px(50))
b.append("td {") .listStyleType("disc")
.append("border-color: ").append(hex("border")).append(";") .property("-bullet-gap", px(10))
.append("border-style: none;") .group("ul li ul li")
.append("border-width: 1px;") .margin(0)
.append("padding: 3px;") .group("ul li ul")
.append("border-collapse: collapse;") .marginLeftLTR(px(25))
.append("}"); .marginRightRTL(px(25))
b.append("th {") .listStyleType("circle")
.append("border-color: ").append(hex("border")).append(";") .group("ul li ul li ul li")
.append("border-style: solid;") .margin(0)
.append("border-width: 1px;") .group("ul li menu")
.append("font-weight: bold;") .marginLeftLTR(px(25))
.append("border-collapse: collapse;") .marginRightRTL(px(25))
.append("padding: 3px;") .listStyleType("circle")
.append("}"); .group("ul li p")
makeSingle(b, "blockquote", "margin", "5px 35px"); .marginTop(0)
makeSingle(b, "center", "text-align", "center"); .marginBottom(0)
b.append("pre {") .group("a")
.append("margin-top: 5px;") .color(hex("hyperlink"))
.append("margin-bottom: 5px;") .textDecoration("underline")
.append("font-family: Monospaced, monospace;") .group("address")
.append("}"); .color(hex("hyperlink"))
makeSingle(b, "pre p", "margin-top", "0"); .fontStyle("italic")
b.append("caption {") .group("big")
.append("caption-side: top;") .fontSize("x-large")
.append("text-align: center;") .group("small")
.append("}"); .fontSize("x-small")
makeSingle(b, "nobr", "white-space", "nowrap"); .group("samp")
makeSingle(b, "input", "border", "none"); .fontSize("small")
b.append("div {") .fontFamily("Monospaced", "monospace")
.append("border-color: ").append(hex("borderSecondary")).append(";") .group("code")
.append("border-width: 1px;") .fontSize("small")
.append("}"); .fontFamily("Monospaced", "monospace")
styleSheet.addRule(b.toString()); .group("kbd")
.fontSize("small")
.fontFamily("Monospaced", "monospace")
.group("cite")
.fontStyle("italic")
.group("dfn")
.fontStyle("italic")
.group("em")
.fontStyle("italic")
.group("i")
.fontStyle("italic")
.group("b")
.fontWeight("bold")
.group("strong")
.fontWeight("bold")
.group("strike")
.textDecoration("line-through")
.group("s")
.textDecoration("line-through")
.group("sub")
.property("vertical-align", "sub")
.group("sup")
.property("vertical-align", "sub")
.group("tt")
.fontFamily("Monospaced", "monospace")
.group("u")
.textDecoration("underline")
.group("var")
.fontWeight("bold")
.fontStyle("italic")
.group("table")
.borderColor(hex("border"))
.borderStyle("solid")
.borderWidth(px(1))
.borderCollapse("collapse")
.group("td")
.borderColor(hex("border"))
.borderStyle("none")
.borderWidth(px(1))
.padding(px(3))
.borderCollapse("collapse")
.group("th")
.borderColor(hex("border"))
.borderStyle("solid")
.borderWidth(px(1))
.padding(px(3))
.fontWeight("bold")
.borderCollapse("collapse")
.group("tr")
.property("text-align", "left")
.group("blockquote")
.margin(px(5) + " " + px(35))
.group("center")
.property("text-align", "center")
.group("pre")
.marginTop(px(5))
.marginBottom(px(5))
.fontFamily("Monospaced", "monospace")
.group("pre p")
.marginTop(0)
.group("caption")
.property("caption-side", "top")
.property("text-align", "center")
.group("nobr")
.property("white-space", "nowrap")
.group("input")
.border("none")
.group("div")
.borderColor(hex("borderSecondary"))
.borderWidth(px(1));
// @formatter:on
styleSheet.addRule(builder.toString());
StyleSheet custom = currentTheme.loadStyleSheet(); StyleSheet custom = currentTheme.loadStyleSheet();
if (custom.getStyleNames().hasMoreElements()) { if (custom.getStyleNames().hasMoreElements()) {
styleSheet.addStyleSheet(custom); styleSheet.addStyleSheet(custom);
@ -182,60 +254,17 @@ public class StyleSheetInitTask implements DefaultsInitTask {
new HTMLEditorKit().setStyleSheet(styleSheet); new HTMLEditorKit().setStyleSheet(styleSheet);
} }
protected void makeHeading(final StringBuilder b, final int type, final String size) { protected String hex(final String key) {
b.append("h").append(type).append("{") Color c = defaults.getColor(key);
.append("font-size: ").append(size).append(";") return "#" + ColorUtil.toHex(c);
.append("font-weight: bold;")
.append("margin-top: 10px;")
.append("margin-bottom: 10px;")
.append("}");
}
protected void makeMargins(final StringBuilder b, final String name) {
b.append(name).append(" {")
.append("margin-left-ltr: 40px;")
.append("margin-right-rtl: 40px;")
.append("margin-top: 10px;")
.append("margin-bottom: 10px;")
.append("}");
}
protected void makeTBMargins(final StringBuilder b, final String name) {
b.append(name).append(" {")
.append("margin-top: 0;")
.append("margin-bottom: 0;")
.append("}");
}
protected void makeZeroMargins(final StringBuilder b, final String name) {
makeSingle(b, name, "margin", "0");
}
protected void makeItalic(final StringBuilder b, final String name) {
makeSingle(b, name, "font-style", "italic");
}
protected void makeBold(final StringBuilder b, final String name) {
makeSingle(b, name, "font-weight", "bold");
}
protected void makeSingle(final StringBuilder b, final String name,
final String key, final String value) {
b.append(name).append(" {")
.append(key).append(": ").append(value).append(";")
.append("}");
} }
protected void makeMono(final StringBuilder b, final String name) { protected String px(final int px) {
b.append(name).append(" {") return px + "px";
.append("font-size: small;")
.append("font-family: Monospaced, monospace;")
.append("}");
} }
protected String hex(final String key) { protected String pt(final int pt) {
Color c = defaults.getColor(key); return pt + "pt";
return "#" + ColorUtil.toHex(c);
} }
@Override @Override

Loading…
Cancel
Save