Skip to content

Commit

Permalink
v2.0 - Font customisation - #5
Browse files Browse the repository at this point in the history
  • Loading branch information
1337kid committed Dec 20, 2023
2 parents bf84fe2 + c440518 commit 1e71d89
Show file tree
Hide file tree
Showing 21 changed files with 133 additions and 30 deletions.
31 changes: 25 additions & 6 deletions NoiceGRUB.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,38 @@
console.print(table)
richprint("[cyan bold][ 0 ][/cyan bold] [green bold]Instructions for creating a preset[/green bold]")

choice = IntPrompt.ask("[yellow]Choice[/yellow]",choices=[str(i) for i in range(len(os.listdir('presets'))+1)])
if choice==0:
choice = IntPrompt.ask("[yellow]Choice[/yellow]",choices=[str(i) for i in range(len(preset_dict)+1)])
if choice == 0:
console.print(Markdown(open('noicegrub/createpreset.md').read()))
exit()

print()
preset = get_preset(preset_dict[choice])
table = preset_info_table(preset)
console.print(table)
font_file = preset[1]['font_family']
if not os.path.exists(f'./fonts/{font_file}'):
richprint(f'[red bold][Error] {font_file} is not present in ./fonts/ [/red bold]')
exit()
choice = Confirm.ask("[yellow bold]Would you like to customise the font ?[/yellow bold]")
if choice:
table,fonts = font_table()
console.print(table)
font_choice = IntPrompt.ask("[yellow]Choice[/yellow]",choices=[str(i) for i in range(1,len(fonts)+1)])
preset[1]['font_family'] = fonts[font_choice]
font_size = IntPrompt.ask("[yellow]Font size in pixels (integer) [/yellow]")
preset[1]['selection_font'] = font_size

if preset[0]=='Kewl': NgCommonProps(preset).export_theme()
elif preset[0]=='Noice': NgNoiceTemplate(preset).export_theme()
elif preset[0]=='TheMan': NgTheManTemplate(preset).export_theme()
elif preset[0]=='Mountains': NgMountainsTemplate(preset).export_theme()
elif preset[0]=='Nico': NgNicoTemplate(preset).export_theme()

richprint('\n[green bold]Executing scripts/install.sh ... [/green bold]')
os.system('chmod +x ./scripts/install.sh')
os.system('sudo ./scripts/install.sh')
richprint('[green bold]Thankyou for using NoiceGRUB[/green bold]')
richprint('\n[cyan bold]Generated theme has been placed in ./export/ [/cyan bold]')
choice = Confirm.ask("[yellow bold]Would you like to install the theme ?[/yellow bold]")
if choice:
richprint('\n[green bold]Executing scripts/install.sh ... [/green bold]')
os.system('chmod +x ./scripts/install.sh')
os.system('sudo ./scripts/install.sh')
richprint('[green bold]Done[/green bold]')
7 changes: 5 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# NoiceGRUB
#### NoiceGRUB is a collection of linear gradient based GRUB2 themes that can be customised. By default NoiceGRUB comes with 4 templates and 8 presets.
#### NoiceGRUB is a collection of linear gradient based GRUB2 themes that can be customised. By default NoiceGRUB comes with 5 templates and 10 presets.
# Contents
* [**Installation**][1]
* [**Presets**][2]
Expand All @@ -18,7 +18,8 @@ python3 NoiceGRUB.py
```
## [Presets][2]
### [Create Your Own Preset][3]
NoiceGRUB preset files have the syntax of TOML and ends with `.toml` extension. Your own preset shall be placed in `presets` folder. It will be shown in the preset menu when you execute `NoiceGRUB.py`
NoiceGRUB preset files have the syntax of TOML and ends with `.toml` extension. Your own preset shall be placed in `presets` folder. It will be shown in the preset menu when you execute `NoiceGRUB.py` <br/><br/>
Fonts are placed in `fonts` folder. User defined fonts should be place in this folder to be recognised by NoiceGRUB
#### File format
```toml
template='template name here'
Expand All @@ -30,6 +31,8 @@ secondary="secondary gradient colour"
header_font_colour = "header font colour or 'BOOTMENU' text colour"
footer_font_colour = "footer font colour or GRUB keymap text colour"
selection_bg_colour = "backgound colour of selection highlighter"
selection_font = "font size in pixels (integer)"
font_family = ".ttf or .otf font filename"

# This section defines theme.txt file and is mandatory too
[theme]
Expand Down
Binary file added fonts/InterRegular.ttf
Binary file not shown.
Binary file added fonts/NicoMojiRegular.ttf
Binary file not shown.
Binary file added fonts/Unifont.otf
Binary file not shown.
Binary file modified img/screenshot.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
32 changes: 30 additions & 2 deletions noicegrub/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@ def get_preset(name):
data=toml.load(open(f'./presets/{name}.toml'))
return [data[i] for i in data]


#================= rich tables
def preset_table():
preset_dict={}
files=os.listdir('presets')
files.sort()
table = Table(title="Available Presets")
table.add_column("No", justify="right", style="cyan", no_wrap=True)
table.add_column("No.", justify="right", style="cyan", no_wrap=True)
table.add_column("Preset", justify="left", style="green", no_wrap=True)
table.add_column("Template", justify="left", style="green", no_wrap=True)
presets=[{i.split('.')[0]:toml.load(open(f'presets/{i}'))['template']} for i in files]
Expand All @@ -20,6 +22,32 @@ def preset_table():
preset_dict[presets.index(i)+1]=preset
return table,preset_dict

def preset_info_table(preset_conf):
table = Table(title="Selected Preset Info")
table.add_column("Template",justify="left", style="cyan", no_wrap=True)
table.add_column("Font Family",justify="left", style="green", no_wrap=True)
table.add_column("Size (pixels)",justify="left", style="green", no_wrap=True)
table.add_row(preset_conf[0],preset_conf[1]['font_family'],preset_conf[1]['selection_font'])
return table

def font_table():
print()
fonts={}
files=os.listdir('fonts')
table = Table(title="Available Fonts")
table.add_column("No.",justify="right", style="cyan", no_wrap=True)
table.add_column("Font Family",justify="left", style="green", no_wrap=True)
for i in range(len(files)):
fonts[i+1]=files[i]
table.add_row(str(i+1),files[i])
return table,fonts

#=================

def gen_font_name(font_name,size):
font_name=font_name.split('.')
return f"export/{font_name[0]}.{size}.{font_name[1]}"

banner = '''
o o o .oPYo. .oPYo. o o .oPYo.
8b 8 8 8 8 `8 8 8 8 `8
Expand All @@ -28,4 +56,4 @@ def preset_table():
8 `b8 8 8 8 8 . 8. 8 8 8 8 8 8 8 8
8 `8 `YooP' 8 `YooP' `Yooo' `YooP8 8 8 `YooP' 8oooP'
..:::..:.....::..:.....::.....::....8 :..:::..:.....::......:
:::::::: @1337kid ::::::::::::::::::8 ::::::: v1.8.3 ::::::::\n'''
:::::::: @1337kid ::::::::::::::::::8 :::::::: v2.0 :::::::::\n'''
32 changes: 21 additions & 11 deletions noicegrub/template/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import os
import os,shutil
import xmltodict
import cairosvg
from noicegrub.templateconf import header_footer_placement,item_placement
from noicegrub.templateconf import header_footer_placement,item_placement,calc_selection_scale
from noicegrub import gen_font_name

class NgCommonProps():
def __init__(self,preset):
Expand All @@ -27,8 +28,10 @@ def set_common_props(self):
self.svg_temp['svg']['defs']['linearGradient'][0]['stop'][1]['@stop-color']=self.common_props['secondary']

def export_theme(self):
if not os.path.exists('export'): os.mkdir('export')
if os.path.exists('export'): shutil.rmtree('export')
os.mkdir('export')
#============ background.png

open('temp.svg','w').write(xmltodict.unparse(self.svg_temp,pretty=True))
'''
When unparsing, xmltodict changes the order of elements.
Expand All @@ -42,18 +45,25 @@ def export_theme(self):
png=cairosvg.svg2png(url='temp.svg')
open('./export/background.png','wb').write(png)
os.remove('temp.svg')
#============ menu box selection background colour
filenames=['select_c','select_e','select_w']

#============ menu box selection background colour & font

height,new_scale = calc_selection_scale(self.common_props['selection_font'])
filenames = ['select_c','select_e','select_w']
for i in filenames:
svg_temp=xmltodict.parse(open(f'template/{i}.svg').read())
svg_temp = xmltodict.parse(open(f'template/{i}.svg').read())
svg_temp['svg']['path']['@fill']=self.common_props['selection_bg_colour']
open('temp.svg','w').write(xmltodict.unparse(svg_temp))
cairosvg.svg2png(url='temp.svg', write_to=f'./export/{i}.png')
cairosvg.svg2png(url='temp.svg', write_to=f'./export/{i}.png',scale=new_scale)
os.remove('temp.svg')
shutil.copy(f"fonts/{self.common_props['font_family']}", gen_font_name(self.common_props['font_family'],self.common_props['selection_font']))

#============ theme.txt config file

font_colour,selection_font_colour,label_colour = self.theme_txt_conf['font_colour'],self.theme_txt_conf['selection_font_colour'],self.theme_txt_conf['label_colour']
config=open('template/theme.txt').read()
config=config.replace('{font_colour}',f'"{font_colour}"')
config=config.replace('{selection_font_colour}',f'"{selection_font_colour}"')
config=config.replace('{label_colour}',f'"{label_colour}"')
config = open('template/theme.txt').read()
config = config.replace('{font_colour}',f'"{font_colour}"')
config = config.replace('{selection_font_colour}',f'"{selection_font_colour}"')
config = config.replace('{label_colour}',f'"{label_colour}"')
config = config.replace('{selection_height}',f'{height}')
open('./export/theme.txt','w').write(config)
11 changes: 10 additions & 1 deletion noicegrub/templateconf.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from math import floor

item_placement={
'Kewl':[4,6],
'Noice':[3,9],
Expand All @@ -11,4 +13,11 @@
'TheMan':[6,5],
'Mountains':[1,4],
'Nico':[None,None],
}
}

def calc_selection_scale(font_size):
height = 30.0
def_fontsize = 16.0
new_height = float(font_size)*(height/def_fontsize)
size = floor(new_height/height)
return (new_height, size)
2 changes: 2 additions & 0 deletions presets/GradientGuy.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ secondary = "#031C20"
header_font_colour = "#fff"
footer_font_colour = "#fff"
selection_bg_colour = "#6C2082"
selection_font = "32"
font_family = "InterRegular.ttf"

[theme]
font_colour = "#fff"
Expand Down
2 changes: 2 additions & 0 deletions presets/LightLime.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ secondary = "#FFFB7D"
header_font_colour = "#fff"
footer_font_colour = "#fff"
selection_bg_colour = "#A7ED9A"
selection_font = "32"
font_family = "InterRegular.ttf"

[theme]
font_colour = "#fff"
Expand Down
2 changes: 2 additions & 0 deletions presets/Mountains.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ secondary = "#6D0470"
header_font_colour = "#4662C8"
footer_font_colour = "#8027A9"
selection_bg_colour = "#4E31A0"
selection_font = "32"
font_family = "InterRegular.ttf"

[theme]
font_colour = "#000"
Expand Down
2 changes: 2 additions & 0 deletions presets/Mountains2.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ secondary = "#00BE9C"
header_font_colour = "#4AF2C0"
footer_font_colour = "#63D2C5"
selection_bg_colour = "#57F2CD"
selection_font = "32"
font_family = "InterRegular.ttf"

[theme]
font_colour = "#000"
Expand Down
2 changes: 2 additions & 0 deletions presets/NicoBlue.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ secondary = "#470B6C"
header_font_colour = "#000"
footer_font_colour = "#000"
selection_bg_colour = "#4441EC"
selection_font = "32"
font_family = "NicoMojiRegular.ttf"

[theme]
font_colour = "#000"
Expand Down
2 changes: 2 additions & 0 deletions presets/NicoPurple.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ secondary = "#6211A1"
header_font_colour = "#000"
footer_font_colour = "#000"
selection_bg_colour = "#A349DA"
selection_font = "32"
font_family = "NicoMojiRegular.ttf"

[theme]
font_colour = "#000"
Expand Down
2 changes: 2 additions & 0 deletions presets/Noice.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ secondary="#3A7385"
header_font_colour = "#D4B3FF"
footer_font_colour = "#1F1A1A"
selection_bg_colour = "#D4B5FB"
selection_font = "32"
font_family = "InterRegular.ttf"

[theme]
font_colour = "#000"
Expand Down
2 changes: 2 additions & 0 deletions presets/TheSky.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ secondary = "#80D0C7"
header_font_colour = "#fff"
footer_font_colour = "#fff"
selection_bg_colour = "#61BAEA"
selection_font = "32"
font_family = "InterRegular.ttf"

[theme]
font_colour = "#fff"
Expand Down
2 changes: 2 additions & 0 deletions presets/VioBlue.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ secondary = "#1C3D92"
header_font_colour = "#fff"
footer_font_colour = "#fff"
selection_bg_colour = "#D594F4"
selection_font = "32"
font_family = "InterRegular.ttf"

[theme]
font_colour = "#fff"
Expand Down
2 changes: 2 additions & 0 deletions presets/WildFire.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ secondary = "#A59508"
header_font_colour = "#fff"
footer_font_colour = "#fff"
selection_bg_colour = "#E64B19"
selection_font = "32"
font_family = "InterRegular.ttf"

[theme]
font_colour = "#fff"
Expand Down
28 changes: 21 additions & 7 deletions scripts/install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,32 @@ get_path() {
fi
}

makefont() {
font=$(ls ./export | grep -E '.ttf|.otf')
size=$(echo $font | awk -F '.' '{print $2}')
name="$(echo $font | awk -F '.' '{print $1}').pf2"
if [[ $(which grub2-mkfont) != "" ]];then
grub2-mkfont -s $size -o $THEME_PATH/$name ./export/$font 2>/dev/null
else
grub-mkfont -s $size -o $THEME_PATH/$name ./export/$font 2>/dev/null
fi
echo $name
}

install() {
printf "\033[92m[+] \033[94mCreating $THEME_PATH\n"
rm -r $THEME_PATH 2> /dev/null
mkdir -p $THEME_PATH
printf "\033[92m[+] \033[94mCopying files\n"
cp ./export/* $THEME_PATH

#======== /etc/default/grub config file
sed -i 's/.*GRUB_THEME=.*//' /etc/default/grub
echo "GRUB_THEME=$THEME_PATH/theme.txt" >> /etc/default/grub
#
echo "GRUB_THEME=$THEME_PATH/theme.txt" >> /etc/default/grub #theme location
font_name=$(makefont)
sed -i 's/.*GRUB_FONT=.*//' /etc/default/grub
echo "GRUB_FONT=$THEME_PATH/$font_name" >> /etc/default/grub #font location

#========= GRUB update
#
printf "\033[92m[+] \033[94mUpdating GRUB config\n\033[92m"
Expand Down Expand Up @@ -58,8 +75,5 @@ fi
get_path
THEME_PATH=$GRUB_PATH"themes/noicegrub"
printf "\033[1m\033[93m"
read -p "Do you want to place the generated theme in $GRUB_PATH [y/n] " inst
case $inst in
[Yy]* ) install;;
* ) exit;;
esac
echo "Generated theme will be placed in $THEMES_PATH"
install
2 changes: 1 addition & 1 deletion template/theme.txt
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ terminal-border: "0"
item_color = {font_colour}
selected_item_color = {selection_font_colour}
selected_item_pixmap_style = "select_*.png"
item_height = 30
item_height = {selection_height}
item_padding = 2
item_spacing = 5
}
Expand Down

0 comments on commit 1e71d89

Please sign in to comment.