program project1;
{$codepage UTF8}
{
Based on:
https://codeload.github.com/bert/262331/zip/b30b437b73ba054d69298aee7d73a5547126e7c7
}
uses
cairo, pango, pangocairo, sysutils;
var
cr: Pcairo_t;
filename: pchar ;
status: cairo_status_t ;
surface: Pcairo_surface_t;
layout: PPangoLayout;
desc: PPangoFontDescription;
ink: PPangoRectangle;
logical: PPangoRectangle;
width: PLongint;
iWidth:LongInt;
Str, sWrap: string;
begin
if (argc <> 4) then
begin
WriteLn('Usage: pangocairo STRING WIDTH WRAP_MODE');
WriteLn('Using default values');
str := 'Just one more test اللغة العربية';
iWidth := 100;
sWrap := 'word';
end
else
begin
str := argv[1];
iWidth := StrToInt(argv[2]);
sWrap := argv[3];
end;
{ Create surface and clear to all-white. }
surface := cairo_pdf_surface_create ('foo.pdf', 200, 200);
cr := cairo_create (surface);
cairo_set_source_rgb (cr, 1.0, 1.0, 1.0);
cairo_paint (cr);
{ Create Pango layout. }
layout := pango_cairo_create_layout (cr);
desc := pango_font_description_from_string ('Sans 12');
{ Changing font/style examples }
//desc := pango_font_description_from_string ('Sans 40px');
//desc := pango_font_description_from_string ('Sans Bold 12');
//desc := pango_font_description_from_string ('Times New Roman 12');
pango_layout_set_font_description (layout, desc);
pango_font_description_free (desc);
pango_layout_set_text (layout, @str[1], -1);
{ Set Pango options according to command line. }
pango_layout_set_width (layout, iWidth);
if (CompareStr(sWrap, 'word')=0) then
pango_layout_set_wrap (layout, PANGO_WRAP_WORD)
else if (CompareStr(sWrap, 'char')=0) then
pango_layout_set_wrap (layout, PANGO_WRAP_CHAR)
else if (CompareStr(sWrap, 'word-char')=0) then
pango_layout_set_wrap (layout, PANGO_WRAP_WORD_CHAR)
else
begin
WriteLn('WRAP_MODE must be ''word'' or ''char'' or ''word-char''');
exit;
end;
{ Draw layout. }
cairo_set_source_rgb (cr, 0.0, 0.0, 0.0);
cairo_move_to (cr, 0, 0);
pango_cairo_show_layout (cr, layout);
{ Print various sizes. }
pango_layout_get_size (layout, width, nil);
if width<>nil then
writeln(format('size: width=%d', [width^]));
pango_layout_get_extents (layout, ink, logical);
if (ink<>nil) and (logical<>nil) then
writeln(format('extents: ink width=%d, logical width=%d', [ink^.width, logical^.width]));
pango_layout_get_pixel_extents (layout, ink, logical);
if (ink<>nil) and (logical<>nil) then
writeln(format('pixel extents: ink width=%d, logical width=%d',[ink^.width, logical^.width]));
{ Clean up. }
//g_object_unref (layout);
cairo_destroy (cr);
cairo_surface_finish (surface);
status := cairo_surface_status (surface);
cairo_surface_destroy (surface);
if (status <> CAIRO_STATUS_SUCCESS) then
begin
writeln(format('Could not save pdf to %s', [filename]));
exit;
end;
end.