Skip to content

Commit

Permalink
#1 working on boundingRectWithSize()
Browse files Browse the repository at this point in the history
  • Loading branch information
colemancda committed May 31, 2016
1 parent eb2d6f9 commit c12c85b
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 27 deletions.
2 changes: 1 addition & 1 deletion Cartfile.resolved
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
github "PureSwift/Cairo" "aada9126e1e2f1ef1f6dd05dd6448c5ffc1c330b"
github "PureSwift/Silica" "b092caa06a5c3537c32d03192687f16aaf5b5fd8"
github "PureSwift/Silica" "4aa95950e0322abf059ac904881bcfdbd7cf3bc1"
6 changes: 2 additions & 4 deletions Sources/Cacao/NSStringDrawing.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,9 @@ public extension String {

let textAttributes = TextAttributes(UIKit: attributes)

var rect = Rect()
let textFrame = self.contentFrame(for: Rect(size: size), textMatrix: context.textMatrix, attributes: textAttributes)

let textFrame = self.contentFrame(for: Rect(size: size), context: context, attributes: textAttributes)

return rect
return textFrame
}
}

Expand Down
30 changes: 8 additions & 22 deletions Sources/Cacao/TextRendering.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,43 +15,29 @@ public extension String {
func draw(in rect: Rect, context: Silica.Context, attributes: TextAttributes = TextAttributes()) {

// set context values
context.fontSize = attributes.font.size
context.setFont(attributes.font.silicaFont)
context.fillColor = attributes.color
context.setTextAttributes(attributes)

// render

let textRect = self.contentFrame(for: rect, context: context, attributes: attributes)
let textRect = self.contentFrame(for: rect, textMatrix: context.textMatrix, attributes: attributes)

context.textPosition = textRect.origin

context.show(text: self)
}

func singleLineWidth(font: Cacao.Font, context: Silica.Context) -> Double {

let scaledFont = font.silicaFont.scaledFont

let size = font.size

let glyphs = self.unicodeScalars.map { scaledFont[UInt($0.value)] }
func contentFrame(for bounds: Rect, textMatrix: AffineTransform = AffineTransform.identity, attributes: TextAttributes = TextAttributes()) -> Rect {

let textWidth = context.advances(for: glyphs).reduce(Double(0), combine: { $0.0 + $0.1.width })

return textWidth
}

func contentFrame(for bounds: Rect, context: Silica.Context, attributes: TextAttributes = TextAttributes()) -> Rect {
// assume horizontal layout (not rendering non-latin languages)

// calculate frame

let scaledFont = attributes.font.silicaFont.scaledFont
let textWidth = attributes.font.silicaFont.singleLineWidth(text: self, fontSize: attributes.font.size, textMatrix: textMatrix)

let glyphs = self.unicodeScalars.map { scaledFont[UInt($0.value)] }
let lines = 1

let textWidth = context.advances(for: glyphs).reduce(Double(0), combine: { $0.0 + $0.1.width })
let textHeight = attributes.font.size * Double(lines)

var textRect = Rect(x: bounds.x, y: bounds.y, width: textWidth, height: attributes.font.size) // height == font.size
var textRect = Rect(x: bounds.x, y: bounds.y, width: textWidth, height: textHeight) // height == font.size

switch attributes.paragraphStyle.alignment {

Expand Down

0 comments on commit c12c85b

Please sign in to comment.