unit dfont_plugin;

{$MODE FPC}
{$MODESWITCH DEFAULTPARAMETERS}
{$MODESWITCH OUT}
{$MODESWITCH RESULT}

interface

uses
  derror,
  dstream;

type
PFontBackend = ^TFontBackend;
TFontBackend = object
type
  PGlyphOpaque = Pointer;

public
  constructor Init;
  destructor Done; virtual;

  function IsAtlas: Boolean; virtual;
      // The font is already atlas
  function IsRasterizable: Boolean; virtual;
  function IsScalable: Boolean; virtual;

  function GetKerning(U1, U2: UInt32): UInt32; virtual;

// for non-vector fonts:
  function HasSize(Size: SizeUInt): Boolean; virtual;
  function GetAvailableSizeGreater(Size: SizeUInt): SizeUInt; virtual;
  function GetAvailableSizeLess(Size: SizeUInt): SizeUInt; virtual;

// state
  function SetGlyph(U: UInt32): Boolean; virtual;
  procedure SetSize(Size: SizeUInt); virtual;
  procedure SetOutline(Size: SizeUInt); virtual;
  procedure SetTransform(XX, XY, YX, YY: Single); virtual;
  procedure GetGlyphSizes(out W, H: SizeInt); virtual;
  procedure GetGlyphAdvance(out W, H: SizeInt); virtual;

  function GetAtlasWidth: SizeUInt; virtual;
  function GetAtlasHeight: SizeUInt; virtual;
  function GetAtlas: Pointer; virtual;

  procedure RasterizeRGBA(
        U: UInt32;
        X, Y: PtrInt;
        W, H: PtrInt;
        RGBA: Pointer); virtual;
  function GenerateTriangles2f(
        U: UInt32;
        Vertices: Pointer;
        Stride: PtrInt;
        VerticesLimit: PtrInt): SizeUInt; virtual;
    // For vector fonts. Returns number of vertices generated.
end;

PFontPlugin = ^TFontPlugin;
TFontPlugin = object
public
  constructor Init;
  destructor Done; virtual;
  function TryLoad(Stream: PStream;
                   out FontBackend: PFontBackend;
                   out Error: TError): Boolean; virtual;
end;

function TryLoadFont(Stream: PStream;
                     out FontBackend: PFontBackend;
                     out Error: TError): Boolean;
procedure RegisterFontPlugin(FontPlugin: PFontPlugin);

implementation

var
  FP: PFontPlugin; // TODO list

constructor TFontBackend.Init;
begin
end;

destructor TFontBackend.Done;
begin
end;

function TFontBackend.IsAtlas: Boolean;
begin
  Exit(False);
end;

function TFontBackend.IsRasterizable: Boolean;
begin
  Exit(False);
end;

function TFontBackend.IsScalable: Boolean;
begin
  Exit(False);
end;

function TFontBackend.GetKerning(U1, U2: UInt32): UInt32;
begin
  Exit(0);
end;

function TFontBackend.HasSize(Size: SizeUInt): Boolean;
begin
  Exit(False);
end;

function TFontBackend.GetAvailableSizeGreater(Size: SizeUInt): SizeUInt;
begin
  Exit(0);
end;

function TFontBackend.GetAvailableSizeLess(Size: SizeUInt): SizeUInt;
begin
  Exit(0);
end;

function TFontBackend.SetGlyph(U: UInt32): Boolean;
begin
  Exit(False);
end;

procedure TFontBackend.SetSize(Size: SizeUInt);
begin
end;

procedure TFontBackend.SetOutline(Size: SizeUInt);
begin
end;

procedure TFontBackend.SetTransform(XX, XY, YX, YY: Single);
begin
end;

procedure TFontBackend.GetGlyphSizes(out W, H: SizeInt);
begin
end;

procedure TFontBackend.GetGlyphAdvance(out W, H: SizeInt);
begin
end;

function TFontBackend.GetAtlasWidth: SizeUInt;
begin
  Exit(0);
end;

function TFontBackend.GetAtlasHeight: SizeUInt;
begin
  Exit(0);
end;

function TFontBackend.GetAtlas: Pointer;
begin
  Exit(nil);
end;

procedure TFontBackend.RasterizeRGBA(
      U: UInt32;
      X, Y: PtrInt;
      W, H: PtrInt;
      RGBA: Pointer);
begin
end;

function TFontBackend.GenerateTriangles2f(
      U: UInt32;
      Vertices: Pointer;
      Stride: PtrInt;
      VerticesLimit: PtrInt): SizeUInt;
begin
  Exit(0);
end;

constructor TFontPlugin.Init;
begin
end;

destructor TFontPlugin.Done;
begin
end;

function TFontPlugin.TryLoad(Stream: PStream;
                 out FontBackend: PFontBackend;
                 out Error: TError): Boolean;
begin
  Exit(False);
end;


function TryLoadFont(Stream: PStream;
                     out FontBackend: PFontBackend;
                     out Error: TError): Boolean;
begin
  Exit(FP^.TryLoad(Stream, FontBackend, Error));
end;

procedure RegisterFontPlugin(FontPlugin: PFontPlugin);
begin
  FP := FontPlugin;
end;

end.
