Ord2Enums offsets for rectangle anchors

after my two last bugs turned out to be features, I’m just gonna flag this as question =)
is there a reason behind the ordering of the enums for rectangle anchor?

9-10-12-17-18-20-33-34-36

ord

looks like flags to me:

vertical:
  Top: 8
  Middle: 16
  Bottom:  32
horizontal
  Left: 1
  Center: 2
  Right: 4

so the enum value is vertical value + horizontal value

or if you prefer to think in bits

0000 0001 Left
0000 0010 Center
0000 0100 Right

0000 1000 Top
0001 0000 Middle
0010 0000 Bottom

if you put those ints into the calculator in ‘programming’ mode you should see the bitpattern

4 Likes

I just tried creating RectangleAnchors from SKTextAlign enums, but it seems they do not work with flags. Would be nice to have this, or is there another easy way to match my vertical and horizontal alignments?


@joreg @Elias

In here you’ll find a C# project you should be able to reference with a node called CreateRectanleAnchor taking those Skia enums.

SkiaUtils.zip (1.4 KB)

It boils down to the following code:

    public static RectangleAnchor CreateRectangleAnchor(SKTextAlign horizontalTextAlignment, VerticalTextAlignment verticalTextAlignment)
    {
        return (RectangleAnchor)((int)horizontalTextAlignment.ToRectangleAnchorHorizontal() | (int)verticalTextAlignment.ToRectangleAnchorVertical());
    }

    public static RectangleAnchorHorizontal ToRectangleAnchorHorizontal(this SKTextAlign horizontalTextAlignment)
    {
        switch (horizontalTextAlignment)
        {
            case SKTextAlign.Left:
                return RectangleAnchorHorizontal.Left;
            case SKTextAlign.Right:
                return RectangleAnchorHorizontal.Right;
            default:
                return RectangleAnchorHorizontal.Center;
        }
    }

    public static RectangleAnchorVertical ToRectangleAnchorVertical(this VerticalTextAlignment verticalTextAlignment)
    {
        switch (verticalTextAlignment)
        {
            case VerticalTextAlignment.Top:
               return RectangleAnchorVertical.Top;
            case VerticalTextAlignment.Bottom:
                return RectangleAnchorVertical.Bottom;
            default:
                return RectangleAnchorVertical.Center;
        }
    }
1 Like

ah, it comes from skia and cannot be changed. thx for sharing the util!