void ShowSupportedScripts(InstalledTypeface installedTypeface) { //show supported lang lstScriptLangs.Items.Clear(); Dictionary <string, ScriptLang> dic = new Dictionary <string, ScriptLang>(); installedTypeface.CollectScriptLang(dic); foreach (var kv in dic) { ScriptLang s = kv.Value; if (s.sysLangTag != 0 && LanguageSystemNames.TryGetLangSystem(s.GetScriptTagString(), out LangSys found)) { } else { } lstScriptLangs.Items.Add(s); } lstSupportedUnicodeLangs.Items.Clear(); foreach (BitposAndAssciatedUnicodeRanges bitposAndUnicodeRanges in installedTypeface.GetSupportedUnicodeLangIter()) { lstSupportedUnicodeLangs.Items.Add(bitposAndUnicodeRanges.ToString()); } }
static bool TryGetScriptLangFromCurrentThreadCultureInfo(out Typography.OpenFont.ScriptLang scLang) { var currentCulture = System.Threading.Thread.CurrentThread.CurrentCulture; if (Typography.TextBreak.IcuData.TryGetFullLanguageNameFromLangCode( currentCulture.TwoLetterISOLanguageName, currentCulture.ThreeLetterISOLanguageName, out string langFullName)) { Typography.OpenFont.ScriptLangInfo scLang1 = Typography.OpenFont.ScriptLangs.GetRegisteredScriptLangFromLanguageName(langFullName); if (scLang1 == null) { //not found -> use default latin //use default lang #if DEBUG System.Diagnostics.Debug.WriteLine(langFullName + " :use latin"); #endif scLang = s_latin; return(true); } else { scLang = new ScriptLang(scLang1.shortname);// scLang1.GetScriptLang(); return(true); } } else { scLang = default; } return(false); }
public TextShapingContext(Typeface typeface, ScriptLang scLang) { _typeface = typeface; _scLang = scLang; _glyphPlanBuffer = new GlyphPlanBuffer(new GlyphPlanList()); _glyphPlanSeqSet = new GlyphPlanSeqSet(); }
public MyLineSegment(ILineSegmentList owner, int startAt, int len) { _owner = owner; _startAt = startAt; _len = len; this.scriptLang = null; }
public void AddScriptLangAndHint(string lang, string hint) { ScriptLang scriptLang = new ScriptLang(ScriptLangs.GetRegisteredScriptLangFromLanguageName(lang).shortname); if (scriptLang.IsEmpty()) { //not found this lang System.Diagnostics.Debugger.Break(); } else { GlyphTextureBuildDetail buildDetail = new GlyphTextureBuildDetail(); buildDetail.ScriptLang = scriptLang; switch (hint) { case "truetype": buildDetail.HintTechnique = HintTechnique.TrueTypeInstruction; break; case "truetype_vertical_only": buildDetail.HintTechnique = HintTechnique.TrueTypeInstruction_VerticalOnly; break; case "cff": buildDetail.HintTechnique = HintTechnique.CffHintInstruction; break; } _scriptDic[lang] = buildDetail; //replace } }
//------------- public TextPrinter() { FontSizeInPoints = 14; ScriptLang = new ScriptLang("latn"); // _curveFlattener = new SimpleCurveFlattener(); _tessTool = new Tesselate.TessTool(); }
/// <summary> /// get glyph layout plan or create if not exists /// </summary> /// <param name="typeface"></param> /// <param name="scriptLang"></param> /// <returns></returns> public GlyphLayoutPlanContext GetPlanOrCreate(Typeface typeface, ScriptLang scriptLang) { GlyphLayoutPlanKey key = new GlyphLayoutPlanKey(typeface, scriptLang.internalName); if (!_collection.TryGetValue(key, out GlyphLayoutPlanContext context)) { var glyphSubstitution = (typeface.GSUBTable != null) ? new GlyphSubstitution(typeface, scriptLang.shortname) : null; var glyphPosition = (typeface.GPOSTable != null) ? new GlyphSetPosition(typeface, scriptLang.shortname) : null; _collection.Add(key, context = new GlyphLayoutPlanContext(glyphSubstitution, glyphPosition)); } return(context); }
void TrySetupCurrentScriptLang() { //set script-lang ScriptLang scLang = DefaultScriptLang; //--------------- //if not default then try guess // if (scLang.scriptTag == 0 && !TryGetScriptLangFromCurrentThreadCultureInfo(out scLang)) { //TODO: handle error here throw new NotSupportedException(); } }
public OpenFontTextService(ScriptLang scLang = null) { _system_id = PixelFarm.Drawing.Internal.RequestFontCacheAccess.GetNewCacheSystemId(); //set up typography text service _txtServices = new TextServices(); //default, user can set this later _txtServices.InstalledFontCollection = InstalledTypefaceCollection.GetSharedTypefaceCollection(collection => { collection.SetFontNameDuplicatedHandler((f0, f1) => FontNameDuplicatedDecision.Skip); }); //create typography service //you can implement this service on your own //just see the code inside the service //script lang has a potentail effect on how the layout engine instance work. // //so try to set default script lang to the layout engine instance //from current system default value... //user can set this to other choices... //eg. directly specific the script lang //set script-lang if (scLang == null) { //use default scLang = DefaultScriptLang; } // if not default then try guess if (scLang == null && !TryGetScriptLangFromCurrentThreadCultureInfo(out scLang)) { //TODO: handle error here } _txtServices.SetDefaultScriptLang(scLang); _txtServices.CurrentScriptLang = scLang; // ... or specific the scriptlang manully, eg. ... //_shapingServices.SetDefaultScriptLang(scLang); //_shapingServices.SetCurrentScriptLang(scLang); //--------------- }
private static TemplateContext NewTemplateContext(ScriptLang lang) { var isLiquid = lang == ScriptLang.Liquid; var context = isLiquid ? new LiquidTemplateContext() { TemplateLoader = new LiquidCustomTemplateLoader() } : new TemplateContext() { TemplateLoader = new CustomTemplateLoader() }; if (lang == ScriptLang.Scientific) { context.UseScientific = true; } // We use a custom output to make sure that all output is using the "\n" context.NewLine = "\n"; return(context); }
public bool TrySettingScriptLangFromCurrentThreadCultureInfo() { //accessory... var currentCulture = System.Threading.Thread.CurrentThread.CurrentCulture; Typography.OpenFont.ScriptLang scLang = null; string langFullName; if (IcuData.TryGetFullLanguageNameFromLangCode( currentCulture.TwoLetterISOLanguageName, currentCulture.ThreeLetterISOLanguageName, out langFullName)) { scLang = Typography.OpenFont.ScriptLangs.GetRegisteredScriptLangFromLanguageName(langFullName); SetDefaultScriptLang(scLang); CurrentScriptLang = scLang; return(true); } throw new System.NotSupportedException(); return(false); }
public static void AssertTemplate(string expected, string input, ScriptLang lang = ScriptLang.Default, bool isRoundtripTest = false, bool supportExactRoundtrip = true, object model = null, bool specialLiquid = false, bool expectParsingErrorForRountrip = false, bool supportRoundTrip = true) { bool isLiquid = lang == ScriptLang.Liquid; var parserOptions = new ParserOptions() { LiquidFunctionsToScriban = isLiquid, }; var lexerOptions = new LexerOptions() { Lang = lang }; if (isRoundtripTest) { lexerOptions.KeepTrivia = true; } if (specialLiquid) { parserOptions.ExpressionDepthLimit = 500; } #if EnableTokensOutput { Console.WriteLine("Tokens"); Console.WriteLine("======================================"); var lexer = new Lexer(input, options: lexerOptions); foreach (var token in lexer) { Console.WriteLine($"{token.Type}: {token.GetText(input)}"); } Console.WriteLine(); } #endif string roundtripText = null; // We loop first on input text, then on roundtrip while (true) { bool isRoundtrip = roundtripText != null; bool hasErrors = false; bool hasException = false; if (isRoundtrip) { Console.WriteLine("Roundtrip"); Console.WriteLine("======================================"); Console.WriteLine(roundtripText); lexerOptions.Lang = lang == ScriptLang.Scientific ? lang : ScriptLang.Default; if (!isLiquid && supportExactRoundtrip) { Console.WriteLine("Checking Exact Roundtrip - Input"); Console.WriteLine("======================================"); TextAssert.AreEqual(input, roundtripText); } input = roundtripText; } else { Console.WriteLine("Input"); Console.WriteLine("======================================"); Console.WriteLine(input); } var template = Template.Parse(input, "text", parserOptions, lexerOptions); var result = string.Empty; var resultAsync = string.Empty; if (template.HasErrors) { hasErrors = true; for (int i = 0; i < template.Messages.Count; i++) { var message = template.Messages[i]; if (i > 0) { result += "\n"; } result += message; } if (specialLiquid && !isRoundtrip) { throw new InvalidOperationException("Parser errors: " + result); } } else { if (isRoundtripTest) { result = template.ToText(); } else { Assert.NotNull(template.Page); if (!isRoundtrip) { // Dumps the roundtrip version var lexerOptionsForTrivia = lexerOptions; lexerOptionsForTrivia.KeepTrivia = true; var templateWithTrivia = Template.Parse(input, "input", parserOptions, lexerOptionsForTrivia); roundtripText = templateWithTrivia.ToText(); } try { // Setup a default model context for the tests if (model == null) { var scriptObj = new ScriptObject { ["page"] = new ScriptObject { ["title"] = "This is a title" }, ["user"] = new ScriptObject { ["name"] = "John" }, ["product"] = new ScriptObject { ["title"] = "Orange", ["type"] = "fruit" }, ["products"] = new ScriptArray() { new ScriptObject { ["title"] = "Orange", ["type"] = "fruit" }, new ScriptObject { ["title"] = "Banana", ["type"] = "fruit" }, new ScriptObject { ["title"] = "Apple", ["type"] = "fruit" }, new ScriptObject { ["title"] = "Computer", ["type"] = "electronics" }, new ScriptObject { ["title"] = "Mobile Phone", ["type"] = "electronics" }, new ScriptObject { ["title"] = "Table", ["type"] = "furniture" }, new ScriptObject { ["title"] = "Sofa", ["type"] = "furniture" }, } }; scriptObj.Import(typeof(SpecialFunctionProvider)); model = scriptObj; } // Render sync { var context = NewTemplateContext(lang); context.PushOutput(new TextWriterOutput(new StringWriter() { NewLine = "\n" })); var contextObj = new ScriptObject(); contextObj.Import(model); context.PushGlobal(contextObj); result = template.Render(context); } // Render async { var asyncContext = NewTemplateContext(lang); asyncContext.PushOutput(new TextWriterOutput(new StringWriter() { NewLine = "\n" })); var contextObj = new ScriptObject(); contextObj.Import(model); asyncContext.PushGlobal(contextObj); resultAsync = template.RenderAsync(asyncContext).Result; } } catch (Exception exception) { hasException = true; if (specialLiquid) { throw; } else { result = GetReason(exception); } } } } var testContext = isRoundtrip ? "Roundtrip - " : String.Empty; Console.WriteLine($"{testContext}Result"); Console.WriteLine("======================================"); Console.WriteLine(result); Console.WriteLine($"{testContext}Expected"); Console.WriteLine("======================================"); Console.WriteLine(expected); if (isRoundtrip && expectParsingErrorForRountrip) { Assert.True(hasErrors, "The roundtrip test is expecting an error"); Assert.AreNotEqual(expected, result); } else { TextAssert.AreEqual(expected, result); } if (!isRoundtrip && !isRoundtripTest && !hasErrors && !hasException) { Console.WriteLine("Checking async"); Console.WriteLine("======================================"); TextAssert.AreEqual(expected, resultAsync); } if (!supportRoundTrip || isRoundtripTest || isRoundtrip || hasErrors) { break; } } }
/// <summary> /// Language of script. /// </summary> /// <param name="lang">language</param> /// <returns>this</returns> public ScriptQueryDescriptor <T> Lang(ScriptLang lang) => Assign(a => a.Lang = lang.GetStringValue());
public void SetDefaultScriptLang(ScriptLang scLang) { this.scLang = _defaultScriptLang = scLang; }
public TextShapingContextKey(Typeface typeface, ScriptLang scLang) { this._typeface = typeface; this._scLang = scLang; }
public MyLineSegment(int startAt, int len) { _startAt = startAt; _len = len; this.scriptLang = null; }
public MappingTransformDescriptor Language(ScriptLang language) { this._mappingTransform.Language = language.GetStringValue(); return(this); }
public AbstractTextSpanPrinter() { ScriptLang = new ScriptLang(ScriptTagDefs.Latin.Tag); }
public TextShapingContextKey(InstalledFont installedFont, ScriptLang scLang) { this._installedFont = installedFont; this._scLang = scLang; }
public void SetCurrentScriptLang(ScriptLang scLang) { _glyphLayout.ScriptLang = scLang; }
public void SetCurrentFont(string fontname, InstalledFontStyle fontStyle, float fontSizeInPts, ScriptLang scLang = null) { InstalledFont installedFont = _hub._openFontStore.GetFont(fontname, fontStyle); if (installedFont == null) { return; //not found request font } if (scLang != null) { _glyphLayout.ScriptLang = scLang; } var key = new TextShapingContextKey(installedFont, _glyphLayout.ScriptLang); if (!_registerShapingContexts.TryGetValue(key, out _currentShapingContext)) { //not found //the create the new one Typeface typeface; using (var fs = new System.IO.FileStream(installedFont.FontPath, System.IO.FileMode.Open, System.IO.FileAccess.Read)) { var reader = new OpenFontReader(); typeface = reader.Read(fs); } var shapingContext = new TextShapingContext(typeface, _glyphLayout.ScriptLang); //shaping context setup ... _registerShapingContexts.Add(key, shapingContext); _currentShapingContext = shapingContext; } _fontSizeInPts = fontSizeInPts; }
public TextPrinterBase() { FontSizeInPoints = 14;// ScriptLang = new ScriptLang(ScriptTagDefs.Latin.Tag); }
public ScriptFormatterOptions(TemplateContext context, ScriptLang language, ScriptFormatterFlags flags) { Language = language; Flags = flags; Context = context == null && language == ScriptLang.Scientific ? throw new ArgumentNullException(nameof(context), "Context cannot be null with scientific language.") : context; }
public CustomScoreQueryDescriptor <T> Lang(ScriptLang lang) { ((ICustomScoreQuery)this).Lang = lang.GetStringValue(); return(this); }
public static void CollectAllAssociateGlyphIndex(this TtfTypeface typeface, List <ushort> outputGlyphIndexList, ScriptLang scLang, UnicodeLangBits[] selectedRangs = null) { //----------- //general glyph index in the unicode range //if user dose not specific the unicode lanf bit ranges //the we try to select it ourself. UnicodeLangBits[] unicodeLangBitsRanges; if (ScriptLangs.TryGenUnicodeLangBitsArray(scLang.shortname, out unicodeLangBitsRanges)) { //one lang may contains may ranges if (selectedRangs != null) { //select only in range unicodeLangBitsRanges = FilterOnlySelectedRange(unicodeLangBitsRanges, selectedRangs); } foreach (UnicodeLangBits unicodeLangBits in unicodeLangBitsRanges) { UnicodeRangeInfo rngInfo = unicodeLangBits.ToUnicodeRangeInfo(); int endAt = rngInfo.EndAt; for (int codePoint = rngInfo.StartAt; codePoint <= endAt; ++codePoint) { ushort glyghIndex = typeface.LookupIndex(codePoint); if (glyghIndex > 0) { //add this glyph index outputGlyphIndexList.Add(glyghIndex); } } } } //----------- var gsub = new GlyphSubstitution(typeface, scLang.shortname); gsub.CollectAdditionalSubstitutionGlyphIndices(outputGlyphIndexList); }
public MappingTransformDescriptor Language(ScriptLang language) { this._mappingTransform.Language = language.GetStringValue(); return this; }
public GlyphPlanCacheForTypefaceAndScriptLang(Typeface typeface, ScriptLang scLang) { _typeface = typeface; _scLang = scLang; _glyphPlanSeqSet = new GlyphPlanSeqSet(); }
public PutScriptDescriptor Lang(ScriptLang lang) { this.Self.Lang = lang.GetStringValue(); return(this); }
/// <summary> /// Language of script. /// </summary> /// <param name="lang">language</param> /// <returns>this</returns> public TDescriptor Lang(ScriptLang lang) => Assign(a => a.Lang = lang.GetStringValue());
public MappingTransformDescriptor Language(ScriptLang language) => Assign(a => a.Language = language.GetStringValue());
/// <inheritdoc cref="IScript.Lang" /> public TDescriptor Lang(ScriptLang lang) => Assign(lang.GetStringValue(), (a, v) => a.Lang = v);
/// <summary> /// Language of script. /// </summary> /// <param name="lang">language</param> /// <returns>this</returns> public ScriptFilterDescriptor Lang(ScriptLang lang) { lang.ThrowIfNull("lang"); ((IScriptFilter)this).Lang = lang.GetStringValue(); return this; }