Skip to content

Instantly share code, notes, and snippets.

@yu-tang
Last active October 16, 2017 07:47
Show Gist options
  • Save yu-tang/5f57a262d7b49666ae3ba583166b7d26 to your computer and use it in GitHub Desktop.
Save yu-tang/5f57a262d7b49666ae3ba583166b7d26 to your computer and use it in GitHub Desktop.
Make the bold font to plain in the source of active segment
/* :name=Make the bold font to plain in the source of active segment
*
* Usage: move this script to
* <your-scripts-folder>/application_startup/ sub-folder
* for event driven automatically execution.
*
* @author Yu Tang
* @date 2017-04-07
* @version 0.2.1
*/
package me.goat.groovy.scripting
import org.omegat.core.CoreEvents
import org.omegat.core.data.SourceTextEntry
import org.omegat.core.events.IEntryEventListener
import org.omegat.gui.editor.IEditor
import org.omegat.gui.editor.SegmentBuilder
import org.omegat.gui.scripting.IScriptLogger
import org.omegat.util.gui.UIThreadsUtil
import javax.swing.SwingUtilities
import javax.swing.text.AbstractDocument.LeafElement
import javax.swing.text.SimpleAttributeSet
import javax.swing.text.StyleConstants
import java.awt.event.KeyAdapter
import java.awt.event.KeyEvent
import static javax.swing.text.StyleConstants.FontConstants.Bold
BoldRemover remover = new BoldRemover(console: console, editor: editor)
// add the listener
editor.editor.addKeyListener remover
CoreEvents.registerEntryEventListener remover
class BoldRemover extends KeyAdapter implements IEntryEventListener {
IScriptLogger console
IEditor editor
@Override
void onNewFile(String activeFileName) {
// do nothing
}
@Override
void onEntryActivated(SourceTextEntry newEntry) {
SwingUtilities.invokeLater {
setCurrentSourceBoldOff()
}
}
@Override
void keyTyped(KeyEvent keyEvent) {
SwingUtilities.invokeLater {
SwingUtilities.invokeLater {
setCurrentSourceBoldOff()
}
}
}
private void setCurrentSourceBoldOff() {
UIThreadsUtil.mustBeSwingThread()
def doc = getDocument()
def para = doc.currentParagraph
def elFirst = para.getElement(0)
if (! elFirst.attributes.containsAttribute(Bold, true)) {
return // Target element does not have 'Bold: true' attribute."
}
def elLast = para.getElement(para.elementCount - 2)
doc.setBoldOff(elLast)
}
private def getDocument() {
def sb = editor.currentSegmentBuilder
def doc = sb.doc
int offset = sb.startSourcePosition
doc.metaClass.setBoldOff = { LeafElement end ->
delegate.trustedChangesInProgress = true;
//StaticUIUtils.setCaretUpdateEnabled(editor.editor as JTextComponent, false);
try {
int len = end.endOffset - offset
def boldOff = new SimpleAttributeSet()
StyleConstants.setBold(boldOff, false)
boolean isReplace = false // == merge
delegate.setCharacterAttributes(offset, len, boldOff, isReplace)
} finally {
delegate.trustedChangesInProgress = false;
//StaticUIUtils.setCaretUpdateEnabled(editor.editor as JTextComponent, true);
}
}
doc.metaClass.getCurrentParagraph = { ->
delegate.getParagraphElement(offset)
}
doc
}
}
@eric74
Copy link

eric74 commented Oct 16, 2017

Works great (using ver.4.1.2). Now kanji don't look like ink blobs! Thanks.
Question: Is there any way to change the font size by adding something? (tried to figure it out myself but couldn't).
I'd like to set the [fuzzy][machine][glossary] panels to a small font, and [editor] panel only to large font. My idea was to set a small font in settings (changes everywhere) then just set a larger font here (changes just the editor). Please help if possible.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment