Browse Source

enable font-size in editor section in MDHL file

Le Tan 8 years ago
parent
commit
eeaeb0b567

+ 5 - 0
src/main.cpp

@@ -46,7 +46,12 @@ void VLogger(QtMsgType type, const QMessageLogContext &context, const QString &m
     Q_UNUSED(context);
 
     QTextStream stream(&g_logFile);
+
+#if defined(Q_OS_WIN)
+    stream << header << localMsg << "\r\n";
+#else
     stream << header << localMsg << "\n";
+#endif
 
     if (type == QtFatalMsg) {
         g_logFile.close();

+ 16 - 3
src/resources/styles/default.mdhl

@@ -1,12 +1,20 @@
 # This is the default markdown styles used for Peg-Markdown-Highlight
-# Created by Le Tan ([email protected])
+# created by Le Tan([email protected]).
+# For a complete description of the syntax, please refer to the original
+# documentation of the style parser
+# [The Syntax of PEG Markdown Highlight Stylesheets](http://hasseg.org/peg-markdown-highlight/docs/stylesheet_syntax.html).
+# VNote adds some styles in the syntax which will be marked [VNote] in the comment.
+#
+# Note: Empty lines within a section is NOT allowed.
+# Note: Do NOT modify this file directly. Copy it and tune your own style!
 
 editor
 # QTextEdit just choose the first available font, so specify the Chinese fonts first
 # Do not use "" to quote the name
 font-family: Hiragino Sans GB, 冬青黑体, Microsoft YaHei, 微软雅黑, Microsoft YaHei UI, WenQuanYi Micro Hei, 文泉驿雅黑, Dengxian, 等线体, STXihei, 华文细黑, Liberation Sans, Droid Sans, NSimSun, 新宋体, SimSun, 宋体, Helvetica, sans-serif, Tahoma, Arial, Verdana, Geneva, Georgia, Times New Roman
-# Style for trailing space
+# [VNote] Style for trailing space
 trailing-space: ffebee
+font-size: 12
 
 editor-selection
 foreground: eeeeee
@@ -14,9 +22,13 @@ background: 005fff
 
 editor-current-line
 background: c5cae9
+# [VNote] Vim insert mode cursor line background
 vim-insert-background: cdc0b0
+# [VNote] Vim normal mode cursor line background
 vim-normal-background: b0bec5
+# [VNote] Vim visual mode cursor line background
 vim-visual-background: 90caf9
+# [VNote] Vim replace mode cursor line background
 vim-replace-background: f8bbd0
 
 H1
@@ -97,7 +109,7 @@ foreground: 93a1a1
 VERBATIM
 foreground: 551a8b
 font-family: Consolas, Monaco, Andale Mono, Monospace, Courier New
-# Codeblock sylte from HighlightJS (bold, italic, underlined, color)
+# [VNote] Codeblock sylte from HighlightJS (bold, italic, underlined, color)
 # The last occurence of the same attribute takes effect
 hljs-comment: 6c6c6c
 hljs-keyword: 0000ee
@@ -114,6 +126,7 @@ hljs-selector-class: 880000
 hljs-quote: 880000
 hljs-template-tag: 880000
 hljs-deletion: 880000
+# Could specify multiple attribute in one line
 hljs-title: bold, 880000
 hljs-section: bold, 880000
 hljs-regexp: bc6060

+ 6 - 2
src/resources/styles/solarized-dark.mdhl

@@ -1,16 +1,20 @@
 # Styles using 'Solarized' color scheme
 # by Ethan Schoonover: http://ethanschoonover.com/solarized
-# 
+#
 # (dark background version)
 
 editor
 foreground: 93a1a1 # base1
 background: 002b36 # base03
 caret: ffffff
+font-size: 12
 
 editor-current-line
 background: 37474f
-vim-background: 004d40
+vim-insert-background: 004d40
+vim-normal-background: 004d40
+vim-visual-background: 004d40
+vim-replace-background: 004d40
 
 H1
 foreground: 6c71c4 # violet

+ 6 - 2
src/resources/styles/solarized-light.mdhl

@@ -1,16 +1,20 @@
 # Styles using 'Solarized' color scheme
 # by Ethan Schoonover: http://ethanschoonover.com/solarized
-# 
+#
 # (light background version)
 
 editor
 foreground: 586e75 # base01
 background: fdf6e3 # base3
 caret: 000000
+font-size: 12
 
 editor-current-line
 background: c5cae9
-vim-background: a5d6a7
+vim-insert-background: a5d6a7
+vim-normal-background: a5d6a7
+vim-visual-background: a5d6a7
+vim-replace-background: a5d6a7
 
 H1
 foreground: 6c71c4 # violet

+ 0 - 1
src/resources/vnote.ini

@@ -13,7 +13,6 @@ auto_list=true
 current_background_color=System
 current_render_background_color=System
 language=System
-editor_font_size=12
 
 ; 0 - Hoedown, 1 - Marked, 2 - Markdown-it, 3 - Showdown
 markdown_converter=2

+ 19 - 11
src/vconfigmanager.cpp

@@ -66,12 +66,7 @@ void VConfigManager::initialize()
     outputDefaultCssStyle();
     outputDefaultEditorStyle();
 
-    m_editorFontSize = getConfigFromSettings("global", "editor_font_size").toInt();
-    if (m_editorFontSize <= 0) {
-        m_editorFontSize = 12;
-    }
-    baseEditFont.setPointSize(m_editorFontSize);
-    baseEditPalette = QTextEdit().palette();
+    m_defaultEditPalette = QTextEdit().palette();
 
     m_editorStyle = getConfigFromSettings("global", "editor_style").toString();
 
@@ -345,15 +340,18 @@ void VConfigManager::updateMarkdownEditStyle()
         return;
     }
 
-    VStyleParser parser;
-    parser.parseMarkdownStyle(styleStr);
-    mdHighlightingStyles = parser.fetchMarkdownStyles(baseEditFont);
-    m_codeBlockStyles = parser.fetchCodeBlockStyles(baseEditFont);
     mdEditPalette = baseEditPalette;
     mdEditFont = baseEditFont;
+
+    VStyleParser parser;
+    parser.parseMarkdownStyle(styleStr);
+
     QMap<QString, QMap<QString, QString>> styles;
     parser.fetchMarkdownEditorStyles(mdEditPalette, mdEditFont, styles);
 
+    mdHighlightingStyles = parser.fetchMarkdownStyles(mdEditFont);
+    m_codeBlockStyles = parser.fetchCodeBlockStyles(mdEditFont);
+
     m_editorCurrentLineBg = defaultCurrentLineBackground;
     m_editorVimInsertBg = defaultVimInsertBg;
     m_editorVimNormalBg = defaultVimNormalBg;
@@ -400,7 +398,11 @@ void VConfigManager::updateMarkdownEditStyle()
 
 void VConfigManager::updateEditStyle()
 {
-    static const QColor defaultColor = baseEditPalette.color(QPalette::Base);
+    // Reset font and palette.
+    baseEditFont = mdEditFont = m_defaultEditFont;
+    baseEditPalette = mdEditPalette = m_defaultEditPalette;
+
+    static const QColor defaultColor = m_defaultEditPalette.color(QPalette::Base);
     QColor newColor = defaultColor;
     bool force = false;
     if (curBackgroundColor != "System") {
@@ -411,6 +413,7 @@ void VConfigManager::updateEditStyle()
                     newColor = QColor(VUtils::QRgbFromString(rgb));
                     force = true;
                 }
+
                 break;
             }
         }
@@ -421,6 +424,11 @@ void VConfigManager::updateEditStyle()
     // Update markdown editor palette
     updateMarkdownEditStyle();
 
+    // Base editor will use the same font size as the markdown editor by now.
+    if (mdEditFont.pointSize() > -1) {
+        baseEditFont.setPointSize(mdEditFont.pointSize());
+    }
+
     if (force) {
         mdEditPalette.setColor(QPalette::Base, newColor);
     }

+ 9 - 1
src/vconfigmanager.h

@@ -236,13 +236,21 @@ private:
     // the new one; if not, use the c_dirConfigFile.
     static QString fetchDirConfigFilePath(const QString &p_path);
 
-    int m_editorFontSize;
+    // Default font and palette.
+    QFont m_defaultEditFont;
+    QPalette m_defaultEditPalette;
+
+    // Font and palette used for non-markdown editor.
     QFont baseEditFont;
     QPalette baseEditPalette;
+
+    // Font and palette used for markdown editor.
     QFont mdEditFont;
     QPalette mdEditPalette;
+
     QVector<HighlightingStyle> mdHighlightingStyles;
     QHash<QString, QTextCharFormat> m_codeBlockStyles;
+
     QString welcomePagePath;
     QString m_templateCss;
     QString m_editorStyle;

+ 21 - 0
src/vstyleparser.cpp

@@ -187,6 +187,12 @@ void VStyleParser::fetchMarkdownEditorStyles(QPalette &palette, QFont &font,
 {
     QString ruleKey;
 
+    int basePointSize = font.pointSize();
+    if (basePointSize == -1) {
+        // The size is specified in pixel. Use 11 pt by default.
+        basePointSize = 11;
+    }
+
     // editor
     pmh_style_attribute *editorStyles = markdownStyles->editor_styles;
     ruleKey = "editor";
@@ -212,6 +218,21 @@ void VStyleParser::fetchMarkdownEditorStyles(QPalette &palette, QFont &font,
             break;
         }
 
+        case pmh_attr_type_font_size_pt:
+        {
+            pmh_attr_font_size *fontSize = editorStyles->value->font_size;
+            int ptSize = fontSize->size_pt;
+            if (fontSize->is_relative) {
+                ptSize += basePointSize;
+            }
+
+            if (ptSize > 0) {
+                font.setPointSize(ptSize);
+            }
+
+            break;
+        }
+
         // Get custom styles:
         //     trailing-space.
         case pmh_attr_type_other:

+ 3 - 0
src/vstyleparser.h

@@ -23,9 +23,12 @@ public:
 
     void parseMarkdownStyle(const QString &styleStr);
     QVector<HighlightingStyle> fetchMarkdownStyles(const QFont &baseFont) const;
+
+    // Fetch style sections: editor, editor-selection, editor-current-line.
     // @styles: [rule] -> ([attr] -> value).
     void fetchMarkdownEditorStyles(QPalette &palette, QFont &font,
                                    QMap<QString, QMap<QString, QString>> &styles) const;
+
     QHash<QString, QTextCharFormat> fetchCodeBlockStyles(const QFont &p_baseFont) const;
 
 private: