Make an image in Kivy exactly the same size as the widget bounding box - kivy

I'm having trouble in Kivy setting an image size. I want to keep the aspect ratio fixed but I also want to position things precisely over the top of the image.
To do this, I make a RelativeLayout that has the same size and position as the image. However, when I check the size of the layout, it is always slightly larger than the displayed image. Making precise alignment very difficult. The wiki mentions something like this:
By default, the image is centered and fits inside the widget bounding box. If you don’t want that, you can set allow_stretch to True and keep_ratio to False.
They also include code if you want to make the image slightly larger than the containing widget:
<-FullImage>:
canvas:
Color:
rgb: (1, 1, 1)
Rectangle:
texture: self.texture
size: self.width + 20, self.height + 20
pos: self.x - 10, self.y - 10
But nothing about making them exactly the same size!? Surely I should be able to dictate the containing widgets size so the aspect ratio etc. is exactly as required.
I have tried many things but whatever I try, I cannot get the outside edges of the displayed image and a layout to coincide.
Does anyone have any experience with this?

Apologies, I have discovered the problem, the images had a transparent border that I wasn't aware of (the images were passed on to me from elsewhere) I have removed this and it has solved the problem.

Related

How to align two UILabels on the Y axis of the center of the first line of text

please see the image below for two examples of what is to be achived
the alignment should be on the Center Y of the first lines of each UILabels and should work regardless of font size or font. currently we have implemented this with different constraints to the top of the super view for different font and font size combinations.
the constraint to align the center of the two UILabels does not work since the text of the second UILabel is not fixed and can have several lines.
also the text is dynamic, so it is not known where the text will wrap to create the first line, thus it cannot be shown in an one line UILabel with the rest of the text in another one below.
currently this is implemented using UIKit, but if there is an easy solution in SwiftUI we can put these two labels in a SwiftUI component. so a SwiftUI solution would also be welcomed.
Your comments said "it should be on the glyphs" ... but, without additional information, my guess is that "real world" usage would not really need that level of precision.
For example:
While the glyphs are not perfectly center-Y aligned, it seems unlikely you'd run into a case where the first line of the "rightLabel" is " ' " ' " or . , . , ..
This layout can be easily done with only a few constraints - no need to do any calculations:
The "Positioning" label would, of course, be set .hidden = true so it would never be seen.
If you really, really want glyph-precision, you'll need to calculate
the Glyph bounding box for the left-label
the Glyph bounding box for first line of the right-label
calculate the "character box" offsets to align the Glyph Y-centers
and then position the two labels accordingly, or use Core Text to draw the text (instead of using UILabel).
Probably more work than necessary -- unless your actual use-case demands it.
That's an interesting problem! You can try using the centerYAnchor for the label on the left, and the firstBaselineAnchor for the label on the right... that will align the center Y with the text baseline, which isn't quite what you want.
To find the correct offset to apply, you can use the information from UIFont about the size of the characters. I'd probably start with capHeight * 0.5 and see if that looks or feels right. Something like:
leftLabel.centerYAnchor.constraint(equalTo: rightLabel.firstBaseLineAnchor, constant: rightFont.capHeight * 0.5)
This is a more difficult problem in SwiftUI, I think, because resolved font metrics aren't directly available to you.

google docs api - insert inline image not resizing image to specified size

so I have been making some image requests with the google docs api using javascript, but I have been unable to change the image size when I make the request.
All the images seem to be appearing in their original size in the google doc.
So I am wondering what I am doing wrong. The documentation seems to be pretty clear:
https://developers.google.com/docs/api/reference/rest/v1/documents/request#InsertInlineImageRequest
Basically it just says to specify objectSize width and height, using magnitude and units (PT).
Here is the function I use to append to my full request, where I specify all of these.
function imageRequest (url: string, width: number, height: number) {
const request:Array<object> = [ {
insertInlineImage: {
uri: url.toString(),
objectSize: {
height: {
magnitude: height,
unit: 'PT'
},
width: {
magnitude: width,
unit: 'PT'
}
},
location: {
index: 1
}
},
} ];
return request;
}
And then I'm pushing each image request to the full request like.
request.push(imageRequest(image.image.url, 468, 648));
but whatever I put for the width and height is not doing anything. The images are always the original size.
The images are public on an s3 style hosting at Wasabi (similar to amazon s3)
I cannot think of what the problem could be, maybe it's something small that I am overlooking?
help is appreciated, thanks.
The image dimensions are defined by the objectSize property and the Docs documentation states a few rules that define the final size of the image:
If neither width nor height is specified, then a default size of the image is calculated based on its resolution.
If one dimension is specified then the other dimension is calculated to preserve the aspect ratio of the image.
If both width and height are specified, the image is scaled to fit within the provided dimensions while maintaining its aspect ratio.
From these rules it seems that Docs will prioritize the aspect ratio of an image over the exact size, so even if you specify a certain set of dimensions, Docs may resize it to preserve the ratio. As a workaround you can specify only the dimension that you deem more important, (width or height) and Docs will calculate the other one automatically.
In addition, it seems that Docs uses points (PT) rather than pixels (PX) as units to measure dimensions. One point is 1/72 inch while one pixel is 1/96 inch, so if you're used to calculating sizes in pixels as it's most common you'll need to make this conversion as well. A pixel will be 0.75pt while a point will be 1.333...px.

Only print ol.style.Text to polygon if text label fits inside its geometry

In OpenLayers I use ol.style.Text to add a text label to federal states polygons. The states have names of different length and also polygons of different sizes. It looks like this:
Is it possible to only print the text marker if it fits inside its polygon (e.g. after zooming in)? For instance, in the above example Hessen, Thüringen, Sachsen and Bayern would be printed, but Rheinland-Pfalz, Saarland and Baden-Württemberg would be omitted because the text goes beyond its feature's geometry...
I know I can set the font property of an ol.style.Text to a particular size based on the resolution but this does not help here as text would still overlap the borders sometimes...
This is currently not possible with help from the library. But you can use CanvasRenderingContext2D#measureText() in your vector layer's stlyeFunction to get the width of the label, and compare that with the extent's width of the polygon at a certain resolution, and decide based on that whether to render or not. You can also get smarter than using the extent's width, but it is probably a good enough approximation for many cases.

iOS: Modify a font height, width, and spacing independently when creating PDF

In an iOS app I need to use a specific font but each character needs to be taller, thinner, and the spacing closed up to fit correctly. Is it possible to stretch/squish a font programmatically?
When you're adding text to a PDF file there are multiple ways to influence how the text is going to appear. The most generic way (and the way that might actually be sufficient for you) is to scale the text matrix:
void CGContextSetTextMatrix ( CGContextRef c, CGAffineTransform t );
As mentioned in the comment by #mkl, you can provide a matrix that will scale up in the Y direction while scaling down in the X direction. The effect will be that the letters are stretched out vertically and squished horizontally.
Normally I would expect you don't have to touch the spacing in that case, as spacing will be "squished" together just as the other characters.
Just in case that isn't sufficient, PDF actually does provide a way to change the spacing between characters too:
void CGContextSetCharacterSpacing ( CGContextRef context, CGFloat spacing );
While Apple's description talks about "additional space" to add between characters, the PDF specification and I suspect Apple's implementation as a result allows the spacing value to be both positive and negative. A negative value would have the effect of moving the characters closer together.
Seems like the best option would be to create your own custom font.
You are able to change the kerning of your font (the space between the letters) and the thickness/thinness of the font, however you probably aren't able to edit the height of the font, unless you edit the bounding box the font is inside of to scale the letters differently.
You might also want to consider using a different font...or if you're REALLY hardcore you can edit the font yourself using photoshop/illustrator.

Repositioning images on FormResize proportionally

I have a Delphi form with TImages on it. Actually, it's a "fake" desktop with "icons" (the TImages).
When the user resizes the form (scales it or maximizes it, for example) the icons on the form should align proportionally.
Right now, I'm doing something like this with the images:
ImageX.Left:=Round(ImageX.Left * (Width / OldWidth));
ImageX.Top:=Round(ImageX.Top * (Height / OldHeight));
Now this is OK, as long as I start to make the maximized form smaller.
In that case the rightmost images are cut in part by the form's border (they're off the form's client area).
If I reposition these images to fit the client area, then the position of the icons get distorted upon scaling back to maximum size.
Any ideas for a better algorithm/fix?
Thanks!
First of all, you can't have a correctly scaled desktop when you only move the images, and don't scale them as well. You can do slightly better by moving the midpoints of your images, not their top left corner. It still won't be perfect, but it will work better. Of course, now the images will be cropped on all four sides, not just bottom and right, but at least it will be symmetrical :-)
Second, you will get accumulative rounding errors since you constantly override the "original" values (ImageX's top and left coordinate). You'd be better off having the original values stored in some sort of collection or array, and setting the new position based on the original value, rather than the previous value.
Something like this:
ImageX.Left:=Round(ImageX_OriginalLeft * (Width / Original_Width));

Resources