How to create, draw and/or print formatted text.

I'm programming for windows using mfc VC++ 2003 and need a suggestion on ways to solve this problem...

My windows mfc app creates a small amount of formatted text (bold, italics and different size fonts).  Later the text needs to be drawn or printed, but the lower level library that does the drawing & printing, gets a CDC* and a char* and that is it (I'm exaggerating a bit but the point is windows/mfc api and controls are not available at the lower level).  Currently all the drawing is done via CDC->DrawText

I was thinking I could use CRichEditCtrl to create RTF text (assuming that's what it does) and then later hand code the drawing of RTF text, but I'd rather not rewrite how to draw from RTF.  Is this approach reasonable?  Are there libraries that do draw/print RTF already?  Is there a better way?

Thanks!
DavidHandsAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

PriyeshCommented:
You said the lower level library does not get access to windows/mfc api. Then how does it know about a CDC and if it's HDC, then still the manipulations are through windows api right? I am not understanding what the restriction is then.
0
bdunz19Commented:
Hi DavidHands,
If your library accepts a CDC or a HDC all you need to do is declare an CFont or HFONT and select it into the CDC/HDC before passing it to the library.

// In your class def:
CFont m_font;

// In your class constructor or really anywhere:
m_font.CreateFontA(10, 0, 0, 0, FW_NORMAL, 0, 0, 0, ANSI_CHARSET, OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS, ANTIALIASED_QUALITY, DEFAULT_PITCH | FF_ROMAN, "MS Sans Serif");

// Then before passing your CDC to the library:
CFont *pOldFont = dc.SelectObject(&m_font); // Select the font into the CDC
dc.SetTextColor(RGB(255,0,0)); // You could also change the text color of the CDC
dc.SetBkMode(TRANSPARENT); // You could also chose to make the background of the text transparent
LibraryCall(&dc); // This is where you pass your CDC* to the library
dc.SelectObject(pOldFont);

Well, good luck!
Brandon
0
DavidHandsAuthor Commented:
Hi Brandon,

Your example code is actually very close to what I currently do.  What is different for me now is I do not know the formatting until runtime.  For example, suppose the user wants the app to draw "Hello world" in Arial 12pt and bold the "Hello", but next time time they want to draw "Hello world" in Bookman 10pt and italicize "Hello".

So now I need a way to store and retrieve the formatting information along with the text so I can correctly set up the CDC prior to drawing.  This is what made me think of using RTF because it's contains the text and formatting in an ASCII format (right?), but I've never used RTF.  I'm not sure what options are available I just know I don't want to reinvent the wheel if I don't need to.

Thanks.
0
bdunz19Commented:
Hi again,

Well that makes more sense and now I see your problem. Let me warn you, as you are about to embark on a very difficult journey! I spent 2 months writing something over last summer that works just like you are saying; it was an html/css printer DLL. Basically the DLL I wrote was fed standard html with cascade style sheets as the formating data, and the DLL then parsed through it to popback a final CDC that would then be sent to a printer. Something like this is not by any means easy. The DLL I wrote only supports about half of the css standard format possibilities (this was because of a lack of time).

The other problem I see you having is that if you can't modify the "low level" library that you are calling, how will your library possibly know how to correctly parse the text or data you are sending it to create the final visual formating of the text? You might in the end have to replace this library with on of your own...

Anyway, those are my thoughts, let me know what you think or how you might be able to simplify this problem, as most people don't have 2 months to blow away writing something like this.

Good luck,
Brandon
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
PriyeshCommented:
David,
If you can store the formatted text as RTF, you should be able to stream it into a RichEditCtrl and print from it.

Look at CRichEditCtrl and the StreamIn function.
Priyesh
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
System Programming

From novice to tech pro — start learning today.