All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.docops.asciidoc.buttons.ButtonMaker.kt Maven / Gradle / Ivy

There is a newer version: 2023.20
Show newest version
/*
 * Copyright 2020 The DocOps Consortium
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package io.docops.asciidoc.buttons

import io.docops.asciidoc.buttons.models.Button
import io.docops.asciidoc.buttons.theme.Theme


abstract class ButtonMaker {
    abstract fun makeButtons(buttons: MutableList>, theme: Theme): String
    protected val types = mutableMapOf()
    private var startLegendHeight = 0
    private fun height(
        buttons: MutableList>,
        heightFactor: Int,
        defaultHeight: Int,
        theme: Theme
    ): Int {
        var height = defaultHeight
        if (buttons.size > 1) {
            height = heightFactor * buttons.size
        }
        if (theme.legendOn) {
            startLegendHeight = height + 30
            val unique = buttonTypes(buttons = buttons, theme = theme)
            height += 20 * (unique.size)
            height += 20
        }
        return height
    }

    private fun buttonTypes(buttons: MutableList>, theme: Theme): MutableMap {
        buttons.forEach {
            it.forEach { btn ->
                types[theme.buttonColor(btn)] = (btn.type)
            }
        }
        return types
    }

    fun makeSvgHead(
        buttons: MutableList>,
        heightFactor: Int = 170,
        defaultHeight: Int = 250,
        widthFactor: Int = 100,
        theme: Theme
    ): String {
        val height = height(buttons, heightFactor = heightFactor, defaultHeight = defaultHeight, theme = theme)
        val maxWidth = theme.columns * widthFactor + theme.columns * 10
        // language=svg
        return """
                """
    }

    fun makeDefs(buttons: MutableList>, theme: Theme): String {
        var linear = ""
        theme.gradientStyle?.let { linear = it.gradientIdToXml() }

        val btnDef = StringBuilder("")
        buttons.forEach {
            btn -> btn.forEach {
                btnDef.append(theme.buildGradientDef(it))
             }
        }
        // language=svg
        return """
        
        
            
            
            
                
            
            
                
                
            
        
        
            
            
        
        
            
            

            
            

            
            

            
            

            
            
                
                
            
        
        
            
            
        
        
            
            
        
        
            
            
        
        
            
            
        
        
            
            
        
        
            
            
        
        
            
            
            
        
        
            
            
            
        
        
            
            
            
        
        
            
            
            
        
        $linear
        $btnDef
        
        
        
        
            
        
        
        
        ${theme.defs}
    
    """
    }

    fun makeSvgEnd(): String {
        return ""
    }



    fun drawLegend(types: MutableMap): String {
        val sb = StringBuilder("")
        var recXpos = "10"
        var textXPos = "30"
        var yPos = startLegendHeight - 20
        val size = types.size/2

        var count = 0

        types.forEach { (color, desc) ->
            if(count == size) {
                recXpos = "50%"
                yPos = startLegendHeight - 20
                textXPos = "54%"
            }
            // language=svg
            sb.append(
                """
                
                $desc
            """.trimIndent()
            )
            yPos += 20
            count++
        }
        sb.append("")
        return sb.toString()
    }


}





© 2015 - 2024 Weber Informatics LLC | Privacy Policy