Skip to content

Request: Text h_align = pixie.JUSTIFY_ALIGN #26

Description

@IagoBeuller

Would be nice if add this alignment style, also give access to Arrangement attributes(selectionRects, lines, positions).

I've made this little library for making this feature for me, it behaves like the Microsoft Word Office.

import
nimpy,
pixie,
unicode
pyExportModule("pixie_utils")
constLF=Rune(10)
NR=Rune(0)
procjustifyArrangement*(arrangement: Arrangement, maxWidth: float32) {.raises: [].} =procright(rect: Rect): float32=
rect.x + rect.w
let
runes: ptr= arrangement.runes.addr
positions: ptr= arrangement.positions.addr
rects: ptr= arrangement.selectionRects.addr
last_lineId: int= arrangement.lines.len-1var
rune, last_rune: Rune
last_wordRuneId: int
spaces_idx: seq[int]
inc_width, space_width, line_width, x_offset: float32for lineId, (start, stop) in arrangement.lines:
last_rune = runes[stop]
# This line must not be justified, if it is the last or if the last rune is a breakline char.if lineId == last_lineId or last_rune.uint32==LF.uint32:
continueecho runes[start..stop]
# Get the spaces indexes of this line to increase their width, and get the line width.
spaces_idx =@[]
last_rune =NRfor r_id in start..stop:
rune = runes[r_id]
ifnot rune.isWhiteSpace():
if last_rune.isWhiteSpace():
spaces_idx.add(r_id-1)
last_wordRuneId = r_id
last_rune = runes[r_id]
line_width = rects[last_wordRuneId].right
echo"Line spaces: ", spaces_idx.len
if spaces_idx.len >0:
# Get the amount of pixels/units to increase each space width in the middle of the line.
inc_width = (maxWidth - line_width) / spaces_idx.len.float32if inc_width >0:
space_width = rects[spaces_idx[0]].w + inc_width
# Adjust the runes X position
x_offset =0for r_id in spaces_idx[0]..stop:
positions[r_id].x += x_offset
if r_id in spaces_idx:
rects[r_id].x += x_offset
rects[r_id].w = space_width
x_offset += inc_width
else:
rects[r_id].x += x_offset
procjustifyArrangement(arrangement_ref: int, maxWidth: float32) {.exportpy: "justifyArrangement".} =justifyArrangement(cast[Arrangement](arrangement_ref), maxWidth)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions