一个 Swift 图形库,旨在用于我的 SwiftyOLED 库。欢迎在基于 ARM 的设备上使用 Swift 创建显示库时,为其设计 API。
该库使用类似 iOS 的坐标系。 X 轴向右递增。 Y 轴向下递增。
每个定义的图元都符合 Drawable 协议。 该库定义了以下几组图元:
Bresenham 算法用于绘制倾斜的线。 即使您可以使用 ObliqueLine
类绘制水平和垂直线,但使用 HorizontalLine
或 VerticalLine
类会更快。
ObliqueLine(from origin: Point, to endPoint: Point)
HorizontalLine(from origin: Point, to endPoint: Point)
HorizontalLine(from origin: Point, lenght: UInt)
VerticalLine(from origin: Point, to endPoint: Point)
VerticalLine(from origin: Point, lenght: UInt)
如果您向 HorizontalLine
或 VerticalLine
传递了不正确的值,即具有不同 y 坐标或 x 坐标的点,则库将回退到使用原点的 y 或 x 坐标。
Rectangle(at origin: Point = Point(x: 0, y: 0), height: UInt, width: UInt)
Square(at origin: Point = Point(x: 0, y: 0), sideSize a: UInt)
Ellipse(at origin: Point = Point(x: 0, y: 0), yRadius: UInt, xRadius: UInt)
Ellipse(at origin: Point = Point(x: 0, y: 0), height: UInt, width: UInt)
Circle(at origin: Point = Point(x: 0, y: 0), radius: UInt)
Circle(at origin: Point = Point(x: 0, y: 0), width: UInt)
Triangle(at origin: Point = Point(x: 0, y: 0), corner1: Point, corner2: Point, corner3: Point)
如果您未指定 pathToFont 参数,则该库将尝试使用嵌入到 Raspbian 中的 DejaVuSans 字体。 如果失败,该库将尝试从 fontconfig
包中获取所有已安装字体的列表,并使用第一个字体。 如果失败,该库将引发 fatalError。
Text(_ text: String, font pathToFont: String? = nil, at origin: Point = Point(x: 0, y: 0), pixelHeight: UInt32 = 16, pixelWidth: UInt32 = 16)
您可以使用两种方法之一来更改文本大小
public func setPixel(height: UInt32, width: UInt32)
public func setChar(height: Int, width: Int, horizontalResolution: UInt32, verticalResolution: UInt32)
这两种方法只是 FreeType 2 库中 FT_Set_Pixel_Sizes
和 FT_Set_Char_Size
的包装器。 有关更多信息,请访问其文档。
Rectangle
、Square
、Ellipse
、Circle
、Triangle
类型可以被填充。 此功能由 .fill()
和 .filled()
方法提供。 唯一的区别是 .filled()
返回它被调用的对象的填充副本。
Drawable 协议保证对于每个对象,您可以通过调用 .generatePointsForDrawing()
来生成整数和整数元组的数组。 这实际上是一个点数组(每个点都有 X 和 Y 坐标)。 它们可以传递给您选择的显示库,因此可以显示出来。
欢迎您提出更改建议、报告错误和问题、创建 pull requests。