Skip to content

Commit

Permalink
fix: font family
Browse files Browse the repository at this point in the history
  • Loading branch information
muedsa committed Sep 6, 2022
1 parent 4792e4d commit a8e5830
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.IOException;
import java.util.Arrays;
import java.util.Objects;
import java.util.Vector;
import java.util.regex.Pattern;
Expand Down Expand Up @@ -96,8 +95,6 @@ public ReaderWindow(Project project, ToolWindow toolWindow) {
}

private void createUIComponents(){
TextReaderEventManage eventManage = textReaderCore.getEventManage();

//字体
String[] fontFamilyNames = GraphicsEnvironment.getLocalGraphicsEnvironment().getAvailableFontFamilyNames();
DefaultComboBoxModel<String> comboBoxModel = new DefaultComboBoxModel<>(fontFamilyNames);
Expand All @@ -107,10 +104,7 @@ private void createUIComponents(){
TextReaderConfig.setConfigValue(TextReaderConfig.ConfigKey.FONT_FAMILY, e.getItem(), ReaderWindow.this);
}
});
int index = Arrays.binarySearch(fontFamilyNames, TextReaderConfig.getFontFamily());
if(index > 0){
fontFamilyEl.setSelectedIndex(index);
}
updateFontFamilyEl(TextReaderConfig.getFontFamily());

//字体大小
SpinnerModel fontSizeSpinnerModel = new SpinnerNumberModel(TextReaderConfig.getFontSize(), 0, 100, 1);
Expand Down Expand Up @@ -383,20 +377,20 @@ private void eventRegister(){
boolean notSelf = Objects.equals(ReaderWindow.this, event.getTag());
switch (configChangeEvent.getConfigKey()){
case FONT_FAMILY:
if(notSelf) fontFamilyEl.getModel().setSelectedItem(event.getData());
updateFontFamily();
if(notSelf) updateFontFamilyEl((String) event.getData());
updateEditorFontFamily();
break;
case FONT_SIZE:
if(notSelf) fontSizeSpinner.setValue(event.getData());
updateFontSize();
updateEditorFontSize();
break;
case LINE_SPACE:
if(notSelf) lineSpaceSpinner.setValue(event.getData());
updateLineSpace();
updateEditorLineSpace();
break;
case FIRST_LINE_INDENT:
if(notSelf) firstLineIndentSpinner.setValue(event.getData());
updateFirstLineIndent();
updateEditorFirstLineIndent();
break;
case PARAGRAPH_SPACE:
if(notSelf) paragraphSpaceSpinner.setValue(event.getData());
Expand Down Expand Up @@ -444,10 +438,10 @@ private void eventRegister(){

private void init(){
//字体风格初始化
updateFontFamily();
updateFontSize();
updateLineSpace();
updateFirstLineIndent();
updateEditorFontFamily();
updateEditorFontSize();
updateEditorLineSpace();
updateEditorFirstLineIndent();

Chapter chapter = textReaderCore.getChapter();
if(chapter != null){
Expand Down Expand Up @@ -477,31 +471,47 @@ private void updateRegex(){
}
}

private void updateFontFamily(){
private void updateFontFamilyEl(String newFontFamily) {
ComboBoxModel<String> model = fontFamilyEl.getModel();
int size = model.getSize();
int newSelectedIndex = -1;
for (int i = 0; i < size; i++) {
String fontFamily = model.getElementAt(i);
if(fontFamily.equals(newFontFamily)){
newSelectedIndex = i;
break;
}
}
if(newSelectedIndex > -1) {
fontFamilyEl.setSelectedIndex(newSelectedIndex);
}
}

private void updateEditorFontFamily(){
String fontFamily = (String)fontFamilyEl.getSelectedItem();
StyledDocument styledDocument = textContent.getStyledDocument();
SimpleAttributeSet attributes = new SimpleAttributeSet();
StyleConstants.setFontFamily(attributes, fontFamily);
styledDocument.setParagraphAttributes(0, styledDocument.getLength(), attributes, false);
}

private void updateFontSize(){
private void updateEditorFontSize(){
int fontSize = (int)fontSizeSpinner.getValue();
StyledDocument styledDocument = textContent.getStyledDocument();
SimpleAttributeSet attributes = new SimpleAttributeSet();
StyleConstants.setFontSize(attributes, fontSize);
styledDocument.setParagraphAttributes(0, styledDocument.getLength(), attributes, false);
}

private void updateLineSpace(){
private void updateEditorLineSpace(){
float lineSpace = ((Double)lineSpaceSpinner.getValue()).floatValue();
StyledDocument styledDocument = textContent.getStyledDocument();
SimpleAttributeSet attributes = new SimpleAttributeSet();
StyleConstants.setLineSpacing(attributes, lineSpace);
styledDocument.setParagraphAttributes(0, styledDocument.getLength(), attributes, false);
}

private void updateFirstLineIndent(){
private void updateEditorFirstLineIndent(){
float firstLineIndent = ((Integer)firstLineIndentSpinner.getValue()) * ((Integer)fontSizeSpinner.getValue()).floatValue();
StyledDocument styledDocument = textContent.getStyledDocument();
SimpleAttributeSet attributes = new SimpleAttributeSet();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

public class TextReaderConfig {
//字体
private static String fontFamily = "Microsoft YaHei UI";
private static String fontFamily = "SansSerif";

//字体大小
private static int fontSize = 12;
Expand Down

0 comments on commit a8e5830

Please sign in to comment.