(*
    Copyright 2016, 2018, 2022 KolibriOS team

    This program 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 3 of the License, or
    (at your option) any later version.

    This program 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, see <http://www.gnu.org/licenses/>.
*)

MODULE RasterWorks;

IMPORT sys := SYSTEM, KOSAPI;


CONST

(* flags *)

  bold            *=   1;
  italic          *=   2;
  underline       *=   4;
  strike_through  *=   8;
  align_right     *=  16;
  align_center    *=  32;

  bpp32           *= 128;


(* encoding *)

  cp866           *=   1;
  utf16le         *=   2;
  utf8            *=   3;


VAR

  // draw text on 24bpp or 32bpp image
  // autofits text between 'x' and 'xSize'
  drawText *: PROCEDURE (canvas, x, y, string, charQuantity, fontColor, params: INTEGER): INTEGER;
(*
  [canvas]:
    xSize        dd  ?
    ySize        dd  ?
    picture      rb  xSize * ySize * bpp

   fontColor     dd  AARRGGBB
    AA = alpha channel   ; 0 = transparent, FF = non transparent

   params        dd  ffeewwhh
    hh = char height
    ww = char width      ; 0 = auto (proportional)
    ee = encoding        ; 1 = cp866, 2 = UTF-16LE, 3 = UTF-8
    ff = flags           ; 0001 = bold, 0010 = italic
                         ; 0100 = underline, 1000 = strike-through
   00010000 = align right, 00100000 = align center
   01000000 = set text area between higher and lower halfs of 'x'
   10000000 = 32bpp canvas insted of 24bpp
   all flags combinable, except align right + align center

   returns: char width (0 = error)
*)

  // calculate amount of valid chars in UTF-8 string
  // supports zero terminated string (set byteQuantity = -1)
  countUTF8Z *: PROCEDURE (string, byteQuantity: INTEGER): INTEGER;


  // calculate amount of chars that fits given width
  charsFit *: PROCEDURE (areaWidth, charHeight: INTEGER): INTEGER;


  // calculate string width in pixels
  strWidth *: PROCEDURE (charQuantity, charHeight: INTEGER): INTEGER;


PROCEDURE params* (charHeight, charWidth, encoding, flags: INTEGER): INTEGER;
(*
    hh = char height
    ww = char width      ; 0 = auto (proportional)
    ee = encoding        ; 1 = cp866, 2 = UTF-16LE, 3 = UTF-8
    ff = flags           ; 0001 = bold, 0010 = italic
                        ; 0100 = underline, 1000 = strike-through
   00010000 = align right, 00100000 = align center
   01000000 = set text area between higher and lower halfs of 'x'
   10000000 = 32bpp canvas insted of 24bpp
   all flags combinable, except align right + align center
*)
  RETURN charHeight + LSL(charWidth, 8) + LSL(encoding, 16) + LSL(flags, 24)
END params;


PROCEDURE main;
VAR Lib: INTEGER;

  PROCEDURE GetProc(Lib, v: INTEGER; name: ARRAY OF CHAR);
  VAR a: INTEGER;
  BEGIN
    a := KOSAPI.GetProcAdr(name, Lib);
    ASSERT(a # 0);
    sys.PUT(v, a)
  END GetProc;

BEGIN
  Lib := KOSAPI.LoadLib("/sys/lib/RasterWorks.obj");
  ASSERT(Lib # 0);
  GetProc(Lib, sys.ADR(drawText),   "drawText");
  GetProc(Lib, sys.ADR(countUTF8Z), "countUTF8Z");
  GetProc(Lib, sys.ADR(charsFit),   "charsFit");
  GetProc(Lib, sys.ADR(strWidth),   "strWidth");
END main;


BEGIN
  main
END RasterWorks.