Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8563611
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T16:59:54+00:00 2026-06-11T16:59:54+00:00

I am drawing a graph using cairo in gtk of C language. I used

  • 0

I am drawing a graph using cairo in gtk of C language. I used cairo_show_text to show text. It is easy to define the center point for center-align. But I didn’t know how to make the text center-align.

The problem is that I don’t know how to calculate text length(It is also concerning about font size I think). If I can get the text legth, I can move to the appropriate point by using cairo_move_to and then show the text by cairo_show_text.

Any suggestion or any other approach?

According to the followed liberforce’s comment, the solution is

/* howto_move: 0 -- cairo_move_to, 1 -- cairo_rel_move_to
 * x, y is the coordinate of the point for center-align. Whether it is absolute
 * or relative coordinate depends on `howto_move'
 */
void cairo_text_align_horizontal_center (cairo_t *cr, char *text, int if_vertical, int howto_move, double x, double y)
{
  cairo_text_extents_t te;
  double new_x, new_y;

  cairo_text_extents(cr, text, &te);
  if(!if_vertical)
  {
    new_x = x - (te.x_bearing + te.width / 2);
    new_y = y;
  }
  else
  {
    new_x = x;
    new_y = y + (te.x_bearing + te.width / 2);
  }
  if(howto_move == 0)
    cairo_move_to(cr, new_x, new_y);
  else
    cairo_rel_move_to(cr, new_x, new_y);
  cairo_save(cr);
  if(!if_vertical)
    cairo_rotate(cr, 0);
  else
    cairo_rotate(cr, - PI / 2.0);
  cairo_show_text(cr, text);
  cairo_restore(cr);
  cairo_stroke(cr);
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T16:59:54+00:00Added an answer on June 11, 2026 at 4:59 pm

    Look at the cairo tutorial text alignment section.

    Keep in mind that for text, the Cairo API is a bit limited. For more advanced stuff, you’ll need pangocairo.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I a drawing a graph using Cairo (pycairo specifically) and I need to know
I'm using core-plot0.4 for drawing waterfall graph. I have used barFillForBarPlot for coloring bars,
I'm drawing as scrollable graph using a custom extension of CALayer with a bunch
Is there a function for drawing a caption box underneath a figure/graph using matplotlib?
I am using the following code for drawing a plot on a graph in
I am using networkx (a python graph-drawing package) http://networkx.lanl.gov/index.html for one of my project.
I'm drawing a graph in a WPF application, but lines drawn using drawingContext.DrawLine(...) are
Here is the best js graph drawing thing - http://thejit.org/static/v20/Jit/Examples/Other/example2.html It is so impressive,
I'm currently porting my jruby/java2d Graph Drawing/Layouting application to macruby/cocoa. Therefore I need to
I want to use core-plot for drawing line graph dynamically. data won't come at

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.