Skip to content

Commit

Permalink
Add handling code for new "StringAlignmentJustify"
Browse files Browse the repository at this point in the history
Relates to #428

Because one text tool will support justified text (advanced text) and one will not (basic text), extra mapping code is needed to ensure text layers can still be converted between the two types.

This commit also adds the necessary UI for a new "justified" alignment, but the justification algorithm has *not* been implemented yet
  • Loading branch information
tannerhelland committed Sep 10, 2022
1 parent 637cea6 commit bc9c11b
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 32 deletions.
50 changes: 29 additions & 21 deletions Classes/pdGlyphCollection.cls
Original file line number Diff line number Diff line change
Expand Up @@ -1320,29 +1320,37 @@ Private Function CalculateGlyphPositions(ByRef boundingRect As RectF, ByVal hori
'Start with horizontal alignment
If (horizontalAlignment <> StringAlignmentNear) Then

'Because we kept a running tally of each line's width during the processing stages, it is easy
' to calculate offsets now.
For curLine = 0 To numOfLines - 1
'Justified text is TODO
If (horizontalAlignment = StringAlignmentJustify) Then

'For each line, replace its calculated line width with the difference between the bounding rect
' and the actual size.
lineDiff = boundingRectRight - m_LineWidths(curLine)
'Right/center alignment is much easier
Else

'Because we kept a running tally of each line's width during the processing stages, it is easy
' to calculate offsets now.
For curLine = 0 To numOfLines - 1

'For each line, replace its calculated line width with the difference between the bounding rect
' and the actual size.
lineDiff = boundingRectRight - m_LineWidths(curLine)

If (horizontalAlignment = StringAlignmentCenter) Then
hLineOffsets(curLine) = lineDiff * 0.5!
Else
hLineOffsets(curLine) = lineDiff
End If

Next curLine

'Each line width tracker now contains the offset required to make alignment work. Apply it to each glyph in turn.
For curGlyph = 0 To m_NumOfGlyphs - 1
With m_Glyphs(curGlyph)
.finalX = .finalX + hLineOffsets(.lineID)
End With
Next curGlyph

End If

If (horizontalAlignment = StringAlignmentCenter) Then
hLineOffsets(curLine) = lineDiff * 0.5!
Else
hLineOffsets(curLine) = lineDiff
End If

Next curLine

'Each line width tracker now contains the offset required to make alignment work. Apply it to each glyph in turn.
For curGlyph = 0 To m_NumOfGlyphs - 1
With m_Glyphs(curGlyph)
.finalX = .finalX + hLineOffsets(.lineID)
End With
Next curGlyph

End If

'Next, vertical alignment
Expand Down
16 changes: 13 additions & 3 deletions Classes/pdTextRenderer.cls
Original file line number Diff line number Diff line change
Expand Up @@ -773,6 +773,8 @@ Private Function GetAlignmentStringFromUnit(ByVal srcValue As GP_StringAlignment
GetAlignmentStringFromUnit = "center"
ElseIf (srcValue = StringAlignmentFar) Then
GetAlignmentStringFromUnit = "far"
ElseIf (srcValue = StringAlignmentJustify) Then
GetAlignmentStringFromUnit = "justify"
Else
InternalError "GetAlignmentStringFromUnit", "bad value: " & srcValue
GetAlignmentStringFromUnit = "near"
Expand All @@ -786,6 +788,8 @@ Private Function GetAlignmentUnitFromString(ByRef srcValue As String) As GP_Stri
GetAlignmentUnitFromString = StringAlignmentCenter
ElseIf (srcValue = "far") Then
GetAlignmentUnitFromString = StringAlignmentFar
ElseIf (srcValue = "justify") Then
GetAlignmentUnitFromString = StringAlignmentJustify
Else
InternalError "GetAlignmentUnitFromString", "bad value: " & srcValue
GetAlignmentUnitFromString = StringAlignmentNear
Expand Down Expand Up @@ -826,6 +830,7 @@ Friend Function GetGenericTextProperty(ByVal desiredProperty As PD_TextProperty)

Case ptp_HorizontalAlignment
GetGenericTextProperty = m_HorizontalAlignment
If (m_RenderingEngine <> te_PhotoDemon) And (m_HorizontalAlignment >= StringAlignmentJustify) Then GetGenericTextProperty = StringAlignmentNear

Case ptp_VerticalAlignment
GetGenericTextProperty = m_VerticalAlignment
Expand Down Expand Up @@ -988,7 +993,7 @@ Friend Function SetGenericTextProperty(ByVal desiredProperty As PD_TextProperty,
If (m_HorizontalAlignment <> CLng(newValue)) Then
m_HorizontalAlignment = CLng(newValue)
SetGenericTextProperty = True
If (m_GDIPlusStringFormat <> 0) Then GdipSetStringFormatAlign m_GDIPlusStringFormat, m_HorizontalAlignment
If (m_GDIPlusStringFormat <> 0) And (m_HorizontalAlignment < StringAlignmentJustify) Then GdipSetStringFormatAlign m_GDIPlusStringFormat, m_HorizontalAlignment
End If

Case ptp_VerticalAlignment
Expand Down Expand Up @@ -2090,8 +2095,9 @@ Private Function RenderTextToDIB_GDI(ByRef dstDIB As pdDIB, ByRef srcString As S
Dim xOffset As Long, yOffset As Long

Select Case m_HorizontalAlignment

Case StringAlignmentNear

'Justified text does *not* work with this renderer
Case StringAlignmentNear, StringAlignmentJustify
xOffset = 0

Case StringAlignmentCenter
Expand Down Expand Up @@ -2153,6 +2159,10 @@ Private Function GetDrawTextAlignmentFlags() As Win32_DrawTextConstants

Case StringAlignmentFar
GetDrawTextAlignmentFlags = GetDrawTextAlignmentFlags Or DT_RIGHT

'PhotoDemon can render justified text, but Win32 cannot.
Case Else
GetDrawTextAlignmentFlags = GetDrawTextAlignmentFlags Or DT_LEFT

End Select

Expand Down
4 changes: 2 additions & 2 deletions Forms/Toolpanel_TextBasic.frm
Original file line number Diff line number Diff line change
Expand Up @@ -142,8 +142,8 @@ Begin VB.Form toolpanel_TextBasic
Left = 7950
TabIndex = 4
Top = 345
Width = 1455
_ExtentX = 2566
Width = 1485
_ExtentX = 2619
_ExtentY = 767
ColorScheme = 1
End
Expand Down
12 changes: 7 additions & 5 deletions Forms/Toolpanel_Typography.frm
Original file line number Diff line number Diff line change
Expand Up @@ -589,14 +589,14 @@ Begin VB.Form toolpanel_TextAdvanced
Left = 7950
TabIndex = 42
Top = 345
Width = 1455
_ExtentX = 2566
Width = 1965
_ExtentX = 3466
_ExtentY = 767
ColorScheme = 1
End
Begin PhotoDemon.pdButtonStrip btsVAlignment
Height = 435
Left = 9510
Left = 9960
TabIndex = 32
Top = 345
Width = 1455
Expand All @@ -610,8 +610,8 @@ Begin VB.Form toolpanel_TextAdvanced
Left = 7920
TabIndex = 33
Top = 0
Width = 3015
_ExtentX = 5318
Width = 3495
_ExtentX = 6165
_ExtentY = 635
Caption = "alignment"
Value = 0 'False
Expand Down Expand Up @@ -1407,6 +1407,7 @@ Private Sub Form_Load()
btsHAlignment.AddItem vbNullString, 0
btsHAlignment.AddItem vbNullString, 1
btsHAlignment.AddItem vbNullString, 2
btsHAlignment.AddItem vbNullString, 3

btsVAlignment.AddItem vbNullString, 0
btsVAlignment.AddItem vbNullString, 1
Expand Down Expand Up @@ -2111,6 +2112,7 @@ Public Sub UpdateAgainstCurrentTheme()
btsHAlignment.AssignImageToItem 0, "format_alignleft", , buttonSize, buttonSize, usePDResamplerInstead:=rf_Box
btsHAlignment.AssignImageToItem 1, "format_aligncenter", , buttonSize, buttonSize, usePDResamplerInstead:=rf_Box
btsHAlignment.AssignImageToItem 2, "format_alignright", , buttonSize, buttonSize, usePDResamplerInstead:=rf_Box
btsHAlignment.AssignImageToItem 3, "format_alignjustify", , buttonSize, buttonSize, usePDResamplerInstead:=rf_Box

btsVAlignment.AssignImageToItem 0, "format_aligntop", , buttonSize, buttonSize, usePDResamplerInstead:=rf_Box
btsVAlignment.AssignImageToItem 1, "format_alignmiddle", , buttonSize, buttonSize, usePDResamplerInstead:=rf_Box
Expand Down
5 changes: 4 additions & 1 deletion Modules/GDIPlus.bas
Original file line number Diff line number Diff line change
Expand Up @@ -734,10 +734,13 @@ Public Enum GP_StringAlignment
StringAlignmentNear = 0
StringAlignmentCenter = 1
StringAlignmentFar = 2

'IMPORTANT NOTE: GDI+ cannot render justified text. This setting *ONLY* works with PhotoDemon's advanced text renderer.
StringAlignmentJustify = 3
End Enum

#If False Then
Private Const StringAlignmentNear = 0, StringAlignmentCenter = 1, StringAlignmentFar = 2
Private Const StringAlignmentNear = 0, StringAlignmentCenter = 1, StringAlignmentFar = 2, StringAlignmentJustify = 3
#End If

Public Enum GP_Unit
Expand Down

0 comments on commit bc9c11b

Please sign in to comment.