<html xmlns:v="urn:schemas-microsoft-com:vml" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:w="urn:schemas-microsoft-com:office:word" xmlns:m="http://schemas.microsoft.com/office/2004/12/omml" xmlns="http://www.w3.org/TR/REC-html40">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta name="Generator" content="Microsoft Word 15 (filtered medium)">
<style><!--
/* Font Definitions */
@font-face
{font-family:"Cambria Math";
panose-1:2 4 5 3 5 4 6 3 2 4;}
@font-face
{font-family:Calibri;
panose-1:2 15 5 2 2 2 4 3 2 4;}
/* Style Definitions */
p.MsoNormal, li.MsoNormal, div.MsoNormal
{margin:0cm;
margin-bottom:.0001pt;
font-size:11.0pt;
font-family:"Calibri",sans-serif;}
a:link, span.MsoHyperlink
{mso-style-priority:99;
color:#0563C1;
text-decoration:underline;}
a:visited, span.MsoHyperlinkFollowed
{mso-style-priority:99;
color:#954F72;
text-decoration:underline;}
p.msonormal0, li.msonormal0, div.msonormal0
{mso-style-name:msonormal;
mso-margin-top-alt:auto;
margin-right:0cm;
mso-margin-bottom-alt:auto;
margin-left:0cm;
font-size:11.0pt;
font-family:"Calibri",sans-serif;}
span.EmailStyle18
{mso-style-type:personal-reply;
font-family:"Calibri",sans-serif;
color:windowtext;}
.MsoChpDefault
{mso-style-type:export-only;
font-family:"Calibri",sans-serif;
mso-fareast-language:EN-US;}
@page WordSection1
{size:612.0pt 792.0pt;
margin:72.0pt 72.0pt 72.0pt 72.0pt;}
div.WordSection1
{page:WordSection1;}
--></style><!--[if gte mso 9]><xml>
<o:shapedefaults v:ext="edit" spidmax="1026" />
</xml><![endif]--><!--[if gte mso 9]><xml>
<o:shapelayout v:ext="edit">
<o:idmap v:ext="edit" data="1" />
</o:shapelayout></xml><![endif]-->
</head>
<body lang="EN-GB" link="#0563C1" vlink="#954F72">
<div class="WordSection1">
<p class="MsoNormal"><span style="mso-fareast-language:EN-US">Hello Eric,<o:p></o:p></span></p>
<p class="MsoNormal"><span style="mso-fareast-language:EN-US"><o:p> </o:p></span></p>
<p class="MsoNormal"><span style="mso-fareast-language:EN-US">I was about to get started so your reply is most timely and appreciated.<o:p></o:p></span></p>
<p class="MsoNormal"><span style="mso-fareast-language:EN-US"><o:p> </o:p></span></p>
<p class="MsoNormal"><span style="mso-fareast-language:EN-US">I’m competent in C and can see what you’re doing here. I should be able to port to Python easily.<o:p></o:p></span></p>
<p class="MsoNormal"><span style="mso-fareast-language:EN-US"><o:p> </o:p></span></p>
<p class="MsoNormal"><span style="mso-fareast-language:EN-US">Thanks again.<o:p></o:p></span></p>
<p class="MsoNormal"><span style="mso-fareast-language:EN-US"><o:p> </o:p></span></p>
<p class="MsoNormal"><span style="mso-fareast-language:EN-US">/W<o:p></o:p></span></p>
<p class="MsoNormal"><span style="mso-fareast-language:EN-US"><o:p> </o:p></span></p>
<p class="MsoNormal"><b><span lang="EN-US">From:</span></b><span lang="EN-US"> cecashon@aol.com <cecashon@aol.com>
<br>
<b>Sent:</b> 30 April 2019 18:48<br>
<b>To:</b> Warren Vick <wvick@europa.uk.com>; cairo@cairographics.org<br>
<b>Subject:</b> Re: [cairo] Text stuffing with Cairo/pycairo?<o:p></o:p></span></p>
<p class="MsoNormal"><o:p> </o:p></p>
<div>
<p class="MsoNormal" style="margin-bottom:12.0pt"><span style="font-size:10.0pt;font-family:"Arial",sans-serif;color:black"><br>
Hi Warren,<br>
<br>
Have you made some progress with this?<br>
<br>
Well, you might need some font metrics and get the transforms in order to be able to do this. The following is in C but it might be helpful. Probably need to change a few things around but it is a start.<br>
<br>
Eric <br>
<br>
<br>
//gcc -Wall cairo_text_size.c -o cairo_text_size `pkg-config --cflags --libs cairo` -lm<br>
<br>
#include<cairo.h><br>
#include<cairo-pdf.h><br>
#include<stdio.h><br>
#include<math.h><br>
<br>
static void text_in_box(cairo_t *cr, double center_box_x, double center_box_y, double box_width, double box_height, double rotate, char *string);<br>
<br>
int main()<br>
{<br>
double width=600.0;<br>
double height=600.0;<br>
char *string1="Center String";<br>
char *string2="Translate Stringssssss!";<br>
char *string3="Rotate String";<br>
char *string4="Joy!!!";<br>
<br>
cairo_surface_t *surface=cairo_pdf_surface_create("cairo_test1.pdf", width, height);<br>
cairo_t *cr=cairo_create(surface);<br>
<br>
//Paint background.<br>
cairo_set_source_rgba(cr, 1.0, 1.0, 1.0, 1.0);<br>
cairo_paint(cr);<br>
<br>
//Draw from the center of surface. Good for testing text and box alignment.<br>
cairo_translate(cr, width/2.0, height/2.0);<br>
//Scale the drawing if needed.<br>
cairo_scale(cr, 1.0, 1.0);<br>
<br>
//Draw cartesian coordinates.<br>
cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 1.0);<br>
cairo_move_to(cr, -width/2.0, 0.0);<br>
cairo_line_to(cr, width/2.0, 0.0);<br>
cairo_stroke(cr);<br>
cairo_move_to(cr, 0.0, -height/2.0);<br>
cairo_line_to(cr, 0.0, height/2.0);<br>
cairo_stroke(cr);<br>
<br>
cairo_select_font_face(cr, "courier", CAIRO_FONT_SLANT_NORMAL, CAIRO_FONT_WEIGHT_BOLD);<br>
//Since the font gets scaled to the box the default font size should be fine.<br>
//cairo_set_font_size(cr, 10.0);<br>
<br>
//Draw text at center.<br>
text_in_box(cr, 0.0, 0.0, 350.0, 100.0, 0.0, string1);<br>
<br>
//Translate text.<br>
text_in_box(cr, -120.0, -150.0, 300.0, 30.0, 0.0, string2);<br>
<br>
//Rotate text.<br>
text_in_box(cr, 150.0, 150.0, 121.0, 52.0, M_PI/4.0, string3);<br>
<br>
text_in_box(cr, -150.0, 185.0, 121.0, 52.0, -M_PI/4.0, string4);<br>
<br>
text_in_box(cr, 150.0, -190.0, 225.0, 52.0, -M_PI/4.0, string4);<br>
<br>
printf("Output to cairo_test1.pdf\n");<br>
<br>
cairo_destroy(cr);<br>
cairo_surface_destroy(surface);<br>
<br>
return 0;<br>
}<br>
static void text_in_box(cairo_t *cr, double center_box_x, double center_box_y, double box_width, double box_height, double rotate, char *string)<br>
{<br>
double move_x=0.0;<br>
double move_y=0.0;<br>
double scale_x=0.0;<br>
double scale_y=0.0;<br>
cairo_text_extents_t te;<br>
cairo_font_extents_t fe;<br>
<br>
//Get some font metrics.<br>
cairo_text_extents(cr, string, &te);<br>
cairo_font_extents(cr, &fe);<br>
move_x=-te.width/2.0-te.x_bearing;<br>
move_y=-te.height/2.0-te.y_bearing;<br>
//Pad the rectangle a little.<br>
scale_x=-te.width/2.0-te.x_bearing-4.0;<br>
scale_y=-te.height/2.0-te.y_bearing+fe.descent;<br>
<br>
//Order transforms and draw.<br>
cairo_save(cr);<br>
cairo_translate(cr, center_box_x, center_box_y);<br>
cairo_rotate(cr, rotate);<br>
cairo_scale(cr, box_width/fabs(2.0*move_x), box_height/fabs(2.0*move_y));<br>
cairo_set_source_rgba(cr, 0.0, 1.0, 0.0, 0.4);<br>
cairo_rectangle(cr, -fabs(scale_x), -fabs(scale_y), fabs(2.0*scale_x), fabs(2.0*scale_y));<br>
cairo_fill(cr); <br>
cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 1.0);<br>
cairo_move_to(cr, move_x, move_y);<br>
cairo_show_text(cr, string);<br>
cairo_restore(cr); <br>
}<o:p></o:p></span></p>
</div>
<div>
<p class="MsoNormal"><span style="font-size:10.0pt;font-family:"Arial",sans-serif;color:black"><o:p> </o:p></span></p>
</div>
<p class="MsoNormal"><o:p> </o:p></p>
</div>
</body>
</html>