From eeaeb0b567d45874f13c0f04e7fe76965674d5e2 Mon Sep 17 00:00:00 2001 From: Le Tan Date: Wed, 28 Jun 2017 20:08:01 +0800 Subject: [PATCH] enable font-size in editor section in MDHL file --- src/main.cpp | 5 ++++ src/resources/styles/default.mdhl | 19 +++++++++++--- src/resources/styles/solarized-dark.mdhl | 8 ++++-- src/resources/styles/solarized-light.mdhl | 8 ++++-- src/resources/vnote.ini | 1 - src/vconfigmanager.cpp | 30 ++++++++++++++--------- src/vconfigmanager.h | 10 +++++++- src/vstyleparser.cpp | 21 ++++++++++++++++ src/vstyleparser.h | 3 +++ 9 files changed, 85 insertions(+), 20 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index 8d866316..6b43355c 100644 --- a/src/main.cpp +++ b/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(); diff --git a/src/resources/styles/default.mdhl b/src/resources/styles/default.mdhl index f9aa37a1..e14c7942 100644 --- a/src/resources/styles/default.mdhl +++ b/src/resources/styles/default.mdhl @@ -1,12 +1,20 @@ # This is the default markdown styles used for Peg-Markdown-Highlight -# Created by Le Tan (tamlokveer@gmail.com) +# created by Le Tan(tamlokveer@gmail.com). +# 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 diff --git a/src/resources/styles/solarized-dark.mdhl b/src/resources/styles/solarized-dark.mdhl index 65a5b979..3d86f513 100644 --- a/src/resources/styles/solarized-dark.mdhl +++ b/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 diff --git a/src/resources/styles/solarized-light.mdhl b/src/resources/styles/solarized-light.mdhl index 23b0663b..876da8d9 100644 --- a/src/resources/styles/solarized-light.mdhl +++ b/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 diff --git a/src/resources/vnote.ini b/src/resources/vnote.ini index 73484476..e35ec256 100644 --- a/src/resources/vnote.ini +++ b/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 diff --git a/src/vconfigmanager.cpp b/src/vconfigmanager.cpp index 6eb70d51..1e9d19d4 100644 --- a/src/vconfigmanager.cpp +++ b/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> 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); } diff --git a/src/vconfigmanager.h b/src/vconfigmanager.h index b590b9d8..3d7f3975 100644 --- a/src/vconfigmanager.h +++ b/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 mdHighlightingStyles; QHash m_codeBlockStyles; + QString welcomePagePath; QString m_templateCss; QString m_editorStyle; diff --git a/src/vstyleparser.cpp b/src/vstyleparser.cpp index ec3de84e..0450aa8d 100644 --- a/src/vstyleparser.cpp +++ b/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: diff --git a/src/vstyleparser.h b/src/vstyleparser.h index 4f2be691..f7a49d37 100644 --- a/src/vstyleparser.h +++ b/src/vstyleparser.h @@ -23,9 +23,12 @@ public: void parseMarkdownStyle(const QString &styleStr); QVector 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> &styles) const; + QHash fetchCodeBlockStyles(const QFont &p_baseFont) const; private: