Charset2FBFont

User projects written in or related to FreeBASIC.
leopardpm
Posts: 1795
Joined: Feb 28, 2009 20:58

Re: Charset2FBFont

Post by leopardpm »

hmmm gets crazy fast... but understanding these different methods will help my code be more modular...
BasicCoder2
Posts: 3906
Joined: Jan 01, 2009 7:03
Location: Australia

Re: Charset2FBFont

Post by BasicCoder2 »

D.J.Peters wrote:You are right but this are only the half of the story :-)
Sorry I didn't make it clear the text was copied word for word from,
http://www.petesqbsite.com/sections/exp ... nters.html
to save the reader having to search for the relevant part themselves.
.
wallyg
Posts: 267
Joined: May 08, 2009 7:08
Location: Tucson Arizona

Re: Charset2FBFont

Post by wallyg »

Thank you for this fine product.

I am going to use it for one of the projects I am working on. I see you include the zlib and freetype license. Do you have a license I should include for this library or is the zlib and freetype license enough.
Muttonhead
Posts: 139
Joined: May 28, 2009 20:07

Re: Charset2FBFont

Post by Muttonhead »

Hm... as long as you want to use the BitmapFont.bi only and a bitmap, howsoever(thanks2googletransltr) it was created,... feel free to use it and earn a lot of money with it! :D
I think no license needed

The created Bitmap...
I'm not a lawyer, the most fonts are copyrighted, take a look at the (c)'s before you use a bitmap of this font.

Mutton
wallyg
Posts: 267
Joined: May 08, 2009 7:08
Location: Tucson Arizona

Re: Charset2FBFont

Post by wallyg »

Thanks, I was not planning on selling anything, just putting together a library for some friends to use.

Thanks for the list. I was planning on doing some research to find a couple of fonts that were in the public domain for use. If anyone knows of fonts that can be used with your software that are in the public domain, I would appreciate finding out about them.
wallyg
Posts: 267
Joined: May 08, 2009 7:08
Location: Tucson Arizona

Charset2FBFont - Thank You and simple request

Post by wallyg »

I appreciate your software and it works great. Thank you for providing it for general use.

I was wondering how hard it would be to remove the GUI software of your Fontmaker and make it a standalone function that I could call with some options( like font name, size, ... ) and have the files generated with a specified base filename that I could then read and use with my homebrew design software as needed.

Or maybe a standalone batch program that I could EXEC that would allow options to be specified as command arguments.

This would allow me not to have to generate and store hundreds of files for different fonts in different heights and options.

Thank you for your consideration and advice.

Gratefully yours, Wally
Muttonhead
Posts: 139
Joined: May 28, 2009 20:07

Re: Charset2FBFont

Post by Muttonhead »

This would allow me not to have to generate and store hundreds of files for different fonts in different heights and options.
;) thats why vector font was invented, but seriously:
under these circumstances, there are good reasons for using the Freetype library directly for the output,
and not to take the diversions via a BitmapFont.By the way, there is a good code example that explains the
basic use of the Freetype library: compiler directory\examples\graphics\FreeType\

Please download the latest version of Charset2FBFont. It contains some small fixes: latest compilerversion(v1.08.0) and 64Bit are supported.

Based on this package try this:

Code: Select all

#include "freetype2/freetype.bi"
#include "vbcompat.bi"
#include "BitmapFont.bi"

'all parameter
dim as string p_SourcePath, p_VectorFontFile, p_DestinationPath, p_BitmapFontFile
dim as integer p_size,p_res,p_rangestart,p_rangeend,p_bitmaptype,p_plottype, p_spacewidth
dim as integer p_pencolor,p_bgcolor,p_pthreshold,p_bgthreshold
dim as single p_gamma

'to create a cp437 bitmap
dim as integer unicodetable(255)

'ft library stuff
dim as FT_Library library
dim as FT_Face face
dim as FT_Bitmap ptr FTbitmap
dim as integer FT_BitmapPitch,FTRenderFlag

'own bitmap font stuff
dim as BitmapFont ptr BMPFont

'helper
'dim as integer pt,dpi,p_spacewidth,gammafactor
dim as uinteger PixelColor
dim as ubyte ftoutput,bmpinput
dim as integer tmpheight,raiserange,index,ucval,ff
dim as string row
'******************************************************************************


screen 19,32'we need a FB screen to create fb images

if (FT_Init_FreeType(@library) <> 0) then print "FT_Init_FreeType() failed": End 1
BMPFont=new BitmapFont
if BMPFont=0 then print "FT_Init_FreeType() failed": End 1 

'read cp437
if fileexists ("CodePage.cfg") then
  ff=freefile
  open "CodePage.cfg" for input as ff
    do
      line input # ff, row
      if left (row,1)<>"#" then
        index= val( "&H" & mid(row,3,2) )
        ucval= val( "&H" & mid(row,8,4) )
        unicodetable(index)=ucval
      end if
    loop until eof(ff)
  close ff
 else 
  for i as integer=0 to 255
    unicodetable(i)=i
  next i
end if

p_SourcePath=curdir
p_VectorFontFile="ArtDeco.otf"
p_DestinationPath=curdir
p_BitmapFontFile=left(p_VectorFontFile, len(p_VectorFontFile)-4) + ".bmp"
p_size=15'font size in Pt
p_res=96'Resolution (dpi)
p_rangestart=32
p_rangeend=255
p_bitmaptype=1'1=DRAW STRING compatible 2=smallest Bitmap
p_plottype=1'1=proportional 2=monospaced
p_spacewidth=1'additional space between chars or fixed char width
p_pencolor=&H0'
p_bgcolor=&HFFFFFF'
p_pthreshold=255' contrast tool parameters
p_bgthreshold=0
p_gamma=1


if FT_New_Face(library, p_VectorFontFile, 0, @face) = 0 then 'if the font file was opened by freetype library
  FTRenderFlag=FT_LOAD_RENDER
  
  BMPFont->firstchar=p_rangestart
  BMPFont->lastchar=p_rangeend
  BMPFont->imgwidth=0
  BMPFont->imgheight=0
  BMPFont->PlotType=1
  BMPFont->PenColor=p_pencolor
  BMPFont->Background=p_bgcolor

  'Limiter
  if BMPFont->firstchar<0 then BMPFont->firstchar=0
  if BMPFont->firstchar>255 then BMPFont->firstchar=255
  if BMPFont->lastchar<0 then BMPFont->lastchar=0
  if BMPFont->lastchar>255 then BMPFont->lastchar=255
  if BMPFont->firstchar>BMPFont->lastchar then swap BMPFont->firstchar,BMPFont->lastchar
  if p_size<1 then p_size=1
  if p_spacewidth<0 then p_spacewidth=0
  if p_plottype=2 then if p_spacewidth<1 then p_spacewidth=1

  FTRenderFlag=FT_LOAD_RENDER

  '1. alle Größen zu jedem Zeichen auslesen, Höhe BaseLine berechnen
  if FT_Set_Char_Size( face, 0, p_size*64,p_res,p_res)=0 then
    FT_BitmapPitch=face->glyph->bitmap.pitch
    for i as integer=BMPFont->firstchar to BMPFont->lastchar
      if FT_Load_Char(face, unicodetable(i), FTRenderFlag)=0 then
        BMPFont->CharSet(i).OffsetX       =BMPFont->imgwidth
        BMPFont->CharSet(i).AdvanceX      =face->glyph->advance.x shr 6
        BMPFont->CharSet(i).GlyphWidth    =face->glyph->bitmap.width
        BMPFont->CharSet(i).GlyphHeight   =face->glyph->bitmap.rows
        BMPFont->CharSet(i).GlyphLeft     =face->glyph->bitmap_left
        BMPFont->CharSet(i).GlyphTop      =face->glyph->bitmap_top
        'Baseline... eigentlich nur Suche nach größter Enfernung nach oben
        if BMPFont->BaseLine < BMPFont->CharSet(i).GlyphTop then BMPFont->BaseLine = BMPFont->CharSet(i).GlyphTop

        select case p_bitmaptype
          case 1'DRAW STRING
            if p_plottype=1 then'proportional
              if BMPFont->CharSet(i).GlyphWidth>0 then BMPFont->CharSet(i).AdvanceX = BMPFont->CharSet(i).GlyphWidth
              BMPFont->CharSet(i).AdvanceX +=p_spacewidth
              BMPFont->CharSet(i).GlyphLeft=cint(p_spacewidth/2)  'cint((BMPFont->CharSet(i).AdvanceX - BMPFont->CharSet(i).GlyphWidth)/2)
              BMPFont->imgwidth += BMPFont->CharSet(i).AdvanceX
            else                                   'monospaced
              BMPFont->CharSet(i).AdvanceX =p_spacewidth
              if BMPFont->CharSet(i).GlyphWidth>p_spacewidth then BMPFont->CharSet(i).GlyphWidth=p_spacewidth
              BMPFont->CharSet(i).GlyphLeft=cint((p_spacewidth - BMPFont->CharSet(i).GlyphWidth)/2)
              BMPFont->imgwidth += p_spacewidth
            end if
          case 2'smallest bitmap
            BMPFont->imgwidth += BMPFont->CharSet(i).GlyphWidth
        end select
      end if
    next i

    '2.ausgehend von der nun bekannten BaseLine die Gesamthöhe des Fonts ermitteln
    '.GlyphTop steht noch im Bezug zur BaseLine, Umrechnung auf "Abstand von oben"
    'damit gilt jetzt bei allen Werten das Screen/Image Koordinatensystem
    tmpheight=0
    for i as integer=BMPFont->firstchar to BMPFont->lastchar
      'GlyphTop "umrechnen"
      BMPFont->CharSet(i).GlyphTop = BMPFont->BaseLine - BMPFont->CharSet(i).GlyphTop
      'Fonthöhe...
      tmpheight =BMPFont->CharSet(i).GlyphTop + BMPFont->CharSet(i).GlyphHeight
      if BMPFont->imgheight < tmpheight then BMPFont->imgheight=tmpheight
    next i

    CreateImage(BMPFont->img,BMPFont->imgwidth,BMPFont->imgheight)
    ImageInfo(BMPFont->img,BMPFont->imgwidth,BMPFont->imgheight,BMPFont->imgbpp,BMPFont->imgpitch,BMPFont->imgpxldata)

    line BMPFont->img,(0,0)-(BMPFont->imgwidth-1,BMPFont->imgheight-1),BMPFont->Background,BF

    '3. alle Zeichen ins Image plotten
    for i as integer=BMPFont->firstchar to BMPFont->lastchar
      if FT_Load_Char(face,unicodetable(i),FTRenderFlag)=0 then'flag 256 anitialias/ monochrom
        FTbitmap = @face->glyph->bitmap
        for y As integer = 0 To (FTbitmap->rows - 1)
          for x As integer = 0 To (FTbitmap->width - 1)

            ftoutput = FTbitmap->buffer[y * FTbitmap->pitch + x]

            'contrast
            if p_bgthreshold > p_pthreshold then swap p_bgthreshold,p_pthreshold
            raiserange=p_pthreshold - p_bgthreshold

            if ftoutput<=p_bgthreshold then bmpinput=0
            if ftoutput>p_bgthreshold and ftoutput<=p_pthreshold then bmpinput=(ftoutput-p_bgthreshold)/raiserange * 255
            if ftoutput>p_pthreshold then bmpinput=255
            
            bmpinput=((bmpinput / 255) ^ p_gamma) * 255
            
            PixelColor=MixColor(BMPFont->Background, BMPFont->PenColor, bmpinput)

            select case  BMPFont->PlotType
              case 1
                pset BMPFont->img, (BMPFont->CharSet(i).OffsetX + BMPFont->CharSet(i).GlyphLeft + x, BMPFont->CharSet(i).GlyphTop + y),PixelColor
              case 2
                pset BMPFont->img, (BMPFont->CharSet(i).OffsetX + x, y),PixelColor
            end select

          next x
        next y
      end if
    next i

  end if

  chdir p_DestinationPath
  BMPFont->SaveFont(p_BitmapFontFile)'a Drawer will created
else
  print "File not opened"
end if

delete BMPFont
a kind of stand alone converter.
all p_* variables are Parameter to feed the converting process.
its your turn to build a command line tool :)
remark: you need allways an open fb screen to handle with bitmaps.

greets Mutton
wallyg
Posts: 267
Joined: May 08, 2009 7:08
Location: Tucson Arizona

Re: Charset2FBFont

Post by wallyg »

Thank you. Especially thank you for helping and trying to educate the rest of us.
Muttonhead
Posts: 139
Joined: May 28, 2009 20:07

Re: Charset2FBFont

Post by Muttonhead »

I am neither able nor do I want to educate anyone here :)
maybe i've misunderstand you

Mutton
wallyg
Posts: 267
Joined: May 08, 2009 7:08
Location: Tucson Arizona

Re: Charset2FBFont

Post by wallyg »

Your willingness to respond and answer questions together with examples and impart your knowledge to others is the true sign of an educator.

Thank you for all you have done for me and the community as a whole.

Wally
RockTheSchock
Posts: 252
Joined: Mar 12, 2006 16:25

Re: Charset2FBFont

Post by RockTheSchock »

Some time ago I wrote / ported a routine to use Draw String with FreeType2 lib to output UTF8 strings.

https://www.freebasic-portal.de/porticu ... -1783.html

Using a font like Google Noto Mono might be cool but I did not test it.
Post Reply