Questions on Fontforge internal scripting - scripting

What is return value on these internal FF calls:
GlyphInfo( "Width" ) and GlyphInfo( "VWidth" ) function calls return Numbers or strings?
If number, is it integer, decimal, hex or other?
Is Width the selected glyph's length ( Horizontal )?
Is VWidth the height of selected glyph? If not how can you get the glyph's height?

Related

How do I produce a number with absolute precision of some power of 10?

Vb.net has a decimal data type.
Unlike normal double or floating points, decimal data type can store values like 0.1
Now say I have a variable like precision.
Say precision is 8
So basically I want to do
Protected Overridable Sub setPairsPricesStep2(decimalPrecission As Long, Optional base As String = "", Optional quote As String = "")
If decimalPrecission = 8 Then
Return
End If
Dim price = 10D ^ (-decimalPrecission)
setPairsPriceStep1(price, base, quote)
End Sub
There is a problem there
the result of Dim price = 10D ^ (-decimalPrecission) is double, not decimal. I can convert it to decimal but then I will lost the precission.
So what is the right way to do it? Should I just use for next but that's hardly elegant.
It's simple
I want a function that given precisions give decimal value.
For example, if precision is 1 I got 0.1. If precision is 5, I got 0.00001
I ended up doing this
For i = 1 To decimalPrecission
price *= 0.1D
Next
But surely there is a better way
Update:
Per comment, I tried
Dim e = 10D ^ -5
Dim e1 = 10D ^ -5L
The type of e and e1 are both double.
I suppose I can do Cdec(e). But then it means I have lost accuracy because normal double cannot store .1 correctly.
I want a function that given precisions give decimal value.
For example, if precision is 1 I got 0.1. If precision is 5, I got 0.00001
Since you are working with the Decimal type, the simplest way to get this result is to use the Decimal constructor that allows you to specify the scale factor.
Public Sub New (lo As Integer, mid As Integer, hi As Integer, isNegative As Boolean, scale As Byte)
From the Remarks section of the above referenced documentation,
The binary representation of a Decimal number consists of a 1-bit sign, a 96-bit integer number, and a scaling factor used to divide the integer number and specify what portion of it is a decimal fraction. The scaling factor is implicitly the number 10 raised to an exponent ranging from 0 to 28.
So you can see that if take the value of one divided by 10 to the first power, the result is 0.1. Likewise, one divided by 10 to the fifth power, the result is 0.00001.
The lo, mid, and hi arguments in the constructor could be obtained by uisng the [Decimal.GetBits Method](Decimal.GetBits Method), but for this simple case, I chose to hard code the values for the value of one stored as a decimal.
To obtain a value of 0.1D:
New Decimal(1, 0, 0, False, 1)
To obtain a value of 0.00001D:
New Decimal(1, 0, 0, False, 5)
Dim stringrepresentation = "1E-" + decimalPrecission.ToString
Dim price = Decimal.Parse(stringrepresentation, System.Globalization.NumberStyles.AllowExponent)
This is what I basically did. Basically I created a string 1E-5, for example, and use decimal.parse to get the decimal
I wonder if there is a better way but I have no idea.
Actually Jimy ways may work too but rounded to a number

Just want to check if the way i have used the assignment expression in python 3.8 program is correct or not

According to Heron's formula for finding area of a triangle , if the sides of a triangle are a, b & c is :
s = (a+b+c) / 2
area =sqrt( s * (s-a) * (s-b) * (s-c)) # sqrt means square root
so for finding the area of the triangle using Heron's formula in Python, if I write code like this, will it be a valid practise? I have used assignment expression while calculating the area.
a = int(input("Enter value of first side")) # Assuming value is integer
b = int(input("Enter value of second side")) # Assuming value is integer
c = int(input("Enter value of third side")) # Assuming value is integer
area = ((s := (a+b+c) /2) *(s -a)*(s-b)*(s-c))**0.5
print("Area of the triangle is", area)
Yes, program counts correct. There is a one drawback in input: I recommend to add '\n' in input because it is uncomfortable to enter values without any space near text. In this code I fixed that. But you need to add checking if sides can make triangle.
a = int(input("Enter value of first side")) // Assuming value is integer
b = int(input("Enter value of second side")) // Assuming value is integer
c = int(input("Enter value of third side")) // Assuming value is integer
area = ((s := (a+b+c) /2) *(s -a)*(s-b)*(s-c))**0.5
print("Area of the triangle is", area)

Spyne: Integer Validation how can I set minimum digit limit?

I want to add validation for an Integer, that its minimum digit value is 5 and maximum digit value is 20
For Integer I have set following validations
Integer(min_occurs=1, gt=9999, max_str_len=20, nillable=False)
I just put work around for min_str_len, I do not find any attribute for min_str_len.
Instead of work around is there any default way ?
You can subclass Integer type (or another one that fits best) and implement validate_native method.
Example for prime number check from docs:
from math import sqrt, floor
class Prime(UnsignedInteger):
"""Custom integer type that only accepts primes."""
#staticmethod
def validate_native(cls, value):
return (
UnsignedInteger.validate_native(value) and \
all(a % i for i in xrange(2, floor(sqrt(a))))
)
Source: http://spyne.io/docs/2.10/manual/05-02_validation.html#a-native-validation-example

How to read the calibration in a line profile

I would like to read the calibration factor of a line profile. It is stored in "Image Display Info - Calibration". I use the function GetUnitsH (image, num), but I only obtain the channel number, not the calibrated position (in nanometers).
Thank you in advance.
The command you are seeking are:
Number ImageGetDimensionScale( BasicImage, Number dimension )
Number ImageGetDimensionOrigin( BasicImage, Number dimension )
String ImageGetDimensionUnitString( BasicImage, Number dimension )
Number ImageGetIntensityScale( BasicImage )
Number ImageGetIntensityOrigin( BasicImage )
String ImageGetIntensityUnitString( Number dimension )
These will give you the calibrations as shown in the image-display.
In order to convert calibrated and uncalibrated units, you have to do the accordign maths yourself.
And yes, each of the "Get" commands has an according "Set" command as well, if you need it.
One thing to watch out for is:
Do you really look at your image, or at a copy of it?
In particular, makes sure that you use := and not = when assigning image variables to images.
Example:
This will work:
Image img := GetFrontImage()
number scale_x = img.ImageGetDimensionScale(0)
Result("\n Scale X:" + scale_x )
This will not work:
Image img = GetFrontImage()
number scale_x = img.ImageGetDimensionScale(0)
Result("\n Scale X:" + scale_x )
In the second case, one gets the refernece to the front-most image, but the = will just copy the values (and not the calibrations or other meta data) into a new image.

Returning a number less than 1

I am working on an app that needs to utilize a ratio of a given number and multiply that ratio times another number. Problem is that I can't get numbers less that 1 to give me the proper decimal ratio, instead it gives me zero (when it should be .5).
Example:
float number = 1/2; // This gives me zero
double number = 1/2; // This also gives me zero
If you don't specify decimal places you're using integers which means the calculation is performed with integer precision before the result is cast to the type on the LHS. You want to do the the following when using hard coded numbers in your code:
float number = 1.0f / 2.0f;
double number = 1.0 / 2.0;
If you're aiming to use integer variables for an operation, you'll want to cast them to the type that you want for your result.
Try this
float number = 1.0/2.0;
Remember that 1 is an int, so you are essentially taking
(int)1 / (int)2
which returns
(int)0
To cast variables that are ints, do
float number = (float)numerator / (float)denominator;