Skip to content

Commit

Permalink
more features
Browse files Browse the repository at this point in the history
-allow multiline text in label text fields (shift+enter for first new line)
-improved label layout with a lot of saved space
-added settings for line-height control (appsettings)
-added fine auto-fontsize control with respect to height (appsettings)
  • Loading branch information
IARI committed Jun 12, 2020
1 parent 5fac92d commit d617806
Show file tree
Hide file tree
Showing 8 changed files with 145 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -159,9 +159,10 @@ class PdfExportController : Controller() {
val col = index.rem(eData.columns)
val row = index.div(eData.columns)
val xOffset = eData.colOffset[col] + eData.offsetH
val yOffset = eData.rowOffset[row] + eData.offsetV
val yOffset = eData.rowOffset.getOrElse(row) { eData.rowOffset.last() } + eData.offsetV
val lWidth = eData.colWidth[col]
val lHeight = eData.rowHeight[row]
val lHeight = eData.rowHeight.getOrElse(row) { eData.rowHeight.last() }
val enableSubtitle = linkedLabel.enableSubTitle.value

rectCanvas(pdfDoc, xOffset, -(yOffset + lHeight), lWidth, lHeight, {
if (data.drawBorder.value) {
Expand Down Expand Up @@ -198,10 +199,10 @@ class PdfExportController : Controller() {
val f = data.font.value.PdfFont
font = f

val lHeightTitlePart = if (linkedLabel.enableSubTitle.value) .6f else 1f
val lHeightTitlePart = if (enableSubtitle) .6f else 1f
renderTextContent(f, linkedLabel.title, lWidth, lHeight * lHeightTitlePart)

if (linkedLabel.enableSubTitle.value) {
if (enableSubtitle) {
renderTextContent(f, linkedLabel.subTitle, lWidth, lHeight * (1 - lHeightTitlePart))
}
}
Expand All @@ -214,22 +215,34 @@ class PdfExportController : Controller() {
textContent: TextContent,
lwidth: Float,
lheight: Float
) = paragraph(textContent.text.value) {
setTextAlignment(TextAlignment.CENTER)
setVerticalAlignment(VerticalAlignment.MIDDLE)
//setSpacingRatio()
setMultipliedLeading(1.05f)
//setBackgroundColor(DeviceRgb.GREEN)
//setBorder(DottedBorder(1f))
fontColor = textContent.color.value

if (textContent.autoSize.value) {
) {
val fontSize = if (textContent.autoSize.value) {
font
//.getBiggestFontSize(textContent.text.value, lwidth * .85f, lheight * 0.8f)
.getBiggestFontSize(textContent.text.value, lwidth * .85f, lheight)
.let(::setFontSize)
} else {
setFontSize(textContent.size.value.toFloat())
.getBiggestFontSize(
textContent.text.value,
lwidth * .85f,
appSettings.labelFontheightFraction.floatValue() * lheight
)
} else textContent.size.value.toFloat()

renderTextContent(fontSize, textContent, textContent.text.value)
}

fun BlockElement<*>.renderTextContent(
fontSize: Float,
textContent: TextContent,
text: String
) {
paragraph(text) {
setTextAlignment(TextAlignment.CENTER)
setVerticalAlignment(VerticalAlignment.MIDDLE)
//setSpacingRatio()
setMultipliedLeading(appSettings.multipliedLeading.floatValue())
//setBackgroundColor(DeviceRgb.GREEN)
//setBorder(DottedBorder(1f))
fontColor = textContent.color.value
setFontSize(fontSize)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,24 @@ class AppSettings {

@ContextualSerialization
val windowWidth = SimpleDoubleProperty(800.0)

@ContextualSerialization
val windowHeight = SimpleDoubleProperty(600.0)

@ContextualSerialization
val windowX = SimpleDoubleProperty(100.0)

@ContextualSerialization
val windowY = SimpleDoubleProperty(100.0)

@ContextualSerialization
val windowMaximized = SimpleBooleanProperty(false)

@ContextualSerialization
val labelFontheightFraction = SimpleDoubleProperty(0.73)

@ContextualSerialization
val multipliedLeading = SimpleDoubleProperty(1.0)
}

class AppSettingsModel : ItemViewModel<AppSettings>() {
Expand All @@ -56,4 +65,8 @@ class AppSettingsModel : ItemViewModel<AppSettings>() {
val windowY = bind<Number, SimpleDoubleProperty, SimpleDoubleProperty>(AppSettings::windowY, true)
val windowMaximized =
bind<Boolean, SimpleBooleanProperty, SimpleBooleanProperty>(AppSettings::windowMaximized, true)
val labelFontheightFraction =
bind<Number, SimpleDoubleProperty, SimpleDoubleProperty>(AppSettings::labelFontheightFraction, true)
val multipliedLeading =
bind<Number, SimpleDoubleProperty, SimpleDoubleProperty>(AppSettings::multipliedLeading, true)
}
Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@
package com.julianjarecki.ettiketten.app.data

import com.julianjarecki.tfxserializer.fxproperties.ColorProperty
import com.julianjarecki.tfxserializer.utils.bindCount
import javafx.beans.property.*
import kotlinx.serialization.ContextualSerialization
import kotlinx.serialization.Serializable
import tornadofx.ItemViewModel
import tornadofx.*

@Serializable
class TextContent {
@ContextualSerialization
val text = SimpleStringProperty("")

@ContextualSerialization
val autoSize = SimpleBooleanProperty(true)

@ContextualSerialization
val size = SimpleDoubleProperty(12.0)
val color = ColorProperty()
Expand All @@ -24,6 +27,13 @@ class TextContent {

class TextContentModel : ItemViewModel<TextContent>() {
val text = bind(TextContent::text, true)
val lines = observableListOf<String>().apply {
text.onChange {
if (it == null) clear()
else setAll(it.split("\n"))
}
}
val multiline = lines.sizeProperty.ge(2)
val autoSize = bind<Boolean, BooleanProperty, SimpleBooleanProperty>(TextContent::autoSize, true)
val size = bind<Number, SimpleDoubleProperty, SimpleDoubleProperty>(TextContent::size, true)
val color = bind(TextContent::color, true)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -167,12 +167,16 @@ enum class Fonts(val fontName: String) {


fun PdfFont.getBiggestFontSize(text: String, availableWidth: Float, availableHeight: Float): Float {
val fontSizeWidth = (availableWidth / (getContentWidth(PdfString(text)) * getFontMatrix()[0]))
val lines = text.split("\n")
val longestLine = lines.maxBy { it.length }!!
val fontSizeWidth = (availableWidth / (getContentWidth(longestLine) * getFontMatrix()[0]))
//val fontSizeHeight = (100f * availableHeight / getHeight(text, 100f))
val fontSizeHeight = availableHeight
val fontSizeHeight = availableHeight / lines.size
return min(fontSizeWidth.toFloat(), fontSizeHeight)
}

fun PdfFont.getContentWidth(text: String) = getContentWidth(PdfString(text))


fun PdfFont.getHeight(text: String, fontSize: Float = 12f) = getAscent(text, fontSize) - getDescent(text, fontSize)

Expand Down
18 changes: 18 additions & 0 deletions src/main/kotlin/com/julianjarecki/ettiketten/styles/Styles.kt
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ class Styles : Stylesheet() {
val noarrow by cssclass()
val gridLine by cssclass()
val gridLineList by cssclass()
val miniButton by cssclass()
}

init {
Expand All @@ -42,6 +43,14 @@ class Styles : Stylesheet() {
effect = DropShadow(5.0, Color.GRAY)
}

miniButton {
padding = box(0.px, 2.px)
backgroundInsets += box(0.px)
and(selected) {
backgroundColor += Color.LIGHTGREEN
}
}

somepadding {
padding = box(6.px)
}
Expand All @@ -54,6 +63,15 @@ class Styles : Stylesheet() {
borderWidth += box(1.px)
borderStyle += BorderStrokeStyle.SOLID
borderColor += box(Color.LIGHTGRAY)
contains(spinner) {
padding = box(0.px)
contains(s(incrementArrowButton, decrementArrowButton, textInput)) {
padding = box(0.px, 2.px)
}
contains(textInput) {
backgroundColor += Color.ALICEBLUE
}
}
}

gridLine {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,16 @@ class LabelContentFragment : DataGridCellFragment<LabelContent>() {
//visibleWhen(labelContent.linkedTo.isEmpty)
textContentFragment(linked.title)

checkbox("Subtitle", linked.enableSubTitle)

textContentFragment(linked.subTitle) {
//root.enableWhen(content.enableSubTitle)
root.hiddenWhen(!linked.enableSubTitle)
hbox {
//checkbox("Subtitle", linked.enableSubTitle)
togglebutton("s\nu\nb") {
addClass(Styles.miniButton)
selectedProperty().bindBidirectional(linked.enableSubTitle)
}
textContentFragment(linked.subTitle) {
//root.enableWhen(content.enableSubTitle)
root.hiddenWhen(!linked.enableSubTitle)
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,34 +2,64 @@ package com.julianjarecki.ettiketten.view.fragments

import com.julianjarecki.ettiketten.app.data.TextContent
import com.julianjarecki.ettiketten.app.data.TextContentModel
import com.julianjarecki.ettiketten.styles.Styles
import com.julianjarecki.tfxserializer.utils.colorpicker
import javafx.beans.value.ObservableValue
import javafx.event.EventTarget
import javafx.scene.input.KeyCode
import javafx.scene.input.KeyEvent
import tornadofx.*

class TextContentFragment : ItemFragment<TextContent>() {
val textContent = TextContentModel().bindTo(this)

override val root = vbox {
hbox {
textfield(textContent.text)
textfield(textContent.text) {
removeWhen(textContent.multiline)
whenVisible {
requestFocus()
}

this.addEventFilter(KeyEvent.KEY_PRESSED) { ev ->
if (ev.isShortcutDown || ev.isShiftDown) {
when (ev.code) {
KeyCode.ENTER -> {
textContent.text.value += "\n"
}
else -> {
}
}
}
}
}
textarea(textContent.text) {
minHeight = 30.0
removeWhen(!textContent.multiline)
whenVisible {
requestFocus()
positionCaret(textContent.text.value.length)
}
}
colorpicker(textContent.color, ColorPickerMode.MenuButton) {
prefWidth = 20.0
prefWidth = 21.0
}
}
hbox {
checkbox("auto", textContent.autoSize)
//checkbox("auto", textContent.autoSize)
togglebutton("auto") {
addClass(Styles.miniButton)
selectedProperty().bindBidirectional(textContent.autoSize)
}
spacer()
spinner(5.0, 100.0, amountToStepBy = 0.25, editable = true, property = textContent.size, enableScroll = true) {
spinner(
5.0, 100.0, amountToStepBy = 0.25, editable = true,
property = textContent.size, enableScroll = true
) {
prefWidth = 64.0
disableWhen(textContent.autoSize)
}
/*
textfield(textContent.size) {
disableWhen(textContent.autoSize)
makeDoublefield()
}
*/

}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,22 @@ class SettingsTab : AppTab("Settings", MaterialDesignIcon.SETTINGS.view) {
combobox(appPreferences.defaultUnits, Units.values().toList()) { }
}
}
fieldset("Rendering") {
field("auto-height factor") {
tooltip("Fraction of Labelheight that determines auto-fontheight")
spinner(
.01, 1.2, amountToStepBy = .01, editable = true,
enableScroll = true, property = appPreferences.labelFontheightFraction
)
}
field("line height") {
tooltip("multiple of the line-height that determines the distance between two lines")
spinner(
.05, 5.0, amountToStepBy = .05, editable = true,
enableScroll = true, property = appPreferences.multipliedLeading
)
}
}
fieldset("Export") {
field("Open Document after Export") {
checkbox("", appPreferences.openDocumentAfterExport)
Expand Down

0 comments on commit d617806

Please sign in to comment.