--
--	rastertext: draw text onto a raster
--	Copyright (C) 2008  Adam Sampson <ats@offog.org>
--
--	This 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 of the License, or (at your option) any later version.
--
--	This 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 library.  If not, see
--	<http://www.gnu.org/licenses/>.
--

--** Support for drawing text onto a raster.
-- @module rastertext

#INCLUDE "raster.module"

--{{{  FUNCTION measure.text
--* Measure the dimensions of some text.
-- This will return the amount of space that the text will occupy when drawn
-- using [@ref draw.text] in the given font.
--
-- @param text String
-- @param font Font
-- @returns width, height Dimensions of the text in pixels
INT, INT FUNCTION measure.text (VAL []BYTE text, VAL []INT font) IS
  font[0] * (SIZE text), font[1]:
--}}}

--{{{  PROC draw.text
--* Draw a string on a raster.
--
-- @param x, y Top left corner coordinates
-- @param text String to draw
-- @param font Font
-- @param colour Colour
-- @param raster Raster
PROC draw.text (VAL INT x, y, VAL []BYTE text, VAL []INT font, VAL INT colour, [][]INT raster)
  VAL INT width IS font[0]:
  VAL INT height IS font[1]:
  VAL []INT data IS [font FROM 2]:
  SEQ yi = 0 FOR height
    []INT line IS raster[y + yi]:
    SEQ i = 0 FOR SIZE text
      []INT part IS [line FROM x + (width * i) FOR width]:
      VAL INT code IS INT text[i]:
      INITIAL INT char IS data[(code * height) + yi]:
      SEQ xi = 0 FOR width
        SEQ
          IF
            (char /\ #80000000) <> 0
              part[xi] := colour
            TRUE
              SKIP
          char := char << 1
:
--}}}
