How to know that the device's screen is bigger? - mobile

When the phone's screen is not very big, like an Alcatel OT-806D's screen, then label text can be seen with a plain, small, proportional font with a size of 9. But when the screen is like HTC's screen then the label's text cannot be read with that font! How can I tell if a screen is "big"?

Use below code.
int screen_width = Display.getInstance().getDisplayWidth();
int screen_height = Display.getInstance().getDisplayHeight();
Its returns the mobile screen width and height.

Display dis = Display.getDisplay(this);
Displayable d = dis.getCurrent();
int width = d.getWidth();
int height = d.getHeight();

Related

Why is pango_layout_get_pixel_size slightly wrong on Ubuntu Linux in C

I'm experiencing a frustrating issue trying to draw text using Pango and Cairo libraries in C in a Gtk application running on Ubuntu Linux.
I'm creating a Pango layout and then drawing it at a given location which is determined by the size of the text as reported by pango_layout_get_pixel_size, but the size returned by that function is wrong in both width and height, especially in height. Here is my full code:
// Create a cairo context with which to draw
// Note that we already have a GtkDrawingArea widget m_pGtkDrawingArea
cairo_t *cr = gdk_cairo_create(m_pGtkDrawingArea->window);
// Text to draw
std::string szText("NO DATA AVAILABLE");
// Create the layout
PangoLayout *pLayout = gtk_widget_create_pango_layout(m_pGtkDrawingArea, szText.c_str());
// Set layout properties
pango_layout_set_alignment(pLayout, PANGO_ALIGN_LEFT);
pango_layout_set_width(pLayout, -1);
// The family to use
std::string szFontFamily("FreeSans");
// The font size to use
double dFontSize = 36.0;
// Format the font description string
char szFontDescription[32];
memset(&(szFontDescription[0]), 0, sizeof(szFontDescription));
snprintf(szFontDescription, sizeof(szFontDescription) - 1, "%s %.1f", szFontFamily.c_str(), dFontSize);
// Get a new pango font description
PangoFontDescription *pFontDescription = pango_font_description_from_string(szFontDescription);
// Set up the pango font description
pango_font_description_set_weight(pFontDescription, PANGO_WEIGHT_NORMAL);
pango_font_description_set_style(pFontDescription, PANGO_STYLE_NORMAL);
pango_font_description_set_variant(pFontDescription, PANGO_VARIANT_NORMAL);
pango_font_description_set_stretch(pFontDescription, PANGO_STRETCH_NORMAL);
// Set this as the pango font description on the layout
pango_layout_set_font_description(pLayout, pFontDescription);
// Use auto direction
pango_layout_set_auto_dir(pLayout, TRUE);
// Get the pixel size of this text - this reports a size of 481x54 pixels
int iPixelWidth = 0, iPixelHeight = 0;
pango_layout_get_pixel_size(pLayout, &iPixelWidth, &iPixelHeight);
// Calculate the text location based on iPixelWidth and iPixelHeight
double dTextLocX = ...;
double dTextLocY = ...;
// Set up the cairo context for drawing the text
cairo_set_source_rgba(cr, 1.0, 1.0, 1.0, 1.0);
cairo_set_antialias(cr, CAIRO_ANTIALIAS_BEST);
// Move into place
cairo_move_to(cr, dTextLocX, dTextLocY);
// Draw the layout
pango_cairo_show_layout(cr, pLayout);
//
// pango_layout_get_pixel_size() reported a size of 481x54 pixels,
// but the actual size when drawn is 478x37 pixels!
//
//
// Clean up...
//
So, as described at the bottom of the above code, the pango_layout_get_pixel_size function reports a size of 481x54 pixels, but the size of the text on the screen is actually 478x37 pixels.
What am I doing wrong here? How can I get the actual correct pixel size?
Thanks in advance for your help!
The text you are displaying ("NO DATA AVAILABLE") is all-caps, and consequently has no descenders (letters which are partly below the baseline, like j, p and q.) Normally, when you measure the extent of a text box, you include room for the descenders whether or not they are present; otherwise, you will see odd artifacts such as inconsistent line separation depending on whether or not a given line has a descender.
Pango provides APIs which return both the logical extent (which includes the full height of the font) and the ink extent (which is the bounding box of the inked part of the image). I suspect you are looking for the ink extent.

Getting the width and height of the client area of a window

The accepted answer in this question says the following about getting the width and height of the client area of a window:
RECT rect;
if(GetWindowRect(hwnd, &rect))
{
int width = rect.right - rect.left;
int height = rect.bottom - rect.top;
}
But I don't understand why not just do the following:
RECT rect;
if(GetWindowRect(hwnd, &rect))
{
int width = rect.right;
int height = rect.bottom;
}
Since the documentation for GetClientRect() says the following:
Because client coordinates are relative to the upper-left corner of a
window's client area, the coordinates of the upper-left corner are
(0,0).
My guess is that this is just a general example of how to get the width and height from a RECT, or maybe I am missing something!
The example you give calls GetWindowRect rather than GetClientRect. And so returns screen coordinates rather than client coordinates. Hence it would be wrong to assume that the top left was at 0, 0.
Note therefore that the code you present calculates the window width and height rather than the client area width and height.
If you want the dimensions of the client area then call GetClientRect instead. And, as you observe, the returned rectangle will have top left at 0, 0.

FreeType: sizing fonts based on cap height?

I am rendering text, and I need the font to have the cap height to a certain number of pixels. For example, in the sign below, I need to have the words SPEED and LIMIT to be the same height, in this case 45 px, so I set the font size to 45 (which I now understand wasn't such a good idea) and get bounding boxes (in red) different from what I should have had.
So I create the font object,
Text *t = (Text *)e;
cairo_surface_t *s = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, 0, 0);
cairo_t *cr = cairo_create(s);
cairo_font_face_t *font = cairo_ft_font_face_create_for_ft_face(fonts[t->series], 0);
cairo_set_font_face(cr, font);
Set the font size to 45,
cairo_set_font_size(cr, 1.5 * t->size);
Then calculate the extents of the bounding box, which doesn't line up with the correct size of the text.
cairo_text_extents_t ext;
cairo_text_extents(cr, t->data, &ext);
t->geom->height = ext.height; // t->size;
t->geom->width = ext.width;
t->lsb = ext.x_bearing;
cairo_font_face_destroy(font);
cairo_destroy(cr);
cairo_surface_destroy(s);
I'm guessing I have to use FT_Set_Pixel_Sizes or something, but I don't exactly know how to use that.
EDIT: Is there a way to set the cap height using Cairo? If so, I'd rather use that.
Using #Jongware's comment, I added a scale field which calculates the scaling factor for this particular piece of text (it should be consistent for a certain cap height).
cairo_text_extents_t ext, xext;
cairo_text_extents(cr, t->data, &ext);
cairo_text_extents(cr, "X", &xext);
t->scale = t->size / xext.height;
t->geom->height = t->size;
t->geom->width = ext.width * t->scale;
t->lsb = ext.x_bearing * t->scale;
It works perfectly now.

Rendering a WPF canvas as a specificly sized bitmap

I have a WPF Canvas that I want to make a bitmap of.
Specifically, I want to render it actual size on a 300dpi bitmap.
The "actual size" of the objects on the canvas is 10 device independent pixels = 1" in real life.
Theoretically, WPF device independent pixels are 96dpi.
I've spent days trying to get this to work and am coming up flummoxed.
My understanding is that the general procedure is roughly:
var masterBitmap = new RenderTargetBitmap((int)(canvas.ActualWidth * ?SomeFactor?),
(int)(canvas.ActualHeight * ?SomeFactor?),
BitmapDpi, BitmapDpi, PixelFormats.Default);
masterBitmap.Render(canvas);
and that I need to set the canvas's LayoutTransform to a ScaleTransform of ?SomeOtherFactor? and then do a measure and arrange of the canvas to ?SomeDesiredSize?
What I am stuck on is what to use for the values of ?SomeFactor?, ?SomeOtherFactor? and ?SomeDesiredSize? to make this work. MSDN documentation gives no indication of what factors to use.
I use this code to display images with 1:1 pixel accuracy.
double dpiXFactor, dpiYFactor;
Matrix m = PresentationSource.FromVisual(Application.Current.MainWindow).CompositionTarget.TransformToDevice;
if (m.M11 > 0 && m.M22 > 0)
{
dpiXFactor = m.M11;
dpiYFactor = m.M22;
}
else
{
// Sometimes this can return a matrix with 0s.
// Fall back to assuming normal DPI in this case.
dpiXFactor = 1;
dpiYFactor = 1;
}
double width = widthPixels / dpiXFactor;
double height = heightPixels / dpiYFactor;
Don't forget to enable UseLayoutRounding on the control as well.

How to draw a rectangle in opencv dynamically according to image width and height?

i want to draw a rectangle in opencv according to the image width and height (i.e. i don't want to give a static values to cvRectangle) i want to draw a rectangle which covers most of the region of any image big or small in other words i want to draw the biggest rectangle in each image,thanks
May be, you'd like to use percentage dimensions?
IplImage *img=cvLoadImage(fileName,CV_LOAD_IMAGE_COLOR);
int imageWidth = img->width;
int imageHeight = img->height;
int imageSize = img->nSize;
int ratio = 90; // our ROI will be 90% of our input image
int roiWidth = (int)(imageWidth*ratio/100);
int roiHeight = (int)(imageHeight*ratio/100);
// offsets from image borders
int dw = (int) (imageWidth-roiWidth)/2;
int dh = (int) (imageHeight-roiHeight)/2;
cvRectangle(img,
cvPoint(dw,dh), // South-West point
cvPoint(roiWidth+dw, roiHeight+dh), // North-East point
cvScalar(0, 255, 0, 0),
1, 8, 0);
cvSetImageROI(img,cvRect(dw,dh,roiWidth,roiHeight));
So, now, If you set ratio = 90, and your input image is 1000x1000 pixels, than your ROI will be 900x900 pixels and it will be in the center of your image.
i have tried that and it works well
IplImage *img=cvLoadImage(fileName,CV_LOAD_IMAGE_COLOR);
int imageWidth=img->width-150;
int imageHeight=img->height-150;
int imageSize=img->nSize;
cvRectangle(img,cvPoint(imageWidth,imageHeight), cvPoint(50, 50),cvScalar(0, 255, 0, 0),1,8,0);
cvSetImageROI(img,cvRect(50,50,(imageWidth-50),(imageHeight-50)));

Resources