Improve new text item continuity.

pull/2/head
Matthew Chen 6 years ago
parent 371c12bd40
commit c31d469651

@ -489,13 +489,15 @@ public class ImageEditorCanvasView: UIView {
// I don't think we need to enable allowsFontSubpixelQuantization // I don't think we need to enable allowsFontSubpixelQuantization
// or set truncationMode. // or set truncationMode.
// This text needs to be rendered at a scale that reflects the sceen scaling // This text needs to be rendered at a scale that reflects:
// AND the item's scaling. //
layer.contentsScale = UIScreen.main.scale * item.scaling // * The screen scaling (so that text looks sharp on Retina devices.
// * The item's scaling (so that text doesn't become blurry as you make it larger).
// * Model transform (so that text doesn't become blurry as you zoom the content).
layer.contentsScale = UIScreen.main.scale * item.scaling * transform.scaling
// TODO: Min with measured width. // TODO: Min with measured width.
let maxWidth = imageFrame.size.width * item.unitWidth let maxWidth = imageFrame.size.width * item.unitWidth
// let maxWidth = viewSize.width * item.unitWidth
let maxSize = CGSize(width: maxWidth, height: CGFloat.greatestFiniteMagnitude) let maxSize = CGSize(width: maxWidth, height: CGFloat.greatestFiniteMagnitude)
// TODO: Is there a more accurate way to measure text in a CATextLayer? // TODO: Is there a more accurate way to measure text in a CATextLayer?

@ -101,10 +101,20 @@ public class ImageEditorTextItem: ImageEditorItem {
} }
@objc @objc
public class func empty(withColor color: ImageEditorColor, unitWidth: CGFloat, fontReferenceImageWidth: CGFloat) -> ImageEditorTextItem { public class func empty(withColor color: ImageEditorColor,
unitWidth: CGFloat,
fontReferenceImageWidth: CGFloat,
scaling: CGFloat,
rotationRadians: CGFloat) -> ImageEditorTextItem {
// TODO: Tune the default font size. // TODO: Tune the default font size.
let font = UIFont.boldSystemFont(ofSize: 30.0) let font = UIFont.boldSystemFont(ofSize: 30.0)
return ImageEditorTextItem(text: "", color: color, font: font, fontReferenceImageWidth: fontReferenceImageWidth, unitWidth: unitWidth) return ImageEditorTextItem(text: "",
color: color,
font: font,
fontReferenceImageWidth: fontReferenceImageWidth,
unitWidth: unitWidth,
rotationRadians: rotationRadians,
scaling: scaling)
} }
@objc @objc
@ -148,6 +158,19 @@ public class ImageEditorTextItem: ImageEditorItem {
scaling: newScaling) scaling: newScaling)
} }
@objc
public func copy(withUnitCenter newUnitCenter: CGPoint, unitWidth newUnitWidth: CGFloat) -> ImageEditorTextItem {
return ImageEditorTextItem(itemId: itemId,
text: text,
color: color,
font: font,
fontReferenceImageWidth: fontReferenceImageWidth,
unitCenter: newUnitCenter,
unitWidth: newUnitWidth,
rotationRadians: rotationRadians,
scaling: scaling)
}
public override func outputScale() -> CGFloat { public override func outputScale() -> CGFloat {
return scaling return scaling
} }

@ -104,6 +104,8 @@ public class ImageEditorTextViewController: OWSViewController, VAlignTextViewDel
private let textItem: ImageEditorTextItem private let textItem: ImageEditorTextItem
private let isNewItem: Bool
private let maxTextWidthPoints: CGFloat private let maxTextWidthPoints: CGFloat
private let textView = VAlignTextView(alignment: .center) private let textView = VAlignTextView(alignment: .center)
@ -117,10 +119,12 @@ public class ImageEditorTextViewController: OWSViewController, VAlignTextViewDel
init(delegate: ImageEditorTextViewControllerDelegate, init(delegate: ImageEditorTextViewControllerDelegate,
model: ImageEditorModel, model: ImageEditorModel,
textItem: ImageEditorTextItem, textItem: ImageEditorTextItem,
isNewItem: Bool,
maxTextWidthPoints: CGFloat) { maxTextWidthPoints: CGFloat) {
self.delegate = delegate self.delegate = delegate
self.model = model self.model = model
self.textItem = textItem self.textItem = textItem
self.isNewItem = isNewItem
self.maxTextWidthPoints = maxTextWidthPoints self.maxTextWidthPoints = maxTextWidthPoints
self.canvasView = ImageEditorCanvasView(model: model, self.canvasView = ImageEditorCanvasView(model: model,
itemIdsToIgnore: [textItem.itemId]) itemIdsToIgnore: [textItem.itemId])
@ -176,6 +180,7 @@ public class ImageEditorTextViewController: OWSViewController, VAlignTextViewDel
}) })
configureTextView() configureTextView()
textView.addRedBorder()
self.view.layoutMargins = UIEdgeInsets(top: 16, left: 20, bottom: 16, right: 20) self.view.layoutMargins = UIEdgeInsets(top: 16, left: 20, bottom: 16, right: 20)
@ -245,7 +250,33 @@ public class ImageEditorTextViewController: OWSViewController, VAlignTextViewDel
} }
private func completeAndDismiss() { private func completeAndDismiss() {
self.delegate?.textEditDidComplete(textItem: textItem, text: textView.text, color: paletteView.selectedValue) var newTextItem = textItem
if isNewItem {
let view = self.canvasView.gestureReferenceView
let viewBounds = view.bounds
// Ensure continuity of the new text item's location
// with its apparent location in this text editor.
let locationInView = view.convert(textView.bounds.center, from: textView)
let textCenterImageUnit = ImageEditorCanvasView.locationImageUnit(forLocationInView: locationInView,
viewBounds: viewBounds,
model: model,
transform: model.currentTransform())
// Same, but for size.
let imageFrame = ImageEditorCanvasView.imageFrame(forViewSize: viewBounds.size,
imageSize: model.srcImageSizePixels,
transform: model.currentTransform())
let unitWidth = textView.width() / imageFrame.width
newTextItem = textItem.copy(withUnitCenter: textCenterImageUnit, unitWidth: unitWidth)
}
// Hide the text view immediately to avoid animation glitches in the dismiss transition.
textView.isHidden = true
self.delegate?.textEditDidComplete(textItem: newTextItem, text: textView.text, color: paletteView.selectedValue)
self.dismiss(animated: false) { self.dismiss(animated: false) {
// Do nothing. // Do nothing.

@ -146,11 +146,19 @@ public class ImageEditorView: UIView {
let textWidthPoints = viewSize.width * ImageEditorTextItem.kDefaultUnitWidth let textWidthPoints = viewSize.width * ImageEditorTextItem.kDefaultUnitWidth
let textWidthUnit = textWidthPoints / imageFrame.size.width let textWidthUnit = textWidthPoints / imageFrame.size.width
// New items should be aligned "upright", so they should have the _opposite_
// of the current transform rotation.
let rotationRadians = -model.currentTransform().rotationRadians
// Similarly, the size of the text item shuo
let scaling = 1 / model.currentTransform().scaling
let textItem = ImageEditorTextItem.empty(withColor: currentColor, let textItem = ImageEditorTextItem.empty(withColor: currentColor,
unitWidth: textWidthUnit, unitWidth: textWidthUnit,
fontReferenceImageWidth: imageFrame.size.width) fontReferenceImageWidth: imageFrame.size.width,
scaling: scaling,
rotationRadians: rotationRadians)
edit(textItem: textItem) edit(textItem: textItem, isNewItem: true)
} }
@objc func didTapDone(sender: UIButton) { @objc func didTapDone(sender: UIButton) {
@ -178,7 +186,7 @@ public class ImageEditorView: UIView {
return return
} }
edit(textItem: textItem) edit(textItem: textItem, isNewItem: false)
} }
// MARK: - Pinch Gesture // MARK: - Pinch Gesture
@ -408,7 +416,7 @@ public class ImageEditorView: UIView {
// MARK: - Edit Text Tool // MARK: - Edit Text Tool
private func edit(textItem: ImageEditorTextItem) { private func edit(textItem: ImageEditorTextItem, isNewItem: Bool) {
Logger.verbose("") Logger.verbose("")
// TODO: // TODO:
@ -418,6 +426,7 @@ public class ImageEditorView: UIView {
let textEditor = ImageEditorTextViewController(delegate: self, let textEditor = ImageEditorTextViewController(delegate: self,
model: model, model: model,
textItem: textItem, textItem: textItem,
isNewItem: isNewItem,
maxTextWidthPoints: maxTextWidthPoints) maxTextWidthPoints: maxTextWidthPoints)
self.delegate?.imageEditor(presentFullScreenView: textEditor, self.delegate?.imageEditor(presentFullScreenView: textEditor,
isTransparent: false) isTransparent: false)

@ -202,6 +202,10 @@ public extension CGPoint {
y: Swift.max(y, value.y)) y: Swift.max(y, value.y))
} }
public var length: CGFloat {
return sqrt(x * x + y * y)
}
public static let unit: CGPoint = CGPoint(x: 1.0, y: 1.0) public static let unit: CGPoint = CGPoint(x: 1.0, y: 1.0)
public static let unitMidpoint: CGPoint = CGPoint(x: 0.5, y: 0.5) public static let unitMidpoint: CGPoint = CGPoint(x: 0.5, y: 0.5)

Loading…
Cancel
Save