Skip to content

Commit 3969f56

Browse files
authored
Merge pull request #3727 from OpenShot/fix-font-dialog
Fixing a few issues with the title editor
2 parents 3788e71 + bcc30f1 commit 3969f56

File tree

1 file changed

+50
-42
lines changed

1 file changed

+50
-42
lines changed

src/windows/title_editor.py

+50-42
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ def __init__(self, edit_file_path=None, duplicate=False):
9494

9595
self.font_weight = 'normal'
9696
self.font_style = 'normal'
97+
self.font_size_pixel = 20
9798

9899
self.new_title_text = ""
99100
self.sub_title_text = ""
@@ -279,8 +280,17 @@ def load_svg_template(self):
279280
for i in range(0, self.text_fields):
280281
if len(self.tspan_node[i].childNodes) > 0:
281282
text = self.tspan_node[i].childNodes[0].data
283+
ar = self.tspan_node[i].attributes["style"].value.split(";")
282284
title_text.append(text)
283285

286+
# Set font size (for possible font dialog)
287+
fs = self.find_in_list(ar, "font-size:")
288+
fs_value = ar[fs][10:]
289+
if fs_value.endswith("px"):
290+
self.qfont.setPixelSize(float(fs_value[:-2]))
291+
elif fs_value.endswith("pt"):
292+
self.qfont.setPointSizeF(float(fs_value[:-2]))
293+
284294
# Create Label
285295
label = QLabel()
286296
label_line_text = _("Line %s:") % str(i + 1)
@@ -414,6 +424,7 @@ def btnFont_clicked(self):
414424
self.font_family = fontinfo.family()
415425
self.font_style = fontinfo.styleName()
416426
self.font_weight = fontinfo.weight()
427+
self.font_size_pixel = fontinfo.pixelSize()
417428
self.set_font_style()
418429

419430
# Something changed, so update temp SVG
@@ -432,7 +443,7 @@ def update_font_color_button(self):
432443
"""Updates the color shown on the font color button"""
433444

434445
# Loop through each TEXT element
435-
for node in self.text_node:
446+
for node in self.text_node + self.tspan_node:
436447

437448
# Get the value in the style attribute
438449
s = node.attributes["style"].value
@@ -449,6 +460,14 @@ def update_font_color_button(self):
449460
# If the color was in an invalid format, try the next text element
450461
log.debug('Failed to parse {} as color value'.format(txt))
451462

463+
# Look up color if needed
464+
# Some colors are located in a different node
465+
if color.startswith("url(#") and self.xmldoc.getElementsByTagName("defs").length == 1:
466+
color_ref_id = color[5:-1]
467+
ref_color = self.get_ref_color(color_ref_id)
468+
if ref_color:
469+
color = ref_color
470+
452471
opacity = self.find_in_list(ar, "opacity:")
453472

454473
try:
@@ -485,6 +504,31 @@ def update_font_color_button(self):
485504
color.setAlphaF(opacity)
486505
self.font_color_code = color
487506

507+
def get_ref_color(self, id):
508+
"""Get the color value from a reference id (i.e. linearGradient3267)"""
509+
found_color = ""
510+
for ref_node in self.xmldoc.getElementsByTagName("defs")[0].childNodes:
511+
if ref_node.attributes and "id" in ref_node.attributes:
512+
ref_node_id = ref_node.attributes["id"].value
513+
if id == ref_node_id:
514+
# Found a matching color reference
515+
if "xlink:href" in ref_node.attributes:
516+
# look up color reference again
517+
xlink_ref_id = ref_node.attributes["xlink:href"].value[1:]
518+
return self.get_ref_color(xlink_ref_id)
519+
if "href" in ref_node.attributes:
520+
# look up color reference again
521+
xlink_ref_id = ref_node.attributes["href"].value[1:]
522+
return self.get_ref_color(xlink_ref_id)
523+
elif ref_node.childNodes:
524+
for stop_node in ref_node.childNodes:
525+
if "stop" == stop_node.nodeName:
526+
# get color from stop
527+
ar = stop_node.attributes["style"].value.split(";")
528+
sc = self.find_in_list(ar, "stop-color:")
529+
return ar[sc][11:]
530+
return found_color
531+
488532
def update_background_color_button(self):
489533
"""Updates the color shown on the background color button"""
490534

@@ -545,9 +589,8 @@ def update_background_color_button(self):
545589

546590
def set_font_style(self):
547591
'''sets the font properties'''
548-
549592
# Loop through each TEXT element
550-
for text_child in self.text_node:
593+
for text_child in self.text_node + self.tspan_node:
551594
# set the style elements for the main text node
552595
s = text_child.attributes["style"].value
553596
# split the text node so we can access each part
@@ -558,40 +601,20 @@ def set_font_style(self):
558601
# ignoring font-weight, as not sure what it represents in Qt.
559602
fs = self.find_in_list(ar, "font-style:")
560603
ff = self.find_in_list(ar, "font-family:")
604+
fp = self.find_in_list(ar, "font-size:")
561605
if fs:
562606
ar[fs] = "font-style:" + self.font_style
563607
if ff:
564608
ar[ff] = "font-family:" + self.font_family
609+
if fp:
610+
ar[fp] = "font-size:%spx" % self.font_size_pixel
565611
# rejoin the modified parts
566612
t = ";"
567613
self.title_style_string = t.join(ar)
568614

569615
# set the text node
570616
text_child.setAttribute("style", self.title_style_string)
571617

572-
# Loop through each TSPAN
573-
for tspan_child in self.tspan_node:
574-
# set the style elements for the main text node
575-
s = tspan_child.attributes["style"].value
576-
# split the text node so we can access each part
577-
ar = s.split(";")
578-
# we need to find each element that we are changing, shouldn't assume
579-
# they are in the same position in any given template.
580-
581-
# ignoring font-weight, as not sure what it represents in Qt.
582-
fs = self.find_in_list(ar, "font-style:")
583-
ff = self.find_in_list(ar, "font-family:")
584-
if fs:
585-
ar[fs] = "font-style:" + self.font_style
586-
if ff:
587-
ar[ff] = "font-family:" + self.font_family
588-
# rejoin the modified parts
589-
t = ";"
590-
self.title_style_string = t.join(ar)
591-
592-
# set the text node
593-
tspan_child.setAttribute("style", self.title_style_string)
594-
595618
def set_bg_style(self, color, alpha):
596619
'''sets the background color'''
597620

@@ -620,7 +643,7 @@ def set_bg_style(self, color, alpha):
620643
def set_font_color_elements(self, color, alpha):
621644

622645
# Loop through each TEXT element
623-
for text_child in self.text_node:
646+
for text_child in self.text_node + self.tspan_node:
624647

625648
# SET TEXT PROPERTIES
626649
s = text_child.attributes["style"].value
@@ -641,21 +664,6 @@ def set_font_color_elements(self, color, alpha):
641664
t = ";"
642665
text_child.setAttribute("style", t.join(ar))
643666

644-
# Loop through each TSPAN
645-
for tspan_child in self.tspan_node:
646-
647-
# SET TSPAN PROPERTIES
648-
s = tspan_child.attributes["style"].value
649-
# split the text node so we can access each part
650-
ar = s.split(";")
651-
fill = self.find_in_list(ar, "fill:")
652-
if fill is None:
653-
ar.append("fill:" + color)
654-
else:
655-
ar[fill] = "fill:" + color
656-
t = ";"
657-
tspan_child.setAttribute("style", t.join(ar))
658-
659667
def accept(self):
660668
app = get_app()
661669
_ = app._tr

0 commit comments

Comments
 (0)