Could not make a CTParagraphStyleRef on iPhone - ios

I found a strange problem when using the CoreText. Below is the code:
CGFloat headindent = theme.horizontalMargin.floatValue; // 10.0
CTParagraphStyleSetting head;
head.spec = kCTParagraphStyleSpecifierHeadIndent;
head.value = &headindent;
head.valueSize = sizeof(float);
CGFloat tailindent = -theme.horizontalMargin.floatValue; //-10.0
CTParagraphStyleSetting tail;
tail.spec = kCTParagraphStyleSpecifierTailIndent;
tail.value = &tailindent;
tail.valueSize = sizeof(float);
CGFloat firstlineindent =theme.horizontalMargin.floatValue;//10.0
CTParagraphStyleSetting firstline;
firstline.spec = kCTParagraphStyleSpecifierFirstLineHeadIndent;
firstline.value = &firstlineindent;
firstline.valueSize = sizeof(float);
CTParagraphStyleSetting settings[]={
head,
tail,
firstline,
};
CTParagraphStyleRef style = CTParagraphStyleCreate(settings, 3);
Then the style is added to make an attributed string. I get the values from a theme stored in CoreData. That works well when I run the app on iPad, but on iPhone the code does not work.
I made a breakpoint to check what is the problem, and I found that the theme values are both right in the iPad and iPhone condition,but the value of style ,which is a CTParagraphStyleRef, are all ZERO when running on iPhone.
Anyone has encounter same problem before? I just could not figure where is wrong.
Thanks for any help!

I just find the where the problem is...
In iPhone ,the sizeof(CGFloat) = 8 ,sizefo(float) = 4,
and in iPad , they are both 4.
But what still confused me is that since
typedef float CGFloat;
why their size are different.

Related

Tesseract has crashed

I am using tesseract on my iOS device and it was working properly until recently it started to crash on me. I have been testing with the same image over and over again and prior to now I had it working about 75 times consecutively. The only thing I can think of is that I deleted the app from my iOS device and then ran it again through Xcode.
I am far from an expert on tesseract and I could really use some advice on what to do next, it would truly be a disappointment for all the hours I put in to go to waste because I cannot read the image anymore. Thank you
This is the crash error it appears to happen when the tesseract file in this method
- (BOOL)recognize
{
int returnCode = _tesseract->Recognize(NULL);// here is where the arrow points on the crash
return (returnCode == 0) ? YES : NO;
}
This is an old question from Alex G and I don't see any answer.
Does anyone find the root cause and solution? Please advice. Many thanks.
I hope you are using AVCaptureSession to take continuously photo and passing to tesseract after some image processing.
So before passing UIImage to tesseract for recognising you should check with this:
CGSize size = [image size];//your image
int width = size.width;
int height = size.height;
if (width < 100 || height < 50) {//UIImage must contain some some size
//Consider as invalid image
return;
}
//This condition is not mandatory.
uint32_t* _pixels = (uint32_t *) malloc(width * height * sizeof(uint32_t));
if (!_pixels) {
return;
//Consider as invalid image
}

Differences in Core-Text between ios 6 and ios 5.1?

It appears there are some differences in the CoreText implementation between ios 5.1 and ios 6, as you can see from these two screenshots :
ios 6:
ios 5:
Firstly the text color isn't applied correctly. It seems that on ios 5.1 the kCTForegroundColorAttributeName requires that you give it a CGColor, whereas on ios 6, passing it a UIColor will suffice. So I solved the issue by changing my code to:
[attributes setObject:(id)[color CGColor]
forKey:(NSString*)kCTForegroundColorAttributeName];
Secondly, the paragraph spacing is a bit off. The distance between "sight" and "According" is 11px vs 25px (measured in the screenshot). In both cases paragraph spacing is set to 5:
NSMutableData *styleSettingsArray = [NSMutableData data];
CGFloat spaceBefore,spaceAfter;
...
CTParagraphStyleSetting styleSettingB = {kCTParagraphStyleSpecifierParagraphSpacingBefore ,sizeof(CGFloat),&spaceBefore};
CTParagraphStyleSetting styleSettingA = {kCTParagraphStyleSpecifierParagraphSpacing ,sizeof(CGFloat),&spaceAfter};
[styleSettingsArray appendBytes:&styleSettingB length:sizeof(styleSettingB)];
[styleSettingsArray appendBytes:&styleSettingA length:sizeof(styleSettingA)];
...
if(styleSettingsArray.length > 0)
{
CTParagraphStyleRef paragraphStyleRef = CTParagraphStyleCreate([styleSettingsArray bytes], [styleSettingsArray length] / sizeof(CTParagraphStyleSetting));
[dictionary setObject:(__bridge id)(paragraphStyleRef) forKey:(NSString*)kCTParagraphStyleAttributeName];
CFRelease(paragraphStyleRef);
}
Description of paragraphStyleRef in console:
iOS 6:
CTParagraphStyle:
base writing direction = -1, alignment = 3, line break mode = 0, default tab interval = 0
first line head indent = 0, head indent = 0, tail indent = 0
line height multiple = 0, maximum line height = 0, minimum line height = 0
line spacing adjustment = 0, paragraph spacing = 5, paragraph spacing before = 5
iOS 5:
CTParagraphStyle:
writing direction = -1, alignment = 3, line break mode = 0, default tab interval = 0
first line head indent = 0, head indent = 0, tail indent = 0
line height multiple = 0, maximum line height = 0, minimum line height = 0
line spacing adjustment = 0, paragraph spacing = 5, paragraph spacing before = 5
which seem the same to me, so I don't know what the problem is. Other than the spacing between the paragraphs, they're identical.
So how can I fix this? Also are there any other things I should be aware of that might cause the text to display differently?
EDIT:
After some investigation, it turnes out that the difference in paragraph styling was actually caused by my line breaks which printed "\r\n". Changing that to "\n" solved the spacing problem.
Core Text got an overhaul in iOS 6. You check out all the changes by watching the WWDC 2012 videos that are available for free if you have an Apple Developer Account.
So now in iOS 6, you can't use any of the low level Core Text attributes like kCTForegroundColorAttributeName or kCTParagraphStyleAttributeName.
Instead you use a new set of high level attributes, like NSForegroundColorAttributeName & NSParagraphStyle.
So your code would change to:
/*Note that you have use the Foundation class
for the attribute value instead of it's Core-Foundation counterpart.*/
[attributes setObject:color
forKey:NSForegroundColorAttributeName];
CGFloat spaceBefore, spaceAfter;
NSMutableParagraphStyle *mutableParagraphStyle = [NSMutableParagraphStyle defaultParagraphStyle];
mutableParagraphStyle.paragraphSpacing = spaceAfter;
mutableParagraphStyle.paragraphSpacingBefore = spaceBefore;
[attributes setObject:mutableParagraphStyle
forKey:NSParagraphStyleAttributeName];
You can find the documentation for the all new attributes here:
http://developer.apple.com/library/ios/#documentation/uikit/reference/NSAttributedString_UIKit_Additions/Reference/Reference.html

Using Core Motion in landscape mode

I am currently working on augmented reality and for that purpose I'd like to use the gyroscope and Core Motion. I've studied the Apple pARk sample code, I understand most of the maths I've spend time on reading documentation because at first glance it was not clear! Everything is fine until I try to make it work in landscape mode.
I won't explain all the theory here it would be too long. But for those who experienced it, my problem is, we take the rotation matrix of the attitude to apply this rotation to our coordinates. Ok, it is fine until here, but it seems Core Motion doesn't adapt it to Landscape Mode. I saw similar questions on this subject but it looks like no one has a solution.
So I tried to make my own, here is what I think:
Everytime we rotate the device to landscape, a rotation of +-90° is made (depending on Landscape Left or right). I decided to create a 4X4 rotation matrix to apply this rotation. And then multiply it to the cameraTransform matrix (adaption of the attitude's 3X3 CMRotationMatrix to 4X4), we obtain then the matrix cameraTransformRotated:
- (void)createMatLandscape{
switch(cameraOrientation){
case UIDeviceOrientationLandscapeLeft:
landscapeRightTransform[0] = cos(degreesToRadians(90));
landscapeRightTransform[1] = -sin(degreesToRadians(90));
landscapeRightTransform[2] = 0;
landscapeRightTransform[3] = 0;
landscapeRightTransform[4] = sin(degreesToRadians(90));
landscapeRightTransform[5] = cos(degreesToRadians(90));
landscapeRightTransform[6] = 0;
landscapeRightTransform[7] = 0;
landscapeRightTransform[8] = 0;
landscapeRightTransform[9] = 0;
landscapeRightTransform[10] = 1;
landscapeRightTransform[11] = 0;
landscapeRightTransform[12] = 0;
landscapeRightTransform[13] = 0;
landscapeRightTransform[14] = 0;
landscapeRightTransform[15] = 1;
multiplyMatrixAndMatrix(cameraTransformRotated, cameraTransform, landscapeRightTransform);
break;
case UIDeviceOrientationLandscapeRight:
landscapeLeftTransform[0] = cos(degreesToRadians(-90));
landscapeLeftTransform[1] = -sin(degreesToRadians(-90));
landscapeLeftTransform[2] = 0;
landscapeLeftTransform[3] = 0;
landscapeLeftTransform[4] = sin(degreesToRadians(-90));
landscapeLeftTransform[5] = cos(degreesToRadians(-90));
landscapeLeftTransform[6] = 0;
landscapeLeftTransform[7] = 0;
landscapeLeftTransform[8] = 0;
landscapeLeftTransform[9] = 0;
landscapeLeftTransform[10] = 1;
landscapeLeftTransform[11] = 0;
landscapeLeftTransform[12] = 0;
landscapeLeftTransform[13] = 0;
landscapeLeftTransform[14] = 0;
landscapeLeftTransform[15] = 1;
multiplyMatrixAndMatrix(cameraTransformRotated, cameraTransform, landscapeLeftTransform);
break;
default:
cameraTransformRotated[0] = cameraTransform[0];
cameraTransformRotated[1] = cameraTransform[1];
cameraTransformRotated[2] = cameraTransform[2];
cameraTransformRotated[3] = cameraTransform[3];
cameraTransformRotated[4] = cameraTransform[4];
cameraTransformRotated[5] = cameraTransform[5];
cameraTransformRotated[6] = cameraTransform[6];
cameraTransformRotated[7] = cameraTransform[7];
cameraTransformRotated[8] = cameraTransform[8];
cameraTransformRotated[9] = cameraTransform[9];
cameraTransformRotated[10] = cameraTransform[10];
cameraTransformRotated[11] = cameraTransform[11];
cameraTransformRotated[12] = cameraTransform[12];
cameraTransformRotated[13] = cameraTransform[13];
cameraTransformRotated[14] = cameraTransform[14];
cameraTransformRotated[15] = cameraTransform[15];
break;
}
}
Then just before we update all the points I do this:
multiplyMatrixAndMatrix(projectionCameraTransform, projectionTransform, cameraTransformRotated);
After that the rest of the code remains unchanged, I just want the annotation to be displayed properly in landscape orientation. For now this is the only idea I have, the rendering in landscape is not good, I move the device to the right or the left hand-side, the annotations go down or up (like it was when I didn't add this code).
Has anyone come up with a solution? I'll keep on searching, especially on the CMRotationMatrix, it doesn't seem it is a typical rotation matrix, I can't find any documentation saying precisely what are the different elements of this matrix.
I just managed to adapt this (Apple's pARk sample) to landscape (right) yesterday and would like to share the changes made. It appears to work correctly, but please call out any mistakes. This only supports landscape right but can probably be adapted easily for left.
In ARView.m,
In -(void)initialize, switch the bounds height and width
createProjectionMatrix(projectionTransform, 60.0f*DEGREES_TO_RADIANS, self.bounds.size.height*1.0f / self.bounds.size.width, 0.25f, 1000.0f);
In -(void)startCameraPreview
[captureLayer setOrientation:AVCaptureVideoOrientationLandscapeRight];
In -(void)drawRect:
//switch x and y
float y = (v[0] / v[3] + 1.0f) * 0.5f;
float x = (v[1] / v[3] + 1.0f) * 0.5f;
poi.view.center = CGPointMake(self.bounds.size.width-x*self.bounds.size.width, self.bounds.size.height-y*self.bounds.size.height); //invert x

UIDatePicker breaks when using UIDatePickerMode.DateAndTime

I'm creating an app with Monotouch 5 and I'm having a problem with creating a datepicker with the mode UIDatePickerMode.DateAndTime on iOS 4.3 or lower when running my app in an iPad Simulator. The DatePicker is always broken, although the dimensions I set for it's frame and the surrounding view are perfectly fine.
This is how it looks like on an iPad Simulator with iOS 4.3:
http://screencast.com/t/isIRcUcKCF5
and this is how it looks on an iPad Simulator with iOS 5.0:
http://screencast.com/t/UxPTpR0Btzb
This problem does not occur on iPhone Simulators or real devices. I know it also does not occur on real iPad devices with iOS 5, but I could not test 4.3 or lower because I have no such device.
The source code for creating the date picker is as follows:
public override void Selected (MonoTouch.Dialog.DialogViewController dvc, UITableView tableView, NSIndexPath path)
{
var vc = new MyViewController (this) {
Autorotate = dvc.Autorotate
};
datePicker = CreatePicker ();
datePicker.Frame = PickerFrameWithSize (datePicker.SizeThatFits (SizeF.Empty));
//vc.View encompasses the entire, screen so there is plenty of space available for the date picker
vc.View.BackgroundColor = UIColor.Black;
vc.View.AddSubview (datePicker);
dvc.ActivateController (vc);
}
public virtual UIDatePicker CreatePicker ()
{
var picker = new UIDatePicker (RectangleF.Empty){
AutoresizingMask = UIViewAutoresizing.FlexibleWidth,
Mode = UIDatePickerMode.DateAndTime,
Date = DateValue,
};
return picker;
}
static RectangleF PickerFrameWithSize (SizeF size)
{
var screenRect = UIScreen.MainScreen.ApplicationFrame;
float fY = 0, fX = 0;
switch (UIApplication.SharedApplication.StatusBarOrientation){
case UIInterfaceOrientation.LandscapeLeft:
case UIInterfaceOrientation.LandscapeRight:
fX = (screenRect.Height - size.Width) /2;
fY = (screenRect.Width - size.Height) / 2 -17;
break;
case UIInterfaceOrientation.Portrait:
case UIInterfaceOrientation.PortraitUpsideDown:
fX = (screenRect.Width - size.Width) / 2;
fY = (screenRect.Height - size.Height) / 2 - 25;
break;
}
// calculated values:
// fx: 224
// fy: 369
// size.Width: 320
// size.Height: 320
return new RectangleF (fX, fY, size.Width, size.Height);
}
When I am using a datepicker with the modes UIDatePickerMode.Date or UIDatePickerMode.Time, the problem does not occur.
The problem looks just like blank UIDatePicker on iPad in mode UIDatePickerModeDateAndTime, but the fix described there does not work in my case for some weird reason.
Any hints on how to work around this bug would be greatly appreciated.
Thanks,
Adrian
I still don't have a clue how to make the code above work, but I managed to work around this iOS bug by using a UIPopupView for displaying the calendar instead.

DirectX: Antialiasing doesn´t work

I just want to enable Antialiasing in DirectX9, but it doesn´t seem to do much, and the text drawn with ID3DXFont.DrawText(...) looks jagged too.
Here is the initialization-part
pDirect3D = Direct3DCreate9( D3D_SDK_VERSION);
memset(&presentParameters, 0, sizeof(_D3DPRESENT_PARAMETERS_));
presentParameters.BackBufferCount = 1;
presentParameters.BackBufferWidth = 800;
presentParameters.BackBufferHeight = 500;
presentParameters.MultiSampleType = D3DMULTISAMPLE_NONMASKABLE;
presentParameters.MultiSampleQuality = 2;
presentParameters.SwapEffect = D3DSWAPEFFECT_DISCARD;
presentParameters.hDeviceWindow = hWnd;
presentParameters.Flags = 0;
presentParameters.FullScreen_RefreshRateInHz = D3DPRESENT_RATE_DEFAULT;
presentParameters.PresentationInterval = D3DPRESENT_INTERVAL_DEFAULT;
presentParameters.BackBufferFormat = D3DFMT_R5G6B5;
presentParameters.EnableAutoDepthStencil = TRUE;
presentParameters.AutoDepthStencilFormat = D3DFMT_D16;
presentParameters.Windowed = TRUE;
pDirect3D->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWnd,D3DCREATE_SOFTWARE_VERTEXPROCESSING, &presentParameters, &pDevice);
pDevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
pDevice->SetRenderState(D3DRS_MULTISAMPLEANTIALIAS, TRUE);
Is there something I do wrong?
ShowWindow(hWnd, nCmdShow);
UpdateWindow(hWnd);
First, text isn't anti-aliased by mutli-sampling, secondly a MultiSampleQuality of 2 is barely noticeable. Try a 4 or 8 ensure that the result is achieved, try toggling and watch the jagged edges.
You should checkout the AntiAlias sample provided in the DirectX SDK for details about setting this up properly.
I am creating text with meshes (D3DXCreateTextW), and I notice a significant difference when MultiSampling, even at low quality levels. With any kind of MultiSampling, the text and other lines are smooth, whereas they are jagged without MultiSampling.
Use CheckDeviceMultiSampleType to confirm that your video card does accept the type and level that you are requesting.

Resources