Commit 643306c3 authored by sumpfralle's avatar sumpfralle

implemented left/center/right align for text editor


git-svn-id: https://pycam.svn.sourceforge.net/svnroot/pycam/trunk@856 bbaffbd6-741e-11dd-a85d-61de82d9cad9
parent fbfe649b
...@@ -7573,6 +7573,9 @@ Please read the description of the Server Mode (linked below) to understand the ...@@ -7573,6 +7573,9 @@ Please read the description of the Server Mode (linked below) to understand the
<property name="position">4</property> <property name="position">4</property>
</packing> </packing>
</child> </child>
<child>
<object class="GtkHBox" id="hbox35">
<property name="visible">True</property>
<child> <child>
<object class="GtkLabel" id="FontDialogInputLabel"> <object class="GtkLabel" id="FontDialogInputLabel">
<property name="visible">True</property> <property name="visible">True</property>
...@@ -7583,6 +7586,73 @@ Please read the description of the Server Mode (linked below) to understand the ...@@ -7583,6 +7586,73 @@ Please read the description of the Server Mode (linked below) to understand the
<packing> <packing>
<property name="expand">False</property> <property name="expand">False</property>
<property name="fill">False</property> <property name="fill">False</property>
<property name="position">0</property>
</packing>
</child>
<child>
<object class="GtkHButtonBox" id="hbuttonbox3">
<property name="visible">True</property>
<child>
<object class="GtkRadioButton" id="FontTextAlignLeft">
<property name="label">gtk-justify-left</property>
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="receives_default">False</property>
<property name="use_stock">True</property>
<property name="active">True</property>
<property name="draw_indicator">False</property>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">False</property>
<property name="position">0</property>
</packing>
</child>
<child>
<object class="GtkRadioButton" id="FontTextAlignCenter">
<property name="label">gtk-justify-center</property>
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="receives_default">False</property>
<property name="use_stock">True</property>
<property name="active">True</property>
<property name="draw_indicator">False</property>
<property name="group">FontTextAlignLeft</property>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">False</property>
<property name="position">1</property>
</packing>
</child>
<child>
<object class="GtkRadioButton" id="FontTextAlignRight">
<property name="label">gtk-justify-right</property>
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="receives_default">False</property>
<property name="use_stock">True</property>
<property name="active">True</property>
<property name="draw_indicator">False</property>
<property name="group">FontTextAlignLeft</property>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">False</property>
<property name="position">2</property>
</packing>
</child>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">False</property>
<property name="pack_type">end</property>
<property name="position">1</property>
</packing>
</child>
</object>
<packing>
<property name="expand">False</property>
<property name="position">5</property> <property name="position">5</property>
</packing> </packing>
</child> </child>
......
...@@ -26,6 +26,10 @@ from pycam.Geometry.Model import ContourModel ...@@ -26,6 +26,10 @@ from pycam.Geometry.Model import ContourModel
from pycam.Geometry.Line import Line from pycam.Geometry.Line import Line
from pycam.Geometry.Point import Point from pycam.Geometry.Point import Point
TEXT_ALIGN_LEFT = 0
TEXT_ALIGN_CENTER = 1
TEXT_ALIGN_RIGHT = 2
class Letter(TransformableContainer): class Letter(TransformableContainer):
...@@ -99,16 +103,20 @@ class Charset(object): ...@@ -99,16 +103,20 @@ class Charset(object):
def get_authors(self): def get_authors(self):
return self.authors return self.authors
def render(self, text, origin=None, skew=0, line_spacing=1.0, pitch=1.0): def render(self, text, origin=None, skew=0, line_spacing=1.0, pitch=1.0,
align=None):
result = ContourModel() result = ContourModel()
if origin is None: if origin is None:
origin = Point(0, 0, 0) origin = Point(0, 0, 0)
if align is None:
align = TEXT_ALIGN_LEFT
base = origin base = origin
letter_spacing = self.letterspacing * pitch letter_spacing = self.letterspacing * pitch
word_spacing = self.wordspacing * pitch word_spacing = self.wordspacing * pitch
line_factor = self.default_linespacing * self.linespacingfactor \ line_factor = self.default_linespacing * self.linespacingfactor \
* line_spacing * line_spacing
for line in text.splitlines(): for line in text.splitlines():
current_line = ContourModel()
line_height = self.default_height line_height = self.default_height
for character in line: for character in line:
if character == " ": if character == " ":
...@@ -121,7 +129,7 @@ class Charset(object): ...@@ -121,7 +129,7 @@ class Charset(object):
new_model.append(line) new_model.append(line)
for polygon in new_model.get_polygons(): for polygon in new_model.get_polygons():
# add polygons instead of lines -> more efficient # add polygons instead of lines -> more efficient
result.append(polygon) current_line.append(polygon)
# update line height # update line height
line_height = max(line_height, charset_letter.maxy()) line_height = max(line_height, charset_letter.maxy())
# shift the base position # shift the base position
...@@ -132,5 +140,18 @@ class Charset(object): ...@@ -132,5 +140,18 @@ class Charset(object):
base = base.add(Point(letter_spacing, 0, 0)) base = base.add(Point(letter_spacing, 0, 0))
# go to the next line # go to the next line
base = Point(origin.x, base.y - line_height * line_factor, origin.z) base = Point(origin.x, base.y - line_height * line_factor, origin.z)
if not current_line.maxx is None:
if align == TEXT_ALIGN_CENTER:
current_line.shift(-current_line.maxx / 2, 0, 0)
elif align == TEXT_ALIGN_RIGHT:
current_line.shift(-current_line.maxx, 0, 0)
else:
# left align
if current_line.minx != 0:
current_line.shift(-current_line.minx, 0, 0)
for polygon in current_line.get_polygons():
result.append(polygon)
# the text should be just above the x axis
result.shift(0, -result.miny, 0)
return result return result
...@@ -34,6 +34,8 @@ import pycam.Utils.log ...@@ -34,6 +34,8 @@ import pycam.Utils.log
import pycam.Utils import pycam.Utils
from pycam.Geometry.utils import sqrt from pycam.Geometry.utils import sqrt
from pycam.Gui.OpenGLTools import ModelViewWindowGL from pycam.Gui.OpenGLTools import ModelViewWindowGL
from pycam.Geometry.Letters import TEXT_ALIGN_LEFT, TEXT_ALIGN_CENTER, \
TEXT_ALIGN_RIGHT
from pycam.Utils import ProgressCounter from pycam.Utils import ProgressCounter
from pycam.Toolpath import Bounds from pycam.Toolpath import Bounds
from pycam import VERSION from pycam import VERSION
...@@ -380,6 +382,10 @@ class ProjectGui: ...@@ -380,6 +382,10 @@ class ProjectGui:
obj.set_value(1.0) obj.set_value(1.0)
obj.connect("value-changed", obj.connect("value-changed",
self.update_font_dialog_preview) self.update_font_dialog_preview)
for objname in ("FontTextAlignLeft", "FontTextAlignCenter",
"FontTextAlignRight"):
self.gui.get_object(objname).connect("toggled",
self.update_font_dialog_preview)
self._font_dialog_window_visible = False self._font_dialog_window_visible = False
self._font_dialog_window_position = None self._font_dialog_window_position = None
self._fonts = None self._fonts = None
...@@ -1660,16 +1666,26 @@ class ProjectGui: ...@@ -1660,16 +1666,26 @@ class ProjectGui:
return True return True
def get_font_dialog_text_rendered(self): def get_font_dialog_text_rendered(self):
text_buffer = self.gui.get_object("FontDialogInput").get_buffer() input_field = self.gui.get_object("FontDialogInput")
text_buffer = input_field.get_buffer()
text = text_buffer.get_text(text_buffer.get_start_iter(), text = text_buffer.get_text(text_buffer.get_start_iter(),
text_buffer.get_end_iter()) text_buffer.get_end_iter())
if text: if text:
skew = self.gui.get_object("FontSideSkewValue").get_value() skew = self.gui.get_object("FontSideSkewValue").get_value()
line_space = self.gui.get_object("FontLineSpacingValue").get_value() line_space = self.gui.get_object("FontLineSpacingValue").get_value()
pitch = self.gui.get_object("FontCharacterSpacingValue").get_value() pitch = self.gui.get_object("FontCharacterSpacingValue").get_value()
# get the active align setting
for objname, value, justification in (
("FontTextAlignLeft", TEXT_ALIGN_LEFT, gtk.JUSTIFY_LEFT),
("FontTextAlignCenter", TEXT_ALIGN_CENTER, gtk.JUSTIFY_CENTER),
("FontTextAlignRight", TEXT_ALIGN_RIGHT, gtk.JUSTIFY_RIGHT)):
obj = self.gui.get_object(objname)
if obj.get_active():
align = value
input_field.set_justification(justification)
font_name = self.font_selector.get_active_text() font_name = self.font_selector.get_active_text()
return self._fonts[font_name].render(text, skew=skew, return self._fonts[font_name].render(text, skew=skew,
line_spacing=line_space, pitch=pitch) line_spacing=line_space, pitch=pitch, align=align)
else: else:
# empty text # empty text
return None return None
...@@ -1696,20 +1712,28 @@ class ProjectGui: ...@@ -1696,20 +1712,28 @@ class ProjectGui:
preview_widget = self.gui.get_object("FontDialogPreview") preview_widget = self.gui.get_object("FontDialogPreview")
final_drawing_area = preview_widget.window final_drawing_area = preview_widget.window
text_model = self.get_font_dialog_text_rendered() text_model = self.get_font_dialog_text_rendered()
# always clean the background
x, y, width, height = preview_widget.get_allocation()
drawing_area = gtk.gdk.Pixmap(final_drawing_area, width, height)
drawing_area.draw_rectangle(preview_widget.get_style().white_gc, True,
0, 0, width, height)
# carefully check if there are lines in the rendered text # carefully check if there are lines in the rendered text
if text_model and (not text_model.maxx is None): if text_model and (not text_model.maxx is None):
x, y, width, height = preview_widget.get_allocation() x_fac = (width - 1) / (text_model.maxx - text_model.minx)
x_fac = width / (text_model.maxx - text_model.minx) y_fac = (height - 1) / (text_model.maxy - text_model.miny)
y_fac = height / (text_model.maxy - text_model.miny)
model_base_x = text_model.minx
model_base_y = text_model.miny
factor = min(x_fac, y_fac) factor = min(x_fac, y_fac)
drawing_area = gtk.gdk.Pixmap(final_drawing_area, width, height)
# always clean the background
drawing_area.draw_rectangle(preview_widget.get_style().white_gc, True, 0, 0, width, height)
gc = drawing_area.new_gc() gc = drawing_area.new_gc()
get_virtual_x = lambda x: int((x - model_base_x) * factor) if text_model.minx == 0:
get_virtual_y = lambda y: height - int((y - model_base_y) * factor) # left align
get_virtual_x = lambda x: int(x * factor)
elif text_model.maxx == 0:
# right align
get_virtual_x = lambda x: width + int(x * factor) - 1
else:
# center align
get_virtual_x = lambda x: int(width / 2.0 + x * factor) - 1
get_virtual_y = lambda y: \
height - int((y - text_model.miny) * factor) - 1
for polygon in text_model.get_polygons(): for polygon in text_model.get_polygons():
draw_points = [] draw_points = []
for point in polygon.get_points(): for point in polygon.get_points():
...@@ -1718,7 +1742,8 @@ class ProjectGui: ...@@ -1718,7 +1742,8 @@ class ProjectGui:
draw_points.append((x, y)) draw_points.append((x, y))
drawing_area.draw_lines(gc, draw_points) drawing_area.draw_lines(gc, draw_points)
final_gc = final_drawing_area.new_gc() final_gc = final_drawing_area.new_gc()
final_drawing_area.draw_drawable(final_gc, drawing_area, 0, 0, 0, 0, -1, -1) final_drawing_area.draw_drawable(final_gc, drawing_area, 0, 0, 0, 0,
-1, -1)
@gui_activity_guard @gui_activity_guard
def toggle_about_window(self, widget=None, event=None, state=None): def toggle_about_window(self, widget=None, event=None, state=None):
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment