How to measure text in a RichTextBox in Silverlight - silverlight-4.0

I want to get the height necessary to display the full text in my RichTextBox (when the text extends beyond the set height of the control).
Reminder: Silverlight has no handy TextRenderer.MeasureText like WPF does, nor any other apparent way to measure text.
Doesn't seem like there's any way to do this. I've seen mention of people measuring text of a single font (not mixed as in my RichTextBox) by creating a TextBlock and getting it's Width. Even this doesn't work - it's perfect for some fonts and inconsistent for others.
My app is occasionally connected, so I can't call the server.

As you say, I don't think there's a good way to do this in Silverlight today. There are some functions available in the Document Toolkit by First Floor Software, however those are geared towards working with XPS documents. I'm not sure what you're trying to do, however in Silverlight 5 the RichTextBox does come with the ability to "overflow" text into multiple other RichTextBoxes when the first one cannot display all of the data. This allows you to more easily create a multi-column text layout.
Document Toolkit: http://firstfloorsoftware.com/documenttoolkit
SL5 Video: http://www.silverlight.net/learn/videos/all/silverlight-5-multi-column-linked-text/
SL5 Blog Post: http://10rem.net/blog/2011/04/13/silverlight-5-advancements-in-text

Related

Curve Texblock in XAML / W8.1 throughTemplate?

Is it possible to access the TextBlock Template to change the border and make it curved?
I tried editing the Template through Blend but with no success.
I'm trying to achieve something like this (couldn't get that given solution working):
Curve TextBlock in Windows 8
I'm trying to do this in C#/XAML - WINRT (Windows 8.1)
I don't think a TextBlock has a template you could modify. It's probably just some parameters used to tell DirectWrite what text to render and with what properties. The easiest way to solve it is when your text is constant to just break it apart into multiple single letter TextBlocks and lay them out on a path using Blend or Illustrator. If don't control what text can show up on the path - you'd have to code up the layout algorithm. Chris's link seems like a good place to start.
There isn't a clean way to do this directly in Xaml. Like Filip says, you can approach it by breaking the letters apart. That can work well for long sentences with small letters, but can be pretty chunky with large or connected letters. If you need smoother rendering then you can interop to Direct2D.
MSDN has a Direct2D animated text on a path sample which you could combine with the XAML SurfaceImageSource DirectX interop sample

Why aren't the initial properties of my forms and controls preserved on another computer?

I've made a little Towers of Hanoi game, but nothing what was set on my PC looks the same on a different computer, not even the fonts. Everything is so messed up, I couldn't do it better even if I wanted this to happen. To say it short the backgrounds used for controls and forms are out of place, the initial values used for size and position of both forms and controls are bloody changed, and my project just looks like a huge mess. It's true I've worked in absolute values, because usually a programming language respects the programmer's point of view, and doesn't scales and moves everything the way it wants. If I wanted my project to rescale according to screen resolution I would had used relative coordinates, and made all my forms and controls dimensions be a certain amount of the screen's width and height.
Is there a way to preserve the project just as it was initially designed, so it would look the same on any computer?
I'm using Visual Studio 2010, and Windows 7 as OS.
You don't tell us specifically which properties you've modified from the defaults, or show us a screenshot of the before and after views from which we might be able to infer which modifications you've made. But you did mention something about changing the font, so we'll go with that.
In fact, it does try to preserve your specified properties to the extent possible. But sometimes it is just not possible. For example, if you specify a font for your controls that isn't available on the other computer, then it has no choice but to fall back to a font that is available. If you have any experience with web page design, it is a very similar problem. You have to use a small subset of web-safe fonts to ensure that they will be available on all of your users' computers. That's also why web designers are so keen now on embedding fonts into pages.
Anyway, it goes without saying that if the font has to change, the layout is going to be messed up. Different fonts are different sizes, so different amounts of text are going to fit, causing some to get cut off. That is why, in general, you should avoid changing properties like Font. If you use the defaults, things are a lot more compatible. But neither web pages nor desktop applications are WYSIWYG. You need images or PDF files for that.
Then there are system settings like DPI that can really mess things up, too. Keeping the default font isn't going to help you there. You have to design your application in a smart way. You mention something about relative layouts—these are the ticket. Unfortunately, WinForms doesn't make it easy to do this. It all but forces you into specifying absolute sizes and positions based on the pixel grid, which is mostly a waste of time, as you've seen. I describe in detail how to accomplish this in WinForms in this answer. The AutoSize property will be very useful to you. Of course, you'll also need the dynamically-growing TableLayoutPanel and/or FlowLayoutPanel controls, otherwise you'll end up with automatically sized controls that overlap one another.
Pre-emptive snarky comment: you should totally drop WinForms and use WPF instead. It is new, and cool, and sexy, and all but forces you the other way into pixel-independent layouts. Of course, it also makes it really easy to create butt-ugly, downright unusable applications that look like some of the stuff Microsoft has been churning out over the past couple of years.
#Cody Gray thanks for your insights.
com/qMBJS.png
The first image is how the main menu looks. It's kinda self-explanatory how it should had looked, without the big white margins while the text should had been inside the labels backgrounds
The second one is some in-game footage. And this is just one of the levels. It's so messed up I'm almost sorry I've lost a night doing some heavy work on the image editing side of the matter. I'm not at my own PC right now, so I've tried to rearrange all the stuff directly in Visual Studio's design window, to show how the level should had closely looked. Just imagine it without the white and black margins surrounding some controls.
Also I've tried to set the controls and forms parameters through code, when the form loads, trying to force it to look as it should but the result wasn't any better.

Possible to control PDF layout with iText?

I'm writing some logic to build a large single PDF file that our users can print at their convenience. I'm using Java's iText library (through Clojure's clj-pdf).
I'm trying to have the PDF show the same exact template form on every single page, however I can't seem to find any documentation or indication that one can have PDF content "fit to a page".
The text in these forms varies a little bit, so there's a chance it might require more of fewer text lines per page. This means that the content has a chance of spilling over to the next page, or being too short, making the next page creep up into the previous one, breaking the requirement of "one form per page" for the rest of the document.
I'm trying to figure out if my option is pretty much only to manually check the length of the text on each page and potentially crop it by hand if I goes over n lines, or if the PDF format somehow supports a smart way of having paragraphs+tables+headings all fit in one page. Some UI systems allow you to control how spill-over is handled, anywhere from cropping to resizing the font, so I'm curious if PDF supports anything of that sort.
Edit: ended up going with pagebreaks for simplicity, wasn't aware of that option when I wrote this question.
If you want to take control over the space taken by text, for instance to fit it on a single page, the way to go would be to create a ColumnText object and to add the content in simulation mode. If the text fits the page, add it for real. If it doesn't, use a smaller font size. This is demonstrated in the MovieAds example where snippets of text are fitted into AcroForm fields.

Debugging Paging in Sql Reporting Services

I'm working on my first significant Sql Reporting Services project and am having problems with paging. Most of the reports are already working.
What is happening is that I"m getting different numbers and locations of page breaks between Web Reportviewer, PDF and Word documents. The word is the closest, but none of the three are really correct.
I've looked for the for the obvious like extra paging and making sure the report does not go outside of the left or right margins. My problem is that I'm not sure how to go about troubleshooting reports that pages that do not break in the correct location.
Does anyone have a suggestion where to start?
I'm using VS2008, SQL2008 DE on Vista Dev box.
This isn't really a problem - the different renderers are rendering the report appropriately for their output. The web viewer is optimised for screen-based reading and generally allows more content per page than the PDF viewer does as the PDF viewer is constrained by the paper size that it formats to. Thus you get more pages when rendering for PDF than web; however, the content of the report is exactly the same.
The best illustration of this is the Excel renderer - the Excel renderer renders the entire report onto a single worksheet in most cases (for reports with grouping and page breaks set on the group footer it will render each group on its own worksheet). You wouldn't want the Excel renderer to artificially create worksheets to try to paginate your report. It does the appropriate thing which is to include all the data in one big worksheet even though that may be logically thought of as one big "page".
The web renderer page length is determined by the InteractiveHeight attribute of the report (in the InteractiveSize property in the Properties pane for the report) but the interactive height is an approximation rather than a fixed page break setting and your page breaks may still not conform to the PDF version even though the InteractiveHeight is set to the same length as your target page length.

Finding a word's frame (position and size) on the screen using Cocoa or Carbon

Here's a tough one:
I need to be able to find a word's position and size (its frame) on the screen (its first occurence is enough, from there I should be able to get the next ones).
For example, I would like to be able to detect word positions in (but not limited to) Word, Excel and PowerPoint for Mac, as well as Safari and others.
The solution should be as fast as possible; I should be able to find at least 5-6 words per second and use as little CPU time as possible.
Here's what I thought of so far:
OCR in a window's screenshot / graphics context (any good Open Source framework that works on Mac OS X 10.4 and that can be used in a commercial product?). Evernote is very good at spotting words in images. I don't know if it uses a custom in-house engine or an Open Source / commercial one but that would be the kind of engine I would like to use if this is a "valid" solution. Ideally I would detect the word's frame in the active application's window (how to get the frame of another application?).
Getting some kind of "hook" on Quartz drawing of text and intercepting the location of the word when it's drawn (does not seem very feasible at first glance!).
AppleScript, but it depends a lot on what API the application offers (I don't think you can get a word's coordinates in a Word document from what I've seen) and it's slow.
... out of ideas ...
My goal is to get all the word's frames in a paragraph in the right order based on a string containing the text of the paragraph.
Thanks in advance for any hints!
As a starting place, you may want to take a look at QuickCursor's code. It retrieves text from many different applications through the AX Accessibility APIs. Now, it won't grab the pixel placement of the word, but it will at least return the NSString associated with the text in that UI element. Of course this means that the app in question has to support these APIs; I don't know if the MS Office suite would. In addition, it only supports editable elements, so an un-editable webpage in Safari won't work either. But it may give you a starting point for some ideas.
Take a look at the QCUIElement.{m,h}, and then the implementation in the QCAppDelegate.m (beginQuickCursorEdit:)... the implementation of his abstracted QCUIElement seems to be as simple as:
QCUIElement *focusedElement = [QCUIElement focusedElement];
id value = focusedElement.value;
Edit: Aha! Check out the Accessibility Inspector Sample code: UIElementInspector. It can actually get the AXPosition of elements on a page. Now, it's not word-by-word, but we're getting closer. It'll tell you the x,y placement of a textblock, as well as the words contained in the textblock.
This is possible, but very hard to get working reliably. You can play with Spell Catcher's Direct Connect feature to see an example.