Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Rotated text using Drawtext

Rotated text using Drawtext

Scheduled Pinned Locked Moved C / C++ / MFC
helpquestion
6 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    Mary Chennai
    wrote on last edited by
    #1

    Hi, I have used the code below to draw rotated text using DrawText. //Draw rotated text double Escapement = -401; CString str = "Hello World"; CFont newFont; newFont.CreateFont(24, 0, Escapement, Escapement, 0, 0, 0, 0, 0, 0, 0, 0, 0, "Arial"); CFont* pFont = pDC->SelectObject(&newFont); // draw the text CSize TextSize = pDC->GetTextExtent(str); CRect rect(0,0,TextSize.cx, TextSize.cy); pDC->DrawText(str, str.GetLength(), rect, DT_CENTER); Text is diplayed fine for non-rotated text. But for rotated text(ie, with Escapemnt != 0), the text appears to be clipped or sometimes not displayed at all. This is because the rect value is too restrictive for rotated text. Please can someone help me to deduce the rect value correctly for rotated texts? Regards Mary

    C 1 Reply Last reply
    0
    • M Mary Chennai

      Hi, I have used the code below to draw rotated text using DrawText. //Draw rotated text double Escapement = -401; CString str = "Hello World"; CFont newFont; newFont.CreateFont(24, 0, Escapement, Escapement, 0, 0, 0, 0, 0, 0, 0, 0, 0, "Arial"); CFont* pFont = pDC->SelectObject(&newFont); // draw the text CSize TextSize = pDC->GetTextExtent(str); CRect rect(0,0,TextSize.cx, TextSize.cy); pDC->DrawText(str, str.GetLength(), rect, DT_CENTER); Text is diplayed fine for non-rotated text. But for rotated text(ie, with Escapemnt != 0), the text appears to be clipped or sometimes not displayed at all. This is because the rect value is too restrictive for rotated text. Please can someone help me to deduce the rect value correctly for rotated texts? Regards Mary

      C Offline
      C Offline
      Chris Losinger
      wrote on last edited by
      #2

      you might try using DrawText with the DT_CALCRECT flag

      image processing toolkits | batch image processing

      M 1 Reply Last reply
      0
      • C Chris Losinger

        you might try using DrawText with the DT_CALCRECT flag

        image processing toolkits | batch image processing

        M Offline
        M Offline
        Mary Chennai
        wrote on last edited by
        #3

        Yes I tried using DT_CALCRECT. But that isn't giving me a rotated rectangle.

        L M CPalliniC 3 Replies Last reply
        0
        • M Mary Chennai

          Yes I tried using DT_CALCRECT. But that isn't giving me a rotated rectangle.

          L Offline
          L Offline
          Luc Pattyn
          wrote on last edited by
          #4

          FYI: 1. DT_CALCRECT returns the size of the bounding rectangle. 2. A rotated rectangle is still a rectangle. :)

          Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles] Nil Volentibus Arduum

          Please use <PRE> tags for code snippets, they preserve indentation, and improve readability.

          1 Reply Last reply
          0
          • M Mary Chennai

            Yes I tried using DT_CALCRECT. But that isn't giving me a rotated rectangle.

            M Offline
            M Offline
            Maximilien
            wrote on last edited by
            #5

            Me think the rectangle is aligned to the coordinate system or the "screen", not to the coordinate system of the rotated text.

            Watched code never compiles.

            1 Reply Last reply
            0
            • M Mary Chennai

              Yes I tried using DT_CALCRECT. But that isn't giving me a rotated rectangle.

              CPalliniC Offline
              CPalliniC Offline
              CPallini
              wrote on last edited by
              #6

              As suggested by Maximilien the computed rectangle is always aligned to screen coordinate space (i.e. rotation is not performed). The following code (warning is a quick and dirty hack...)

              #include <float.h>
              #include <math.h>

              //..

              // draw the text
              CSize TextSize = dc.GetTextExtent(str);
              //CRect rect(0,0,TextSize.cx, TextSize.cy);

              double x[4],y[4];
              double alpha = ((double) Escapement) * atan(1.) * 4 / 1800;
              x[0] = .0;
              y[0] = .0;
              x[1] = TextSize.cx * cos(alpha);
              y[1] = -TextSize.cx * sin(alpha);
              x[2] = TextSize.cx * cos(alpha) +TextSize.cy * sin(alpha);
              y[2] = - TextSize.cx * sin(alpha) + TextSize.cy * cos(alpha);
              x[3] = TextSize.cy * sin(alpha);
              y[3] = TextSize.cy *cos(alpha);

              double xoff=0.,yoff = 0.;
              for (int i=0; i<4; i++)
              {
              if ( xoff > x[i] ) xoff = x[i];
              if ( yoff > y[i] ) yoff = y[i];
              }
              for (int i=0; i<4; i++)
              {
              x[i]-=xoff;
              y[i]-=yoff;
              }
              dc.MoveTo(x[0],y[0]);
              dc.LineTo(x[1],y[1]);
              dc.LineTo(x[2],y[2]);
              dc.LineTo(x[3],y[3]);
              dc.LineTo(x[0],y[0]);

              double xmin = DBL_MAX;
              double ymin = DBL_MAX;
              double xmax = 0.;
              double ymax = 0.;
              for (int i=0; i<4; i++)
              {
              if (xmin > x[i]) xmin = x[i];
              if (ymin > y[i]) ymin = y[i];
              if (xmax < x[i]) xmax = x[i];
              if (ymax < y[i]) ymax = y[i];
              }
              CBrush *pOldBrush = (CBrush*)dc.SelectStockObject(NULL_BRUSH);
              dc.Rectangle(xmin, ymin, xmax, ymax);
              dc.SelectObject(pOldBrush);

              CRect rect(xmin-xoff,ymin,xmax-xoff,ymax);

              dc.DrawText(str, str.GetLength(), rect, DT_NOCLIP);
              //...

              Shows you:

              • The 'rotated text bounding rectangle'
              • The 'screen aligned rectangle' bounding the rotated one
              • The rotated text

              :)

              If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
              This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
              [My articles]

              In testa che avete, signor di Ceprano?

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups