tk.Text: Tag Font - python-3.x

Is there a way to get a proper Font instance out of a tag, so I can change just the size? I know I can dump it in tk.font.Font but that seems really inefficient.
for tag in self.tag_names():
#I need `f` to be a Font instance, not just a string
f = self.tag_cget(tag, 'font')

If you used font objects to create the tags, then you can get the font name from the tag and then use nametofont to convert it to an instance of tkinter.font.Font. However, this only works if the tag has a font associated with it, and if the font is a font object rather than shorthand notation (eg: ("Helvetica", 24, "bold")).
from tkinter.font import nametofont
...
for tag_name in self.tag_names():
font_name = self.tag_cget(tag_name, "font")
if font_name:
font = nametofont(font_name)
size = int(font.cget("size"))
font.configure(size = size + delta)

One method is to premake all of the Font instances and assign the instances to the applicable tag's font option. When you want to change the sizes, loop over the stored Font instances, and change their sizes directly. Every tag that uses your Font instances will change accordingly.

Related

How to use icon font with Jetpack Compose?

in before xml in TextView we have
holder.binding.iconInfo.text = "&#x${iconValue}"
and set out icon font to it with typeface dose anyone can help same thing but with jetpack compose
The specific syntax you mention that can be used with TextViews is due to the fact that XML is used when defining layouts. That is an XML numeric character entity that specifies a character with its unicode value in hexadecimal notation.
So it is not related to TextViews, layouts, fonts or icons.
If you want to do the same with a String in Kotlin and the character value is dynamic, you can do something like this
val text = "${Char(iconValue)}"
In Compose a custom font can be defined either as part of your Theme -> Typography and then used as text style or you can define it directly like this
val fontFamily = FontFamily(
// If you have the font in resources
Font(R.font.my_font_normal, weight = FontWeight.Normal),
Font(R.font.my_font_bold, weight = FontWeight.Bold),
Font(R.font.my_font_italic, weight = FontWeight.Normal, style = FontStyle.Italic),
// If you are loading the font at runtime from a file
Font(File(...), weight = FontWeight.Normal),
// ...
)
// Use with Text composable
Text(text = text, fontFamily = fontFamily)

Multiple variations of TKInter fonts

I am trying to get a better understanding of using fonts in tkinter and ttk.
My plan is to have two different styles for headings, each with their own font size. I used nametofont() to create an instance of the font and then set the size in two different styles:
labelFont = tkinter.font.nametofont('TkTextFont')
labelFont.config(weight='bold')
ttk.Style().configure("TLabel", font=labelFont, size=12)
ttk.Style().configure("heading.TLabel", font=labelFont, size=48)
then apply the styles to headings:
heading = ttk.Label(root, text="Heading", style="heading.TLabel")
label = ttk.Label(root, text="Label", style="TLabel") # is style redundant?
Unfortunately, I don’t get two different sizes, so this is obviously the wrong approach.
I also tried something like this:
labelFont = tkinter.font.nametofont('TkTextFont')
headingFont = tkinter.font.nametofont('TkTextFont')
# etc
thinking that I would get two independent instance of the font, but they appear to be the same instance. If they were independent, I could have used configure() to give each of them their own font size.
I took this approach because I wanted to use the built-in named font, and use variables to maintain consistency. What is the correct approach to this?
You need to use .config(size=...) on the different instances of Font:
labelFont = tkinter.font.nametofont('TkTextFont')
labelFont.config(weight='bold', size=12)
# create a clone of labelFont using Font.copy()
headingFont = labelFont.copy()
headingFont.config(size=48)
s = ttk.Style()
s.configure('TLabel', font=labelFont) # apply to all instance of ttk.Label
s.configure('heading.TLabel', font=headingFont, size=96)
heading = ttk.Label(root, text='Heading', style='heading.TLabel')
label = ttk.Label(root, text='Label') # style='TLabel' is not necessary

In Scala.js, modifying svg.Stylable.style

Working with Scala.js, I have created some SVG elements like Text, Line, Rect, and now I am trying to set the style attribute with code like this, where element is of type svg.Stylable:
element.style.fillOpacity = "0.0"
element.style.stroke = "yellow"
element.style.strokeWidth = "2"
I tried different variations of above code, but the desired style does not realize, and when I inspect the element in the Browser, the style attribute is an empty String (""). I am able to set other attributes with no problem (e.g. x, y, width, height).
How do I set the style? Thanks!
For SVG modications in Scala.js I normally use the d3 library (scala.js facade: https://github.com/spaced/scala-js-d3).
Then u can use:
d3.select("#mySvgElement").attr("style", "stroke:yellow; stroke-width:2") // etc.
*Edit: mySvgElement would be the ID of the element i want to change the style for. You can also other different kind of selectors.

Display number in rectangle

Note that I am using Python3 and Phoenix.
I would like to display a number (double, but that does not matter now) formatted in some way (again, no matter what that way is) within a rectangle: almost a wx.StaticText but not editable by the user. This is to display some data coming from some hardware, such as a temperature.
Is there such a widget?
I tried with using the default wx.StaticText with a style but I must have done something wrong:
hbox = wx.BoxSizer(wx.HORIZONTAL)
title = wx.StaticText(parent, label=label)
title.SetLabelMarkup("<b>{}</b>".format(label))
hbox.Add(title, border=5)
value = wx.StaticText(parent, label="3.141592", style=wx.BORDER_RAISED)
value.SetWindowStyle(wx.BORDER_SIMPLE)
hbox.Add(value, border=5)
title = wx.StaticText(parent, label="\u2103")
hbox.Add(title, border=5)
Shows this on Linux (Fedora 24, GTK):
Wouldn't using a wx.TextCtrl set to read only do the job?
Temp = wx.TextCtrl(panel1, value="3.141592", style=wx.TE_READONLY)
Temp.SetBackgroundColour('green')
The simplest solution is to just use wxStaticText with a border style (e.g. wxBORDER_SIMPLE, ...). If you don't like the appearance this results in, it's pretty simple to make your own widget drawing whatever border you desire: just create a window, define its wxEVT_PAINT handler and draw the (presumably centered) text in it and a border outside of it.

Custom font does not render correctly when using ShowText

I have a custom view in which I am trying to draw text using a Windows font (calibri.ttf). However, I am getting different behaviour between using the DrawString & ShowText functions.
I have embedded the font as part of the app (added it to the UIAppFonts list & set it's build action to Content) and I all works fine when I use the DrawString method from my custom UIView e.g.
Code
public override void Draw (System.Drawing.RectangleF rect)
{
base.Draw (rect);
DrawString("Calibri font via DrawString", new RectangleF(10, 10, 100, 100), UIFont.FromName("Calibri", 16f));
}
Result
However, if I attempt to draw the same text this time using ShowText it appears as if the font isn't encoding the text correctly, or the character mapping is wrong.
Code
public override void Draw (System.Drawing.RectangleF rect)
{
base.Draw (rect);
var ctx = UIGraphics.GetCurrentContext();
ctx.SelectFont("Calibri", 16f, MonoTouch.CoreGraphics.CGTextEncoding.FontSpecific);
ctx.ShowTextAtPoint(10, 10, "Calibri font via ShowText using SelectFont");
}
Result
UPDATE - Here is what I get if I use MacRoman encoding instead of FontSpecific:
I have also tried loading in the font manually and using that but then I get nothing at all, it's like it doesn't recognise the font e.g.
var fontPath = NSBundle.MainBundle.PathForResource("calibri", "ttf");
var provider = new CGDataProvider(fontPath);
var font = CGFont.CreateFromProvider(provider);
var ctx = UIGraphics.GetCurrentContext();
ctx.SetFont(font);
ctx.ShowTextAtPoint(10, 20, "Calibri font via ShowText using SetFont");
I know that DrawString is UIKit & ShowText is CG (Quartz) so I understand that there may be differences. However, from what I gathered the only difference with DrawString was it corrected the issue with the difference in Offset (CG being at the bottom left/UIKit being at the top left).
NOTE - The underlying problem I have is I need to use this font to draw text onto a layer via a custom CALayerDelegate. I don't have access to the DrawString function from in there, therefore, the only way I can see to draw the text is via ShowText. Alternative solutions are welcome!
This really looks like an encoding issue. Try using CGTextEncoding.MacRoman instead of CGTextEncoding.FontSpecific (even Arial wouldn't render, as expected, with FontSpecific).
UPDATE Oct 12th
a) your last code sample won't work because Apple doc specifically states not to use SetFont and ShowText together. Quote follow:
Quartz uses font data provided by the system to map each byte of the array through the encoding vector of the current font to obtain the glyph to display. Note that the font must have been set using CGContextSelectFont. Don’t use CGContextShowText in conjunction with CGContextSetFont.
b) the CGTextEncoding.MacRoman code works with several other iPhone-supplied fonts. I'm beginning to suspect it's something about the Calibri.ttf font itself that is not supported by iOS.

Resources