TTF Text Font

Post here for help on using FreeCAD's graphical user interface (GUI).
Forum rules
and Helpful information
IMPORTANT: Please click here and read this first, before asking for help

Also, be nice to others! Read the FreeCAD code of conduct!
arnschmidt
Posts: 2
Joined: Wed May 23, 2018 7:37 pm

TTF Text Font

Postby arnschmidt » Wed May 23, 2018 7:44 pm

Where can I find a TTF Text font like ARIAL-TTF?
I was trying to do the Draft ShapeString Tutorial.
Thanks
chrisb
Posts: 21503
Joined: Tue Mar 17, 2015 9:14 am

Re: TTF Text Font

Postby chrisb » Wed May 23, 2018 10:27 pm

On which system?

Windows keeps the fonts in C:\Windows\Fonts.
Tilli
Posts: 93
Joined: Thu Jan 25, 2018 10:38 pm

Re: TTF Text Font

Postby Tilli » Thu May 24, 2018 8:34 am

arnschmidt wrote:
Wed May 23, 2018 7:44 pm
Where . . .?
chrisb wrote:
Wed May 23, 2018 10:27 pm
. . . Windows . . . C:\Windows\Fonts.

Hi and welcome to the forum.

In any future help-requests you'd do well to follow the IMPORTANT GUIDELINES shown above in jarring red. It helps the experts to help us. ;)

Fonts.

On this Linux Mint there are various fonts in /usr/share/fonts/, and TTFs specifically in the ./truetype subfolder.

But, do you mean where to find them on your system, or where to get some in the first place..? In which case, maybe a typical search could help..?

Just a thought.

;)
arnschmidt
Posts: 2
Joined: Wed May 23, 2018 7:37 pm

Re: TTF Text Font

Postby arnschmidt » Sat May 26, 2018 10:45 pm

I'm using Windows 10 and FreeCAD 0.17
I know I have font libraries on Windows 10 and WP software but I can't find any True Type Fonts (.TTF)
I have searched the internet for them but no luck.
It seems that is the only format the Draft ShapeString tutorial, that came with FreeCAD, looks for.
chrisb
Posts: 21503
Joined: Tue Mar 17, 2015 9:14 am

Re: TTF Text Font

Postby chrisb » Sat May 26, 2018 11:14 pm

arnschmidt wrote:
Sat May 26, 2018 10:45 pm
I know I have font libraries on Windows 10 and WP software but I can't find any True Type Fonts (.TTF)
Windows may hide the font folder. Enter the path mentioned above directly in the addressline of the file explorer.
TheMarkster
Posts: 1090
Joined: Thu Apr 05, 2018 1:53 am

Re: TTF Text Font

Postby TheMarkster » Sat May 26, 2018 11:31 pm

I downloaded some off the web, but I don't recall which site. Do a google search for free true type fonts.

edit: Open a command prompt: Start -> cmd.exe. Enter:

Code: Select all

c:
cd c:\windows
dir /a /s *.ttf
My FreeCAD video series on youtube: https://www.youtube.com/playlist?list=P ... 48TYSI7GET
User avatar
wandererfan
Posts: 3486
Joined: Tue Nov 06, 2012 5:42 pm

Re: TTF Text Font

Postby wandererfan » Sun May 27, 2018 1:20 pm

arnschmidt wrote:
Sat May 26, 2018 10:45 pm
I know I have font libraries on Windows 10 and WP software but I can't find any True Type Fonts (.TTF)
...
It seems that is the only format the Draft ShapeString tutorial, that came with FreeCAD, looks for.
ShapeString should handle *.ttf, *.pfb and *.otf fonts.

Code: Select all

https://www.wfonts.com/font/arial
mario52
Posts: 2810
Joined: Wed May 16, 2012 2:13 pm

Re: TTF Text Font

Postby mario52 » Sun May 27, 2018 5:06 pm

hi

can be useful for Windows 10

Code: Select all

# -*- coding: utf-8 -*-
from __future__ import unicode_literals
"""
***************************************************************************
*   Copyright (c) 2016 2017 2018 2019  <mario52>                          *
*                                                                         *
*   This file is a supplement to the FreeCAD CAx development system.      *
*                                                                         *
*   This program is free software; you can redistribute it and/or modify  *
*   it under the terms of the GNU Lesser General Public License (LGPL)    *
*   as published by the Free Software Foundation; either version 2 of     *
*   the License, or (at your option) any later version.                   *
*   for detail see the LICENCE text file.                                 *
*                                                                         *
*   This software is distributed in the hope that it will be useful,      *
*   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
*   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
*   GNU Library General Public License for more details.                  *
*                                                                         *
*   You should have received a copy of the GNU Library General Public     *
*   License along with this macro; if not, write to the Free Software     *
*   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  *
*   USA                                                                   *
***************************************************************************
*           WARNING! All changes in this file will be lost and            *  
*                  may cause malfunction of the program                   *
***************************************************************************
"""
#Macro_Window_fonts 20/09/2016 27/05/2018 23/03/2019
#
#OS: Windows 10
#Word size of OS: 64-bit
#Word size of FreeCAD: 64-bit
#Version: 0.16.6706 (Git)
#Build type: Release
#Branch: releases/FreeCAD-0-16
#Hash: f86a4e411ff7848dea98d7242f43b7774bee8fa0
#Python version: 2.7.8
#Qt version: 4.8.7
#Coin version: 4.0.0a
#OCC version: 6.8.0.oce-0.17
#
__title__   = "Window_fonts"
__author__  = "Mario52"
__url__     = "http://www.freecadweb.org/index-fr.html"
__version__ = "00.03"
__date__    = "23/03/2019"

import PySide
from PySide import QtGui ,QtCore
from PySide.QtGui import *
from PySide.QtCore import *
 
import Draft, Part, PartGui, FreeCADGui, FreeCAD
from FreeCAD import Base
import math
from math import sqrt, pi, sin, cos, asin

Gui = FreeCADGui
App = FreeCAD

global PolicePath
import os, time, sys
import platform

if platform.system() == "Windows" :
    PolicePath = "C:/Windows/Fonts/ARIAL.TTF"
elif platform.system() == "Linux" :
    PolicePath = "/usr/share/fonts/truetype/ubuntu-font-family/Ubuntu-M.ttf"
elif platform.system() == "Darwin":
    PolicePath = "/Library/Fonts/Arial.ttf"
else:
    PolicePath = "C:/Windows/Fonts/ARIAL.TTF"

nomPolice = os.path.basename(PolicePath).split('.')[0]
     
class Ui_MainWindow(object):

    def __init__(self ):
        self.window = MainWindow
        #self.path   = FreeCAD.ConfigGet("AppHomePath")
        self.path   = FreeCAD.ConfigGet("UserAppData")
        self.path   = self.path.replace("\\","/")

        self.nomPolice = "Arial"
        self.FontSize  = 10.0
        self.fonteComp = ""
        self.font      = ""

        self.red       = (204.0 / 255)
        self.green     = (204.0 / 255)
        self.blue      = (204.0 / 255)
        self.alpha     = 1.0                      # 1.0 = visibility max (opacity)

        self.fontGlobal_08 = QtGui.QFont()        #
        self.fontGlobal_08.setFamily("Arial")     #
        self.fontGlobal_08.setPointSize(8.0)      #
 
    def setupUi(self, MainWindow):
        self.window = MainWindow

        MainWindow.setObjectName(("MainWindow"))
        MainWindow.resize(300, 200)
        MainWindow.setMinimumSize(QtCore.QSize(300, 200))
        MainWindow.setMaximumSize(QtCore.QSize(300, 200))
        self.centralwidget = QtGui.QWidget(MainWindow)
        self.centralwidget.setObjectName(("centralwidget"))

        self.fontComboBox = QtGui.QFontComboBox(self.centralwidget)               # 1: for QtGui.QFontComboBox() 
        self.fontComboBox.setGeometry(QtCore.QRect(20, 20, 257, 22))
        self.fontComboBox.setToolTip("Model 1")
        self.fontComboBox.setMaxVisibleItems(20)
        self.fontComboBox.setFontFilters(QtGui.QFontComboBox.AllFonts)
        font = QtGui.QFont()
        font.setFamily(("Arial"))
        font.setPointSize(10)
        self.fontComboBox.setCurrentFont(font)
        self.fontComboBox.setObjectName(("fontComboBox"))
        self.fontComboBox.currentFontChanged.connect(self.on_fontComboBox)         # ""

        self.PB_Fonte_expl = QtGui.QPushButton(self.centralwidget)                 # 2: for QFileDialog()
        self.PB_Fonte_expl.setGeometry(QtCore.QRect(20, 50, 257, 23))
        self.PB_Fonte_expl.setToolTip("Model 2")
        self.PB_Fonte_expl.setFont(self.fontGlobal_08)                #
        self.PB_Fonte_expl.setObjectName(("PB_Fonte_expl"))
        self.PB_Fonte_expl.clicked.connect(self.on_PB_Fonte_expl)                  # ""

        self.PB_Fonte = QtGui.QPushButton(self.centralwidget)                      # 3: for QtGui.QFontDialog.getFont()
        self.PB_Fonte.setGeometry(QtCore.QRect(20, 80, 257, 23))
        self.PB_Fonte.setToolTip("Model 3")
        self.PB_Fonte.setFont(self.fontGlobal_08)                     #
        self.PB_Fonte.setObjectName(("PB_Fonte"))
        self.PB_Fonte.clicked.connect(self.on_PB_Fonte)                            # ""

        self.textEdit = QtGui.QTextEdit(self.centralwidget)
        self.textEdit.setGeometry(QtCore.QRect(20, 110, 257, 41))
        self.textEdit.setToolTip("Your text")
        self.textEdit.setFont(self.fontGlobal_08)                     #
        self.textEdit.setObjectName(("textEdit"))

        self.PB_OK = QtGui.QPushButton(self.centralwidget)
        self.PB_OK.setGeometry(QtCore.QRect(20, 160, 75, 23))
        self.PB_OK.setToolTip("Validate")
        self.PB_OK.setFont(self.fontGlobal_08)
        self.PB_OK.setObjectName(("PB_OK"))
        self.PB_OK.clicked.connect(self.on_PB_OK)                                   # ""

        self.PB_color = QtGui.QPushButton(self.centralwidget)
        self.PB_color.setGeometry(QtCore.QRect(100, 160, 75, 23))
        self.PB_color.setToolTip("Text color")
        self.PB_color.setFont(self.fontGlobal_08)
        self.PB_color.setObjectName(("PB_color"))
        self.PB_color.clicked.connect(self.on_PB_color)                             # ""

        self.checkBox_1 = QtGui.QCheckBox(self.centralwidget)
        self.checkBox_1.setGeometry(QtCore.QRect(240, 160, 75, 23))
        self.checkBox_1.setToolTip("Create text")
        self.checkBox_1.setObjectName(("checkBox_1"))
        self.checkBox_1.setChecked(False)
#        self.checkBox_1.clicked.connect(self.on_checkBox_1_clicked)

        MainWindow.setCentralWidget(self.centralwidget)

        self.retranslateUi(MainWindow)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)

    def retranslateUi(self, MainWindow):
        MainWindow.setWindowFlags(PySide.QtCore.Qt.WindowStaysOnTopHint)            # PySide cette fonction met la fenetre en avant
        MainWindow.setWindowTitle("Window_fonts")
        self.PB_Fonte_expl.setText("Model 2")
        self.PB_Fonte.setText("Model 3")
        self.PB_OK.setText("Ok")
        self.PB_color.setText("Color")
        self.checkBox_1.setText("Text")

    def on_fontComboBox(self):                                                      # 1: for QtGui.QFontComboBox(self.centralWidget) 1:
        global PolicePath
        self.fonte     = self.fontComboBox.currentFont()
        self.nomPolice = self.fonte.rawName()
        self.FontSize  = self.fonte.pointSizeF()
        if platform.system() == "Windows" :
            self.pathFont  = QtGui.QDesktopServices.storageLocation(QtGui.QDesktopServices.FontsLocation) # path font search OK for Windows
            FreeCAD.Console.PrintMessage("Windows" + "\n")
        else:
            self.pathFont  = QtGui.QDesktopServices.storageLocation(QtGui.QDesktopServices.FontsLocation) # 
            FreeCAD.Console.PrintMessage("linux" + "\n")
        self.fonteComp = self.pathFont + "/" + self.nomPolice + ".TTF"              # complete path and name
        self.fonteComp = self.fonteComp.replace("\\","/")                           # replace the slatch to "/"
        FreeCAD.Console.PrintMessage(str("1: fontComboBox " ) + self.fonteComp + "\n")

    def on_PB_Fonte_expl(self):                                                     # 2: for QFileDialog                              2:
        global PolicePath
        font = ""
        self.window.hide()
        font, Filter = PySide.QtGui.QFileDialog.getOpenFileName(None, "New Font", PolicePath, "*.ttf") #PySide
#        font, Filter = PySide.QtGui.QFileDialog.getOpenFileName(None, "New Font", PolicePath, "File TTF (*.ttf);;"
#                                                                                                  "File FON (*.fon);;"
#                                                                                                  "All type (*.*);;") #PySide
        self.window.show()
        try:
            if font != "":
                self.fonte = font
                self.fonteComp = self.fonte
                self.fonteComp = self.fonteComp.replace("\\","/")                           # replace the slatch to "/"
            FreeCAD.Console.PrintMessage(str("2: QFileDialog ") + self.fonteComp + "\n")
        except Exception:
            None

    def on_PB_Fonte(self):                                                          # 3: for QtGui.QFontDialog.getFont()             3:
        global PolicePath
        self.window.hide()
        font, ok = QtGui.QFontDialog.getFont(QFont("Arial", 8))                     # ("Arial", 8) the font imposted if ok == False
        self.window.show()
        if ok:
            self.fonte     = font
            self.nomPolice = self.fonte.rawName()
            self.FontSize  = self.fonte.pointSizeF()
            if platform.system() == "Windows" :
                self.pathFont  = QtGui.QDesktopServices.storageLocation(QtGui.QDesktopServices.FontsLocation) # path font search OK for Windows
                FreeCAD.Console.PrintMessage("Windows" + "\n")
            else:
                self.pathFont  = QtGui.QDesktopServices.storageLocation(QtGui.QDesktopServices.FontsLocation) # 
                FreeCAD.Console.PrintMessage("linux" + "\n")
            self.fonteComp = self.pathFont + "/" + self.nomPolice + ".TTF"          # complete path and name
            self.fonteComp = self.fonteComp.replace("\\","/")                       # replace the slatch to "/"
        FreeCAD.Console.PrintMessage(str("3: QFontDialog ") + self.fonteComp + "\n")

    def on_PB_OK(self):
        global PolicePath
        texte = unicode(self.textEdit.toPlainText())
        if texte != "":
            if self.fonteComp == "": self.fonteComp = PolicePath
            if self.FontSize == 0:   self.FontSize  = 2.0

            if " " in self.fonteComp:
                self.fonteComp = self.fonteComp.replace(" ","")
                tx = Draft.makeShapeString(String=unicode(texte),FontFile= self.fonteComp ,Size=self.FontSize,Tracking=0) # create the text
#                tx = Draft.makeShapeString(String=unicode(texte),FontFile= chr(34)+self.fonteComp+chr(34) ,Size=self.FontSize,Tracking=0) # create the text
#                FreeCAD.Console.PrintMessage("trouve" + "\n")
            else:
                tx = Draft.makeShapeString(String=unicode(texte),FontFile= self.fonteComp ,Size=self.FontSize,Tracking=0) # create the text
#                FreeCAD.Console.PrintMessage("non trouve" + "\n")

            lab = self.fonteComp.split("/")[-1]
            tx.Label = lab
            tx.ViewObject.ShapeColor = (self.red, self.green, self.blue)                # color of ShapeString

            ## Texte
            if self.checkBox_1.isChecked():
                tx2 = Draft.makeText([texte],point=FreeCAD.Vector(0.0,0.0,0.0))
                tx2.ViewObject.FontName  = self.fonteComp
                tx2.ViewObject.TextColor = (self.red, self.green, self.blue)            # color of text
                tx2.Label = lab

            FreeCAD.ActiveDocument.recompute()

            FreeCAD.Console.PrintMessage(str(texte) + "\n")
            FreeCAD.Console.PrintMessage(self.fonteComp + "\n")
            FreeCAD.Console.PrintMessage(str(self.FontSize) + "\n")
            FreeCAD.Console.PrintMessage("___________________" + "\n")

        else:
            FreeCAD.Console.PrintMessage("Not string"+"\n")

    def on_PB_color(self):
        self.PB_color.setStyleSheet("background-color: QPalette.Base")                  # original color system (button "Color")
        self.window.hide()                                                              # hide the window for open color dialog
        couleur = QtGui.QColorDialog.getColor()                                         # open the color dialog
        if couleur.isValid():                                                           # if color is valid (Ok)

            self.red   = couleur.redF()                                                 # extract the color RGBa with Qt (0.0 to 1.0)
            self.green = couleur.greenF()                                               # extract the color RGBa with Qt (0.0 to 1.0)
            self.blue  = couleur.blueF()                                                # extract the color RGBa with Qt (0.0 to 1.0)
            self.alpha = couleur.alphaF()                                               # extract the color RGBa with Qt (0.0 to 1.0)

#            self.red   = int(str(couleur.name()[1:3]),16)                               # example extract color use and convert hexadecimal code (0 to 255)
#            self.green = int(str(couleur.name()[3:5]),16)                               # example extract color use and convert hexadecimal code (0 to 255)
#            self.blue  = int(str(couleur.name()[5:7]),16)                               # example extract color use and convert hexadecimal code (0 to 255)

        self.PB_color.setStyleSheet("background-color: " + couleur.name())              # with hexadecimal value ex: "#ffaaff" (button "Color")
        self.window.show()                                                              # show the window and close color

#______________________________________________________________________________________

doc = FreeCAD.ActiveDocument
if doc == None:
    doc = FreeCAD.newDocument()

MainWindow = QtGui.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()


EDIT :23/03/2019 16h17 Paris
modify and correct section 2 (as FC)

EDIT :27/05/2018 23h10 Paris
modify code for create annotation text with the ShapeString

mario
Last edited by mario52 on Sat Mar 23, 2019 3:19 pm, edited 1 time in total.
Maybe you need a special feature, go into Macros_recipes and Code_snippets, Dialog creation. My macros on Gist.github.
User avatar
Sembazuru
Posts: 7
Joined: Wed Jan 23, 2019 4:58 pm
Location: Delaware, USA

Re: TTF Text Font

Postby Sembazuru » Sat Mar 23, 2019 2:26 am

mario52 wrote:
Sun May 27, 2018 5:06 pm
hi

can be useful for Windows 10
I'm sure that code might be useful for Windows 10, but what does it do and how would I use it if I wanted to?

Sorry for such a basic question, but I'm a rank amateur with FreeCAD, and just want to easily choose fonts on my Windows 10 laptop for use with the ShapeString tool. The select a font file is quite non-helpful in these modern days with font chooser requesters. Especially with Windows giving me a permission error when I try to navigate to C:\Windows\Fonts using the provided file requester. I'm not trying to write to the fonts directory, just read from it. Stupid Windows...
User avatar
wandererfan
Posts: 3486
Joined: Tue Nov 06, 2012 5:42 pm

Re: TTF Text Font

Postby wandererfan » Sat Mar 23, 2019 12:54 pm

Sembazuru wrote:
Sat Mar 23, 2019 2:26 am
The select a font file is quite non-helpful in these modern days with font chooser requesters.
The issue is that ShapeString needs the actual font file to extract the glyph geometry. The connection between font name and font file name/location is not obvious and is not available from say QFontDialog.

In your case, I would mimic the Linux solution - make your own fonts directory (where you have full permissions) and copy the font files you need into that directory.

Most common TTF fonts are available for download.