07-08-2016, 08:24 AM
(07-06-2016, 01:43 PM)clasqm Wrote: I'm looking into a font manager.
I'm pretty happy with this. Needs my libclasqm libraries package.
The hpkg is downloadable on Beshare when I'm online. I'll update my repo this weekend. The Advent pro font will be uploaded then too, so until then, the app will fail (but not crash) if you try to load that particular font.
Code:
#!/bin/env yab
################################
############# Prologue #############
################################
#########Compile-time Dependencies############
#libclasqm_main
#########Run-time Dependencies############
#cmd:pkgman
#cmd:ping
#cmd:ls
#app:Terminal
#app:Tracker
//Yabadabbadoo notification
########DO NOT RENAME THIS FILE!########
//Yabadabbadoo needs it to function.
##Fill in these fields with your own particulars.
##The variables will be used in the About Box and in naming the program.
ProgramName$ = "FontMonkey"
AuthorName$ = "Michel Clasquin-Johnson"
ProgramVersion$ = "V0.1.1"
ProgramBriefDescription$ = "A program to preview, load and unload fonts from the clasqm repo.\n\nThis program does not manage fonts like the Liberation, SIL or Libertine families that are meant to be drop-in replacements for Courier, Arial or Times New Roman. It also does not touch the standard Haiku fonts.\n\nIt is specifically for odd, unusual and display fonts that you need to install for a specific project and then remove from your system when they are no longer needed.\n"
ProgramLicense$ = "Public Domain software"
ProgramAcknowledgements$ ="With thanks to the font creators on www.1001fonts.com and elsewhere."
//*************************
//*****Global Variables****
//*************************
## Technically, yab does not require you to declare global variables,
##It just is a really, really good idea to do it anyway.
// set DEBUG = 1 to print out all messages on the console
DEBUG = 1
//change this to DEBUG = 0 when you are ready to bind the program for distribution
import libclasqm_main
path$ = WhereAmI$()
downloadname$ = ""
##########################
######Preliminary Commands#####
##########################
## Commands to run before the Main Loop come here.
## e.g. setting up a window with a menu.
OpenWindow()
#######End of Prologue#######
//Yabadabbadoo notification
########DO NOT RENAME THIS FILE!########
//Yabadabbadoo needs it to function.
//Main Message Loop
dim msg$(1)
while(not leavingLoop)
nCommands = token(message$, msg$(), "|")
for everyCommand = 1 to nCommands
if(DEBUG and msg$(everyCommand)<>"") print msg$(everyCommand)
switch(msg$(everyCommand))
case "_QuitRequested"
case "MainWindow:_QuitRequested"
case "MainWindow:File:Quit"
leavingLoop = true
break
case "PreviewWindow:_QuitRequested"
window close "PreviewWindow"
break
case "MainWindow:Help:About"
Alert ProgramName$ + " " + ProgramVersion$ + "\n" + "by " + AuthorName$ +"\n\n" + ProgramBriefDescription$ + "\n" + ProgramLicense$ + "\n" + ProgramAcknowledgements$, "OK", "none"
break
case "InstallButton"
system("/boot/system/apps/Terminal pkgman install " + downloadname$)
if window get "PreviewWindow", "exists" <> 0 CheckFontStatus()
break
case "UnInstallButton"
if CheckLocalInstallation() = 1 then
break
else
system("/boot/system/apps/Terminal pkgman uninstall " + downloadname$)
if window get "PreviewWindow", "exists" <> 0 CheckFontStatus()
endif
break
default
break
end switch
if left$((msg$(everyCommand)), 17) = "FontList:_Select:" then
MakePreview(mid$((msg$(everyCommand)), 18))
endif
next everyCommand
wend
CloseWindow()
end
sub CheckConnection()
local connected, connected$, getout
view 0,0 to 142,400, "PrelimView", "MainWindow"
textedit 0,0 to 142,400, "PrelimText", 0, "PrelimView"
textedit color "PrelimText", "bgcolor", 185,185,185
textedit set "PrelimText", "editable", 0
textedit add "PrelimText", "Welcome to\n"
textedit add "PrelimText", "FontMonkey.\n\n"
textedit add "PrelimText", "Running checks ...\n\n"
//check if we are running on a modern version of haiku
/////////////////////////////////////////////////////////////////////////////////////
textedit add "PrelimText", "Checking Haiku ...\n"
if FileExists("/boot/system/bin/pkgman") then
textedit add "PrelimText", "OK, pkgman found.\n\n"
pause 0.1
else
alert "This does not look like a Haiku installation with package management.", "OK", "warning"
return 1
endif
close #1
//check if the system font folder exists
////////////////////////////////////////////////////////////
textedit add "PrelimText", "Checking fonts ...\n"
if FileExists("/boot/system/data/fonts") then
textedit add "PrelimText", "OK, fonts found.\n\n"
pause 0.1
else
alert "It looks like this Haiku installation is damaged - no system font folder was found.", "OK", "warning"
return 1
endif
close #1
//check if internet connection is live by pinging a google server
/////////////////////////////////////////////////////////////////////////////////////////////////
while(connected = 0)
textedit add "PrelimText", "Checking internet ...\n"
connected$=system$("ping -c 1 216.58.223.36")
if instr(connected$, "64 bytes from 216.58.223.36") = 0 then
getout = alert "Internet connection is required for this program. Please switch on your connection", "OK","Quit","","warning"
if getout = 2 return 1
else
textedit add "PrelimText", "OK, connected.\n\n"
connected =1
pause 0.1
endif
wend
//check if my repo is loaded
///////////////////////////////////////////
connected = 0
while(connected = 0)
textedit add "PrelimText", "Checking repo ...\n"
connected$ = system$("pkgman list-repos")
if instr(connected$, "clasqm's repo") = 0 then
getout = alert "This program requires that the clasqm repository be added to your repo list. Proceed?", "OK","Quit","","warning"
if getout = 2 then
return 1
else
system("pkgman add-repo clasquin-johnson.co.za/michel/repo")
endif
else
textedit add "PrelimText", "OK, repo found.\n\n"
connected = 1
pause 0.5
endif
wend
view remove "PrelimView"
return 0
end sub
sub CheckFontStatus()
local installedpkgs$
if window get "PreviewWindow", "exists" = 0 return //program will crash if buttons do not exist
option set "InstallButton", "enabled", 0
option set "UnInstallButton", "enabled", 0
installedpkgs$ = system$("ls -1 /boot/system/packages/*.hpkg")
installedpkgs$ = installedpkgs$ + system$("ls -1 /boot/home/config/packages/*.hpkg")
if instr(installedpkgs$, downloadname$) = 0 then
option set "InstallButton", "enabled", 1
else
option set "UnInstallButton", "enabled", 1
endif
end sub
sub CheckLocalInstallation()
//pkgman can only uninstall from /boot/system/packages ATM
//locally installed packages must be removed manually
//which is how they got there in the first place, so ...
local installedpkgs$
installedpkgs$ = system$("ls -1 /boot/home/config/packages/*.hpkg")
if instr(installedpkgs$, downloadname$) = 0 then
return 0
else
alert "The package " + downloadname$ + " appears to be installed in /boot/home/config/packages. You need to remove it manually. Tracker will now display this folder.", "OK", "warning"
system("/boot/system/Tracker /boot/home/config/packages")
if window get "PreviewWindow", "exists" =1 window close "PreviewWindow"
return 1
endif
end sub
sub CloseWindow()
//close down the preview window if it exists
if window get "PreviewWindow", "exists" <> 0 window close "PreviewWindow"
//Close down the main window
window close "MainWindow"
end sub
sub MakeMenu()
//Create menu in MainWindow
menu "File", "Quit", "Q", "MainWindow"
menu "Help", "About", "", "MainWindow"
end sub
sub MakePreview(fontnumber$)
local fullfontname$,fontname$, previewgrfx$,errcode
fullfontname$ = listbox get$ "FontList", val(fontnumber$)
fontname$=ParsePreviewName$(fullfontname$)
downloadname$= fontname$ + "font"
previewgrfx$ = path$ + "/previews/" + fontname$
if FileExists(previewgrfx$) = 0 previewgrfx$ = path$ + "/previews/default"
close #1
errcode = bitmap image previewgrfx$, "FontPreview"
if window get "PreviewWindow", "exists" = 0 then
window open 280, 100 to 792, 386, "PreviewWindow", ""
button 50,259 to 251,283, "InstallButton", "Install this font", "PreviewWindow"
button 261,259 to 462,283, "UnInstallButton", "Remove this font", "PreviewWindow"
option set "InstallButton", "enabled", 0
option set "UnInstallButton", "enabled", 0
window set "PreviewWindow", "flags", "not-resizable"
window set "PreviewWindow", "look", "floating"
window set "PreviewWindow", "flags", "not-zoomable"
endif
window set "PreviewWindow", "title", "Preview of " + fullfontname$
draw bitmap 0,0, "FontPreview", "copy", "PreviewWindow"
bitmap remove "FontPreview"
CheckFontStatus()
end sub
sub OpenWindow()
//Setup the main window here
window open 100,100 to 242,500, "MainWindow", "Font Selector"
window set "MainWindow", "flags", "not-resizable"
window set "MainWindow", "flags", "not-zoomable"
if CheckConnection() then
leavingLoop =1
return
endif
MakeMenu()
listbox 0,20 to 141,399, "FontList", 1, "MainWindow"
PopulateFontList()
end sub
sub ParsePreviewName$(inputname$)
local outputname$
for f = 1 to len(inputname$)
if mid$(inputname$, f,1) <> " " then
outputname$ = outputname$ + mid$(inputname$, f,1)
endif
next f
return lower$(outputname$)
end sub
sub PopulateFontList()
local fontname$, fontlist$
fontlist$=path$ + "/fonts.txt"
open fontlist$ for reading as #1
while (not(EOF(1)))
line input #1 fontname$
if fontname$ <> "" listbox add "FontList", fontname$
wend
close #1
end sub