Getting Fonts, Sizes, Bold,...Etc

Getting fonts, sizes, bold,...etc

GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
String[] fonts = ge.getAvailableFontFamilyNames();

The sizes and styles can be set at run-time.

E.G.

Font Chooser

import java.awt.*;
import javax.swing.*;

public class ShowFonts {

public static void main(String[] args) {
SwingUtilities.invokeLater(() -> {
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
String[] fonts = ge.getAvailableFontFamilyNames();
JComboBox fontChooser = new JComboBox(fonts);
fontChooser.setRenderer(new FontCellRenderer());
JOptionPane.showMessageDialog(null, fontChooser);
});
}
}

class FontCellRenderer extends DefaultListCellRenderer {

public Component getListCellRendererComponent(
JList list,
Object value,
int index,
boolean isSelected,
boolean cellHasFocus) {
JLabel label = (JLabel)super.getListCellRendererComponent(
list,value,index,isSelected,cellHasFocus);
Font font = new Font(value.toString(), Font.PLAIN, 20);
label.setFont(font);
return label;
}
}

JavaDoc

The JDoc for GraphicsEnvironment.getAvailableFontFamilyNames() state in part..

Returns an array containing the names of all font families in this GraphicsEnvironment localized for the default locale, as returned by Locale.getDefault()..

See also:

getAllFonts()..

How to set a custom font's size and other attributes (bold, italic, etc) in Java SWING

createFont returns a Font and you can call deriveFont(...) on this, passing in a float for the point size, or an int and float for Font style and point size. I cannot say whether it will work for your particular situation, but it's worth a try.

e.g.,

InputStream is = OptionsValues.class.getResourceAsStream("fonts//KOMIKAX_.ttf");
TITLEFONT = Font.createFont(Font.TRUETYPE_FONT, is).deriveFont(Font.BOLD, 40f);

Get selected font size/style

Use GetTextMetrics to get this for selected font

typedef struct tagTEXTMETRIC {
LONG tmHeight;
LONG tmAscent;
LONG tmDescent;
LONG tmInternalLeading;
LONG tmExternalLeading;
LONG tmAveCharWidth;
LONG tmMaxCharWidth;
LONG tmWeight;
LONG tmOverhang;
LONG tmDigitizedAspectX;
LONG tmDigitizedAspectY;
TCHAR tmFirstChar;
TCHAR tmLastChar;
TCHAR tmDefaultChar;
TCHAR tmBreakChar;
BYTE tmItalic;
BYTE tmUnderlined;
BYTE tmStruckOut;
BYTE tmPitchAndFamily;
BYTE tmCharSet;
} TEXTMETRIC, *PTEXTMETRIC;

Italic is a boolean byte value tmItalic. Boldness is tmWeight with values of about 700 or more usually being thought of as "Bold".

How I can use the differents font size of a *.ttf file?

Truetype fonts may include glyphs of different styles and weights (so you can specify what the file contains) but they scale so it wouldn't make sense to specify what sizes of fonts the file contains: It contains all of them.

The size-adjust property allows you to apply a multiplier to the scaling so that different fonts can play better together.

How do I decide if a selection of text in CRichEditCtrl has multiple font sizes?

As the above answer notes, the easiest way I can think of to do this is to use the Text Object Model (TOM), which is accessed through the ITextDocument COM interface. To get at this from your rich edit control (note code not tested, but should work):

CComPtr<IRichEditOle> richOle;
richOle.Attach(edit.GetIRichEditOle());
CComQIPtr<ITextDocument> textDoc(richOle);

Then get a range. Here this is for the selected text, but one of the advantages of TOM is that you can operate on any range, not just what's selected.

CComPtr<ITextSelection> range;
textDoc->GetSelection(&range);

Then get the font for the range, and see what its characteristics are, e.g.

CComPtr<ITextFont> font;
range->GetFont(&font);
long size;
font->GetSize(&size);

If the range is formatted with a single font size, you'll get that back in "size". If there's multiple font sizes, you'll get the value "tomUndefined" instead.

How to add multiple font files for the same font?

The solution seems to be to add multiple @font-face rules, for example:

@font-face {
font-family: "DejaVu Sans";
src: url("fonts/DejaVuSans.ttf");
}
@font-face {
font-family: "DejaVu Sans";
src: url("fonts/DejaVuSans-Bold.ttf");
font-weight: bold;
}
@font-face {
font-family: "DejaVu Sans";
src: url("fonts/DejaVuSans-Oblique.ttf");
font-style: italic, oblique;
}
@font-face {
font-family: "DejaVu Sans";
src: url("fonts/DejaVuSans-BoldOblique.ttf");
font-weight: bold;
font-style: italic, oblique;
}

By the way, it would seem Google Chrome doesn't know about the format("ttf") argument, so you might want to skip that.

(This answer was correct for the CSS 2 specification. CSS3 only allows for one font-style rather than a comma-separated list.)

Visual Studio 2022: Bold font in the entire editor

This is due to Microsoft changing the default font in Visual Studio 2022 from Consolas to Cascadia Code: a font which is designed to make text easier to read for people with disabilities like Character Dysmorphia or Dyslexia. You can change this back to "Consolas", the VS 2019 font by going to

Tools > Options > Environment > Fonts and Colors > "Show settings for: Text Edit" > Font: "Consolas"

How is font size calculated?

See the spec:

The font size corresponds to the em
square, a concept used in typography.

Note that certain glyphs may bleed
outside their em squares.



Related Topics



Leave a reply



Submit