Italian community of Lazarus and Free Pascal

Programmazione => Graphics => Topic aperto da: xinyiman - Ottobre 02, 2019, 10:56:21 am

Titolo: (RISOLTO) Problema canvas e textout
Inserito da: xinyiman - Ottobre 02, 2019, 10:56:21 am
Ciao ragazzi, devo fare una piccola applicazione web (in futuro sarà una cgi, ora per test è in server embedded) che mi manipola delle immagini. Sembra funzionare tutto, ma non la scrittura sull'immagine. Qualche anima pia con maggior esperienza di me nel settore grafica saprebbe dirmi dove sbaglio?
A questo indirizzo trovate da scaricare il test che replica l'errore.

https://forum.lazarus.freepascal.org/index.php/topic,46917.0.html
Titolo: Re:Problema canvas e textout
Inserito da: Mimmo - Ottobre 02, 2019, 02:49:43 pm
Ciao,
non so darti una risposta certa ma potresti guardare qui https://wiki.lazarus.freepascal.org/fcl-image (https://wiki.lazarus.freepascal.org/fcl-image).
Forse devi creare espressamente il font.
Titolo: Re:Problema canvas e textout
Inserito da: Stilgar - Ottobre 02, 2019, 04:51:45 pm
Mumble

Usi il canvas per produrre il testo?
https://wiki.freepascal.org/fcl-image (https://wiki.freepascal.org/fcl-image)

Codice: [Seleziona]
program fontdraw;

{$mode objfpc}{$H+}

uses
  Classes, SysUtils, FPimage, FPImgCanv, ftfont, FPWritePNG, FPCanvas;

procedure TestFPImgFont;
var
  Img: TFPMemoryImage;
  Writer: TFPWriterPNG;
  ms: TMemoryStream;
  ImgCanvas: TFPImageCanvas;
  fs: TFileStream;
  AFont: TFreeTypeFont;
begin
  Img:=nil;
  ImgCanvas:=nil;
  Writer:=nil;
  ms:=nil;
  fs:=nil;
  AFont:=nil;
  try
    // initialize free type font manager
    ftfont.InitEngine;
    FontMgr.SearchPath:='/usr/share/fonts/truetype/ttf-dejavu/';
    AFont:=TFreeTypeFont.Create;

    // create an image of width 200, height 100
    Img:=TFPMemoryImage.Create(200,100);
    Img.UsePalette:=false;
    // create the canvas with the drawing operations
    ImgCanvas:=TFPImageCanvas.create(Img);

    // paint white background
    ImgCanvas.Brush.FPColor:=colWhite;
    ImgCanvas.Brush.Style:=bsSolid;
    ImgCanvas.Rectangle(0,0,Img.Width,Img.Height);

    // paint text
    ImgCanvas.Font:=AFont;
    ImgCanvas.Font.Name:='DejaVuSans';
    ImgCanvas.Font.Size:=20;
    ImgCanvas.TextOut(10,30,'Test');

    // write image as png to memory stream
    Writer:=TFPWriterPNG.create;
    ms:=TMemoryStream.Create;
    writer.ImageWrite(ms,Img);
    // write memory stream to file
    ms.Position:=0;
    fs:=TFileStream.Create('testfont.png',fmCreate);
    fs.CopyFrom(ms,ms.Size);
  finally
    AFont.Free;
    ms.Free;
    Writer.Free;
    ImgCanvas.Free;
    Img.Free;
    fs.Free;
  end;
end;

begin
  TestFPImgFont;
end.
Titolo: Re:Problema canvas e textout
Inserito da: xinyiman - Ottobre 03, 2019, 08:37:38 am
Grazie mille alla fine mi avete dato sia la soluzione qui che sul forum internazionale.