font size bug with CGContextShowTextAtPoint - macos

I have some rather simple code drawing some text into a CGContext. Here is an excerpt (slightly edited).
CGContextSelectFont(context, "Helvetica", 1.5, kCGEncodingMacRoman);
CGContextShowTextAtPoint(context, xpos, ypos, "Hello", 5);
The text renders ok. For some unknown reason, however, the font changes to a smaller size after I click in the view containing the context. Also when I resize the window containing the view the font returns to original size. What is the reason for this?

1.5 points is mighty tiny to begin with. Assuming no other scaling is in effect, that will be one whole pixel and a blurry pixel above it on the screen.
You're probably seeing a bug that I ran into myself: On entry into drawRect:, the current context's text matrix was not the identity matrix. In my case, I saw it contain a scale by 13 on both axes, plus a translation. (Possibly left over from drawing the title bar.) I filed this in Radar as #10585106, in case you want to file your own and cite it.
The solution is to set the text matrix back to the identity transform before trying to draw text.
Once you do that, you'll find that your text will be exactly as tiny as you asked for it to be. You should change your font size to something more reasonable; Core Text contains a function to get the system fonts (from which you can get their sizes), and AppKit's NSFont class contains methods for the same purpose.

Related

how to make a bitmap to fit to the static picture control of a property sheet?

I am having a property sheet and added three pages.In the first page I added a picture control and in the properties of that picture control for "Image" property I am setting a bitmap.And was able to load very successfully.But,the problem here I faced when I run my exe then I am getting bitmap on the page header(which is some kind of banner for the page.)But what I noticed is there is some gap between the banner bitmap and the frame edge (which is occurring at the right top corner). And when I ran my xxx.exe on a Japanese machine then I had observed like the sheet is somewhat stretched and banner bitmap is not stretched completely till the edge of the sheet (top right corner)for Instance if we take a dialog on load a bitmap on it then we can observe that it is entirely got stretched till the edge.
So how can we avoid this issue like whatever the operating system it is and whatever the resolution it might be that banner should get stretched till the edge of the sheet.The gap has to be removed.
I am not loading the bitmap dynamically (setting in the properties.)
Can anyone please help me ti achieve this
The reason you get different results on different systems is that dialogs and controls are sized using "dialog units" which are based on the average size of the font, rather than an absolute number of pixels.
By default a static image control will resize itself to the size of the bitmap it's displaying. So if the dialog itself ends up bigger than normal (because the font is physically bigger), the picture control will appear to have shrunk, leaving a gap.
You could try setting the SS_REALSIZECONTROL style on the static control. The normal behavior of the static control (when displaying a picture) is to resize itself to the size of the bitmap, however the SS_REALSIZECONTROL style overrides that behavior and instead causes the bitmap to be resized to the size of the control. However the results of that may be less than optimal (e.g. the aspect ratio will probably be wrong), so instead you may want to look into scaling the bitmap yourself.

Xcode GLKit printing Text on GLKView without using UIImages

I have an app, its a small game using opengles with GLKit.
No im wondering how it works when i want to draw text on
my screen (if it is possible).
How can i do it?
i draw all of my game objects using images (wrapped in some kind
of sprite). its possible to scale, to move, and to rotate.
everything works fine.
but finding out how it works to print text on that glkview
gets me deep inside of problems ^^
I dont want to use uiimages cause i also dont know how
to present uiimages on a glkview.
There are a number of ways to do what you want:
1) Have an image with all the text glyphs you need in it. For example, if your application is in English, you'd have the 26 uppercase and 26 lowercase letters in the image. Upload that texture to the GPU and use the proper texture coordinates or glSubTexImage2d() to pull out the glyphs you need. (It's not clear to me if this is what you meant by not wanting a UIImage. It doesn't have to be a UIImage, though that's probably easiest.)
2) Every time you need to display text, draw it on the CPU on the fly, and upload the entire word, phrase, or sentence as a texture. You could create a CGBitmapContext and use Core Graphics to draw text to it. Then upload it using glTexImage2D().
3) Get the individual glyphs out of the fonts and draw directly using the bezier curves that make up the glyphs. This allows for 3D extrusion, too. However, this option is the most time consuming to code and probably least performant. It also involves dealing with the many small problems that fonts have (like degenerate segments, and incorrect winding orders). IF you want to go down this path, I think maybe Core Text can help.
There are at least two clean ways to do this, depending on your requirements.
While documentation advises against compositing over a CAEAGLLayer (GLKView), it works quite well, at least in recent iOS versions, when transparent content is layered on top of the CAEAGLLayer. For example, try dropping a UITextView, with opaque set to false and a clear background color, on top of a GLKView in your Storyboard in Interface Builder in the Apple GLKit template or your app. In my test on an iPhone 5, frame rendering time remained around 1ms, even while scrolling in the text view. If your text needs are static, or you don't want the user to interact with the text, use CATextLayer as a child layer of your EAGLLayer instead of a view.
The second approach is to render the text into a texture. You can then composite the text onto your view by disabling the depth buffer and rendering the texture on a full screen rectangle. Look at UIGraphicsBeginImageContextWithOptions to see how to render to an offscreen image with Quartz. UIGraphicsGetImageFromCurrentImageContext allows you to retrieve the UIImage to use as a texture.

Attributes Inspector: how to use "Stretching"

What are the numbers in the Stretching box in the View sub-box Interface Builder's Attributes Inspector?
(as a side question - I suppose a respectable company such as Apple would have actually released documentation for its tools, as opposed to letting developers just guess everything; so, where's this documentation?...)
This blog post Karol seems to explain it pretty well. - http://macoscope.com/blog/stretchable-images-using-interface-builder/
Stretching properties are pretty simple (I don't think so, but the
articles does :).
The fraction of the original image left without stretching on the left
is specified by X The fraction of the original image that gets
stretched in the x-axis is specified by Width The fraction of the
original image left without stretching on the right is equal to 1 – X
– Width If we use 0 for Width the stretched area will interpolate
between the last pixel of the left part and the first pixel of the
right part The y-axis works analogously
Viewing a .storyboard file in a text editor (figures it's an XML file) revealed the answer: it is linked to UIView's contentStretch property.
FYI - this was deprecated in iOS 6.0 https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIView_Class/index.html#//apple_ref/occ/instp/UIView/contentStretch
(updated link)
Deprecation Documentation
Instance Property
contentStretch
The rectangle that defines the stretchable and nonstretchable regions of a view.
Deprecated
To achieve the same effect, use resizableImageWithCapInsets: instead.

How to draw images among rich-text with CoreText? (iOS)

I can draw rich-text with Core Text, the problem is placing images flowing with the text.
(iOS SDK 4.1)
I'm try to drawing some kind of rich-text. Problem is designer placed many icons among text. So the text what I have to draw is something like this:
Here is a word <an icon image>, and another words.
The image(<another icon>) should be placed like a glyph.
It's part of text, not an example.
<icon> are images. (This is not a code. Just an illustration.)
I can draw this by laying out all of them manually, but it's too hard keeping complex text layout behaviors. So I'm finding a way to draw this with Core Text.
I got solution.
The key of laying out non-text content is CTRunDelegate.
Core Text does not support non-text content, so you have to make blank spaces for them, and draw or place them yourself later.
A part of NSAttributedString attributed with kCTRunDelegateAttributeName will call registered callback to determine width of each glyph. This will let you make blank space for each non-text object.
However, after drawing the text with Core Text, the layout information stored with frame/line/run will invalidated. So you have to draw/place non-text contents after layout with framesetter/typesetter, but before drawing.
This link describes basic usage of CTRunDelegate:
How to use CTRunDelegate in iPad?
There is a problem with Core Text. Originally, CTRunDelegate designed to support variable width and vertical alignment via CTRunDelegateCallbacks.getAscent and CTRunDelegateCallbacks.getDescent. But vertical alignment feature doesn't work currently. This might be a bug.
I described this problem here:
Aligning multiple sized text vertical center instead of baseline with Core Text in iOS
If you have informations about this problem, please see my question at the link.
You simply set a delegate for a given CTRun and the delegate object is responsible to let know Core Text what is the CTRun ascent space, descent space and width.
When Core Text "reaches" a CTRun which has a CTRunDelegate it asks the delegate - how much width should I leave for this chunk of data, how high should it be? This way you build a hole in the text - then you draw your image in that very spot.
Here is a blog about Core Text.It has the answer for you .
How To Create a Simple Magazine App with Core Text

How to fix blurred Icon Decorator on DSL Tools?

I faced this problem and after an extensive research I found its root cause and a workaround that can be useful for other people.
The icon decorator becomes "blurred" because its positioning on the shape and the conversion from pixels to inches.
Usually the drawing surface of the Domain Specific Language has a resolution of 96dpi and the Icon Decorators are positioned using an offset of 0.06 inches. Translating it to pixels it becomes an offset of 5.76 pixels in the drawing surface. As it is not possible to draw a "half pixel" on the screen the GDI+ adjusts the image in an attempt to emulate the "haf pixel" positioning. That is the reason why the image becomes blurred.
My suggestion as workaround, is to use the Horizontal Offset and the Vertical Offset properties of the Icon decorator class for fixing the "half pixel" decorator position. If you use the "0.0025" inches as vertical and horizontal offsets, when the image positioning is translated from inches to pixels it becomes 6 pixels, instead of 5.76. It happens because the offset now is the default value 0.06 inches plus the offset you set 0.0025 = 0.0625.
I also found that using png images with transparency causes the image blurring, even using the offset workaround I suggested here. Converting the image to bitmap format fixes the problem.
If someone also has any suggestions for fixing the problem, please add your solution or workaround.
I fixed the blurred problem by creating a new Bitmap:
Under the overridden method
public override System.Drawing.Image GetDisplayImage(ShapeElement parentShape)
I call my custom method FixBitmap
Bitmap imageFixed = DynamicImageHelper.FixBitmap(image, out dynamicOffset);
This method receives the original image that DSL would show, but instead returns the exact same image but created as a new Bitmap
Bitmap fixedImage = new Bitmap(original, original.Width, original.Height);
return fixedImage;
If you check the new instance properties there are little differences (I don't quite remember which because i implemented this about 1 year ago).
Also, i mostly use .png files with transparency and they look exactly as they are.
Hope this helped.
If you need any further help, do not hesitate,
Regards,
Luís

Resources