
(********************************************************************)
(*                                                                  *)
(*  bitmapfont.s7i  Defines the bitmap font type.                   *)
(*  Copyright (C) 2007  Thomas Mertes                               *)
(*                                                                  *)
(*  This file is part of the Seed7 Runtime Library.                 *)
(*                                                                  *)
(*  The Seed7 Runtime Library is free software; you can             *)
(*  redistribute it and/or modify it under the terms of the GNU     *)
(*  Lesser General Public License as published by the Free Software *)
(*  Foundation; either version 2.1 of the License, or (at your      *)
(*  option) any later version.                                      *)
(*                                                                  *)
(*  The Seed7 Runtime Library is distributed in the hope that it    *)
(*  will be useful, but WITHOUT ANY WARRANTY; without even the      *)
(*  implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR *)
(*  PURPOSE.  See the GNU Lesser General Public License for more    *)
(*  details.                                                        *)
(*                                                                  *)
(*  You should have received a copy of the GNU Lesser General       *)
(*  Public License along with this program; if not, write to the    *)
(*  Free Software Foundation, Inc., 51 Franklin Street,             *)
(*  Fifth Floor, Boston, MA  02110-1301, USA.                       *)
(*                                                                  *)
(********************************************************************)


include "font.s7i";
include "pixmapfont.s7i";


#
# bitmapFont
#


const type: fontPicType is hash [char] array string;

const type: bitmapFont is sub fontProperties struct
    var fontPicType: fontPictures is fontPicType.value;
  end struct;

type_implements_interface(bitmapFont, font);


const func integer: width (in bitmapFont: bmpFont, in string: stri) is func
  result
    var integer: result is 0;
  local
    var char: ch is ' ';
  begin
    for ch range stri do
      if ch in bmpFont.fontPictures then
        result +:= length(bmpFont.fontPictures[ch][1]);
      else
        result +:= length(bmpFont.fontPictures[' '][1]);
      end if;
    end for;
  end func;


const func integer: numOfCharsInWidth (in bitmapFont: bmpFont,
    in string: stri, in integer: allowedWidth) is func
  result
    var integer: result is 0;
  local
    var integer: index is 1;
    var integer: totalWidth is 0;
  begin
    result := length(stri);
    while index <= length(stri) and totalWidth <= allowedWidth do
      if stri[index] in bmpFont.fontPictures then
        totalWidth +:= length(bmpFont.fontPictures[stri[index]][1]);
      else
        totalWidth +:= length(bmpFont.fontPictures[' '][1]);
      end if;
      if totalWidth <= allowedWidth then
        incr(index);
      end if;
    end while;
    result := pred(index);
  end func;


const func PRIMITIVE_WINDOW: genPixmap (in array string: pattern,
    in color: foreground, in color: background, in integer: scale) is func
  result
    var PRIMITIVE_WINDOW: result is PRIMITIVE_WINDOW.value;
  local
    var integer: height is 0;
    var integer: width is 0;
    var integer: line is 0;
    var integer: column is 0;
  begin
    height := length(pattern);
    width := length(pattern[1]);
    result := newPixmap(width * scale, height * scale);
    clear(result, background);
    for line range 1 to height do
      for column range 1 to width do
        if pattern[line][column] <> ' ' then
          rect(result, pred(column) * scale, pred(line) * scale,
              scale, scale, foreground);
        end if;
      end for;
    end for;
  end func;


const func pixmapFontType: genPixmapFont (in bitmapFont: bmpFont,
    in integer: fontSize, in integer: scale,
    in color: foreground, in color: background) is func
  result
    var pixmapFontType: result is pixmapFontType.value;
  local
    var char: ch is ' ';
  begin
    if ' ' in bmpFont.fontPictures then
      incl(result.pixmap, ' ', genPixmap(bmpFont.fontPictures[' '],
          foreground, background, scale));
    end if;
    result.baseFont := bmpFont;
    result.fontSize := fontSize;
    result.scale := scale;
    result.foreground := foreground;
    result.background := background;
    result.baseLineDelta := baseLineDelta(bmpFont) * scale;
    result.line_delta := lineHeight(bmpFont) * scale;
    result.column_delta := columnWidth(bmpFont) * scale;
    result.characterSpacing := characterSpacing(bmpFont) * scale;
  end func;


const func PRIMITIVE_WINDOW: getFontCharPixmap (in bitmapFont: bmpFont,
    inout pixmapFontType: pixmapFont, in char: ch) is func
  result
    var PRIMITIVE_WINDOW: charPixmap is PRIMITIVE_WINDOW.value;
  begin
    if ch in bmpFont.fontPictures then
      charPixmap := genPixmap(bmpFont.fontPictures[ch],
          pixmapFont.foreground, pixmapFont.background, pixmapFont.scale);
      incl(pixmapFont.pixmap, ch, charPixmap);
    else
      charPixmap := pixmapFont.pixmap[' '];
    end if;
  end func;


const proc: setFont (inout pixmapFontFile: fontFile, in bitmapFont: aFont) is func
  begin
    fontFile.font := getFont(aFont, fontFile.font.fontSize,
        fontFile.font.scale, fontFile.font.foreground, fontFile.font.background);
  end func;


#
# Helper function for the font definition
#


const func integer: columnWidth (in fontPicType: fontPictures) is func
  result
    var integer: result is 0;
  local
    var char: ch is ' ';
  begin
    for key ch range fontPictures do
      if length(fontPictures[ch][1]) > result then
        result := length(fontPictures[ch][1]);
      end if;
    end for;
  end func;
