Skip to content

Commit

Permalink
Test place label with gloss
Browse files Browse the repository at this point in the history
  • Loading branch information
1ec5 committed Dec 3, 2022
1 parent 40835c1 commit 5d76fd3
Show file tree
Hide file tree
Showing 4 changed files with 284 additions and 121 deletions.
1 change: 1 addition & 0 deletions src/americana.js
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,7 @@ maplibregl.setRTLTextPlugin(
true
);

window.maplibregl = maplibregl;
export const map = (window.map = new maplibregl.Map({
container: "map", // container id
hash: "map",
Expand Down
126 changes: 125 additions & 1 deletion src/constants/label.js
Original file line number Diff line number Diff line change
Expand Up @@ -118,10 +118,134 @@ export function localizeLayers(layers, locales) {
}
}

// Placeholder to be resolved by buildLayers()
/**
* The name in the user's preferred language.
*/
export const localizedName = [
"let",
"localizedName",
"",
["var", "localizedName"],
];

/**
* The name in the user's preferred language, followed by the name in the local language in parentheses if it differs.
*/
export const localizedNameWithLocalGloss = [
"let",
"localizedName",
"",
"localizedCollator",
["collator", {}],
"diacriticInsensitiveCollator",
["collator", {}],
[
"case",
[
"==",
["var", "localizedName"],
["get", "name"],
["var", "localizedCollator"],
],
["var", "localizedName"],
// If the name in the preferred language is the same as the name in the local language except for the omission of diacritics and/or the addition of a suffix (e.g., "City" in English), replace the common prefix with the local name.
[
"all",
[
"==",
["slice", ["var", "localizedName"], 0, ["length", ["get", "name"]]],
["get", "name"],
["var", "diacriticInsensitiveCollator"],
],
[
"in",
[
"slice",
// "Montreal" vs. "Montréal"
["concat", ["var", "localizedName"], " "],
["length", ["get", "name"]],
["+", ["length", ["get", "name"]], 1],
],
// "Quebec City" vs. "Québec", "Washington, D.C." vs. "Washington"
" ,",
],
],
[
"concat",
["get", "name"],
["slice", ["var", "localizedName"], ["length", ["get", "name"]]],
],
// If the name in the preferred language is the same as the name in the local language except for the omission of diacritics and/or the addition of a prefix (e.g., "City of" in English or "Ciudad de" in Spanish), replace the common suffix with the local name.
[
"all",
[
"==",
[
"slice",
["var", "localizedName"],
[
"-",
["length", ["var", "localizedName"]],
["length", ["get", "name"]],
],
],
["get", "name"],
["var", "diacriticInsensitiveCollator"],
],
[
"==",
[
"slice",
["var", "localizedName"],
[
"-",
[
"-",
["length", ["var", "localizedName"]],
["length", ["get", "name"]],
],
1,
],
[
"-",
["length", ["var", "localizedName"]],
["length", ["get", "name"]],
],
],
" ",
],
],
[
"concat",
[
"slice",
["var", "localizedName"],
0,
[
"-",
["length", ["var", "localizedName"]],
["length", ["get", "name"]],
],
],
["get", "name"],
],
// Gloss the name in the local language if it differs from the localized name.
[
"format",
["var", "localizedName"],
"\n",
"(\u200B",
{ "font-scale": 0.8 },
// GL JS lacks support for bidirectional isolating characters, so use a character from the localized name to insulate the parentheses from the embedded text's writing direction. Make it so small that GL JS doesn't bother rendering it.
["concat", ["slice", ["var", "localizedName"], 0, 1], " "],
{ "font-scale": 0.001 },
["get", "name"],
{ "font-scale": 0.8 },
["concat", " ", ["slice", ["var", "localizedName"], 0, 1]],
{ "font-scale": 0.001 },
// A ZWSP prevents GL JS from combining this component with the preceding one, which would cause it to vanish along with the faux isolating character.
"\u200B)",
{ "font-scale": 0.8 },
],
],
];
119 changes: 1 addition & 118 deletions src/layer/place.js
Original file line number Diff line number Diff line change
Expand Up @@ -181,124 +181,7 @@ export const city = {
[11, 0.9],
],
},
"text-field": [
"let",
"localizedName",
"",
"localizedCollator",
["collator", {}],
"diacriticInsensitiveCollator",
["collator", {}],
[
"case",
[
"==",
["var", "localizedName"],
["get", "name"],
["var", "localizedCollator"],
],
["var", "localizedName"],
// If the name in the preferred language is the same as the name in the local language except for the omission of diacritics and/or the addition of a suffix (e.g., "City" in English), replace the common prefix with the local name.
[
"all",
[
"==",
["slice", ["var", "localizedName"], 0, ["length", ["get", "name"]]],
["get", "name"],
["var", "diacriticInsensitiveCollator"],
],
[
"in",
[
"slice",
// "Montreal" vs. "Montréal"
["concat", ["var", "localizedName"], " "],
["length", ["get", "name"]],
["+", ["length", ["get", "name"]], 1],
],
// "Quebec City" vs. "Québec", "Washington, D.C." vs. "Washington"
" ,",
],
],
[
"concat",
["get", "name"],
["slice", ["var", "localizedName"], ["length", ["get", "name"]]],
],
// If the name in the preferred language is the same as the name in the local language except for the omission of diacritics and/or the addition of a prefix (e.g., "City of" in English or "Ciudad de" in Spanish), replace the common suffix with the local name.
[
"all",
[
"==",
[
"slice",
["var", "localizedName"],
[
"-",
["length", ["var", "localizedName"]],
["length", ["get", "name"]],
],
],
["get", "name"],
["var", "diacriticInsensitiveCollator"],
],
[
"==",
[
"slice",
["var", "localizedName"],
[
"-",
[
"-",
["length", ["var", "localizedName"]],
["length", ["get", "name"]],
],
1,
],
[
"-",
["length", ["var", "localizedName"]],
["length", ["get", "name"]],
],
],
" ",
],
],
[
"concat",
[
"slice",
["var", "localizedName"],
0,
[
"-",
["length", ["var", "localizedName"]],
["length", ["get", "name"]],
],
],
["get", "name"],
],
// Gloss the name in the local language if it differs from the localized name.
[
"format",
["var", "localizedName"],
"\n",
"(\u200B",
{ "font-scale": 0.8 },
// GL JS lacks support for bidirectional isolating characters, so use a character from the localized name to insulate the parentheses from the embedded text's writing direction. Make it so small that GL JS doesn't bother rendering it.
["concat", ["slice", ["var", "localizedName"], 0, 1], " "],
{ "font-scale": 0.001 },
["get", "name"],
{ "font-scale": 0.8 },
["concat", " ", ["slice", ["var", "localizedName"], 0, 1]],
{ "font-scale": 0.001 },
// A ZWSP prevents GL JS from combining this component with the preceding one, which would cause it to vanish along with the faux isolating character.
"\u200B)",
{ "font-scale": 0.8 },
],
],
],
"text-field": Label.localizedNameWithLocalGloss,
"text-anchor": "bottom",
"text-variable-anchor": [
"bottom",
Expand Down
Loading

0 comments on commit 5d76fd3

Please sign in to comment.