watermark
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © Ness92

//@version=5
indicator("watermark", overlay=true)

/////////////////////////////////////////////////
//                  FONTS
/////////////////////////////////////////////////
//{
var FONT1 = array.new<string>(0)
var FONT2 = array.new<string>(0)

if barstate.isfirst
    array.push(FONT1, "░█████╗░,██████╗░,░█████╗░,██████╗░,███████╗,███████╗,░██████╗░,██╗░░██╗,██╗,░░░░░██╗,██╗░░██╗,██╗░░░░░,███╗░░░███╗,███╗░░██╗,░█████╗░,██████╗░,░██████╗░,██████╗░,░██████╗,████████╗,██╗░░░██╗,██╗░░░██╗,░██╗░░░░░░░██╗,██╗░░██╗,██╗░░░██╗,███████╗,░█████╗░,░░███╗░░,██████╗░,██████╗░,░░██╗██╗,███████╗,░█████╗░,███████╗,░█████╗░,░█████╗░,░░░░")
    array.push(FONT1, "██╔══██╗,██╔══██╗,██╔══██╗,██╔══██╗,██╔════╝,██╔════╝,██╔════╝░,██║░░██║,██║,░░░░░██║,██║░██╔╝,██║░░░░░,████╗░████║,████╗░██║,██╔══██╗,██╔══██╗,██╔═══██╗,██╔══██╗,██╔════╝,╚══██╔══╝,██║░░░██║,██║░░░██║,░██║░░██╗░░██║,╚██╗██╔╝,╚██╗░██╔╝,╚════██║,██╔══██╗,░████║░░,╚════██╗,╚════██╗,░██╔╝██║,██╔════╝,██╔═══╝░,╚════██║,██╔══██╗,██╔══██╗,░░░░")
    array.push(FONT1, "███████║,██████╦╝,██║░░╚═╝,██║░░██║,█████╗░░,█████╗░░,██║░░██╗░,███████║,██║,░░░░░██║,█████═╝░,██║░░░░░,██╔████╔██║,██╔██╗██║,██║░░██║,██████╔╝,██║██╗██║,██████╔╝,╚█████╗░,░░░██║░░░,██║░░░██║,╚██╗░██╔╝,░╚██╗████╗██╔╝,░╚███╔╝░,░╚████╔╝░,░░███╔═╝,██║░░██║,██╔██║░░,░░███╔═╝,░█████╔╝,██╔╝░██║,██████╗░,██████╗░,░░░░██╔╝,╚█████╔╝,╚██████║,░░░░")
    array.push(FONT1, "██╔══██║,██╔══██╗,██║░░██╗,██║░░██║,██╔══╝░░,██╔══╝░░,██║░░╚██╗,██╔══██║,██║,██╗░░██║,██╔═██╗░,██║░░░░░,██║╚██╔╝██║,██║╚████║,██║░░██║,██╔═══╝░,╚██████╔╝,██╔══██╗,░╚═══██╗,░░░██║░░░,██║░░░██║,░╚████╔╝░,░░████╔═████║░,░██╔██╗░,░░╚██╔╝░░,██╔══╝░░,██║░░██║,╚═╝██║░░,██╔══╝░░,░╚═══██╗,███████║,╚════██╗,██╔══██╗,░░░██╔╝░,██╔══██╗,░╚═══██║,░░░░")
    array.push(FONT1, "██║░░██║,██████╦╝,╚█████╔╝,██████╔╝,███████╗,██║░░░░░,╚██████╔╝,██║░░██║,██║,╚█████╔╝,██║░╚██╗,███████╗,██║░╚═╝░██║,██║░╚███║,╚█████╔╝,██║░░░░░,░╚═██╔═╝░,██║░░██║,██████╔╝,░░░██║░░░,╚██████╔╝,░░╚██╔╝░░,░░╚██╔╝░╚██╔╝░,██╔╝╚██╗,░░░██║░░░,███████╗,╚█████╔╝,███████╗,███████╗,██████╔╝,╚════██║,██████╔╝,╚█████╔╝,░░██╔╝░░,╚█████╔╝,░█████╔╝,░░░░")
    array.push(FONT1, "╚═╝░░╚═╝,╚═════╝░,░╚════╝░,╚═════╝░,╚══════╝,╚═╝░░░░░,░╚═════╝░,╚═╝░░╚═╝,╚═╝,░╚════╝░,╚═╝░░╚═╝,╚══════╝,╚═╝░░░░░╚═╝,╚═╝░░╚══╝,░╚════╝░,╚═╝░░░░░,░░░╚═╝░░░,╚═╝░░╚═╝,╚═════╝░,░░░╚═╝░░░,░╚═════╝░,░░░╚═╝░░░,░░░╚═╝░░░╚═╝░░,╚═╝░░╚═╝,░░░╚═╝░░░,╚══════╝,░╚════╝░,╚══════╝,╚══════╝,╚═════╝░,░░░░░╚═╝,╚═════╝░,░╚════╝░,░░╚═╝░░░,░╚════╝░,░╚════╝░,░░░░")
    
    array.push(FONT2, "╭━━━╮,╭━━╮╱,╭━━━╮,╭━━━╮,╭━━━╮,╭━━━╮,╭━━━╮,╭╮╱╭╮,╭━━╮,╱╱╭╮,╭╮╭━╮,╭╮╱╱╱,╭━╮╭━╮,╭━╮╱╭╮,╭━━━╮,╭━━━╮,╭━━━╮,╭━━━╮,╭━━━╮,╭━━━━╮,╭╮╱╭╮,╭╮╱╱╭╮,╭╮╭╮╭╮,╭━╮╭━╮,╭╮╱╱╭╮,╭━━━━╮,╭━━━╮,╱╭╮╱,╭━━━╮,╭━━━╮,╭╮╱╭╮,╭━━━╮,╭━━━╮,╭━━━╮,╭━━━╮,╭━━━╮,╱╱")
    array.push(FONT2, "┃╭━╮┃,┃╭╮┃╱,┃╭━╮┃,╰╮╭╮┃,┃╭━━╯,┃╭━━╯,┃╭━╮┃,┃┃╱┃┃,╰┫┣╯,╱╱┃┃,┃┃┃╭╯,┃┃╱╱╱,┃┃╰╯┃┃,┃┃╰╮┃┃,┃╭━╮┃,┃╭━╮┃,┃╭━╮┃,┃╭━╮┃,┃╭━╮┃,┃╭╮╭╮┃,┃┃╱┃┃,┃╰╮╭╯┃,┃┃┃┃┃┃,╰╮╰╯╭╯,┃╰╮╭╯┃,╰━━╮━┃,┃╭━╮┃,╭╯┃╱,┃╭━╮┃,┃╭━╮┃,┃┃╱┃┃,┃╭━━╯,┃╭━━╯,┃╭━╮┃,┃╭━╮┃,┃╭━╮┃,╱╱")
    array.push(FONT2, "┃┃╱┃┃,┃╰╯╰╮,┃┃╱╰╯,╱┃┃┃┃,┃╰━━╮,┃╰━━╮,┃┃╱╰╯,┃╰━╯┃,╱┃┃╱,╱╱┃┃,┃╰╯╯╱,┃┃╱╱╱,┃╭╮╭╮┃,┃╭╮╰╯┃,┃┃╱┃┃,┃╰━╯┃,┃┃╱┃┃,┃╰━╯┃,┃╰━━╮,╰╯┃┃╰╯,┃┃╱┃┃,╰╮┃┃╭╯,┃┃┃┃┃┃,╱╰╮╭╯╱,╰╮╰╯╭╯,╱╱╭╯╭╯,┃┃┃┃┃,╰╮┃╱,╰╯╭╯┃,╰╯╭╯┃,┃╰━╯┃,┃╰━━╮,┃╰━━╮,╰╯╭╯┃,┃╰━╯┃,┃╰━╯┃,╱╱")
    array.push(FONT2, "┃╰━╯┃,┃╭━╮┃,┃┃╱╭╮,╱┃┃┃┃,┃╭━━╯,┃╭━━╯,┃┃╭━╮,┃╭━╮┃,╱┃┃╱,╭╮┃┃,┃╭╮┃╱,┃┃╱╭╮,┃┃┃┃┃┃,┃┃╰╮┃┃,┃┃╱┃┃,┃╭━━╯,┃╰━╯┃,┃╭╮╭╯,╰━━╮┃,╱╱┃┃╱╱,┃┃╱┃┃,╱┃╰╯┃╱,┃╰╯╰╯┃,╱╭╯╰╮╱,╱╰╮╭╯╱,╱╭╯╭╯╱,┃┃┃┃┃,╱┃┃╱,╭━╯╭╯,╭╮╰╮┃,╰━━╮┃,╰━━╮┃,┃╭━╮┃,╱╱┃╭╯,┃╭━╮┃,╰━━╮┃,╱╱")
    array.push(FONT2, "┃╭━╮┃,┃╰━╯┃,┃╰━╯┃,╭╯╰╯┃,┃╰━━╮,┃┃╱╱╱,┃╰┻━┃,┃┃╱┃┃,╭┫┣╮,┃╰╯┃,┃┃┃╰╮,┃╰━╯┃,┃┃┃┃┃┃,┃┃╱┃┃┃,┃╰━╯┃,┃┃╱╱╱,╰━━╮┃,┃┃┃╰╮,┃╰━╯┃,╱╱┃┃╱╱,┃╰━╯┃,╱╰╮╭╯╱,╰╮╭╮╭╯,╭╯╭╮╰╮,╱╱┃┃╱╱,╭╯━┻━╮,┃╰━╯┃,╭╯╰╮,┃━━┻╮,┃╰━╯┃,╱╱╱┃┃,╭━━╯┃,┃╰━╯┃,╱╱┃┃╱,┃╰━╯┃,╭━━╯┃,╱╱")
    array.push(FONT2, "╰╯╱╰╯,╰━━━╯,╰━━━╯,╰━━━╯,╰━━━╯,╰╯╱╱╱,╰━━━╯,╰╯╱╰╯,╰━━╯,╰━━╯,╰╯╰━╯,╰━━━╯,╰╯╰╯╰╯,╰╯╱╰━╯,╰━━━╯,╰╯╱╱╱,╱╱╱╰╯,╰╯╰━╯,╰━━━╯,╱╱╰╯╱╱,╰━━━╯,╱╱╰╯╱╱,╱╰╯╰╯╱,╰━╯╰━╯,╱╱╰╯╱╱,╰━━━━╯,╰━━━╯,╰━━╯,╰━━━╯,╰━━━╯,╱╱╱╰╯,╰━━━╯,╰━━━╯,╱╱╰╯╱,╰━━━╯,╰━━━╯,╱╱")
    
var FONT_REF = array.from("A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z","0","1","2","3","4","5","6","7","8","9"," ")
//}
/////////////////////////////////////////////////
//                 SETTINGS
/////////////////////////////////////////////////
//{
WATERMARK_TEXT      = input.string("YOUR TEXT", "Text")
WATERMARK_STYLE     = input.string("Type1", "Style", options=["Type1","Type2"])
WATERMARK_COLOR     = input.color(color.new(color.gray,85), "Color")
WATERMARK_SIZE_OPT  = input.string("Normal", "Size", options=["Huge","Large","Normal","Small","Tiny"])
WATERMARK_POS_OPT   = input.string("Middle-Center", "Position", options=["Bottom-Center","Bottom-Left","Bottom-Right","Middle-Center","Middle-Left","Middle-Right","Top-Center","Top-Left","Top-Right"])
WATERMARK_REP_ROW   = input.int(1, "Repetition: Row ", 1, inline="repetition")
WATERMARK_REP_COL   = input.int(1, "Col ", 1, inline="repetition")
WATERMARK_SPACING_H = input.int(1, "Spacing: H", 1, inline="spacing")
WATERMARK_SPACING_V = input.int(1, "V", 1, inline="spacing")
WATERMARK_FRAME     = input.bool(true, "Enable frame")

WATERMARK_SIZE = switch WATERMARK_SIZE_OPT
    "Huge"   => size.huge
    "Large"  => size.large
    "Normal" => size.normal
    "Small"  => size.small
    "Tiny"   => size.tiny

WATERMARK_POS = switch WATERMARK_POS_OPT
    "Bottom-Center" => position.bottom_center
    "Bottom-Left"   => position.bottom_left
    "Bottom-Right"  => position.bottom_right
    "Middle-Center" => position.middle_center
    "Middle-Left"   => position.middle_left
    "Middle-Right"  => position.middle_right
    "Top-Center"    => position.top_center
    "Top-Left"      => position.top_left
    "Top-Right"     => position.top_right

FONT = switch WATERMARK_STYLE
    "Type1" => FONT1
    "Type2" => FONT2
    
    
CHAR_FRAME_TOP          = ( WATERMARK_FRAME == false )? "" : ( WATERMARK_STYLE == "Type1" )? "▀" : "━"
CHAR_FRAME_TOP_LEFT     = ( WATERMARK_FRAME == false )? "" : ( WATERMARK_STYLE == "Type1" )? "▛" : "┏"
CHAR_FRAME_TOP_RIGHT    = ( WATERMARK_FRAME == false )? "" : ( WATERMARK_STYLE == "Type1" )? "▜" : "┓"

CHAR_FRAME_BOTTOM       = ( WATERMARK_FRAME == false )? "" : ( WATERMARK_STYLE == "Type1" )? "▄" : "━"
CHAR_FRAME_BOTTOM_LEFT  = ( WATERMARK_FRAME == false )? "" : ( WATERMARK_STYLE == "Type1" )? "▙" : "┗"
CHAR_FRAME_BOTTOM_RIGHT = ( WATERMARK_FRAME == false )? "" : ( WATERMARK_STYLE == "Type1" )? "▟" : "┛"

CHAR_FRAME_LEFT         = ( WATERMARK_FRAME == false )? "" : ( WATERMARK_STYLE == "Type1" )? "▌" : "┃"
CHAR_FRAME_RIGHT        = ( WATERMARK_FRAME == false )? "" : ( WATERMARK_STYLE == "Type1" )? "▐" : "┃"


//}
/////////////////////////////////////////////////
//                  MAIN
/////////////////////////////////////////////////
var watermark_table = table.new(WATERMARK_POS, WATERMARK_REP_COL, WATERMARK_REP_ROW, na, na)

gen_watermark(message) =>
    msg = str.split(str.upper(message), "")
    idx_list = array.new<int>(0)
    for ii=0 to array.size(msg)-1
        for jj=0 to array.size(FONT_REF)-1
            if array.get(msg, ii) == array.get(FONT_REF, jj)
                array.push(idx_list, jj)
                break
    
    spacing_h = ""
    if (WATERMARK_REP_COL > 1)
        for jj=0 to WATERMARK_SPACING_H-1 
            spacing_h := spacing_h + "\t"
    
    watermark_width = 0
    watermark_msg = ""
    for ii=0 to array.size(FONT)-1
        watermark_msg := watermark_msg + spacing_h + CHAR_FRAME_LEFT
        font_line = str.split(array.get(FONT,ii), ",")
        for jj=0 to array.size(idx_list)-1
            idx = array.get(idx_list, jj)
            watermark_msg := watermark_msg + array.get(font_line, idx)
            watermark_width := ( ii == 0 )? watermark_width + str.length(array.get(font_line, idx)) : watermark_width
        watermark_msg := watermark_msg + CHAR_FRAME_RIGHT + spacing_h + "\n"
    
    if( WATERMARK_FRAME == true )
        frame_top    = spacing_h + CHAR_FRAME_TOP_LEFT
        frame_bottom = spacing_h + CHAR_FRAME_BOTTOM_LEFT
    
        for ii=0 to watermark_width-1
            frame_top    := frame_top    + CHAR_FRAME_TOP
            frame_bottom := frame_bottom + CHAR_FRAME_BOTTOM
            
        frame_top    := frame_top    + CHAR_FRAME_TOP_RIGHT
        frame_bottom := frame_bottom + CHAR_FRAME_BOTTOM_RIGHT
                
        watermark_msg := frame_top + "\n" + watermark_msg + frame_bottom
        
    for mm=0 to WATERMARK_REP_COL-1
        for nn=0 to WATERMARK_REP_ROW-1
            spacing_v = ""
            if (WATERMARK_REP_ROW > 1) and (nn < WATERMARK_REP_ROW-1)
                for jj=0 to WATERMARK_SPACING_V-1
                    spacing_v := spacing_v + "\n"
            table.cell(watermark_table, mm, nn, watermark_msg+spacing_v, text_color=WATERMARK_COLOR, text_halign=text.align_left, text_size=WATERMARK_SIZE)
 
if barstate.islastconfirmedhistory
    gen_watermark(WATERMARK_TEXT)
