Not logged inGosu Forums
Forum back to libgosu.org Help Search Register Login
Up Topic Gosu / Gosu Exchange / Exporting fonts
- - By il mietitore Date 2012-01-20 22:22
Heya ;)

I'm working on a project for a university exam, and I'm trying to find a way to export the game and packaging, somehow, the fonts with the executible.

I mean: if I use ocra, I obtain a .exe file which is all ok. But if I run the program on a computer that doesn't have the font needed (for instance: Segoe UI) all of the text formatting goes to hell.

I tried to put the font file in a folder, but the instruction

@font_Segoe_UI_20 = Gosu::Font.new(self, "fonts/segoeui.ttf", 20)

doesn't seem to work.

Is there a way to do such a thing?
Parent - By chase4926 Date 2012-01-20 23:55
By "doesn't seem to work" what error are you receiving? I've loaded fonts from a file many times with no issues what-so-ever.
Parent - - By jlnr (dev) Date 2012-01-21 04:48
Have you included the TTF file in the ocra call? How do you call Ocra?
Parent - - By jlnr (dev) Date 2012-01-29 16:14
^
Parent - - By il mietitore Date 2012-01-29 18:52
eh, i just write

ocra MC.rb

how can i add fonts? :(
Parent - - By Spooner Date 2012-01-30 00:10
ocra MC.rb --windows /images/**/*.* /fonts/**/*.*
However you need to ensure that all paths are file-relative, not relative to the working directory (when running the executable, so require_relative for ruby scripts and stuff like File.expand_path("../../fonts/whatever.ttf", __FILE__) when referring to assets.

Also, I'd consider using Releasy, which is my gem for making life a lot easier in this sort of circumstance. Even helps you build executables for other platforms :)
Parent - - By il mietitore Date 2012-01-31 14:30
Mah. That's too much for my brain.

ocra works, but if I move the file somewhere else then it doesn't work anymore because there aren't the other files with him. And even more: i just can't understand why, but if i link the font during initializing, it doesn't work with the font I should use (segoeui.ttf) but it works if I use ANOTHER font (arabdances.ttf, for instance, works).

Someone can help me in a pratical way? Here is the download link for the script and the other files. Someone can do some magic trick, make me this executible and then tell me how he did*? pleeeeeease *__*

*so that I can do it too, if I find out things to correct in the source...
Parent - - By Spooner Date 2012-02-01 00:07
Ocra only includes the script and gems automatically. You need to tell it about assets like images and fonts:
ocra mc.rb --windows --no-enc immagini/*.* fonts/*.*

(The --windows means don't open a console window when run; --no-enc means don't include lots of encoding files you will never use)

In your actual script, you need to set up some methods to calculate asset paths for you based on the position of the script when it is unpacked from the exe, not the executable itself:

def image_path(image); File.expand_path("../immagini/#{image}", __FILE__); end
def font_path(font); File.expand_path("../fonts/#{font}", __FILE__); end


In #initialize, do the loading

    @immagini_titoli = Gosu::Image.new(self,  image_path("testa1.png"), true)
    @immagini_cursore = Gosu::Image.new(self, image_path("cursore.png"), true)
    @immagini_start = Gosu::Image.new(self, image_path("start1.png"), true)
    @immagini_sbarra = Gosu::Image.new(self, image_path("sbarra.png"), true)

   
The script also gives away why the font isn't working on other machines;
the font is named based on how it is installed on your machine, not loading the actual ttf you provide:
    @font_Segoe_UI_20 = Gosu::Font.new(self, font_path("segoeui.ttf"), 20)
Parent - - By Spooner Date 2012-02-01 00:22
Actually, whatever I do to use the font, it doesn't seem to load it (in script or executable version). I assume that is why you were loading it with "Segoe UI", rather that "fonts/segoeui.ttf" - it doesn't seem to load from the ttf, but it will from your home system? It will load on my system if I call it "Segoe UI", but not directly from the ttf :P

Apparently "Segoe UI" is a Microsoft font included in Vista/Windows 7, so it should be available simply named that way on all newish PCs (And will load under that name for me!); are you sure the ttf you have is the same thing?
Parent - By il mietitore Date 2012-02-01 07:48
uhm, well: i went in the fonts directory, and copied the font file. There are various versions (Segoe UI Italic, Segoe UI bold...), so i took the normal one. It should be the right one :\

Anyway: let's hope that the professor have a pc with the font installed. I'll attach the font file and say "if it doesn't work consider to install the font you find in this folder".
- - By il mietitore Date 2012-01-29 15:58
I don't exactly receive an error. The text it's just formatted in a default font.

This is how it should be:


This is how it appears if there is no font:


About ocra, i just go in the directory and type "ocra MC.rb". I suppose I'll have to write something else, isn't it? :\
Parent - - By RunnerPack Date 2012-02-02 02:34
The default font doesn't look that bad... Can't you just re-wrap a few lines to make the text fit on the screen?
Parent - - By il mietitore Date 2012-02-02 22:45
it's, ehm, a pain in the ass: every line is a @draw_Segoe_UI_20.draw command. I have to say that I used the wrong library, or anyway I could have found a way more simple way to format the thing, because the entire code is basically all like this part:

    if (@game_begin == true) and (@current_n == 19)
      @font_Segoe_UI_20.draw_rel("19", 400, 50, 0, 0.5, 0.5, 1, 1, 0xffff0000)
      @font_Segoe_UI_20.draw_rel("L'ATTENZIONE DI UN FANTASMA", 400, 70, 0, 0.5, 0.5, 1, 1, 0xffff0000)
      @font_Segoe_UI_20.draw("Di' cosa fa il fantasma, chiedi al giocatore come si comporta, e di' cosa succede.", 70, 90, 0, 1, 1, 0xffffffff)
      @font_Segoe_UI_20.draw("Continua in questo modo fino a che non si verifica uno dei seguenti casi:", 70, 110, 0, 1, 1, 0xffffffff)
      @font_Segoe_UI_20.draw("- Decidi che l'altro giocatore ha provocato la violenza del fantasma. Vai al     .", 70, 130, 0, 1, 1, 0xffffffff)
      @font_Segoe_UI_20.draw("29", (@font_Segoe_UI_20.text_width("- Decidi che l'altro giocatore ha provocato la violenza del fantasma. Vai al ", 1)) + 70, 130, 0, 1, 1, ((0xff - @glow) << 24) + 0xffff00)
      @font_Segoe_UI_20.draw("- L'altro giocatore sta cercando di passare oltre il fantasma. Digli di andare al     .", 70, 150, 0, 1, 1, 0xffffffff)
      @font_Segoe_UI_20.draw("12", (@font_Segoe_UI_20.text_width("- L'altro giocatore sta cercando di passare oltre il fantasma. Digli di andare al ", 1)) + 70, 150, 0, 1, 1, ((0xff - @glow) << 24) + 0xffff00)
      @font_Segoe_UI_20.draw("- Decidi che l'altro giocatore sta confrontando il fantasma. Digli di andare al     .", 70, 170, 0, 1, 1, 0xffffffff)
      @font_Segoe_UI_20.draw("34", (@font_Segoe_UI_20.text_width("- Decidi che l'altro giocatore sta confrontando il fantasma. Digli di andare al ", 1)) + 70, 170, 0, 1, 1, ((0xff - @glow) << 24) + 0xffff00)
      @font_Segoe_UI_20.draw("- Decidi che l'altro giocatore ha suscitato la curiosita' del fantasma. Digli di andare al     .", 70, 190, 0, 1, 1, 0xffffffff)
      @font_Segoe_UI_20.draw("36", (@font_Segoe_UI_20.text_width("- Dicidi che l'altro giocatore ha suscitato la curiosita' del fantasma. Digli di andare al ", 1)) + 70, 190, 0, 1, 1, ((0xff - @glow) << 24) + 0xffff00)
      @font_Segoe_UI_20.draw("- Decidi che l'altro giocatore ha suscitato la pieta' del fantasma. Digli di andare al     .", 70, 210, 0, 1, 1, 0xffffffff)
      @font_Segoe_UI_20.draw("38", (@font_Segoe_UI_20.text_width("- Decidi che l'altro giocatore ha suscitato la pieta' del fantasma. Digli di andare al ", 1)) + 70, 210, 0, 1, 1, ((0xff - @glow) << 24) + 0xffff00)
      @font_Segoe_UI_20.draw("- L'altro giocatore sta cercando di nascondersi. Digli di andare al     .", 70, 230, 0, 1, 1, 0xffffffff)
      @font_Segoe_UI_20.draw("32", (@font_Segoe_UI_20.text_width("- L'altro giocatore sta cercando di nascondersi. Digli di andare al ", 1)) + 70, 230, 0, 1, 1, ((0xff - @glow) << 24) + 0xffff00)
      @font_Segoe_UI_20.draw("- L'altro giocatore e' riuscito a lasciare la stanza. Vai al   .", 70, 250, 0, 1, 1, 0xffffffff)
      @font_Segoe_UI_20.draw("5", (@font_Segoe_UI_20.text_width("- L'altro giocatore e' riuscito a lasciare la stanza. Vai al ", 1)) + 70, 250, 0, 1, 1, ((0xff - @glow) << 24) + 0xffff00)
      @font_Segoe_UI_20.draw("- L'altro giocatore fa qualcosa tale per cui vuoi testare la sua capacita' di mantenere la calma.", 70, 270, 0, 1, 1, 0xffffffff)
      @font_Segoe_UI_20.draw("Digli di andare al   ,   ,     ,       o      (scegli liberamente, vanno bene tutti)", 70, 290, 0, 1, 1, 0xffffffff)
      @font_Segoe_UI_20.draw("6  8  10  14    16", (@font_Segoe_UI_20.text_width("Digli di andare al ", 1)) + 70, 290, 0, 1, 1, ((0xff - @glow) << 24) + 0xffff00)
      @font_Segoe_UI_20.draw("- Decidi che il fantasma se ne va. Vai al   .", 70, 310, 0, 1, 1, 0xffffffff)
      @font_Segoe_UI_20.draw("3", (@font_Segoe_UI_20.text_width("- Decidi che il fantasma se ne va. Vai al ", 1)) + 70, 310, 0, 1, 1, ((0xff - @glow) << 24) + 0xffff00)
    end


And I don't know how does that default font work, indeed. Maybe on a different computer I would even get a different font.

Anyway the project has been delivered. We'll see if something has gone wrong in professor's pc.
Parent - By Spooner Date 2012-02-03 00:37
You could have used Chingu and/or Fidgit. Fidgit particularly has an auto-wrapping control which might have saved some trouble (TextArea).
Parent - By jlnr (dev) Date 2012-02-03 07:33 Edited 2012-02-03 07:49
Gosu::Font is meant for short, non-wrapping, dynamic texts. Gosu::Image.from_text provides line-wrapping, text alignment and some formatting.

I would have rendered the whole image twice, once with <c=00000000>...</c> around the glowing parts (making them transparent) and once with the same tag around all other text. Then you'd have one normal Gosu::Image for the base text and one for the glowing parts, so you could just pulse the second image over the first and be done in two single Image#draw calls. :)

Bonus: When using from_text, you could save the generated images to PNG with Gosu::Image#save and reload them. Then you would not need to ship the font, unless you also use the Gosu::Font in other places.

--

No matter how you do it, you should write quick helper functions to display your content. I don't think Gosu is the wrong library (I am biased of course), but it is normal that you build some intermediate functions or classes for your game's specific needs. Gosu::Font and Image are dumb, it is normal to build upon them.
Up Topic Gosu / Gosu Exchange / Exporting fonts

Powered by mwForum 2.29.7 © 1999-2015 Markus Wichitill